Repository navigation
Real-time collaboration: Add UndoManager support for collaborative editing #72407
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
Changes from all commits
f55808e
72a425a
137489a
d7637c9
cad2133
1f1020c
021c75d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import type * as Y from 'yjs'; | ||
|
|
||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import type { HistoryRecord } from '@wordpress/undo-manager'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { LOCAL_EDITOR_ORIGIN } from './config'; | ||
| import { YMultiDocUndoManager } from './y-utilities/y-multidoc-undomanager'; | ||
| import type { ObjectData, SyncUndoManager } from './types'; | ||
|
|
||
| /** | ||
| * Implementation of the WordPress UndoManager interface using YMultiDocUndoManager | ||
| * internally. This allows undo/redo operations to be transacted against multiple | ||
| * CRDT documents (one per entity) and giving each peer their own undo/redo stack | ||
| * without conflicts. | ||
| */ | ||
| export function createUndoManager(): SyncUndoManager { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if this deserves some unit tests, to check the behavior of undoing/redoing stuff. Seems important.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, it def is. I'll be making a follow up PR with that. I wanted to limit the scope of this PR on just the UndoManager changes. Happy to make it in this PR, if you'd prefer that. |
||
| const yUndoManager = new YMultiDocUndoManager( [], { | ||
| // Throttle undo/redo captures. (default: 500ms) | ||
| captureTimeout: 200, | ||
| // Ensure that we only scope the undo/redo to the current editor. | ||
| // The yjs document's clientID is added once it's available. | ||
| trackedOrigins: new Set( [ LOCAL_EDITOR_ORIGIN ] ), | ||
| } ); | ||
|
|
||
| return { | ||
| /** | ||
| * Record changes into the history. | ||
| * Since Yjs automatically tracks changes, this method translates the WordPress | ||
| * HistoryRecord format into Yjs operations. | ||
| * | ||
| * @param _record A record of changes to record. | ||
| * @param _isStaged Whether to immediately create an undo point or not. | ||
| */ | ||
| addRecord( | ||
| _record?: HistoryRecord< ObjectData >, | ||
| _isStaged = false // eslint-disable-line @typescript-eslint/no-unused-vars | ||
| ): void { | ||
| // This is a no-op for Yjs since it automatically tracks changes. | ||
| // If needed, we could implement custom logic to handle specific records. | ||
| }, | ||
|
|
||
| /** | ||
| * Add a Yjs map to the scope of the undo manager. | ||
| * | ||
| * @param {Y.Map< any >} ymap The Yjs map to add to the scope. | ||
| */ | ||
| addToScope( ymap: Y.Map< any > ): void { | ||
| yUndoManager.addToScope( ymap ); | ||
| }, | ||
|
|
||
| /** | ||
| * Undo the last recorded changes. | ||
| * | ||
| */ | ||
| undo(): HistoryRecord< ObjectData > | undefined { | ||
| if ( ! yUndoManager.canUndo() ) { | ||
| return; | ||
| } | ||
|
|
||
| // Perform the undo operation | ||
| yUndoManager.undo(); | ||
|
|
||
| // Intentionally return an empty array, because the SyncProvider will update | ||
| // the entity record based on the Yjs document changes. | ||
| return []; | ||
| }, | ||
|
|
||
| /** | ||
| * Redo the last undone changes. | ||
| */ | ||
| redo(): HistoryRecord< ObjectData > | undefined { | ||
| if ( ! yUndoManager.canRedo() ) { | ||
| return; | ||
| } | ||
|
|
||
| // Perform the redo operation | ||
| yUndoManager.redo(); | ||
|
|
||
| // Intentionally return an empty array, because the SyncProvider will update | ||
| // the entity record based on the Yjs document changes. | ||
| return []; | ||
| }, | ||
|
|
||
| /** | ||
| * Check if there are changes that can be undone. | ||
| * | ||
| * @return {boolean} Whether there are changes to undo. | ||
| */ | ||
| hasUndo(): boolean { | ||
| return yUndoManager.canUndo(); | ||
| }, | ||
|
|
||
| /** | ||
| * Check if there are changes that can be redone. | ||
| * | ||
| * @return {boolean} Whether there are changes to redo. | ||
| */ | ||
| hasRedo(): boolean { | ||
| return yUndoManager.canRedo(); | ||
| }, | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| The MIT License (MIT) | ||
|
|
||
| Copyright (c) 2022 Kevin Jahns <kevin.jahns@protonmail.com>. | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One subtle issue here is that every-time
state.undoManagerchanges, thegetUndoManagerprivate selector is called (if used within useSelect and things like that) which ensures the UI is always properly reactive.But, it's not the case with the custom "sync" undo manager.
Solving that is a bit challenging. one possibility is building a custom
@wordpress/datastore in the sync package (a store is a function that returns subscribe, getActions, getSelectors) and ensure the listeners (folks that called subscribe) are actually called one new undo/redo actions are triggered. Once you have the store, you could transformgetUndoManagerto a "registry selector" (createRegistrySelector).As evidenced by the above, it's a bit involved, so I wonder what you think. Should we try to address it or should we assume that if there's an undo or redo that is created something else in the "state" of core-data is probably changing at the same time (which triggers the selectors again). (Basically accept that the bug exists but is hidden, or hard to reproduce)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing that out, and for explaining it so well.
Would you know off the top of your head, what could be a rough way to reproduce this bug? I'd be interested to see what the circumstances could be to trigger such a situation. We haven't been able to reproduce any issues in our testing, so maybe there is a workflow that we are missing. It'd be good to consider any such edge cases.
I'd lean towards accepting that the bug exists but is hidden or hard to reproduce. Given we have this as behind an experiment, it won't show up in the vanilla version of Gutenberg which is great. We can see if it comes up in during testing, and address it based on that.
I think this is the assumption that I'd want to go with as well.
The multidocundomanager would keep track of all the changes in the editor, and so we'd be able to ensure that nothing is amiss.
Hopefully I clarified everything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now, it's not really possible to reproduce because any thing that creates an "undo" level also updates something else in the store (like entities data or something) which means the "subscribe" of the core-data store is triggered. But I'm not sure if this is a guarantee or just the current state of things.
Let's say tomorrow, we move "edits" out of the core-data store into the doc exclusively and the bug appears basically.
I'm ok with this, I think we should mention the assumption above in the selector JSDoc to clarify that we're aware of it but we assume that an undo or redo level will always be accompanied by a change to the state.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely understand that. I've updated the private selector with a summary of the issue, the assumption that's being made, and a link to this comment chain.
That should be sufficient for any changes that are needed down the line then.