Skip to content

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

:bug: Restore table actions in rich cell context menus https://github.com/siyuan-note/siyuan/issues/19188

Danielfb6e1d6d3000
diff --git a/app/src/menus/protyle.ts b/app/src/menus/protyle.ts
index 7171ab45ff..d36d9732b2 100644
--- a/app/src/menus/protyle.ts
+++ b/app/src/menus/protyle.ts
@@ -903,10 +903,26 @@ export const contentMenu = (protyle: IProtyle, nodeElement: Element) => {
}
}).element);
}
- if (nodeElement.classList.contains("table") && !protyle.disabled) {
- const cellElement = hasClosestByTag(range.startContainer, "TD") || hasClosestByTag(range.startContainer, "TH");
+ const cellContext = getTableCellRichContext(protyle);
+ if ((nodeElement.classList.contains("table") || cellContext) && !protyle.disabled && !cellContext?.owner.disabled) {
+ const cellElement = cellContext?.cell || hasClosestByTag(range.startContainer, "TD") || hasClosestByTag(range.startContainer, "TH");
if (cellElement) {
- const tableMenus = tableMenu(protyle, nodeElement, cellElement as HTMLTableCellElement, range);
+ const tableRange = cellContext ? document.createRange() : range;
+ if (cellContext) {
+ tableRange.selectNodeContents(cellElement);
+ tableRange.collapse(true);
+ }
+ const tableMenus = tableMenu(cellContext?.owner || protyle,
+ cellContext ? cellElement.closest('[data-type="NodeTable"]') : nodeElement,
+ cellElement as HTMLTableCellElement, tableRange);
+ if (cellContext) {
+ prepareTableCellMenuItems([...tableMenus.insertMenus, ...tableMenus.removeMenus,
+ ...tableMenus.otherMenus, ...tableMenus.other2Menus], () => {
+ cellContext.finish();
+ tableRange.selectNodeContents(cellElement);
+ tableRange.collapse(true);
+ });
+ }
if (tableMenus.insertMenus.length > 0) {
window.siyuan.menus.menu.append(new MenuItem({
id: "separator_1",
@@ -2764,3 +2780,4 @@ export const setFoldById = (data: {
}
});
};
+import {getTableCellRichContext, prepareTableCellMenuItems} from "../protyle/util/tableCellRichContext";
diff --git a/app/src/protyle/render/tableCellRichEditor.ts b/app/src/protyle/render/tableCellRichEditor.ts
index 4fce208edf..1e7e24d10c 100644
--- a/app/src/protyle/render/tableCellRichEditor.ts
+++ b/app/src/protyle/render/tableCellRichEditor.ts
@@ -21,6 +21,7 @@ import {captureRichCellSelection, restoreRichCellSelection} from "../util/tableC
import {matchHotKey} from "../util/hotKey";
import {bindTableCellRichDrag} from "../util/tableCellRichDrag";
import {getTableCellEditorLute} from "../util/tableCellRichLute";
+import {setTableCellRichContext} from "../util/tableCellRichContext";
let activeEditor: {cell: Element, finish: () => void} | undefined;
@@ -246,6 +247,7 @@ export const openTableCellRichEditor = (owner: IProtyle, cell: HTMLTableCellElem
}
};
activeEditor = {cell, finish};
+ setTableCellRichContext(fragment.protyle, {owner, cell, finish});
const signal = controller.signal;
bindTableCellRichDrag(owner, cell, fragment.wysiwyg, finish, signal,
target => openTableCellRichEditor(owner, target));
diff --git a/app/src/protyle/util/tableCellRichContext.ts b/app/src/protyle/util/tableCellRichContext.ts
new file mode 100644
index 0000000000..5c7158aa91
--- /dev/null
+++ b/app/src/protyle/util/tableCellRichContext.ts
@@ -0,0 +1,44 @@
+type TableCellContext = {owner: IProtyle, cell: HTMLTableCellElement, finish: () => void};
+
+const contexts = new WeakMap<IProtyle, TableCellContext>();
+
+export const setTableCellRichContext = (protyle: IProtyle, context: TableCellContext) => {
+ contexts.set(protyle, context);
+};
+
+export const getTableCellRichContext = (protyle: IProtyle) => {
+ const context = contexts.get(protyle);
+ return context?.cell.contains(protyle.element) ? context : undefined;
+};
+
+// 菜单展示期间保留文字选区,执行表格操作前再提交单元格内容。
+export const prepareTableCellMenuItems = (items: IMenu[], prepare: () => void) => {
+ items.forEach(item => {
+ if (item.click) {
+ const click = item.click;
+ item.click = (...args) => {
+ prepare();
+ return click(...args);
+ };
+ }
+ if (item.bind) {
+ const bind = item.bind;
+ item.bind = element => {
+ element.addEventListener("click", event => {
+ if (!(event.target instanceof HTMLInputElement)) {
+ prepare();
+ }
+ }, {capture: true});
+ element.addEventListener("keydown", event => {
+ if (event.key === "Enter" && !event.isComposing) {
+ prepare();
+ }
+ }, {capture: true});
+ bind(element);
+ };
+ }
+ if (item.submenu) {
+ prepareTableCellMenuItems(item.submenu, prepare);
+ }
+ });
+};