Skip to content

Commit

Permalink
feat(core): expose appended transactions in the onChange handler (#1558)
Browse files Browse the repository at this point in the history
  • Loading branch information
whawker committed Mar 17, 2022
1 parent 974a586 commit 68677ca
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/perfect-vans-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@remirror/core': minor
'@remirror/react-core': minor
---

Expose appended transactions via the onChange handler
22 changes: 21 additions & 1 deletion packages/remirror__core/src/framework/base-framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,16 @@ export interface RemirrorEventListenerProps<Extension extends AnyExtension>
*/
tr?: Transaction<GetSchema<Extension>>;

/**
* When the state updates are not controlled and it was a transaction that
* caused the state to be updated this value captures all the transaction
* updates caused by prosemirror plugins hook state methods like
* `filterTransactions` and `appendTransactions`.
*
* This is for advanced users only.
*/
transactions?: Array<Transaction<GetSchema<Extension>>>;

/**
* A shorthand way of checking whether the update was triggered by editor
* usage (internal) or overwriting the state.
Expand Down Expand Up @@ -324,7 +334,17 @@ export interface TriggerChangeProps {

export interface ListenerProps<Extension extends AnyExtension>
extends Partial<EditorStateProps<GetSchema<Extension>>>,
Partial<TransactionProps<GetSchema<Extension>>> {}
Partial<TransactionProps<GetSchema<Extension>>> {
/**
* When the state updates are not controlled and it was a transaction that
* caused the state to be updated this value captures all the transaction
* updates caused by prosemirror plugins hook state methods like
* `filterTransactions` and `appendTransactions`.
*
* This is for advanced users only.
*/
transactions?: Array<Transaction<GetSchema<Extension>>>;
}

export interface FrameworkEvents<Extension extends AnyExtension>
extends Pick<ManagerEvents, 'destroy'> {
Expand Down
3 changes: 2 additions & 1 deletion packages/remirror__core/src/framework/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,11 @@ export abstract class Framework<
protected eventListenerProps(
props: ListenerProps<Extension> = object(),
): RemirrorEventListenerProps<Extension> {
const { state, tr } = props;
const { state, tr, transactions } = props;

return {
tr,
transactions,
internalUpdate: !tr,
view: this.view,
firstRender: this.#firstRender,
Expand Down
4 changes: 2 additions & 2 deletions packages/remirror__react-core/src/react-framework.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export class ReactFramework<Extension extends AnyExtension> extends Framework<
this.previousStateOverride = this.getState();
}

this.onChange({ state, tr });
this.onChange({ state, tr, transactions });

return;
}
Expand All @@ -218,7 +218,7 @@ export class ReactFramework<Extension extends AnyExtension> extends Framework<
// If `transactions` is an empty array, that means the transaction was cancelled by `filterTransaction`.
if (triggerChange && transactions?.length !== 0) {
// Update the `onChange` handler before notifying the manager but only when a change should be triggered.
this.onChange({ state, tr });
this.onChange({ state, tr, transactions });
}

this.manager.onStateUpdate({ previousState: this.previousState, state, tr, transactions });
Expand Down

1 comment on commit 68677ca

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Published on https://remirror.io as production
🚀 Deployed on https://62339e32eb241d0e2e96d3cc--remirror.netlify.app

Please sign in to comment.