1
1
import { useEditorEngine } from '@/components/store' ;
2
2
import { getRelativeMousePositionToWebview } from '@/components/store/editor/engine/overlay/utils' ;
3
- import { EditorMode , type MouseAction } from '@onlook/models/editor' ;
4
- import type { ElementPosition } from '@onlook/models/element' ;
3
+ import { EditorMode , MouseAction } from '@onlook/models/editor' ;
4
+ import type { DomElement , ElementPosition } from '@onlook/models/element' ;
5
+ import type { WebFrame } from '@onlook/models/frame' ;
5
6
import { cn } from '@onlook/ui/utils' ;
6
7
import throttle from 'lodash/throttle' ;
7
8
import { observer } from 'mobx-react-lite' ;
8
9
import { useCallback , useEffect , useMemo } from 'react' ;
9
10
import { RightClickMenu } from './right-click' ;
10
-
11
- export const GestureScreen = observer ( ( ) => {
11
+ import type { WebFrameView } from './web-frame' ;
12
+ export const GestureScreen = observer ( ( { frame , webFrameRef } : { frame : WebFrame , webFrameRef : React . RefObject < WebFrameView | null > } ) => {
12
13
const editorEngine = useEditorEngine ( ) ;
13
14
const isResizing = false ;
15
+ const webFrame = webFrameRef . current ;
14
16
15
- const getWebview = useCallback ( ( ) : Electron . WebviewTag => {
16
- // const webview = webviewRef.current as Electron.WebviewTag | null;
17
- // if (!webview) {
18
- // throw Error('No webview found');
19
- // }
20
- // return webview;
21
- } , [ ] ) ;
17
+ if ( ! webFrame ) {
18
+ console . log ( 'No web frame found in gesture screen for frame' , frame . id ) ;
19
+ return null ;
20
+ }
22
21
23
- const getRelativeMousePosition = useCallback (
24
- ( e : React . MouseEvent < HTMLDivElement > ) : ElementPosition => {
25
- const webview = getWebview ( ) ;
26
- return getRelativeMousePositionToWebview ( e , webview ) ;
27
- } ,
28
- [ getWebview ] ,
29
- ) ;
22
+ const getRelativeMousePosition = ( e : React . MouseEvent < HTMLDivElement > ) : ElementPosition => {
23
+ return getRelativeMousePositionToWebview ( e , webFrame ) ;
24
+ }
30
25
31
26
const handleMouseEvent = useCallback (
32
27
async ( e : React . MouseEvent < HTMLDivElement > , action : MouseAction ) => {
33
- // const webview = getWebview( );
34
- // const pos = getRelativeMousePosition(e );
35
- // const el: DomElement = await webview.executeJavaScript(
36
- // `window.api?.getElementAtLoc(${pos.x}, ${pos.y}, ${action === MouseAction.MOUSE_DOWN || action === MouseAction.DOUBLE_CLICK})`,
37
- // );
38
- // if (!el) {
39
- // return;
40
- // }
41
-
42
- // switch (action) {
43
- // case MouseAction.MOVE:
44
- // editorEngine.elements.mouseover(el, webview);
45
- // if (e.altKey) {
46
- // editorEngine.elements.showMeasurement();
47
- // } else {
48
- // editorEngine.overlay.removeMeasurement();
49
- // }
50
- // break;
51
- // case MouseAction.MOUSE_DOWN:
52
- // if (el.tagName.toLocaleLowerCase() === 'body') {
53
- // editorEngine.webview.select(webview);
54
- // return;
55
- // }
56
- // // Ignore right-clicks
57
- // if (e.button == 2) {
58
- // break;
59
- // }
60
- // if (editorEngine.text.isEditing) {
61
- // editorEngine.text.end();
62
- // }
63
- // if (e.shiftKey) {
64
- // editorEngine.elements.shiftClick(el, webview);
65
- // } else {
66
- // editorEngine.move.start(el, pos, webview);
67
- // editorEngine.elements.click([el], webview);
68
- // }
69
- // break;
70
- // case MouseAction.DOUBLE_CLICK:
71
- // editorEngine.text.start(el, webview);
72
- // break;
73
- // }
28
+ const pos = getRelativeMousePosition ( e ) ;
29
+ const shouldGetStyle = [ MouseAction . MOUSE_DOWN , MouseAction . DOUBLE_CLICK ] . includes ( action ) ;
30
+ const el : DomElement = await webFrame . getElementAtLoc ( pos . x , pos . y , shouldGetStyle ) ;
31
+ if ( ! el ) {
32
+ console . log ( 'No element found' ) ;
33
+ return ;
34
+ }
35
+
36
+ switch ( action ) {
37
+ case MouseAction . MOVE :
38
+ // editorEngine.elements.mouseover(el, webview);
39
+ // if (e.altKey) {
40
+ // editorEngine.elements.showMeasurement();
41
+ // } else {
42
+ // editorEngine.overlay.removeMeasurement();
43
+ // }
44
+ break ;
45
+ case MouseAction . MOUSE_DOWN :
46
+ console . log ( 'mouse down' , el ) ;
47
+ // if (el.tagName.toLocaleLowerCase() === 'body') {
48
+ // editorEngine.webview.select(webview);
49
+ // return;
50
+ // }
51
+ // // Ignore right-clicks
52
+ // if (e.button == 2) {
53
+ // break;
54
+ // }
55
+ // if (editorEngine.text.isEditing) {
56
+ // editorEngine.text.end();
57
+ // }
58
+ // if (e.shiftKey) {
59
+ // editorEngine.elements.shiftClick(el, webview);
60
+ // } else {
61
+ // editorEngine.move.start(el, pos, webview);
62
+ // editorEngine.elements.click([el], webview);
63
+ // }
64
+ break ;
65
+ case MouseAction . DOUBLE_CLICK :
66
+ // editorEngine.text.start(el, webview);
67
+ break ;
68
+ }
74
69
} ,
75
- [ getWebview , getRelativeMousePosition , editorEngine ] ,
70
+ [ getRelativeMousePosition , editorEngine ] ,
76
71
) ;
77
72
78
73
const throttledMouseMove = useMemo (
79
74
( ) =>
80
75
throttle ( ( e : React . MouseEvent < HTMLDivElement > ) => {
81
- // if (editorEngine.state.move.isDragging) {
82
- // editorEngine.state.move.drag(e, getRelativeMousePosition);
76
+ handleMouseEvent ( e , MouseAction . MOVE ) ;
77
+
78
+ // if (editorEngine.move.isDragging) {
79
+ // editorEngine.move.drag(e, getRelativeMousePosition);
83
80
// } else if (
84
81
// editorEngine.state.editorMode === EditorMode.DESIGN ||
85
82
// ((editorEngine.state.editorMode === EditorMode.INSERT_DIV ||
@@ -107,7 +104,7 @@ export const GestureScreen = observer(() => {
107
104
// editorEngine.webview.deselectAll();
108
105
// editorEngine.webview.select(webview);
109
106
} ,
110
- [ getWebview , editorEngine . webview ] ,
107
+ [ editorEngine . webview ] ,
111
108
) ;
112
109
113
110
function handleDoubleClick ( e : React . MouseEvent < HTMLDivElement > ) {
@@ -118,15 +115,15 @@ export const GestureScreen = observer(() => {
118
115
}
119
116
120
117
function handleMouseDown ( e : React . MouseEvent < HTMLDivElement > ) {
121
- // if (editorEngine.state.editorMode === EditorMode.DESIGN) {
122
- // handleMouseEvent(e, MouseAction.MOUSE_DOWN);
123
- // } else if (
124
- // editorEngine.state.editorMode === EditorMode.INSERT_DIV ||
125
- // editorEngine.state.editorMode === EditorMode.INSERT_TEXT ||
126
- // editorEngine.state.editorMode === EditorMode.INSERT_IMAGE
127
- // ) {
128
- // editorEngine.insert.start(e);
129
- // }
118
+ if ( editorEngine . state . editorMode === EditorMode . DESIGN ) {
119
+ handleMouseEvent ( e , MouseAction . MOUSE_DOWN ) ;
120
+ } else if (
121
+ editorEngine . state . editorMode === EditorMode . INSERT_DIV ||
122
+ editorEngine . state . editorMode === EditorMode . INSERT_TEXT ||
123
+ editorEngine . state . editorMode === EditorMode . INSERT_IMAGE
124
+ ) {
125
+ // editorEngine.insert.start(e);
126
+ }
130
127
}
131
128
132
129
async function handleMouseUp ( e : React . MouseEvent < HTMLDivElement > ) {
0 commit comments