-
Notifications
You must be signed in to change notification settings - Fork 21
feat: emit change events with the applied patches for local and remote changes
#3199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
christianhg
wants to merge
1
commit into
next
Choose a base branch
from
feat/change-channel
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,285
−40
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| --- | ||
| '@portabletext/editor': minor | ||
| --- | ||
|
|
||
| feat: emit `change` events with the applied operations for local and remote changes | ||
|
|
||
| The editor now emits a `change` event for every edit applied to the document. Each event carries the applied operations and an `origin` that tells you whether the edit was made in this editor (`'local'`) or arrived from the outside (`'remote'`). Use it to keep derived state (indexes, anchors, external copies of the value) in sync without diffing the value yourself. Previously only local edits were observable, through the `mutation` event. | ||
|
|
||
| ```tsx | ||
| import {EventListenerPlugin} from '@portabletext/editor/plugins' | ||
|
|
||
| <EventListenerPlugin | ||
| on={(event) => { | ||
| if (event.type === 'change' && event.origin === 'remote') { | ||
| for (const operation of event.operations) { | ||
| invalidateBlock(operation.path[0]) | ||
| } | ||
| } | ||
| }} | ||
| /> | ||
| ``` | ||
|
|
||
| Notes: | ||
|
|
||
| - `operations` uses the same operation types as the `operation` event: `insert`, `insert.text`, `remove.text`, `set`, and `unset`. | ||
| - Local events arrive at the same cadence as `mutation` events. Undo and redo count as local. | ||
| - Remote updates emit one event per applied block, in application order. Apply them in that order. | ||
| - An `update value` that changes nothing emits nothing. | ||
| - The initial value sync also emits a `change`, taking the editor's empty seed document to your configured initial value. If you maintain a copy of the value loaded from storage, start applying events at the `ready` event; otherwise you would re-apply content your copy already has. | ||
| - The `operations` array is a fresh copy per event, but the operation objects in it are shared with the editor: treat them as read-only and copy anything you keep around. | ||
|
|
||
| New exports: `ChangeEvent`, and the `'change'` member on `EditorEmittedEvent`. Exhaustive switches over emitted event types gain a case. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import {defineSchema} from '@portabletext/schema' | ||
| import {createTestKeyGenerator} from '@portabletext/test' | ||
| import {describe, expect, test, vi} from 'vitest' | ||
| import {stopActor} from '../internal-utils/stop-actor' | ||
| import {createInternalEditor} from './create-editor' | ||
| import type {EditorEmittedEvent} from './relay' | ||
|
|
||
| /** | ||
| * Pins the `mutation`-`change` pairing for edits applied while the editor | ||
| * actor is idle: `editorEngine.apply` is the same direct-engine call | ||
| * `Editable.tsx`'s focus handler uses, bypassing `editorActor.send` and | ||
| * with it the actor's mailbox deferral. | ||
| */ | ||
| describe('createInternalEditor: local `change` joins a `mutation` applied outside actor processing', () => { | ||
| test('a `set` operation applied directly on the engine still reports a `change`', async () => { | ||
| const internalEditor = createInternalEditor({ | ||
| keyGenerator: createTestKeyGenerator(), | ||
| schemaDefinition: defineSchema({}), | ||
| initialValue: [ | ||
| { | ||
| _type: 'block', | ||
| _key: 'b1', | ||
| style: 'normal', | ||
| markDefs: [], | ||
| children: [{_type: 'span', _key: 'b1-span', text: 'foo', marks: []}], | ||
| }, | ||
| ], | ||
| }) | ||
|
|
||
| const unsubscribers = internalEditor.subscriptions.map((subscribe) => | ||
| subscribe(), | ||
| ) | ||
| internalEditor.actors.editorActor.start() | ||
| internalEditor.actors.editorActor.send({ | ||
| type: 'add editor engine', | ||
| editor: internalEditor.editorEngine, | ||
| }) | ||
| internalEditor.relay.start() | ||
| internalEditor.actors.syncActor.start() | ||
|
|
||
| const events: Array<EditorEmittedEvent> = [] | ||
| internalEditor.editor.on('*', (event) => { | ||
| events.push(event) | ||
| }) | ||
|
|
||
| // The initial value reaches the engine through the sync machine's own | ||
| // (async) reconciliation: `apply` must wait for it, or it targets the | ||
| // placeholder block the engine starts with instead of `b1`. | ||
| await vi.waitFor(() => { | ||
| expect(internalEditor.editorEngine.snapshot.context.value).toEqual([ | ||
| expect.objectContaining({_key: 'b1'}), | ||
| ]) | ||
| }) | ||
|
|
||
| internalEditor.editorEngine.apply({ | ||
| type: 'set', | ||
| path: [{_key: 'b1'}, 'style'], | ||
| value: 'h1', | ||
| }) | ||
|
|
||
| await vi.waitFor(() => { | ||
| expect(events.some((event) => event.type === 'mutation')).toBe(true) | ||
| }) | ||
|
|
||
| expect( | ||
| events.some( | ||
| (event) => event.type === 'change' && event.origin === 'local', | ||
| ), | ||
| ).toBe(true) | ||
|
|
||
| for (const unsubscribe of unsubscribers) { | ||
| unsubscribe() | ||
| } | ||
| stopActor(internalEditor.actors.editorActor) | ||
| internalEditor.relay.stop() | ||
| stopActor(internalEditor.actors.syncActor) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.