Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: command palette fixes (#7669)

01zulfi76f27adbea69
diff --git a/apps/web/src/dialogs/command-palette/command-palette-dialog.tsx b/apps/web/src/dialogs/command-palette/command-palette-dialog.tsx
index bdf1c40d4..8ef92718b 100644
--- a/apps/web/src/dialogs/command-palette/command-palette-dialog.tsx
+++ b/apps/web/src/dialogs/command-palette/command-palette-dialog.tsx
@@ -72,7 +72,7 @@ export const CommandPaletteDialog = DialogManager.register(
if (!defaultCommands.current)
defaultCommands.current = await getDefaultCommands();
const commands = props.isCommandMode
- ? commandSearch(query, defaultCommands.current)
+ ? sortCommands(commandSearch(query, defaultCommands.current))
: await dbSearch(query);
const groups = commands.reduce(
(acc, command) => {
@@ -120,6 +120,7 @@ export const CommandPaletteDialog = DialogManager.register(
if (e.key == "Enter") {
e.preventDefault();
const command = commands.value.commands[selected];
+ if (!command) return;
command.action?.(command, {
openInNewTab: e.ctrlKey || e.metaKey
});
@@ -280,9 +281,10 @@ export const CommandPaletteDialog = DialogManager.register(
{command.group === "recent" && (
<Button
title={strings.removeFromRecents()}
- onClick={(e) => {
+ onClick={async (e) => {
e.stopPropagation();
removeRecentCommand(command.id);
+ defaultCommands.current = await getDefaultCommands();
commands.refresh();
}}
variant="secondary"
@@ -350,6 +352,7 @@ function commandSearch(query: string, commands: Command[]) {
return fuzzy(
query,
commands,
+ (command) => command.id + command.group,
{
title: 10,
group: 5
@@ -419,6 +422,7 @@ function useDatabaseFuzzySearch() {
for (const item of fuzzy(
query,
items,
+ (item) => item.id,
{
title: 10
},
@@ -529,3 +533,27 @@ function getCommandPaletteHelp(isCommandMode: boolean) {
])
];
}
+
+/**
+ * commands need to be sorted wrt groups,
+ * meaning commands of same group should be next to each other,
+ * and recent commands should be at the top
+ */
+function sortCommands(commands: Command[]) {
+ const recent: Command[] = [];
+ const sortedWrtGroups: Command[][] = [];
+ for (const command of commands) {
+ const group = command.group;
+ if (group === "recent") {
+ recent.push(command);
+ continue;
+ }
+ const index = sortedWrtGroups.findIndex((c) => c[0].group === group);
+ if (index === -1) {
+ sortedWrtGroups.push([command]);
+ } else {
+ sortedWrtGroups[index].push(command);
+ }
+ }
+ return recent.concat(sortedWrtGroups.flat());
+}
diff --git a/apps/web/src/dialogs/command-palette/commands.ts b/apps/web/src/dialogs/command-palette/commands.ts
index 7d2e62508..7263654a5 100644
--- a/apps/web/src/dialogs/command-palette/commands.ts
+++ b/apps/web/src/dialogs/command-palette/commands.ts
@@ -234,7 +234,10 @@ export async function getDefaultCommands(): Promise<Command[]> {
}
export function getCommandById(id: string): Command | undefined {
- return staticCommands.find((command) => command.id === id);
+ return (
+ staticCommands.find((command) => command.id === id) ||
+ getEditorCommands().find((command) => command.id === id)
+ );
}
export async function resolveRecentCommand(
diff --git a/packages/core/src/api/lookup.ts b/packages/core/src/api/lookup.ts
index 86e2a50fb..fce1a16d7 100644
--- a/packages/core/src/api/lookup.ts
+++ b/packages/core/src/api/lookup.ts
@@ -217,6 +217,7 @@ export default class Lookup {
return fuzzy(
query,
items,
+ (item) => item.id,
Object.fromEntries(
fields.filter((f) => !f.ignore).map((f) => [f.name, f.weight || 1])
) as Record<keyof T, number>,
diff --git a/packages/core/src/utils/__tests__/fuzzy.test.ts b/packages/core/src/utils/__tests__/fuzzy.test.ts
index bf7a6470f..f35cb6c5c 100644
--- a/packages/core/src/utils/__tests__/fuzzy.test.ts
+++ b/packages/core/src/utils/__tests__/fuzzy.test.ts
@@ -37,7 +37,9 @@ describe("lookup.fuzzy", () => {
}
];
const query = "ems";
- expect(fuzzy(query, items, { title: 1 })).toStrictEqual([items[2]]);
+ expect(fuzzy(query, items, (item) => item.id, { title: 1 })).toStrictEqual([
+ items[2]
+ ]);
});
describe("opts.prefix", () => {
test("should prefix matched field with provided value when given", () => {
@@ -56,6 +58,7 @@ describe("lookup.fuzzy", () => {
fuzzy(
query,
items,
+ (item) => item.id,
{ title: 1 },
{
prefix: "prefix-"
@@ -81,6 +84,7 @@ describe("lookup.fuzzy", () => {
fuzzy(
query,
items,
+ (item) => item.id,
{ title: 1 },
{
suffix: "-suffix"
diff --git a/packages/core/src/utils/fuzzy.ts b/packages/core/src/utils/fuzzy.ts
index 075f4228f..0597812b1 100644
--- a/packages/core/src/utils/fuzzy.ts
+++ b/packages/core/src/utils/fuzzy.ts
@@ -20,9 +20,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { match, surround } from "fuzzyjs";
import { clone } from "./clone";
-export function fuzzy<T extends { id: string }>(
+export function fuzzy<T>(
query: string,
items: T[],
+ getIdentifier: (item: T) => string,
fields: Partial<Record<keyof T, number>>,
options: {
limit?: number;
@@ -41,11 +42,13 @@ export function fuzzy<T extends { id: string }>(
for (const item of items) {
if (options.limit && results.size >= options.limit) break;
+ const identifier = getIdentifier(item);
+
for (const field in fields) {
const result = match(query, `${item[field]}`);
if (!result.match) continue;
- const oldMatch = results.get(item.id);
+ const oldMatch = results.get(identifier);
const clonedItem = oldMatch?.item || clone(item);
if (options.suffix || options.prefix) {
@@ -58,7 +61,7 @@ export function fuzzy<T extends { id: string }>(
if (oldMatch) {
oldMatch.score += result.score * (fields[field] || 1);
} else {
- results.set(item.id, {
+ results.set(identifier, {
item: clonedItem,
score: result.score * (fields[field] || 1)
});