fix(core) Nested chain not preserving dispatch state (#4152)

* Fix nested chain not preserving dispatch state

* Change test to read as sentence
This commit is contained in:
Cameron Hessler 2023-07-07 04:20:29 -05:00 committed by GitHub
parent b24df3aa4c
commit 26610cdff3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View File

@ -133,7 +133,7 @@ export class CommandManager {
transaction: tr,
}),
dispatch: shouldDispatch ? () => undefined : undefined,
chain: () => this.createChain(tr),
chain: () => this.createChain(tr, shouldDispatch),
can: () => this.createCan(tr),
get commands() {
return Object.fromEntries(

View File

@ -167,4 +167,32 @@ describe('can', () => {
expect(canSetMarkToBold).to.eq(true)
})
it('builds and passes down an undefined dispatch for nested "can" chain', () => {
const editor = new Editor({
extensions: [Document, Paragraph, Text, History],
})
let capturedOuterDispatch: ((args?: any) => any) | undefined
let capturedInnerDispatch: ((args?: any) => any) | undefined
editor
.can()
.chain()
.command(({ chain, dispatch: outterDispatch }) => {
capturedOuterDispatch = outterDispatch
return chain()
.command(({ dispatch: innerDispatch }) => {
capturedInnerDispatch = innerDispatch
return true
})
.run()
})
.run()
// eslint-disable-next-line no-unused-expressions
expect(capturedOuterDispatch).to.be.undefined
// eslint-disable-next-line no-unused-expressions
expect(capturedInnerDispatch).to.be.undefined
})
})