add can method to editor class
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
import { EditorState, Transaction } from 'prosemirror-state'
|
import { EditorState, Transaction } from 'prosemirror-state'
|
||||||
import { ChainedCommands, Editor, CommandSpec } from './Editor'
|
import {
|
||||||
|
SingleCommands, ChainedCommands, Editor, CommandSpec,
|
||||||
|
} from './Editor'
|
||||||
import getAllMethodNames from './utils/getAllMethodNames'
|
import getAllMethodNames from './utils/getAllMethodNames'
|
||||||
|
|
||||||
export default class CommandManager {
|
export default class CommandManager {
|
||||||
@@ -95,6 +97,30 @@ export default class CommandManager {
|
|||||||
}) as ChainedCommands
|
}) as ChainedCommands
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public createCan(startTr?: Transaction) {
|
||||||
|
const { commands, editor } = this
|
||||||
|
const { state } = editor
|
||||||
|
const dispatch = false
|
||||||
|
const hasStartTransaction = !!startTr
|
||||||
|
const tr = hasStartTransaction ? startTr : state.tr
|
||||||
|
|
||||||
|
if (!tr) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const props = this.buildProps(tr, dispatch)
|
||||||
|
const formattedCommands = Object.fromEntries(Object
|
||||||
|
.entries(commands)
|
||||||
|
.map(([name, command]) => {
|
||||||
|
return [name, (...args: any[]) => command(...args)({ ...props, dispatch })]
|
||||||
|
}))
|
||||||
|
|
||||||
|
return {
|
||||||
|
...formattedCommands,
|
||||||
|
chain: () => this.createChain(tr, dispatch),
|
||||||
|
} as SingleCommands & { chain: () => ChainedCommands }
|
||||||
|
}
|
||||||
|
|
||||||
public buildProps(tr: Transaction, shouldDispatch = true) {
|
public buildProps(tr: Transaction, shouldDispatch = true) {
|
||||||
const { editor, commands } = this
|
const { editor, commands } = this
|
||||||
const { state, view } = editor
|
const { state, view } = editor
|
||||||
@@ -108,19 +134,7 @@ export default class CommandManager {
|
|||||||
? () => true
|
? () => true
|
||||||
: undefined,
|
: undefined,
|
||||||
chain: () => this.createChain(tr),
|
chain: () => this.createChain(tr),
|
||||||
can: () => {
|
can: () => this.createCan(tr),
|
||||||
const dispatch = false
|
|
||||||
const formattedCommands = Object.fromEntries(Object
|
|
||||||
.entries(commands)
|
|
||||||
.map(([name, command]) => {
|
|
||||||
return [name, (...args: any[]) => command(...args)({ ...props, dispatch })]
|
|
||||||
}))
|
|
||||||
|
|
||||||
return {
|
|
||||||
...formattedCommands,
|
|
||||||
chain: () => this.createChain(tr, dispatch),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
get commands() {
|
get commands() {
|
||||||
return Object.fromEntries(Object
|
return Object.fromEntries(Object
|
||||||
.entries(commands)
|
.entries(commands)
|
||||||
|
|||||||
@@ -115,7 +115,6 @@ export class Editor extends EventEmitter {
|
|||||||
this.createExtensionManager()
|
this.createExtensionManager()
|
||||||
this.createSchema()
|
this.createSchema()
|
||||||
this.createView()
|
this.createView()
|
||||||
// this.registerCommands(coreCommands)
|
|
||||||
this.injectCSS()
|
this.injectCSS()
|
||||||
|
|
||||||
window.setTimeout(() => this.proxy.focus(this.options.autoFocus), 0)
|
window.setTimeout(() => this.proxy.focus(this.options.autoFocus), 0)
|
||||||
@@ -138,6 +137,13 @@ export class Editor extends EventEmitter {
|
|||||||
return this.commandManager.createChain()
|
return this.commandManager.createChain()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a command or a command chain can be executed. Without executing it.
|
||||||
|
*/
|
||||||
|
public can() {
|
||||||
|
return this.commandManager.createCan()
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inject CSS styles.
|
* Inject CSS styles.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user