UNPKG

5.17 kBTypeScriptView Raw
1/**
2 * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4 */
5import type { LoadedPlugins, PluginClassConstructor, PluginConstructor, PluginInterface } from './plugin.js';
6declare const PluginCollection_base: {
7 new (): import("@ckeditor/ckeditor5-utils").Emitter;
8 prototype: import("@ckeditor/ckeditor5-utils").Emitter;
9};
10/**
11 * Manages a list of CKEditor plugins, including loading, resolving dependencies and initialization.
12 */
13export default class PluginCollection<TContext extends object> extends PluginCollection_base implements Iterable<PluginEntry<TContext>> {
14 private _context;
15 private _plugins;
16 /**
17 * A map of plugin constructors that can be retrieved by their names.
18 */
19 private _availablePlugins;
20 /**
21 * Map of {@link module:core/contextplugin~ContextPlugin context plugins} which can be retrieved by their constructors or instances.
22 */
23 private _contextPlugins;
24 /**
25 * Creates an instance of the plugin collection class.
26 * Allows loading and initializing plugins and their dependencies.
27 * Allows providing a list of already loaded plugins. These plugins will not be destroyed along with this collection.
28 *
29 * @param availablePlugins Plugins (constructors) which the collection will be able to use
30 * when {@link module:core/plugincollection~PluginCollection#init} is used with the plugin names (strings, instead of constructors).
31 * Usually, the editor will pass its built-in plugins to the collection so they can later be
32 * used in `config.plugins` or `config.removePlugins` by names.
33 * @param contextPlugins A list of already initialized plugins represented by a `[ PluginConstructor, pluginInstance ]` pair.
34 */
35 constructor(context: TContext, availablePlugins?: Iterable<PluginConstructor<TContext>>, contextPlugins?: Iterable<PluginEntry<TContext>>);
36 /**
37 * Iterable interface.
38 *
39 * Returns `[ PluginConstructor, pluginInstance ]` pairs.
40 */
41 [Symbol.iterator](): IterableIterator<PluginEntry<TContext>>;
42 get<TConstructor extends PluginClassConstructor<TContext>>(key: TConstructor): InstanceType<TConstructor>;
43 get<TName extends string>(key: TName): PluginsMap[TName];
44 /**
45 * Checks if a plugin is loaded.
46 *
47 * ```ts
48 * // Check if the 'Clipboard' plugin was loaded.
49 * if ( editor.plugins.has( 'ClipboardPipeline' ) ) {
50 * // Now use the clipboard plugin instance:
51 * const clipboard = editor.plugins.get( 'ClipboardPipeline' );
52 *
53 * // ...
54 * }
55 * ```
56 *
57 * @param key The plugin constructor or {@link module:core/plugin~PluginStaticMembers#pluginName name}.
58 */
59 has(key: PluginConstructor<TContext> | string): boolean;
60 /**
61 * Initializes a set of plugins and adds them to the collection.
62 *
63 * @param plugins An array of {@link module:core/plugin~PluginInterface plugin constructors}
64 * or {@link module:core/plugin~PluginStaticMembers#pluginName plugin names}.
65 * @param pluginsToRemove Names of the plugins or plugin constructors
66 * that should not be loaded (despite being specified in the `plugins` array).
67 * @param pluginsSubstitutions An array of {@link module:core/plugin~PluginInterface plugin constructors}
68 * that will be used to replace plugins of the same names that were passed in `plugins` or that are in their dependency tree.
69 * A useful option for replacing built-in plugins while creating tests (for mocking their APIs). Plugins that will be replaced
70 * must follow these rules:
71 * * The new plugin must be a class.
72 * * The new plugin must be named.
73 * * Both plugins must not depend on other plugins.
74 * @returns A promise which gets resolved once all plugins are loaded and available in the collection.
75 */
76 init(plugins: ReadonlyArray<PluginConstructor<TContext> | string>, pluginsToRemove?: ReadonlyArray<PluginConstructor<TContext> | string>, pluginsSubstitutions?: ReadonlyArray<PluginConstructor<TContext>>): Promise<LoadedPlugins>;
77 /**
78 * Destroys all loaded plugins.
79 */
80 destroy(): Promise<unknown>;
81 /**
82 * Adds the plugin to the collection. Exposed mainly for testing purposes.
83 *
84 * @param PluginConstructor The plugin constructor.
85 * @param plugin The instance of the plugin.
86 */
87 private _add;
88}
89/**
90 * A `[ PluginConstructor, pluginInstance ]` pair.
91 */
92export type PluginEntry<TContext> = [PluginConstructor<TContext>, PluginInterface];
93/**
94 * Helper type that maps plugin names to their types.
95 * It is meant to be extended with module augmentation.
96 *
97 * ```ts
98 * class MyPlugin extends Plugin {
99 * public static pluginName() {
100 * return 'MyPlugin' as const;
101 * }
102 * }
103 *
104 * declare module '@ckeditor/ckeditor5-core' {
105 * interface PluginsMap {
106 * [ MyPlugin.pluginName ]: MyPlugin;
107 * }
108 * }
109 *
110 * // Returns `MyPlugin`.
111 * const myPlugin = editor.plugins.get( 'MyPlugin' );
112 * ```
113 */
114export interface PluginsMap {
115 [name: string]: PluginInterface;
116}
117export {};
118
\No newline at end of file