Skip to content

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

:bug: Preserve blocks when tabbing across a text selection

Daniel1c59a0be145e
diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.ts
index 0c819b2953..cefd4bdb05 100644
--- a/app/src/protyle/wysiwyg/keydown.ts
+++ b/app/src/protyle/wysiwyg/keydown.ts
@@ -2586,6 +2586,10 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
// tab 需等待 list 和 table 处理完成
if (event.key === "Tab" && isNotCtrl(event) && !event.altKey) {
event.preventDefault();
+ // 跨块选区不能交给原生插入,否则会删除选中的块。
+ if (!range.collapsed && nodeElement !== endElement) {
+ return true;
+ }
let tabNodeElement = nodeElement;
let tabRange = range;
if (blockSelectionModeElement && !event.shiftKey) {
diff --git a/app/src/protyle/wysiwyg/tabKeydown.test.ts b/app/src/protyle/wysiwyg/tabKeydown.test.ts
new file mode 100644
index 0000000000..3f0fd31720
--- /dev/null
+++ b/app/src/protyle/wysiwyg/tabKeydown.test.ts
@@ -0,0 +1,72 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {ModuleKind, ScriptTarget, createSourceFile, isIfStatement, transpileModule} from "typescript";
+
+const source = createSourceFile("keydown.ts", readFileSync("src/protyle/wysiwyg/keydown.ts", "utf8"),
+ ScriptTarget.ES2021, true);
+let tabBranch: import("typescript").IfStatement;
+const findTabBranch = (node: import("typescript").Node) => {
+ if (isIfStatement(node) &&
+ node.expression.getText(source) === 'event.key === "Tab" && isNotCtrl(event) && !event.altKey') {
+ tabBranch = node;
+ }
+ node.forEachChild(findTabBranch);
+};
+source.forEachChild(findTabBranch);
+assert.ok(tabBranch);
+
+const compiled = transpileModule(`const handleTab = (event, range, nodeElement, endElement, protyle) => {
+ const blockSelectionModeElement = undefined;
+ ${tabBranch.getText(source)}
+};
+handleTab;`, {compilerOptions: {module: ModuleKind.None, target: ScriptTarget.ES2021}}).outputText;
+
+const setup = () => {
+ const blocks = ["1", "2", "3", "4", "5", "6"];
+ const commands: Array<{name: string, value: string}> = [];
+ const codeBlockTabs: boolean[] = [];
+ const handleTab = runInNewContext(compiled, {
+ isNotCtrl: () => true,
+ document: {execCommand: (name: string, _showUI: boolean, value: string) => {
+ commands.push({name, value});
+ blocks.splice(1, 4);
+ }},
+ window: {siyuan: {config: {editor: {codeTabSpaces: 4}}}},
+ tabCodeBlock: (_protyle: unknown, _block: unknown, _range: unknown, outdent: boolean) => {
+ codeBlockTabs.push(outdent);
+ },
+ }) as (event: KeyboardEvent, range: Range, nodeElement: HTMLElement, endElement: HTMLElement,
+ protyle: IProtyle) => boolean;
+ const event = {
+ key: "Tab",
+ altKey: false,
+ shiftKey: false,
+ defaultPrevented: false,
+ preventDefault() {
+ this.defaultPrevented = true;
+ },
+ } as KeyboardEvent;
+ const paragraph = {getAttribute: () => "NodeParagraph"} as unknown as HTMLElement;
+ const codeBlock = {getAttribute: () => "NodeCodeBlock"} as unknown as HTMLElement;
+ return {blocks, commands, codeBlockTabs, handleTab, event, paragraph, codeBlock};
+};
+
+test("Tab keeps a cross-block text selection and all selected blocks intact", () => {
+ const {blocks, commands, handleTab, event, paragraph} = setup();
+ const result = handleTab(event, {collapsed: false} as Range, paragraph,
+ {getAttribute: () => "NodeParagraph"} as unknown as HTMLElement, {} as IProtyle);
+ assert.equal(result, true);
+ assert.equal(event.defaultPrevented, true);
+ assert.deepEqual(blocks, ["1", "2", "3", "4", "5", "6"]);
+ assert.equal(commands.length, 0);
+});
+
+test("Tab still inserts spaces in one paragraph and indents one code block", () => {
+ const {commands, codeBlockTabs, handleTab, event, paragraph, codeBlock} = setup();
+ assert.equal(handleTab(event, {collapsed: false} as Range, paragraph, paragraph, {} as IProtyle), true);
+ assert.deepEqual(commands, [{name: "insertHTML", value: " "}]);
+ assert.equal(handleTab(event, {collapsed: false} as Range, codeBlock, codeBlock, {} as IProtyle), true);
+ assert.deepEqual(codeBlockTabs, [false]);
+});