UNPKG

9.07 kBTypeScriptView Raw
1import type { ExtensionPriority } from '@remirror/core-constants';
2import type { AnyFunction, CommandFunction, KeyBindingCommandFunction, Listable, LiteralUnion, NonChainableCommandFunction, ProsemirrorAttributes, Shape } from '@remirror/core-types';
3import type { I18n } from '@remirror/i18n';
4import type { CoreIcon } from '@remirror/icons';
5import type { AnyExtension, HelperAnnotation } from '../extension';
6import type { GetOptions, TypedPropertyDescriptor } from '../types';
7/**
8 * A decorator which can be applied to top level methods on an extension to
9 * identify them as helpers. This can be used as a replacement for the
10 * `createHelpers` method.
11 *
12 * To allow the TypeScript compiler to automatically infer types, please create
13 * your methods with the following type signature.
14 *
15 * ```ts
16 * import { CommandFunction } from '@remirror/core';
17 *
18 * type Signature = (...args: any[]) => CommandFunction;
19 * ```
20 *
21 * The following is an example of how this can be used within your extension.
22 *
23 * ```ts
24 * import { helper, Helper } from '@remirror/core';
25 *
26 * class MyExtension {
27 * get name() {
28 * return 'my';
29 * }
30 *
31 * @helper()
32 * alwaysTrue(): Helper<boolean> {
33 * return true;
34 * }
35 * }
36 * ```
37 *
38 * The above helper can now be used within your editor instance.
39 *
40 * ```tsx
41 * import { useRemirrorContext } from '@remirror/react';
42 *
43 * const MyEditorButton = () => {
44 * const { helpers } = useRemirrorContext();
45 *
46 * return helpers.alwaysTrue() ? <button>My Button</button> : null
47 * }
48 * ```
49 *
50 * @category Method Decorator
51 */
52export declare function helper(options?: HelperDecoratorOptions): <Extension extends AnyExtension, Type>(target: Extension, propertyKey: string, _descriptor: TypedPropertyDescriptor<AnyFunction<NonNullable<Type> extends HelperAnnotation ? Type : never>>) => void;
53/**
54 * A decorator which can be applied to top level methods on an extension to
55 * identify them as commands. This can be used as a replacement for the
56 * `createCommands` method.
57 *
58 * If you prefer not to use decorators, then you can continue using
59 * `createCommands`. Internally the decorators are being used as they are better
60 * for documentation purposes.
61 *
62 * For automated type inference methods that use this decorator must implement
63 * the following type signature.
64 *
65 * ```ts
66 * import { CommandFunction } from '@remirror/core';
67 *
68 * type Signature = (...args: any[]) => CommandFunction;
69 * ```
70 *
71 * The following is an example of how this can be used within your extension.
72 *
73 * ```ts
74 * import { command, CommandFunction } from '@remirror/core';
75 *
76 * class MyExtension {
77 * get name() {
78 * return 'my';
79 * }
80 *
81 * @command() myCommand(text: string): CommandFunction {return ({ tr, dispatch
82 * }) => {dispatch?.(tr.insertText('my command ' + text)); return true;
83 * }
84 * }
85 * }
86 * ```
87 *
88 * The above command can now be used within your editor instance.
89 *
90 * ```tsx
91 * import { useRemirrorContext } from '@remirror/react';
92 *
93 * const MyEditorButton = () => {
94 * const { commands } = useRemirrorContext();
95 *
96 * return <button onClick={() => commands.myCommand('hello')}>My Button</button>
97 * }
98 * ```
99 *
100 * @category Method Decorator
101 */
102export declare function command<Extension extends AnyExtension>(options?: ChainableCommandDecoratorOptions<Required<GetOptions<Extension>>>): ExtensionDecorator<Extension, CommandFunction, void>;
103export declare function command<Extension extends AnyExtension>(options: NonChainableCommandDecoratorOptions<Required<GetOptions<Extension>>>): ExtensionDecorator<Extension, NonChainableCommandFunction, void>;
104/**
105 * A decorator which can be applied to an extension method to
106 * identify as a key binding method. This can be used as a replacement for
107 * the `createKeymap` method depending on your preference.
108 *
109 * If you prefer not to use decorators, then you can continue using
110 * `createKeymap`.
111 *
112 * @category Method Decorator
113 */
114export declare function keyBinding<Extension extends AnyExtension>(options: KeybindingDecoratorOptions<Required<GetOptions<Extension>>>): (target: Extension, propertyKey: string, _descriptor: TypedPropertyDescriptor<KeyBindingCommandFunction>) => void;
115export interface HelperDecoratorOptions {
116}
117export declare type KeyboardShortcutFunction<Options extends Shape = Shape> = (options: Options, store: Remirror.ExtensionStore) => KeyboardShortcut;
118export declare type KeyboardShortcutValue = Listable<LiteralUnion<'Enter' | 'ArrowDown' | 'ArrowUp' | 'ArrowLeft' | 'ArrowRight' | 'Escape' | 'Delete' | 'Backspace', string>>;
119export declare type KeyboardShortcut = KeyboardShortcutValue | KeyboardShortcutFunction;
120export interface KeybindingDecoratorOptions<Options extends Shape = Shape> {
121 /**
122 * The keypress sequence to intercept.
123 *
124 * - `Enter`
125 * - `Shift-Enter`
126 */
127 shortcut: KeyboardShortcut;
128 /**
129 * This can be used to set a keybinding as inactive based on the provided
130 * options.
131 */
132 isActive?: (options: Options, store: Remirror.ExtensionStore) => boolean;
133 /**
134 * The priority for this keybinding.
135 */
136 priority?: ExtensionPriority | ((options: Options, store: Remirror.ExtensionStore) => ExtensionPriority);
137 /**
138 * The name of the command that the keybinding should be attached to.
139 */
140 command?: Remirror.AllUiCommandNames;
141}
142declare type ExtensionDecorator<Extension extends AnyExtension, Fn, Return> = (target: Extension, propertyKey: string, _descriptor: TypedPropertyDescriptor<AnyFunction<Fn>>) => Return;
143export interface CommandUiIcon {
144 /**
145 * The icon name.
146 */
147 name: CoreIcon;
148 /**
149 * Text placed in a superscript position. For `ltr` this is in the top right
150 * hand corner of the icon.
151 */
152 sup?: string;
153 /**
154 * Text placed in a subscript position. For `ltr` this is in the bottom right
155 * hand corner.
156 */
157 sub?: string;
158}
159export declare type CommandDecoratorShortcut = string | {
160 shortcut: string;
161 attrs: ProsemirrorAttributes;
162} | string[] | Array<{
163 shortcut: string;
164 attrs: ProsemirrorAttributes;
165}>;
166export interface CommandUiDecoratorOptions {
167 /**
168 * The default command icon to use if this has a UI representation.
169 */
170 icon?: CommandDecoratorValue<CoreIcon | CommandUiIcon>;
171 /**
172 * A label for the command with support for i18n. This makes use of
173 * `babel-plugin-macros` to generate the message.
174 */
175 label?: CommandDecoratorMessage;
176 /**
177 * An i18n compatible description which can be used to provide extra context
178 * for the command.
179 */
180 description?: CommandDecoratorMessage;
181 /**
182 * A keyboard shortcut which can be used to run the specified command.
183 *
184 * Rather than defining this here, you should create a decorated `keyBinding`
185 * and set the `command` name option. This way the shortcut will dynamically
186 * be added at runtime.
187 */
188 shortcut?: CommandDecoratorShortcut;
189}
190export interface CommandDecoratorMessageProps {
191 /**
192 * True when the command is enabled.
193 */
194 enabled: boolean;
195 /**
196 * True when the extension is active.
197 */
198 active: boolean;
199 /**
200 * Predefined attributes which can influence the returned value.
201 */
202 attrs: ProsemirrorAttributes | undefined;
203 /**
204 * A translation utility for translating a predefined string / or message
205 * descriptor.
206 */
207 t: I18n['_'];
208}
209/**
210 * @template Value - the value which should be returned from the function or
211 * used directly.
212 */
213export declare type CommandDecoratorValue<Value> = ((props: CommandDecoratorMessageProps) => Value) | Value;
214export declare type CommandDecoratorMessage = CommandDecoratorValue<string>;
215interface ChainableCommandDecoratorOptions<Options extends Shape> extends Remirror.CommandDecoratorOptions<Options> {
216 /**
217 * Set this to `true` to disable chaining of this command. This means it will
218 * no longer be available when running `
219 *
220 * @default false
221 */
222 disableChaining?: false;
223}
224interface NonChainableCommandDecoratorOptions<Options extends Shape> extends Remirror.CommandDecoratorOptions<Options> {
225 /**
226 * Set this to `true` to disable chaining of this command. This means it will
227 * no longer be available when running `
228 *
229 * @default false
230 */
231 disableChaining: true;
232}
233export declare type CommandDecoratorOptions<Options extends Shape = Shape> = ChainableCommandDecoratorOptions<Options> | NonChainableCommandDecoratorOptions<Options>;
234declare global {
235 namespace Remirror {
236 /**
237 * UX options for the command which can be extended.
238 */
239 interface CommandDecoratorOptions<Options extends Shape = Shape> extends CommandUiDecoratorOptions {
240 /**
241 * A function which can be used to override whether a command is already
242 * active for the current selection.
243 */
244 active?: (options: Options, store: ExtensionStore) => boolean;
245 }
246 }
247}
248export {};