diff --git a/packages/core/src/plugin_manager/index.ts b/packages/core/src/plugin_manager/index.ts index 52826a5f3..1b3aa88d0 100644 --- a/packages/core/src/plugin_manager/index.ts +++ b/packages/core/src/plugin_manager/index.ts @@ -1,8 +1,8 @@ import { isFunction, isString } from 'underscore'; import { ItemManagerModule, ModuleConfig } from '../abstract/Module'; import BlocksEvents from '../block_manager/types'; -import { ComponentsEvents } from '../dom_components/types'; import DeviceEvents from '../device_manager/types'; +import { ComponentsEvents } from '../dom_components/types'; import EditorModel from '../editor/model/Editor'; import type { EditorEvent, EditorEventCallbacks } from '../editor/types'; import { KeymapsEvents } from '../keymaps/types'; @@ -10,7 +10,7 @@ import { StyleManagerEvents } from '../style_manager/types'; import PluginModel, { createPluginAdded } from './model/Plugin'; import Plugins from './model/Plugins'; import { Plugin, PluginCleanup, PluginInput, PluginOptions, PluginsEvents, PluginTarget } from './types'; -import { getPluginId, getPlugin, isPluginDescriptor, isPluginFunction, logPluginWarn, unwrapPluginMeta } from './utils'; +import { getPlugin, getPluginId, isPluginDescriptor, isPluginFunction, logPluginWarn, unwrapPluginMeta } from './utils'; export default class PluginManager extends ItemManagerModule { events = PluginsEvents; diff --git a/packages/core/src/plugin_manager/types.ts b/packages/core/src/plugin_manager/types.ts index 6236f7150..1adaec0a5 100644 --- a/packages/core/src/plugin_manager/types.ts +++ b/packages/core/src/plugin_manager/types.ts @@ -1,4 +1,3 @@ -import type Editor from '../editor'; import { EventCallbackAdd, EventCallbackAll, @@ -6,6 +5,7 @@ import { EventCallbackRemoveBefore, EventCallbackUpdate, } from '../common'; +import type Editor from '../editor'; import PluginModel from './model/Plugin'; export type PluginOptions = Record; diff --git a/packages/core/src/plugin_manager/utils.ts b/packages/core/src/plugin_manager/utils.ts index 45740fb58..eb31dab51 100644 --- a/packages/core/src/plugin_manager/utils.ts +++ b/packages/core/src/plugin_manager/utils.ts @@ -1,6 +1,5 @@ import { isFunction, isString } from 'underscore'; import type Editor from '../editor'; -import EditorModel from '../editor/model/Editor'; import { getGlobal } from '../utils/mixins'; import type { Plugin, PluginDescriptor, PluginOptions, PluginWithMeta } from './types'; @@ -39,7 +38,7 @@ export const clearLegacyPlugins = () => legacyPlugins.clear(); export const addLegacyPlugin = (id: string, plugin: Plugin) => { console.error(LEGACY_PLUGIN_ERROR); - legacyPlugins.set(id, plugin as Plugin); + legacyPlugins.set(id, plugin); return plugin; }; @@ -75,44 +74,37 @@ export const getPluginId = (plugin: Plugin) => export const getPluginById = (pluginId: string) => { const legacy = getLegacyPlugin(pluginId); - if (legacy) { - return legacy; - } + if (legacy) return legacy; const globalPlugin = (getGlobal() as any)[pluginId]; return globalPlugin?.default || globalPlugin; }; -export const getPlugin = (plugin: string | Plugin) => { +export const getPlugin = (plugin: string | Plugin): Plugin | undefined => { const { plugin: unwrapped } = unwrapPluginMeta(plugin); return isString(unwrapped) ? getPluginById(unwrapped) : (unwrapped as unknown as { default?: Plugin })?.default || unwrapped; }; -export const logPluginWarn = (editor: { getModel(): EditorModel }, plugin: string) => { - editor.getModel().logWarning(`Plugin ${plugin} not found`, { - context: 'plugins', - plugin, - }); +export const logPluginWarn = (editor: Editor, plugin: string) => { + editor.getModel().logWarning(`Plugin ${plugin} not found`, { context: 'plugins', plugin }); }; export const usePlugin =

| string>(plugin: P, opts?: P extends Plugin ? C : {}) => { - const wrapped = ((editor: Editor) => { + const options = opts || {}; + const wrapped: PluginWithMeta = (editor: Editor) => { const pluginResult = getPlugin(plugin); if (pluginResult) { - pluginResult(editor, opts || {}); + pluginResult(editor, options); } else { - logPluginWarn(editor as { getModel(): EditorModel }, plugin as string); + logPluginWarn(editor, plugin as string); } - }) as PluginWithMeta; - - wrapped.__gjsPluginMeta = { - plugin, - options: (opts || {}) as Record, - id: typeof plugin === 'string' ? plugin : plugin.__gjsPluginId, }; + const id = typeof plugin === 'string' ? plugin : plugin.__gjsPluginId; + wrapped.__gjsPluginMeta = { id, plugin, options }; + return wrapped; };