Skip to content

Commit 79abfbe

Browse files
authored
fix(plugging): fix plugin auto-update (#253) (#264)
1 parent 04b621d commit 79abfbe

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

src/Plugging.ts

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const pluginIcons: Record<PluginKind | MenuPosition, string> = {
4141
bottom: 'play_circle',
4242
};
4343

44-
function storeDefaultPlugins(): void {
44+
function resetPlugins(): void {
4545
localStorage.setItem(
4646
'plugins',
4747
JSON.stringify(
@@ -63,6 +63,7 @@ const menuOrder: (PluginKind | MenuPosition)[] = [
6363
'middle',
6464
'bottom',
6565
];
66+
6667
function menuCompare(a: Plugin, b: Plugin): -1 | 0 | 1 {
6768
if (a.kind === b.kind && a.position === b.position) return 0;
6869
const earlier = menuOrder.find(kind =>
@@ -113,22 +114,24 @@ export function Plugging<TBase extends new (...args: any[]) => EditingElement>(
113114
private get plugins(): Plugin[] {
114115
return this.storedPlugins
115116
.map(plugin => {
116-
if (!plugin.official) return this.addContent(plugin);
117+
if (!plugin.official) return plugin;
117118
const officialPlugin = officialPlugins.find(
118119
needle => needle.src === plugin.src
119120
);
120-
return this.addContent(<Omit<Plugin, 'content'>>{
121+
return <Plugin>{
121122
...officialPlugin,
122123
...plugin,
123-
});
124+
};
124125
})
125126
.sort(compareNeedsDoc)
126127
.sort(menuCompare);
127128
}
128129

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+
)
132135
);
133136
}
134137

@@ -147,10 +150,35 @@ export function Plugging<TBase extends new (...args: any[]) => EditingElement>(
147150
this.requestUpdate();
148151
}
149152

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+
150178
private addExternalPlugin(plugin: Omit<Plugin, 'content'>): void {
151179
if (this.storedPlugins.some(p => p.src === plugin.src)) return;
152180

153-
const newPlugins = this.storedPlugins;
181+
const newPlugins: Omit<Plugin, 'content'>[] = this.storedPlugins;
154182
newPlugins.push(plugin);
155183
localStorage.setItem('plugins', JSON.stringify(newPlugins));
156184
}
@@ -218,8 +246,7 @@ export function Plugging<TBase extends new (...args: any[]) => EditingElement>(
218246
constructor(...args: any[]) {
219247
super(...args);
220248

221-
if (localStorage.getItem('officialPlugins') === null)
222-
storeDefaultPlugins();
249+
this.updatePlugins();
223250
this.requestUpdate();
224251
}
225252

@@ -403,7 +430,7 @@ export function Plugging<TBase extends new (...args: any[]) => EditingElement>(
403430
icon="refresh"
404431
label="${translate('reset')}"
405432
@click=${async () => {
406-
storeDefaultPlugins();
433+
resetPlugins();
407434
this.requestUpdate();
408435
}}
409436
style="--mdc-theme-primary: var(--mdc-theme-error)"

0 commit comments

Comments
 (0)