@@ -16,6 +16,7 @@ const INSIDE_CANVAS = "INSIDE_CANVAS" as const;
1616const OUTSIDE_CANVAS = "OUTSIDE_CANVAS" as const ;
1717const MOUSE_MOVE = "MOUSE_MOVE" as const ;
1818const MOUSE_UP = "MOUSE_UP" as const ;
19+ const MOUSE_DOWN = "MOUSE_DOWN" as const ;
1920const COMPONENT_CREATED = "COMPONENT_CREATED" as const ; // emitted only after drag-drop
2021const SCROLL = "SCROLL" as const ;
2122
@@ -46,6 +47,10 @@ type MOUSE_UP_EVENT = {
4647 type : typeof MOUSE_UP ;
4748 event : { pageX : number ; pageY : number ; target : MouseEvent [ "target" ] } ;
4849} ;
50+ type MOUSE_DOWN_EVENT = {
51+ type : typeof MOUSE_DOWN ;
52+ event : { pageX : number ; pageY : number ; target : MouseEvent [ "target" ] } ;
53+ } ;
4954type COMPONENT_CREATED_EVENT = {
5055 type : typeof COMPONENT_CREATED ;
5156 compId : string ;
@@ -66,6 +71,7 @@ type CanvasMachineEvent =
6671 | OUTSIDE_CANVAS_EVENT
6772 | MOUSE_MOVE_EVENT
6873 | MOUSE_UP_EVENT
74+ | MOUSE_DOWN_EVENT
6975 | COMPONENT_CREATED_EVENT
7076 | SCROLL_EVENT ;
7177
@@ -80,6 +86,8 @@ const drag_in_progress_active = "drag_in_progress_active" as const;
8086// inside ready
8187const idle = "idle" as const ;
8288const hover = "hover" as const ;
89+ const pressed = "pressed" as const ;
90+ const selected = "selected" as const ;
8391
8492// context
8593
@@ -94,6 +102,7 @@ type CanvasMachineContext = {
94102 target : MouseEvent [ "target" ] ;
95103 } | null ;
96104 hovered : string | null ;
105+ selected : string | null ;
97106} ;
98107
99108// actions
@@ -108,7 +117,7 @@ function setDragData(
108117
109118function setMousePosition (
110119 context : CanvasMachineContext ,
111- event : MOUSE_MOVE_EVENT | MOUSE_UP_EVENT
120+ event : MOUSE_MOVE_EVENT | MOUSE_UP_EVENT | MOUSE_DOWN_EVENT
112121) {
113122 context . mousePosition = event . event ;
114123}
@@ -127,11 +136,25 @@ function setHoverComponent(
127136 }
128137}
129138
139+ function setSelectedComponent (
140+ context : CanvasMachineContext ,
141+ event : MOUSE_DOWN_EVENT
142+ ) {
143+ const { target } = event . event ;
144+ if ( target !== null && "closest" in target ) {
145+ const comp = ( target as any ) . closest ( "[data-atri-comp-id]" ) ;
146+ if ( comp !== null ) {
147+ const compId = comp . getAttribute ( "data-atri-comp-id" ) ;
148+ context . selected = compId ;
149+ }
150+ }
151+ }
152+
130153// conds
131154
132155function insideComponent (
133156 _context : CanvasMachineContext ,
134- event : MOUSE_MOVE_EVENT
157+ event : MOUSE_MOVE_EVENT | MOUSE_DOWN_EVENT
135158) {
136159 const { target } = event . event ;
137160 if ( target !== null && "closest" in target ) {
@@ -158,6 +181,21 @@ function hoveringOverDifferentComponent(
158181 return false ;
159182}
160183
184+ function selectedDifferentComponent (
185+ context : CanvasMachineContext ,
186+ event : MOUSE_DOWN_EVENT
187+ ) {
188+ const { target } = event . event ;
189+ if ( target !== null && "closest" in target ) {
190+ const comp = ( target as any ) . closest ( "[data-atri-comp-id]" ) ;
191+ if ( comp !== null ) {
192+ const compId = comp . getAttribute ( "data-atri-comp-id" ) ;
193+ return compId !== context . selected ;
194+ }
195+ }
196+ return false ;
197+ }
198+
161199type Callback = (
162200 context : CanvasMachineContext ,
163201 event : CanvasMachineEvent
@@ -192,6 +230,7 @@ export function createCanvasMachine(id: string) {
192230 }
193231 } ;
194232 }
233+
195234 function callSubscribers (
196235 state : SubscribeStates ,
197236 context : CanvasMachineContext ,
@@ -215,6 +254,7 @@ export function createCanvasMachine(id: string) {
215254 callSubscribers ( state , context , event ) ;
216255 } ;
217256 }
257+
218258 const canvasMachine = createMachine < CanvasMachineContext , CanvasMachineEvent > (
219259 {
220260 id,
@@ -227,23 +267,48 @@ export function createCanvasMachine(id: string) {
227267 dragData : null ,
228268 mousePosition : null ,
229269 hovered : null ,
270+ selected : null ,
230271 } ,
231272 states : {
232273 [ initial ] : {
274+ entry : ( ) => {
275+ console . log ( "Entered Initial State" , id ) ;
276+ } ,
277+ exit : ( ) => {
278+ console . log ( "Exited Initial State" , id ) ;
279+ } ,
233280 on : {
234281 [ IFRAME_DETECTED ] : { target : checks_completed } ,
235282 [ TOP_WINDOW_DETECTED ] : { target : noop } ,
236283 } ,
237284 } ,
238285 [ checks_completed ] : {
286+ entry : ( ) => {
287+ console . log ( "Entered Checks Completed State" , id ) ;
288+ } ,
289+ exit : ( ) => {
290+ console . log ( "Exited Checks Completed State" , id ) ;
291+ } ,
239292 on : {
240293 [ WINDOW_LOADED ] : { target : ready , actions : [ "emitReady" ] } ,
241294 } ,
242295 } ,
243296 [ ready ] : {
297+ entry : ( ) => {
298+ console . log ( "Entered Ready State" , id ) ;
299+ } ,
300+ exit : ( ) => {
301+ console . log ( "Exited Ready State" , id ) ;
302+ } ,
244303 initial : idle ,
245304 states : {
246305 [ idle ] : {
306+ entry : ( ) => {
307+ console . log ( "Entered Ready Idle State" , id ) ;
308+ } ,
309+ exit : ( ) => {
310+ console . log ( "Exited Ready Idle State" , id ) ;
311+ } ,
247312 on : {
248313 [ MOUSE_MOVE ] : {
249314 target : hover ,
@@ -253,25 +318,55 @@ export function createCanvasMachine(id: string) {
253318 } ,
254319 } ,
255320 [ hover ] : {
321+ entry : ( ) => {
322+ console . log ( "Entered Ready Hover State" , id ) ;
323+ } ,
324+ exit : ( ) => {
325+ console . log ( "Exited Ready Hover State" , id ) ;
326+ } ,
256327 on : {
257328 [ MOUSE_MOVE ] : {
258329 target : hover ,
259330 cond : hoveringOverDifferentComponent ,
260331 actions : [ "setHoverComponent" ] ,
261332 } ,
333+ [ MOUSE_DOWN ] : {
334+ target : pressed ,
335+ } ,
262336 [ SCROLL ] : {
263337 target : idle ,
264338 } ,
265339 [ OUTSIDE_CANVAS ] : {
266340 target : idle ,
267341 } ,
268342 } ,
269- entry : ( context , event ) => {
270- callSubscribers ( "hover" , context , event ) ;
343+ } ,
344+ [ pressed ] : {
345+ entry : ( ) => {
346+ console . log ( "Entered Pressed State" , id ) ;
347+ } ,
348+ exit : ( ) => {
349+ console . log ( "Exited Pressed State" , id ) ;
350+ } ,
351+ on : {
352+ [ MOUSE_UP ] : {
353+ target : selected ,
354+ actions : [ "setSelectedComponent" ] ,
355+ } ,
356+ } ,
357+ } ,
358+ [ selected ] : {
359+ entry : ( ) => {
360+ console . log ( "Entered Selected State" , id ) ;
361+ } ,
362+ exit : ( ) => {
363+ console . log ( "Exited Selected State" , id ) ;
271364 } ,
272- exit : ( context , event ) => {
273- context . hovered = null ;
274- callSubscribers ( "hoverEnd" , context , event ) ;
365+ on : {
366+ [ MOUSE_DOWN ] : {
367+ target : pressed ,
368+ cond : selectedDifferentComponent ,
369+ } ,
275370 } ,
276371 } ,
277372 } ,
@@ -286,9 +381,21 @@ export function createCanvasMachine(id: string) {
286381 } ,
287382 } ,
288383 [ drag_in_progress ] : {
384+ entry : ( ) => {
385+ console . log ( "Entered Drag In Progress State" , id ) ;
386+ } ,
387+ exit : ( ) => {
388+ console . log ( "Exited Drag In Progress State" , id ) ;
389+ } ,
289390 initial : drag_in_progress_idle ,
290391 states : {
291392 [ drag_in_progress_idle ] : {
393+ entry : ( ) => {
394+ console . log ( "Entered Drag In Progress Idle State" , id ) ;
395+ } ,
396+ exit : ( ) => {
397+ console . log ( "Exited Drag In Progress Idle State" , id ) ;
398+ } ,
292399 on : {
293400 [ DRAG_STOPPED ] : { target : `#${ id } .${ ready } ` } ,
294401 [ INSIDE_CANVAS ] : {
@@ -298,6 +405,12 @@ export function createCanvasMachine(id: string) {
298405 } ,
299406 } ,
300407 [ drag_in_progress_active ] : {
408+ entry : ( ) => {
409+ console . log ( "Entered Drag In Progress Active State" , id ) ;
410+ } ,
411+ exit : ( ) => {
412+ console . log ( "Exited Drag In Progress Active State" , id ) ;
413+ } ,
301414 on : {
302415 [ MOUSE_MOVE ] : {
303416 actions : [ "setMousePosition" , "emitMoveWhileDrag" ] ,
@@ -328,6 +441,7 @@ export function createCanvasMachine(id: string) {
328441 emitReady : callSubscribersFromAction ( "ready" ) ,
329442 emitComponentCreated : callSubscribersFromAction ( "COMPONENT_CREATED" ) ,
330443 setHoverComponent,
444+ setSelectedComponent,
331445 } ,
332446 }
333447 ) ;
0 commit comments