@@ -127,8 +127,6 @@ export default class NsdTemplateUpdated extends ScopedElementsMixin(
127127 @state ( )
128128 lNodeTypeDescription = '' ;
129129
130- private ignoreNextEditCount = false ;
131-
132130 updated ( changedProperties : Map < string , unknown > ) {
133131 super . updated ?.( changedProperties ) ;
134132 if ( changedProperties . has ( 'doc' ) ) {
@@ -137,18 +135,12 @@ export default class NsdTemplateUpdated extends ScopedElementsMixin(
137135 }
138136
139137 if ( changedProperties . has ( 'editCount' ) && this . editCount >= 0 ) {
140- const shouldRebuildTree = ! this . ignoreNextEditCount ;
141-
142- if ( this . ignoreNextEditCount ) {
143- this . ignoreNextEditCount = false ;
144- }
145-
146138 this . lNodeTypes = getLNodeTypes ( this . doc ) ;
147- this . refreshSelectedLNodeType ( shouldRebuildTree ) ;
139+ this . refreshSelectedLNodeType ( ) ;
148140 }
149141 }
150142
151- private refreshSelectedLNodeType ( rebuildTree : boolean = true ) : void {
143+ private refreshSelectedLNodeType ( ) : void {
152144 if ( ! this . selectedLNodeType ) return ;
153145
154146 const selectedId = this . selectedLNodeType . getAttribute ( 'id' ) ;
@@ -164,8 +156,6 @@ export default class NsdTemplateUpdated extends ScopedElementsMixin(
164156 this . selectedLNodeType = updatedLNodeType ;
165157 this . lNodeTypeDescription = updatedLNodeType . getAttribute ( 'desc' ) ?? '' ;
166158
167- if ( ! rebuildTree ) return ;
168-
169159 // Rebuild the tree to show the updated structure after undo/redo
170160 const selectedLNodeTypeClass = updatedLNodeType . getAttribute ( 'lnClass' ) ;
171161 if ( selectedLNodeTypeClass ) {
@@ -176,6 +166,7 @@ export default class NsdTemplateUpdated extends ScopedElementsMixin(
176166 ) ;
177167 if ( tree ) {
178168 this . lNodeTypeSelection = lNodeTypeToSelection ( updatedLNodeType ) ;
169+ this . nsdSelection = this . lNodeTypeSelection ;
179170 this . treeUI . tree = tree ;
180171 this . treeUI . selection = this . lNodeTypeSelection ;
181172 this . treeUI . requestUpdate ( ) ;
@@ -216,6 +207,22 @@ export default class NsdTemplateUpdated extends ScopedElementsMixin(
216207 this . choiceDialog ?. close ( ) ;
217208 }
218209
210+ // eslint-disable-next-line class-methods-use-this
211+ private applyDescriptionUpdate (
212+ newLNodeType : Element ,
213+ desc : string ,
214+ currentLNodeType : Element
215+ ) : void {
216+ const currentDesc = currentLNodeType . getAttribute ( 'desc' ) ?? '' ;
217+ if ( desc !== currentDesc ) {
218+ if ( desc ) {
219+ newLNodeType . setAttribute ( 'desc' , desc ) ;
220+ } else {
221+ newLNodeType . removeAttribute ( 'desc' ) ;
222+ }
223+ }
224+ }
225+
219226 private async saveTemplates ( ) {
220227 if ( ! this . doc || ! this . nsdSelection ) return ;
221228 const updateSetting =
@@ -241,6 +248,7 @@ export default class NsdTemplateUpdated extends ScopedElementsMixin(
241248 if ( this . selectedLNodeType && descChanged ) {
242249 this . updateLNodeTypeDescription ( desc ) ;
243250 this . lNodeTypes = getLNodeTypes ( this . doc ) ;
251+ this . showSuccessFeedback ( lnID ) ;
244252 }
245253 return ;
246254 }
@@ -252,76 +260,13 @@ export default class NsdTemplateUpdated extends ScopedElementsMixin(
252260 } ) ;
253261
254262 if ( updateSetting === UpdateSetting . Update ) {
255- const lNodeTypeInsertIndex = inserts . findIndex (
256- insert => ( insert . node as Element ) . tagName === 'LNodeType'
263+ const allEdits = this . buildUpdateEdits (
264+ inserts ,
265+ currentLNodeType ,
266+ lnID ,
267+ desc
257268 ) ;
258269
259- let newLNodeType : Element | undefined ;
260- let allEdits : Edit [ ] ;
261-
262- if ( lNodeTypeInsertIndex >= 0 ) {
263- // We have a new LNodeType from insertSelectedLNodeType
264- const originalInsert = inserts [ lNodeTypeInsertIndex ] ;
265- newLNodeType = ( originalInsert . node as Element ) . cloneNode (
266- true
267- ) as Element ;
268- newLNodeType . setAttribute ( 'id' , lnID ) ;
269-
270- const currentDescription = currentLNodeType . getAttribute ( 'desc' ) ?? '' ;
271- if ( desc !== currentDescription ) {
272- if ( desc ) {
273- newLNodeType . setAttribute ( 'desc' , desc ) ;
274- } else {
275- newLNodeType . removeAttribute ( 'desc' ) ;
276- }
277- }
278-
279- // Get supporting types (everything except the LNodeType insert)
280- const supportingTypes = inserts . filter (
281- ( _ , index ) => index !== lNodeTypeInsertIndex
282- ) ;
283-
284- // Remove the old LNodeType
285- const removeOld = removeDataType (
286- { node : currentLNodeType } ,
287- { force : true }
288- ) ;
289-
290- // Create new insert with the modified element
291- const newInsert = {
292- parent : originalInsert . parent ,
293- node : newLNodeType ,
294- reference : originalInsert . reference ,
295- } ;
296-
297- // Combine: supporting types first, then INSERT new, then remove old
298- // This ensures the reference element exists when we do the insert
299- allEdits = [ ...supportingTypes , newInsert , ...removeOld ] ;
300- } else if ( inserts . length === 0 ) {
301- // Only removals - clone existing and remove DOs
302- newLNodeType = removeDOsNotInSelection (
303- this . selectedLNodeType ! ,
304- this . nsdSelection !
305- ) ;
306-
307- newLNodeType . setAttribute ( 'id' , lnID ) ;
308-
309- const currentDescription = currentLNodeType . getAttribute ( 'desc' ) ?? '' ;
310- if ( desc !== currentDescription ) {
311- if ( desc ) {
312- newLNodeType . setAttribute ( 'desc' , desc ) ;
313- } else {
314- newLNodeType . removeAttribute ( 'desc' ) ;
315- }
316- }
317-
318- const updateEdits = updateLNodeType ( newLNodeType , this . doc ) ;
319- allEdits = updateEdits ;
320- } else {
321- // Only supporting types, no LNodeType changes
322- allEdits = inserts ;
323- }
324-
325270 if ( allEdits . length > 0 ) {
326271 this . dispatchEvent (
327272 newEditEvent ( allEdits , {
@@ -330,25 +275,16 @@ export default class NsdTemplateUpdated extends ScopedElementsMixin(
330275 ) ;
331276 }
332277
333- // Update the reference to point to the updated element in the document
334- this . selectedLNodeType = getSelectedLNodeType ( this . doc , lnID ) ;
335-
336- // Update nsdSelection to reflect the actual current state after the edit
337- if ( this . selectedLNodeType ) {
338- this . nsdSelection = lNodeTypeToSelection ( this . selectedLNodeType ) ;
339- }
340-
341- this . fabLabel = `${ lnID } updated!` ;
278+ this . showSuccessFeedback ( lnID , 'update' ) ;
342279 } else {
343- this . ignoreNextEditCount = true ;
280+ // Swap mode: Insert new, then remove old with squash
344281 this . dispatchEvent ( newEditEvent ( inserts ) ) ;
345282 await this . updateComplete ;
346283
347284 const remove = removeDataType (
348285 { node : this . selectedLNodeType ! } ,
349286 { force : true }
350287 ) ;
351- this . ignoreNextEditCount = true ;
352288 this . dispatchEvent (
353289 newEditEvent ( remove , { squash : true , title : `Update ${ lnID } ` } )
354290 ) ;
@@ -357,29 +293,87 @@ export default class NsdTemplateUpdated extends ScopedElementsMixin(
357293 insert => ( insert . node as Element ) . tagName === 'LNodeType'
358294 ) ?. node as Element ;
359295
360- if ( updatedLNodeType ) {
361- const updatedLNodeTypeID = updatedLNodeType . getAttribute ( 'id' ) ;
362- this . selectedLNodeType = updatedLNodeType ;
363- await this . updateComplete ;
364-
365- if ( this . lNodeTypeUI && updatedLNodeType ) {
366- this . lNodeTypeUI . value = updatedLNodeType . getAttribute ( 'id' ) ?? '' ;
367- }
368-
369- this . fabLabel = `${ updatedLNodeTypeID } swapped!` ;
296+ if ( updatedLNodeType && this . lNodeTypeUI ) {
297+ this . lNodeTypeUI . value = updatedLNodeType . getAttribute ( 'id' ) ?? '' ;
370298 }
299+
300+ const updatedID = updatedLNodeType ?. getAttribute ( 'id' ) ?? lnID ;
301+ this . showSuccessFeedback ( updatedID , 'swap' ) ;
371302 }
372303
373304 await this . updateComplete ;
374305 this . lNodeTypes = getLNodeTypes ( this . doc ) ;
306+ }
375307
308+ private showSuccessFeedback (
309+ lnID : string ,
310+ mode : 'update' | 'swap' = 'update'
311+ ) : void {
312+ this . fabLabel = mode === 'swap' ? `${ lnID } swapped!` : `${ lnID } updated!` ;
376313 setTimeout ( ( ) => {
377314 this . fabLabel = 'Update Logical Node Type' ;
378315 } , 5000 ) ;
379316 }
380317
318+ private buildUpdateEdits (
319+ inserts : Edit [ ] ,
320+ currentLNodeType : Element ,
321+ lnID : string ,
322+ desc : string
323+ ) : Edit [ ] {
324+ const lNodeTypeInsert = inserts . find (
325+ insert =>
326+ 'node' in insert && ( insert . node as Element ) . tagName === 'LNodeType'
327+ ) ;
328+
329+ if (
330+ lNodeTypeInsert &&
331+ 'node' in lNodeTypeInsert &&
332+ 'parent' in lNodeTypeInsert &&
333+ 'reference' in lNodeTypeInsert
334+ ) {
335+ const newLNodeType = ( lNodeTypeInsert . node as Element ) . cloneNode (
336+ true
337+ ) as Element ;
338+
339+ newLNodeType . setAttribute ( 'id' , lnID ) ;
340+ this . applyDescriptionUpdate ( newLNodeType , desc , currentLNodeType ) ;
341+
342+ const supportingTypes = inserts . filter (
343+ insert => insert !== lNodeTypeInsert
344+ ) ;
345+ const removeOld = removeDataType (
346+ { node : currentLNodeType } ,
347+ { force : true }
348+ ) ;
349+
350+ return [
351+ ...supportingTypes ,
352+ {
353+ parent : lNodeTypeInsert . parent ,
354+ node : newLNodeType ,
355+ reference : lNodeTypeInsert . reference ,
356+ } ,
357+ ...removeOld ,
358+ ] ;
359+ }
360+
361+ if ( inserts . length === 0 ) {
362+ const newLNodeType = removeDOsNotInSelection (
363+ currentLNodeType ,
364+ this . nsdSelection !
365+ ) ;
366+
367+ newLNodeType . setAttribute ( 'id' , lnID ) ;
368+ this . applyDescriptionUpdate ( newLNodeType , desc , currentLNodeType ) ;
369+
370+ return updateLNodeType ( newLNodeType , this . doc ! ) ;
371+ }
372+
373+ return inserts ;
374+ }
375+
381376 private updateLNodeTypeDescription ( desc : string ) : void {
382- this . ignoreNextEditCount = true ;
383377 this . lNodeTypeDescription = desc ;
384378 this . dispatchEvent (
385379 newEditEvent ( [
@@ -406,7 +400,6 @@ export default class NsdTemplateUpdated extends ScopedElementsMixin(
406400 { force : true }
407401 ) ;
408402
409- this . ignoreNextEditCount = true ;
410403 this . dispatchEvent ( newEditEvent ( remove , { title : `Delete ${ lnID } ` } ) ) ;
411404
412405 this . resetUI ( true ) ;
@@ -421,8 +414,6 @@ export default class NsdTemplateUpdated extends ScopedElementsMixin(
421414 this . treeUI . selection
422415 ) ;
423416
424- // Only update nsdSelection if the user's selection has actually changed
425- // This prevents overwriting the document-synced selection from a previous update
426417 if ( JSON . stringify ( newNsdSelection ) !== JSON . stringify ( this . nsdSelection ) ) {
427418 this . nsdSelection = newNsdSelection ;
428419 }
0 commit comments