An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: Improve Enter behavior for child blocks in list items https://github.com/siyuan-note/siyuan/issues/18896
Daniel02b318cc3a8a
diff --git a/app/src/protyle/wysiwyg/enter.ts b/app/src/protyle/wysiwyg/enter.tsindex f66d2caf6d..92bfa926b3 100644--- a/app/src/protyle/wysiwyg/enter.ts+++ b/app/src/protyle/wysiwyg/enter.ts@@ -32,6 +32,7 @@ import {processRender} from "../util/processCode";import {hasClosestByAttribute, hasClosestByClassName} from "../util/hasClosest";import {blockRender} from "../render/blockRender";import {isCodeBlockFenceBeforeCaret} from "./codeBlockEnter";+import {isEmptyListItemBlock, shouldCreateListItemChildOnEnter} from "./listContext";export const enter = async (blockElement: HTMLElement, range: Range, protyle: IProtyle) => {const embedResultElement = hasClosestByClassName(blockElement, "protyle-wysiwyg__embed");@@ -451,9 +452,8 @@ const getEmbedListEnterMode = (blockElement: HTMLElement, embedContext: IEmbedCh}const listElement = listItemElement.parentElement;const editableElement = getContenteditableElement(blockElement);- const exitsList = ["", "\n"].includes(editableElement.textContent) &&- blockElement.previousElementSibling.classList.contains("protyle-action") &&- !blockElement.querySelector("img");+ const exitsList = isEmptyListItemBlock(editableElement.textContent, !!blockElement.querySelector("img")) &&+ blockElement.previousElementSibling.classList.contains("protyle-action");if (exitsList && listElement.parentElement === embedContext.resultElement) {return "none";}@@ -463,10 +463,16 @@ const getEmbedListEnterMode = (blockElement: HTMLElement, embedContext: IEmbedChconst listEnter = async (protyle: IProtyle, blockElement: HTMLElement, range: Range) => {const listItemElement = blockElement.parentElement;const editableElement = getContenteditableElement(blockElement);+ const isPrimaryBlock = blockElement.previousElementSibling.classList.contains("protyle-action");+ const isEmptyBlock = isEmptyListItemBlock(editableElement.textContent, !!blockElement.querySelector("img"));+ // 列表项的非空末尾子块按普通块处理,空子块再次回车时仍创建后续列表项。+ if (shouldCreateListItemChildOnEnter(isPrimaryBlock,+ blockElement.nextElementSibling.classList.contains("protyle-attr"), isEmptyBlock)) {+ return false;+ }if (// \n 是因为 https://github.com/siyuan-note/siyuan/issues/3846- ["", "\n"].includes(editableElement.textContent) &&- blockElement.previousElementSibling.classList.contains("protyle-action") &&- !blockElement.querySelector("img") // https://ld246.com/article/1651820644238+ isEmptyBlock &&+ isPrimaryBlock // https://ld246.com/article/1651820644238) {if (listItemElement.nextElementSibling?.classList.contains("protyle-attr")) {await listOutdent(protyle, [blockElement.parentElement], range);@@ -494,15 +500,15 @@ const listEnter = async (protyle: IProtyle, blockElement: HTMLElement, range: Raconst listStart = getOrderedListStart(listElement);wbrElement.remove();let newElement = genListItemElement(listItemElement, -1, true);- if (!blockElement.previousElementSibling.classList.contains("protyle-action")) {+ if (!isPrimaryBlock) {// 列表项中有多个块,最后一个块为空,换行应进行缩进- if (getContenteditableElement(blockElement).textContent !== "") {+ if (!isEmptyBlock) {return false;}blockElement.remove();newElement = genListItemElement(listItemElement, -1, true);listItemElement.insertAdjacentElement("afterend", newElement);- } else if (getContenteditableElement(blockElement).textContent === "") {+ } else if (isEmptyBlock) {listItemElement.insertAdjacentElement("afterend", newElement);} else {listItemElement.insertAdjacentElement("beforebegin", newElement);diff --git a/app/src/protyle/wysiwyg/listContext.test.ts b/app/src/protyle/wysiwyg/listContext.test.tsindex 692dadccc7..7636c41b6c 100644--- a/app/src/protyle/wysiwyg/listContext.test.ts+++ b/app/src/protyle/wysiwyg/listContext.test.ts@@ -11,8 +11,10 @@ import {getOrderedListMaxStart,getPreviousListItemID,getListShortcutAction,+ isEmptyListItemBlock,isListItemActionElement,parseOrderedListStart,+ shouldCreateListItemChildOnEnter,shouldIgnoreListShortcut,shouldOpenListItemAttr,type TListSubtype@@ -266,6 +268,30 @@ describe("getListShortcutAction", () => {});});+describe("isEmptyListItemBlock", () => {+ it("treats empty text and a single soft line as empty", () => {+ assert.equal(isEmptyListItemBlock("", false), true);+ assert.equal(isEmptyListItemBlock("\n", false), true);+ });++ it("treats text and images as content", () => {+ assert.equal(isEmptyListItemBlock("content", false), false);+ assert.equal(isEmptyListItemBlock("", true), false);+ });+});++describe("shouldCreateListItemChildOnEnter", () => {+ it("uses normal block creation for a non-empty trailing child", () => {+ assert.equal(shouldCreateListItemChildOnEnter(false, true, false), true);+ });++ it("keeps list handling for primary, non-trailing, and empty blocks", () => {+ assert.equal(shouldCreateListItemChildOnEnter(true, true, false), false);+ assert.equal(shouldCreateListItemChildOnEnter(false, false, false), false);+ assert.equal(shouldCreateListItemChildOnEnter(false, true, true), false);+ });+});+describe("getFollowingOrderedListMarkerUpdates", () => {it("increments following markers after inserting a list item", () => {assert.deepEqual(getFollowingOrderedListMarkerUpdates("1.", ["2.", "3."]), ["3.", "4."]);diff --git a/app/src/protyle/wysiwyg/listContext.ts b/app/src/protyle/wysiwyg/listContext.tsindex 2d52a54c01..bf5edfcca3 100644--- a/app/src/protyle/wysiwyg/listContext.ts+++ b/app/src/protyle/wysiwyg/listContext.ts@@ -117,6 +117,13 @@ export const getListShortcutAction = (context: TListContext, targetSubtype: TLisreturn "insertChildList";};+export const isEmptyListItemBlock = (textContent: string, hasImage: boolean) =>+ ["", "\n"].includes(textContent) && !hasImage;++export const shouldCreateListItemChildOnEnter = (isPrimaryBlock: boolean, isLastBlock: boolean,+ isEmptyBlock: boolean) =>+ !isPrimaryBlock && isLastBlock && !isEmptyBlock;+export const getFollowingOrderedListMarkerUpdates = (currentMarker: string, followingMarkers: string[]) => {const currentIndex = Number.parseInt(currentMarker, 10);if (Number.isNaN(currentIndex)) {