An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Preserve nested list structure in cross-block pasting
Daniel8c784ca1ed85
diff --git a/app/src/protyle/util/paste.ts b/app/src/protyle/util/paste.tsindex 20ad746740..747d46590a 100644--- a/app/src/protyle/util/paste.ts+++ b/app/src/protyle/util/paste.ts@@ -22,7 +22,7 @@ import {updateTransaction} from "../wysiwyg/transaction";import * as dayjs from "dayjs";import {updateListOrder} from "../wysiwyg/list";import {refreshSbAndPersistWidth} from "../../block/util";-import {shouldPreservePastedBlockStructure} from "./pasteSource";+import {extractCrossBlockPasteContext, shouldPreservePastedBlockStructure} from "./pasteSource";import {normalizePasteResponse} from "./pasteResponse";import {applyLuteMarkdownSyntax} from "../render/luteMarkdownSyntax";@@ -311,7 +311,8 @@ export const convertPastedListItemSubtype = (listItemElement: HTMLElement, subty}};-const pasteCrossBlockRange = (protyle: IProtyle, tempElement: HTMLElement, range: Range) => {+const pasteCrossBlockRange = (protyle: IProtyle, tempElement: HTMLElement, range: Range,+ preserveNestedList: boolean) => {const pastedRoots = Array.from(tempElement.children) as HTMLElement[];if (!range.collapsed || pastedRoots.length < 2) {return false;@@ -368,12 +369,14 @@ const pasteCrossBlockRange = (protyle: IProtyle, tempElement: HTMLElement, rangetargetBlockElement.previousElementSibling?.classList.contains("protyle-action") !== true) {return false;}- const targetSubtype = targetContainerElement.getAttribute("data-subtype");- pastedChildren.forEach(item => {- if (item.getAttribute("data-subtype") !== targetSubtype) {- convertPastedListItemSubtype(item, targetSubtype);- }- });+ if (!preserveNestedList) {+ const targetSubtype = targetContainerElement.getAttribute("data-subtype");+ pastedChildren.forEach(item => {+ if (item.getAttribute("data-subtype") !== targetSubtype) {+ convertPastedListItemSubtype(item, targetSubtype);+ }+ });+ }}const targetEditableElement = getContenteditableElement(targetBlockElement);const firstEditableElement = getContenteditableElement(firstBlockElement);@@ -382,8 +385,10 @@ const pasteCrossBlockRange = (protyle: IProtyle, tempElement: HTMLElement, rangereturn false;}- const oldHTML = targetChildElement.outerHTML;- const oldListItemHTML = containerType === "NodeList" ? new Map(Array.from(+ const isNestedListPaste = preserveNestedList && containerType === "NodeList";+ const transactionElement = isNestedListPaste ? targetBlockElement : targetChildElement;+ const oldHTML = transactionElement.outerHTML;+ const oldListItemHTML = containerType === "NodeList" && !isNestedListPaste ? new Map(Array.from(targetContainerElement.querySelectorAll<HTMLElement>(":scope > .li")).map(item => [item.getAttribute("data-node-id"), item.outerHTML])) : undefined;const undoFocusContext = getUndoFocusContext(protyle.wysiwyg.element, range, true);@@ -415,11 +420,17 @@ const pasteCrossBlockRange = (protyle: IProtyle, tempElement: HTMLElement, range}fixAdjacentTags(targetEditableElement);fixAdjacentTags(endEditableElement);- targetChildElement.after(...pastedChildren);- if (containerType === "NodeList") {+ const insertedElements = isNestedListPaste ? [pastedContainerElement] : pastedChildren;+ if (isNestedListPaste) {+ targetBlockElement.after(pastedContainerElement);+ updateListOrder(pastedContainerElement);+ } else {+ targetChildElement.after(...pastedChildren);+ }+ if (containerType === "NodeList" && !isNestedListPaste) {updateListOrder(targetContainerElement);}- targetChildElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));+ transactionElement.setAttribute("updated", dayjs().format("YYYYMMDDHHmmss"));const widthDoOperations: IOperation[] = [];const widthUndoOperations: IOperation[] = [];@@ -430,12 +441,13 @@ const pasteCrossBlockRange = (protyle: IProtyle, tempElement: HTMLElement, range}const doOperations: IOperation[] = [];const undoOperations: IOperation[] = [];- pastedChildren.slice().reverse().forEach(item => {+ insertedElements.slice().reverse().forEach(item => {doOperations.push({action: "insert",id: item.getAttribute("data-node-id"),data: item.outerHTML,- previousID: targetChildElement.getAttribute("data-node-id")+ previousID: (isNestedListPaste ? targetBlockElement : targetChildElement).getAttribute("data-node-id"),+ parentID: isNestedListPaste ? targetChildElement.getAttribute("data-node-id") : undefined,});undoOperations.push({action: "delete",@@ -465,7 +477,7 @@ const pasteCrossBlockRange = (protyle: IProtyle, tempElement: HTMLElement, rangedoOperations.push(...widthDoOperations);undoOperations.unshift(...widthUndoOperations);focusByOffset(endBlockElement, pastedEnd, pastedEnd, true, true);- updateTransaction(protyle, targetChildElement, oldHTML, undoFocusContext, {+ updateTransaction(protyle, transactionElement, oldHTML, undoFocusContext, {doOperations,undoOperations,context: getUndoFocusContext(protyle.wysiwyg.element, getEditorRange(protyle.wysiwyg.element), true)@@ -592,6 +604,14 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven}}+ // 插件可能替换完整剪贴板载荷,因此仅从最终保留的内部数据中读取层级上下文+ const crossBlockPasteContext = siyuanHTML ? extractCrossBlockPasteContext(textHTML) : {+ nestedList: false,+ html: textHTML,+ };+ textHTML = crossBlockPasteContext.html;+ originalTextHTML = extractCrossBlockPasteContext(originalTextHTML).html;+let nodeElement = hasClosestBlock(event.target);const range = getEditorRange(protyle.wysiwyg.element);@@ -748,7 +768,8 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEvenitem.hasAttribute("parent-heading"));// 完整块选择以及以标题开头的跨块文本选区需要保留块类型// 标题及下方块还需要保留折叠关系 https://github.com/siyuan-note/siyuan/issues/18419- if (isBlock && !preservePastedBlockStructure && !hasHeadingChildren && pasteCrossBlockRange(protyle, tempElement, range)) {+ if (isBlock && !preservePastedBlockStructure && !hasHeadingChildren &&+ pasteCrossBlockRange(protyle, tempElement, range, crossBlockPasteContext.nestedList)) {blockRender(protyle, protyle.wysiwyg.element);processRender(protyle.wysiwyg.element);highlightRender(protyle.wysiwyg.element);diff --git a/app/src/protyle/util/pasteSource.test.ts b/app/src/protyle/util/pasteSource.test.tsindex a361b3c314..94b4f6177c 100644--- a/app/src/protyle/util/pasteSource.test.ts+++ b/app/src/protyle/util/pasteSource.test.ts@@ -1,6 +1,11 @@import {describe, it} from "node:test";import * as assert from "node:assert/strict";-import {shouldPreservePastedBlockStructure} from "./pasteSource";+import {+ extractCrossBlockPasteContext,+ isNestedListCrossBlockSelection,+ NESTED_LIST_PASTE_MARKER,+ shouldPreservePastedBlockStructure,+} from "./pasteSource";const element = (type: string, selected = false) => ({classList: {@@ -35,3 +40,48 @@ describe("shouldPreservePastedBlockStructure", () => {]), false);});});++describe("isNestedListCrossBlockSelection", () => {+ const item = () => {+ const children = new Set<Element>();+ return {+ element: {+ contains(child: Element) {+ return children.has(child);+ },+ } as unknown as Element,+ children,+ };+ };++ it("detects a selection from a parent list item into its descendant", () => {+ const parent = item();+ const child = item();+ parent.children.add(child.element);+ assert.equal(isNestedListCrossBlockSelection(parent.element, child.element), true);+ });++ it("ignores selections within one list item or between sibling items", () => {+ const first = item();+ const second = item();+ assert.equal(isNestedListCrossBlockSelection(first.element, first.element), false);+ assert.equal(isNestedListCrossBlockSelection(first.element, second.element), false);+ });+});++describe("extractCrossBlockPasteContext", () => {+ it("extracts and removes the nested-list marker", () => {+ const html = `<!--data-siyuan='encoded'-->${NESTED_LIST_PASTE_MARKER}<p>content</p>`;+ assert.deepEqual(extractCrossBlockPasteContext(html), {+ nestedList: true,+ html: "<!--data-siyuan='encoded'--><p>content</p>",+ });+ });++ it("leaves unmarked HTML unchanged", () => {+ assert.deepEqual(extractCrossBlockPasteContext("<p>content</p>"), {+ nestedList: false,+ html: "<p>content</p>",+ });+ });+});diff --git a/app/src/protyle/util/pasteSource.ts b/app/src/protyle/util/pasteSource.tsindex 99b2c2b118..c99349dbba 100644--- a/app/src/protyle/util/pasteSource.ts+++ b/app/src/protyle/util/pasteSource.ts@@ -1,3 +1,14 @@+export const NESTED_LIST_PASTE_MARKER = "<!--data-siyuan-paste='nested-list'-->";++export const isNestedListCrossBlockSelection = (startListItem?: Element | false, endListItem?: Element | false) => {+ return !!startListItem && !!endListItem && startListItem !== endListItem && startListItem.contains(endListItem);+};++export const extractCrossBlockPasteContext = (html: string) => ({+ nestedList: html.includes(NESTED_LIST_PASTE_MARKER),+ html: html.replaceAll(NESTED_LIST_PASTE_MARKER, ""),+});+export const shouldPreservePastedBlockStructure = (elements: ArrayLike<Element>) => {const rootElements = Array.from(elements);return rootElements.some(item => item.classList.contains("protyle-wysiwyg--select")) ||diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.tsindex 37da75a090..de4310a401 100644--- a/app/src/protyle/wysiwyg/index.ts+++ b/app/src/protyle/wysiwyg/index.ts@@ -123,6 +123,7 @@ import {showMessage} from "../../dialog/message";import {getBacklinkHeadingMore, loadBreadcrumb} from "./renderBacklink";import {removeSearchMark} from "../toolbar/util";import {getTableCellTextStyleMenus} from "../toolbar/tableCell";+import {isNestedListCrossBlockSelection, NESTED_LIST_PASTE_MARKER} from "../util/pasteSource";import {activeBlur} from "../../mobile/util/keyboardToolbar";import {commonClick} from "./commonClick";import {avClick, avContextmenu, updateAVName} from "../render/av/action";@@ -725,6 +726,7 @@ export class WYSIWYG {let textPlain = "";let isInCodeBlock = false;let needClipboardWrite = false;+ let nestedListPaste = false;if (selectElements.length > 0) {const isRefText = selectElements[0].getAttribute("data-reftext") === "true";if (selectElements[0].getAttribute("data-type") === "NodeListItem" &&@@ -916,6 +918,10 @@ export class WYSIWYG {tempElement.append(range.cloneContents());const isCrossBlock = nodeElement !== hasClosestBlock(range.endContainer);if (isCrossBlock) {+ nestedListPaste = isNestedListCrossBlockSelection(+ hasClosestByAttribute(range.startContainer, "data-type", "NodeListItem"),+ hasClosestByAttribute(range.endContainer, "data-type", "NodeListItem"),+ );this.normalizeCrossBlockCopy(tempElement, range);}const crossBlockTextPlain = isCrossBlock ? Array.from(tempElement.children)@@ -994,7 +1000,8 @@ export class WYSIWYG {exportedHTML = prepared.html;clipboardText = prepared.source;}- const textHTML = `<!--data-siyuan='${encodeBase64(textSiyuan)}'-->` + exportedHTML;+ const textHTML = `<!--data-siyuan='${encodeBase64(textSiyuan)}'-->` ++ (nestedListPaste ? NESTED_LIST_PASTE_MARKER : "") + exportedHTML;event.clipboardData.setData("text/plain", clipboardText);event.clipboardData.setData("text/html", textHTML);if (needClipboardWrite) {