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: Restore Enter opening selected search results https://github.com/siyuan-note/siyuan/issues/20447
Daniele40832edb487
diff --git a/app/src/boot/globalEvent/searchKeydown.test.ts b/app/src/boot/globalEvent/searchKeydown.test.tsindex cff53385d5..d912dea12b 100644--- a/app/src/boot/globalEvent/searchKeydown.test.ts+++ b/app/src/boot/globalEvent/searchKeydown.test.ts@@ -2,15 +2,37 @@ 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 {createSourceFile, forEachChild, isCallExpression, isVariableStatement, ModuleKind, ScriptTarget, transpileModule} from "typescript";-const runEnter = (targetId: string, method: number, resultType: string | undefined) => {+const evaluate = (source: string, globals: Record<string, unknown>) => {+ runInNewContext(transpileModule(source, {+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},+ }).outputText, globals);+};++const inputHandlers = (method: number) => {+ const handlers: Record<string, (event: any) => void> = {};+ const source = createSourceFile("util.ts", readFileSync(__dirname + "/../../search/util.ts", "utf8"),+ ScriptTarget.ES2021, true);+ const visit = (node: import("typescript").Node) => {+ if (isCallExpression(node) && node.expression.getText(source) === "searchInputElement.addEventListener" &&+ ["\"keydown\"", "\"beforeinput\""].includes(node.arguments[0].getText(source))) {+ evaluate(node.getText(source), {config: {method}, electronUndo: (): void => undefined,+ searchInputElement: {addEventListener: (type: string, handler: (event: any) => void) => handlers[type] = handler}});+ }+ forEachChild(node, visit);+ };+ visit(source);+ return handlers;+};++const runEnter = (targetId: string, method: number, resultType: string | undefined, bubble = false, keyword = "missing") => {const calls: string[] = [];const hidden = {classList: {contains: () => true}};const current = resultType ? {getAttribute: () => resultType, dataset: {}} : null;const element = {querySelector: (selector: string) => {if (selector === "#searchAssets" || selector === "#searchUnRefPanel") return hidden;- if (selector === "#searchInput") return {value: "missing"};+ if (selector === "#searchInput") return {value: keyword};return {querySelector: () => current};}};const dialog = {element: {contains: () => true, querySelector: (selector: string) => selector === "#searchList" ? {} : element},@@ -23,7 +45,25 @@ const runEnter = (targetId: string, method: number, resultType: string | undefin}).outputText, {exports, require: () => dependencies, getSelection: () => ({rangeCount: 1, getRangeAt: () => ({startContainer: {}})}),window: {siyuan: {dialogs: [dialog], config: {keymap: {general: {}, editor: {general: {}}}}, storage: {}, menus: {menu: {element: hidden}}}}});const handle = exports.searchKeydown as typeof import("./searchKeydown").searchKeydown;- const handled = handle({} as Parameters<typeof handle>[0], {key: "Enter", target: {id: targetId}} as unknown as KeyboardEvent);+ const event = {key: "Enter", target: {id: targetId}, defaultPrevented: false,+ preventDefault() { this.defaultPrevented = true; }, stopPropagation(): void {}};+ let handled = false;+ if (bubble) {+ inputHandlers(method).keydown(event);+ const source = createSourceFile("keydown.ts", readFileSync(__dirname + "/keydown.ts", "utf8"),+ ScriptTarget.ES2021, true);+ const declaration = source.statements.find(statement => isVariableStatement(statement) &&+ statement.declarationList.declarations.some(item => item.name.getText(source) === "windowKeyDown"));+ assert.ok(declaration);+ evaluate(declaration.getText(source) + "\nwindowKeyDown({}, event);", {+ exports: {}, event, switchDialog: undefined, filterHotkey: () => false,+ searchKeydown: (app: Parameters<typeof handle>[0], event: KeyboardEvent) => handled = handle(app, event),+ isWindow: () => false, bindMenuKeydown: () => true,+ });+ assert.equal(event.defaultPrevented, true);+ } else {+ handled = handle({} as Parameters<typeof handle>[0], event as unknown as KeyboardEvent);+ }return {calls, handled};};@@ -41,3 +81,22 @@ test("search input Enter retains keyword document creation and ordinary result oassert.deepEqual(runEnter("searchInput", 0, "search-result"), {calls: ["open"], handled: true});assert.deepEqual(runEnter("searchInput", 2, "search-result"), {calls: [], handled: false});});++test("search input Enter reaches global result opening before newline prevention", () => {+ for (const method of [0, 1, 3]) {+ for (const keyword of ["", "missing"]) {+ assert.deepEqual(runEnter("searchInput", method, "search-result", true, keyword), {calls: ["open"], handled: true});+ }+ }+ assert.deepEqual(runEnter("searchInput", 0, "search-new", true), {calls: ["newFile"], handled: true});+});++test("search input rejects non-SQL line breaks and preserves SQL multiline editing", () => {+ for (const method of [0, 1, 2, 3]) {+ for (const inputType of ["insertLineBreak", "insertText", "insertCompositionText"]) {+ const event = {inputType, defaultPrevented: false, preventDefault() { this.defaultPrevented = true; }};+ inputHandlers(method).beforeinput(event);+ assert.equal(event.defaultPrevented, inputType === "insertLineBreak" && method !== 2);+ }+ }+});diff --git a/app/src/search/util.ts b/app/src/search/util.tsindex f300bef3c5..d9c5c31ff9 100644--- a/app/src/search/util.ts+++ b/app/src/search/util.ts@@ -999,10 +999,12 @@ export const genSearch = (app: App, config: Config.IUILayoutTabSearchConfig, ele}saveKeyList("keys", searchInputElement.value, config);});- searchInputElement.addEventListener("keydown", (event) => {- if (event.key === "Enter" && config.method !== 2) {+ searchInputElement.addEventListener("beforeinput", (event: InputEvent) => {+ if (event.inputType === "insertLineBreak" && config.method !== 2) {event.preventDefault();}+ });+ searchInputElement.addEventListener("keydown", (event) => {electronUndo(event);});replaceInputElement.addEventListener("keydown", (event) => {