11import {
22 EditV2 ,
33 Insert ,
4- isComplex ,
5- isInsert ,
6- isRemove ,
7- isSetAttributes ,
8- isSetTextContent ,
94 Remove ,
105 SetAttributes ,
116 SetTextContent ,
127} from "@omicronenergy/oscd-api" ;
138
9+ import {
10+ isComplexEditV2 ,
11+ isInsert ,
12+ isRemove ,
13+ isSetAttributes ,
14+ isSetTextContent ,
15+ } from "@omicronenergy/oscd-api/utils.js" ;
16+
1417function handleSetTextContent ( {
1518 element,
1619 textContent,
@@ -32,65 +35,69 @@ function handleSetTextContent({
3235
3336function handleSetAttributes ( {
3437 element,
35- attributes,
36- attributesNS,
38+ attributes = { } ,
39+ attributesNS = { } ,
3740} : SetAttributes ) : SetAttributes {
3841 const oldAttributes = { ...attributes } ;
3942 const oldAttributesNS = { ...attributesNS } ;
4043
4144 // save element's non-prefixed attributes for undo
42- Object . keys ( attributes )
43- . reverse ( )
44- . forEach ( ( name ) => {
45- oldAttributes [ name ] = element . getAttribute ( name ) ;
46- } ) ;
45+ if ( attributes )
46+ Object . keys ( attributes )
47+ . reverse ( )
48+ . forEach ( ( name ) => {
49+ oldAttributes [ name ] = element . getAttribute ( name ) ;
50+ } ) ;
4751
4852 // change element's non-prefixed attributes
49- for ( const entry of Object . entries ( attributes ) ) {
50- try {
51- const [ name , value ] = entry as [ string , string | null ] ;
52- if ( value === null ) element . removeAttribute ( name ) ;
53- else element . setAttribute ( name , value ) ;
54- } catch ( _e ) {
55- // undo nothing if update didn't work on this attribute
56- delete oldAttributes [ entry [ 0 ] ] ;
53+ if ( attributes )
54+ for ( const entry of Object . entries ( attributes ) ) {
55+ try {
56+ const [ name , value ] = entry as [ string , string | null ] ;
57+ if ( value === null ) element . removeAttribute ( name ) ;
58+ else element . setAttribute ( name , value ) ;
59+ } catch ( _e ) {
60+ // undo nothing if update didn't work on this attribute
61+ delete oldAttributes [ entry [ 0 ] ] ;
62+ }
5763 }
58- }
5964
6065 // save element's namespaced attributes for undo
61- Object . entries ( attributesNS ) . forEach ( ( [ ns , attrs ] ) => {
62- Object . keys ( attrs ! )
63- . reverse ( )
64- . forEach ( ( name ) => {
65- oldAttributesNS [ ns ] = {
66- ...oldAttributesNS [ ns ] ,
67- [ name ] : element . getAttributeNS ( ns , name . split ( ":" ) . pop ( ) ! ) ,
68- } ;
66+ if ( attributesNS )
67+ Object . entries ( attributesNS ) . forEach ( ( [ ns , attrs ] ) => {
68+ Object . keys ( attrs ! )
69+ . reverse ( )
70+ . forEach ( ( name ) => {
71+ oldAttributesNS [ ns ] = {
72+ ...oldAttributesNS [ ns ] ,
73+ [ name ] : element . getAttributeNS ( ns , name . split ( ":" ) . pop ( ) ! ) ,
74+ } ;
75+ } ) ;
76+ Object . keys ( attrs ! ) . forEach ( ( name ) => {
77+ delete oldAttributesNS [ ns ] ! [ name ] ;
6978 } ) ;
70- Object . keys ( attrs ! ) . forEach ( ( name ) => {
71- delete oldAttributesNS [ ns ] ! [ name ] ;
7279 } ) ;
73- } ) ;
7480
7581 // change element's namespaced attributes
76- for ( const nsEntry of Object . entries ( attributesNS ) ) {
77- const [ ns , attrs ] = nsEntry as [
78- string ,
79- Partial < Record < string , string | null > > ,
80- ] ;
81- for ( const entry of Object . entries ( attrs ) ) {
82- try {
83- const [ name , value ] = entry as [ string , string | null ] ;
84- if ( value === null ) {
85- element . removeAttributeNS ( ns , name . split ( ":" ) . pop ( ) ! ) ;
86- } else {
87- element . setAttributeNS ( ns , name , value ) ;
82+ if ( attributesNS )
83+ for ( const nsEntry of Object . entries ( attributesNS ) ) {
84+ const [ ns , attrs ] = nsEntry as [
85+ string ,
86+ Partial < Record < string , string | null > > ,
87+ ] ;
88+ for ( const entry of Object . entries ( attrs ) ) {
89+ try {
90+ const [ name , value ] = entry as [ string , string | null ] ;
91+ if ( value === null ) {
92+ element . removeAttributeNS ( ns , name . split ( ":" ) . pop ( ) ! ) ;
93+ } else {
94+ element . setAttributeNS ( ns , name , value ) ;
95+ }
96+ } catch ( _e ) {
97+ delete oldAttributesNS [ ns ] ! [ entry [ 0 ] ] ;
8898 }
89- } catch ( _e ) {
90- delete oldAttributesNS [ ns ] ! [ entry [ 0 ] ] ;
9199 }
92100 }
93- }
94101
95102 return {
96103 element,
@@ -141,13 +148,11 @@ export function handleEdit(edit: EditV2): EditV2 {
141148 if ( isRemove ( edit ) ) return handleRemove ( edit ) ;
142149 if ( isSetAttributes ( edit ) ) return handleSetAttributes ( edit ) ;
143150 if ( isSetTextContent ( edit ) ) return handleSetTextContent ( edit ) ;
144- if ( isComplex ( edit ) )
151+ if ( isComplexEditV2 ( edit ) )
145152 return edit
146153 . map ( ( edit ) => handleEdit ( edit ) )
147154 . reverse ( )
148155 . flat ( Infinity as 1 ) ;
149156
150- console . error ( `Invalid edit provided: ${ edit } ` ) ;
151-
152157 return [ ] ;
153158}
0 commit comments