| 1 |
/** |
| 2 |
* Tracks whether a selection modifier key (Ctrl / Cmd / Shift) is currently |
| 3 |
* held down. While a modifier is held, cell-selection handlers preserve the |
| 4 |
* existing selection so a multi-cell selection can be built up for merging. |
| 5 |
* |
| 6 |
* State is synced from keydown/keyup as well as mousedown — the mousedown sync |
| 7 |
* (capture phase) runs before the selection handlers, guaranteeing an accurate |
| 8 |
* value at the moment those handlers check it, even if a keydown landed |
| 9 |
* in a document we aren't listening to. |
| 10 |
* |
| 11 |
* Listeners are attached per-document so this works in both the iframed and |
| 12 |
* non-iframed editor. |
| 13 |
*/ |
| 14 |
|
| 15 |
let modifierActive = false; |
| 16 |
|
| 17 |
function sync(event: KeyboardEvent | MouseEvent): void { |
| 18 |
modifierActive = event.ctrlKey || event.metaKey || event.shiftKey; |
| 19 |
} |
| 20 |
|
| 21 |
export function isModifierActive(): boolean { |
| 22 |
return modifierActive; |
| 23 |
} |
| 24 |
|
| 25 |
export function trackModifierKeys(doc: Document): () => void { |
| 26 |
doc.addEventListener("keydown", sync, true); |
| 27 |
doc.addEventListener("keyup", sync, true); |
| 28 |
doc.addEventListener("mousedown", sync, true); |
| 29 |
|
| 30 |
return () => { |
| 31 |
doc.removeEventListener("keydown", sync, true); |
| 32 |
doc.removeEventListener("keyup", sync, true); |
| 33 |
doc.removeEventListener("mousedown", sync, true); |
| 34 |
}; |
| 35 |
} |
| 36 |
|