UNPKG

7.65 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 * @defaultValue '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 * @defaultValue true
19 */
20 undoInputRuleOnBackspace?: boolean;
21 /**
22 * Determines whether the escape key selects the current node.
23 *
24 * @defaultValue 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 * @defaultValue 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 * The underlying keydown handler.
97 */
98 private keydownHandler;
99 /**
100 * Get the shortcut map.
101 */
102 private get shortcutMap();
103 /**
104 * This adds the `createKeymap` method functionality to all extensions.
105 */
106 onCreate(): void;
107 /** Add the created keymap to the available plugins. */
108 createExternalPlugins(): ProsemirrorPlugin[];
109 private setupKeydownHandler;
110 /**
111 * Updates the stored keymap bindings on this extension.
112 */
113 private generateKeymapBindings;
114 /**
115 * Handle exiting the mark forwards.
116 */
117 arrowRightShortcut(props: KeyBindingProps): boolean;
118 /**
119 * Handle the arrow left key to exit the mark.
120 */
121 arrowLeftShortcut(props: KeyBindingProps): boolean;
122 /**
123 * Handle exiting the mark forwards.
124 */
125 backspace(props: KeyBindingProps): boolean;
126 /**
127 * Create the base keymap and give it a low priority so that all other keymaps
128 * override it.
129 */
130 createKeymap(): PrioritizedKeyBindings;
131 /**
132 * Get the real shortcut name from the named shortcut.
133 */
134 getNamedShortcut(shortcut: string, options?: Shape): Helper<string[]>;
135 /**
136 * @internalremarks
137 *
138 * Think about the case where bindings are disposed of and then added in a
139 * different position in the `extraKeyBindings` array. This is especially
140 * pertinent when using hooks.
141 */
142 protected onAddCustomHandler: AddCustomHandler<KeymapOptions>;
143 /**
144 * Handle changes in the dynamic properties.
145 */
146 protected onSetOptions(props: OnSetOptionsProps<KeymapOptions>): void;
147 private sortKeymaps;
148 /**
149 * The method for rebuilding all the extension keymaps.
150 *
151 * 1. Rebuild keymaps.
152 * 2. Replace `this.keydownHandler` with the new keydown handler.
153 */
154 private readonly rebuildKeymap;
155 /**
156 * Exits the mark forwards when at the end of a block node.
157 */
158 private exitMarkForwards;
159 private exitNodeBackwards;
160 /**
161 * Exit a mark when at the beginning of a block node.
162 */
163 private exitMarkBackwards;
164}
165/**
166 * A shortcut map which is used by the `KeymapExtension`.
167 */
168export type ShortcutMap = Record<NamedShortcut, string>;
169/**
170 * The default named shortcuts used within `remirror`.
171 */
172export declare const DEFAULT_SHORTCUTS: ShortcutMap;
173/**
174 * Shortcuts used within google docs.
175 */
176export declare const GOOGLE_DOC_SHORTCUTS: ShortcutMap;
177export declare const keyboardShortcuts: {
178 default: ShortcutMap;
179 googleDoc: ShortcutMap;
180};
181export type KeyboardShortcuts = keyof typeof keyboardShortcuts | ShortcutMap;
182/**
183 * KeyBindings as a tuple with priority and the keymap.
184 */
185export type KeyBindingsTuple = [priority: ExtensionPriority, bindings: KeyBindings];
186/**
187 * `KeyBindings` as an object or prioritized tuple.
188 */
189export type PrioritizedKeyBindings = KeyBindings | KeyBindingsTuple;
190declare global {
191 namespace Remirror {
192 interface ExcludeOptions {
193 /**
194 * Whether to exclude keybindings support. This is not a recommended
195 * action and can break functionality.
196 *
197 * @defaultValue undefined
198 */
199 keymap?: boolean;
200 }
201 interface ExtensionStore {
202 /**
203 * When called this will run through every `createKeymap` method on every
204 * extension to recreate the keyboard bindings.
205 *
206 * @remarks
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