Browse Source

Cleanup tracking

new-plugin-system
Artur Arseniev 4 months ago
parent
commit
d5fb443b60
  1. 41
      packages/core/src/plugin_manager/index.ts
  2. 1
      packages/core/src/plugin_manager/model/Plugin.ts
  3. 3
      packages/core/src/plugin_manager/types.ts

41
packages/core/src/plugin_manager/index.ts

@ -4,15 +4,14 @@ import BlocksEvents from '../block_manager/types';
import { ComponentsEvents } from '../dom_components/types'; import { ComponentsEvents } from '../dom_components/types';
import DeviceEvents from '../device_manager/types'; import DeviceEvents from '../device_manager/types';
import EditorModel from '../editor/model/Editor'; import EditorModel from '../editor/model/Editor';
import type { EditorEvent, EditorEventCallbacks } from '../editor/types';
import { KeymapsEvents } from '../keymaps/types'; import { KeymapsEvents } from '../keymaps/types';
import { StyleManagerEvents } from '../style_manager/types'; import { StyleManagerEvents } from '../style_manager/types';
import PluginModel, { createPluginAdded } from './model/Plugin'; import PluginModel, { createPluginAdded } from './model/Plugin';
import Plugins from './model/Plugins'; import Plugins from './model/Plugins';
import { Plugin, PluginCleanup, PluginInput, PluginOptions, PluginsEvents } from './types'; import { Plugin, PluginCleanup, PluginInput, PluginOptions, PluginsEvents, PluginTarget } from './types';
import { getPluginId, getPlugin, isPluginDescriptor, isPluginFunction, logPluginWarn, unwrapPluginMeta } from './utils'; import { getPluginId, getPlugin, isPluginDescriptor, isPluginFunction, logPluginWarn, unwrapPluginMeta } from './utils';
type PluginTarget = string | Plugin<any> | PluginModel;
export default class PluginManager extends ItemManagerModule<ModuleConfig, Plugins> { export default class PluginManager extends ItemManagerModule<ModuleConfig, Plugins> {
events = PluginsEvents; events = PluginsEvents;
storageKey = ''; storageKey = '';
@ -84,56 +83,50 @@ export default class PluginManager extends ItemManagerModule<ModuleConfig, Plugi
const added = createPluginAdded(); const added = createPluginAdded();
const cleanup: PluginCleanup[] = []; const cleanup: PluginCleanup[] = [];
const { editor } = this; const { editor } = this;
const listeners: Array<[string, (...args: any[]) => void]> = []; const listeners: Array<[EditorEvent, (...args: any[]) => void]> = [];
const listen = (event: string, callback: (...args: any[]) => void) => { const listen = <E extends EditorEvent & keyof EditorEventCallbacks>(
editor.on(event, callback); event: E,
callback: (...args: EditorEventCallbacks[E]) => void,
) => {
editor.on(event, callback as any);
listeners.push([event, callback]); listeners.push([event, callback]);
}; };
listen(BlocksEvents.add, (block: any) => { listen(BlocksEvents.add, (block) => {
const id = block.get?.('id'); const id = block.get('id');
if (!id || added.blocks.includes(id)) return; if (!id || added.blocks.includes(id)) return;
added.blocks.push(id); added.blocks.push(id);
cleanup.push(() => editor.Blocks.remove(id)); cleanup.push(() => editor.Blocks.remove(id));
}); });
listen(KeymapsEvents.add, (keymap: any) => { listen(KeymapsEvents.add, (keymap) => {
const id = keymap.id; const id = keymap.id;
if (!id || added.keymaps.includes(id)) return; if (!id || added.keymaps.includes(id)) return;
added.keymaps.push(id); added.keymaps.push(id);
cleanup.push(() => editor.Keymaps.remove(id)); cleanup.push(() => editor.Keymaps.remove(id));
}); });
listen(ComponentsEvents.typeAdd, (type: any) => { listen(ComponentsEvents.typeAdd, (type) => {
const id = type?.id; const id = type.id;
if (!id || added.componentTypes.includes(id)) return; if (!id || added.componentTypes.includes(id)) return;
added.componentTypes.push(id); added.componentTypes.push(id);
cleanup.push(() => editor.Components.removeType(id)); cleanup.push(() => editor.Components.removeType(id));
}); });
listen(DeviceEvents.add, (device: any) => { listen(DeviceEvents.add, (device) => {
const id = device?.id || device?.get?.('id'); const id = device.get('id');
if (!id || added.devices.includes(id)) return; if (!id || added.devices.includes(id)) return;
added.devices.push(id); added.devices.push(id);
cleanup.push(() => editor.Devices.remove(id)); cleanup.push(() => editor.Devices.remove(id));
}); });
listen(StyleManagerEvents.sectorAdd, (sector: any) => { listen(StyleManagerEvents.sectorAdd, (sector) => {
const id = sector?.id || sector?.get?.('id'); const id = sector.get('id');
if (!id || added.styleSectors.includes(id)) return; if (!id || added.styleSectors.includes(id)) return;
added.styleSectors.push(id); added.styleSectors.push(id);
cleanup.push(() => editor.StyleManager.removeSector(id)); cleanup.push(() => editor.StyleManager.removeSector(id));
}); });
listen(StyleManagerEvents.propertyAdd, (property: any) => {
const sectorId = property?.collection?.sector?.get?.('id');
const id = property?.get?.('property') || property?.get?.('id');
const alreadyAdded = added.styleProperties.some((item) => item.sectorId === sectorId && item.id === id);
if (!sectorId || !id || alreadyAdded) return;
added.styleProperties.push({ sectorId, id });
cleanup.push(() => editor.StyleManager.removeProperty(sectorId, id));
});
return { return {
added, added,
stop: () => listeners.forEach(([event, callback]) => editor.off(event, callback)), stop: () => listeners.forEach(([event, callback]) => editor.off(event, callback)),

1
packages/core/src/plugin_manager/model/Plugin.ts

@ -11,7 +11,6 @@ export const createPluginAdded = (): PluginAdded => ({
styleTypes: [], styleTypes: [],
assetTypes: [], assetTypes: [],
styleSectors: [], styleSectors: [],
styleProperties: [],
}); });
export interface PluginProperties { export interface PluginProperties {

3
packages/core/src/plugin_manager/types.ts

@ -10,6 +10,8 @@ import PluginModel from './model/Plugin';
export type PluginOptions = Record<string, any>; export type PluginOptions = Record<string, any>;
export type PluginTarget = string | Plugin<any> | PluginModel;
export interface PluginAdded { export interface PluginAdded {
blocks: string[]; blocks: string[];
commands: string[]; commands: string[];
@ -20,7 +22,6 @@ export interface PluginAdded {
styleTypes: string[]; styleTypes: string[];
assetTypes: string[]; assetTypes: string[];
styleSectors: string[]; styleSectors: string[];
styleProperties: Array<{ sectorId: string; id: string }>;
} }
export type PluginCleanup = () => void; export type PluginCleanup = () => void;

Loading…
Cancel
Save