prosemirror-commands: Add types Command and Keymap (#45836)

Previously, no types were specified for a Command or for a Keymap. This
meant that (a) in prosemirror-commands, the types for pcBaseKeymap,
macBaseKeymap, and baseKeymap were not very well specified, and (b) in
prosemirror-keymap, there was some duplication of types.
This commit is contained in:
Mike Morearty 2020-07-08 12:05:50 -07:00 committed by GitHub
parent 816d93383e
commit 535948083a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 20 deletions

View File

@ -4,13 +4,26 @@
// David Hahn <https://github.com/davidka>
// Tim Baumann <https://github.com/timjb>
// Patrick Simmelbauer <https://github.com/patsimm>
// Mike Morearty <https://github.com/mmorearty>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
// TypeScript Version: 3.0
import { MarkType, Node as ProsemirrorNode, NodeType, Schema } from 'prosemirror-model';
import { EditorState, Transaction } from 'prosemirror-state';
import { EditorView } from 'prosemirror-view';
export interface Command<S extends Schema = any> {
(
state: EditorState<S>,
dispatch: (tr: Transaction<S>) => void,
view: EditorView<S>
): boolean;
}
export interface Keymap<S extends Schema = any> {
[key: string]: Command<S>;
}
/**
* Delete the selection, if there is one.
*/
@ -222,17 +235,17 @@ export function chainCommands<S extends Schema = any>(
* * **Mod-Delete** to `deleteSelection`, `joinForward`, `selectNodeForward`
* * **Mod-a** to `selectAll`
*/
export let pcBaseKeymap: { [key: string]: any };
export let pcBaseKeymap: Keymap;
/**
* A copy of `pcBaseKeymap` that also binds **Ctrl-h** like Backspace,
* **Ctrl-d** like Delete, **Alt-Backspace** like Ctrl-Backspace, and
* **Ctrl-Alt-Backspace**, **Alt-Delete**, and **Alt-d** like
* Ctrl-Delete.
*/
export let macBaseKeymap: { [key: string]: any };
export let macBaseKeymap: Keymap;
/**
* Depending on the detected platform, this will hold
* [`pcBasekeymap`](#commands.pcBaseKeymap) or
* [`macBaseKeymap`](#commands.macBaseKeymap).
*/
export let baseKeymap: { [key: string]: any };
export let baseKeymap: Keymap;

View File

@ -9,3 +9,13 @@ commands.deleteSelection(state);
commands.baseKeymap["Ctrl-h"];
commands.wrapIn(nodeType, { attr: 'value' });
const c1: commands.Command = commands.deleteSelection;
const c2: commands.Command = commands.joinBackward;
const keymap: commands.Keymap = {
ArrowUp: () => true, // takes no args
ArrowDown: commands.deleteSelection, // takes two args
ArrowLeft: commands.joinBackward, // takes three args
ArrowRight: (state, dispatch, view) => true, // arg types inferred
};

View File

@ -6,10 +6,11 @@
// Patrick Simmelbauer <https://github.com/patsimm>
// Mike Morearty <https://github.com/mmorearty>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
// TypeScript Version: 3.0
import { Keymap } from 'prosemirror-commands';
import { Schema } from 'prosemirror-model';
import { EditorState, Plugin, Transaction } from 'prosemirror-state';
import { Plugin } from 'prosemirror-state';
import { EditorView } from 'prosemirror-view';
/**
@ -43,23 +44,13 @@ import { EditorView } from 'prosemirror-view';
* which they appear determines their precedence (the ones early in
* the array get to dispatch first).
*/
export function keymap<S extends Schema = any>(bindings: {
[key: string]: (
state: EditorState<S>,
dispatch: (tr: Transaction<S>) => void,
view: EditorView<S>
) => boolean;
}): Plugin;
export function keymap<S extends Schema = any>(bindings: Keymap<S>): Plugin;
/**
* Given a set of bindings (using the same format as
* [`keymap`](#keymap.keymap), return a [keydown
* handler](#view.EditorProps.handleKeyDown) handles them.
*/
export function keydownHandler<S extends Schema = any>(bindings: {
[key: string]: (
state: EditorState<S>,
dispatch: (tr: Transaction<S>) => void,
view: EditorView<S>
) => boolean;
}): (view: EditorView, event: KeyboardEvent) => boolean;
export function keydownHandler<S extends Schema = any>(
bindings: Keymap<S>,
): (view: EditorView, event: KeyboardEvent) => boolean;