From 778e0649798897c45fa41baedbcc033be9998ee9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Mon, 21 Sep 2020 21:40:44 +0200 Subject: [PATCH] add basic ChainedCommands type --- docs/src/demos/Examples/Simple/index.vue | 2 +- packages/core/src/Editor.ts | 21 ++++++++++++------- packages/core/src/commands/deleteSelection.ts | 6 ++++++ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/docs/src/demos/Examples/Simple/index.vue b/docs/src/demos/Examples/Simple/index.vue index e146722d..14a97cf5 100644 --- a/docs/src/demos/Examples/Simple/index.vue +++ b/docs/src/demos/Examples/Simple/index.vue @@ -5,7 +5,7 @@ - + diff --git a/packages/core/src/Editor.ts b/packages/core/src/Editor.ts index 35cecfda..27815826 100644 --- a/packages/core/src/Editor.ts +++ b/packages/core/src/Editor.ts @@ -19,6 +19,7 @@ import Mark from './Mark' import ComponentRenderer from './ComponentRenderer' import defaultPlugins from './plugins' import * as commands from './commands' +import { deleteSelection } from 'prosemirror-commands' // export type Command = (next: Function, editor: Editor) => (...args: any) => any @@ -35,18 +36,24 @@ export interface CommandSpec { [key: string]: Command } -type EditorContent = string | JSON | null +export interface Commands {} -// interface Element { -// editor?: Editor -// } +// export type CommandNames = Extract + +export type ChainedCommands = { + [Command in keyof Commands]: Commands[Command] extends (...args: any[]) => any + ? (...args: Parameters) => ChainedCommands + : never +} & { + run: () => boolean +} + +type EditorContent = string | JSON | null interface HTMLElement { editor?: Editor } -// Element.prototype.editor = Editor - interface EditorOptions { element: Element, content: EditorContent, @@ -176,7 +183,7 @@ export class Editor extends EventEmitter { return proxy } } - }) + }) as ChainedCommands } protected chainableEditorState(tr: Transaction, state: EditorState): EditorState { diff --git a/packages/core/src/commands/deleteSelection.ts b/packages/core/src/commands/deleteSelection.ts index 3ee15721..c2fde9f6 100644 --- a/packages/core/src/commands/deleteSelection.ts +++ b/packages/core/src/commands/deleteSelection.ts @@ -9,6 +9,12 @@ declare module '../Editor' { } } +// declare module '../Editor' { +// interface Commands { +// deleteSelection: DeleteSelectionCommand, +// } +// } + export const deleteSelection: DeleteSelectionCommand = () => ({ state, dispatch }) => { return originalDeleteSelection(state, dispatch) }