UNPKG

10.5 kBTypeScriptView Raw
1import type { Handler, ProsemirrorPlugin } from '@remirror/core-types';
2import { EditorState, Plugin, PluginKey } from '@remirror/pm/state';
3import { AnyExtension, AnyExtensionConstructor, GetNameUnion, PlainExtension } from '../extension';
4import type { AppendLifecycleProps, ApplyStateLifecycleProps, CreateExtensionPlugin } from '../types';
5export interface PluginsOptions {
6 /**
7 * The event handler which can be used by hooks to listen to state updates
8 * when they are being applied to the editor.
9 */
10 applyState?: Handler<(props: ApplyStateLifecycleProps) => void>;
11 /**
12 * The event handler which can be used by hooks to listen to intercept updates
13 * to the transaction.
14 */
15 appendTransaction?: Handler<(props: AppendLifecycleProps) => void>;
16}
17/**
18 * This extension allows others extension to add the `createPlugin` method using
19 * Prosemirror Plugins.
20 *
21 * @remarks
22 *
23 * This is an example of adding custom functionality to an extension via the
24 * `ExtensionParameterMethods`.
25 *
26 * @category Builtin Extension
27 */
28export declare class PluginsExtension extends PlainExtension<PluginsOptions> {
29 get name(): "plugins";
30 /**
31 * All plugins created by other extension as well.
32 */
33 private plugins;
34 /**
35 * The plugins added via the manager (for reference only).
36 */
37 private managerPlugins;
38 /**
39 * Called when the state is is being applied after an update.
40 */
41 private readonly applyStateHandlers;
42 /**
43 * Called when the state is first initialized.
44 */
45 private readonly initStateHandlers;
46 /**
47 * Handlers for the `onAppendTransaction` lifecycle method.
48 */
49 private readonly appendTransactionHandlers;
50 /**
51 * Store the plugin keys.
52 */
53 private readonly pluginKeys;
54 /**
55 * Store state getters for the extension.
56 */
57 private readonly stateGetters;
58 /**
59 * This extension is responsible for adding state to the editor.
60 */
61 onCreate(): void;
62 /**
63 * Create a plugin which adds the [[`onInitState`]] and [[`onApplyState`]]
64 * lifecycle methods.
65 */
66 createPlugin(): CreateExtensionPlugin<void>;
67 /**
68 * Get all the plugins from the extension.
69 */
70 private extractExtensionPlugins;
71 private readonly getPluginStateCreator;
72 /**
73 * Add or replace a plugin.
74 */
75 private updatePlugins;
76 private readonly getStateByName;
77 /**
78 * Add the plugin specific properties and methods to the manager and extension
79 * store.
80 */
81 private updateExtensionStore;
82 /**
83 * Reruns the `createPlugin` and `createExternalPlugins` methods of the
84 * provided extension.
85 *
86 * ```ts
87 * // From within an extension
88 * this.store.updateExtensionPlugins(this);
89 * ```
90 */
91 private updateExtensionPlugins;
92 /**
93 * Applies the store plugins to the state. If any have changed then it will be
94 * updated.
95 */
96 private dispatchPluginUpdate;
97}
98declare global {
99 namespace Remirror {
100 interface ManagerSettings {
101 /**
102 * Add custom plugins to the manager while creating it.
103 *
104 * Plugins created via the manager are given priority over all extension
105 * based plugins. There's scope for adding a priority based model for
106 * inserting plugins, but it seems like a sane default until that's
107 * available.
108 */
109 plugins?: ProsemirrorPlugin[];
110 }
111 interface ExtensionStore {
112 /**
113 * Retrieve the state for any given extension name. This will throw an
114 * error if the extension identified by that name doesn't implement the
115 * `createPlugin` method.
116 *
117 * @param name - the name of the extension
118 *
119 * @remarks
120 *
121 * ```ts
122 * const pluginState = getPluginState(extension.name);
123 * ```
124 */
125 getPluginState<State>(name: string): State;
126 /**
127 * Add the new plugins. If previous plugins are provided then also remove
128 * the previous plugins.
129 *
130 * ```ts
131 * this.store.updatePlugins(this.createExternalPlugins(), this.externalPlugins);
132 * ```
133 *
134 * @param plugins - the plugins to add
135 * @param previousPlugins - the plugins to remove
136 */
137 updatePlugins(plugins: ProsemirrorPlugin[], previousPlugins?: ProsemirrorPlugin[]): void;
138 /**
139 * Reruns the `createPlugin` and `createExternalPlugins` methods of the
140 * provided extension.
141 *
142 * This will also automatically update the state with the newly generated
143 * plugins by dispatching an update.
144 *
145 * ```ts
146 * // From within an extension
147 * this.store.updateExtensionPlugins(this);
148 * this.store.dispatchPluginUpdate();
149 * ```
150 *
151 * @param extension - the extension instance, constructor or name.
152 */
153 updateExtensionPlugins(extension: AnyExtension | AnyExtensionConstructor | string): void;
154 /**
155 * Applies the store plugins to the state. If any have changed then it
156 * will be updated.
157 */
158 dispatchPluginUpdate(): void;
159 }
160 interface ManagerStore<Extension extends AnyExtension> {
161 /**
162 * All of the plugins combined together from all sources
163 */
164 plugins: ProsemirrorPlugin[];
165 /**
166 * Retrieve the state for a given extension name. This will throw an error
167 * if the extension doesn't exist.
168 *
169 * @param name - the name of the extension
170 */
171 getPluginState: <State>(name: GetNameUnion<Extension>) => State;
172 /**
173 * All the plugin keys available to be used by plugins.
174 */
175 pluginKeys: Record<string, PluginKey>;
176 }
177 interface ExcludeOptions {
178 /**
179 * Whether to exclude the extension's plugin
180 *
181 * @default undefined
182 */
183 plugins?: boolean;
184 }
185 interface BaseExtension {
186 /**
187 * The plugin key for custom plugin created by this extension. This only
188 * exists when there is a valid `createPlugin` method on the extension.
189 *
190 * This can be used to set and retrieve metadata.
191 *
192 * ```ts
193 * const meta = tr.getMeta(this.pluginKey);
194 * ```
195 */
196 pluginKey: PluginKey;
197 /**
198 * The plugin that was created by the `createPlugin` method. This only
199 * exists for extension which implement that method.
200 */
201 plugin: Plugin;
202 /**
203 * The external plugins created by the `createExternalPlugins` method.
204 */
205 externalPlugins: Plugin[];
206 /**
207 * Retrieve the state of the custom plugin for this extension. This will
208 * throw an error if the extension doesn't have a valid `createPlugin`
209 * method.
210 *
211 * @remarks
212 *
213 * ```ts
214 * const pluginState = this.getPluginState();
215 * ```
216 *
217 * This is only available after the initialize stage of the editor manager
218 * lifecycle.
219 *
220 * If you would like to use it before that e.g. in the decorations prop of
221 * the `createPlugin` method, you can call it with a current state which
222 * will be used to retrieve the plugin state.
223 *
224 * Please note that when using this in the decorations callback it is
225 * advisable to pass in the `state` argument in case the callback is
226 * called before the framework, or the view have been initialized.
227 */
228 getPluginState: <State>(state?: EditorState) => State;
229 /**
230 * Create a custom plugin directly in the editor.
231 *
232 * @remarks
233 *
234 * A unique `key` is automatically applied to enable easier retrieval of
235 * the plugin state.
236 *
237 * ```ts
238 * import { CreateExtensionPlugin } from 'remirror';
239 *
240 * class MyExtension extends PlainExtension {
241 * get name() {
242 * return 'me' as const;
243 * }
244 *
245 * createPlugin(): CreateExtensionPlugin {
246 * return {
247 * props: {
248 * handleKeyDown: keydownHandler({
249 * Backspace: handler,
250 * 'Mod-Backspace': handler,
251 * Delete: handler,
252 * 'Mod-Delete': handler,
253 * 'Ctrl-h': handler,
254 * 'Alt-Backspace': handler,
255 * 'Ctrl-d': handler,
256 * 'Ctrl-Alt-Backspace': handler,
257 * 'Alt-Delete': handler,
258 * 'Alt-d': handler,
259 * }),
260 * decorations: state => {
261 * const pluginState = this.getPluginState(state);
262 * pluginState.setDeleted(false);
263 * return pluginState.decorationSet;
264 * },
265 * },
266 * }
267 * }
268 * }
269 * ```
270 */
271 createPlugin?(): CreateExtensionPlugin;
272 /**
273 * Register third party plugins when this extension is placed into the
274 * editor.
275 *
276 * @remarks
277 *
278 * Some plugins (like the table plugin) consume several different plugins,
279 * creator method allows you to return a list of plugins you'd like to
280 * support.
281 */
282 createExternalPlugins?(): ProsemirrorPlugin[];
283 }
284 interface AllExtensions {
285 plugins: PluginsExtension;
286 }
287 }
288}