add can method

This commit is contained in:
Philipp Kühn
2020-11-02 14:29:58 +01:00
parent 85c1a8ace9
commit 8f4abb720a
3 changed files with 24 additions and 9 deletions

View File

@@ -57,7 +57,7 @@ export default class CommandManager {
} }
} }
public createChain(startTr?: Transaction) { public createChain(startTr?: Transaction, shouldDispatch = true) {
const { commands, editor } = this const { commands, editor } = this
const { state, view } = editor const { state, view } = editor
const callbacks: boolean[] = [] const callbacks: boolean[] = []
@@ -71,7 +71,7 @@ export default class CommandManager {
return new Proxy({}, { return new Proxy({}, {
get: (_, name: string, proxy) => { get: (_, name: string, proxy) => {
if (name === 'run') { if (name === 'run') {
if (!hasStartTransaction) { if (!hasStartTransaction && shouldDispatch) {
view.dispatch(tr) view.dispatch(tr)
} }
@@ -85,7 +85,7 @@ export default class CommandManager {
} }
return (...args: any) => { return (...args: any) => {
const props = this.buildProps(tr) const props = this.buildProps(tr, shouldDispatch)
const callback = command(...args)(props) const callback = command(...args)(props)
callbacks.push(callback) callbacks.push(callback)
@@ -95,7 +95,7 @@ export default class CommandManager {
}) as ChainedCommands }) as ChainedCommands
} }
public buildProps(tr: Transaction) { public buildProps(tr: Transaction, shouldDispatch = true) {
const { editor, commands } = this const { editor, commands } = this
const { state, view } = editor const { state, view } = editor
@@ -104,8 +104,23 @@ export default class CommandManager {
editor, editor,
view, view,
state: this.chainableState(tr, state), state: this.chainableState(tr, state),
dispatch: () => false, dispatch: shouldDispatch
? () => true
: undefined,
chain: () => this.createChain(tr), chain: () => this.createChain(tr),
can: () => {
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)

View File

@@ -23,6 +23,7 @@ export type Command = (props: {
editor: Editor, editor: Editor,
tr: Transaction, tr: Transaction,
commands: SingleCommands, commands: SingleCommands,
can: () => SingleCommands & { chain: () => ChainedCommands },
chain: () => ChainedCommands, chain: () => ChainedCommands,
state: EditorState, state: EditorState,
view: EditorView, view: EditorView,

View File

@@ -10,7 +10,7 @@ export const ToggleList = createExtension({
addCommands() { addCommands() {
return { return {
toggleList: (listTypeOrName: string | NodeType, itemTypeOrName: string | NodeType): Command => ({ toggleList: (listTypeOrName: string | NodeType, itemTypeOrName: string | NodeType): Command => ({
tr, state, dispatch, chain, tr, state, dispatch, chain, commands, can,
}) => { }) => {
const { extensions } = this.editor.options const { extensions } = this.editor.options
const listType = getNodeType(listTypeOrName, state.schema) const listType = getNodeType(listTypeOrName, state.schema)
@@ -39,18 +39,17 @@ export const ToggleList = createExtension({
} }
} }
const canWrapInList = wrapInList(listType)(state) const canWrapInList = can().wrapInList(listType)
// try to convert node to paragraph if needed // try to convert node to paragraph if needed
if (!canWrapInList) { if (!canWrapInList) {
return chain() return chain()
// .setBlockType('paragraph')
.clearNodes() .clearNodes()
.wrapInList(listType) .wrapInList(listType)
.run() .run()
} }
return wrapInList(listType)(state, dispatch) return commands.wrapInList(listType)
}, },
} }
}, },