Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:sparkles: Improve list conversion menu ordering and recursive removal https://github.com/siyuan-note/siyuan/issues/19899

Daniel9c40f4a4b15c
diff --git a/app/src/config/entryVisibility/catalog.test.ts b/app/src/config/entryVisibility/catalog.test.ts
index 09d605b9e4..f5bd614df9 100644
--- a/app/src/config/entryVisibility/catalog.test.ts
+++ b/app/src/config/entryVisibility/catalog.test.ts
@@ -38,9 +38,9 @@ import {
} from "./catalog";
import {getBuiltinProfileEntryVisibility} from "./profile";
-test("remove list follows paragraph conversion in single and multiple block menus", () => {
+test("remove list leads single and multiple block conversion menus", () => {
for (const path of ["gutter.single.turnInto", "gutter.multi.turnInto"]) {
- assert.deepEqual(getEntryCatalogChildren(path).slice(0, 2).map(item => item.key), ["paragraph", "removeList"]);
+ assert.deepEqual(getEntryCatalogChildren(path).slice(0, 2).map(item => item.key), ["removeList", "list"]);
const entry = getEntryCatalogNode(`${path}.removeList`);
assert.equal(entry.type, "entry");
assert.equal(entry.simple, true);
@@ -681,10 +681,10 @@ test("heading conversions follow list conversions across block menu scopes", ()
const headingKeys = ["heading1", "heading2", "heading3", "heading4", "heading5", "heading6"];
["gutter.single.turnInto", "gutter.multi.turnInto"].forEach((path) => {
const keys = getEntryCatalogChildren(path).map(item => item.key);
- assert.equal(keys[0], "paragraph");
+ assert.equal(keys[0], "removeList");
assert.equal(getEntryCatalogNode(`${path}.paragraph`)?.simple, true);
const headingIndex = keys.indexOf("heading1");
- assert.equal(headingIndex, keys.indexOf(path === "gutter.single.turnInto" ? "listMindmap" : "check") + 1);
+ assert.equal(headingIndex, keys.indexOf(path === "gutter.single.turnInto" ? "tabs" : "calloutCustom") + 1);
assert.deepEqual(keys.slice(headingIndex, headingIndex + headingKeys.length), headingKeys);
headingKeys.forEach(key => {
const entry = getEntryCatalogNode(`${path}.${key}`);
@@ -697,7 +697,7 @@ test("heading conversions follow list conversions across block menu scopes", ()
test("tab conversion belongs to the single block conversion menu", () => {
const keys = getEntryCatalogChildren("gutter.single.turnInto").map(item => item.key);
assert.equal(keys[keys.indexOf("tabs") - 1], "calloutCustom");
- assert.equal(keys[keys.indexOf("tabs") + 1], "list");
+ assert.equal(keys[keys.indexOf("tabs") + 1], "heading1");
assert.equal(getEntryCatalogNode("gutter.single.turnInto.tabs")?.simple, true);
assert.equal(getEntryCatalogNode("gutter.single.turnInto.tabs")?.type, "entry");
assert.equal(getEntryCatalogNode("gutter.multi.turnInto.tabs"), undefined);
@@ -1311,6 +1311,21 @@ test("simple profile follows the reviewed defaults", () => {
hidden.forEach((path) => assert.equal(getEntryCatalogNode(path)?.simple, false, path));
});
+test("list conversion prioritizes list actions and keeps recursive entry identities", () => {
+ for (const root of ["gutter.single", "gutter.multi"]) {
+ const keys = getEntryCatalogChildren(`${root}.turnInto`).map(item => item.key);
+ assert.deepEqual(keys.slice(0, root === "gutter.single" ? 7 : 6), [
+ "removeList", "list", "orderedList", "check",
+ ...(root === "gutter.single" ? ["listMindmap"] : []), "includeSublists", "paragraph",
+ ]);
+ const children = getEntryCatalogChildren(`${root}.turnInto.includeSublists`);
+ assert.deepEqual(children.map(item => item.key), [
+ "recursiveRemoveList", "recursiveList", "recursiveOrderedList", "recursiveCheck", "recursiveParagraph",
+ ]);
+ assert.equal(children[0].simple, true);
+ }
+});
+
test("entry catalog resolves navigation columns for deeply nested entries", () => {
assert.equal(getEntryCatalogSection("gutter.single"), entryCatalog.find((item) => item.key === "gutter.single"));
assert.deepEqual(getEntryCatalogPathChain("gutter.single",
diff --git a/app/src/config/entryVisibility/catalog.ts b/app/src/config/entryVisibility/catalog.ts
index 448e60cd4e..0bbe1f66cf 100644
--- a/app/src/config/entryVisibility/catalog.ts
+++ b/app/src/config/entryVisibility/catalog.ts
@@ -212,8 +212,19 @@ const gutterCopyChildren = (single = false) => [
];
const gutterTurnInto = (multi: boolean) => node("turnInto", lang("turnInto"), true, [
- node("paragraph", lang("paragraph")),
node("removeList", lang("removeList")),
+ node("list", lang("list")),
+ node("orderedList", lang("ordered-list")),
+ node("check", lang("check")),
+ ...(!multi ? [node("listMindmap", lang("mindmap"))] : []),
+ node("includeSublists", lang("includeSublists"), true, [
+ node("recursiveRemoveList", lang("removeList")),
+ node("recursiveList", lang("list")),
+ node("recursiveOrderedList", lang("ordered-list")),
+ node("recursiveCheck", lang("check")),
+ node("recursiveParagraph", lang("paragraph")),
+ ]),
+ node("paragraph", lang("paragraph")),
node("quote", lang("quote")),
node("callout", lang("callout")),
node("calloutNote", location(lang("callout"), literal("Note"))),
@@ -223,10 +234,6 @@ const gutterTurnInto = (multi: boolean) => node("turnInto", lang("turnInto"), tr
node("calloutCaution", location(lang("callout"), literal("Caution"))),
node("calloutCustom", location(lang("callout"), () => `${window.siyuan.languages.custom}...`)),
...(!multi ? [node("tabs", lang("tabs"))] : []),
- node("list", lang("list")),
- node("orderedList", lang("ordered-list")),
- node("check", lang("check")),
- ...(!multi ? [node("listMindmap", lang("mindmap"))] : []),
// 列表及列表项复用段落和标题入口,保持单选、多选的配置标识和默认顺序一致。
node("heading1", lang("heading1")),
node("heading2", lang("heading2")),
@@ -239,12 +246,6 @@ const gutterTurnInto = (multi: boolean) => node("turnInto", lang("turnInto"), tr
node("table", lang("tableBlock")),
node("line", lang("line")),
node("math", lang("math")),
- node("includeSublists", lang("includeSublists"), true, [
- node("recursiveParagraph", lang("paragraph")),
- node("recursiveList", lang("list")),
- node("recursiveOrderedList", lang("ordered-list")),
- node("recursiveCheck", lang("check")),
- ]),
]);
const gutterHeadingTransform = () => node("tWithSubtitle", lang("tWithSubtitle"), true, [
diff --git a/app/src/config/entryVisibility/order.test.ts b/app/src/config/entryVisibility/order.test.ts
index fbcabbe826..1a61508093 100644
--- a/app/src/config/entryVisibility/order.test.ts
+++ b/app/src/config/entryVisibility/order.test.ts
@@ -78,13 +78,25 @@ test("remove list merges into saved conversion menus and preserves plugin slots"
saved.splice(1, 0, "plugin:example:item");
const merged = mergeEntryOrderPreservingUnknown(defaults, saved);
assert.deepEqual(merged.filter(key => key !== "removeList"), saved);
- assert.equal(merged[merged.indexOf("paragraph") + 1], "removeList");
+ assert.equal(merged[merged.indexOf("list") - 1], "removeList");
const separators = new Set(entries.filter(item => item.type === "separator").map(item => item.key));
assert.deepEqual(resolveEntryOrder([...defaults, "plugin:example:item"], merged, separators), merged);
assert.deepEqual(resolveEntryOrder(["paragraph", "removeList"], merged, separators), ["paragraph", "removeList"]);
}
});
+test("recursive remove list merges without changing saved actions or plugin slots", () => {
+ for (const root of ["gutter.single", "gutter.multi"]) {
+ const defaults = getEntryCatalogChildren(`${root}.turnInto.includeSublists`).map(item => item.key);
+ const saved = ["recursiveParagraph", "plugin:example:item", "recursiveList", "recursiveOrderedList", "recursiveCheck"];
+ const merged = mergeEntryOrderPreservingUnknown(defaults, saved);
+ assert.deepEqual(merged.filter(key => key !== "recursiveRemoveList"), saved);
+ assert.equal(merged[1], "plugin:example:item");
+ assert.equal(merged[merged.indexOf("recursiveList") - 1], "recursiveRemoveList");
+ assert.deepEqual(resolveEntryOrder([...defaults, "plugin:example:item"], merged, new Set()), merged);
+ }
+});
+
test("list mind map view merges into conversion menus and preserves plugin slots", () => {
const entries = getEntryCatalogChildren("gutter.single.turnInto");
const defaults = entries.map(item => item.key);
@@ -233,7 +245,7 @@ test("tab conversion merges into saved block menus without moving plugin slots",
const merged = mergeEntryOrderPreservingUnknown(defaults, saved);
assert.deepEqual(merged.filter(key => key !== "tabs"), saved);
assert.equal(merged[1], "plugin:example:item");
- assert.equal(merged[merged.indexOf("tabs") + 1], "list");
+ assert.equal(merged[merged.indexOf("tabs") + 1], "heading1");
});
test("document tree profiles merge sibling creation while preserving custom order and plugin slots", () => {
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts
index 38d86384d5..ad877394b3 100644
--- a/app/src/protyle/gutter/index.ts
+++ b/app/src/protyle/gutter/index.ts
@@ -875,16 +875,10 @@ export class Gutter {
parent.getAttribute("data-type") === "NodeList" && parent.contains(item));
});
const submenu: IMenu[] = [{
- id: "recursiveParagraph",
- icon: "iconParagraph",
- label: window.siyuan.languages.paragraph,
- click() {
- turnListsRecursively({
- protyle,
- nodeElements: listElements,
- type: "CancelListRecursively"
- });
- }
+ id: "recursiveRemoveList",
+ icon: "iconListItem",
+ label: window.siyuan.languages.removeList,
+ click: () => removeListStructure(protyle, listElements, true),
}];
[{
id: "recursiveList",
@@ -916,6 +910,18 @@ export class Gutter {
}
});
});
+ submenu.push({
+ id: "recursiveParagraph",
+ icon: "iconParagraph",
+ label: window.siyuan.languages.paragraph,
+ click() {
+ turnListsRecursively({
+ protyle,
+ nodeElements: listElements,
+ type: "CancelListRecursively"
+ });
+ }
+ });
return {
id: "includeSublists",
icon: "iconListItem",
@@ -1785,49 +1791,9 @@ export class Gutter {
}));
}
} else if ((type === "NodeList" || type === "NodeMindmap") && allowStructuralMutation) {
- turnIntoSubmenu.push(type === "NodeList" ? this.turnsInto({
- menuId: "paragraph", icon: "iconParagraph", label: window.siyuan.languages.paragraph,
- accelerator: window.siyuan.config.keymap.editor.heading.paragraph.custom,
- protyle, selectsElement: [nodeElement], type: "Blocks2Ps",
- }) : this.turnsOneInto({
- menuId: "paragraph",
- id,
- icon: "iconParagraph",
- label: window.siyuan.languages.paragraph,
- accelerator: window.siyuan.config.keymap.editor.heading.paragraph.custom,
- protyle,
- nodeElement,
- type: "CancelList"
- }));
if (type === "NodeList") {
turnIntoSubmenu.push(this.removeListMenu(protyle, [nodeElement]));
}
- turnIntoSubmenu.push(this.turnsIntoOne({
- menuId: "quote",
- icon: "iconQuote",
- label: window.siyuan.languages.quote,
- accelerator: window.siyuan.config.keymap.editor.insert.quote.custom,
- protyle,
- selectsElement: [nodeElement],
- type: "Blocks2Blockquote"
- }));
- turnIntoSubmenu.push(this.turnsIntoOne({
- menuId: "callout",
- icon: "iconCallout",
- label: window.siyuan.languages.callout,
- protyle,
- selectsElement: [nodeElement],
- type: "Blocks2Callout"
- }));
- turnIntoSubmenu.push(this.turnsOneInto({
- menuId: "tabs",
- id,
- icon: "iconTabs",
- label: window.siyuan.languages.tabs,
- protyle,
- nodeElement,
- type: "List2Tabs"
- }));
const listSubtype = nodeElement.getAttribute("data-subtype");
const isMindmap = type === "NodeMindmap" ||
nodeElement.getAttribute(Constants.CUSTOM_SY_LIST_MINDMAP) === "1";
@@ -1914,12 +1880,52 @@ export class Gutter {
},
});
}
- if (!isMindmap) {
- turnIntoSubmenu.push(...this.headingTurnIntoMenu(protyle, [nodeElement]));
- }
if (!isMindmap && this.hasSublist([nodeElement])) {
turnIntoSubmenu.push(this.recursiveListMenu(protyle, [nodeElement]));
}
+ turnIntoSubmenu.push(type === "NodeList" ? this.turnsInto({
+ menuId: "paragraph", icon: "iconParagraph", label: window.siyuan.languages.paragraph,
+ accelerator: window.siyuan.config.keymap.editor.heading.paragraph.custom,
+ protyle, selectsElement: [nodeElement], type: "Blocks2Ps",
+ }) : this.turnsOneInto({
+ menuId: "paragraph",
+ id,
+ icon: "iconParagraph",
+ label: window.siyuan.languages.paragraph,
+ accelerator: window.siyuan.config.keymap.editor.heading.paragraph.custom,
+ protyle,
+ nodeElement,
+ type: "CancelList"
+ }));
+ turnIntoSubmenu.push(this.turnsIntoOne({
+ menuId: "quote",
+ icon: "iconQuote",
+ label: window.siyuan.languages.quote,
+ accelerator: window.siyuan.config.keymap.editor.insert.quote.custom,
+ protyle,
+ selectsElement: [nodeElement],
+ type: "Blocks2Blockquote"
+ }));
+ turnIntoSubmenu.push(this.turnsIntoOne({
+ menuId: "callout",
+ icon: "iconCallout",
+ label: window.siyuan.languages.callout,
+ protyle,
+ selectsElement: [nodeElement],
+ type: "Blocks2Callout"
+ }));
+ turnIntoSubmenu.push(this.turnsOneInto({
+ menuId: "tabs",
+ id,
+ icon: "iconTabs",
+ label: window.siyuan.languages.tabs,
+ protyle,
+ nodeElement,
+ type: "List2Tabs"
+ }));
+ if (!isMindmap) {
+ turnIntoSubmenu.push(...this.headingTurnIntoMenu(protyle, [nodeElement]));
+ }
} else if (type === "NodeListItem" && allowStructuralMutation) {
turnIntoSubmenu.push(...this.listTurnIntoMenu(protyle, [nodeElement]));
} else if (type === "NodeTabs" && allowStructuralMutation) {
diff --git a/app/src/protyle/wysiwyg/listConversion.test.ts b/app/src/protyle/wysiwyg/listConversion.test.ts
index 07a0759f67..5a5d6119fa 100644
--- a/app/src/protyle/wysiwyg/listConversion.test.ts
+++ b/app/src/protyle/wysiwyg/listConversion.test.ts
@@ -37,11 +37,12 @@ const browserCases = async (source: string, menuSource: string, rangeSource: str
typeof import("./transaction") & typeof import("./headingConversion");
const protyle = {wysiwyg: {element: editor}, lute, block: {rootID: "document", parentID: "document"},
observerLoad: {disconnect: noop}} as unknown as IProtyle;
- const Gutter = new Function("turnsIntoTransaction", "getHeadingConversionElements", "removeListStructure", menuSource + "\nreturn Gutter;")(
- api.turnsIntoTransaction, api.getHeadingConversionElements, api.removeListStructure);
+ const Gutter = new Function("turnsIntoTransaction", "getHeadingConversionElements", "removeListStructure", "turnListsRecursively", menuSource + "\nreturn Gutter;")(
+ api.turnsIntoTransaction, api.getHeadingConversionElements, api.removeListStructure, api.turnListsRecursively);
const gutter = new Gutter() as {
headingTurnIntoMenu: (protyle: IProtyle, elements: Element[], includeParagraph?: boolean) => Array<{id: string, click: () => Promise<void>}>,
listTurnIntoMenu: (protyle: IProtyle, elements: Element[]) => Array<{id: string, click: () => Promise<void>}>,
+ recursiveListMenu: (protyle: IProtyle, elements: Element[]) => {submenu: Array<{id: string, click: () => Promise<void>}>},
};
window.siyuan = {languages: {}, config: {keymap: {editor: {heading: {paragraph: {custom: "Alt+Ctrl+0"}}}}}} as typeof window.siyuan;
for (let level = 1; level <= 6; level++) {
@@ -220,7 +221,7 @@ const browserCases = async (source: string, menuSource: string, rangeSource: str
}
}
// 取消列表不要求首块为段落或标题,并支持多列表、嵌套选择去重及混合选区。
- for (const scenario of ["code", "nested", "overlap", "multiple", "mixed"]) {
+ for (const scenario of ["code", "nested", "overlap", "multiple", "mixed", "recursive"]) {
editor.innerHTML = lute.Md2BlockDOM("- # outer\n\n - ## inner\n\n- # last");
const list = editor.firstElementChild;
const nested = list.querySelector('[data-type="NodeList"]');
@@ -238,19 +239,26 @@ const browserCases = async (source: string, menuSource: string, rangeSource: str
selection = [nested];
} else if (scenario === "overlap") {
selection = [list, items(list)[0], nested];
- } else {
+ } else if (scenario !== "recursive") {
editor.insertAdjacentHTML("beforeend", lute.Md2BlockDOM(scenario === "mixed" ? "# outside" : "1. # another"));
selection = Array.from(editor.children);
}
const before = editor.innerHTML;
const content = shape(before).filter(block => !["NodeList", "NodeListItem"].includes(block.type));
batches.length = 0;
- await gutter.listTurnIntoMenu(protyle, selection).find(item => item.id === "removeList").click();
+ if (scenario === "recursive") {
+ const menu = gutter.recursiveListMenu(protyle, selection).submenu;
+ check.deepEqual(menu.map(item => item.id), ["recursiveRemoveList", "recursiveList", "recursiveOrderedList", "recursiveCheck", "recursiveParagraph"]);
+ await menu[0].click();
+ check.equal(editor.querySelector('[data-type="NodeList"]'), null);
+ } else {
+ await gutter.listTurnIntoMenu(protyle, selection).find(item => item.id === "removeList").click();
+ }
const after = editor.innerHTML;
check.equal(batches.length, 1);
const withoutParent = (block: ReturnType<typeof shape>[number]): ReturnType<typeof shape>[number] => ({...block, parent: undefined});
check.deepEqual(shape(after).filter(block => content.some(original => original.id === block.id)).map(withoutParent), content.map(withoutParent));
- if (scenario !== "nested") {
+ if (scenario !== "nested" && scenario !== "recursive") {
check.ok(editor.querySelector(`[data-node-id="${id(nested)}"]`), "keep nested lists");
}
replay(batches[0].undoOperations);
@@ -289,7 +297,7 @@ test("list conversion removes selected wrappers and preserves content, database
const parsed = createSourceFile("gutter.ts", read("../gutter/index.ts"), ScriptTarget.Latest, true);
const gutter = parsed.statements.find(isClassDeclaration);
const methods = gutter.members.filter(member => isMethodDeclaration(member) &&
- ["headingTurnIntoMenu", "listTurnIntoMenu", "removeListMenu", "turnsInto"].includes(member.name.getText(parsed)));
+ ["headingTurnIntoMenu", "listTurnIntoMenu", "removeListMenu", "recursiveListMenu", "turnsInto"].includes(member.name.getText(parsed)));
const menuSource = compile("class Gutter {\n" + methods.map(member => member.getText(parsed)).join("\n") + "\n}");
const temporary = mkdtempSync(path.join(tmpdir(), "siyuan-list-conversion-"));
const script = path.join(temporary, "run.cjs");
diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.ts
index 90882eaffb..addfcaaf17 100644
--- a/app/src/protyle/wysiwyg/transaction.ts
+++ b/app/src/protyle/wysiwyg/transaction.ts
@@ -2077,8 +2077,8 @@ const unfoldListHeadings = async (protyle: IProtyle, nodeElements: Element[]) =>
return foldOperations.reverse();
};
-export const removeListStructure = (protyle: IProtyle, nodeElements: Element[]) =>
- turnListBlocksInto({protyle}, nodeElements.filter(isListHeadingContainer));
+export const removeListStructure = (protyle: IProtyle, nodeElements: Element[], recursively = false) =>
+ turnListBlocksInto({protyle, recursively}, nodeElements.filter(isListHeadingContainer));
const turnListBlocksInto = async (options: {
protyle: IProtyle,