Skip to content

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

:sparkles: Add stable plugin content insertion https://github.com/siyuan-note/siyuan/issues/20566

Danielf1ece6b1d70c
diff --git a/app/src/plugin/API.test.ts b/app/src/plugin/API.test.ts
index 2f1373b63f..939e91111b 100644
--- a/app/src/plugin/API.test.ts
+++ b/app/src/plugin/API.test.ts
@@ -29,7 +29,9 @@ it("defers plugin API dependency reads and shares the initialized API", () => {
const exports: {getAPI?: () => {
adaptHotkey: typeof adaptHotkey,
openInputDialog: typeof openInputDialog,
- openAssetPicker: typeof openAssetPicker
+ openAssetPicker: typeof openAssetPicker,
+ insertContent: (options: import("./insertContent").IInsertContentOptions) =>
+ import("./insertContent").IInsertContentResult
}} = {};
runInNewContext(compiled, {exports, require: () => dependency});
dependenciesReady = true;
@@ -37,5 +39,6 @@ it("defers plugin API dependency reads and shares the initialized API", () => {
assert.equal(api.adaptHotkey, adaptHotkey);
assert.equal(api.openInputDialog, openInputDialog);
assert.equal(api.openAssetPicker, openAssetPicker);
+ assert.equal(typeof api.insertContent, "function");
assert.equal(exports.getAPI(), api);
});
diff --git a/app/src/plugin/API.ts b/app/src/plugin/API.ts
index ae1a7c9f75..4cb1ec92c9 100644
--- a/app/src/plugin/API.ts
+++ b/app/src/plugin/API.ts
@@ -37,6 +37,7 @@ import {ProtyleMethod} from "./ProtyleMethod";
import {openEmojiPanel} from "../emoji";
import {openAssetPicker} from "../asset/picker";
import {adjustEditorFontSize, setEditorFontSize} from "../util/editorFontSize";
+import {insertEditorContent, IInsertContentOptions} from "./insertContent";
/// #if !MOBILE
import {isDockPanelVisible, toggleDockPanel} from "../layout/dock/panel";
/// #endif
@@ -286,6 +287,19 @@ const getActiveEditor = (wndActive = true) => {
return editor;
};
+const insertContent = (options: IInsertContentOptions) => {
+ if (options?.editor) {
+ return insertEditorContent(options, []);
+ }
+ let editors: Protyle[];
+ /// #if MOBILE
+ editors = [window.siyuan.mobile.popEditor, window.siyuan.mobile.editor].filter(Boolean);
+ /// #else
+ editors = getAllModels().editor.map(model => model.editor);
+ /// #endif
+ return insertEditorContent(options, editors);
+};
+
export const expandDocTree = async (options: {
id: string,
isSetCurrent?: boolean
@@ -428,6 +442,7 @@ const createAPI = () => ({
getAllTabs,
/// #endif
getActiveEditor,
+ insertContent,
platformUtils,
openSetting: openPluginSetting,
openAttributePanel,
diff --git a/app/src/plugin/insertContent.test.ts b/app/src/plugin/insertContent.test.ts
new file mode 100644
index 0000000000..ff5bfb9bae
--- /dev/null
+++ b/app/src/plugin/insertContent.test.ts
@@ -0,0 +1,304 @@
+import * as assert from "node:assert/strict";
+import {execFile} from "node:child_process";
+import {mkdtempSync, readFileSync, rmSync, writeFileSync} from "node:fs";
+import {tmpdir} from "node:os";
+import * as path from "node:path";
+import {test} from "node:test";
+import {promisify} from "node:util";
+import {createSourceFile, isVariableStatement, ModuleKind, ScriptTarget, transpileModule} from "typescript";
+
+const browserCases = async (sources: Record<string, string>, insertionSource: string) => {
+ const check: typeof assert = require("node:assert/strict");
+ const modules: Record<string, {exports: any}> = {};
+ const load = (name: string): any => {
+ if (modules[name]) {
+ return modules[name].exports;
+ }
+ const module = {exports: {}};
+ modules[name] = module;
+ new Function("require", "module", "exports", sources[name])((dependency: string) => {
+ const parts = name.split("/");
+ parts.pop();
+ dependency.split("/").forEach(part => {
+ if (part === "..") parts.pop();
+ else if (part !== ".") parts.push(part);
+ });
+ return load(parts.join("/"));
+ }, module, module.exports);
+ return module.exports;
+ };
+ const elementOf = (node: Node) => node.nodeType === 1 ? node as HTMLElement : node.parentElement;
+ const hasClosestBlock = (node: Node) => elementOf(node)?.closest("[data-node-id]");
+ const getContenteditableElement = (block: Element) => block.querySelector('[contenteditable="true"]');
+ const focusByRange = (range: Range) => {
+ getSelection().removeAllRanges();
+ getSelection().addRange(range);
+ };
+ const changes: Array<{oldHTML: string, html: string}> = [];
+ const updateTransaction = (_protyle: IProtyle, block: Element, oldHTML: string) => {
+ changes.push({oldHTML, html: block.outerHTML});
+ };
+ const dependencies = {
+ hasClosestBlock, getContenteditableElement, focusByRange, updateTransaction,
+ prepareInlineElementBoundaryMutation() {}, getBlockRanges: (): never[] => [],
+ hasClosestByAttribute: (node: Node, name: string, value: string) => elementOf(node)?.closest(`[${name}="${value}"]`),
+ hasClosestByClassName: (node: Node, name: string) => elementOf(node)?.closest(`.${name}`),
+ hasClosestByTag: (node: Node, tag: string) => elementOf(node)?.closest(tag),
+ isInEmbedBlock: (element: Element) => element.closest('[data-type="NodeBlockQueryEmbed"]'),
+ getTextWithoutSemanticMarkers: (element: Element) => element.textContent,
+ Constants: {ZWSP: "\u200b", ATTRIBUTE_EDITING: "data-editing", TIMEOUT_LOAD: 1},
+ dayjs: () => ({format: () => "20261015000000"}), scrollCenter() {}, highlightRender() {},
+ fixAdjacentTags() {},
+ input: (protyle: IProtyle, block: Element, range: Range) => {
+ updateTransaction(protyle, block, protyle.wysiwyg.lastHTMLs[block.getAttribute("data-node-id")]);
+ focusByRange(range);
+ },
+ focusByWbr: (_block: Element, range: Range) => focusByRange(range),
+ };
+ const createInsertion = new Function(...Object.keys(dependencies), insertionSource + "return insertHTML;");
+ const insertHTML = createInsertion(...Object.values(dependencies));
+ modules["protyle/util/insertHTML"] = {exports: {insertHTML}};
+ modules["protyle/util/hasClosest"] = {exports: dependencies};
+ modules["protyle/wysiwyg/getBlock"] = {exports: {getContenteditableElement}};
+ for (const name of ["protyle/render/blockRender", "protyle/util/processCode", "protyle/render/highlightRender",
+ "protyle/render/av/render"]) {
+ modules[name] = {exports: {blockRender() {}, processRender() {}, highlightRender() {}, avRender() {}}};
+ }
+ modules["protyle/runtimeCapabilities"] = {exports: {
+ getProtyleBlockDOMSanitizer: (protyle: any) => protyle.sanitize,
+ getProtyleUnsupportedPasteBlocks: (protyle: any) => protyle.unsupported,
+ }};
+ const {insertEditorContent} = load("plugin/insertContent") as typeof import("./insertContent");
+ const memory = load("protyle/util/editorFocusMemory") as typeof import("../protyle/util/editorFocusMemory");
+ const lute = Lute.New();
+ lute.SetTextMark(true);
+ lute.SetProtyleWYSIWYG(true);
+ lute.SetSpin(true);
+ const fixture = (text = "hello world", time = 1) => {
+ const element = document.createElement("div");
+ element.className = "protyle";
+ const root = document.createElement("div");
+ root.className = "protyle-wysiwyg";
+ root.contentEditable = "true";
+ root.innerHTML = lute.Md2BlockDOM(text);
+ element.append(root);
+ document.body.append(element);
+ const dispose = memory.bindEditorFocusMemory(root);
+ const editor = {protyle: {
+ element, wysiwyg: {element: root, lastHTMLs: {}}, lute, disabled: false,
+ toolbar: {getCurrentType: (): string[] => []}, model: {headElement: {dataset: {activetime: String(time)}}},
+ }} as unknown as import("../protyle").Protyle;
+ return {editor, root, element, dispose, content: () => root.querySelector('[contenteditable="true"]')};
+ };
+ const select = (node: Node, start: number, end = start) => {
+ const range = document.createRange();
+ range.setStart(node, start);
+ range.setEnd(node, end);
+ focusByRange(range);
+ return range;
+ };
+ const outside = document.createElement("div");
+ outside.textContent = "sidebar";
+ document.body.append(outside);
+ const leaveEditor = () => {
+ outside.dispatchEvent(new PointerEvent("pointerdown", {bubbles: true}));
+ select(outside.firstChild, 0);
+ };
+ const first = fixture();
+ select(first.content().firstChild, 5);
+ leaveEditor();
+ check.equal(insertEditorContent({data: "**bold**"}, [first.editor]).status, "inserted");
+ check.equal(first.content().textContent, "hellobold world");
+ check.ok(first.content().querySelector('[data-type="strong"]'));
+ const undo = document.createElement("div");
+ undo.innerHTML = changes.at(-1).oldHTML;
+ check.equal(undo.querySelector('[contenteditable="true"]').textContent, "hello world");
+ check.ok(changes.at(-1).html.includes("bold"));
+ check.equal(getSelection().getRangeAt(0).collapsed, true);
+ const caret = getSelection().getRangeAt(0);
+ const prefix = document.createRange();
+ prefix.selectNodeContents(first.content());
+ prefix.setEnd(caret.startContainer, caret.startOffset);
+ check.equal(prefix.toString(), "hellobold");
+ const selected = fixture();
+ select(selected.content().firstChild, 6, 11);
+ leaveEditor();
+ check.equal(insertEditorContent({data: "<em>there</em>", dataType: "html"}, [selected.editor]).status, "inserted");
+ check.equal(selected.content().textContent, "hello there");
+ check.ok(selected.content().querySelector('[data-type="em"]'));
+
+ const older = fixture("older", 1);
+ const newer = fixture("newer", 2);
+ select(older.content().firstChild, 5);
+ leaveEditor();
+ select(newer.content().firstChild, 5);
+ leaveEditor();
+ check.equal(insertEditorContent({data: "!"}, [older.editor, newer.editor]).status, "inserted");
+ check.equal(newer.content().textContent, "newer!");
+ check.equal(older.content().textContent, "older");
+ select(older.content().firstChild, 5);
+ check.equal(insertEditorContent({data: "?"}, [older.editor, newer.editor]).status, "inserted");
+ check.equal(older.content().textContent, "older?");
+ leaveEditor();
+ const explicit = select(older.content().firstChild, 0);
+ leaveEditor();
+ check.equal(insertEditorContent({data: "X", editor: older.editor, range: explicit}, [newer.editor]).status, "inserted");
+ check.equal(older.content().textContent, "Xolder?");
+
+ const unchanged = older.root.innerHTML;
+ check.equal(insertEditorContent({data: "bad", editor: older.editor, range: select(outside.firstChild, 0)}, []).status,
+ "invalid-range");
+ older.editor.protyle.disabled = true;
+ check.equal(insertEditorContent({data: "bad", editor: older.editor}, []).status, "readonly");
+ older.editor.protyle.disabled = false;
+ older.element.classList.add("fn__none");
+ check.equal(insertEditorContent({data: "bad", editor: older.editor}, []).status, "no-editor");
+ older.element.classList.remove("fn__none");
+ check.equal(older.root.innerHTML, unchanged);
+
+ const stale = fixture("stale");
+ select(stale.content().firstChild, 2);
+ leaveEditor();
+ stale.root.innerHTML = lute.Md2BlockDOM("replacement");
+ check.equal(insertEditorContent({data: "bad"}, [stale.editor]).status, "invalid-range");
+ check.equal(stale.content().textContent, "replacement");
+ const changedText = fixture("old text");
+ select(changedText.content().firstChild, 3);
+ leaveEditor();
+ changedText.content().firstChild.nodeValue = "new text";
+ check.equal(insertEditorContent({data: "bad"}, [changedText.editor]).status, "invalid-range");
+ check.equal(changedText.content().textContent, "new text");
+ const fresh = fixture("fresh");
+ check.equal(insertEditorContent({data: "bad"}, [fresh.editor]).status, "invalid-range");
+ check.equal(insertEditorContent({data: "bad"}, []).status, "no-editor");
+ select(fresh.content().firstChild, 0);
+ check.equal(insertEditorContent({data: ""}, [fresh.editor]).status, "empty-content");
+ check.throws(() => insertEditorContent({data: "bad", dataType: "dom" as "html"}, [fresh.editor]), TypeError);
+ const embedded = fixture("embedded");
+ embedded.root.firstElementChild.setAttribute("data-type", "NodeBlockQueryEmbed");
+ select(embedded.content().firstChild, 0);
+ check.equal(insertEditorContent({data: "bad"}, [embedded.editor]).status, "unsupported-range");
+ const restricted = fixture("restricted");
+ Object.assign(restricted.editor.protyle, {unsupported: () => ["NodeHeading"]});
+ select(restricted.content().firstChild, 0);
+ check.equal(insertEditorContent({data: "# heading", editor: restricted.editor}, []).status, "unsupported-content");
+ check.equal(restricted.content().textContent, "restricted");
+ const noneditable = fixture("immutable");
+ const immutable = noneditable.content();
+ immutable.setAttribute("contenteditable", "false");
+ select(immutable.firstChild, 1);
+ check.equal(insertEditorContent({data: "bad"}, [noneditable.editor]).status, "unsupported-range");
+ check.equal(immutable.textContent, "immutable");
+
+ const crossing = fixture("crossing");
+ document.body.append(outside);
+ const crossRange = select(crossing.content().firstChild, 0);
+ crossRange.setEndAfter(outside);
+ check.equal(insertEditorContent({data: "bad", editor: crossing.editor}, []).status, "invalid-range");
+ check.equal(crossing.content().textContent, "crossing");
+
+ const focusOnly = fixture("focus only");
+ const input = document.createElement("input");
+ document.body.append(input);
+ focusOnly.root.focus();
+ select(focusOnly.content().firstChild, 5);
+ await new Promise(resolve => setTimeout(resolve, 0));
+ input.focus();
+ getSelection().removeAllRanges();
+ check.equal(insertEditorContent({data: "X", editor: focusOnly.editor}, []).status, "inserted");
+ check.equal(focusOnly.content().textContent, "focusX only");
+ input.remove();
+
+ const code = fixture("```\nhello world\n```");
+ select(code.content().firstChild, 5);
+ leaveEditor();
+ check.equal(insertEditorContent({data: "<b>literal</b>", dataType: "html", editor: code.editor}, []).status, "inserted");
+ check.ok(code.content().textContent.includes("hello<b>literal</b> world"));
+ check.equal(code.content().querySelector("b"), null);
+
+ for (const item of [first, selected, older, newer, stale, changedText, fresh, embedded, restricted,
+ noneditable, crossing, focusOnly, code]) {
+ item.dispose();
+ item.element.remove();
+ check.equal(memory.getRememberedEditorFocusRange(item.root), undefined);
+ }
+ return "Plugin content insertion cases passed";
+};
+
+test("plugin insertion uses the document selection across external UI focus changes", {
+ skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,
+ timeout: 45000,
+}, async () => {
+ const compile = (source: string) => transpileModule(source, {
+ compilerOptions: {target: ScriptTarget.ES2021, module: ModuleKind.CommonJS},
+ }).outputText;
+ const read = (name: string) => readFileSync(path.resolve(__dirname, "..", name + ".ts"), "utf8");
+ const sources = Object.fromEntries(["plugin/insertContent", "protyle/util/editorFocus",
+ "protyle/util/editorFocusMemory"].map(name => [name, compile(read(name))]));
+ const file = createSourceFile("insertHTML.ts", read("protyle/util/insertHTML"), ScriptTarget.Latest, true);
+ const declaration = file.statements.filter(isVariableStatement).flatMap(statement =>
+ Array.from(statement.declarationList.declarations)).find(item => item.name.getText(file) === "insertHTML");
+ const insertionSource = compile(`const insertHTML = ${declaration.initializer.getText(file)};`);
+ const source = `const __name = value => value; (${browserCases.toString()})(${JSON.stringify(sources)}, ${JSON.stringify(insertionSource)})`;
+ const temporary = mkdtempSync(path.join(tmpdir(), "siyuan-plugin-insertion-test-"));
+ const script = path.join(temporary, "run.cjs");
+ const lutePath = path.resolve(__dirname, "../../stage/protyle/js/lute/lute.min.js");
+ writeFileSync(script, `const {app, BrowserWindow} = require("electron");
+app.setPath("userData", ${JSON.stringify(path.join(temporary, "profile"))});
+app.commandLine.appendSwitch("disable-gpu");
+app.whenReady().then(async () => {
+ const win = new BrowserWindow({show: false, webPreferences: {nodeIntegration: true, contextIsolation: false, offscreen: true}});
+ try {
+ await win.loadURL("about:blank");
+ await win.webContents.executeJavaScript(require("node:fs").readFileSync(${JSON.stringify(lutePath)}, "utf8"));
+ console.log(await win.webContents.executeJavaScript(${JSON.stringify(source)}));
+ win.destroy();
+ app.exit(0);
+ } catch (error) {
+ console.error(error);
+ win.destroy();
+ app.exit(1);
+ }
+});`, "utf8");
+ const env = {...process.env};
+ delete env.ELECTRON_RUN_AS_NODE;
+ try {
+ const result = await promisify(execFile)(require("electron") as unknown as string, [script],
+ {env, timeout: 40000, windowsHide: true});
+ assert.match(result.stdout, /Plugin content insertion cases passed/);
+ } finally {
+ assert.equal(path.dirname(path.resolve(temporary)), path.resolve(tmpdir()));
+ assert.ok(path.basename(temporary).startsWith("siyuan-plugin-insertion-test-"));
+ rmSync(temporary, {recursive: true, force: true});
+ }
+});
+
+test("desktop and mobile insertion resolve document editors and honor an explicit target", () => {
+ const source = readFileSync(path.join(__dirname, "API.ts"), "utf8");
+ const file = createSourceFile("API.ts", source, ScriptTarget.Latest, true);
+ const declaration = file.statements.filter(isVariableStatement).flatMap(statement =>
+ Array.from(statement.declarationList.declarations)).find(item => item.name.getText(file) === "insertContent");
+ for (const mobile of [false, true]) {
+ let keep = true;
+ const body = declaration.initializer.getText(file).split("\n").filter(line => {
+ if (line.includes("/// #if MOBILE")) keep = mobile;
+ else if (line.includes("/// #else")) keep = !mobile;
+ else if (line.includes("/// #endif")) keep = true;
+ else return keep;
+ return false;
+ }).join("\n");
+ const compiled = transpileModule(`const insertContent = ${body};`, {
+ compilerOptions: {target: ScriptTarget.ES2021},
+ }).outputText;
+ const primary = {};
+ const popup = {};
+ const auxiliary = {};
+ const insert = new Function("getAllModels", "insertEditorContent", "window", compiled + "return insertContent;")(
+ () => ({editor: [{editor: primary}], custom: [{editors: [auxiliary]}]}),
+ (_options: unknown, editors: unknown[]) => editors,
+ {siyuan: {mobile: {editor: primary, popEditor: popup, auxiliary}}},
+ );
+ assert.deepEqual(insert({data: "text"}), mobile ? [popup, primary] : [primary]);
+ assert.deepEqual(insert({data: "text", editor: auxiliary}), []);
+ }
+});
diff --git a/app/src/plugin/insertContent.ts b/app/src/plugin/insertContent.ts
new file mode 100644
index 0000000000..66ce3b1d56
--- /dev/null
+++ b/app/src/plugin/insertContent.ts
@@ -0,0 +1,120 @@
+import type {Protyle} from "../protyle";
+import {getRememberedEditorFocusRange, rememberEditorFocusRange} from "../protyle/util/editorFocusMemory";
+import {getEditorFocusRange, restoreEditorFocusRange} from "../protyle/util/editorFocus";
+import {hasClosestBlock, isInEmbedBlock} from "../protyle/util/hasClosest";
+import {getContenteditableElement} from "../protyle/wysiwyg/getBlock";
+import {insertHTML} from "../protyle/util/insertHTML";
+import {getProtyleBlockDOMSanitizer, getProtyleUnsupportedPasteBlocks} from "../protyle/runtimeCapabilities";
+import {blockRender} from "../protyle/render/blockRender";
+import {processRender} from "../protyle/util/processCode";
+import {highlightRender} from "../protyle/render/highlightRender";
+import {avRender} from "../protyle/render/av/render";
+
+export interface IInsertContentOptions {
+ data: string;
+ dataType?: "markdown" | "html";
+ editor?: Protyle;
+ range?: Range;
+}
+
+export interface IInsertContentResult {
+ status: "inserted" | "no-editor" | "readonly" | "invalid-range" | "unsupported-range" |
+ "unsupported-content" | "empty-content";
+}
+
+const isVisible = (editor: Protyle) => editor?.protyle.element.ownerDocument === document &&
+ editor.protyle.element.isConnected &&
+ !editor.protyle.element.closest(".fn__none, [inert]") && editor.protyle.element.getClientRects().length > 0;
+
+const getInsertionEditor = (editors: Protyle[], range?: Range) => {
+ const visible = editors.filter(isVisible);
+ const selected = range && visible.find(editor => editor.protyle.wysiwyg.element.contains(range.startContainer));
+ if (selected) {
+ return selected;
+ }
+ const active = visible.find(editor => editor.protyle.element.closest(".layout__wnd--active"));
+ if (active) {
+ return active;
+ }
+ return visible.reduce<Protyle | undefined>((latest, editor) => {
+ const time = Number(editor.protyle.model?.headElement?.dataset.activetime) || 0;
+ const latestTime = Number(latest?.protyle.model?.headElement?.dataset.activetime) || 0;
+ return !latest || time > latestTime ? editor : latest;
+ }, undefined);
+};
+
+const isEditableBoundary = (root: HTMLElement, node: Node) => {
+ const element = node.nodeType === 1 ? node as Element : node.parentElement;
+ if (element?.closest("[contenteditable]")?.getAttribute("contenteditable") === "false") {
+ return false;
+ }
+ const block = hasClosestBlock(node);
+ if (!block || isInEmbedBlock(block) || block.getAttribute("data-type") === "NodeAttributeView") {
+ return false;
+ }
+ const editable = getContenteditableElement(block, node);
+ return !!editable && root.contains(editable) && (editable === node || editable.contains(node));
+};
+
+export const insertEditorContent = (options: IInsertContentOptions, editors: Protyle[]): IInsertContentResult => {
+ if (typeof options?.data !== "string" || options.dataType && !["markdown", "html"].includes(options.dataType)) {
+ throw new TypeError("Content must be a string with markdown or html dataType");
+ }
+ const selection = document.getSelection();
+ const currentRange = selection?.rangeCount ? selection.getRangeAt(0) : undefined;
+ const editor = options.editor || getInsertionEditor(editors, currentRange);
+ if (!editor || !isVisible(editor)) {
+ return {status: "no-editor"};
+ }
+ const protyle = editor.protyle;
+ const root = protyle.wysiwyg.element;
+ if (protyle.disabled || protyle.element.getAttribute("data-readonly") === "true" ||
+ root.getAttribute("data-readonly") === "true") {
+ return {status: "readonly"};
+ }
+ if (!options.range && currentRange &&
+ root.contains(currentRange.startContainer) !== root.contains(currentRange.endContainer)) {
+ return {status: "invalid-range"};
+ }
+ const range = options.range ? getEditorFocusRange(root, options.range) :
+ getEditorFocusRange(root, currentRange, getRememberedEditorFocusRange(root));
+ if (!range) {
+ return {status: "invalid-range"};
+ }
+ if (!isEditableBoundary(root, range.startContainer) || !isEditableBoundary(root, range.endContainer) ||
+ Array.from(root.querySelectorAll('[data-type="NodeBlockQueryEmbed"], [data-type="NodeAttributeView"], [data-readonly="true"]'))
+ .some(element => range.intersectsNode(element))) {
+ return {status: "unsupported-range"};
+ }
+ if (options.data === "") {
+ return {status: "empty-content"};
+ }
+ const block = hasClosestBlock(range.startContainer);
+ const inCode = block && (block.getAttribute("data-type") === "NodeCodeBlock" ||
+ protyle.toolbar.getCurrentType(range).includes("code"));
+ let html = inCode ? options.data : options.dataType === "html" ?
+ protyle.lute.HTML2BlockDOM(options.data) : protyle.lute.Md2BlockDOM(options.data);
+ if (!inCode) {
+ if (getProtyleUnsupportedPasteBlocks(protyle)?.(html).length) {
+ return {status: "unsupported-content"};
+ }
+ html = getProtyleBlockDOMSanitizer(protyle)?.(html) ?? html;
+ }
+ if (!html) {
+ return {status: "empty-content"};
+ }
+ if (!restoreEditorFocusRange(root, range)) {
+ return {status: "unsupported-range"};
+ }
+ protyle.toolbar.range = range;
+ insertHTML(html, protyle, false, true, true);
+ blockRender(protyle, root);
+ processRender(root);
+ highlightRender(root);
+ avRender(root, protyle);
+ const insertedRange = selection?.rangeCount ? selection.getRangeAt(0) : undefined;
+ if (insertedRange) {
+ rememberEditorFocusRange(root, insertedRange);
+ }
+ return {status: "inserted"};
+};
diff --git a/app/src/protyle/util/editorFocusMemory.ts b/app/src/protyle/util/editorFocusMemory.ts
new file mode 100644
index 0000000000..a8847237f8
--- /dev/null
+++ b/app/src/protyle/util/editorFocusMemory.ts
@@ -0,0 +1,76 @@
+interface IRememberedBoundary {
+ node: Node;
+ value: string | null;
+ before?: ChildNode;
+ after?: ChildNode;
+}
+
+interface IRememberedRange {
+ range: Range;
+ start: IRememberedBoundary;
+ end: IRememberedBoundary;
+}
+
+const rememberBoundary = (node: Node, offset: number): IRememberedBoundary => ({
+ node, value: node.nodeValue, before: node.childNodes[offset - 1], after: node.childNodes[offset],
+});
+
+const isBoundaryAvailable = (saved: IRememberedBoundary, node: Node, offset: number) =>
+ saved.node === node && node.isConnected && saved.value === node.nodeValue &&
+ saved.before === node.childNodes[offset - 1] && saved.after === node.childNodes[offset];
+
+const editors = new WeakSet<HTMLElement>();
+const documents = new WeakSet<Document>();
+const ranges = new WeakMap<HTMLElement, IRememberedRange>();
+
+export const rememberEditorFocusRange = (element: HTMLElement, range: Range) => {
+ if (!element.contains(range.startContainer) || !element.contains(range.endContainer)) {
+ return;
+ }
+ ranges.set(element, {
+ range: range.cloneRange(),
+ start: rememberBoundary(range.startContainer, range.startOffset),
+ end: rememberBoundary(range.endContainer, range.endOffset),
+ });
+};
+
+export const getRememberedEditorFocusRange = (element: HTMLElement) => {
+ const saved = ranges.get(element);
+ // DOM 重建后浏览器会把 Range 移到父节点,不能将该位置当作原插入位置。
+ if (!saved || !isBoundaryAvailable(saved.start, saved.range.startContainer, saved.range.startOffset) ||
+ !isBoundaryAvailable(saved.end, saved.range.endContainer, saved.range.endOffset) ||
+ !element.contains(saved.start.node) || !element.contains(saved.end.node)) {
+ ranges.delete(element);
+ return undefined;
+ }
+ return saved.range.cloneRange();
+};
+
+export const bindEditorFocusMemory = (element: HTMLElement) => {
+ editors.add(element);
+ const ownerDocument = element.ownerDocument;
+ if (!documents.has(ownerDocument)) {
+ documents.add(ownerDocument);
+ const remember = () => {
+ const selection = ownerDocument.getSelection();
+ if (!selection?.rangeCount) {
+ return;
+ }
+ const range = selection.getRangeAt(0);
+ const node = range.startContainer;
+ const root = (node.nodeType === 1 ? node as Element : node.parentElement)
+ ?.closest<HTMLElement>(".protyle-wysiwyg");
+ if (root && editors.has(root)) {
+ rememberEditorFocusRange(root, range);
+ }
+ };
+ ownerDocument.addEventListener("selectionchange", remember);
+ // 在外部按钮、输入框或触摸操作改变浏览器选区前保存正文位置。
+ ownerDocument.addEventListener("pointerdown", remember, true);
+ ownerDocument.addEventListener("focusout", remember, true);
+ }
+ return () => {
+ editors.delete(element);
+ ranges.delete(element);
+ };
+};
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index 9eceb3941e..36f172fc55 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -1,4 +1,5 @@
import {toggleMenu} from "../../menus/menuToggle";
+import {bindEditorFocusMemory} from "../util/editorFocusMemory";
import {recordReplacementUndo} from "./replacementInput";
import {expandAndroidWordSelection, isAndroidWordSelectionPending} from "../../mobile/util/wordSelection";
import {bindBoundedBlockDragSelect} from "./boundedBlockDragSelect";
@@ -413,6 +414,7 @@ export class WYSIWYG {
private largeListVirtualizer?: LargeListVirtualizer;
private largeTableVirtualizer?: LargeTableVirtualizer;
private disposeSpellcheckFocus?: () => void;
+ private disposeFocusMemory?: () => void;
private disposeEmbedToolbarVisibility?: () => void;
private disposeHeadingFoldIndicators?: () => void;
private disposeBoundedBlockDragSelect?: () => void;
@@ -536,6 +538,7 @@ export class WYSIWYG {
this.protyle = protyle;
this.element = document.createElement("div");
this.element.className = "protyle-wysiwyg";
+ this.disposeFocusMemory = bindEditorFocusMemory(this.element);
this.element.setAttribute("spellcheck", window.siyuan.config.editor.spellcheck.toString());
// Android 和 iPhone 的原生编辑限于正文节点,避免输入法修改列表等结构容器。
this.element.setAttribute("contenteditable", (isIPhone() || isAndroid()) ? "false" : "true");
@@ -593,6 +596,7 @@ export class WYSIWYG {
}
public destroy() {
+ this.disposeFocusMemory?.();
this.imageTitleEditor?.destroy();
this.persistComposition?.();
this.persistComposition = undefined;