add basic ChainedCommands type

This commit is contained in:
Philipp Kühn
2020-09-21 21:40:44 +02:00
parent 4c392d5759
commit 778e064979
3 changed files with 21 additions and 8 deletions

View File

@@ -5,7 +5,7 @@
<button @click="editor.chain().focus().insertText('wat').insertHTML('<p>2</p>').run()">chain 1</button> <button @click="editor.chain().focus().insertText('wat').insertHTML('<p>2</p>').run()">chain 1</button>
<button @click="editor.chain().insertText('1').focus().run()">chain 2</button> <button @click="editor.chain().insertText('1').focus().run()">chain 2</button>
<button @click="editor.chain().setContent('reset').insertText('1').clearContent().run()">setContent</button> <button @click="editor.chain().setContent('reset').insertText('1').clearContent().run()">setContent</button>
<button @click="editor.chain().deleteSelection().run()">deleteSelection</button> <button @click="editor.chain().focus().deleteSelection().run()">deleteSelection</button>
<editor-content :editor="editor" /> <editor-content :editor="editor" />
</div> </div>
</template> </template>

View File

@@ -19,6 +19,7 @@ import Mark from './Mark'
import ComponentRenderer from './ComponentRenderer' import ComponentRenderer from './ComponentRenderer'
import defaultPlugins from './plugins' import defaultPlugins from './plugins'
import * as commands from './commands' import * as commands from './commands'
import { deleteSelection } from 'prosemirror-commands'
// export type Command = (next: Function, editor: Editor) => (...args: any) => any // export type Command = (next: Function, editor: Editor) => (...args: any) => any
@@ -35,18 +36,24 @@ export interface CommandSpec {
[key: string]: Command [key: string]: Command
} }
type EditorContent = string | JSON | null export interface Commands {}
// interface Element { // export type CommandNames = Extract<keyof Commands, string>
// editor?: Editor
// } export type ChainedCommands = {
[Command in keyof Commands]: Commands[Command] extends (...args: any[]) => any
? (...args: Parameters<Commands[Command]>) => ChainedCommands
: never
} & {
run: () => boolean
}
type EditorContent = string | JSON | null
interface HTMLElement { interface HTMLElement {
editor?: Editor editor?: Editor
} }
// Element.prototype.editor = Editor
interface EditorOptions { interface EditorOptions {
element: Element, element: Element,
content: EditorContent, content: EditorContent,
@@ -176,7 +183,7 @@ export class Editor extends EventEmitter {
return proxy return proxy
} }
} }
}) }) as ChainedCommands
} }
protected chainableEditorState(tr: Transaction, state: EditorState): EditorState { protected chainableEditorState(tr: Transaction, state: EditorState): EditorState {

View File

@@ -9,6 +9,12 @@ declare module '../Editor' {
} }
} }
// declare module '../Editor' {
// interface Commands {
// deleteSelection: DeleteSelectionCommand,
// }
// }
export const deleteSelection: DeleteSelectionCommand = () => ({ state, dispatch }) => { export const deleteSelection: DeleteSelectionCommand = () => ({ state, dispatch }) => {
return originalDeleteSelection(state, dispatch) return originalDeleteSelection(state, dispatch)
} }