@@ -26,26 +26,30 @@ import {
26
26
} from '@openscd/open-scd/src/foundation.js' ;
27
27
import { Nsdoc } from '@openscd/open-scd/src/foundation/nsdoc.js' ;
28
28
import { getIcon } from '@openscd/open-scd/src/icons/icons.js' ;
29
+ import { OscdApi } from '@openscd/core' ;
29
30
30
31
/** An editor [[`plugin`]] for editing the `IED` section. */
31
32
export default class IedPlugin extends LitElement {
32
33
/** The document being edited as provided to plugins by [[`OpenSCD`]]. */
33
34
@property ( )
34
35
doc ! : XMLDocument ;
35
-
36
+
36
37
@property ( { type : Number } )
37
38
editCount = - 1 ;
38
-
39
+
39
40
/** All the nsdoc files that are being uploaded via the settings. */
40
41
@property ( )
41
42
nsdoc ! : Nsdoc ;
42
-
43
+
44
+ @property ( )
45
+ oscdApi : OscdApi | null = null ;
46
+
43
47
@state ( )
44
48
selectedIEDs : string [ ] = [ ] ;
45
-
49
+
46
50
@state ( )
47
51
selectedLNClasses : string [ ] = [ ] ;
48
-
52
+
49
53
@state ( )
50
54
private get iedList ( ) : Element [ ] {
51
55
return this . doc
@@ -61,26 +65,26 @@ export default class IedPlugin extends LitElement {
61
65
const uniqueLNClassList : string [ ] = [ ] ;
62
66
if ( currentIed ) {
63
67
return Array . from ( currentIed . querySelectorAll ( 'LN0, LN' ) )
64
- . filter ( element => element . hasAttribute ( 'lnClass' ) )
65
- . filter ( element => {
66
- const lnClass = element . getAttribute ( 'lnClass' ) ?? '' ;
67
- if ( uniqueLNClassList . includes ( lnClass ) ) {
68
- return false ;
69
- }
70
- uniqueLNClassList . push ( lnClass ) ;
71
- return true ;
72
- } )
73
- . sort ( ( a , b ) => {
74
- const aLnClass = a . getAttribute ( 'lnClass' ) ?? '' ;
75
- const bLnClass = b . getAttribute ( 'lnClass' ) ?? '' ;
76
-
77
- return aLnClass . localeCompare ( bLnClass ) ;
78
- } )
79
- . map ( element => {
80
- const lnClass = element . getAttribute ( 'lnClass' ) ;
81
- const label = this . nsdoc . getDataDescription ( element ) . label ;
82
- return [ lnClass , label ] ;
83
- } ) as string [ ] [ ] ;
68
+ . filter ( element => element . hasAttribute ( 'lnClass' ) )
69
+ . filter ( element => {
70
+ const lnClass = element . getAttribute ( 'lnClass' ) ?? '' ;
71
+ if ( uniqueLNClassList . includes ( lnClass ) ) {
72
+ return false ;
73
+ }
74
+ uniqueLNClassList . push ( lnClass ) ;
75
+ return true ;
76
+ } )
77
+ . sort ( ( a , b ) => {
78
+ const aLnClass = a . getAttribute ( 'lnClass' ) ?? '' ;
79
+ const bLnClass = b . getAttribute ( 'lnClass' ) ?? '' ;
80
+
81
+ return aLnClass . localeCompare ( bLnClass ) ;
82
+ } )
83
+ . map ( element => {
84
+ const lnClass = element . getAttribute ( 'lnClass' ) ;
85
+ const label = this . nsdoc . getDataDescription ( element ) . label ;
86
+ return [ lnClass , label ] ;
87
+ } ) as string [ ] [ ] ;
84
88
}
85
89
return [ ] ;
86
90
}
@@ -101,6 +105,16 @@ export default class IedPlugin extends LitElement {
101
105
102
106
lNClassListOpenedOnce = false ;
103
107
108
+ connectedCallback ( ) : void {
109
+ super . connectedCallback ( ) ;
110
+ this . loadPluginState ( ) ;
111
+ }
112
+
113
+ disconnectedCallback ( ) : void {
114
+ super . disconnectedCallback ( ) ;
115
+ this . storePluginState ( ) ;
116
+ }
117
+
104
118
protected updated ( _changedProperties : PropertyValues ) : void {
105
119
super . updated ( _changedProperties ) ;
106
120
@@ -132,6 +146,23 @@ export default class IedPlugin extends LitElement {
132
146
}
133
147
}
134
148
149
+ private loadPluginState ( ) : void {
150
+ const stateApi = this . oscdApi ?. pluginState ;
151
+ const selectedIEDs : string [ ] | null = ( stateApi ?. getState ( ) ?. selectedIEDs as string [ ] ) ?? null ;
152
+
153
+ if ( selectedIEDs ) {
154
+ this . onSelectionChange ( selectedIEDs ) ;
155
+ }
156
+ }
157
+
158
+ private storePluginState ( ) : void {
159
+ const stateApi = this . oscdApi ?. pluginState ;
160
+
161
+ if ( stateApi ) {
162
+ stateApi . setState ( { selectedIEDs : this . selectedIEDs } ) ;
163
+ }
164
+ }
165
+
135
166
private calcSelectedLNClasses ( ) : string [ ] {
136
167
const somethingSelected = this . selectedLNClasses . length > 0 ;
137
168
const lnClasses = this . lnClassList . map ( lnClassInfo => lnClassInfo [ 0 ] ) ;
@@ -147,6 +178,29 @@ export default class IedPlugin extends LitElement {
147
178
return selectedLNClasses ;
148
179
}
149
180
181
+ private onSelectionChange ( selectedIeds : string [ ] ) : void {
182
+ const equalArrays = < T > ( first : T [ ] , second : T [ ] ) : boolean => {
183
+ return (
184
+ first . length === second . length &&
185
+ first . every ( ( val , index ) => val === second [ index ] )
186
+ ) ;
187
+ } ;
188
+
189
+ const selectionChanged = ! equalArrays (
190
+ this . selectedIEDs ,
191
+ selectedIeds
192
+ ) ;
193
+
194
+ if ( ! selectionChanged ) {
195
+ return ;
196
+ }
197
+
198
+ this . lNClassListOpenedOnce = false ;
199
+ this . selectedIEDs = selectedIeds ;
200
+ this . selectedLNClasses = [ ] ;
201
+ this . requestUpdate ( 'selectedIed' ) ;
202
+ }
203
+
150
204
render ( ) : TemplateResult {
151
205
const iedList = this . iedList ;
152
206
if ( iedList . length > 0 ) {
@@ -158,28 +212,7 @@ export default class IedPlugin extends LitElement {
158
212
id= "iedFilter"
159
213
icon = "developer_board"
160
214
.header = ${ get ( 'iededitor.iedSelector' ) }
161
- @selected-items-changed = "${ ( e : SelectedItemsChangedEvent ) => {
162
- const equalArrays = < T > ( first : T [ ] , second : T [ ] ) : boolean => {
163
- return (
164
- first . length === second . length &&
165
- first . every ( ( val , index ) => val === second [ index ] )
166
- ) ;
167
- } ;
168
-
169
- const selectionChanged = ! equalArrays (
170
- this . selectedIEDs ,
171
- e . detail . selectedItems
172
- ) ;
173
-
174
- if ( ! selectionChanged ) {
175
- return ;
176
- }
177
-
178
- this . lNClassListOpenedOnce = false ;
179
- this . selectedIEDs = e . detail . selectedItems ;
180
- this . selectedLNClasses = [ ] ;
181
- this . requestUpdate ( 'selectedIed' ) ;
182
- } } "
215
+ @selected-items-changed = "${ ( e : SelectedItemsChangedEvent ) => this . onSelectionChange ( e . detail . selectedItems ) } "
183
216
>
184
217
${ iedList . map ( ied => {
185
218
const name = getNameAttribute ( ied ) ;
0 commit comments