Skip to content

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

:bug: Restore focused list item cuts correctly https://github.com/siyuan-note/siyuan/issues/11990

Daniel9bd3d28cc4e0
diff --git a/app/src/protyle/wysiwyg/list.ts b/app/src/protyle/wysiwyg/list.ts
index 3deb38fffa..b6ee801059 100644
--- a/app/src/protyle/wysiwyg/list.ts
+++ b/app/src/protyle/wysiwyg/list.ts
@@ -15,6 +15,7 @@ import {
getLastListItemElement,
getOrderedListMarkerUpdates,
getOrderedListMaxStart,
+ getPreviousListItemID,
parseOrderedListStart,
type TListSubtype
} from "./listContext";
@@ -270,6 +271,32 @@ export const getFocusedOrderedListRemoveOperations = (listElement: HTMLElement,
return {doOperations, undoOperations};
};
+export const getFocusedOrderedListDeleteOperations = (listElement: HTMLElement,
+ removedListItemElement: HTMLElement) => {
+ if (listElement.getAttribute("data-subtype") !== "o" ||
+ removedListItemElement.getAttribute("data-subtype") !== "o") {
+ return;
+ }
+ const removedListItemID = removedListItemElement.getAttribute("data-node-id");
+ const removedClone = getDirectListItemByID(listElement, removedListItemID);
+ if (!removedClone) {
+ return;
+ }
+ const previousID = getPreviousListItemID(listElement, removedListItemID);
+ removedClone.replaceWith(removedListItemElement.cloneNode(true));
+ const updatedRemovedClone = getDirectListItemByID(listElement, removedListItemID);
+ if (!updatedRemovedClone) {
+ return;
+ }
+ const oldHTML = listElement.outerHTML;
+ const startIndex = getOrderedListStart(listElement);
+ updatedRemovedClone.remove();
+ const doOperations: IOperation[] = [];
+ const undoOperations: IOperation[] = [];
+ appendFocusedListUpdate(listElement, oldHTML, doOperations, undoOperations, startIndex);
+ return {doOperations, undoOperations, previousID};
+};
+
const appendFocusedIndentListUpdate = (listElement: HTMLElement, previousElement: HTMLElement,
movedElements: Element[], doOperations: IOperation[],
undoOperations: IOperation[]) => {
diff --git a/app/src/protyle/wysiwyg/listContext.test.ts b/app/src/protyle/wysiwyg/listContext.test.ts
index aa57a49a69..fc8fbd4c9b 100644
--- a/app/src/protyle/wysiwyg/listContext.test.ts
+++ b/app/src/protyle/wysiwyg/listContext.test.ts
@@ -9,6 +9,7 @@ import {
getListConversionType,
getOrderedListMarkerUpdates,
getOrderedListMaxStart,
+ getPreviousListItemID,
getListShortcutAction,
parseOrderedListStart,
shouldIgnoreListShortcut,
@@ -187,6 +188,19 @@ describe("getFirstListItemElement", () => {
});
});
+describe("getPreviousListItemID", () => {
+ it("returns the original predecessor of a focused list item", () => {
+ const first = new TestElement("NodeListItem", "first", "o");
+ const middle = new TestElement("NodeListItem", "middle", "o");
+ const last = new TestElement("NodeListItem", "last", "o");
+ const list = new TestElement("NodeList", "list", "o").append(first, middle, last, new TestElement());
+
+ assert.equal(getPreviousListItemID(asHTMLElement(list), "middle"), "first");
+ assert.equal(getPreviousListItemID(asHTMLElement(list), "first"), undefined);
+ assert.equal(getPreviousListItemID(asHTMLElement(list), "missing"), undefined);
+ });
+});
+
describe("getListShortcutAction", () => {
it("cancels or converts a single empty list", () => {
const {paragraph, editor} = createList("u");
diff --git a/app/src/protyle/wysiwyg/listContext.ts b/app/src/protyle/wysiwyg/listContext.ts
index 502428f09f..53b2731ea4 100644
--- a/app/src/protyle/wysiwyg/listContext.ts
+++ b/app/src/protyle/wysiwyg/listContext.ts
@@ -54,6 +54,13 @@ export const getFirstListItemElement = (listElement: HTMLElement) => {
item.getAttribute("data-type") === "NodeListItem") as HTMLElement | undefined;
};
+export const getPreviousListItemID = (listElement: HTMLElement, listItemID: string) => {
+ const listItemElements = Array.from(listElement.children).filter((item) =>
+ item.getAttribute("data-type") === "NodeListItem");
+ const index = listItemElements.findIndex((item) => item.getAttribute("data-node-id") === listItemID);
+ return index > 0 ? listItemElements[index - 1].getAttribute("data-node-id") : undefined;
+};
+
export const getListContext = (nodeElement: HTMLElement, editorElement: HTMLElement): TListContext | undefined => {
let listItemElement: HTMLElement | null = nodeElement;
while (listItemElement && listItemElement !== editorElement &&
diff --git a/app/src/protyle/wysiwyg/remove.ts b/app/src/protyle/wysiwyg/remove.ts
index fd368a387c..5d69e68bb5 100644
--- a/app/src/protyle/wysiwyg/remove.ts
+++ b/app/src/protyle/wysiwyg/remove.ts
@@ -28,6 +28,7 @@ import {
import {transaction, turnsIntoOneTransaction, turnsIntoTransaction, updateTransaction} from "./transaction";
import {cancelSB, genEmptyElement, rebalanceSbWidth, refreshSbResize} from "../../block/util";
import {
+ getFocusedOrderedListDeleteOperations,
getFocusedOrderedListRemoveOperations,
getFocusedParentOrderedList,
getOrderedListStart,
@@ -683,6 +684,10 @@ export const removeBlock = async (protyle: IProtyle, blockElement: Element, rang
type: "Delete" | "Backspace" | "remove", skipRefCheck = false) => {
const selectElements = Array.from(protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select"));
if (selectElements?.length > 0) {
+ const selectedTopElement = selectElements.length === 1 ? getTopAloneElement(selectElements[0]) : undefined;
+ const focusedOrderedListItem = selectedTopElement?.parentElement === protyle.wysiwyg.element &&
+ selectedTopElement.getAttribute("data-type") === "NodeListItem" &&
+ selectedTopElement.getAttribute("data-subtype") === "o" ? selectedTopElement as HTMLElement : undefined;
const embedSelectElements = selectElements.filter(item => isInEmbedBlock(item));
if (embedSelectElements.length > 0) {
// 嵌入块内暂不支持跨边界或多块删除,避免上溯时删除查询目标。
@@ -744,6 +749,18 @@ export const removeBlock = async (protyle: IProtyle, blockElement: Element, rang
return false;
}
}
+ let focusedOrderOperations: ReturnType<typeof getFocusedOrderedListDeleteOperations>;
+ if (focusedOrderedListItem) {
+ const focusedParentListElement = await getFocusedParentOrderedList(protyle, protyle.wysiwyg.element);
+ if (!focusedParentListElement) {
+ return false;
+ }
+ focusedOrderOperations = getFocusedOrderedListDeleteOperations(focusedParentListElement,
+ focusedOrderedListItem);
+ if (!focusedOrderOperations) {
+ return false;
+ }
+ }
protyle.observerLoad?.disconnect();
// 删除后,防止滚动条滚动后调用 get 请求,因为返回的请求已查找不到内容块了
preventScroll(protyle);
@@ -839,6 +856,9 @@ export const removeBlock = async (protyle: IProtyle, blockElement: Element, rang
}
const previousBlockElement = getPreviousBlockSibling(topElement);
let previousID = previousBlockElement ? previousBlockElement.getAttribute("data-node-id") : "";
+ if (focusedOrderOperations && id === focusedOrderedListItem.getAttribute("data-node-id")) {
+ previousID = focusedOrderOperations.previousID || "";
+ }
if (previousBlockElement &&
previousBlockElement.getAttribute("data-type") === "NodeHeading" &&
previousBlockElement.getAttribute("fold") === "1") {
@@ -936,6 +956,10 @@ export const removeBlock = async (protyle: IProtyle, blockElement: Element, rang
}
}
}
+ if (focusedOrderOperations) {
+ deletes.push(...focusedOrderOperations.doOperations);
+ inserts.splice(0, 0, ...focusedOrderOperations.undoOperations);
+ }
if (deletes.length > 0) {
if (topParentElement && topParentElement.getAttribute("data-type") === "NodeSuperBlock" && getSbChildBlockCount(topParentElement) === 1) {
const sbData = await cancelSB(protyle, topParentElement, range);