@@ -133,6 +133,18 @@ window.addEventListener('load', () => {
133133 } else if ( e . target . classList . contains ( 'duplicate-section' ) ) {
134134 const original = e . target . closest ( '.section-editor' ) ;
135135 duplicateSection ( original ) ;
136+ } else if ( e . target . classList . contains ( 'add-library' ) ) {
137+ const section = e . target . closest ( '.section-editor' ) ;
138+ const idx = section . dataset . index ;
139+ const name = prompt ( 'Block name' ) ;
140+ if ( ! name ) return ;
141+ const html = editors [ idx ] . root . innerHTML ;
142+ const token = document . querySelector ( 'input[name="__RequestVerificationToken"]' ) . value ;
143+ fetch ( '/AdminBlockTemplate/CreateFromSection' , {
144+ method : 'POST' ,
145+ headers : { 'RequestVerificationToken' : token } ,
146+ body : new URLSearchParams ( { name, html } )
147+ } ) . then ( ( ) => alert ( 'Saved to library' ) ) ;
136148 }
137149 } ) ;
138150
@@ -145,7 +157,7 @@ window.addEventListener('load', () => {
145157 }
146158 } ) ;
147159
148- function addSection ( ) {
160+ function addSection ( htmlContent = '' , zone = null ) {
149161 const index = sectionCount ++ ;
150162 const html = templateHtml . replace ( / _ _ i n d e x _ _ / g, index ) ;
151163 const temp = document . createElement ( 'div' ) ;
@@ -155,9 +167,29 @@ window.addEventListener('load', () => {
155167 populateZones ( section . querySelector ( '.zone-select' ) ) ;
156168 placeSection ( section ) ;
157169 initSectionEditor ( index ) ;
170+ if ( htmlContent ) {
171+ editors [ index ] . root . innerHTML = htmlContent ;
172+ const input = document . getElementById ( `Html-${ index } ` ) ;
173+ if ( input ) input . value = htmlContent ;
174+ }
175+ if ( zone ) {
176+ const select = section . querySelector ( '.zone-select' ) ;
177+ if ( select ) { select . value = zone ; }
178+ placeSection ( section ) ;
179+ }
158180 updateIndexes ( ) ;
159181 updatePreview ( ) ;
160182 updateZoneCounts ( ) ;
183+ return index ;
184+ }
185+
186+ function addSectionFromBlock ( id , zone ) {
187+ fetch ( `/AdminBlockTemplate/Html/${ id } ` )
188+ . then ( r => r . text ( ) )
189+ . then ( html => {
190+ addSection ( html , zone ) ;
191+ autoSave ( ) ;
192+ } ) ;
161193 }
162194
163195 function duplicateSection ( original ) {
@@ -204,11 +236,13 @@ window.addEventListener('load', () => {
204236 }
205237
206238 let dragged = null ;
239+ let draggedBlockId = null ;
207240 const dropIndicator = document . createElement ( 'div' ) ;
208241 dropIndicator . className = 'drop-indicator' ;
209242
210243 container . addEventListener ( 'dragstart' , e => {
211244 dragged = e . target . closest ( '.section-editor' ) ;
245+ draggedBlockId = null ;
212246 if ( dragged ) {
213247 dragged . classList . add ( 'dragging' ) ;
214248 document . querySelectorAll ( '.zone-group' ) . forEach ( z => z . classList . add ( 'drag-over' ) ) ;
@@ -221,7 +255,7 @@ window.addEventListener('load', () => {
221255 const zone = e . target . closest ( '.zone-group' ) ;
222256 const target = e . target . closest ( '.section-editor' ) ;
223257 if ( zone ) zone . classList . add ( 'drag-over' ) ;
224- if ( dragged && target && target !== dragged ) {
258+ if ( ! draggedBlockId && dragged && target && target !== dragged ) {
225259 const rect = target . getBoundingClientRect ( ) ;
226260 const next = ( e . clientY - rect . top ) > ( rect . height / 2 ) ;
227261 target . parentNode . insertBefore ( dropIndicator , next ? target . nextSibling : target ) ;
@@ -237,6 +271,13 @@ window.addEventListener('load', () => {
237271
238272 container . addEventListener ( 'drop' , e => {
239273 e . preventDefault ( ) ;
274+ const zone = e . target . closest ( '.zone-group' ) ;
275+ if ( draggedBlockId && zone ) {
276+ addSectionFromBlock ( draggedBlockId , zone . dataset . zone ) ;
277+ draggedBlockId = null ;
278+ document . querySelectorAll ( '.zone-group.drag-over' ) . forEach ( z => z . classList . remove ( 'drag-over' ) ) ;
279+ return ;
280+ }
240281 if ( dropIndicator . parentNode ) {
241282 dropIndicator . parentNode . insertBefore ( dragged , dropIndicator ) ;
242283 dropIndicator . remove ( ) ;
@@ -261,6 +302,12 @@ window.addEventListener('load', () => {
261302 } ) ;
262303 }
263304
305+ function autoSave ( ) {
306+ if ( ! form ) return ;
307+ const fd = new FormData ( form ) ;
308+ fetch ( form . action , { method : 'POST' , body : fd } ) ;
309+ }
310+
264311 function initSectionEditor ( index ) {
265312 const typeSelect = document . getElementById ( `type-select-${ index } ` ) ;
266313 const htmlDiv = document . getElementById ( `html-editor-${ index } ` ) ;
0 commit comments