Skip to content

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

:bug: Respect keyboard block selection in list shortcuts https://github.com/siyuan-note/siyuan/issues/20053

Danielb2356fa31379
diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.ts
index de0f1575ba..3dd7934f53 100644
--- a/app/src/protyle/wysiwyg/keydown.ts
+++ b/app/src/protyle/wysiwyg/keydown.ts
@@ -159,6 +159,7 @@ import {
clearBlockSelectionMode,
clearBlockSelection,
getBlockSelectionModeElement,
+ getBlockOperationElements,
getBlockSelectionStatusIDs,
isContinuousBlockSelection,
runBlockSelectionDelete,
@@ -2273,7 +2274,7 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
}
return;
}
- const selectsElement: HTMLElement[] = Array.from(protyle.wysiwyg.element.querySelectorAll(".protyle-wysiwyg--select"));
+ const selectsElement = getBlockOperationElements(protyle.wysiwyg.element, blockSelectionModeElement);
const hasBlockSelection = selectsElement.length > 0;
if (!hasBlockSelection) {
selectsElement.push(nodeElement);
diff --git a/app/src/protyle/wysiwyg/listShortcut.dom.test.ts b/app/src/protyle/wysiwyg/listShortcut.dom.test.ts
new file mode 100644
index 0000000000..f1d4a00400
--- /dev/null
+++ b/app/src/protyle/wysiwyg/listShortcut.dom.test.ts
@@ -0,0 +1,144 @@
+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 {createSourceFile, isImportDeclaration, isVariableStatement, ScriptTarget, transpileModule} from "typescript";
+
+const browserCases = async (source: string) => {
+ const check = require("node:assert/strict");
+ const hidden = () => Object.assign(document.createElement("div"), {className: "fn__none"});
+ const root = document.createElement("div");
+ root.className = "protyle-wysiwyg";
+ root.contentEditable = "true";
+ document.body.append(root);
+ const calls: {nodeElement: HTMLElement, type: string}[] = [];
+ const noop = (): undefined => undefined;
+ window.siyuan = {config: {keymap: {general: {}, editor: {
+ general: {}, heading: {}, list: {}, insert: {
+ list: {custom: "⌘J"}, "ordered-list": {custom: "⇧⌘J"}, check: {custom: "⌘L"},
+ },
+ }}}, menus: {menu: {element: hidden(), remove: noop}}} as any;
+ const dependencies = {
+ Constants: {KEYCODELIST: {27: "Escape", 38: "↑", 74: "J", 76: "L"},
+ CUSTOM_SY_LIST_MINDMAP: "custom-sy-list-mindmap"},
+ bindVerticalNavigationReset: noop, logKeyboardDiagnostic: noop, getAVTemplateInteractiveElement: noop,
+ prepareVerticalNavigation: noop, avKeydown: noop, fixTable: noop, commonHotkey: noop,
+ countBlockWord: noop, clearAtomicFocus: noop,
+ setInsertWbrHTML: noop, getAtomicVerticalNavigationOwner: noop,
+ formatPainter: {deactivate: () => false},
+ isMac: () => false,
+ isProtyleListItemFragment: () => false,
+ getAdjacentVisibleBlock: (element: Element) => element.previousElementSibling,
+ hideElements: (panels: string[]) => {
+ if (panels.includes("select")) {
+ root.querySelectorAll(".protyle-wysiwyg--select, .protyle-wysiwyg--select-mode").forEach(element =>
+ element.classList.remove("protyle-wysiwyg--select", "protyle-wysiwyg--select-mode"));
+ }
+ },
+ turnsOneInto: (options: any) => calls.push(options),
+ turnsIntoOneTransaction: (options: any) => calls.push({nodeElement: options.selectsElement[0], type: options.type}),
+ };
+ const keydown = new Function(...Object.keys(dependencies), source + "; return keydown;")(...Object.values(dependencies));
+ const protyle = {wysiwyg: {element: root}, selectElement: hidden(), contentElement: root,
+ toolbar: {element: hidden(), subElement: hidden()}, hint: {element: hidden()},
+ options: {render: {}, toolbar: [] as IMenuItem[]}, block: {}, scroll: {}};
+ keydown(protyle, root);
+ let failure: unknown;
+ window.addEventListener("unhandledrejection", event => {
+ event.preventDefault();
+ failure = event.reason;
+ });
+ const press = async (key: string, keyCode: number, ctrlKey = false, shiftKey = false) => {
+ const event = new KeyboardEvent("keydown", {key, keyCode, ctrlKey, shiftKey, bubbles: true, cancelable: true});
+ root.dispatchEvent(event);
+ await new Promise(resolve => setTimeout(resolve, 0));
+ if (failure) {
+ throw failure;
+ }
+ return event;
+ };
+ for (const conversion of ["OL2UL", "UL2OL", "UL2TL"]) {
+ root.innerHTML = '<div data-type="NodeMindmap" data-node-id="map" class="mindmap" data-subtype="u">' +
+ '<div class="mindmap-view" contenteditable="false">Map</div></div>' +
+ '<div data-type="NodeParagraph" data-node-id="paragraph"><div contenteditable="true">Text</div></div>';
+ const mindmap = root.firstElementChild;
+ const paragraph = root.lastElementChild;
+ root.focus();
+ getSelection().setBaseAndExtent(paragraph.firstChild.firstChild, 0, paragraph.firstChild.firstChild, 0);
+ calls.length = 0;
+ await press("Escape", 27);
+ check.ok(paragraph.classList.contains("protyle-wysiwyg--select-mode"));
+ await press("ArrowUp", 38);
+ check.ok(mindmap.classList.contains("protyle-wysiwyg--select-mode"));
+ check.equal(root.querySelector(".protyle-wysiwyg--select"), null);
+ check.ok(paragraph.contains(getSelection().anchorNode));
+ const event = await press(conversion === "UL2TL" ? "l" : "j", conversion === "UL2TL" ? 76 : 74,
+ true, conversion === "UL2OL");
+ check.ok(event.defaultPrevented);
+ check.equal(calls.length, 1);
+ check.equal(calls[0].nodeElement, mindmap);
+ check.equal(calls[0].type, conversion);
+ }
+ return "Keyboard block selection cases passed";
+};
+
+test("Escape and ArrowUp select the mindmap for all three list shortcuts", {
+ skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,
+ timeout: 45000,
+}, async () => {
+ const read = (name: string) => readFileSync(path.join(__dirname, name + ".ts"), "utf8");
+ const extract = (file: string, names?: string[]) => {
+ const source = createSourceFile(file, read(file), ScriptTarget.Latest, true);
+ if (names) {
+ return names.map(name => {
+ const declaration = source.statements.filter(isVariableStatement).flatMap(statement =>
+ Array.from(statement.declarationList.declarations)).find(item => item.name.getText(source) === name);
+ return `const ${name} = ${declaration.initializer.getText(source)};`;
+ }).join("\n");
+ }
+ return source.statements.filter(statement => !isImportDeclaration(statement))
+ .map(statement => statement.getText(source).replace(/^export /, "")).join("\n");
+ };
+ const source = transpileModule([
+ extract("../util/hasClosest"), extract("getBlock"),
+ extract("../../util/keymapBindings"), extract("../util/hotKey", ["matchHotKey", "matchAuxiliaryHotKey"]),
+ extract("../util/compatibility", ["isNotCtrl", "isOnlyMeta"]),
+ `const {BLOCK_SELECTION_CLASS, getBlockSelectionModeElement, setBlockSelectionModeElement,
+ clearBlockSelectionMode, getBlockOperationElements, getBlockSelectionStatusIDs} = (() => {${extract("blockSelection")}
+ return {BLOCK_SELECTION_CLASS, getBlockSelectionModeElement, setBlockSelectionModeElement,
+ clearBlockSelectionMode, getBlockOperationElements, getBlockSelectionStatusIDs};})();`, extract("listContext"),
+ extract("../util/selection", ["getEditorRange", "focusBlock"]),
+ extract("verticalNavigation", ["focusAtomicRegion", "focusVerticalBlockSelection"]), extract("keydown"),
+ ].join("\n"), {compilerOptions: {target: ScriptTarget.ES2021}}).outputText;
+ const temporary = mkdtempSync(path.join(tmpdir(), "siyuan-list-shortcut-test-"));
+ const script = path.join(temporary, "run.cjs");
+ const expression = `const __name = value => value; (${browserCases.toString()})(${JSON.stringify(source)})`;
+ writeFileSync(script, `const {app, BrowserWindow} = 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}});
+ try {
+ await win.loadURL("about:blank");
+ console.log(await win.webContents.executeJavaScript(${JSON.stringify(expression)}));
+ app.exit(0);
+ } catch (error) {
+ console.error(error);
+ 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, /Keyboard block selection cases passed/);
+ } finally {
+ assert.equal(path.dirname(path.resolve(temporary)), path.resolve(tmpdir()));
+ assert.ok(path.basename(temporary).startsWith("siyuan-list-shortcut-test-"));
+ rmSync(temporary, {recursive: true, force: true});
+ }
+});
diff --git a/app/src/protyle/wysiwyg/listShortcut.test.ts b/app/src/protyle/wysiwyg/listShortcut.test.ts
index 4cea8766ca..e76e871707 100644
--- a/app/src/protyle/wysiwyg/listShortcut.test.ts
+++ b/app/src/protyle/wysiwyg/listShortcut.test.ts
@@ -4,6 +4,7 @@ import {test} from "node:test";
import {runInNewContext} from "node:vm";
import {ScriptTarget, transpileModule} from "typescript";
import {getListConversionType, shouldIgnoreListShortcut} from "./listContext";
+import {getBlockOperationElements} from "./blockSelection";
const source = readFileSync("src/protyle/wysiwyg/keydown.ts", "utf8");
const start = source.indexOf(" const isMatchList =");
@@ -13,7 +14,8 @@ const compiled = transpileModule(`(async () => {${source.slice(start, end)}})()`
compilerOptions: {target: ScriptTarget.ES2021},
}).outputText;
-const runShortcut = async (type: string, subtype: string, key: string, legacy = false, embedded = false) => {
+const runShortcut = async (type: string, subtype: string, key: string, legacy = false, embedded = false,
+ selectionMode = false) => {
const selected = {
dataset: {type, subtype, nodeId: "selected"},
getAttribute: (name: string) => name === "custom-sy-list-mindmap" && legacy ? "1" : null,
@@ -26,10 +28,14 @@ const runShortcut = async (type: string, subtype: string, key: string, legacy =
Constants: {CUSTOM_SY_LIST_MINDMAP: "custom-sy-list-mindmap"},
matchHotKey: (binding: string) => binding === key,
event: {preventDefault: () => prevented = true, stopPropagation() {}},
- protyle: {wysiwyg: {element: {querySelectorAll: () => [selected]}},
+ protyle: {wysiwyg: {element: {querySelectorAll: () => selectionMode ? [] : [selected]}},
hint: {fillCommand: (value: string) => insertions.push(value)}},
nodeElement: {dataset: {type: "NodeParagraph", nodeId: "following-paragraph"}},
range: {}, isCrossBlock: false, selectText: "",
+ blockSelectionModeElement: selectionMode ? selected : undefined,
+ getBlockOperationElements,
+ getListContext: (): undefined => undefined,
+ turnsIntoOneTransaction: (options: {type: string}) => calls.push(options),
isProtyleListItemFragment: () => false,
isInEmbedBlock: () => embedded,
shouldIgnoreListShortcut, getListConversionType,
@@ -62,3 +68,16 @@ test("ordinary list cancellation and embedded mindmap protection remain intact",
assert.deepEqual(embedded.calls, []);
assert.deepEqual(embedded.insertions, []);
});
+
+test("list shortcuts use the active block in keyboard selection mode", async () => {
+ for (const legacy of [false, true]) {
+ for (const [key, type] of [["list", "OL2UL"], ["ordered-list", "UL2OL"], ["check", "UL2TL"]]) {
+ const result = await runShortcut(legacy ? "NodeList" : "NodeMindmap", "u", key, legacy, false, true);
+ assert.equal(result.calls.length, 1);
+ assert.equal(result.calls[0].type, type);
+ assert.equal(result.calls[0].nodeElement, result.selected);
+ assert.deepEqual(result.insertions, []);
+ assert.equal(result.prevented, true);
+ }
+ }
+});