1 | // Type definitions for prosemirror-commands 1.0
|
2 | // Project: https://github.com/ProseMirror/prosemirror-commands
|
3 | // Definitions by: Bradley Ayers <https://github.com/bradleyayers>
|
4 | // David Hahn <https://github.com/davidka>
|
5 | // Tim Baumann <https://github.com/timjb>
|
6 | // Patrick Simmelbauer <https://github.com/patsimm>
|
7 | // Mike Morearty <https://github.com/mmorearty>
|
8 | // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
9 | // TypeScript Version: 3.0
|
10 |
|
11 | import { MarkType, Node as ProsemirrorNode, NodeType, Schema } from 'prosemirror-model';
|
12 | import { EditorState, Transaction } from 'prosemirror-state';
|
13 | import { EditorView } from 'prosemirror-view';
|
14 |
|
15 | /**
|
16 | * A command function takes an editor state, *optionally* a `dispatch`
|
17 | * function that it can use to dispatch a transaction and optionally
|
18 | * an `EditorView` instance. It should return a boolean that indicates
|
19 | * whether it could perform any action. When no `dispatch` callback is
|
20 | * passed, the command should do a 'dry run', determining whether it is
|
21 | * applicable, but not actually doing anything.
|
22 | */
|
23 | export interface Command<S extends Schema = any> {
|
24 | (state: EditorState<S>, dispatch?: (tr: Transaction<S>) => void, view?: EditorView<S>): boolean;
|
25 | }
|
26 |
|
27 | export interface Keymap<S extends Schema = any> {
|
28 | [key: string]: Command<S>;
|
29 | }
|
30 |
|
31 | /**
|
32 | * Delete the selection, if there is one.
|
33 | */
|
34 | export function deleteSelection<S extends Schema = any>(
|
35 | state: EditorState<S>,
|
36 | dispatch?: (tr: Transaction<S>) => void,
|
37 | ): boolean;
|
38 | /**
|
39 | * If the selection is empty and at the start of a textblock, try to
|
40 | * reduce the distance between that block and the one before it—if
|
41 | * there's a block directly before it that can be joined, join them.
|
42 | * If not, try to move the selected block closer to the next one in
|
43 | * the document structure by lifting it out of its parent or moving it
|
44 | * into a parent of the previous block. Will use the view for accurate
|
45 | * (bidi-aware) start-of-textblock detection if given.
|
46 | */
|
47 | export function joinBackward<S extends Schema = any>(
|
48 | state: EditorState<S>,
|
49 | dispatch?: (tr: Transaction<S>) => void,
|
50 | view?: EditorView<S>,
|
51 | ): boolean;
|
52 | /**
|
53 | * When the selection is empty and at the start of a textblock, select
|
54 | * the node before that textblock, if possible. This is intended to be
|
55 | * bound to keys like backspace, after
|
56 | * [`joinBackward`](#commands.joinBackward) or other deleting
|
57 | * commands, as a fall-back behavior when the schema doesn't allow
|
58 | * deletion at the selected point.
|
59 | */
|
60 | export function selectNodeBackward<S extends Schema = any>(
|
61 | state: EditorState<S>,
|
62 | dispatch?: (tr: Transaction<S>) => void,
|
63 | view?: EditorView<S>,
|
64 | ): boolean;
|
65 | /**
|
66 | * If the selection is empty and the cursor is at the end of a
|
67 | * textblock, try to reduce or remove the boundary between that block
|
68 | * and the one after it, either by joining them or by moving the other
|
69 | * block closer to this one in the tree structure. Will use the view
|
70 | * for accurate start-of-textblock detection if given.
|
71 | */
|
72 | export function joinForward<S extends Schema = any>(
|
73 | state: EditorState<S>,
|
74 | dispatch?: (tr: Transaction<S>) => void,
|
75 | view?: EditorView<S>,
|
76 | ): boolean;
|
77 | /**
|
78 | * When the selection is empty and at the end of a textblock, select
|
79 | * the node coming after that textblock, if possible. This is intended
|
80 | * to be bound to keys like delete, after
|
81 | * [`joinForward`](#commands.joinForward) and similar deleting
|
82 | * commands, to provide a fall-back behavior when the schema doesn't
|
83 | * allow deletion at the selected point.
|
84 | */
|
85 | export function selectNodeForward<S extends Schema = any>(
|
86 | state: EditorState<S>,
|
87 | dispatch?: (tr: Transaction<S>) => void,
|
88 | view?: EditorView<S>,
|
89 | ): boolean;
|
90 | /**
|
91 | * Join the selected block or, if there is a text selection, the
|
92 | * closest ancestor block of the selection that can be joined, with
|
93 | * the sibling above it.
|
94 | */
|
95 | export function joinUp<S extends Schema = any>(state: EditorState<S>, dispatch?: (tr: Transaction<S>) => void): boolean;
|
96 | /**
|
97 | * Join the selected block, or the closest ancestor of the selection
|
98 | * that can be joined, with the sibling after it.
|
99 | */
|
100 | export function joinDown<S extends Schema = any>(
|
101 | state: EditorState<S>,
|
102 | dispatch?: (tr: Transaction<S>) => void,
|
103 | ): boolean;
|
104 | /**
|
105 | * Lift the selected block, or the closest ancestor block of the
|
106 | * selection that can be lifted, out of its parent node.
|
107 | */
|
108 | export function lift<S extends Schema = any>(state: EditorState<S>, dispatch?: (tr: Transaction<S>) => void): boolean;
|
109 | /**
|
110 | * If the selection is in a node whose type has a truthy
|
111 | * [`code`](#model.NodeSpec.code) property in its spec, replace the
|
112 | * selection with a newline character.
|
113 | */
|
114 | export function newlineInCode<S extends Schema = any>(
|
115 | state: EditorState<S>,
|
116 | dispatch?: (tr: Transaction<S>) => void,
|
117 | ): boolean;
|
118 | /**
|
119 | * When the selection is in a node with a truthy
|
120 | * [`code`](#model.NodeSpec.code) property in its spec, create a
|
121 | * default block after the code block, and move the cursor there.
|
122 | */
|
123 | export function exitCode<S extends Schema = any>(
|
124 | state: EditorState<S>,
|
125 | dispatch?: (tr: Transaction<S>) => void,
|
126 | ): boolean;
|
127 | /**
|
128 | * If a block node is selected, create an empty paragraph before (if
|
129 | * it is its parent's first child) or after it.
|
130 | */
|
131 | export function createParagraphNear<S extends Schema = any>(
|
132 | state: EditorState<S>,
|
133 | dispatch?: (tr: Transaction<S>) => void,
|
134 | ): boolean;
|
135 | /**
|
136 | * If the cursor is in an empty textblock that can be lifted, lift the
|
137 | * block.
|
138 | */
|
139 | export function liftEmptyBlock<S extends Schema = any>(
|
140 | state: EditorState<S>,
|
141 | dispatch?: (tr: Transaction<S>) => void,
|
142 | ): boolean;
|
143 | /**
|
144 | * Split the parent block of the selection. If the selection is a text
|
145 | * selection, also delete its content.
|
146 | */
|
147 | export function splitBlock<S extends Schema = any>(
|
148 | state: EditorState<S>,
|
149 | dispatch?: (tr: Transaction<S>) => void,
|
150 | ): boolean;
|
151 | /**
|
152 | * Acts like [`splitBlock`](#commands.splitBlock), but without
|
153 | * resetting the set of active marks at the cursor.
|
154 | */
|
155 | export function splitBlockKeepMarks<S extends Schema = any>(
|
156 | state: EditorState<S>,
|
157 | dispatch?: (tr: Transaction<S>) => void,
|
158 | ): boolean;
|
159 | /**
|
160 | * Move the selection to the node wrapping the current selection, if
|
161 | * any. (Will not select the document node.)
|
162 | */
|
163 | export function selectParentNode<S extends Schema = any>(
|
164 | state: EditorState<S>,
|
165 | dispatch?: (tr: Transaction<S>) => void,
|
166 | ): boolean;
|
167 | /**
|
168 | * Select the whole document.
|
169 | */
|
170 | export function selectAll<S extends Schema = any>(
|
171 | state: EditorState<S>,
|
172 | dispatch?: (tr: Transaction<S>) => void,
|
173 | ): boolean;
|
174 | /**
|
175 | * Wrap the selection in a node of the given type with the given
|
176 | * attributes.
|
177 | */
|
178 | export function wrapIn<S extends Schema = any>(
|
179 | nodeType: NodeType<S>,
|
180 | attrs?: { [key: string]: any },
|
181 | ): (state: EditorState<S>, dispatch?: (tr: Transaction<S>) => void) => boolean;
|
182 | /**
|
183 | * Returns a command that tries to set the textblock around the
|
184 | * selection to the given node type with the given attributes.
|
185 | */
|
186 | export function setBlockType<S extends Schema = any>(
|
187 | nodeType: NodeType<S>,
|
188 | attrs?: { [key: string]: any },
|
189 | ): (state: EditorState<S>, dispatch?: (tr: Transaction<S>) => void) => boolean;
|
190 | /**
|
191 | * Create a command function that toggles the given mark with the
|
192 | * given attributes. Will return `false` when the current selection
|
193 | * doesn't support that mark. This will remove the mark if any marks
|
194 | * of that type exist in the selection, or add it otherwise. If the
|
195 | * selection is empty, this applies to the [stored
|
196 | * marks](#state.EditorState.storedMarks) instead of a range of the
|
197 | * document.
|
198 | */
|
199 | export function toggleMark<S extends Schema = any>(
|
200 | markType: MarkType<S>,
|
201 | attrs?: { [key: string]: any },
|
202 | ): (state: EditorState<S>, dispatch?: (tr: Transaction<S>) => void) => boolean;
|
203 | /**
|
204 | * Wrap a command so that, when it produces a transform that causes
|
205 | * two joinable nodes to end up next to each other, those are joined.
|
206 | * Nodes are considered joinable when they are of the same type and
|
207 | * when the `isJoinable` predicate returns true for them or, if an
|
208 | * array of strings was passed, if their node type name is in that
|
209 | * array.
|
210 | */
|
211 | export function autoJoin<S extends Schema = any>(
|
212 | command: (state: EditorState<S>, p1?: (tr: Transaction<S>) => void) => boolean,
|
213 | isJoinable: ((before: ProsemirrorNode<S>, after: ProsemirrorNode<S>) => boolean) | string[],
|
214 | ): (state: EditorState<S>, p1?: (tr: Transaction<S>) => void) => boolean;
|
215 | /**
|
216 | * Combine a number of command functions into a single function (which
|
217 | * calls them one by one until one returns true).
|
218 | */
|
219 | export function chainCommands<S extends Schema = any>(...commands: Array<Command<S>>): Command<S>;
|
220 | /**
|
221 | * A basic keymap containing bindings not specific to any schema.
|
222 | * Binds the following keys (when multiple commands are listed, they
|
223 | * are chained with [`chainCommands`](#commands.chainCommands)):
|
224 | *
|
225 | * * **Enter** to `newlineInCode`, `createParagraphNear`, `liftEmptyBlock`, `splitBlock`
|
226 | * * **Mod-Enter** to `exitCode`
|
227 | * * **Backspace** and **Mod-Backspace** to `deleteSelection`, `joinBackward`, `selectNodeBackward`
|
228 | * * **Delete** and **Mod-Delete** to `deleteSelection`, `joinForward`, `selectNodeForward`
|
229 | * * **Mod-Delete** to `deleteSelection`, `joinForward`, `selectNodeForward`
|
230 | * * **Mod-a** to `selectAll`
|
231 | */
|
232 | export let pcBaseKeymap: Keymap;
|
233 | /**
|
234 | * A copy of `pcBaseKeymap` that also binds **Ctrl-h** like Backspace,
|
235 | * **Ctrl-d** like Delete, **Alt-Backspace** like Ctrl-Backspace, and
|
236 | * **Ctrl-Alt-Backspace**, **Alt-Delete**, and **Alt-d** like
|
237 | * Ctrl-Delete.
|
238 | */
|
239 | export let macBaseKeymap: Keymap;
|
240 | /**
|
241 | * Depending on the detected platform, this will hold
|
242 | * [`pcBasekeymap`](#commands.pcBaseKeymap) or
|
243 | * [`macBaseKeymap`](#commands.macBaseKeymap).
|
244 | */
|
245 | export let baseKeymap: Keymap;
|