-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(collab): Fix Duplicate commands make editors become out-of-sync
Duplicate commands, when serialized, did not store enough information to be completely reconstructed
- Loading branch information
1 parent
56975a1
commit bee6451
Showing
3 changed files
with
110 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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,47 @@ | ||
import Duplicate from './Duplicate'; | ||
import { Color4, Path } from '@js-draw/math'; | ||
import { EditorImage, pathToRenderable, SerializableCommand, Stroke } from '../lib'; | ||
import createEditor from '../testing/createEditor'; | ||
|
||
const getAllStrokeIds = (editorImage: EditorImage) => { | ||
const strokes = editorImage.getAllElements().filter((elem) => elem instanceof Stroke); | ||
return strokes.map((stroke) => stroke.getId()); | ||
}; | ||
|
||
describe('Duplicate', () => { | ||
test('deserialized Duplicate commands should create clones with the same IDs', async () => { | ||
const editor = createEditor(); | ||
|
||
const stroke = new Stroke([ | ||
pathToRenderable(Path.fromString('m0,0 l10,10 l-10,0'), { fill: Color4.red }), | ||
]); | ||
await editor.dispatch(editor.image.addElement(stroke)); | ||
|
||
const command = new Duplicate([stroke]); | ||
command.apply(editor); | ||
|
||
// Should have duplicated [element] | ||
const strokes1 = getAllStrokeIds(editor.image); | ||
expect(strokes1).toHaveLength(2); | ||
// Should not have removed the first element | ||
expect(strokes1).toContain(stroke.getId()); | ||
// Clone should have a different ID | ||
expect(strokes1.filter((id) => id !== stroke.getId())).toHaveLength(1); | ||
|
||
// Apply a deserialized copy of the command | ||
const serialized = command.serialize(); | ||
const deserialized = SerializableCommand.deserialize(serialized, editor); | ||
|
||
command.unapply(editor); | ||
deserialized.apply(editor); | ||
|
||
// The copy should produce a clone with the same ID | ||
const strokes2 = getAllStrokeIds(editor.image); | ||
expect(strokes1).toEqual(strokes2); | ||
|
||
// It should be possible to unapply the deserialized command | ||
deserialized.unapply(editor); | ||
|
||
expect(getAllStrokeIds(editor.image)).toEqual([stroke.getId()]); | ||
}); | ||
}); |
This file contains 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 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