From 6a04f9746df69667fa93601eec65465990fa7f23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Fri, 11 Sep 2020 17:30:55 +0200 Subject: [PATCH] remove async commands (WIP) --- packages/core/src/Editor.ts | 65 +++++++++++++-------- packages/core/src/ExtensionManager.ts | 13 ++++- packages/core/src/commands/focus.ts | 3 + packages/core/src/commands/splitListItem.ts | 4 +- packages/extension-list-item/index.ts | 2 +- 5 files changed, 59 insertions(+), 28 deletions(-) diff --git a/packages/core/src/Editor.ts b/packages/core/src/Editor.ts index 7ac2742d..1ed4daed 100644 --- a/packages/core/src/Editor.ts +++ b/packages/core/src/Editor.ts @@ -46,6 +46,7 @@ export class Editor extends EventEmitter { private commands: { [key: string]: any } = {} private css!: HTMLStyleElement private lastCommand = Promise.resolve() + public lastCommandValue: any = undefined public schema!: Schema public view!: EditorView public selection = { from: 0, to: 0 } @@ -59,6 +60,7 @@ export class Editor extends EventEmitter { editable: true, } + constructor(options: Partial = {}) { super() this.options = { ...this.options, ...options } @@ -153,12 +155,50 @@ export class Editor extends EventEmitter { } this.commands[name] = this.chainCommand((...args: any) => { - return new Promise(resolve => callback(resolve, this.proxy)(...args)) + // console.log('command', this.lastCommandValue) + const commandValue = callback(() => {}, this.proxy)(...args) + + // if (commandValue !== undefined) { + this.lastCommandValue = commandValue + // } + + return this.proxy }) return this.proxy } + /** + * Call a command. + * + * @param name The name of the command you want to call. + * @param options The options of the command. + */ + public command(name: string, ...options: any) { + return this.commands[name](...options) + } + + /** + * Wraps a command to make it chainable. + * + * @param method + */ + private chainCommand = (method: Function) => (...args: any) => { + // console.log('chain', this.lastCommandValue) + // this.lastCommand = this.lastCommand + // .then(() => { + + // const jo = method.apply(this, args) + + // console.log({jo}) + // }) + // // .then(method.apply(this, args)) + // .catch(console.error) + method.apply(this, args) + + return this.proxy + } + /** * Register a ProseMirror plugin. * @@ -189,29 +229,6 @@ export class Editor extends EventEmitter { this.view.updateState(state) } - /** - * Call a command. - * - * @param name The name of the command you want to call. - * @param options The options of the command. - */ - public command(name: string, ...options: any) { - return this.commands[name](...options) - } - - /** - * Wraps a command to make it chainable. - * - * @param method - */ - private chainCommand = (method: Function) => (...args: any) => { - this.lastCommand = this.lastCommand - .then(() => method.apply(this, args)) - .catch(console.error) - - return this.proxy - } - /** * Creates an extension manager. */ diff --git a/packages/core/src/ExtensionManager.ts b/packages/core/src/ExtensionManager.ts index df056a02..4f4f4e54 100644 --- a/packages/core/src/ExtensionManager.ts +++ b/packages/core/src/ExtensionManager.ts @@ -94,7 +94,18 @@ export default class ExtensionManager { return collect(this.extensions) .map(extension => extension.config.keys) .filter(keys => keys) - .map(keys => keymap(keys)) + .map(keys => { + const values = Object.entries(keys).map(([key, action]) => { + return [key, () => { + this.editor.lastCommandValue = undefined + // @ts-ignore + const bla = action().lastCommandValue + // console.log({bla}) + return bla + }] + }) + return keymap(Object.fromEntries(values)) + }) .toArray() } diff --git a/packages/core/src/commands/focus.ts b/packages/core/src/commands/focus.ts index be1bd171..a2f3ef19 100644 --- a/packages/core/src/commands/focus.ts +++ b/packages/core/src/commands/focus.ts @@ -61,5 +61,8 @@ export default (next: Function, editor: Editor) => (position = null) => { view.dispatch(transaction) view.focus() + //@ts-ignore + // console.log(bla) + // return 'FOCUS' next() } \ No newline at end of file diff --git a/packages/core/src/commands/splitListItem.ts b/packages/core/src/commands/splitListItem.ts index 930eaec3..81e9b4a7 100644 --- a/packages/core/src/commands/splitListItem.ts +++ b/packages/core/src/commands/splitListItem.ts @@ -15,6 +15,6 @@ export default (next: Function, editor: Editor) => (typeOrName: string | NodeTyp const { view, state, schema } = editor const type = getNodeType(typeOrName, schema) - splitListItem(type)(state, view.dispatch) - next() + return splitListItem(type)(state, view.dispatch) + // next() } diff --git a/packages/extension-list-item/index.ts b/packages/extension-list-item/index.ts index fa384d6e..a29b53da 100644 --- a/packages/extension-list-item/index.ts +++ b/packages/extension-list-item/index.ts @@ -12,7 +12,7 @@ export default new Node() toDOM: () => ['li', 0], })) .keys(({ editor, name }) => ({ - // Enter: () => editor.splitListItem(name), + Enter: () => editor.splitListItem(name).focus() // Tab: () => editor.sinkListItem(name), // 'Shift-Tab': () => editor.liftListItem(name), }))