@@ -31,6 +31,13 @@ export function createInputManager(editor: Editor, container: HTMLElement, dialo
31
31
32
32
let viewportPointerInteractionOngoing = false ;
33
33
let textInput = undefined as undefined | HTMLDivElement ;
34
+ let inCanvas = true ;
35
+
36
+ const app = window . document . getElementById ( "app" ) ;
37
+ app ?. focus ( ) ;
38
+ app ?. addEventListener ( "blur" , ( ) => {
39
+ inCanvas = false ;
40
+ } ) ;
34
41
35
42
// Keyboard events
36
43
@@ -66,7 +73,6 @@ export function createInputManager(editor: Editor, container: HTMLElement, dialo
66
73
if ( e . ctrlKey && e . shiftKey && key === "j" ) return false ;
67
74
68
75
// Don't redirect tab or enter if not in canvas (to allow navigating elements)
69
- const inCanvas = e . target instanceof Element && e . target . closest ( "[data-canvas]" ) ;
70
76
if ( ! inCanvas && [ "tab" , "enter" , " " , "arrowdown" , "arrowup" , "arrowleft" , "arrowright" ] . includes ( key . toLowerCase ( ) ) ) return false ;
71
77
72
78
// Redirect to the backend
@@ -113,6 +119,10 @@ export function createInputManager(editor: Editor, container: HTMLElement, dialo
113
119
const inFloatingMenu = e . target instanceof Element && e . target . closest ( "[data-floating-menu-content]" ) ;
114
120
if ( ! viewportPointerInteractionOngoing && inFloatingMenu ) return ;
115
121
122
+ const { target } = e ;
123
+ inCanvas = ( target instanceof Element && target . closest ( "[data-canvas]" ) ) instanceof Element && ! targetIsTextField ( window . document . activeElement ) ;
124
+ if ( inCanvas ) app ?. focus ( ) ;
125
+
116
126
const modifiers = makeKeyboardModifiersBitfield ( e ) ;
117
127
editor . instance . on_mouse_move ( e . clientX , e . clientY , e . buttons , modifiers ) ;
118
128
}
@@ -131,7 +141,7 @@ export function createInputManager(editor: Editor, container: HTMLElement, dialo
131
141
132
142
if ( ! inTextInput ) {
133
143
if ( textInput ) editor . instance . on_change_text ( textInputCleanup ( textInput . innerText ) ) ;
134
- else if ( inCanvas ) viewportPointerInteractionOngoing = true ;
144
+ else viewportPointerInteractionOngoing = inCanvas instanceof Element ;
135
145
}
136
146
137
147
if ( viewportPointerInteractionOngoing ) {
@@ -246,7 +256,7 @@ export function createInputManager(editor: Editor, container: HTMLElement, dialo
246
256
} ) ;
247
257
}
248
258
249
- function targetIsTextField ( target : EventTarget | null ) : boolean {
259
+ function targetIsTextField ( target : EventTarget | HTMLElement | null ) : boolean {
250
260
return target instanceof HTMLElement && ( target . nodeName === "INPUT" || target . nodeName === "TEXTAREA" || target . isContentEditable ) ;
251
261
}
252
262
0 commit comments