An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:zap: Reduce table cell activation work and improve context menus https://github.com/siyuan-note/siyuan/issues/20728
Danielf56329ddcd82
diff --git a/app/src/protyle/render/tableCellInput.test.ts b/app/src/protyle/render/tableCellInput.test.tsindex ddadd1c0fa..e745521b91 100644--- a/app/src/protyle/render/tableCellInput.test.ts+++ b/app/src/protyle/render/tableCellInput.test.ts@@ -113,8 +113,13 @@ const browserCases = async (source: string, queueSource: string, editorSource: sreturn {owner, queue, element, wysiwyg, table, range};};let tableExitTarget: HTMLElement;+ let serializations = 0;const editorDependencies = {...dependencies, ...api, TABLE_CELL_INLINE_ATTRIBUTE: "data-sy-table-cell-inline",+ serializeTableCellRich: (html: string) => {+ serializations++;+ return api.serializeTableCellRich(html);+ },TABLE_CELL_RICH_ATTRIBUTE: "data-sy-table-cell-rich", TABLE_CELL_SLASH_IDS: new Set(),hintRef: noop, hintTag: noop, registerBuiltinSlashHint: (callback: unknown) => callback,getDefaultToolbar: (): string[] => [], updateOutlineCurrentBlock: noop,@@ -210,6 +215,34 @@ const browserCases = async (source: string, queueSource: string, editorSource: sawait tick();};+ for (const platform of [false, true]) {+ mobile = platform;+ for (const rich of [false, true]) {+ const {owner, element, table} = fixture();+ const cells = Array.from(table.querySelectorAll<HTMLTableCellElement>("th, td"));+ if (rich) {+ cells.forEach(cell => api.setTableCellRich(cell, "**First**\n\nSecond"));+ }+ const before = api.cleanTableCellRichHTML(table.outerHTML);+ serializations = 0;+ for (const cell of cells) {+ await open(owner, cell);+ }+ check.equal(serializations, 0, "entering and switching unchanged cells skips Markdown serialization");+ check.equal(changes.length, 0, "navigation must not create document transactions");+ check.equal(api.cleanTableCellRichHTML(table.outerHTML), before, "navigation preserves stored cell content");+ const editable = cells[cells.length - 1].querySelector<HTMLElement>('[contenteditable="true"]');+ editable.textContent = "Edited";+ // A synchronous switch must save changes even before the MutationObserver callback runs.+ await open(owner, cells[0]);+ check.equal(changes.length, 1, "switching captures edits without depending on a dirty flag");+ check.equal(serializations, 2, "only the original and changed content need serialization");+ check.match(operationHTML(changes[0].doOperations[0]), /Edited/);+ await finish(element);+ }+ }+ mobile = true;+const generalKeymap = window.siyuan.config.keymap.editor.general;window.siyuan.config.keymap.editor.general = {...generalKeymap,alignLeft: {custom: "l"}, alignCenter: {custom: "e"}, alignRight: {custom: "r"},diff --git a/app/src/protyle/render/tableCellRichEditor.ts b/app/src/protyle/render/tableCellRichEditor.tsindex 725dc978b7..5753b6422b 100644--- a/app/src/protyle/render/tableCellRichEditor.ts+++ b/app/src/protyle/render/tableCellRichEditor.ts@@ -173,7 +173,6 @@ const mountTableCellRichEditor = async (request: object, previousEditor: typeoflet source: string;try {initialBlockHTML = getTableCellRichBlockDOM(cell);- source = serializeTableCellRich(initialBlockHTML).markdown;} catch (error) {console.error(error);showMessage(window.siyuan.languages.tableCellRichInvalid);@@ -279,13 +278,23 @@ const mountTableCellRichEditor = async (request: object, previousEditor: typeoffragment.protyle.block.rootID = owner.block.rootID;fragment.protyle.block.parentID = table.dataset.nodeId;fragment.protyle.path = owner.path;+ // 进入和离开未修改的单元格无需经过 Markdown 往返转换。+ let savedBlockHTML = fragment.getBlockHTML();+ const getSource = () => source ??= serializeTableCellRich(initialBlockHTML).markdown;const commit = () => {if (!cell.isConnected || !table.isConnected || !host.isConnected || owner.disabled || composing) {return;}try {- const serialized = serializeTableCellRich(fragment.getBlockHTML());- if (serialized.markdown === source) {+ const blockHTML = fragment.getBlockHTML();+ if (blockHTML === savedBlockHTML) {+ contentChanged = false;+ return;+ }+ const previousSource = getSource();+ const serialized = serializeTableCellRich(blockHTML);+ if (serialized.markdown === previousSource) {+ savedBlockHTML = blockHTML;contentChanged = false;return;}@@ -309,6 +318,7 @@ const mountTableCellRichEditor = async (request: object, previousEditor: typeofupdateTransaction(owner, table, oldHTML, focusContext(undoSelection), {doOperations: [], undoOperations: [], context: focusContext(redoSelection),});+ savedBlockHTML = blockHTML;undoSelection = redoSelection;contentChanged = false;} catch (error) {@@ -380,7 +390,7 @@ const mountTableCellRichEditor = async (request: object, previousEditor: typeofbindTableCellRichDrag(owner, cell, fragment.wysiwyg, finish, signal,target => openTableCellRichEditor(owner, target));const captureBeforeChange = () => {- if (!contentChanged || serializeTableCellRich(fragment.getBlockHTML()).markdown === source) {+ if (!contentChanged || serializeTableCellRich(fragment.getBlockHTML()).markdown === getSource()) {undoSelection = captureRichCellSelection(fragment.wysiwyg, getSelection());}};diff --git a/app/src/protyle/util/tableControl.ts b/app/src/protyle/util/tableControl.tsindex 742ea24982..e8422a0803 100644--- a/app/src/protyle/util/tableControl.ts+++ b/app/src/protyle/util/tableControl.ts@@ -743,15 +743,23 @@ export class TableControl {}}, {capture: true, signal});this.wysiwygElement.addEventListener("contextmenu", event => {- if (!this.selection) {+ if (event.shiftKey || this.protyle.toolbar.isMultiSelectMode()) {return;}const cell = getCell(event.target);- if (!cell || getTableNode(cell) !== this.selection.node) {+ if (!cell || !getTableNode(cell) || cell.closest(".protyle-wysiwyg") !== this.wysiwygElement) {this.clear();return;}- if (!this.isCellInSelection(cell)) {+ if (!this.selection || getTableNode(cell) !== this.selection.node) {+ // 保留触屏长按和内嵌编辑器的文字菜单;未进入编辑的单元格可直接右键操作。+ if (this.touch || (event.target as Element).closest(".table__cell-editor") ||+ !finishTableCellEditing(cell)) {+ return;+ }+ restoreTableVirtualizationDOM(cell.closest("table"));+ this.selectFromCell("cell", cell, false, false);+ } else if (!this.isCellInSelection(cell)) {this.selectFromCell(this.selection.mode, cell, false, false);}event.preventDefault();diff --git a/app/src/protyle/util/tableTouch.test.ts b/app/src/protyle/util/tableTouch.test.tsindex 08c6613810..b22277a5e5 100644--- a/app/src/protyle/util/tableTouch.test.ts+++ b/app/src/protyle/util/tableTouch.test.ts@@ -793,6 +793,39 @@ const browserCases = async (sources: Record<string, string>) => {check.equal(ran, false, "unfinished composition blocks structural menu actions");}mobile = false;+ {+ const f = fixture();+ const rightClick = (target: Element, shiftKey = false) => {+ const event = new MouseEvent("contextmenu", {bubbles: true, cancelable: true, shiftKey});+ target.dispatchEvent(event);+ return event;+ };+ getSelection().removeAllRanges();+ check.equal(rightClick(f.cell(1, 1)).defaultPrevented, true);+ check.equal(menuElement.dataset.name, "table-cell");+ check.deepEqual(f.core.getSelectedCells(), [f.cell(1, 1)], "right click selects a previously inactive cell");+ f.core.selectCellRange(f.cell(1, 0), f.cell(2, 1));+ rightClick(f.cell(2, 1));+ check.equal(f.core.getSelectedCells().length, 4, "right click inside a selected range keeps the range");+ rightClick(f.cell(1, 2));+ check.deepEqual(f.core.getSelectedCells(), [f.cell(1, 2)], "right click outside the range selects the target");+ check.equal(rightClick(f.table.parentElement).defaultPrevented, false, "table block padding retains the block menu");+ check.equal(f.core.getSelectedCells().length, 0);+ check.equal(rightClick(f.cell(1, 1), true).defaultPrevented, false, "Shift preserves the native menu");+ f.setBlockMultiSelect(true);+ check.equal(rightClick(f.cell(1, 1)).defaultPrevented, false, "block multiselection retains its menu");+ f.setBlockMultiSelect(false);+ const cell = f.cell(1, 1);+ cell.innerHTML = '<div class="table__cell-editor"><div class="protyle-wysiwyg"><div contenteditable="true">Text</div></div></div>';+ const editable = cell.querySelector('[contenteditable="true"]');+ check.equal(rightClick(editable).defaultPrevented, false, "rich text editing retains its own context menu");+ cell.textContent = "A2";+ f.owner.disabled = true;+ rightClick(cell);+ check.ok(items.some(item => item.label === "copy" && !item.disabled));+ check.ok(!items.some(item => item.label === "paste" && !item.disabled), "readonly cell menus cannot paste");+ f.dispose();+ }for (const mode of ["row", "column", "both"]) {const f = fixture();f.table.style.marginBottom = "0";