From c1a941a042b548f3157a30597535dc09270673c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Mon, 21 Sep 2020 18:40:32 +0200 Subject: [PATCH] add support for prosemirror commands --- docs/src/demos/Examples/Simple/index.vue | 1 + packages/core/src/Editor.ts | 48 +++++++++++++++++-- packages/core/src/commands/deleteSelection.ts | 11 ++--- packages/core/src/commands/index.ts | 2 +- 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/docs/src/demos/Examples/Simple/index.vue b/docs/src/demos/Examples/Simple/index.vue index cba3febe..e146722d 100644 --- a/docs/src/demos/Examples/Simple/index.vue +++ b/docs/src/demos/Examples/Simple/index.vue @@ -5,6 +5,7 @@ + diff --git a/packages/core/src/Editor.ts b/packages/core/src/Editor.ts index 058583f9..e9f28dfc 100644 --- a/packages/core/src/Editor.ts +++ b/packages/core/src/Editor.ts @@ -28,6 +28,7 @@ export type Command = (props: { tr: Transaction // TODO: find correct type commands: any + state: EditorState }) => boolean export interface CommandSpec { @@ -118,7 +119,14 @@ export class Editor extends EventEmitter { return (...args: any) => { const { tr } = this.state - const callback = command(...args)({ editor: this.proxy, tr }) + const callback = command(...args)({ + editor: this.proxy, + state: this.chainableEditorState(tr, this.state), + view: this.view, + dispatch: () => {}, + tr, + }) + this.view.dispatch(tr) return callback @@ -126,8 +134,7 @@ export class Editor extends EventEmitter { } public chain() { - // const { tr } = this.state - const tr = this.state.tr + const { tr } = this.state const callbacks = [] return new Proxy({}, { @@ -147,6 +154,9 @@ export class Editor extends EventEmitter { return (...args: any) => { const props = { editor: this.proxy, + state: this.chainableEditorState(tr, this.state), + view: this.view, + dispatch: () => {}, tr, } @@ -169,6 +179,38 @@ export class Editor extends EventEmitter { }) } + protected chainableEditorState(tr: Transaction, state: EditorState): EditorState { + let selection = tr.selection + let doc = tr.doc + let storedMarks = tr.storedMarks + + return { + ...state, + schema: state.schema, + plugins: state.plugins, + apply: state.apply.bind(state), + applyTransaction: state.applyTransaction.bind(state), + reconfigure: state.reconfigure.bind(state), + toJSON: state.toJSON.bind(state), + get storedMarks() { + return storedMarks + }, + get selection() { + return selection + }, + get doc() { + return doc + }, + get tr() { + selection = tr.selection + doc = tr.doc + storedMarks = tr.storedMarks + + return tr + }, + }; + } + /** * Update editor options. * diff --git a/packages/core/src/commands/deleteSelection.ts b/packages/core/src/commands/deleteSelection.ts index 63bbd7a3..3ee15721 100644 --- a/packages/core/src/commands/deleteSelection.ts +++ b/packages/core/src/commands/deleteSelection.ts @@ -1,7 +1,7 @@ -import { Editor } from '../Editor' -import { deleteSelection } from 'prosemirror-commands' +import { Command } from '../Editor' +import { deleteSelection as originalDeleteSelection } from 'prosemirror-commands' -type DeleteSelectionCommand = () => Editor +type DeleteSelectionCommand = () => Command declare module '../Editor' { interface Editor { @@ -9,7 +9,6 @@ declare module '../Editor' { } } -export default (next: Function, { state, view }: Editor) => () => { - deleteSelection(state, view.dispatch) - next() +export const deleteSelection: DeleteSelectionCommand = () => ({ state, dispatch }) => { + return originalDeleteSelection(state, dispatch) } diff --git a/packages/core/src/commands/index.ts b/packages/core/src/commands/index.ts index 46cd18ac..39588c4f 100644 --- a/packages/core/src/commands/index.ts +++ b/packages/core/src/commands/index.ts @@ -1,6 +1,6 @@ export { blur } from './blur' export { clearContent } from './clearContent' -// export { default as deleteSelection } from './deleteSelection' +export { deleteSelection } from './deleteSelection' export { focus } from './focus' export { insertHTML } from './insertHTML' export { insertText } from './insertText'