a tool for shared writing and social publishing

Fix block command bar ESC key handling (#232)

* fix: BlockCommandBar ESC key handling

* Align clearCommandSearchText more with repo code style

authored by

Mark Tomlinson and committed by
GitHub
9a3d8d68 9fb4d20c

+22 -4
+22 -4
components/Blocks/BlockCommandBar.tsx
··· 6 6 import { NestedCardThemeProvider } from "components/ThemeManager/ThemeProvider"; 7 7 import { UndoManager } from "src/undoManager"; 8 8 import { useLeafletPublicationData } from "components/PageSWRDataProvider"; 9 + import { setEditorState, useEditorStates } from "src/state/useEditorState"; 9 10 10 11 type Props = { 11 12 parent: string; ··· 31 32 let { rep, undoManager } = useReplicache(); 32 33 let entity_set = useEntitySetContext(); 33 34 let { data: pub } = useLeafletPublicationData(); 35 + 36 + // This clears '/' AND anything typed after it 37 + const clearCommandSearchText = () => { 38 + if (!props.entityID) return; 39 + const entityID = props.entityID; 40 + 41 + const existingState = useEditorStates.getState().editorStates[entityID]; 42 + if (!existingState) return; 43 + 44 + const tr = existingState.editor.tr; 45 + tr.deleteRange(1, tr.doc.content.size - 1); 46 + setEditorState(entityID, { editor: existingState.editor.apply(tr) }); 47 + }; 34 48 35 49 let commandResults = blockCommands.filter((command) => { 36 50 const matchesSearch = command.name ··· 98 112 undoManager.endGroup(); 99 113 return; 100 114 } 101 - 102 - // radix menu component handles esc 103 - if (e.key === "Escape") return; 104 115 }; 105 116 window.addEventListener("keydown", listener); 106 117 ··· 108 119 }, [highlighted, setHighlighted, commandResults, rep, entity_set.set, props]); 109 120 110 121 return ( 111 - <Popover.Root open> 122 + <Popover.Root 123 + open 124 + onOpenChange={(open) => { 125 + if (!open) { 126 + clearCommandSearchText(); 127 + } 128 + }} 129 + > 112 130 <Popover.Trigger className="absolute left-0"></Popover.Trigger> 113 131 <Popover.Portal> 114 132 <Popover.Content