@@ -41,7 +41,7 @@ export const pluginIcons: Record<PluginKind | MenuPosition, string> = {
41
41
bottom : 'play_circle' ,
42
42
} ;
43
43
44
- function storeDefaultPlugins ( ) : void {
44
+ function resetPlugins ( ) : void {
45
45
localStorage . setItem (
46
46
'plugins' ,
47
47
JSON . stringify (
@@ -63,6 +63,7 @@ const menuOrder: (PluginKind | MenuPosition)[] = [
63
63
'middle' ,
64
64
'bottom' ,
65
65
] ;
66
+
66
67
function menuCompare ( a : Plugin , b : Plugin ) : - 1 | 0 | 1 {
67
68
if ( a . kind === b . kind && a . position === b . position ) return 0 ;
68
69
const earlier = menuOrder . find ( kind =>
@@ -113,22 +114,24 @@ export function Plugging<TBase extends new (...args: any[]) => EditingElement>(
113
114
private get plugins ( ) : Plugin [ ] {
114
115
return this . storedPlugins
115
116
. map ( plugin => {
116
- if ( ! plugin . official ) return this . addContent ( plugin ) ;
117
+ if ( ! plugin . official ) return plugin ;
117
118
const officialPlugin = officialPlugins . find (
118
119
needle => needle . src === plugin . src
119
120
) ;
120
- return this . addContent ( < Omit < Plugin , 'content' > > {
121
+ return < Plugin > {
121
122
...officialPlugin ,
122
123
...plugin ,
123
- } ) ;
124
+ } ;
124
125
} )
125
126
. sort ( compareNeedsDoc )
126
127
. sort ( menuCompare ) ;
127
128
}
128
129
129
- private get storedPlugins ( ) : Omit < Plugin , 'content' > [ ] {
130
- return < Omit < Plugin , 'content' > [ ] > (
131
- JSON . parse ( localStorage . getItem ( 'plugins' ) ?? '[]' )
130
+ private get storedPlugins ( ) : Plugin [ ] {
131
+ return < Plugin [ ] > (
132
+ JSON . parse ( localStorage . getItem ( 'plugins' ) ?? '[]' , ( key , value ) =>
133
+ value . src ? this . addContent ( value ) : value
134
+ )
132
135
) ;
133
136
}
134
137
@@ -147,10 +150,35 @@ export function Plugging<TBase extends new (...args: any[]) => EditingElement>(
147
150
this . requestUpdate ( ) ;
148
151
}
149
152
153
+ private updatePlugins ( ) {
154
+ const stored : (
155
+ | Plugin
156
+ | { src : string ; installed : boolean ; official : true }
157
+ ) [ ] = this . storedPlugins ;
158
+ const officialStored = stored . filter ( p => p . official ) ;
159
+ const newOfficial = officialPlugins
160
+ . filter ( p => ! officialStored . find ( o => o . src === p . src ) )
161
+ . map ( plugin => {
162
+ return {
163
+ src : plugin . src ,
164
+ installed : plugin . default ?? false ,
165
+ official : true as const ,
166
+ } ;
167
+ } ) ;
168
+ const oldOfficial = officialStored . filter (
169
+ p => ! officialPlugins . find ( o => p . src === o . src )
170
+ ) ;
171
+ const newPlugins = stored . filter (
172
+ p => ! oldOfficial . find ( o => p . src === o . src )
173
+ ) ;
174
+ newOfficial . map ( p => newPlugins . push ( p ) ) ;
175
+ localStorage . setItem ( 'plugins' , JSON . stringify ( newPlugins ) ) ;
176
+ }
177
+
150
178
private addExternalPlugin ( plugin : Omit < Plugin , 'content' > ) : void {
151
179
if ( this . storedPlugins . some ( p => p . src === plugin . src ) ) return ;
152
180
153
- const newPlugins = this . storedPlugins ;
181
+ const newPlugins : Omit < Plugin , 'content' > [ ] = this . storedPlugins ;
154
182
newPlugins . push ( plugin ) ;
155
183
localStorage . setItem ( 'plugins' , JSON . stringify ( newPlugins ) ) ;
156
184
}
@@ -218,8 +246,7 @@ export function Plugging<TBase extends new (...args: any[]) => EditingElement>(
218
246
constructor ( ...args : any [ ] ) {
219
247
super ( ...args ) ;
220
248
221
- if ( localStorage . getItem ( 'officialPlugins' ) === null )
222
- storeDefaultPlugins ( ) ;
249
+ this . updatePlugins ( ) ;
223
250
this . requestUpdate ( ) ;
224
251
}
225
252
@@ -403,7 +430,7 @@ export function Plugging<TBase extends new (...args: any[]) => EditingElement>(
403
430
icon ="refresh "
404
431
label ="${ translate ( 'reset' ) } "
405
432
@click =${ async ( ) => {
406
- storeDefaultPlugins ( ) ;
433
+ resetPlugins ( ) ;
407
434
this . requestUpdate ( ) ;
408
435
} }
409
436
style ="--mdc-theme-primary: var(--mdc-theme-error)"
0 commit comments