From 66eb289d6e911e9c931520a5e35f2003ef8715c9 Mon Sep 17 00:00:00 2001 From: Evgenii Karagodin Date: Wed, 4 Jan 2023 22:37:09 +0700 Subject: [PATCH] Prevent losing commands with context (#254) --- web/editor.tsx | 21 +++++++++++++++++++-- web/reducer.ts | 12 ++---------- web/types.ts | 1 + 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/web/editor.tsx b/web/editor.tsx index f700c43..3ce6da9 100644 --- a/web/editor.tsx +++ b/web/editor.tsx @@ -74,7 +74,7 @@ import { vim, yUndoManagerKeymap, } from "./deps.ts"; -import { CommandHook } from "./hooks/command.ts"; +import { AppCommand, CommandHook } from "./hooks/command.ts"; import { SlashCommandHook } from "./hooks/slash_command.ts"; import { PathPageNavigator } from "./navigator.ts"; import reducer from "./reducer.ts"; @@ -926,7 +926,7 @@ export class Editor { }); } }} - commands={viewState.commands} + commands={this.getCommandsByContext(viewState)} vimMode={viewState.uiOptions.vimMode} darkMode={viewState.uiOptions.darkMode} completer={this.miniEditorComplete.bind(this)} @@ -1069,6 +1069,23 @@ export class Editor { preactRender(, container); } + private getCommandsByContext( + state: AppViewState, + ): Map { + const commands = new Map(state.commands); + for (const [k, v] of state.commands.entries()) { + if ( + v.command.contexts && + (!state.showCommandPaletteContext || + !v.command.contexts.includes(state.showCommandPaletteContext)) + ) { + commands.delete(k); + } + } + + return commands; + } + private getContext(): string | undefined { const state = this.editorView!.state; const selection = state.selection.main; diff --git a/web/reducer.ts b/web/reducer.ts index e345fa4..9675e5e 100644 --- a/web/reducer.ts +++ b/web/reducer.ts @@ -63,25 +63,17 @@ export default function reducer( }; } case "show-palette": { - const commands = new Map(state.commands); - for (const [k, v] of state.commands.entries()) { - if ( - v.command.contexts && - (!action.context || !v.command.contexts.includes(action.context)) - ) { - commands.delete(k); - } - } return { ...state, - commands, showCommandPalette: true, + showCommandPaletteContext: action.context, }; } case "hide-palette": return { ...state, showCommandPalette: false, + showCommandPaletteContext: undefined, }; case "command-run": return { diff --git a/web/types.ts b/web/types.ts index d730684..7ef9970 100644 --- a/web/types.ts +++ b/web/types.ts @@ -29,6 +29,7 @@ export type AppViewState = { isLoading: boolean; showPageNavigator: boolean; showCommandPalette: boolean; + showCommandPaletteContext?: string; unsavedChanges: boolean; panels: { [key: string]: PanelConfig }; allPages: Set;