Skip to content

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

:art: Prioritize recently used commands in the command palette https://github.com/siyuan-note/siyuan/issues/19363

Danielb48579fe9842
diff --git a/app/src/boot/globalEvent/command/panel.ts b/app/src/boot/globalEvent/command/panel.ts
index a4e2ec7298..2985aff6f4 100644
--- a/app/src/boot/globalEvent/command/panel.ts
+++ b/app/src/boot/globalEvent/command/panel.ts
@@ -2,7 +2,7 @@ import {getKeymapBindings, getKeymapItem} from "../../../util/keymapBindings";
import {Dialog} from "../../../dialog";
import type {App} from "../../../index";
import {upDownHint} from "../../../util/upDownHint";
-import {updateHotkeyTip} from "../../../protyle/util/compatibility";
+import {setStorageVal, updateHotkeyTip} from "../../../protyle/util/compatibility";
import {isMobile} from "../../../util/functions";
import {Constants} from "../../../constants";
import {hasClosestByClassName} from "../../../protyle/util/hasClosest";
@@ -11,7 +11,9 @@ import {matchHotKey} from "../../../protyle/util/hotKey";
import {captureCommandContext} from "../../../command/context";
import {ensureCommandSystem, executeCommandById} from "../../../command/executor";
import {initializeEnglishCommandTranslations} from "../../../command/english";
-import {createPaletteFocusLifecycle, queryCommandPalette} from "../../../command/paletteCore";
+import {
+ COMMAND_PALETTE_HISTORY_KEY, createPaletteFocusLifecycle, queryCommandPalette, recordPaletteCommand,
+} from "../../../command/paletteCore";
import type {ICommandContextSnapshot, ICommandDefinition} from "../../../command/types";
/// #if MOBILE
import {activeBlur} from "../../../mobile/util/keyboardToolbar";
@@ -39,7 +41,13 @@ const renderCommands = (listElement: HTMLElement, commands: ICommandDefinition[]
};
const executePaletteCommand = (app: App, commandId: string, context: ICommandContextSnapshot) => {
- void executeCommandById(app, commandId, context).catch(error => {
+ void executeCommandById(app, commandId, context).then(result => {
+ if (result.status === "executed") {
+ const history = recordPaletteCommand(window.siyuan.storage[COMMAND_PALETTE_HISTORY_KEY], commandId);
+ window.siyuan.storage[COMMAND_PALETTE_HISTORY_KEY] = history;
+ setStorageVal(COMMAND_PALETTE_HISTORY_KEY, history);
+ }
+ }).catch(error => {
console.error(`Unable to execute command "${commandId}":`, error);
});
};
@@ -117,7 +125,9 @@ export const commandPanel = (app: App) => {
inputElement.placeholder = window.siyuan.languages.commandPanel;
}
const refresh = () => {
- renderCommands(listElement, queryCommandPalette(registry, context, inputElement.value));
+ renderCommands(listElement, queryCommandPalette(
+ registry, context, inputElement.value, window.siyuan.storage[COMMAND_PALETTE_HISTORY_KEY],
+ ));
};
refresh();
inputElement.focus();
diff --git a/app/src/command/paletteCore.test.ts b/app/src/command/paletteCore.test.ts
index e2aada2102..42641e5bda 100644
--- a/app/src/command/paletteCore.test.ts
+++ b/app/src/command/paletteCore.test.ts
@@ -1,6 +1,6 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
-import {createPaletteFocusLifecycle, queryCommandPalette} from "./paletteCore";
+import {createPaletteFocusLifecycle, normalizePaletteHistory, queryCommandPalette, recordPaletteCommand} from "./paletteCore";
import {CommandRegistry} from "./registry";
import type {ICommandContextSnapshot, ICommandDefinition} from "./types";
@@ -21,6 +21,39 @@ const command = (id: string, label: string, order: number): ICommandDefinition =
});
describe("command palette core", () => {
+ it("prioritizes recent available commands and preserves the remaining order", () => {
+ const registry = new CommandRegistry();
+ registry.register(command("core.first", "First", 0), {});
+ registry.register(command("core.second", "Second", 1), {});
+ registry.register(command("plugin/recent", "Recent", 10_000), {});
+ registry.register({...command("core.hidden", "Hidden", 2), when: () => false}, {});
+ const history = ["missing", "core.hidden", "plugin/recent", "core.second"];
+ assert.deepEqual(queryCommandPalette(registry, context, "", history).map(item => item.id), [
+ "plugin/recent", "core.second", "core.first",
+ ]);
+ });
+
+ it("keeps search relevance ahead of recency and uses recency to break ties", () => {
+ const registry = new CommandRegistry();
+ registry.register(command("exact", "Open", 0), {});
+ registry.register(command("older", "Open file", 1), {});
+ registry.register(command("recent", "Open notebook", 2), {});
+ registry.register(command("unrelated", "Close", 3), {});
+ assert.deepEqual(queryCommandPalette(registry, context, "open", ["unrelated", "recent"]).map(item => item.id), [
+ "exact", "recent", "older",
+ ]);
+ });
+
+ it("deduplicates and bounds persisted history and tolerates invalid data", () => {
+ assert.deepEqual(normalizePaletteHistory({}), []);
+ assert.deepEqual(normalizePaletteHistory([null, "", 1, "a", "a", "b"]), ["a", "b"]);
+ assert.deepEqual(recordPaletteCommand(["a", "b"], "b"), ["b", "a"]);
+ const history = recordPaletteCommand(Array.from({length: 100}, (_, index) => `${index}`), "new");
+ assert.equal(history.length, 64);
+ assert.equal(history[0], "new");
+ assert.equal(history[63], "62");
+ });
+
it("keeps core commands before plugins regardless of registration time", () => {
const registry = new CommandRegistry();
registry.register(command("plugin/sample/open", "Plugin open", 10_000), {});
diff --git a/app/src/command/paletteCore.ts b/app/src/command/paletteCore.ts
index 6133b95378..6282ba69a8 100644
--- a/app/src/command/paletteCore.ts
+++ b/app/src/command/paletteCore.ts
@@ -2,8 +2,22 @@ import type {CommandRegistry} from "./registry";
import {searchCommands} from "./search";
import type {ICommandContextSnapshot} from "./types";
-export const queryCommandPalette = (registry: CommandRegistry, context: ICommandContextSnapshot, query: string) =>
- searchCommands(registry.list(context), query);
+export const COMMAND_PALETTE_HISTORY_KEY = "local-command-palette-history";
+
+export const normalizePaletteHistory = (value: unknown): string[] => Array.isArray(value) ?
+ [...new Set(value.filter((id): id is string => typeof id === "string" && !!id.trim()))].slice(0, 64) : [];
+
+export const recordPaletteCommand = (history: unknown, commandId: string) =>
+ normalizePaletteHistory([commandId, ...normalizePaletteHistory(history).filter(id => id !== commandId)]);
+
+export const queryCommandPalette = (
+ registry: CommandRegistry, context: ICommandContextSnapshot, query: string, history?: unknown,
+) => {
+ const recent = new Map(normalizePaletteHistory(history).map((id, index) => [id, index]));
+ const commands = registry.list(context).sort((first, second) =>
+ (recent.get(first.id) ?? Infinity) - (recent.get(second.id) ?? Infinity));
+ return searchCommands(commands, query);
+};
export const createPaletteFocusLifecycle = (restore: () => void) => {
let restored = false;