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: Guard against editing list markers
Vanessa31a0a5445ac8
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.tsindex 36f172fc55..952c7090c7 100644--- a/app/src/protyle/wysiwyg/index.ts+++ b/app/src/protyle/wysiwyg/index.ts@@ -14,6 +14,7 @@ import {isTabTextBoundary} from "./tabsBoundary";import {captureCompositionText} from "./compositionCaret";import {isCommittedTextInput} from "./compositionInput";import {bindIOSTouchCaret} from "./touchCaret";+import {bindListMarkerCaret} from "./listMarkerCaret";import {clearTouchBlockSelection} from "./touchBlockSelection";import {isDirectMathClick} from "../util/mathClick";import {@@ -419,6 +420,7 @@ export class WYSIWYG {private disposeHeadingFoldIndicators?: () => void;private disposeBoundedBlockDragSelect?: () => void;private disposeTouchCaret?: () => void;+ private disposeListMarkerCaret?: () => void;private imageTitleEditor: ReturnType<typeof bindImageTitleEditor>;private scheduleInput(callback: () => void | Promise<void>, delay = 0, replace = true) {@@ -551,6 +553,9 @@ export class WYSIWYG {this.bindCommonEvent(protyle);this.imageTitleEditor = bindImageTitleEditor(protyle, this.element);this.bindEvent(protyle);+ this.disposeListMarkerCaret = bindListMarkerCaret(this.element, () => !protyle.disabled &&+ !protyle.toolbar.isMultiSelectMode() && !window.siyuan.touchDragActive &&+ !getBlockSelectionModeElement(this.element));this.disposeEmbedToolbarVisibility = bindEmbedToolbarVisibility(this.element);/// #if BROWSERif (!isMobile() && !isPhablet() && navigator.userAgent.includes("Chrome/")) {@@ -605,6 +610,7 @@ export class WYSIWYG {this.disposeSpellcheckFocus?.();this.disposeBoundedBlockDragSelect?.();this.disposeTouchCaret?.();+ this.disposeListMarkerCaret?.();this.largeListVirtualizer?.destroy();this.largeTableVirtualizer?.destroy();}diff --git a/app/src/protyle/wysiwyg/listMarkerCaret.dom.test.ts b/app/src/protyle/wysiwyg/listMarkerCaret.dom.test.tsnew file mode 100644index 0000000000..5039302399--- /dev/null+++ b/app/src/protyle/wysiwyg/listMarkerCaret.dom.test.ts@@ -0,0 +1,218 @@+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 {ScriptTarget, transpileModule} from "typescript";++const browserCases = async (source: string) => {+ const check: typeof assert = require("node:assert/strict");+ const {ipcRenderer} = require("electron");+ const {bindListMarkerCaret} = new Function(source + "\nreturn {bindListMarkerCaret};")();+ const editor = document.createElement("div");+ editor.className = "protyle-wysiwyg";+ document.body.append(editor);+ const selection = getSelection();+ const tick = () => new Promise(resolve => setTimeout(resolve, 20));+ const select = (node: Node, offset = 0) => {+ selection.setBaseAndExtent(node, offset, node, offset);+ };+ const makeList = (content = "body", marker = "1.") => {+ editor.innerHTML = '<div data-type="NodeList" data-node-id="list"><div data-type="NodeListItem" ' ++ 'data-node-id="item"><div class="protyle-action" contenteditable="false"><span>' + marker ++ '</span></div><div data-type="NodeParagraph" data-node-id="paragraph"><div contenteditable="true">' ++ content + "</div></div></div></div>";+ const action = editor.querySelector<HTMLElement>(".protyle-action");+ const body = editor.querySelector<HTMLElement>('[contenteditable="true"]');+ editor.focus();+ body.focus();+ return {action, body, item: action.parentElement};+ };+ const atBodyStart = (body: HTMLElement) => {+ check.ok(body.contains(selection.anchorNode), `${selection.anchorNode?.nodeName}: ${selection.anchorNode?.textContent}`);+ const range = document.createRange();+ range.selectNodeContents(body);+ range.setEnd(selection.anchorNode, selection.anchorOffset);+ check.equal(range.toString(), "");+ check.ok(selection.isCollapsed);+ };+ const inputEvent = (inputType = "insertText", targetRanges: StaticRange[] = []) => new InputEvent("beforeinput", {+ bubbles: true, cancelable: true, inputType, data: "x", targetRanges,+ });++ // 无保护时,真实 DOM Selection 允许光标停在不可编辑的序号文本内部。+ editor.contentEditable = "true";+ const baseline = makeList();+ select(baseline.action.firstChild.firstChild);+ await tick();+ check.equal(selection.anchorNode, baseline.action.firstChild.firstChild);++ let enabled = true;+ const dispose = bindListMarkerCaret(editor, () => enabled);+ // 覆盖 iPad/桌面的外层编辑宿主以及 iPhone/Android 的独立正文宿主。+ for (const rootEditable of ["true", "false"]) {+ editor.contentEditable = rootEditable;+ for (const marker of ["1.", "123.", "•", '<svg><use href="#iconCheck"></use></svg>']) {+ for (const position of ["text", "marker", "before", "after"]) {+ const {action, body, item} = makeList("body", marker);+ select(position === "text" ? action.firstChild.firstChild : position === "marker" ? action : item,+ position === "after" ? 1 : 0);+ await tick();+ atBodyStart(body);+ const markerHTML = action.innerHTML;+ await ipcRenderer.invoke("marker-type", "测");+ check.equal(body.textContent, "测body");+ check.equal(action.innerHTML, markerHTML);+ }+ }+ }+ const empty = makeList("");+ select(empty.action);+ await tick();+ atBodyStart(empty.body);++ // 选区变化通知尚未到达时,在输入入口阻止对序号的插入、退格、删除及粘贴。+ for (const inputType of ["insertText", "deleteContentBackward", "deleteContentForward", "insertFromPaste"]) {+ const {action, body} = makeList();+ select(action.firstChild.firstChild);+ const event = inputEvent(inputType);+ check.equal(editor.dispatchEvent(event), false);+ atBodyStart(body);+ check.equal(action.textContent, "1.");+ check.equal(body.textContent, "body");+ }+ // 即便选区已经校正,输入事件携带的旧目标仍不可写入。+ const stale = makeList();+ select(stale.body.firstChild);+ const target = new StaticRange({startContainer: stale.action.firstChild.firstChild, startOffset: 0,+ endContainer: stale.action.firstChild.firstChild, endOffset: 0});+ check.equal(editor.dispatchEvent(inputEvent("insertText", [target])), false);+ const deletionTarget = new StaticRange({startContainer: stale.action.firstChild.firstChild, startOffset: 0,+ endContainer: stale.action.firstChild.firstChild, endOffset: 1});+ check.equal(editor.dispatchEvent(inputEvent("deleteContentBackward", [deletionTarget])), false);++ // 组合开始前校正光标,候选更新期间保留正文中的合法光标和选区。+ const composition = makeList();+ select(composition.action.firstChild.firstChild);+ let startWasRepaired = false;+ editor.addEventListener("compositionstart", () => {+ atBodyStart(composition.body);+ startWasRepaired = true;+ }, {once: true});+ editor.dispatchEvent(new CompositionEvent("compositionstart", {bubbles: true}));+ check.ok(startWasRepaired);+ select(composition.body.firstChild, 2);+ await tick();+ check.equal(selection.anchorOffset, 2);+ editor.dispatchEvent(new CompositionEvent("compositionend", {bubbles: true}));+ check.equal(selection.anchorOffset, 2);+ check.equal(editor.dispatchEvent(inputEvent()), true);++ // 缺失组合结束事件时,明确提交的输入恢复后续光标校正。+ editor.dispatchEvent(new CompositionEvent("compositionstart", {bubbles: true}));+ editor.dispatchEvent(new InputEvent("input", {inputType: "insertText", isComposing: false}));+ select(composition.action.firstChild.firstChild);+ await tick();+ atBodyStart(composition.body);++ // 嵌套列表只定位到当前列表项正文,不跳到外层列表的首项。+ const nested = makeList();+ const childList = document.createElement("div");+ childList.innerHTML = nested.item.outerHTML;+ nested.item.append(childList);+ const childBody = childList.querySelector<HTMLElement>('[contenteditable="true"]');+ childBody.focus();+ select(childList.querySelector(".protyle-action").firstChild.firstChild);+ await tick();+ atBodyStart(childBody);++ const normal = makeList();+ selection.setBaseAndExtent(normal.action.firstChild.firstChild, 0, normal.body.firstChild, 2);+ await tick();+ check.equal(selection.anchorNode, normal.action.firstChild.firstChild);+ check.equal(selection.focusOffset, 2);+ const crossTarget = new StaticRange({startContainer: normal.action.firstChild.firstChild, startOffset: 0,+ endContainer: normal.body.firstChild, endOffset: 2});+ check.equal(editor.dispatchEvent(inputEvent("insertText", [crossTarget])), true);++ // 代码块等其他工具栏不属于列表序号保护的范围。+ const code = document.createElement("div");+ code.setAttribute("data-type", "NodeCodeBlock");+ code.innerHTML = '<div class="protyle-action">js</div>';+ editor.append(code);+ select(code.firstChild.firstChild);+ await tick();+ check.equal(selection.anchorNode, code.firstChild.firstChild);++ // 禁用、外部编辑器及销毁后的选区不被抢占。+ enabled = false;+ select(normal.action.firstChild.firstChild);+ await tick();+ check.equal(selection.anchorNode, normal.action.firstChild.firstChild);+ enabled = true;+ const outside = document.createElement("div");+ outside.contentEditable = "true";+ outside.textContent = "outside";+ document.body.append(outside);+ outside.focus();+ select(normal.action.firstChild.firstChild);+ await tick();+ check.equal(document.activeElement, outside);+ check.equal(selection.anchorNode, normal.action.firstChild.firstChild);+ select(outside.firstChild, 2);+ await tick();+ check.equal(selection.anchorNode, outside.firstChild);+ check.equal(selection.anchorOffset, 2);+ dispose();+ normal.body.focus();+ select(normal.action.firstChild.firstChild);+ await tick();+ check.equal(selection.anchorNode, normal.action.firstChild.firstChild);+ editor.remove();+ outside.remove();+ return "List marker caret cases passed";+};++test("list marker carets are repaired before editing without disturbing body input or selections", {+ skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,+ timeout: 45000,+}, async () => {+ const compile = (file: string) => transpileModule(readFileSync(path.join(__dirname, file), "utf8")+ .replace(/^import [\s\S]*?;\r?\n/gm, "").replace(/^export /gm, ""),+ {compilerOptions: {target: ScriptTarget.ES2021}}).outputText;+ const source = compile("getBlock.ts") + "\n" + compile("compositionInput.ts") + "\n" + compile("listMarkerCaret.ts");+ const temporary = mkdtempSync(path.join(tmpdir(), "siyuan-list-marker-test-"));+ const script = path.join(temporary, "run.cjs");+ writeFileSync(script, `const {app, BrowserWindow, ipcMain} = 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, backgroundThrottling: false}});+ ipcMain.handle("marker-type", (_event, text) => win.webContents.insertText(text));+ try {+ await win.loadURL("about:blank");+ console.log(await win.webContents.executeJavaScript(${JSON.stringify(+ `const __name = value => value; (${browserCases.toString()})(${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, /List marker caret cases passed/, result.stderr);+ } finally {+ assert.equal(path.dirname(path.resolve(temporary)), path.resolve(tmpdir()));+ assert.ok(path.basename(temporary).startsWith("siyuan-list-marker-test-"));+ rmSync(temporary, {recursive: true, force: true});+ }+});diff --git a/app/src/protyle/wysiwyg/listMarkerCaret.ts b/app/src/protyle/wysiwyg/listMarkerCaret.tsnew file mode 100644index 0000000000..6b1f69aacf--- /dev/null+++ b/app/src/protyle/wysiwyg/listMarkerCaret.ts@@ -0,0 +1,89 @@+import {getContenteditableElement} from "./getBlock";+import {isCommittedTextInput} from "./compositionInput";++const getMarkerItem = (root: HTMLElement, node: Node, offset: number) => {+ if (!node || !root.contains(node)) {+ return;+ }+ const element = node.nodeType === Node.ELEMENT_NODE ? node as Element : node.parentElement;+ const marker = element?.closest(".protyle-action");+ if (marker?.parentElement?.getAttribute("data-type") === "NodeListItem") {+ return marker.parentElement;+ }+ // 原生光标也可能落在列表项容器中、序号的前后,而非序号文本内部。+ if (element === node && element.getAttribute("data-type") === "NodeListItem") {+ const action = element.querySelector(":scope > .protyle-action");+ if (action && offset <= Array.prototype.indexOf.call(element.childNodes, action) + 1) {+ return element as HTMLElement;+ }+ }+};++export const bindListMarkerCaret = (root: HTMLElement, canPlaceCaret: () => boolean) => {+ const ownerDocument = root.ownerDocument;+ let composing = false;+ const repair = () => {+ const selection = ownerDocument.getSelection();+ if (!canPlaceCaret() || !root.contains(ownerDocument.activeElement) ||+ !selection?.isCollapsed || !selection.rangeCount) {+ return false;+ }+ const item = getMarkerItem(root, selection.anchorNode, selection.anchorOffset);+ const editable = item && getContenteditableElement(item) as HTMLElement;+ if (!editable?.isContentEditable || !root.contains(editable)) {+ return false;+ }+ const range = ownerDocument.createRange();+ range.selectNodeContents(editable);+ range.collapse(true);+ editable.focus({preventScroll: true});+ selection.removeAllRanges();+ selection.addRange(range);+ return true;+ };+ const selectionChange = () => {+ if (!composing) {+ repair();+ }+ };+ const beforeInput = (event: InputEvent) => {+ if (!canPlaceCaret()) {+ return;+ }+ const invalidTarget = event.getTargetRanges().some(range => {+ const item = getMarkerItem(root, range.startContainer, range.startOffset);+ return item && item === getMarkerItem(root, range.endContainer, range.endOffset);+ });+ const repaired = !composing && repair();+ // 输入事件可能仍保存校正前的目标位置,取消这一笔,避免原生编辑删除或改写序号。+ if ((invalidTarget || repaired) && event.cancelable) {+ event.preventDefault();+ event.stopImmediatePropagation();+ }+ };+ const compositionStart = () => {+ repair();+ composing = true;+ };+ const compositionEnd = () => {+ composing = false;+ repair();+ };+ const input = (event: InputEvent) => {+ if (isCommittedTextInput(event)) {+ composing = false;+ }+ };+ ownerDocument.addEventListener("selectionchange", selectionChange);+ root.addEventListener("beforeinput", beforeInput, true);+ root.addEventListener("compositionstart", compositionStart, true);+ root.addEventListener("compositionend", compositionEnd);+ root.addEventListener("input", input);+ return () => {+ ownerDocument.removeEventListener("selectionchange", selectionChange);+ root.removeEventListener("beforeinput", beforeInput, true);+ root.removeEventListener("compositionstart", compositionStart, true);+ root.removeEventListener("compositionend", compositionEnd);+ root.removeEventListener("input", input);+ };+};