1- class App {
1+ class WebVisualEditor {
22 codeEdits = [ ] ;
33 operation = '' ;
44 keyboard = {
@@ -38,7 +38,7 @@ class App {
3838 htmlParser = null ;
3939
4040 constructor ( ) {
41- const state = JSON . parse ( sessionStorage . getItem ( codeId ) ?? '{}' ) ;
41+ const state = JSON . parse ( sessionStorage . getItem ( wve . codeId ) ?? '{}' ) ;
4242 this . zoom = state . zoom ?? '1' ;
4343 this . linkCode = state . linkCode ?? true ;
4444 }
@@ -94,7 +94,7 @@ class App {
9494 toolbarZoomOut : 'wve-zoom-out' ,
9595 toolbarGroupAlign : 'wve-group-align' ,
9696 } ;
97- this . toolbar . innerHTML = `
97+ let toolbarHtml = ( `
9898 <fieldset>
9999 <label class="wve-button" title="Link selections with editor">
100100 <input id="${ controls . toolbarLinkCode } " type="checkbox">
@@ -104,30 +104,36 @@ class App {
104104 <span id="${ controls . toolbarZoomValue } ">100%</span>
105105 <button id="${ controls . toolbarZoomOut } " type="button" class="wve-button">zoom_out</button>
106106 </fieldset>
107- <fieldset id="${ controls . toolbarGroupAlign } " disabled>
108- <button type="button" class="wve-button" id="align-horizontal-left">align_horizontal_left</button>
109- <button type="button" class="wve-button" id="align-horizontal-center">align_horizontal_center</button>
110- <button type="button" class="wve-button" id="align-horizontal-right">align_horizontal_right</button>
111- <button type="button" class="wve-button" id="align-vertical-top">align_vertical_top</button>
112- <button type="button" class="wve-button" id="align-vertical-center">align_vertical_center</button>
113- <button type="button" class="wve-button" id="align-vertical-bottom">align_vertical_bottom</button>
114- <button type="button" class="wve-button" id="align-horizontal-justify">align_justify_space_even</button>
115- <button type="button" class="wve-button" id="align-vertical-justify">align_space_even</button>
116- </fieldset>
117- ` ;
107+ ` ) ;
108+ if ( wve . config . enableMovingElements ) {
109+ toolbarHtml += `
110+ <fieldset id="${ controls . toolbarGroupAlign } " disabled>
111+ <button type="button" class="wve-button" id="align-horizontal-left">align_horizontal_left</button>
112+ <button type="button" class="wve-button" id="align-horizontal-center">align_horizontal_center</button>
113+ <button type="button" class="wve-button" id="align-horizontal-right">align_horizontal_right</button>
114+ <button type="button" class="wve-button" id="align-vertical-top">align_vertical_top</button>
115+ <button type="button" class="wve-button" id="align-vertical-center">align_vertical_center</button>
116+ <button type="button" class="wve-button" id="align-vertical-bottom">align_vertical_bottom</button>
117+ <button type="button" class="wve-button" id="align-horizontal-justify">align_justify_space_even</button>
118+ <button type="button" class="wve-button" id="align-vertical-justify">align_space_even</button>
119+ </fieldset>` ;
120+ }
121+ this . toolbar . innerHTML = toolbarHtml ;
118122 Object . entries ( controls ) . forEach ( ( [ key , id ] ) => {
119123 this [ key ] = fragment . getElementById ( id ) ;
120124 } ) ;
121- this . toolbarZoomIn . addEventListener ( 'click' , event => { this . updateZoom ( 1 ) ; } ) ;
122- this . toolbarZoomOut . addEventListener ( 'click' , event => { this . updateZoom ( - 1 ) ; } ) ;
123- this . toolbarGroupAlign . addEventListener ( 'click' , this . onClickGroupAlign ) ;
124125 this . toolbarLinkCode . addEventListener ( 'change' , event => {
125126 this . linkCode = event . target . checked ;
126127 this . saveState ( ) ;
127128 } ) ;
129+ this . toolbarZoomIn . addEventListener ( 'click' , event => { this . updateZoom ( 1 ) ; } ) ;
130+ this . toolbarZoomOut . addEventListener ( 'click' , event => { this . updateZoom ( - 1 ) ; } ) ;
128131 this . toolbarRefresh . addEventListener ( 'click' , event => {
129132 vscode . postMessage ( { type : 'refresh' } ) ;
130133 } ) ;
134+ if ( wve . config . enableMovingElements ) {
135+ this . toolbarGroupAlign . addEventListener ( 'click' , this . onClickGroupAlign ) ;
136+ }
131137 document . body . appendChild ( fragment ) ;
132138 }
133139
@@ -167,7 +173,7 @@ class App {
167173 const state = Object . fromEntries (
168174 [ 'zoom' , 'linkCode' ] . map ( key => [ key , this [ key ] ] )
169175 ) ;
170- sessionStorage . setItem ( codeId , JSON . stringify ( state ) ) ;
176+ sessionStorage . setItem ( wve . codeId , JSON . stringify ( state ) ) ;
171177 vscode . postMessage ( { type : 'state' , data : state } ) ;
172178 }
173179
@@ -213,7 +219,9 @@ class App {
213219 } ) ;
214220 vscode . postMessage ( { type : 'edit' , data } ) ;
215221 this . codeEdits = [ ] ;
216- this . moversBeforeEdit . clear ( ) ;
222+ if ( wve . config . enableMovingElements ) {
223+ this . moversBeforeEdit . clear ( ) ;
224+ }
217225 }
218226
219227 emitSelectionChange ( ) {
@@ -245,10 +253,12 @@ class App {
245253 }
246254 this . selected . add ( element ) ;
247255 element . setAttribute ( 'wve-selected' , '' ) ;
248- if ( element . hasAttribute ( 'wve-movable' ) ) {
249- this . movers . add ( element ) ;
256+ if ( wve . config . enableMovingElements ) {
257+ if ( element . hasAttribute ( 'wve-movable' ) ) {
258+ this . movers . add ( element ) ;
259+ }
260+ if ( this . movers . size > 1 ) { this . toolbarGroupAlign . removeAttribute ( 'disabled' ) ; }
250261 }
251- if ( this . movers . size > 1 ) { this . toolbarGroupAlign . removeAttribute ( 'disabled' ) ; }
252262 if ( emit ) { this . emitSelectionChange ( ) ; }
253263 }
254264 // Deselect element
@@ -265,8 +275,10 @@ class App {
265275 }
266276 this . selected . delete ( element ) ;
267277 element . removeAttribute ( 'wve-selected' ) ;
268- this . movers . delete ( element ) ;
269- if ( this . movers . size < 2 ) { this . toolbarGroupAlign . setAttribute ( 'disabled' , '' ) ; }
278+ if ( wve . config . enableMovingElements ) {
279+ this . movers . delete ( element ) ;
280+ if ( this . movers . size < 2 ) { this . toolbarGroupAlign . setAttribute ( 'disabled' , '' ) ; }
281+ }
270282 this . emitSelectionChange ( ) ;
271283 }
272284 // Deselect if the element is selected, otherwise select it
@@ -278,9 +290,12 @@ class App {
278290 }
279291 }
280292 beginStyleEdit ( ) {
281- this . moversBeforeEdit = new Map ( this . movers . values ( ) . map ( el => [ el , el . cloneNode ( true ) ] ) ) ;
293+ if ( wve . config . enableMovingElements ) {
294+ this . moversBeforeEdit = new Map ( this . movers . values ( ) . map ( el => [ el , el . cloneNode ( true ) ] ) ) ;
295+ }
282296 }
283297 finishStyleEdit ( type ) {
298+ if ( ! wve . config . enableMovingElements ) { return ; }
284299 this . movers . forEach ( element => {
285300 const style = element . getAttribute ( 'style' ) ;
286301 if ( style === this . moversBeforeEdit . get ( element ) . getAttribute ( 'style' ) ) { return ; }
@@ -348,14 +363,16 @@ class App {
348363 if ( ! prev . Control && kbd . Control ) {
349364 document . body . classList . add ( 'wve-adding-selection' ) ;
350365 }
351- if ( this . operation === '' ) {
352- if ( ! kbd . arrow || this . movers . size === 0 ) { return ; }
353- if ( ! prev . arrow ) { this . beginStyleEdit ( ) ; }
354- const dx = kbd . ArrowRight ? 1 : kbd . ArrowLeft ? - 1 : 0 ;
355- const dy = kbd . ArrowDown ? 1 : kbd . ArrowUp ? - 1 : 0 ;
356- this . movers . forEach ( el => { this . moveElement ( el , dx , dy ) ; } ) ;
357- // Disable scroll
358- event . preventDefault ( ) ;
366+ if ( wve . config . enableMovingElements ) {
367+ if ( this . operation === '' ) {
368+ if ( ! kbd . arrow || this . movers . size === 0 ) { return ; }
369+ if ( ! prev . arrow ) { this . beginStyleEdit ( ) ; }
370+ const dx = kbd . ArrowRight ? 1 : kbd . ArrowLeft ? - 1 : 0 ;
371+ const dy = kbd . ArrowDown ? 1 : kbd . ArrowUp ? - 1 : 0 ;
372+ this . movers . forEach ( el => { this . moveElement ( el , dx , dy ) ; } ) ;
373+ // Disable scroll
374+ event . preventDefault ( ) ;
375+ }
359376 }
360377 } ;
361378
@@ -375,7 +392,7 @@ class App {
375392 if ( prev . Control && ! this . keyboard . Control ) {
376393 document . body . classList . remove ( 'wve-adding-selection' ) ;
377394 }
378- if ( prev . arrow && ! this . keyboard . arrow ) {
395+ if ( wve . config . enableMovingElements && prev . arrow && ! this . keyboard . arrow ) {
379396 this . finishStyleEdit ( 'move' ) ;
380397 this . emitCodeEdits ( ) ;
381398 }
@@ -609,15 +626,17 @@ const vscode = acquireVsCodeApi();
609626
610627// Initial display
611628document . addEventListener ( 'DOMContentLoaded' , async ( ) => {
612- const app = new App ( ) ;
629+ const app = new WebVisualEditor ( ) ;
613630 // Remove Visual Studio Code default styles
614631 document . getElementById ( '_defaultStyles' ) ?. remove ( ) ;
615632 // Incorporate styles into the user-layer
616633 // NOTE Implement here rather than Extension Host due to JSDOM's lack of @layer support
617634 document . querySelectorAll ( 'style:not(#wve-user-css-imports)' ) . forEach ( el => {
618635 el . textContent = `\n@layer user-style {\n${ el . textContent } \n}` ;
619636 } ) ;
620- app . initMovables ( ) ;
637+ if ( wve . config . enableMovingElements ) {
638+ app . initMovables ( ) ;
639+ }
621640 app . initSelector ( ) ;
622641 app . initToolbar ( ) ;
623642 app . updateZoom ( ) ;
0 commit comments