Skip to content

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

:bug: Merge selected blocks when inserting Tab text https://github.com/siyuan-note/siyuan/issues/19798

Daniel60ec4c096078
diff --git a/app/src/protyle/wysiwyg/keydown.ts b/app/src/protyle/wysiwyg/keydown.ts
index f26ed53406..e6237cab72 100644
--- a/app/src/protyle/wysiwyg/keydown.ts
+++ b/app/src/protyle/wysiwyg/keydown.ts
@@ -1,7 +1,6 @@
import type {BlockQueryRequestInput} from "../../types/api";
import {isProtyleListItemFragment} from "../runtimeCapabilities";
import {hideElements} from "../ui/hideElements";
-import {insertHTML} from "../util/insertHTML";
import {isTabTextBoundary} from "./tabsBoundary";
import {isNotCtrl, isOnlyMeta, updateHotkeyTip, writeText} from "../util/compatibility";
import {
@@ -2589,11 +2588,13 @@ 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) {
+ // 跨块替换需合并末尾文本块,并保留块引用确认和撤销信息。
+ if (!range.collapsed && endElement && nodeElement !== endElement) {
if (!event.shiftKey) {
- insertHTML(window.siyuan.config.editor.codeTabSpaces === 0 ? "\t" :
- "".padStart(window.siyuan.config.editor.codeTabSpaces, " "), protyle);
+ await removeCrossBlockRange(protyle, range, nodeElement, endElement, false, {
+ text: window.siyuan.config.editor.codeTabSpaces === 0 ? "\t" :
+ "".padStart(window.siyuan.config.editor.codeTabSpaces, " "),
+ });
}
return true;
}
diff --git a/app/src/protyle/wysiwyg/tabKeydown.test.ts b/app/src/protyle/wysiwyg/tabKeydown.test.ts
index 5cfb067de8..8e027368fb 100644
--- a/app/src/protyle/wysiwyg/tabKeydown.test.ts
+++ b/app/src/protyle/wysiwyg/tabKeydown.test.ts
@@ -17,30 +17,32 @@ const findTabBranch = (node: import("typescript").Node) => {
source.forEachChild(findTabBranch);
assert.ok(tabBranch);
-const compiled = transpileModule(`const handleTab = (event, range, nodeElement, endElement, protyle) => {
+const compiled = transpileModule(`const handleTab = async (event, range, nodeElement, endElement, protyle) => {
const blockSelectionModeElement = undefined;
${tabBranch.getText(source)}
};
handleTab;`, {compilerOptions: {module: ModuleKind.None, target: ScriptTarget.ES2021}}).outputText;
const setup = (codeTabSpaces = 4) => {
- const blocks = ["1", "2", "3", "4", "5", "6"];
const commands: Array<{name: string, value: string}> = [];
const codeBlockTabs: boolean[] = [];
- const inserted: string[] = [];
+ const replacements: Array<{range: Range, start: HTMLElement, end: HTMLElement,
+ skipRefCheck: boolean, text: string}> = [];
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}}}},
- insertHTML: (html: string) => inserted.push(html),
+ removeCrossBlockRange: async (_protyle: IProtyle, range: Range, start: HTMLElement,
+ end: HTMLElement, skipRefCheck: boolean, replacement: {text: string}) => {
+ replacements.push({range, start, end, skipRefCheck, text: replacement.text});
+ },
tabCodeBlock: (_protyle: unknown, _block: unknown, _range: unknown, outdent: boolean) => {
codeBlockTabs.push(outdent);
},
}) as (event: KeyboardEvent, range: Range, nodeElement: HTMLElement, endElement: HTMLElement,
- protyle: IProtyle) => boolean;
+ protyle: IProtyle) => Promise<boolean>;
const event = {
key: "Tab",
altKey: false,
@@ -52,34 +54,39 @@ const setup = (codeTabSpaces = 4) => {
} as KeyboardEvent;
const paragraph = {getAttribute: () => "NodeParagraph"} as unknown as HTMLElement;
const codeBlock = {getAttribute: () => "NodeCodeBlock"} as unknown as HTMLElement;
- return {blocks, commands, inserted, codeBlockTabs, handleTab, event, paragraph, codeBlock};
+ return {commands, replacements, codeBlockTabs, handleTab, event, paragraph, codeBlock};
};
-test("Tab replaces cross-block text through editor insertion instead of native DOM editing", () => {
- const {blocks, commands, inserted, handleTab, event, paragraph} = setup();
- const result = handleTab(event, {collapsed: false} as Range, paragraph,
- {getAttribute: () => "NodeParagraph"} as unknown as HTMLElement, {} as IProtyle);
+test("Tab replaces cross-block text through the merging removal path", async () => {
+ const {commands, replacements, handleTab, event, paragraph} = setup();
+ const range = {collapsed: false} as Range;
+ const end = {getAttribute: () => "NodeParagraph"} as unknown as HTMLElement;
+ const result = await handleTab(event, range, paragraph, end, {} 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);
- assert.deepEqual(inserted, [" "]);
+ assert.equal(replacements.length, 1);
+ assert.equal(replacements[0].range, range);
+ assert.equal(replacements[0].start, paragraph);
+ assert.equal(replacements[0].end, end);
+ assert.equal(replacements[0].skipRefCheck, false);
+ assert.equal(replacements[0].text, " ");
});
-test("cross-block Tab honors literal tabs while Shift+Tab preserves the selection", () => {
- const {commands, inserted, handleTab, event, paragraph} = setup(0);
+test("cross-block Tab honors literal tabs while Shift+Tab preserves the selection", async () => {
+ const {commands, replacements, handleTab, event, paragraph} = setup(0);
const end = {getAttribute: () => "NodeParagraph"} as unknown as HTMLElement;
- handleTab(event, {collapsed: false} as Range, paragraph, end, {} as IProtyle);
- assert.deepEqual(inserted, ["\t"]);
- handleTab({...event, shiftKey: true}, {collapsed: false} as Range, paragraph, end, {} as IProtyle);
- assert.deepEqual(inserted, ["\t"]);
+ await handleTab(event, {collapsed: false} as Range, paragraph, end, {} as IProtyle);
+ assert.equal(replacements[0].text, "\t");
+ await handleTab({...event, shiftKey: true}, {collapsed: false} as Range, paragraph, end, {} as IProtyle);
+ assert.equal(replacements.length, 1);
assert.equal(commands.length, 0);
});
-test("Tab still inserts spaces in one paragraph and indents one code block", () => {
+test("Tab still inserts spaces in one paragraph and indents one code block", async () => {
const {commands, codeBlockTabs, handleTab, event, paragraph, codeBlock} = setup();
- assert.equal(handleTab(event, {collapsed: false} as Range, paragraph, paragraph, {} as IProtyle), true);
+ assert.equal(await 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.equal(await handleTab(event, {collapsed: false} as Range, codeBlock, codeBlock, {} as IProtyle), true);
assert.deepEqual(codeBlockTabs, [false]);
});