feat: support revert changes (#1782)
This commit is contained in:
@@ -179,10 +179,9 @@ describe('milestone', () => {
|
||||
const map = snapshot.getMap('map');
|
||||
expect(map.get('1')).toBe(1);
|
||||
}
|
||||
revertUpdate(doc, milestones.test1, {
|
||||
map: 'Map',
|
||||
array: 'Array',
|
||||
});
|
||||
revertUpdate(doc, milestones.test1, key =>
|
||||
key === 'map' ? 'Map' : 'Array'
|
||||
);
|
||||
{
|
||||
const map = doc.getMap('map');
|
||||
expect(map.get('1')).toBe(1);
|
||||
@@ -207,10 +206,9 @@ describe('milestone', () => {
|
||||
const doc2 = new Doc();
|
||||
applyUpdate(doc2, encodeStateAsUpdate(doc));
|
||||
|
||||
revertUpdate(doc2, milestones.test1, {
|
||||
map: 'Map',
|
||||
array: 'Array',
|
||||
});
|
||||
revertUpdate(doc2, milestones.test1, key =>
|
||||
key === 'map' ? 'Map' : 'Array'
|
||||
);
|
||||
{
|
||||
const map = doc2.getMap('map');
|
||||
expect(map.get('1')).toBe(1);
|
||||
|
||||
@@ -15,12 +15,10 @@ const snapshotOrigin = Symbol('snapshot-origin');
|
||||
|
||||
let mergeCount = 500;
|
||||
|
||||
type Metadata = Record<string, 'Text' | 'Map' | 'Array'>;
|
||||
|
||||
export function revertUpdate(
|
||||
doc: Doc,
|
||||
snapshotUpdate: Uint8Array,
|
||||
metadata: Metadata
|
||||
getMetadata: (key: string) => 'Text' | 'Map' | 'Array'
|
||||
) {
|
||||
const snapshotDoc = new Doc();
|
||||
applyUpdate(snapshotDoc, snapshotUpdate, snapshotOrigin);
|
||||
@@ -34,11 +32,12 @@ export function revertUpdate(
|
||||
);
|
||||
const undoManager = new UndoManager(
|
||||
[...snapshotDoc.share.keys()].map(key => {
|
||||
if (metadata[key] === 'Text') {
|
||||
const type = getMetadata(key);
|
||||
if (type === 'Text') {
|
||||
return snapshotDoc.getText(key);
|
||||
} else if (metadata[key] === 'Map') {
|
||||
} else if (type === 'Map') {
|
||||
return snapshotDoc.getMap(key);
|
||||
} else if (metadata[key] === 'Array') {
|
||||
} else if (type === 'Array') {
|
||||
return snapshotDoc.getArray(key);
|
||||
}
|
||||
throw new Error('Unknown type');
|
||||
|
||||
Reference in New Issue
Block a user