Skip to content

Commit

Permalink
fix(frontend): handle Esc key in lint panel
Browse files Browse the repository at this point in the history
  • Loading branch information
kris7t committed Nov 23, 2024
1 parent 6be4fa8 commit 78f0407
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
19 changes: 19 additions & 0 deletions subprojects/frontend/src/editor/LintPanelStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,23 @@ export default class LintPanelStore extends PanelStore {
constructor(store: EditorStore) {
super('cm-panel-lint', openLintPanel, closeLintPanel, store);
}

protected override doOpen(): void {
super.doOpen();
// CodeMirror will close the lint panel on Esc without notifying us.
// Override this behavior to keep the panel state synchronized with the store.
this.element?.addEventListener(
'keydown',
(event) => {
if (event.key === 'Escape') {
event.preventDefault();
event.stopImmediatePropagation();
this.close();
}
},
{
capture: true,
},
);
}
}
5 changes: 3 additions & 2 deletions subprojects/frontend/src/editor/PanelStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const log = getLogger('editor.PanelStore');
export default class PanelStore {
state = false;

element: Element | undefined;
element: HTMLElement | undefined;

constructor(
readonly panelClass: string,
Expand Down Expand Up @@ -99,7 +99,8 @@ export default class PanelStore {
// where we control the creation of the element, so that we can have a uniform way to
// access panel created by both CodeMirror and us.
this.element =
view.dom.querySelector(`.${this.panelClass}.cm-panel`) ?? undefined;
view.dom.querySelector<HTMLElement>(`.${this.panelClass}.cm-panel`) ??
undefined;
if (this.element === undefined) {
log.error('Failed to add panel', this.panelClass, 'to DOM');
return;
Expand Down

0 comments on commit 78f0407

Please sign in to comment.