@gem-sdk/pages 1.22.31-staging.1 → 1.23.0-staging.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,671 @@
1
+ import { memo, useRef, useCallback, useEffect } from 'react';
2
+
3
+ const TOOLBAR_HOVER_HEIGHT = 24;
4
+ // const TOOLBAR_ACTIVE_HEIGHT = 32;
5
+ const getDOMElementParents = ($el, selector, limit)=>{
6
+ // Set up a parent array
7
+ const parents = [];
8
+ // Push each parent $elms to the array
9
+ while($el){
10
+ $el = $el.parentElement ?? undefined;
11
+ if ($el) {
12
+ if ($el.tagName === 'BODY' || $el.getAttribute('data-uid') === 'ROOT') {
13
+ break;
14
+ }
15
+ if (selector) {
16
+ if ($el.matches(selector)) {
17
+ parents.push($el);
18
+ if (limit && parents.length == limit) {
19
+ return parents;
20
+ }
21
+ }
22
+ continue;
23
+ }
24
+ parents.push($el);
25
+ if (limit && parents.length == limit) {
26
+ return parents;
27
+ }
28
+ }
29
+ }
30
+ // Return our parent array
31
+ return parents;
32
+ };
33
+ const getChildrenByAttrSelector = ($el, attrSelector)=>{
34
+ const childLen = $el.children.length;
35
+ if (childLen) {
36
+ for(let i = 0; i < childLen; i++){
37
+ const children = $el.children[i];
38
+ if (children) {
39
+ const is = children.getAttribute(attrSelector);
40
+ if (is) {
41
+ return children;
42
+ }
43
+ }
44
+ }
45
+ }
46
+ };
47
+ const isOverParent = (current, parent, index)=>{
48
+ for(let i = 0; i < index; i++){
49
+ const is = current.top - (TOOLBAR_HOVER_HEIGHT - 1) * i >= parent.top && current.top - (TOOLBAR_HOVER_HEIGHT - 1) * i <= parent.top + parent.height && current.left >= parent.left && current.left <= parent.left + parent.width;
50
+ if (is) return true;
51
+ }
52
+ return false;
53
+ };
54
+ const waitForElementToExist = (selector, timeout = 200)=>{
55
+ return new Promise((resolve)=>{
56
+ const intervalID = setInterval(()=>{
57
+ const el = document.querySelector(selector);
58
+ if (el) {
59
+ clearInterval(intervalID);
60
+ clearTimeout(timeoutID);
61
+ resolve(el);
62
+ }
63
+ }, 50);
64
+ const timeoutID = setTimeout(()=>{
65
+ clearInterval(intervalID);
66
+ clearTimeout(timeoutID);
67
+ resolve(null);
68
+ }, timeout);
69
+ });
70
+ };
71
+ const notVisible = (el)=>{
72
+ const overflow = getComputedStyle(el).overflow;
73
+ return overflow !== 'visible';
74
+ };
75
+ const isSection = (el)=>{
76
+ const tag = el.getAttribute('data-component-tag');
77
+ return tag === 'Section';
78
+ };
79
+ const isOverToolbarPosition = (el, parent)=>{
80
+ const rect = el.getBoundingClientRect();
81
+ const rectP = parent.getBoundingClientRect();
82
+ // 32px = toolbar active height
83
+ return rect.top - rectP.top < 32 + 5;
84
+ };
85
+ const findOverflowParent = (element, initEl)=>{
86
+ const thisEl = element;
87
+ const origEl = initEl || thisEl;
88
+ if (!thisEl) return;
89
+ if (isSection(thisEl)) return;
90
+ if (notVisible(thisEl) && isOverToolbarPosition(initEl, thisEl)) return thisEl;
91
+ if (thisEl.parentElement) {
92
+ return findOverflowParent(thisEl.parentElement, origEl);
93
+ } else {
94
+ return;
95
+ }
96
+ };
97
+ const Toolbar = ()=>{
98
+ const currentComponentActive = useRef(null);
99
+ const isDragging = useRef(false);
100
+ const obsActiveComponent = useRef();
101
+ const isResizeSpacing = useRef(false);
102
+ /* Functions */ const setHoverComponent = useCallback(({ $component, componentUid, focus, isThemeSection })=>{
103
+ if (!$component && !componentUid) return;
104
+ if (!$component) {
105
+ const $c = document.querySelector(`[data-uid="${componentUid}"]`);
106
+ if (!$c) return;
107
+ $component = $c;
108
+ }
109
+ if (!componentUid) {
110
+ const cUid = $component.getAttribute('data-uid');
111
+ if (!cUid) return;
112
+ componentUid = cUid;
113
+ }
114
+ const $toolbar = getChildrenByAttrSelector($component, 'data-toolbar');
115
+ const $outline = getChildrenByAttrSelector($component, 'data-outline');
116
+ const $btnAddTop = getChildrenByAttrSelector($component, 'data-toolbar-add-top');
117
+ const $btnAddBottom = getChildrenByAttrSelector($component, 'data-toolbar-add-bottom');
118
+ const $themeSectionStatus = getChildrenByAttrSelector($component, 'data-theme-section-status');
119
+ if (isThemeSection && $themeSectionStatus) {
120
+ $themeSectionStatus.setAttribute('data-theme-section-status-active', 'true');
121
+ }
122
+ if ($toolbar) {
123
+ $toolbar.removeAttribute('style');
124
+ $toolbar.setAttribute('data-toolbar-hover', 'true');
125
+ if (focus) {
126
+ $toolbar.setAttribute('data-toolbar-hover-focus', 'true');
127
+ }
128
+ const $parentOverflow = findOverflowParent($component, $toolbar);
129
+ if ($parentOverflow) {
130
+ $toolbar.setAttribute('data-toolbar-hover-revert', 'true');
131
+ }
132
+ }
133
+ if ($outline) {
134
+ $outline.setAttribute('data-outline-hover', 'true');
135
+ if (isThemeSection) {
136
+ $outline.setAttribute('data-outline-overlay-theme-section', 'true');
137
+ }
138
+ if (focus) {
139
+ $outline.setAttribute('data-outline-hover-focus', 'true');
140
+ }
141
+ }
142
+ if ($btnAddTop) {
143
+ $btnAddTop.setAttribute('data-toolbar-add-hover', 'true');
144
+ }
145
+ if ($btnAddBottom) {
146
+ $btnAddBottom.setAttribute('data-toolbar-add-hover', 'true');
147
+ }
148
+ }, []);
149
+ const setHoverComponentParents = useCallback(({ $component, componentUid })=>{
150
+ if (!$component) {
151
+ const $c = document.querySelector(`[data-uid="${componentUid}"]`);
152
+ if (!$c) return;
153
+ $component = $c;
154
+ }
155
+ const $parents = getDOMElementParents($component, '[data-uid][data-component-type="component"]:not([data-component-no-setting])');
156
+ if ($parents.length) {
157
+ for (const $parent of $parents){
158
+ if ($parent) {
159
+ setHoverComponent({
160
+ $component: $parent
161
+ });
162
+ }
163
+ }
164
+ changePositionToolbarParents({
165
+ $component,
166
+ $parents
167
+ });
168
+ }
169
+ }, [
170
+ setHoverComponent
171
+ ]);
172
+ const changePositionToolbarParents = ({ $component, $parents })=>{
173
+ if (!$component) return;
174
+ if (!$parents?.length) return;
175
+ const $currentToolbar = getChildrenByAttrSelector($component, 'data-toolbar-hover-focus');
176
+ if ($currentToolbar) {
177
+ const currentRect = $currentToolbar.getBoundingClientRect();
178
+ let index = 1;
179
+ for (const $parent of $parents){
180
+ if ($parent) {
181
+ const tag = $parent.getAttribute('data-component-tag');
182
+ if (tag === 'Section') continue;
183
+ const $toolbar = getChildrenByAttrSelector($parent, 'data-toolbar-hover');
184
+ if ($toolbar) {
185
+ // Ignore with toolbar active
186
+ const isActive = $toolbar.getAttribute('data-toolbar-active');
187
+ if (isActive) continue;
188
+ // Start calc
189
+ const parentRect = $toolbar.getBoundingClientRect();
190
+ if (isOverParent(currentRect, parentRect, index)) {
191
+ const parentStyle = getComputedStyle($toolbar);
192
+ const isRevert = $toolbar.getAttribute('data-toolbar-hover-revert') ? true : false;
193
+ // parentStyle.top
194
+ const diffTop = currentRect.top - parentRect.top;
195
+ const diffLeft = currentRect.left - parentRect.left;
196
+ let newTop = parseFloat(parentStyle.top) + diffTop - (TOOLBAR_HOVER_HEIGHT - 1) * index; // -1 border bottom
197
+ if (isRevert) {
198
+ newTop = parseFloat(parentStyle.top) - diffTop + (TOOLBAR_HOVER_HEIGHT - 1) * index; // -1 border bottom
199
+ }
200
+ const newLeft = parseFloat(parentStyle.left) + diffLeft;
201
+ $toolbar.setAttribute('style', `top: ${newTop}px;left: ${newLeft}px;`);
202
+ index++;
203
+ }
204
+ }
205
+ }
206
+ }
207
+ }
208
+ };
209
+ const removeHoverOverlayComponent = useCallback(()=>{
210
+ const clearAttrs = [
211
+ 'data-outline-overlay',
212
+ 'data-outline-overlay-theme-section',
213
+ 'data-theme-section-status-active'
214
+ ];
215
+ const $elms = document.querySelectorAll(clearAttrs.map((attr)=>`[${attr}]`).join(','));
216
+ if ($elms) {
217
+ clearAttrs.forEach((attr)=>{
218
+ $elms.forEach(($el)=>$el.removeAttribute(attr));
219
+ });
220
+ }
221
+ }, []);
222
+ const removeHoverComponent = useCallback(()=>{
223
+ const clearAttrs = [
224
+ 'data-toolbar-hover-focus',
225
+ 'data-toolbar-hover',
226
+ 'data-outline-hover-focus',
227
+ 'data-toolbar-hover-revert',
228
+ 'data-outline-hover',
229
+ 'data-toolbar-add-hover'
230
+ ];
231
+ const $elms = document.querySelectorAll(clearAttrs.map((attr)=>`[${attr}]`).join(','));
232
+ if ($elms) {
233
+ clearAttrs.forEach((attr)=>{
234
+ $elms.forEach(($el)=>$el.removeAttribute(attr));
235
+ });
236
+ }
237
+ removeHoverOverlayComponent();
238
+ }, [
239
+ removeHoverOverlayComponent
240
+ ]);
241
+ const removeActiveComponent = useCallback(()=>{
242
+ currentComponentActive.current = null;
243
+ const clearAttrs = [
244
+ 'data-toolbar-active',
245
+ 'data-outline-active',
246
+ 'data-toolbar-add-active',
247
+ 'data-toolbar-active-revert',
248
+ 'data-spacing-margin-bottom-active',
249
+ 'data-toolbar-force-hover',
250
+ 'data-outline-force-hover'
251
+ ];
252
+ const $elms = document.querySelectorAll(clearAttrs.map((attr)=>`[${attr}]`).join(','));
253
+ if ($elms) {
254
+ clearAttrs.forEach((attr)=>{
255
+ $elms.forEach(($el)=>$el.removeAttribute(attr));
256
+ });
257
+ }
258
+ setFocusTextEditor(false);
259
+ if (obsActiveComponent.current) obsActiveComponent.current.disconnect();
260
+ }, []);
261
+ const watchComponentReRender = ($el, callback)=>{
262
+ const parent = $el.parentNode;
263
+ if (!parent) return;
264
+ if (obsActiveComponent.current) obsActiveComponent.current.disconnect();
265
+ obsActiveComponent.current = new MutationObserver((mutations)=>{
266
+ for (const mutation of mutations){
267
+ mutation.removedNodes.forEach((el)=>{
268
+ if (el === $el) {
269
+ if (obsActiveComponent.current) obsActiveComponent.current.disconnect();
270
+ callback();
271
+ }
272
+ });
273
+ }
274
+ });
275
+ obsActiveComponent.current.observe(parent, {
276
+ childList: true
277
+ });
278
+ };
279
+ const setActiveComponentSpacing = useCallback(({ $component })=>{
280
+ if (!$component) return;
281
+ const style = getComputedStyle($component);
282
+ const $spacing = getChildrenByAttrSelector($component, 'data-spacing');
283
+ const $marginBottom = ($spacing?.querySelector('[data-spacing-margin-bottom]')) || null;
284
+ if ($marginBottom) {
285
+ const $bg = $marginBottom.querySelector('[data-spacing-margin-bottom-bg]') || null;
286
+ const $drag = $marginBottom.querySelector('[data-spacing-margin-bottom-drag]') || null;
287
+ if ($bg && $drag) {
288
+ const value = style.marginBottom;
289
+ $bg.style.height = value;
290
+ $drag.style.top = value;
291
+ $marginBottom.setAttribute('data-spacing-margin-bottom-active', 'true');
292
+ }
293
+ }
294
+ }, []);
295
+ const setActiveComponentForceHoverSection = ($component, value)=>{
296
+ const $section = $component.closest('[data-toolbar-wrap][data-component-tag="Section"]');
297
+ if ($section) {
298
+ if (value) {
299
+ const $toolbar = getChildrenByAttrSelector($section, 'data-toolbar');
300
+ const $outline = getChildrenByAttrSelector($section, 'data-outline');
301
+ if ($toolbar) {
302
+ $toolbar.setAttribute('data-toolbar-force-hover', 'true');
303
+ }
304
+ if ($outline) {
305
+ $outline.setAttribute('data-outline-force-hover', 'true');
306
+ }
307
+ } else {
308
+ const $toolbar = getChildrenByAttrSelector($section, 'data-toolbar');
309
+ const $outline = getChildrenByAttrSelector($section, 'data-outline');
310
+ if ($toolbar) {
311
+ $toolbar.removeAttribute('data-toolbar-force-hover');
312
+ }
313
+ if ($outline) {
314
+ $outline.removeAttribute('data-outline-force-hover');
315
+ }
316
+ }
317
+ }
318
+ };
319
+ const setActiveComponent = useCallback(async ({ componentUid, productId, timeAwait = 500, forceReActive })=>{
320
+ if (!componentUid) return;
321
+ const $component = await waitForElementToExist(`${productId ? `[data-product-id="${productId}"] ` : ''}[data-uid="${componentUid}"]`, timeAwait);
322
+ if (!$component) return;
323
+ if (!forceReActive && componentUid == currentComponentActive.current?.componentUid && productId == currentComponentActive.current?.productId) return;
324
+ if (!forceReActive && componentUid !== currentComponentActive.current?.componentUid || productId !== currentComponentActive.current?.productId) removeActiveComponent();
325
+ const $toolbar = getChildrenByAttrSelector($component, 'data-toolbar');
326
+ const $outline = getChildrenByAttrSelector($component, 'data-outline');
327
+ const $btnAddTop = getChildrenByAttrSelector($component, 'data-toolbar-add-top');
328
+ const $btnAddBottom = getChildrenByAttrSelector($component, 'data-toolbar-add-bottom');
329
+ if ($toolbar) {
330
+ currentComponentActive.current = {
331
+ componentUid,
332
+ productId
333
+ };
334
+ $toolbar.removeAttribute('style');
335
+ $toolbar.setAttribute('data-toolbar-active', 'true');
336
+ const $parentOverflow = findOverflowParent($component, $toolbar);
337
+ if ($parentOverflow) {
338
+ $toolbar.setAttribute('data-toolbar-active-revert', 'true');
339
+ }
340
+ }
341
+ if ($outline) {
342
+ $outline.setAttribute('data-outline-active', 'true');
343
+ }
344
+ if ($btnAddTop) {
345
+ $btnAddTop.setAttribute('data-toolbar-add-active', 'true');
346
+ }
347
+ if ($btnAddBottom) {
348
+ $btnAddBottom.setAttribute('data-toolbar-add-active', 'true');
349
+ }
350
+ // Active same element in product list
351
+ if (productId) {
352
+ const $relatedElements = document.querySelectorAll(`[data-uid="${componentUid}"]`);
353
+ if ($relatedElements?.length) {
354
+ $relatedElements.forEach(($relatedElement)=>{
355
+ const $outline = getChildrenByAttrSelector($relatedElement, 'data-outline');
356
+ if ($outline) {
357
+ $outline.setAttribute('data-outline-active', 'true');
358
+ }
359
+ });
360
+ }
361
+ }
362
+ setActiveComponentSpacing({
363
+ $component
364
+ });
365
+ setActiveComponentForceHoverSection($component, true);
366
+ removeHoverComponent();
367
+ // Reactive when component re-render
368
+ watchComponentReRender($component, ()=>{
369
+ setActiveComponent({
370
+ componentUid,
371
+ productId,
372
+ timeAwait: 2000,
373
+ forceReActive: true
374
+ });
375
+ });
376
+ }, [
377
+ removeActiveComponent,
378
+ removeHoverComponent,
379
+ setActiveComponentSpacing
380
+ ]);
381
+ const setFocusTextEditor = async (value)=>{
382
+ if (!value) {
383
+ const $components = document.querySelectorAll('[data-outline-editor-inline-focus],[data-toolbar-editor-inline-focus],[data-spacing-hidden]');
384
+ if ($components.length) {
385
+ $components.forEach(($component)=>{
386
+ if ($component) {
387
+ $component.removeAttribute('data-toolbar-editor-inline-focus');
388
+ $component.removeAttribute('data-outline-editor-inline-focus');
389
+ $component.removeAttribute('data-spacing-hidden');
390
+ }
391
+ });
392
+ }
393
+ } else {
394
+ if (currentComponentActive.current?.componentUid) {
395
+ const componentUid = currentComponentActive.current?.componentUid;
396
+ const productId = currentComponentActive.current?.productId;
397
+ const $component = await waitForElementToExist(`${productId ? `[data-product-id="${productId}"] ` : ''}[data-uid="${componentUid}"]`, 500);
398
+ if ($component) {
399
+ const $toolbar = getChildrenByAttrSelector($component, 'data-toolbar');
400
+ const $outline = getChildrenByAttrSelector($component, 'data-outline');
401
+ const $spacing = getChildrenByAttrSelector($component, 'data-spacing');
402
+ if ($toolbar) {
403
+ if (value) {
404
+ $toolbar.setAttribute('data-toolbar-editor-inline-focus', 'true');
405
+ }
406
+ }
407
+ if ($outline) {
408
+ if (value) {
409
+ $outline.setAttribute('data-outline-editor-inline-focus', 'true');
410
+ }
411
+ }
412
+ if ($spacing) {
413
+ if (value) {
414
+ $spacing.setAttribute('data-spacing-hidden', 'true');
415
+ }
416
+ }
417
+ }
418
+ }
419
+ }
420
+ };
421
+ const hoverActiveThemeSection = useCallback(($target)=>{
422
+ const $themeSection = $target.closest('[data-theme-section]');
423
+ const $themeSectionUid = $themeSection?.getAttribute('data-uid');
424
+ const isActiveThemeSection = $themeSection && $themeSectionUid === currentComponentActive.current?.componentUid;
425
+ if (!isActiveThemeSection) return;
426
+ const $themeSectionStatus = getChildrenByAttrSelector($themeSection, 'data-theme-section-status');
427
+ if ($themeSectionStatus) {
428
+ $themeSectionStatus.setAttribute('data-theme-section-status-active', 'true');
429
+ }
430
+ }, []);
431
+ const setShowParents = async ({ value })=>{
432
+ if (!value) {
433
+ return;
434
+ }
435
+ const $component = await waitForElementToExist(`${currentComponentActive.current?.productId ? `[data-product-id="${currentComponentActive.current?.productId}"] ` : ''}[data-uid="${currentComponentActive.current?.componentUid}"]`, 500);
436
+ if ($component) {
437
+ const $parents = $component?.querySelectorAll('[data-toolbar-parent]');
438
+ if ($parents.length) {
439
+ const onHover = ($parent)=>{
440
+ const uid = $parent.getAttribute('data-parent-uid');
441
+ if (!uid) return;
442
+ const $parentComponents = document.querySelectorAll(`[data-uid="${uid}"]`);
443
+ if ($parentComponents.length) {
444
+ $parentComponents.forEach(($parentComponent)=>{
445
+ const $outline = getChildrenByAttrSelector($parentComponent, 'data-outline');
446
+ if ($outline) {
447
+ $outline.setAttribute('data-outline-force-hover', 'true');
448
+ $outline.setAttribute('data-outline-force-overlay', 'true');
449
+ }
450
+ });
451
+ }
452
+ };
453
+ const outHover = ($parent)=>{
454
+ const uid = $parent.getAttribute('data-parent-uid');
455
+ if (!uid) return;
456
+ const $parentComponents = document.querySelectorAll(`[data-uid="${uid}"]`);
457
+ if ($parentComponents.length) {
458
+ $parentComponents.forEach(($parentComponent)=>{
459
+ const $outline = getChildrenByAttrSelector($parentComponent, 'data-outline');
460
+ if ($outline) {
461
+ $outline.removeAttribute('data-outline-force-hover');
462
+ $outline.removeAttribute('data-outline-force-overlay');
463
+ }
464
+ });
465
+ }
466
+ };
467
+ const onClick = async ($parent)=>{
468
+ const uid = $parent.getAttribute('data-parent-uid');
469
+ if (!uid) return;
470
+ const isElementInsideProduct = async ()=>{
471
+ const $component = await waitForElementToExist(`[data-uid="${uid}"]`, 500);
472
+ const $product = $component?.closest('[data-product-id]');
473
+ const productId = $product?.getAttribute('data-product-id') || '';
474
+ return productId ? true : false;
475
+ };
476
+ let productId = '';
477
+ if (await isElementInsideProduct()) {
478
+ productId = currentComponentActive.current?.productId || '';
479
+ }
480
+ const event = new CustomEvent('editor:toolbar:force-active-component', {
481
+ bubbles: true,
482
+ detail: {
483
+ componentUid: uid,
484
+ productId: productId
485
+ }
486
+ });
487
+ outHover($parent);
488
+ window.dispatchEvent(event);
489
+ };
490
+ $parents.forEach(($parent)=>{
491
+ $parent.addEventListener('mouseover', ()=>onHover($parent));
492
+ $parent.addEventListener('mouseout', ()=>outHover($parent));
493
+ $parent.addEventListener('click', ()=>onClick($parent));
494
+ });
495
+ }
496
+ }
497
+ };
498
+ /* Event listener */ const onMouseMove = useCallback((e)=>{
499
+ if (isDragging.current) return;
500
+ if (isResizeSpacing.current) return;
501
+ const $target = e.target;
502
+ if (!$target) {
503
+ removeHoverOverlayComponent();
504
+ return;
505
+ }
506
+ const $toolbarHover = $target.closest('[data-toolbar-hover]');
507
+ if ($toolbarHover) {
508
+ removeHoverOverlayComponent(); // remove overlay old
509
+ // Hover to toolbar is focus
510
+ if ($toolbarHover?.getAttribute('data-toolbar-hover-focus')) return;
511
+ const $component = $target.closest('[data-toolbar-wrap]');
512
+ if (!$component) return;
513
+ const $outline = getChildrenByAttrSelector($component, 'data-outline');
514
+ if (!$outline) return;
515
+ const isThemeSection = $component.getAttribute('data-theme-section');
516
+ const outlineOverlay = isThemeSection ? 'data-outline-overlay-theme-section' : 'data-outline-overlay';
517
+ $outline.setAttribute(outlineOverlay, 'true');
518
+ } else {
519
+ // Hover to other component
520
+ const $component = $target.closest('[data-toolbar-wrap]');
521
+ const componentUid = $component?.getAttribute('data-uid');
522
+ if (!$component || !componentUid || componentUid == 'ROOT') {
523
+ removeHoverComponent();
524
+ return;
525
+ }
526
+ const $toolbar = getChildrenByAttrSelector($component, 'data-toolbar');
527
+ const $outline = getChildrenByAttrSelector($component, 'data-outline');
528
+ if ($outline) $outline.removeAttribute('data-outline-overlay');
529
+ if (!componentUid) return;
530
+ if (componentUid == 'ROOT') return;
531
+ if ($toolbar?.getAttribute('data-toolbar-hover-focus')) return;
532
+ if (!$toolbar?.getAttribute('data-toolbar-hover-focus')) removeHoverComponent();
533
+ hoverActiveThemeSection($target);
534
+ // Disable event when hover active component
535
+ if (componentUid == currentComponentActive.current?.componentUid) {
536
+ if (currentComponentActive.current.productId) {
537
+ const $product = $component.closest(`[data-product-id]`);
538
+ if ($product) {
539
+ const productId = $product.getAttribute('data-product-id');
540
+ if (productId == currentComponentActive.current.productId) {
541
+ return;
542
+ }
543
+ }
544
+ }
545
+ const $themeSection = $target.closest('[data-theme-section]');
546
+ if ($themeSection) {
547
+ setHoverComponent({
548
+ $component: $themeSection,
549
+ focus: true,
550
+ isThemeSection: true
551
+ });
552
+ } else {
553
+ return;
554
+ }
555
+ }
556
+ const $themeSection = $target.closest('[data-theme-section]');
557
+ if ($themeSection) {
558
+ setHoverComponent({
559
+ $component: $themeSection,
560
+ focus: true,
561
+ isThemeSection: true
562
+ });
563
+ } else {
564
+ setHoverComponent({
565
+ $component,
566
+ componentUid,
567
+ focus: true
568
+ });
569
+ setHoverComponentParents({
570
+ $component,
571
+ componentUid
572
+ });
573
+ }
574
+ }
575
+ }, [
576
+ hoverActiveThemeSection,
577
+ removeHoverComponent,
578
+ setHoverComponent,
579
+ setHoverComponentParents,
580
+ removeHoverOverlayComponent,
581
+ currentComponentActive
582
+ ]);
583
+ const onActiveComponent = useCallback((e)=>{
584
+ if (isDragging.current) return;
585
+ const detail = e.detail;
586
+ if (detail?.componentUid) {
587
+ setActiveComponent({
588
+ componentUid: detail.componentUid,
589
+ productId: detail.productId
590
+ });
591
+ } else {
592
+ removeActiveComponent();
593
+ }
594
+ }, [
595
+ setActiveComponent,
596
+ removeActiveComponent,
597
+ isDragging
598
+ ]);
599
+ const onFocusOutsidePreview = useCallback(()=>{
600
+ removeHoverComponent();
601
+ }, [
602
+ removeHoverComponent
603
+ ]);
604
+ const onIsDragging = useCallback((e)=>{
605
+ const detail = e.detail;
606
+ if (detail.value) {
607
+ removeHoverComponent();
608
+ removeActiveComponent();
609
+ }
610
+ isDragging.current = detail.value;
611
+ }, [
612
+ removeHoverComponent,
613
+ removeActiveComponent
614
+ ]);
615
+ const onIsEditingTextEditor = useCallback((e)=>{
616
+ const detail = e.detail;
617
+ if (detail.value) {
618
+ setFocusTextEditor(true);
619
+ } else {
620
+ setFocusTextEditor(false);
621
+ }
622
+ }, []);
623
+ const onShowParents = useCallback((e)=>{
624
+ if (isDragging.current) return;
625
+ const detail = e.detail;
626
+ setShowParents({
627
+ value: detail?.value || false
628
+ });
629
+ }, []);
630
+ const onResizeSpacing = useCallback((e)=>{
631
+ const detail = e.detail;
632
+ if (detail?.value) {
633
+ removeHoverComponent();
634
+ isResizeSpacing.current = true;
635
+ } else {
636
+ isResizeSpacing.current = false;
637
+ }
638
+ }, [
639
+ removeHoverComponent
640
+ ]);
641
+ /* Register event */ useEffect(()=>{
642
+ document.addEventListener('mousemove', onMouseMove);
643
+ window.addEventListener('editor:active-component', onActiveComponent);
644
+ window.addEventListener('editor:focus-outside-preview', onFocusOutsidePreview);
645
+ window.addEventListener('editor:is-dragging', onIsDragging);
646
+ window.addEventListener('editor:is-editing-text-editor', onIsEditingTextEditor);
647
+ window.addEventListener('editor:toolbar:show-parents', onShowParents);
648
+ window.addEventListener('editor:toolbar:resize-spacing', onResizeSpacing);
649
+ return ()=>{
650
+ document.removeEventListener('mousemove', onMouseMove);
651
+ window.removeEventListener('editor:active-component', onActiveComponent);
652
+ window.removeEventListener('editor:is-dragging', onFocusOutsidePreview);
653
+ window.removeEventListener('editor:is-dragging', onIsDragging);
654
+ window.removeEventListener('editor:is-editing-text-editor', onIsEditingTextEditor);
655
+ window.removeEventListener('editor:toolbar:show-parents', onShowParents);
656
+ window.removeEventListener('editor:toolbar:resize-spacing', onResizeSpacing);
657
+ };
658
+ }, [
659
+ onMouseMove,
660
+ onActiveComponent,
661
+ onFocusOutsidePreview,
662
+ onIsDragging,
663
+ onIsEditingTextEditor,
664
+ onShowParents,
665
+ onResizeSpacing
666
+ ]);
667
+ return null;
668
+ };
669
+ var Toolbar$1 = /*#__PURE__*/ memo(Toolbar);
670
+
671
+ export { Toolbar$1 as default };