-
-
Notifications
You must be signed in to change notification settings - Fork 209
fix(app): forward keyboard shortcuts from sandbox iframes #839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import type { Ref } from 'vue' | ||
| import { useEventListener } from '@vueuse/core' | ||
| import { isRef } from 'vue' | ||
| import { isRef, ref } from 'vue' | ||
| import { isMac } from './env.js' | ||
|
|
||
| export type KeyboardShortcut = string[] | ||
|
|
@@ -11,14 +11,6 @@ export interface KeyboardShortcutOptions { | |
| event?: 'keyup' | 'keydown' | 'keypress' | ||
| } | ||
|
|
||
| export function onKeyboardShortcut(shortcut: KeyboardShortcut | Ref<KeyboardShortcut>, handler: KeyboardHandler, options: KeyboardShortcutOptions = {}) { | ||
| useEventListener(options.event ?? 'keydown', (event) => { | ||
| if (isMatchingShortcut(isRef(shortcut) ? shortcut.value : shortcut)) { | ||
| handler(event) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| const modifiers: { [i: string]: { key: string, pressed: boolean } } = { | ||
| ctrl: { key: 'Control', pressed: false }, | ||
| alt: { key: 'Alt', pressed: false }, | ||
|
|
@@ -28,37 +20,73 @@ const modifiers: { [i: string]: { key: string, pressed: boolean } } = { | |
|
|
||
| const pressedKeys = new Set<string>() | ||
|
|
||
| window.addEventListener('keydown', (event) => { | ||
| for (const i in modifiers) { | ||
| const mod = modifiers[i] | ||
| if (mod.key === event.key) { | ||
| mod.pressed = true | ||
| return | ||
| } | ||
| } | ||
| pressedKeys.add(event.key.toLocaleLowerCase()) | ||
| }) | ||
| const trackedWindows = ref<Window[]>([]) | ||
|
|
||
| window.addEventListener('keyup', (event) => { | ||
| requestAnimationFrame(() => { | ||
| pressedKeys.clear() | ||
| function bindTracking(target: Window) { | ||
| const onKeydown = (event: KeyboardEvent) => { | ||
| for (const i in modifiers) { | ||
| const mod = modifiers[i] | ||
| if (mod.key === event.key) { | ||
| mod.pressed = false | ||
| break | ||
| mod.pressed = true | ||
| return | ||
| } | ||
| } | ||
| }) | ||
| }) | ||
| pressedKeys.add(event.key.toLocaleLowerCase()) | ||
| } | ||
| const onKeyup = (event: KeyboardEvent) => { | ||
| requestAnimationFrame(() => { | ||
| pressedKeys.clear() | ||
| for (const i in modifiers) { | ||
| const mod = modifiers[i] | ||
| if (mod.key === event.key) { | ||
| mod.pressed = false | ||
| break | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| const onBlur = () => { | ||
| pressedKeys.clear() | ||
| for (const i in modifiers) { | ||
| modifiers[i].pressed = false | ||
| } | ||
| } | ||
| target.addEventListener('keydown', onKeydown) | ||
| target.addEventListener('keyup', onKeyup) | ||
| target.addEventListener('blur', onBlur) | ||
| return () => { | ||
| target.removeEventListener('keydown', onKeydown) | ||
| target.removeEventListener('keyup', onKeyup) | ||
| target.removeEventListener('blur', onBlur) | ||
| } | ||
| } | ||
|
|
||
| window.addEventListener('blur', () => { | ||
| pressedKeys.clear() | ||
| for (const i in modifiers) { | ||
| const mod = modifiers[i] | ||
| mod.pressed = false | ||
| // Forward shortcuts from sandbox iframes. Closes #350. | ||
| export function trackWindow(target: Window): () => void { | ||
| if (trackedWindows.value.includes(target)) return () => {} | ||
| const cleanup = bindTracking(target) | ||
| trackedWindows.value = [...trackedWindows.value, target] | ||
| return () => { | ||
| cleanup() | ||
| trackedWindows.value = trackedWindows.value.filter(w => w !== target) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| trackWindow(window) | ||
|
|
||
| export function onKeyboardShortcut(shortcut: KeyboardShortcut | Ref<KeyboardShortcut>, handler: KeyboardHandler, options: KeyboardShortcutOptions = {}) { | ||
| useEventListener(trackedWindows, options.event ?? 'keydown', (event: KeyboardEvent) => { | ||
| // Sync modifier state from the event so a blur-clear (e.g. focusing an | ||
| // iframe while holding a modifier) doesn't drop the shortcut. | ||
| modifiers.ctrl.pressed = event.ctrlKey | ||
| modifiers.alt.pressed = event.altKey | ||
| modifiers.shift.pressed = event.shiftKey | ||
| modifiers.meta.pressed = event.metaKey | ||
| if (isMatchingShortcut(isRef(shortcut) ? shortcut.value : shortcut)) { | ||
| handler(event) | ||
| } | ||
| }) | ||
| } | ||
|
Comment on lines
+77
to
+89
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The global export function onKeyboardShortcut(shortcut: KeyboardShortcut | Ref<KeyboardShortcut>, handler: KeyboardHandler, options: KeyboardShortcutOptions = {}) {
useEventListener(trackedWindows, options.event ?? 'keydown', (event: KeyboardEvent) => {
modifiers.ctrl.pressed = event.ctrlKey
modifiers.alt.pressed = event.altKey
modifiers.shift.pressed = event.shiftKey
modifiers.meta.pressed = event.metaKey
if (isMatchingShortcut(isRef(shortcut) ? shortcut.value : shortcut)) {
handler(event)
}
})
} |
||
|
|
||
| function isMatchingShortcut(shortcut: KeyboardShortcut): boolean { | ||
| for (const combination of shortcut) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accessing or adding event listeners to a cross-origin iframe's
contentWindowwill throw a security error (DOMException). Since Histoire sandboxes can be hosted on different origins (e.g., to prevent CSS/JS leakage), we should wrap the tracking initialization in atry-catchblock to prevent the application from crashing when a cross-origin iframe is loaded.