An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Keep typing in newly inserted blocks https://github.com/siyuan-note/siyuan/issues/19751
Daniel87e0f8f8226c
diff --git a/app/src/block/insertEmptyBlock.test.ts b/app/src/block/insertEmptyBlock.test.tsnew file mode 100644index 0000000000..c63d414d47--- /dev/null+++ b/app/src/block/insertEmptyBlock.test.ts@@ -0,0 +1,136 @@+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, transpileModule} from "typescript";+import {+ BLOCK_SELECTION_CLASS,+ BLOCK_SELECTION_MODE_CLASS,+ clearBlockSelectionMode,+ getBlockSelectionModeElement,+} from "../protyle/wysiwyg/blockSelection";++const extract = (path: string, name: string, next: string) => {+ const source = readFileSync(path, "utf8");+ return source.slice(source.indexOf(`export const ${name} =`), source.indexOf(`export const ${next} =`));+};+const compiled = transpileModule(+ extract("src/protyle/ui/hideElements.ts", "hideElements", "hideAllElements") ++ extract("src/block/util.ts", "insertEmptyBlock", "insertEmptySuperBlockColumn"), {+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},+ }).outputText;++class TestElement {+ parentElement: TestElement;+ children: TestElement[] = [];+ attributes = new Map<string, string>();+ classes = new Set<string>();+ classList = {+ contains: (name: string) => this.classes.has(name),+ remove: (...names: string[]) => names.forEach(name => this.classes.delete(name)),+ };++ constructor(id: string, type = "NodeParagraph") {+ this.attributes.set("data-node-id", id);+ this.attributes.set("data-type", type);+ }++ get outerHTML() {+ return `<div data-node-id="${this.getAttribute("data-node-id")}" class="${[...this.classes].join(" ")}"></div>`;+ }++ getAttribute(name: string) {+ return this.attributes.get(name) || null;+ }++ removeAttribute(name: string) {+ this.attributes.delete(name);+ }++ append(...children: TestElement[]) {+ children.forEach(child => child.parentElement = this);+ this.children.push(...children);+ }++ querySelectorAll(selector: string): TestElement[] {+ return this.children.flatMap(child => {+ const matches = selector.startsWith(".") ? child.classes.has(selector.slice(1)) :+ selector === `[data-node-id="${child.getAttribute("data-node-id")}"]`;+ return [...(matches ? [child] : []), ...child.querySelectorAll(selector)];+ });+ }++ querySelector(selector: string) {+ return this.querySelectorAll(selector)[0] || null;+ }++ insertAdjacentElement(position: string, element: TestElement) {+ const siblings = this.parentElement.children;+ element.parentElement = this.parentElement;+ siblings.splice(siblings.indexOf(this) + (position === "afterend" ? 1 : 0), 0, element);+ }+}++for (const position of ["beforebegin", "afterend"]) {+ for (const targetKind of ["cursor", "element", "id", "multiple", "editing"]) {+ for (const type of ["NodeParagraph", "NodeHeading", "NodeCodeBlock"]) {+ test(`${position} from ${targetKind} at ${type} clears the old input target`, async () => {+ const editor = new TestElement("editor");+ editor.classes.add("protyle-wysiwyg");+ const first = new TestElement("first", type);+ const last = new TestElement("last");+ editor.append(first, last);+ if (targetKind !== "editing") {+ first.classes.add(BLOCK_SELECTION_MODE_CLASS);+ }+ if (targetKind === "multiple") {+ first.classes.add(BLOCK_SELECTION_CLASS);+ last.classes.add(BLOCK_SELECTION_CLASS);+ first.attributes.set("select-start", "true");+ last.attributes.set("select-end", "true");+ }+ const inserted = new TestElement("inserted");+ const range = {startContainer: first};+ const context = {undoFocusId: "first"};+ const protyle = {wysiwyg: {element: editor}};+ const exports: {insertEmptyBlock?: (...args: unknown[]) => Promise<void>} = {};+ let operations: Array<{id: string; previousID?: string; nextID?: string}>;+ let inverse: Array<{action: string; id: string; context: object}>;+ runInNewContext(compiled, {+ exports,+ clearBlockSelectionMode,+ getEditorRange: () => range,+ hasClosestBlock: (element: TestElement) => element,+ getTopAloneElement: (element: TestElement) => element,+ getUndoFocusContext: () => context,+ getPreviousBlockSibling: (): Element | undefined => undefined,+ genEmptyElement: () => inserted,+ transaction: (_protyle: unknown, forward: typeof operations, backward: typeof inverse) => {+ operations = forward;+ inverse = backward;+ },+ focusByWbr: () => range.startContainer = inserted,+ scrollCenter: () => {},+ });++ await exports.insertEmptyBlock(protyle, position,+ targetKind === "element" ? first : targetKind === "id" ? "first" : undefined);++ assert.equal(getBlockSelectionModeElement(editor as unknown as Element), undefined);+ assert.equal(editor.querySelectorAll(`.${BLOCK_SELECTION_CLASS}`).length, 0);+ assert.equal(first.getAttribute("select-start"), null);+ assert.equal(last.getAttribute("select-end"), null);+ assert.equal(range.startContainer, inserted);+ const anchor = targetKind === "multiple" && position === "afterend" ? last : first;+ assert.equal(editor.children.indexOf(inserted), editor.children.indexOf(anchor) ++ (position === "afterend" ? 1 : -1));+ assert.equal(operations[0].id, "inserted");+ assert.equal(operations[0][position === "afterend" ? "previousID" : "nextID"],+ anchor.getAttribute("data-node-id"));+ assert.equal(inverse[0].action, "delete");+ assert.equal(inverse[0].id, "inserted");+ assert.equal(inverse[0].context, context);+ });+ }+ }+}diff --git a/app/src/block/util.ts b/app/src/block/util.tsindex ef1137adee..8d1cfb4be4 100644--- a/app/src/block/util.ts+++ b/app/src/block/util.ts@@ -341,7 +341,6 @@ export const insertEmptyBlock = async (protyle: IProtyle, position: InsertPositi} else {blockElement = selectElements[selectElements.length - 1];}- hideElements(["select"], protyle);} else {blockElement = hasClosestBlock(range.startContainer) as HTMLElement;blockElement = getTopAloneElement(blockElement);@@ -356,6 +355,8 @@ export const insertEmptyBlock = async (protyle: IProtyle, position: InsertPositiif (!blockElement) {return;}+ // 插入新块前退出块选择模式,避免后续输入重新聚焦选中的块。+ hideElements(["select"], protyle);// 页签项不能容纳同级普通块,上下插入以所属页签组为目标。if (blockElement.getAttribute("data-type") === "NodeTabItem" &&blockElement.parentElement.getAttribute("data-type") === "NodeTabs") {