remove async commands (WIP)

This commit is contained in:
Philipp Kühn
2020-09-11 17:30:55 +02:00
parent 5d3a2e900b
commit 6a04f9746d
5 changed files with 59 additions and 28 deletions

View File

@@ -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<EditorOptions> = {}) {
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.
*/

View File

@@ -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()
}

View File

@@ -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()
}

View File

@@ -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()
}

View File

@@ -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),
}))