Skip to content

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

:bug: Guard missing table selection controls on editor clicks

Danielb47e01008f63
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index 59c5f34e11..86e6ecf0f5 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -4983,10 +4983,15 @@ export class WYSIWYG {
if (item.tagName !== "DIV") {
return;
}
+ // 脑图等只读预览中的表格不携带选区控件。
+ const tableSelectElement = item.querySelector(".table__select");
+ if (!tableSelectElement) {
+ return;
+ }
if (!tableElement || item !== tableElement) {
- item.querySelector(".table__select").removeAttribute("style");
+ tableSelectElement.removeAttribute("style");
}
- if (tableElement && tableElement === item && item.querySelector(".table__select").getAttribute("style")) {
+ if (tableElement && tableElement === item && tableSelectElement.getAttribute("style")) {
// 防止合并单元格的菜单消失
event.stopPropagation();
}
diff --git a/app/src/protyle/wysiwyg/tableSelectionClick.test.ts b/app/src/protyle/wysiwyg/tableSelectionClick.test.ts
new file mode 100644
index 0000000000..a489e5108e
--- /dev/null
+++ b/app/src/protyle/wysiwyg/tableSelectionClick.test.ts
@@ -0,0 +1,83 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {join} from "node:path";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {createSourceFile, forEachChild, isExpressionStatement, Node as TSNode, ScriptTarget, transpileModule} from "typescript";
+
+const source = createSourceFile("index.ts", readFileSync(join(__dirname, "index.ts"), "utf8"), ScriptTarget.Latest);
+const loops: string[] = [];
+const findLoop = (node: TSNode) => {
+ if (isExpressionStatement(node) &&
+ node.getText(source).startsWith('this.element.querySelectorAll(".table").forEach(')) {
+ loops.push(node.getText(source));
+ }
+ forEachChild(node, findLoop);
+};
+findLoop(source);
+assert.equal(loops.length, 1);
+const compiled = transpileModule(`function click() {${loops[0]}}`, {
+ compilerOptions: {target: ScriptTarget.ES2021},
+}).outputText;
+
+const table = (hasSelection = true, tagName = "DIV") => {
+ const attributes = new Map([["style", "width: 100px; height: 30px;"]]);
+ const selection = hasSelection ? {
+ removeAttribute: (name: string) => attributes.delete(name),
+ getAttribute: (name: string) => attributes.get(name) ?? null,
+ } : null;
+ return {tagName, attributes, querySelector: (selector: string) => {
+ assert.equal(selector, ".table__select");
+ return selection;
+ }};
+};
+
+const click = (tables: ReturnType<typeof table>[], active?: ReturnType<typeof table>) => {
+ let stopped = false;
+ let continued = false;
+ const context: any = {
+ tableElement: active,
+ event: {stopPropagation: () => { stopped = true; }},
+ };
+ runInNewContext(compiled, context);
+ context.click.call({element: {querySelectorAll: (selector: string) => {
+ assert.equal(selector, ".table");
+ return tables;
+ }}});
+ continued = true;
+ return {stopped, continued};
+};
+
+test("outside clicks skip preview tables without selection controls and clear all editable table selections", () => {
+ const first = table();
+ const second = table();
+ const result = click([table(false), first, table(false), second]);
+ assert.equal(first.attributes.has("style"), false);
+ assert.equal(second.attributes.has("style"), false);
+ assert.deepEqual(result, {stopped: false, continued: true});
+});
+
+test("clicking a preview table without selection controls continues without blocking editor events", () => {
+ const preview = table(false);
+ const editable = table();
+ const result = click([preview, editable], preview);
+ assert.equal(editable.attributes.has("style"), false);
+ assert.deepEqual(result, {stopped: false, continued: true});
+});
+
+test("clicking the selected table preserves its selection and prevents the merged-cell menu from closing", () => {
+ const active = table();
+ const other = table();
+ const result = click([other, table(false), active], active);
+ assert.equal(active.attributes.has("style"), true);
+ assert.equal(other.attributes.has("style"), false);
+ assert.deepEqual(result, {stopped: true, continued: true});
+ active.attributes.clear();
+ assert.equal(click([active], active).stopped, false, "unselected tables do not stop propagation");
+});
+
+test("non-block elements with the table class remain outside table selection cleanup", () => {
+ const nonBlock = table(true, "SPAN");
+ assert.deepEqual(click([nonBlock]), {stopped: false, continued: true});
+ assert.equal(nonBlock.attributes.has("style"), true);
+});