@@ -3,7 +3,11 @@ window.addEventListener('load', () => {
33 if ( ! container ) return ;
44 const templateHtml = document . getElementById ( 'section-template' ) . innerHTML . trim ( ) ;
55 let sectionCount = container . querySelectorAll ( '.section-editor' ) . length ;
6- const editors = { } ;
6+ const editors = new Map ( ) ;
7+ document . addEventListener ( 'section-editor:ready' , e => {
8+ editors . set ( e . detail . index , e . detail . quill ) ;
9+ e . detail . quill . on ( 'text-change' , schedulePreview ) ;
10+ } ) ;
711 let activeIndex = null ;
812 const layoutSelect = document . getElementById ( 'layout-select' ) ;
913 let currentLayout = layoutSelect ? layoutSelect . value : 'single-column' ;
@@ -50,7 +54,8 @@ window.addEventListener('load', () => {
5054 const idx = sec . dataset . index ;
5155 const typeSel = document . getElementById ( `type-select-${ idx } ` ) ;
5256 if ( typeSel && typeSel . value === 'Html' ) {
53- return editors [ idx ] . root . innerHTML ;
57+ const q = editors . get ( idx ) ;
58+ return q ? q . root . innerHTML : '' ;
5459 }
5560 const inp = document . getElementById ( `Html-${ idx } ` ) ;
5661 return inp ? inp . value : '' ;
@@ -138,15 +143,15 @@ window.addEventListener('load', () => {
138143 templateSelect . addEventListener ( 'change' , ( ) => {
139144 const id = templateSelect . value ;
140145 if ( ! id ) return ;
141- if ( activeIndex === null || ! editors [ activeIndex ] ) {
146+ if ( activeIndex === null || ! editors . has ( activeIndex ) ) {
142147 alert ( 'Select a section first' ) ;
143148 templateSelect . value = '' ;
144149 return ;
145150 }
146151 fetch ( `/AdminBlockTemplate/Html/${ id } ` )
147152 . then ( r => r . text ( ) )
148153 . then ( html => {
149- const quill = editors [ activeIndex ] ;
154+ const quill = editors . get ( activeIndex ) ;
150155 quill . root . innerHTML = html ;
151156 const input = document . getElementById ( `Html-${ activeIndex } ` ) ;
152157 if ( input ) input . value = html ;
@@ -161,7 +166,7 @@ window.addEventListener('load', () => {
161166 const idx = el . dataset . index ;
162167 populateZones ( el . querySelector ( '.zone-select' ) ) ;
163168 placeSection ( el ) ;
164- initSectionEditor ( idx ) ;
169+ document . dispatchEvent ( new CustomEvent ( 'section-editor:add' , { detail : el } ) ) ;
165170 } ) ;
166171 updatePreview ( ) ;
167172 updateZoneCounts ( ) ;
@@ -195,7 +200,8 @@ window.addEventListener('load', () => {
195200 const idx = section . dataset . index ;
196201 const name = prompt ( 'Block name' ) ;
197202 if ( ! name ) return ;
198- const html = editors [ idx ] . root . innerHTML ;
203+ const q = editors . get ( idx ) ;
204+ const html = q ? q . root . innerHTML : '' ;
199205 const token = document . querySelector ( 'input[name="__RequestVerificationToken"]' ) . value ;
200206 fetch ( '/AdminBlockTemplate/CreateFromSection' , {
201207 method : 'POST' ,
@@ -223,9 +229,10 @@ window.addEventListener('load', () => {
223229 section . dataset . index = index ;
224230 populateZones ( section . querySelector ( '.zone-select' ) ) ;
225231 placeSection ( section ) ;
226- initSectionEditor ( index ) ;
232+ document . dispatchEvent ( new CustomEvent ( 'section-editor:add' , { detail : section } ) ) ;
227233 if ( htmlContent ) {
228- editors [ index ] . root . innerHTML = htmlContent ;
234+ const q = editors . get ( index ) ;
235+ if ( q ) q . root . innerHTML = htmlContent ;
229236 const input = document . getElementById ( `Html-${ index } ` ) ;
230237 if ( input ) input . value = htmlContent ;
231238 }
@@ -270,11 +277,13 @@ window.addEventListener('load', () => {
270277 } ) ;
271278 populateZones ( clone . querySelector ( '.zone-select' ) ) ;
272279 placeSection ( clone ) ;
273- initSectionEditor ( index ) ;
274- if ( editors [ original . dataset . index ] ) {
275- editors [ index ] . root . innerHTML = editors [ original . dataset . index ] . root . innerHTML ;
280+ document . dispatchEvent ( new CustomEvent ( 'section-editor:add' , { detail : clone } ) ) ;
281+ const orig = editors . get ( original . dataset . index ) ;
282+ const copy = editors . get ( index ) ;
283+ if ( orig && copy ) {
284+ copy . root . innerHTML = orig . root . innerHTML ;
276285 const destInput = clone . querySelector ( `#Html-${ index } ` ) ;
277- if ( destInput ) destInput . value = editors [ index ] . root . innerHTML ;
286+ if ( destInput ) destInput . value = copy . root . innerHTML ;
278287 }
279288 updateIndexes ( ) ;
280289 updatePreview ( ) ;
@@ -294,6 +303,9 @@ window.addEventListener('load', () => {
294303
295304 let dragged = null ;
296305 let draggedBlockId = null ;
306+ document . addEventListener ( 'blockdragstart' , e => {
307+ draggedBlockId = e . detail ;
308+ } ) ;
297309 const dropIndicator = document . createElement ( 'div' ) ;
298310 dropIndicator . className = 'drop-indicator' ;
299311
@@ -349,7 +361,7 @@ window.addEventListener('load', () => {
349361 const form = document . querySelector ( 'form' ) ;
350362 if ( form ) {
351363 form . addEventListener ( 'submit' , ( ) => {
352- Object . entries ( editors ) . forEach ( ( [ key , quill ] ) => {
364+ editors . forEach ( ( quill , key ) => {
353365 const typeSelect = document . getElementById ( `type-select-${ key } ` ) ;
354366 if ( typeSelect && typeSelect . value === 'Html' ) {
355367 const input = document . getElementById ( `Html-${ key } ` ) ;
@@ -365,37 +377,6 @@ window.addEventListener('load', () => {
365377 fetch ( form . action , { method : 'POST' , body : fd } ) ;
366378 }
367379
368- function initSectionEditor ( index ) {
369- const typeSelect = document . getElementById ( `type-select-${ index } ` ) ;
370- const htmlDiv = document . getElementById ( `html-editor-${ index } ` ) ;
371- const markdownDiv = document . getElementById ( `markdown-editor-${ index } ` ) ;
372- const codeDiv = document . getElementById ( `code-editor-${ index } ` ) ;
373- const fileDiv = document . getElementById ( `file-editor-${ index } ` ) ;
374- const quillInput = document . getElementById ( `Html-${ index } ` ) ;
375- const markdown = markdownDiv ? markdownDiv . querySelector ( 'textarea' ) : null ;
376- const code = codeDiv ? codeDiv . querySelector ( 'textarea' ) : null ;
377- const quill = new Quill ( `#quill-editor-${ index } ` , { theme : 'snow' } ) ;
378- quill . root . innerHTML = quillInput . value || '' ;
379- quill . root . addEventListener ( 'click' , ( ) => { activeIndex = index ; } ) ;
380- quill . root . addEventListener ( 'focus' , ( ) => { activeIndex = index ; } ) ;
381- editors [ index ] = quill ;
382- quill . on ( 'text-change' , schedulePreview ) ;
383-
384- function update ( ) {
385- const type = typeSelect . value ;
386- htmlDiv . style . display = type === 'Html' ? 'block' : 'none' ;
387- markdownDiv . style . display = type === 'Markdown' ? 'block' : 'none' ;
388- codeDiv . style . display = type === 'Code' ? 'block' : 'none' ;
389- fileDiv . style . display = ( type === 'Image' || type === 'Video' ) ? 'block' : 'none' ;
390- if ( markdown ) markdown . disabled = type !== 'Markdown' ;
391- if ( code ) code . disabled = type !== 'Code' ;
392- quillInput . disabled = type !== 'Html' ;
393- const fileInput = fileDiv ? fileDiv . querySelector ( 'input[type="file"]' ) : null ;
394- if ( fileInput ) fileInput . disabled = ! ( type === 'Image' || type === 'Video' ) ;
395- }
396- update ( ) ;
397- typeSelect . addEventListener ( 'change' , update ) ;
398- }
399380
400381 modePreview ?. addEventListener ( 'click' , ( ) => {
401382 pageEditor ?. classList . add ( 'preview' ) ;
0 commit comments