add can method
This commit is contained in:
@@ -57,7 +57,7 @@ export default class CommandManager {
|
||||
}
|
||||
}
|
||||
|
||||
public createChain(startTr?: Transaction) {
|
||||
public createChain(startTr?: Transaction, shouldDispatch = true) {
|
||||
const { commands, editor } = this
|
||||
const { state, view } = editor
|
||||
const callbacks: boolean[] = []
|
||||
@@ -71,7 +71,7 @@ export default class CommandManager {
|
||||
return new Proxy({}, {
|
||||
get: (_, name: string, proxy) => {
|
||||
if (name === 'run') {
|
||||
if (!hasStartTransaction) {
|
||||
if (!hasStartTransaction && shouldDispatch) {
|
||||
view.dispatch(tr)
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ export default class CommandManager {
|
||||
}
|
||||
|
||||
return (...args: any) => {
|
||||
const props = this.buildProps(tr)
|
||||
const props = this.buildProps(tr, shouldDispatch)
|
||||
const callback = command(...args)(props)
|
||||
callbacks.push(callback)
|
||||
|
||||
@@ -95,7 +95,7 @@ export default class CommandManager {
|
||||
}) as ChainedCommands
|
||||
}
|
||||
|
||||
public buildProps(tr: Transaction) {
|
||||
public buildProps(tr: Transaction, shouldDispatch = true) {
|
||||
const { editor, commands } = this
|
||||
const { state, view } = editor
|
||||
|
||||
@@ -104,8 +104,23 @@ export default class CommandManager {
|
||||
editor,
|
||||
view,
|
||||
state: this.chainableState(tr, state),
|
||||
dispatch: () => false,
|
||||
dispatch: shouldDispatch
|
||||
? () => true
|
||||
: undefined,
|
||||
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() {
|
||||
return Object.fromEntries(Object
|
||||
.entries(commands)
|
||||
|
||||
@@ -23,6 +23,7 @@ export type Command = (props: {
|
||||
editor: Editor,
|
||||
tr: Transaction,
|
||||
commands: SingleCommands,
|
||||
can: () => SingleCommands & { chain: () => ChainedCommands },
|
||||
chain: () => ChainedCommands,
|
||||
state: EditorState,
|
||||
view: EditorView,
|
||||
|
||||
@@ -10,7 +10,7 @@ export const ToggleList = createExtension({
|
||||
addCommands() {
|
||||
return {
|
||||
toggleList: (listTypeOrName: string | NodeType, itemTypeOrName: string | NodeType): Command => ({
|
||||
tr, state, dispatch, chain,
|
||||
tr, state, dispatch, chain, commands, can,
|
||||
}) => {
|
||||
const { extensions } = this.editor.options
|
||||
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
|
||||
if (!canWrapInList) {
|
||||
return chain()
|
||||
// .setBlockType('paragraph')
|
||||
.clearNodes()
|
||||
.wrapInList(listType)
|
||||
.run()
|
||||
}
|
||||
|
||||
return wrapInList(listType)(state, dispatch)
|
||||
return commands.wrapInList(listType)
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user