UNPKG

7.81 kBTypeScriptView Raw
1import { ExtensionPriority, NamedShortcut } from '@remirror/core-constants';
2import type { CustomHandler, KeyBindingProps, KeyBindings, ProsemirrorPlugin, Shape } from '@remirror/core-types';
3import { Helper, PlainExtension } from '../extension';
4import type { AddCustomHandler } from '../extension/base-class';
5import type { OnSetOptionsProps } from '../types';
6import { KeybindingDecoratorOptions } from './builtin-decorators';
7export interface KeymapOptions {
8 /**
9 * The shortcuts to use for named keybindings in the editor.
10 *
11 * @default 'default'
12 */
13 shortcuts?: KeyboardShortcuts;
14 /**
15 * Determines whether a backspace after an input rule has been applied should
16 * reverse the effect of the input rule.
17 *
18 * @default true
19 */
20 undoInputRuleOnBackspace?: boolean;
21 /**
22 * Determines whether the escape key selects the current node.
23 *
24 * @default false
25 */
26 selectParentNodeOnEscape?: boolean;
27 /**
28 * When true will exclude the default prosemirror keymap.
29 *
30 * @remarks
31 *
32 * You might want to set this to true if you want to fully customise the
33 * keyboard mappings for your editor. Otherwise it is advisable to leave it
34 * unchanged.
35 *
36 * @default false
37 */
38 excludeBaseKeymap?: boolean;
39 /**
40 * Whether to support exiting marks when the left and right array keys are
41 * pressed.
42 *
43 * Can be set to
44 *
45 * - `true` - enables exits from both the entrance and the end of the mark
46 */
47 exitMarksOnArrowPress?: boolean;
48 /**
49 * The implementation for the extra keybindings added to the settings.
50 *
51 * @remarks
52 *
53 * This allows for you to add extra key mappings which will be checked before
54 * the default keymaps, if they return false then the default keymaps are
55 * still checked.
56 *
57 * No key mappings are removed in this process.
58 *
59 * ```ts
60 * const extension = BaseKeymapExtension.create({ keymap: {
61 * Enter({ state, dispatch }) {
62 * //... Logic
63 * return true;
64 * },
65 * }});
66 * ```
67 */
68 keymap?: CustomHandler<PrioritizedKeyBindings>;
69}
70/**
71 * This extension allows others extension to use the `createKeymaps` method.
72 *
73 * @remarks
74 *
75 * Keymaps are the way of controlling how the editor responds to a keypress and
76 * different key combinations.
77 *
78 * Without this extension most of the shortcuts and behaviors we have come to
79 * expect from text editors would not be provided.
80 *
81 * @category Builtin Extension
82 */
83export declare class KeymapExtension extends PlainExtension<KeymapOptions> {
84 get name(): "keymap";
85 /**
86 * The custom keybindings added by the handlers. In react these can be added
87 * via `hooks`.
88 */
89 private extraKeyBindings;
90 /**
91 * Track the backward exits from a mark to allow double tapping the left arrow
92 * to move to the previous block node.
93 */
94 private readonly backwardMarkExitTracker;
95 /**
96 * Get the shortcut map.
97 */
98 private get shortcutMap();
99 /**
100 * This adds the `createKeymap` method functionality to all extensions.
101 */
102 onCreate(): void;
103 /** Add the created keymap to the available plugins. */
104 createExternalPlugins(): ProsemirrorPlugin[];
105 /**
106 * Updates the stored keymap plugin on this extension.
107 */
108 private generateKeymap;
109 /**
110 * Handle exiting the mark forwards.
111 */
112 arrowRightShortcut(props: KeyBindingProps): boolean;
113 /**
114 * Handle the arrow left key to exit the mark.
115 */
116 arrowLeftShortcut(props: KeyBindingProps): boolean;
117 /**
118 * Handle exiting the mark forwards.
119 */
120 backspace(props: KeyBindingProps): boolean;
121 /**
122 * Create the base keymap and give it a low priority so that all other keymaps
123 * override it.
124 */
125 createKeymap(): PrioritizedKeyBindings;
126 /**
127 * Get the real shortcut name from the named shortcut.
128 */
129 getNamedShortcut(shortcut: string, options?: Shape): Helper<string[]>;
130 /**
131 * @internalremarks
132 *
133 * Think about the case where bindings are disposed of and then added in a
134 * different position in the `extraKeyBindings` array. This is especially
135 * pertinent when using hooks.
136 */
137 protected onAddCustomHandler: AddCustomHandler<KeymapOptions>;
138 /**
139 * Handle changes in the dynamic properties.
140 */
141 protected onSetOptions(props: OnSetOptionsProps<KeymapOptions>): void;
142 private sortKeymaps;
143 /**
144 * The method for rebuilding all the extension keymaps.
145 *
146 * 1. Rebuild keymaps.
147 * 2. Replace the old keymap plugin.
148 * 3. Update the plugins used in the state (triggers an editor update).
149 */
150 private readonly rebuildKeymap;
151 /**
152 * Exits the mark forwards when at the end of a block node.
153 */
154 private exitMarkForwards;
155 private exitNodeBackwards;
156 /**
157 * Exit a mark when at the beginning of a block node.
158 */
159 private exitMarkBackwards;
160}
161/**
162 * A shortcut map which is used by the `KeymapExtension`.
163 */
164export declare type ShortcutMap = Record<NamedShortcut, string>;
165/**
166 * The default named shortcuts used within `remirror`.
167 */
168export declare const DEFAULT_SHORTCUTS: ShortcutMap;
169/**
170 * Shortcuts used within google docs.
171 */
172export declare const GOOGLE_DOC_SHORTCUTS: ShortcutMap;
173export declare const keyboardShortcuts: {
174 default: ShortcutMap;
175 googleDoc: ShortcutMap;
176};
177export declare type KeyboardShortcuts = keyof typeof keyboardShortcuts | ShortcutMap;
178/**
179 * KeyBindings as a tuple with priority and the keymap.
180 */
181export declare type KeyBindingsTuple = [priority: ExtensionPriority, bindings: KeyBindings];
182/**
183 * `KeyBindings` as an object or prioritized tuple.
184 */
185export declare type PrioritizedKeyBindings = KeyBindings | KeyBindingsTuple;
186declare global {
187 namespace Remirror {
188 interface ExcludeOptions {
189 /**
190 * Whether to exclude keybindings support. This is not a recommended
191 * action and can break functionality.
192 *
193 * @default undefined
194 */
195 keymap?: boolean;
196 }
197 interface ExtensionStore {
198 /**
199 * When called this will run through every `createKeymap` method on every
200 * extension to recreate the keyboard bindings.
201 *
202 * @remarks
203 *
204 * Under the hood it updates the plugin which is used to insert the
205 * keybindings into the editor. This causes the state to be updated and
206 * will cause a rerender in your ui framework.
207 *
208 * **NOTE** - This will not update keybinding for extensions that
209 * implement their own keybinding functionality (e.g. any plugin using
210 * Suggestions)
211 */
212 rebuildKeymap: () => void;
213 }
214 interface BaseExtension {
215 /**
216 * Stores all the keybinding names and options for this decoration that
217 * have been added as decorators to the extension instance. This is used
218 * by the `KeymapExtension` to pick the commands and store metadata
219 * attached to each command.
220 *
221 * @internal
222 */
223 decoratedKeybindings?: Record<string, KeybindingDecoratorOptions>;
224 /**
225 * Add keymap bindings for this extension.
226 *
227 * @param parameter - schema parameter with type included
228 */
229 createKeymap?(extractShortcutNames: (shortcut: string) => string[]): PrioritizedKeyBindings;
230 }
231 interface AllExtensions {
232 keymap: KeymapExtension;
233 }
234 }
235}
236
\No newline at end of file