Skip to content

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

:bug: Preserve ordered list start after item changes https://github.com/siyuan-note/siyuan/issues/11990

Daniel95f087addf84
diff --git a/app/src/protyle/hint/extend.ts b/app/src/protyle/hint/extend.ts
index af9085f1c4..3f90dec20a 100644
--- a/app/src/protyle/hint/extend.ts
+++ b/app/src/protyle/hint/extend.ts
@@ -13,7 +13,7 @@ import {replaceFileName} from "../../editor/rename";
import {transaction} from "../wysiwyg/transaction";
import {getAssetExtension, getAssetName, getDisplayName, isEncryptedBox} from "../../util/pathName";
import {genEmptyElement} from "../../block/util";
-import {updateListOrder} from "../wysiwyg/list";
+import {getOrderedListStart, updateListOrder} from "../wysiwyg/list";
import {escapeHtml, stripSearchMark} from "../../util/escape";
import {zoomOut} from "../../menus/protyle";
import {hideElements} from "../ui/hideElements";
@@ -646,6 +646,7 @@ export const hintMoveBlock = (pathString: string, sourceElements: Element[], pro
const doOperations: IOperation[] = [];
let topSourceElement: Element;
const parentElement = sourceElements[0].parentElement;
+ const listStart = getOrderedListStart(parentElement);
let sideElement;
sourceElements.forEach((item, index) => {
if (index === sourceElements.length - 1 &&
@@ -673,7 +674,7 @@ export const hintMoveBlock = (pathString: string, sourceElements: Element[], pro
topSourceElement.remove();
} else if (parentElement.classList.contains("list") && parentElement.getAttribute("data-subtype") === "o" &&
parentElement.childElementCount > 1) {
- updateListOrder(parentElement, 1);
+ updateListOrder(parentElement, listStart);
Array.from(parentElement.children).forEach((item) => {
if (item.classList.contains("protyle-attr")) {
return;
diff --git a/app/src/protyle/util/editorCommonEvent.ts b/app/src/protyle/util/editorCommonEvent.ts
index 9e75344ccd..a51e694e58 100644
--- a/app/src/protyle/util/editorCommonEvent.ts
+++ b/app/src/protyle/util/editorCommonEvent.ts
@@ -27,7 +27,7 @@ import {
refreshSbResize
} from "../../block/util";
import {transaction, turnsIntoOneTransaction} from "../wysiwyg/transaction";
-import {updateListOrder} from "../wysiwyg/list";
+import {getOrderedListStart, updateListOrder} from "../wysiwyg/list";
import {fetchPost, fetchSyncPost} from "../../util/fetch";
import {onGet} from "./onGet";
/// #if !MOBILE
@@ -233,7 +233,7 @@ const moveTo = async (protyle: IProtyle, sourceElements: Element[], targetElemen
});
let newListElement: Element;
let newListId: string;
- const orderListElements: { [key: string]: Element } = {};
+ const orderListElements: { [key: string]: { element: Element, start?: number } } = {};
// 在 DOM 移动前显式捕获每个源块的位置,供 undoOperations 使用。
// 不能依赖循环内 getParentBlock(item)(移动后 item 的父已变),否则撤销会移到错误位置。
// 关键:对于文档顶层块,getParentBlock 返回 .protyle-wysiwyg 容器(无 data-node-id),
@@ -379,7 +379,14 @@ const moveTo = async (protyle: IProtyle, sourceElements: Element[], targetElemen
}
}
if (item.classList.contains("li") && item.getAttribute("data-subtype") === "o") {
- orderListElements[item.parentElement.getAttribute("data-node-id")] = item.parentElement;
+ const listElement = item.parentElement;
+ const listID = listElement.getAttribute("data-node-id");
+ if (listID && !orderListElements[listID]) {
+ orderListElements[listID] = {
+ element: listElement,
+ start: getOrderedListStart(listElement),
+ };
+ }
}
if (newListId) {
newListElement.insertAdjacentElement("afterbegin", item);
@@ -562,7 +569,8 @@ const moveTo = async (protyle: IProtyle, sourceElements: Element[], targetElemen
}
}
Object.keys(orderListElements).forEach(key => {
- Array.from(orderListElements[key].children).forEach((item) => {
+ const orderList = orderListElements[key];
+ Array.from(orderList.element.children).forEach((item) => {
if (item.classList.contains("protyle-attr")) {
return;
}
@@ -572,8 +580,8 @@ const moveTo = async (protyle: IProtyle, sourceElements: Element[], targetElemen
data: item.outerHTML
});
});
- updateListOrder(orderListElements[key], 1);
- Array.from(orderListElements[key].children).forEach((item) => {
+ updateListOrder(orderList.element, orderList.start);
+ Array.from(orderList.element.children).forEach((item) => {
if (item.classList.contains("protyle-attr")) {
return;
}
@@ -835,6 +843,8 @@ const dragSame = async (protyle: IProtyle, sourceElements: Element[], targetElem
parentID: targetParentElement.getAttribute("data-node-id")
});
}
+ const targetListStart = targetElement.getAttribute("data-type") === "NodeListItem" &&
+ targetElement.getAttribute("data-subtype") === "o" ? getOrderedListStart(targetElement.parentElement) : undefined;
const moveToResult = await moveTo(protyle, sourceElements, sourceRowElement || targetElement, isSameEditor,
sourceRowElement ? "afterbegin" : (isBottom ? "afterend" : "beforebegin"), isCopy, sourcePositions);
if (sourceRowElement && isCopy) {
@@ -882,7 +892,7 @@ const dragSame = async (protyle: IProtyle, sourceElements: Element[], targetElem
data: item.outerHTML
});
});
- updateListOrder(targetElement.parentElement, 1);
+ updateListOrder(targetElement.parentElement, targetListStart);
Array.from(targetElement.parentElement.children).forEach((item) => {
if (item.classList.contains("protyle-attr")) {
return;
diff --git a/app/src/protyle/wysiwyg/enter.ts b/app/src/protyle/wysiwyg/enter.ts
index 1121c95e67..c9866c087d 100644
--- a/app/src/protyle/wysiwyg/enter.ts
+++ b/app/src/protyle/wysiwyg/enter.ts
@@ -17,6 +17,7 @@ import {
genListItemElement,
getFocusedOrderedListInsertOperations,
getFocusedParentOrderedList,
+ getOrderedListStart,
listOutdent,
updateListOrder
} from "./list";
@@ -485,7 +486,9 @@ const listEnter = async (protyle: IProtyle, blockElement: HTMLElement, range: Ra
// https://github.com/siyuan-note/siyuan/issues/8935
const wbrElement = document.createElement("wbr");
range.insertNode(wbrElement);
- const html = listItemElement.parentElement.outerHTML;
+ const listElement = listItemElement.parentElement;
+ const html = listElement.outerHTML;
+ const listStart = getOrderedListStart(listElement);
wbrElement.remove();
let newElement = genListItemElement(listItemElement, -1, true);
if (!blockElement.previousElementSibling.classList.contains("protyle-action")) {
@@ -502,9 +505,9 @@ const listEnter = async (protyle: IProtyle, blockElement: HTMLElement, range: Ra
listItemElement.insertAdjacentElement("beforebegin", newElement);
}
if (listItemElement.getAttribute("data-subtype") === "o") {
- updateListOrder(listItemElement.parentElement);
+ updateListOrder(listElement, listStart);
}
- updateTransaction(protyle, listItemElement.parentElement, html);
+ updateTransaction(protyle, listElement, html);
focusByWbr(newElement, range);
scrollCenter(protyle);
removeEmptyNode(newElement);
@@ -533,11 +536,12 @@ const listEnter = async (protyle: IProtyle, blockElement: HTMLElement, range: Ra
// 段末换行,在子列表中插入
range.insertNode(document.createElement("wbr"));
const html = listItemElement.outerHTML;
+ const listStart = getOrderedListStart(subListElement);
blockElement.querySelector("wbr").remove();
newElement = genListItemElement(subListElement.firstElementChild, -1, true);
subListElement.firstElementChild.before(newElement);
if (subListElement.getAttribute("data-subtype") === "o") {
- updateListOrder(subListElement);
+ updateListOrder(subListElement, listStart);
}
updateTransaction(protyle, listItemElement, html);
focusByWbr(listItemElement, range);
diff --git a/app/src/protyle/wysiwyg/list.ts b/app/src/protyle/wysiwyg/list.ts
index fdc9daf03e..3deb38fffa 100644
--- a/app/src/protyle/wysiwyg/list.ts
+++ b/app/src/protyle/wysiwyg/list.ts
@@ -72,6 +72,14 @@ export const updateListOrder = (listElement: Element, sIndex?: number) => {
const getOrderedListItemElements = (listElement: Element) => Array.from(listElement.children).filter((item) =>
item.getAttribute("data-type") === "NodeListItem") as HTMLElement[];
+export const getOrderedListStart = (listElement: Element) => {
+ if (listElement.getAttribute("data-subtype") !== "o") {
+ return;
+ }
+ const start = Number.parseInt(getOrderedListItemElements(listElement)[0]?.getAttribute("data-marker"), 10);
+ return Number.isFinite(start) ? Math.trunc(start) : undefined;
+};
+
export const setOrderedListStart = (protyle: IProtyle, listElement: HTMLElement, start: number) => {
if (listElement.getAttribute("data-type") !== "NodeList" ||
listElement.getAttribute("data-subtype") !== "o") {
@@ -228,7 +236,7 @@ export const getFocusedOrderedListInsertOperations = (listElement: HTMLElement,
return {doOperations, undoOperations};
}
const oldHTML = listElement.outerHTML;
- const startIndex = Number.parseInt(getOrderedListItemElements(listElement)[0]?.getAttribute("data-marker"), 10);
+ const startIndex = getOrderedListStart(listElement);
currentElement.replaceWith(listItemElement.cloneNode(true));
const updatedCurrentElement = getDirectListItemByID(listElement,
listItemElement.getAttribute("data-node-id"));
@@ -255,7 +263,7 @@ export const getFocusedOrderedListRemoveOperations = (listElement: HTMLElement,
return {doOperations, undoOperations};
}
const oldHTML = listElement.outerHTML;
- const startIndex = Number.parseInt(getOrderedListItemElements(listElement)[0]?.getAttribute("data-marker"), 10);
+ const startIndex = getOrderedListStart(listElement);
previousClone.replaceWith(previousListItemElement.cloneNode(true));
removedClone.remove();
appendFocusedListUpdate(listElement, oldHTML, doOperations, undoOperations, startIndex);
@@ -271,7 +279,7 @@ const appendFocusedIndentListUpdate = (listElement: HTMLElement, previousElement
return;
}
const oldHTML = listElement.outerHTML;
- const startIndex = Number.parseInt(getOrderedListItemElements(listElement)[0]?.getAttribute("data-marker"), 10);
+ const startIndex = getOrderedListStart(listElement);
previousClone.replaceWith(previousElement.cloneNode(true));
movedElements.forEach((item) => {
getDirectListItemByID(listElement, item.getAttribute("data-node-id"))?.remove();
@@ -288,7 +296,7 @@ const appendFocusedOutdentListUpdate = (listElement: HTMLElement, parentListItem
return;
}
const oldHTML = listElement.outerHTML;
- const startIndex = Number.parseInt(getOrderedListItemElements(listElement)[0]?.getAttribute("data-marker"), 10);
+ const startIndex = getOrderedListStart(listElement);
const referenceElement = parentClone.nextElementSibling;
if (parentListItemElement.isConnected) {
parentClone.replaceWith(parentListItemElement.cloneNode(true));
diff --git a/app/src/protyle/wysiwyg/listContext.test.ts b/app/src/protyle/wysiwyg/listContext.test.ts
index e2663fbe2e..aa57a49a69 100644
--- a/app/src/protyle/wysiwyg/listContext.test.ts
+++ b/app/src/protyle/wysiwyg/listContext.test.ts
@@ -263,10 +263,13 @@ describe("getOrderedListMarkerUpdates", () => {
it("supports an explicit zero start index", () => {
assert.deepEqual(getOrderedListMarkerUpdates(["1.", "2."], 0), ["0.", "1."]);
+ assert.deepEqual(getOrderedListMarkerUpdates(["1.", "2.", "0."], 0), ["0.", "1.", "2."]);
+ assert.deepEqual(getOrderedListMarkerUpdates(["2.", "0.", "1."], 0), ["0.", "1.", "2."]);
});
it("preserves an explicit custom start when the original first item is removed", () => {
assert.deepEqual(getOrderedListMarkerUpdates(["1.", "12.", "13."], 10), ["10.", "11.", "12."]);
+ assert.deepEqual(getOrderedListMarkerUpdates(["4.", "5.", "2."], 4), [undefined, undefined, "6."]);
});
it("replaces invalid markers instead of producing NaN", () => {
diff --git a/app/src/protyle/wysiwyg/remove.ts b/app/src/protyle/wysiwyg/remove.ts
index 40a56088e9..fd368a387c 100644
--- a/app/src/protyle/wysiwyg/remove.ts
+++ b/app/src/protyle/wysiwyg/remove.ts
@@ -30,6 +30,7 @@ import {cancelSB, genEmptyElement, rebalanceSbWidth, refreshSbResize} from "../.
import {
getFocusedOrderedListRemoveOperations,
getFocusedParentOrderedList,
+ getOrderedListStart,
listOutdent,
updateListOrder
} from "./list";
@@ -467,7 +468,10 @@ export const removeCrossBlockRange = async (protyle: IProtyle, selectedRange: Ra
});
const orderListElements = Array.from(new Set(removeElements
.filter(item => item.classList.contains("li") && item.parentElement.getAttribute("data-subtype") === "o")
- .map(item => item.parentElement)));
+ .map(item => item.parentElement))).map(item => ({
+ element: item,
+ start: getOrderedListStart(item),
+ }));
rangesByBlock.forEach(blockRanges => {
blockRanges.forEach(item => {
@@ -595,11 +599,11 @@ export const removeCrossBlockRange = async (protyle: IProtyle, selectedRange: Ra
});
});
orderListElements.forEach(item => {
- const oldListItems = new Map(Array.from(item.querySelectorAll<HTMLElement>(":scope > .li")).map(listItem => {
+ const oldListItems = new Map(Array.from(item.element.querySelectorAll<HTMLElement>(":scope > .li")).map(listItem => {
return [listItem.getAttribute("data-node-id"), listItem.outerHTML];
}));
- updateListOrder(item);
- item.querySelectorAll<HTMLElement>(":scope > .li").forEach(listItem => {
+ updateListOrder(item.element, item.start);
+ item.element.querySelectorAll<HTMLElement>(":scope > .li").forEach(listItem => {
const id = listItem.getAttribute("data-node-id");
const oldHTML = oldListItems.get(id);
if (!oldHTML || oldHTML === listItem.outerHTML) {
@@ -762,6 +766,7 @@ export const removeBlock = async (protyle: IProtyle, blockElement: Element, rang
}
}
let listElement: Element;
+ let listStart: number | undefined;
let topParentElement: Element;
hideElements(["select"], protyle);
const unfoldData: {
@@ -857,9 +862,13 @@ export const removeBlock = async (protyle: IProtyle, blockElement: Element, rang
parentID: getOperationParentID(topElement, protyle.block.parentID)
});
if (topElement.getAttribute("data-subtype") === "o" && topElement.classList.contains("li")) {
- listElement = topElement.parentElement;
+ if (listElement !== topElement.parentElement) {
+ listElement = topElement.parentElement;
+ listStart = getOrderedListStart(listElement);
+ }
} else {
listElement = undefined;
+ listStart = undefined;
}
// https://github.com/siyuan-note/siyuan/issues/12327
if (topElement.parentElement.classList.contains("li") && topElement.parentElement.childElementCount === 4 &&
@@ -918,7 +927,7 @@ export const removeBlock = async (protyle: IProtyle, blockElement: Element, rang
data: listElement.outerHTML
});
listElement.setAttribute(Constants.ATTRIBUTE_EDITING, "true");
- updateListOrder(listElement, 1);
+ updateListOrder(listElement, listStart);
deletes.push({
action: "update",
id: listElement.getAttribute("data-node-id"),
@@ -1516,6 +1525,7 @@ const removeLi = async (protyle: IProtyle, blockElement: Element, range: Range,
range.insertNode(document.createElement("wbr"));
const listElement = blockElement.parentElement.parentElement;
const listHTML = listElement.outerHTML;
+ const listStart = getOrderedListStart(listElement);
const previousLastElement = blockElement.parentElement.parentElement.previousElementSibling.lastElementChild;
const previousHTML = previousLastElement.parentElement.outerHTML;
blockElement.parentElement.firstElementChild.remove();
@@ -1523,7 +1533,7 @@ const removeLi = async (protyle: IProtyle, blockElement: Element, range: Range,
previousLastElement.insertAdjacentHTML("beforebegin", blockElement.parentElement.innerHTML);
blockElement.parentElement.remove();
if (listElement.getAttribute("data-subtype") === "o") {
- updateListOrder(listElement);
+ updateListOrder(listElement, listStart);
}
listElement.setAttribute(Constants.ATTRIBUTE_EDITING, "true");
previousLastElement.parentElement.setAttribute(Constants.ATTRIBUTE_EDITING, "true");
@@ -1561,6 +1571,7 @@ const removeLi = async (protyle: IProtyle, blockElement: Element, range: Range,
range.insertNode(document.createElement("wbr"));
const listElement = blockElement.parentElement.parentElement;
const listHTML = listElement.outerHTML;
+ const listStart = getOrderedListStart(listElement);
blockElement.parentElement.firstElementChild.remove();
blockElement.parentElement.lastElementChild.remove();
const tempElement = document.createElement("div");
@@ -1583,7 +1594,7 @@ const removeLi = async (protyle: IProtyle, blockElement: Element, range: Range,
listElement.insertAdjacentHTML("beforebegin", blockElement.parentElement.innerHTML);
blockElement.parentElement.remove();
if (listElement.getAttribute("data-subtype") === "o") {
- updateListOrder(listElement, parseInt(listElement.firstElementChild.getAttribute("data-marker")) - 1);
+ updateListOrder(listElement, listStart);
}
listElement.setAttribute(Constants.ATTRIBUTE_EDITING, "true");
doOperations.splice(0, 0, {