mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 10:56:53 +00:00
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:
parent
2bcb0b9260
commit
de475b6d82
18
types/prosemirror-commands/index.d.ts
vendored
18
types/prosemirror-commands/index.d.ts
vendored
@ -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
|
||||
|
||||
@ -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];
|
||||
});
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}),
|
||||
|
||||
Loading…
Reference in New Issue
Block a user