@@ -44,8 +44,13 @@ const EVENTS = {
4444 SETTING_UPDATED : 'setting:updated' ,
4545 SECTION_HIGHLIGHT : 'section:highlight' ,
4646 SECTION_UNHIGHLIGHT : 'section:unhighlight' ,
47- SECTION_SELECTED : 'section:selected' ,
48- BLOCK_SELECTED : 'block:selected' ,
47+ SECTION_SELECT : 'section:select' ,
48+ SECTION_DESELECT : 'section:deselect' ,
49+ SECTION_LOAD : 'section:load' ,
50+ SECTION_UNLOAD : 'section:unload' ,
51+
52+ BLOCK_SELECT : 'block:select' ,
53+ BLOCK_DESELECT : 'block:deselect' ,
4954 SECTION_ADDED : 'section:added' ,
5055 SECTION_REMOVED : 'section:removed' ,
5156 SECTIONS_REORDERED : 'sectionsOrder' ,
@@ -130,6 +135,8 @@ class VisualObject {
130135 ( targetEl . style as any ) [ config . style ] = value ;
131136 } else if ( config . attr ) {
132137 targetEl . setAttribute ( config . attr , value ) ;
138+ } else {
139+ return ;
133140 }
134141
135142 skipRefresh ( ) ;
@@ -158,8 +165,10 @@ class ThemeEditor {
158165
159166 private messageHandlers : Record < string , ( data : any , messageId ?: string ) => void > = {
160167 [ EVENTS . SECTION_HIGHLIGHT ] : ( data ) => this . handleSectionHighlight ( data ) ,
161- [ EVENTS . SECTION_SELECTED ] : ( data ) => this . handleSectionSelected ( data ) ,
162- [ EVENTS . BLOCK_SELECTED ] : ( data ) => this . handleBlockSelected ( data ) ,
168+ [ EVENTS . SECTION_SELECT ] : ( data ) => this . handleSectionSelected ( data ) ,
169+ [ EVENTS . SECTION_DESELECT ] : ( data ) => this . handleSectionDeselected ( data ) ,
170+ [ EVENTS . BLOCK_SELECT ] : ( data ) => this . handleBlockSelected ( data ) ,
171+ [ EVENTS . BLOCK_DESELECT ] : ( data ) => this . handleBlockDeselected ( data ) ,
163172 [ EVENTS . SECTION_ADDED ] : ( data ) => this . handleSectionAdded ( data ) ,
164173 [ EVENTS . SECTION_REMOVED ] : ( data ) => this . handleSectionRemoved ( data ) ,
165174 [ EVENTS . SECTION_UNHIGHLIGHT ] : ( ) => this . handleUnhighlightSection ( ) ,
@@ -262,6 +271,7 @@ class ThemeEditor {
262271 }
263272
264273 private handleWindowResize ( ) {
274+ console . log ( 'Window resized, focusing on active section if exists' , this . activeSectionId ) ;
265275 if ( this . activeSectionId ) {
266276 const activeSection = document . querySelector ( `[${ ATTRS . SectionId } ="${ this . activeSectionId } "]` ) as HTMLElement ;
267277
@@ -315,17 +325,42 @@ class ThemeEditor {
315325 this . handleSectionHighlight ( id ) ;
316326 el . scrollIntoView ( { behavior : 'smooth' , block : 'start' } ) ;
317327
318- window . Visual . _dispatch ( EVENTS . SECTION_SELECTED , {
328+ window . Visual . _dispatch ( EVENTS . SECTION_SELECT , {
319329 section : {
320- id : el . dataset . sectionId ,
330+ id,
321331 type : el . dataset . sectionType ,
322332 } ,
323333 } ) ;
334+
335+ window . Visual . _dispatch ( EVENTS . SECTION_SELECT + `:${ id } ` , { } ) ;
336+ }
337+
338+ private handleSectionDeselected ( id : string ) {
339+ if ( this . activeSectionId === id ) {
340+ this . clearActiveSection ( ) ;
341+ this . activeSectionId = null ;
342+ }
343+
344+ const el = document . querySelector ( `[${ ATTRS . SectionId } ="${ id } "]` ) as HTMLElement ;
345+ if ( ! el ) {
346+ return ;
347+ }
348+
349+ el . removeAttribute ( ATTRS . VisualHighlighted ) ;
350+
351+ window . Visual . _dispatch ( EVENTS . SECTION_DESELECT , { section : { id, type : el . dataset . sectionType } } ) ;
352+ window . Visual . _dispatch ( EVENTS . SECTION_DESELECT + `:${ id } ` , { } ) ;
324353 }
325354
326355 private handleBlockSelected ( data : { sectionId : string ; blockId : string } ) {
327356 this . handleSectionSelected ( data . sectionId ) ;
328- window . Visual . _dispatch ( EVENTS . BLOCK_SELECTED , data ) ;
357+ window . Visual . _dispatch ( EVENTS . BLOCK_SELECT , data ) ;
358+ window . Visual . _dispatch ( EVENTS . BLOCK_SELECT + `:${ data . blockId } ` , { } ) ;
359+ }
360+
361+ private handleBlockDeselected ( data : { sectionId : string ; blockId : string } ) {
362+ window . Visual . _dispatch ( EVENTS . BLOCK_DESELECT , data ) ;
363+ window . Visual . _dispatch ( EVENTS . BLOCK_DESELECT + `:${ data . blockId } ` , { } ) ;
329364 }
330365
331366 private handleSectionAdded ( { section } : { section : SectionData } ) {
@@ -399,7 +434,7 @@ class ThemeEditor {
399434 const el = document . querySelector ( `[${ ATTRS . SectionId } ="${ this . activeSectionId } "]` ) as HTMLElement ;
400435 if ( el ) {
401436 this . focusOnSection ( el ) ;
402- el . scrollIntoView ( { behavior : 'smooth' , block : 'start' } ) ;
437+ // el.scrollIntoView({ behavior: 'smooth', block: 'start' });
403438 }
404439 }
405440
@@ -448,9 +483,9 @@ class ThemeEditor {
448483 return false ;
449484 }
450485
451- elements . forEach ( ( el ) => {
486+ for ( const el of elements ) {
452487 const type = el . getAttribute ( attrName ) ;
453- const [ updateType , updateKey ] = type ?. split ( ':' ) ?? [ 'text' , undefined ] ;
488+ const [ updateType , updateKey ] = type ?. split ( / : ( . + ) / ) ?? [ 'text' , undefined ] ;
454489
455490 switch ( updateType ) {
456491 case 'text' :
@@ -464,6 +499,10 @@ class ThemeEditor {
464499 break ;
465500 case 'attr' :
466501 if ( ! settingValue ) {
502+ if ( el . tagName . toLowerCase ( ) === 'img' && updateKey === 'src' ) {
503+ return false ;
504+ }
505+
467506 el . removeAttribute ( updateKey as string ) ;
468507 } else {
469508 el . setAttribute ( updateKey as string , settingValue ) ;
@@ -476,10 +515,13 @@ class ThemeEditor {
476515 ( el as HTMLElement ) . style . setProperty ( updateKey as string , settingValue ) ;
477516 }
478517 break ;
518+ case 'toggleClass' :
519+ el . classList . toggle ( updateKey as string ) ;
520+ break ;
479521 default :
480522 console . warn ( `Unknown live update type: ${ updateType } ` ) ;
481523 }
482- } ) ;
524+ }
483525
484526 return true ;
485527 }
@@ -539,6 +581,7 @@ class ThemeEditor {
539581 const newEl = newDoc . querySelector ( `[data-section-id="${ sectionId } "]` ) ;
540582
541583 if ( oldEl && newEl ) {
584+ window . Visual . _dispatch ( EVENTS . SECTION_UNLOAD , context ) ;
542585 this . patchNode ( oldEl , newEl ) ;
543586 } else if ( ! oldEl && newEl ) {
544587 const sections = document . querySelectorAll ( `[${ ATTRS . SectionType } ]` ) ;
@@ -560,7 +603,7 @@ class ThemeEditor {
560603 return ;
561604 }
562605
563- window . Visual . _dispatch ( 'section:updated' , context ) ;
606+ window . Visual . _dispatch ( EVENTS . SECTION_LOAD , context ) ;
564607 } ) ;
565608 }
566609
0 commit comments