From 26610cdff315c139e4e6d9ffade4ea85ad82631c Mon Sep 17 00:00:00 2001 From: Cameron Hessler Date: Fri, 7 Jul 2023 04:20:29 -0500 Subject: [PATCH] fix(core) Nested chain not preserving dispatch state (#4152) * Fix nested chain not preserving dispatch state * Change test to read as sentence --- packages/core/src/CommandManager.ts | 2 +- tests/cypress/integration/core/can.spec.ts | 28 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/packages/core/src/CommandManager.ts b/packages/core/src/CommandManager.ts index ae9071959..32063a904 100644 --- a/packages/core/src/CommandManager.ts +++ b/packages/core/src/CommandManager.ts @@ -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( diff --git a/tests/cypress/integration/core/can.spec.ts b/tests/cypress/integration/core/can.spec.ts index 0c8abee50..380892f44 100644 --- a/tests/cypress/integration/core/can.spec.ts +++ b/tests/cypress/integration/core/can.spec.ts @@ -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 + }) })