Skip to content

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

:bug: Preserve headings copied from cross-block text selections https://github.com/siyuan-note/siyuan/issues/18513

Daniel1ba06c64b7af
diff --git a/app/src/protyle/util/paste.ts b/app/src/protyle/util/paste.ts
index 84eba50db4..1f31cf261c 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 {hasBlockSelectionPasteMarker} from "./pasteSource";
+import {shouldPreservePastedBlockStructure} from "./pasteSource";
export const beforePaste = (protyle: IProtyle, blockElement: HTMLElement) => {
// 链接,备注,样式,引用,pdf标注粘贴 https://github.com/siyuan-note/siyuan/issues/11572
@@ -660,7 +660,7 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven
} else {
tempElement.innerHTML = siyuanHTML;
}
- const isBlockSelectionPaste = hasBlockSelectionPasteMarker(tempElement.children);
+ const preservePastedBlockStructure = shouldPreservePastedBlockStructure(tempElement.children);
const startRangeBlockElement = hasClosestBlock(range.startContainer);
const endRangeBlockElement = hasClosestBlock(range.endContainer);
if (startRangeBlockElement && endRangeBlockElement && startRangeBlockElement !== endRangeBlockElement) {
@@ -754,8 +754,9 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven
});
const hasHeadingChildren = Array.from(tempElement.children).some(item =>
item.hasAttribute("parent-heading"));
- // 完整块选择以及标题下方块需要保留块结构,交由 insertHTML 处理 https://github.com/siyuan-note/siyuan/issues/18419
- if (isBlock && !isBlockSelectionPaste && !hasHeadingChildren && pasteCrossBlockRange(protyle, tempElement, range)) {
+ // 完整块选择以及以标题开头的跨块文本选区需要保留块类型
+ // 标题及下方块还需要保留折叠关系 https://github.com/siyuan-note/siyuan/issues/18419
+ if (isBlock && !preservePastedBlockStructure && !hasHeadingChildren && pasteCrossBlockRange(protyle, tempElement, range)) {
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.ts
index dfae2657ae..a361b3c314 100644
--- a/app/src/protyle/util/pasteSource.test.ts
+++ b/app/src/protyle/util/pasteSource.test.ts
@@ -1,21 +1,37 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
-import {hasBlockSelectionPasteMarker} from "./pasteSource";
+import {shouldPreservePastedBlockStructure} from "./pasteSource";
-const element = (selected = false) => ({
+const element = (type: string, selected = false) => ({
classList: {
contains(className: string) {
return selected && className === "protyle-wysiwyg--select";
}
+ },
+ getAttribute(attribute: string) {
+ return attribute === "data-type" ? type : null;
}
}) as unknown as Element;
-describe("hasBlockSelectionPasteMarker", () => {
- it("detects a marker on any pasted root block", () => {
- assert.equal(hasBlockSelectionPasteMarker([element(), element(true)]), true);
+describe("shouldPreservePastedBlockStructure", () => {
+ it("preserves a selection containing complete blocks", () => {
+ assert.equal(shouldPreservePastedBlockStructure([
+ element("NodeParagraph"),
+ element("NodeParagraph", true)
+ ]), true);
});
- it("does not classify a cross-block text range as a block selection", () => {
- assert.equal(hasBlockSelectionPasteMarker([element(), element()]), false);
+ it("preserves a cross-block text selection that starts with a heading", () => {
+ assert.equal(shouldPreservePastedBlockStructure([
+ element("NodeHeading"),
+ element("NodeHeading")
+ ]), true);
+ });
+
+ it("merges a cross-block paragraph selection into the target block", () => {
+ assert.equal(shouldPreservePastedBlockStructure([
+ element("NodeParagraph"),
+ element("NodeParagraph")
+ ]), false);
});
});
diff --git a/app/src/protyle/util/pasteSource.ts b/app/src/protyle/util/pasteSource.ts
index 9c3e7fca98..99b2c2b118 100644
--- a/app/src/protyle/util/pasteSource.ts
+++ b/app/src/protyle/util/pasteSource.ts
@@ -1,3 +1,5 @@
-export const hasBlockSelectionPasteMarker = (elements: ArrayLike<Element>) => {
- return Array.from(elements).some(item => item.classList.contains("protyle-wysiwyg--select"));
+export const shouldPreservePastedBlockStructure = (elements: ArrayLike<Element>) => {
+ const rootElements = Array.from(elements);
+ return rootElements.some(item => item.classList.contains("protyle-wysiwyg--select")) ||
+ rootElements[0]?.getAttribute("data-type") === "NodeHeading";
};