| 1 |
import { useEditModeStore } from '../state/edit-mode'; |
| 2 |
import { useQuickEditStore } from '../state/store'; |
| 3 |
import { decideEscapeAction } from './escape-rule'; |
| 4 |
|
| 5 |
const CANCEL_EVENT = 'extendify-agent:cancel-workflow'; |
| 6 |
|
| 7 |
const onKey = (e) => { |
| 8 |
if (e.key !== 'Escape') return; |
| 9 |
const editModeOn = useEditModeStore.getState().on; |
| 10 |
if (!editModeOn) return; |
| 11 |
|
| 12 |
const { |
| 13 |
agentBlock, |
| 14 |
selected, |
| 15 |
committedSelection, |
| 16 |
setAgentBlock, |
| 17 |
clearSelected, |
| 18 |
setCommittedSelection, |
| 19 |
} = useQuickEditStore.getState(); |
| 20 |
const sameBlock = |
| 21 |
selected?.blockId != null && |
| 22 |
agentBlock?.id != null && |
| 23 |
String(selected.blockId) === String(agentBlock.id); |
| 24 |
const { action } = decideEscapeAction({ |
| 25 |
editModeOn: true, |
| 26 |
hasAgentBlock: !!agentBlock, |
| 27 |
hasQuickEditSelection: !!selected, |
| 28 |
hasCommittedSelection: !!committedSelection, |
| 29 |
sameBlock, |
| 30 |
}); |
| 31 |
|
| 32 |
if (action === 'clear-selection-and-agent-block') { |
| 33 |
window.dispatchEvent(new CustomEvent(CANCEL_EVENT)); |
| 34 |
setAgentBlock(null); |
| 35 |
clearSelected(); |
| 36 |
e.preventDefault(); |
| 37 |
e.stopPropagation(); |
| 38 |
return; |
| 39 |
} |
| 40 |
if (action === 'clear-agent-block') { |
| 41 |
window.dispatchEvent(new CustomEvent(CANCEL_EVENT)); |
| 42 |
setAgentBlock(null); |
| 43 |
e.preventDefault(); |
| 44 |
e.stopPropagation(); |
| 45 |
return; |
| 46 |
} |
| 47 |
if (action === 'clear-selection') { |
| 48 |
clearSelected(); |
| 49 |
e.preventDefault(); |
| 50 |
e.stopPropagation(); |
| 51 |
return; |
| 52 |
} |
| 53 |
if (action === 'clear-committed-selection') { |
| 54 |
setCommittedSelection(null); |
| 55 |
e.preventDefault(); |
| 56 |
e.stopPropagation(); |
| 57 |
return; |
| 58 |
} |
| 59 |
// noop: stop propagation so the old window-level fallback (which |
| 60 |
// would have toggled edit mode off) stays quiet. preventDefault is |
| 61 |
// intentionally not called — there's nothing to suppress. |
| 62 |
e.stopPropagation(); |
| 63 |
}; |
| 64 |
|
| 65 |
export const wireGlobalEscape = () => { |
| 66 |
document.addEventListener('keydown', onKey); |
| 67 |
return () => document.removeEventListener('keydown', onKey); |
| 68 |
}; |
| 69 |
|