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: Preserve rich table cell navigation and pointer selections https://github.com/siyuan-note/siyuan/issues/19188
Daniel66822b8b66eb
diff --git a/app/src/protyle/render/tableCellRichEditor.ts b/app/src/protyle/render/tableCellRichEditor.tsindex 4a6cc84fb7..41c21f68ff 100644--- a/app/src/protyle/render/tableCellRichEditor.ts+++ b/app/src/protyle/render/tableCellRichEditor.ts@@ -11,10 +11,13 @@ import {mathRender} from "./mathRender";import {renderTableCellRichElements} from "./tableCellRich";import {cleanTableCellRichHTML, getTableCellInlineHTML, getTableCellRichBlockDOM, renderTableCellRich, serializeTableCellRich, setTableCellRich, TABLE_CELL_INLINE_ATTRIBUTE, updateTableCellEditingValue} from "../util/tableCellRich";import {TABLE_CELL_RICH_ATTRIBUTE} from "../util/tableCellRichValue";-import {focusByOffset, getSelectionOffset} from "../util/selection";+import {focusByOffset, focusByRange, getSelectionOffset} from "../util/selection";+import {getAdjacentRichTableCell, isTableCellCaretAtBoundary} from "../util/tableCellRichNavigation";+import {focusEditableAtGoalX, getCaretGoalX} from "../wysiwyg/verticalCaret";import {fixTable} from "../util/table";import {updateTableCellContentLayout} from "../util/tableCellRich";import {TABLE_CELL_SLASH_IDS} from "../util/tableCellRichMenu";+import {captureRichCellSelection, restoreRichCellSelection} from "../util/tableCellRichSelection";let activeEditor: {cell: Element, finish: () => void} | undefined;@@ -68,7 +71,8 @@ export const applyTableCellRichInlineMark = (owner: IProtyle, cells: HTMLTableCe}};-export const openTableCellRichEditor = (owner: IProtyle, cell: HTMLTableCellElement) => {+export const openTableCellRichEditor = (owner: IProtyle, cell: HTMLTableCellElement,+ navigation?: {key: string, goalX: number}, point?: {x: number, y: number}) => {if (owner.disabled || !cell.isConnected || activeEditor?.cell === cell) {return;}@@ -90,13 +94,16 @@ export const openTableCellRichEditor = (owner: IProtyle, cell: HTMLTableCellElemshowMessage(window.siyuan.languages.tableCellRichInvalid);return;}- owner.wysiwyg.tableControl?.clear();hideElements(["gutter"], owner);const selection = getSelection();const initialRange = selection.rangeCount ? selection.getRangeAt(0) : undefined;+ const richSelection = cell.hasAttribute(TABLE_CELL_RICH_ATTRIBUTE) ? captureRichCellSelection(cell, selection) : undefined;+ const preserveSelection = initialRange && !initialRange.collapsed &&+ cell.contains(initialRange.startContainer) && cell.contains(initialRange.endContainer);const initialOffset = !cell.hasAttribute(TABLE_CELL_RICH_ATTRIBUTE) && initialRange &&cell.contains(initialRange.startContainer) && cell.contains(initialRange.endContainer) ?getSelectionOffset(cell, owner.wysiwyg.element, initialRange) : undefined;+ owner.wysiwyg.tableControl?.clear();if (!cell.hasAttribute(TABLE_CELL_RICH_ATTRIBUTE)) {cell.setAttribute(TABLE_CELL_INLINE_ATTRIBUTE, cell.innerHTML);}@@ -240,6 +247,18 @@ export const openTableCellRichEditor = (owner: IProtyle, cell: HTMLTableCellElemfragment.hintElement.classList.contains("fn__none") &&fragment.protyle.toolbar.subElement.classList.contains("fn__none")) {const range = getSelection().rangeCount ? getSelection().getRangeAt(0) : undefined;+ if (range && !event.shiftKey && ["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"].includes(event.key) &&+ isTableCellCaretAtBoundary(fragment.wysiwyg, range, event.key)) {+ event.preventDefault();+ event.stopImmediatePropagation();+ const nextCell = getAdjacentRichTableCell(cell, event.key);+ if (nextCell) {+ const goalX = getCaretGoalX(range);+ finish();+ openTableCellRichEditor(owner, nextCell, {key: event.key, goalX});+ }+ return;+ }const target = range?.startContainer instanceof Element ? range.startContainer : range?.startContainer.parentElement;const inListOrCode = target?.closest('[data-type="NodeList"], [data-type="NodeCodeBlock"]');const navigate = !inListOrCode && (event.key === "Tab" ||@@ -280,6 +299,32 @@ export const openTableCellRichEditor = (owner: IProtyle, cell: HTMLTableCellElem}, {capture: true, signal});observer.observe(owner.element, {childList: true, subtree: true});fragment.focus(true);+ if (navigation) {+ const editables = fragment.wysiwyg.querySelectorAll<HTMLElement>('[contenteditable="true"]');+ const backward = navigation.key === "ArrowLeft" || navigation.key === "ArrowUp";+ const edit = backward ? editables[editables.length - 1] : editables[0];+ if (edit) {+ if ((navigation.key === "ArrowUp" || navigation.key === "ArrowDown") &&+ focusEditableAtGoalX(edit, backward ? "up" : "down", navigation.goalX, owner.contentElement)) {+ return;+ }+ const range = document.createRange();+ range.selectNodeContents(edit);+ range.collapse(!backward);+ focusByRange(range);+ }+ return;+ }+ if (richSelection && (preserveSelection || !point) && restoreRichCellSelection(fragment.wysiwyg, richSelection)) {+ return;+ }+ if (point && !preserveSelection) {+ const range = document.caretRangeFromPoint(point.x, point.y);+ if (range && fragment.wysiwyg.contains(range.startContainer)) {+ focusByRange(range);+ return;+ }+ }if (initialOffset) {const edit = fragment.wysiwyg.querySelector('[contenteditable="true"]');if (edit) {diff --git a/app/src/protyle/util/table.ts b/app/src/protyle/util/table.tsindex fbdc1f58f5..f4e03f9747 100644--- a/app/src/protyle/util/table.ts+++ b/app/src/protyle/util/table.ts@@ -409,7 +409,7 @@ export const moveColumnToRight = (protyle: IProtyle, range: Range, cellElement:export const fixTable = (protyle: IProtyle, event: KeyboardEvent, range: Range) => {const cellElement = (hasClosestByTag(range.startContainer, "TD") || hasClosestByTag(range.startContainer, "TH")) as HTMLTableCellElement;const nodeElement = hasClosestBlock(range.startContainer) as HTMLTableElement;- if (!cellElement || !nodeElement) {+ if (!cellElement || !nodeElement || !protyle.wysiwyg.element.contains(cellElement)) {return false;}// 光标在表格中,选中其他块标后按删除按钮无效diff --git a/app/src/protyle/util/tableCellRichNavigation.ts b/app/src/protyle/util/tableCellRichNavigation.tsnew file mode 100644index 0000000000..d2af8f6d90--- /dev/null+++ b/app/src/protyle/util/tableCellRichNavigation.ts@@ -0,0 +1,39 @@+import {buildTableGrid, getVerticalTableCell} from "./table";+import {isCaretAtVerticalBoundary} from "../wysiwyg/verticalCaret";++export const isTableCellCaretAtBoundary = (element: HTMLElement, range: Range, key: string) => {+ if (!range.collapsed || !element.contains(range.startContainer)) {+ return false;+ }+ const editables = Array.from(element.querySelectorAll<HTMLElement>('[contenteditable="true"]'));+ const backward = key === "ArrowLeft" || key === "ArrowUp";+ const editable = backward ? editables[0] : editables[editables.length - 1];+ if (!editable?.contains(range.startContainer)) {+ return false;+ }+ if (key === "ArrowUp" || key === "ArrowDown") {+ return isCaretAtVerticalBoundary(editable, range, backward ? "up" : "down");+ }+ const remaining = document.createRange();+ remaining.selectNodeContents(editable);+ if (backward) {+ remaining.setEnd(range.startContainer, range.startOffset);+ } else {+ remaining.setStart(range.startContainer, range.startOffset);+ }+ return remaining.toString().replace(/\u200b/g, "") === "" &&+ !remaining.cloneContents().querySelector("br, img, [data-type='inline-math']");+};++export const getAdjacentRichTableCell = (cell: HTMLTableCellElement, key: string) => {+ if (key === "ArrowUp" || key === "ArrowDown") {+ return getVerticalTableCell(cell, key === "ArrowUp" ? "up" : "down");+ }+ const table = cell.closest("table");+ if (!table) {+ return;+ }+ const {cellInfos} = buildTableGrid(table);+ const index = cellInfos.findIndex(info => info.cell === cell);+ return index < 0 ? undefined : cellInfos[index + (key === "ArrowLeft" ? -1 : 1)]?.cell;+};diff --git a/app/src/protyle/util/tableCellRichSelection.ts b/app/src/protyle/util/tableCellRichSelection.tsnew file mode 100644index 0000000000..609bd856cf--- /dev/null+++ b/app/src/protyle/util/tableCellRichSelection.ts@@ -0,0 +1,48 @@+import {focusByOffset, focusByRange, getSelectionOffset} from "./selection";++const getCellEditables = (root: Element) => Array.from(root.querySelectorAll<HTMLElement>(+ '[data-type="NodeParagraph"] > [contenteditable], [data-type="NodeHeading"] > [contenteditable], ' ++ '[data-type="NodeCodeBlock"] .hljs > [contenteditable]'));++export const captureRichCellSelection = (cell: Element, selection: Selection) => {+ if (!selection.rangeCount) {+ return;+ }+ const range = selection.getRangeAt(0);+ const editables = getCellEditables(cell);+ const startIndex = editables.findIndex(edit => edit.contains(range.startContainer));+ const endIndex = editables.findIndex(edit => edit.contains(range.endContainer));+ if (startIndex < 0 || endIndex < 0) {+ return;+ }+ const startRange = range.cloneRange();+ startRange.collapse(true);+ const endRange = range.cloneRange();+ endRange.collapse(false);+ return {+ startIndex, endIndex,+ start: getSelectionOffset(editables[startIndex], undefined, startRange).start,+ end: getSelectionOffset(editables[endIndex], undefined, endRange).start,+ backward: !range.collapsed && selection.anchorNode === range.endContainer && selection.anchorOffset === range.endOffset,+ };+};++export const restoreRichCellSelection = (root: Element, saved: ReturnType<typeof captureRichCellSelection>) => {+ if (!saved) {+ return false;+ }+ const editables = getCellEditables(root);+ const start = focusByOffset(editables[saved.startIndex], saved.start, saved.start, false);+ const end = focusByOffset(editables[saved.endIndex], saved.end, saved.end, false);+ if (!start || !end) {+ return false;+ }+ const range = document.createRange();+ range.setStart(start.startContainer, start.startOffset);+ range.setEnd(end.startContainer, end.startOffset);+ focusByRange(range);+ if (saved.backward) {+ getSelection().setBaseAndExtent(range.endContainer, range.endOffset, range.startContainer, range.startOffset);+ }+ return true;+};diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.tsindex 34c8133edf..84ab5e2da2 100644--- a/app/src/protyle/wysiwyg/index.ts+++ b/app/src/protyle/wysiwyg/index.ts@@ -4487,7 +4487,8 @@ export class WYSIWYG {richCell.closest(".protyle-wysiwyg") === this.element &&!event.target.closest("a, [data-type~='block-ref'], [data-type~='a'], img")) {event.preventDefault();- void import("../render/tableCellRichEditor").then(module => module.openTableCellRichEditor(protyle, richCell));+ void import("../render/tableCellRichEditor").then(module => module.openTableCellRichEditor(protyle, richCell,+ undefined, {x: event.clientX, y: event.clientY}));return;}if (areProtylePluginExtensionsEnabled(protyle)) {