Skip to content

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

:bug: Cover cross-block IME replacement structures https://github.com/siyuan-note/siyuan/issues/18986

Daniela8fc6785445f
diff --git a/app/src/protyle/wysiwyg/remove.ts b/app/src/protyle/wysiwyg/remove.ts
index e03dc27f68..9bf452a4b6 100644
--- a/app/src/protyle/wysiwyg/remove.ts
+++ b/app/src/protyle/wysiwyg/remove.ts
@@ -61,6 +61,7 @@ import {
getCrossBlockMergeRemoveElement,
getCrossBlockSiblingListItemMergeContext,
getDeletedBlockElements,
+ isNativeCrossBlockCompositionSupported,
isEntireBlockContentSelected,
mergeCrossBlockNestedLists,
mergeCrossBlockSiblingListItems
@@ -395,11 +396,6 @@ const prepareNativeCrossBlockComposition = (protyle: IProtyle, selectedRange: Ra
ICrossBlockComposition | undefined => {
const editorElement = protyle.wysiwyg.element;
const ranges = getBlockRanges(editorElement, selectedRange);
- const supportedTypes = new Set(["NodeParagraph", "NodeHeading", "NodeList", "NodeListItem"]);
- if (ranges.length < 2 || ranges.some(item =>
- !["NodeParagraph", "NodeHeading"].includes(item.blockElement.getAttribute("data-type")))) {
- return;
- }
const startRootElement = getEditorRootElement(editorElement, startElement);
const endRootElement = getEditorRootElement(editorElement, endElement);
const undoFocusContext = getUndoFocusContext(editorElement, selectedRange, true);
@@ -415,15 +411,16 @@ ICrossBlockComposition | undefined => {
}
currentNode = currentNode.nextSibling;
}
- if (currentNode !== endRootElement || snapshotNodes.some(node => {
+ const snapshotTypes = snapshotNodes.flatMap(node => {
if (node.nodeType !== Node.ELEMENT_NODE) {
- return false;
+ return [];
}
const element = node as HTMLElement;
- return !supportedTypes.has(element.getAttribute("data-type")) ||
- Array.from(element.querySelectorAll<HTMLElement>("[data-node-id]"))
- .some(item => !supportedTypes.has(item.getAttribute("data-type")));
- })) {
+ return [element, ...Array.from(element.querySelectorAll<HTMLElement>("[data-node-id]"))]
+ .map(item => item.getAttribute("data-type") || "");
+ });
+ if (currentNode !== endRootElement || !isNativeCrossBlockCompositionSupported(
+ ranges.map(item => item.blockElement.getAttribute("data-type") || ""), snapshotTypes)) {
return;
}
const startMarker = document.createComment("siyuan-cross-block-composition-start");
diff --git a/app/src/protyle/wysiwyg/removeRange.test.ts b/app/src/protyle/wysiwyg/removeRange.test.ts
index 54e1bb5aa2..044d8145f9 100644
--- a/app/src/protyle/wysiwyg/removeRange.test.ts
+++ b/app/src/protyle/wysiwyg/removeRange.test.ts
@@ -6,6 +6,7 @@ import {
getCrossBlockMergeRemoveElement,
getCrossBlockSiblingListItemMergeContext,
getDeletedBlockElements,
+ isNativeCrossBlockCompositionSupported,
isEntireBlockContentSelected
} from "./removeRange";
@@ -87,6 +88,26 @@ const block = (name: string, type: string, ...children: TestElement[]) =>
const attr = (name: string) => new TestElement(name);
const asHTMLElement = (element: TestElement) => element as unknown as HTMLElement;
+describe("isNativeCrossBlockCompositionSupported", () => {
+ it("支持跨容器的文本块、代码块和表格组合输入", () => {
+ assert.equal(isNativeCrossBlockCompositionSupported(
+ ["NodeParagraph", "NodeParagraph"],
+ ["NodeBlockquote", "NodeParagraph", "NodeCallout", "NodeParagraph"]), true);
+ assert.equal(isNativeCrossBlockCompositionSupported(
+ ["NodeCodeBlock", "NodeParagraph"],
+ ["NodeSuperBlock", "NodeCodeBlock", "NodeParagraph"]), true);
+ assert.equal(isNativeCrossBlockCompositionSupported(
+ ["NodeParagraph", "NodeTable"], ["NodeParagraph", "NodeTable"]), true);
+ });
+
+ it("拒绝属性视图和需要重新渲染的快照", () => {
+ assert.equal(isNativeCrossBlockCompositionSupported(
+ ["NodeAttributeView", "NodeParagraph"], ["NodeAttributeView", "NodeParagraph"]), false);
+ assert.equal(isNativeCrossBlockCompositionSupported(
+ ["NodeHeading", "NodeParagraph"], ["NodeHeading", "NodeMathBlock", "NodeParagraph"]), false);
+ });
+});
+
describe("getCrossBlockEndAction", () => {
it("合并相同类型的段落和标题边界", () => {
assert.equal(getCrossBlockEndAction("NodeParagraph", "NodeParagraph", false, false), "merge");
diff --git a/app/src/protyle/wysiwyg/removeRange.ts b/app/src/protyle/wysiwyg/removeRange.ts
index bab6b36484..76ba7b4279 100644
--- a/app/src/protyle/wysiwyg/removeRange.ts
+++ b/app/src/protyle/wysiwyg/removeRange.ts
@@ -6,6 +6,27 @@ const getNextBlockSibling = (element: Element) => {
return nextElement;
};
+const NATIVE_COMPOSITION_EDITABLE_TYPES = new Set([
+ "NodeParagraph",
+ "NodeHeading",
+ "NodeCodeBlock",
+ "NodeTable",
+]);
+
+const NATIVE_COMPOSITION_SNAPSHOT_TYPES = new Set([
+ ...NATIVE_COMPOSITION_EDITABLE_TYPES,
+ "NodeList",
+ "NodeListItem",
+ "NodeBlockquote",
+ "NodeCallout",
+ "NodeSuperBlock",
+]);
+
+export const isNativeCrossBlockCompositionSupported = (rangeTypes: string[], snapshotTypes: string[]) => {
+ return rangeTypes.length >= 2 && rangeTypes.every(type => NATIVE_COMPOSITION_EDITABLE_TYPES.has(type)) &&
+ snapshotTypes.every(type => NATIVE_COMPOSITION_SNAPSHOT_TYPES.has(type));
+};
+
const getListItemElement = (element: HTMLElement, editorElement: HTMLElement) => {
let currentElement = element.parentElement;
while (currentElement && currentElement !== editorElement) {