prosemirror-commands: Fix Command type (#45972)

The 'dispatch' and 'view' arguments must be optional to support
'dry runs'.

Also add doc comment for 'Command' type by copying relevant the part
from the ProseMirror documentation.

Also simplify type definition of 'chainCommands' using 'Command'.
This commit is contained in:
Tim Baumann 2020-07-10 15:38:30 +02:00 committed by GitHub
parent 2bcb0b9260
commit de475b6d82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 9 deletions

View File

@ -12,11 +12,19 @@ import { MarkType, Node as ProsemirrorNode, NodeType, Schema } from 'prosemirror
import { EditorState, Transaction } from 'prosemirror-state';
import { EditorView } from 'prosemirror-view';
/**
* A command function takes an editor state, *optionally* a `dispatch`
* function that it can use to dispatch a transaction and optionally
* an `EditorView` instance. It should return a boolean that indicates
* whether it could perform any action. When no `dispatch` callback is
* passed, the command should do a 'dry run', determining whether it is
* applicable, but not actually doing anything.
*/
export interface Command<S extends Schema = any> {
(
state: EditorState<S>,
dispatch: (tr: Transaction<S>) => void,
view: EditorView<S>
dispatch?: (tr: Transaction<S>) => void,
view?: EditorView<S>
): boolean;
}
@ -218,11 +226,7 @@ export function autoJoin<S extends Schema = any>(
* Combine a number of command functions into a single function (which
* calls them one by one until one returns true).
*/
export function chainCommands<S extends Schema = any>(
...commands: Array<
(p1: EditorState<S>, p2?: (tr: Transaction<S>) => void, p3?: EditorView<S>) => boolean
>
): (p1: EditorState<S>, p2?: (tr: Transaction<S>) => void, p3?: EditorView<S>) => boolean;
export function chainCommands<S extends Schema = any>(...commands: Array<Command<S>>): Command<S>;
/**
* A basic keymap containing bindings not specific to any schema.
* Binds the following keys (when multiple commands are listed, they

View File

@ -19,3 +19,7 @@ const keymap: commands.Keymap = {
ArrowLeft: commands.joinBackward, // takes three args
ArrowRight: (state, dispatch, view) => true, // arg types inferred
};
Object.keys(commands.baseKeymap).forEach(key => {
keymap[key] = keymap[key] ? commands.chainCommands(keymap[key], commands.baseKeymap[key]) : commands.baseKeymap[key];
});

View File

@ -4,7 +4,9 @@ import { Plugin } from 'prosemirror-state';
const plugin1: Plugin = keymap.keymap({
// Test that the argument types are correctly inferred
Enter: (state, dispatch, view) => {
dispatch(state.tr.insertText("hello"));
if (dispatch) {
dispatch(state.tr.insertText("hello"));
}
return true;
},
});
@ -14,7 +16,9 @@ const plugin2 = new Plugin({
handleKeyDown: keymap.keydownHandler({
// Test that the argument types are correctly inferred
Enter: (state, dispatch, view) => {
dispatch(state.tr.insertText("hello"));
if (dispatch) {
dispatch(state.tr.insertText("hello"));
}
return true;
}
}),