mirror of
https://github.com/toeverything/AFFiNE.git
synced 2024-12-22 23:01:35 +03:00
feat: support revert changes (#1782)
This commit is contained in:
parent
e0eecffb2f
commit
95879cc1d0
@ -14,21 +14,21 @@ import { usePageMeta } from '../use-page-meta';
|
|||||||
|
|
||||||
let blockSuiteWorkspace: BlockSuiteWorkspace;
|
let blockSuiteWorkspace: BlockSuiteWorkspace;
|
||||||
|
|
||||||
|
function handleNewPage(page: Page) {
|
||||||
|
const pageBlockId = page.addBlock('affine:page', { title: '' });
|
||||||
|
const frameId = page.addBlock('affine:frame', {}, pageBlockId);
|
||||||
|
page.addBlock('affine:paragraph', {}, frameId);
|
||||||
|
}
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
blockSuiteWorkspace = new BlockSuiteWorkspace({
|
blockSuiteWorkspace = new BlockSuiteWorkspace({
|
||||||
id: 'test',
|
id: 'test',
|
||||||
})
|
})
|
||||||
.register(AffineSchemas)
|
.register(AffineSchemas)
|
||||||
.register(__unstableSchemas);
|
.register(__unstableSchemas);
|
||||||
blockSuiteWorkspace.slots.pageAdded.on(pageId => {
|
handleNewPage(blockSuiteWorkspace.createPage('page0'));
|
||||||
const page = blockSuiteWorkspace.getPage(pageId) as Page;
|
handleNewPage(blockSuiteWorkspace.createPage('page1'));
|
||||||
const pageBlockId = page.addBlock('affine:page', { title: '' });
|
handleNewPage(blockSuiteWorkspace.createPage('page2'));
|
||||||
const frameId = page.addBlock('affine:frame', {}, pageBlockId);
|
|
||||||
page.addBlock('affine:paragraph', {}, frameId);
|
|
||||||
});
|
|
||||||
blockSuiteWorkspace.createPage('page0');
|
|
||||||
blockSuiteWorkspace.createPage('page1');
|
|
||||||
blockSuiteWorkspace.createPage('page2');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('useBlockSuiteWorkspaceHelper', () => {
|
describe('useBlockSuiteWorkspaceHelper', () => {
|
||||||
@ -46,4 +46,20 @@ describe('useBlockSuiteWorkspaceHelper', () => {
|
|||||||
pageMetaHook.rerender();
|
pageMetaHook.rerender();
|
||||||
expect(pageMetaHook.result.current.length).toBe(4);
|
expect(pageMetaHook.result.current.length).toBe(4);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('milestone', async () => {
|
||||||
|
expect(blockSuiteWorkspace.meta.pageMetas.length).toBe(3);
|
||||||
|
const helperHook = renderHook(() =>
|
||||||
|
useBlockSuiteWorkspaceHelper(blockSuiteWorkspace)
|
||||||
|
);
|
||||||
|
await helperHook.result.current.markMilestone('test');
|
||||||
|
expect(blockSuiteWorkspace.meta.pageMetas.length).toBe(3);
|
||||||
|
handleNewPage(helperHook.result.current.createPage('page4'));
|
||||||
|
expect(blockSuiteWorkspace.meta.pageMetas.length).toBe(4);
|
||||||
|
expect(await helperHook.result.current.listMilestone()).toHaveProperty(
|
||||||
|
'test'
|
||||||
|
);
|
||||||
|
await helperHook.result.current.revertMilestone('test');
|
||||||
|
expect(blockSuiteWorkspace.meta.pageMetas.length).toBe(3);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
import type { Page } from '@blocksuite/store';
|
import type { Page } from '@blocksuite/store';
|
||||||
import { assertExists } from '@blocksuite/store';
|
import { assertExists } from '@blocksuite/store';
|
||||||
|
import {
|
||||||
|
getMilestones,
|
||||||
|
markMilestone,
|
||||||
|
revertUpdate,
|
||||||
|
} from '@toeverything/y-indexeddb';
|
||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
|
|
||||||
import type { BlockSuiteWorkspace } from '../shared';
|
import type { BlockSuiteWorkspace } from '../shared';
|
||||||
@ -13,6 +18,27 @@ export function useBlockSuiteWorkspaceHelper(
|
|||||||
assertExists(blockSuiteWorkspace);
|
assertExists(blockSuiteWorkspace);
|
||||||
return blockSuiteWorkspace.createPage(pageId, parentId);
|
return blockSuiteWorkspace.createPage(pageId, parentId);
|
||||||
},
|
},
|
||||||
|
markMilestone: async (name: string) => {
|
||||||
|
assertExists(blockSuiteWorkspace);
|
||||||
|
const doc = blockSuiteWorkspace.doc;
|
||||||
|
await markMilestone(blockSuiteWorkspace.id, doc, name);
|
||||||
|
},
|
||||||
|
revertMilestone: async (name: string) => {
|
||||||
|
assertExists(blockSuiteWorkspace);
|
||||||
|
const doc = blockSuiteWorkspace.doc;
|
||||||
|
const list = await getMilestones(blockSuiteWorkspace.id);
|
||||||
|
if (!list) {
|
||||||
|
throw new Error('no milestone');
|
||||||
|
}
|
||||||
|
const milestone = list[name];
|
||||||
|
if (milestone) {
|
||||||
|
revertUpdate(doc, milestone, () => 'Map');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
listMilestone: async () => {
|
||||||
|
assertExists(blockSuiteWorkspace);
|
||||||
|
return await getMilestones(blockSuiteWorkspace.id);
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
[blockSuiteWorkspace]
|
[blockSuiteWorkspace]
|
||||||
);
|
);
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { DebugLogger } from '@affine/debug';
|
||||||
import { config } from '@affine/env';
|
import { config } from '@affine/env';
|
||||||
import {
|
import {
|
||||||
createUserApis,
|
createUserApis,
|
||||||
@ -30,6 +31,8 @@ const affineApis = {} as ReturnType<typeof createUserApis> &
|
|||||||
Object.assign(affineApis, createUserApis(prefixUrl));
|
Object.assign(affineApis, createUserApis(prefixUrl));
|
||||||
Object.assign(affineApis, createWorkspaceApis(prefixUrl));
|
Object.assign(affineApis, createWorkspaceApis(prefixUrl));
|
||||||
|
|
||||||
|
const debugLogger = new DebugLogger('affine-debug-apis');
|
||||||
|
|
||||||
if (!globalThis.AFFINE_APIS) {
|
if (!globalThis.AFFINE_APIS) {
|
||||||
globalThis.AFFINE_APIS = affineApis;
|
globalThis.AFFINE_APIS = affineApis;
|
||||||
globalThis.setLogin = (response: LoginResponse) => {
|
globalThis.setLogin = (response: LoginResponse) => {
|
||||||
|
@ -179,10 +179,9 @@ describe('milestone', () => {
|
|||||||
const map = snapshot.getMap('map');
|
const map = snapshot.getMap('map');
|
||||||
expect(map.get('1')).toBe(1);
|
expect(map.get('1')).toBe(1);
|
||||||
}
|
}
|
||||||
revertUpdate(doc, milestones.test1, {
|
revertUpdate(doc, milestones.test1, key =>
|
||||||
map: 'Map',
|
key === 'map' ? 'Map' : 'Array'
|
||||||
array: 'Array',
|
);
|
||||||
});
|
|
||||||
{
|
{
|
||||||
const map = doc.getMap('map');
|
const map = doc.getMap('map');
|
||||||
expect(map.get('1')).toBe(1);
|
expect(map.get('1')).toBe(1);
|
||||||
@ -207,10 +206,9 @@ describe('milestone', () => {
|
|||||||
const doc2 = new Doc();
|
const doc2 = new Doc();
|
||||||
applyUpdate(doc2, encodeStateAsUpdate(doc));
|
applyUpdate(doc2, encodeStateAsUpdate(doc));
|
||||||
|
|
||||||
revertUpdate(doc2, milestones.test1, {
|
revertUpdate(doc2, milestones.test1, key =>
|
||||||
map: 'Map',
|
key === 'map' ? 'Map' : 'Array'
|
||||||
array: 'Array',
|
);
|
||||||
});
|
|
||||||
{
|
{
|
||||||
const map = doc2.getMap('map');
|
const map = doc2.getMap('map');
|
||||||
expect(map.get('1')).toBe(1);
|
expect(map.get('1')).toBe(1);
|
||||||
|
@ -15,12 +15,10 @@ const snapshotOrigin = Symbol('snapshot-origin');
|
|||||||
|
|
||||||
let mergeCount = 500;
|
let mergeCount = 500;
|
||||||
|
|
||||||
type Metadata = Record<string, 'Text' | 'Map' | 'Array'>;
|
|
||||||
|
|
||||||
export function revertUpdate(
|
export function revertUpdate(
|
||||||
doc: Doc,
|
doc: Doc,
|
||||||
snapshotUpdate: Uint8Array,
|
snapshotUpdate: Uint8Array,
|
||||||
metadata: Metadata
|
getMetadata: (key: string) => 'Text' | 'Map' | 'Array'
|
||||||
) {
|
) {
|
||||||
const snapshotDoc = new Doc();
|
const snapshotDoc = new Doc();
|
||||||
applyUpdate(snapshotDoc, snapshotUpdate, snapshotOrigin);
|
applyUpdate(snapshotDoc, snapshotUpdate, snapshotOrigin);
|
||||||
@ -34,11 +32,12 @@ export function revertUpdate(
|
|||||||
);
|
);
|
||||||
const undoManager = new UndoManager(
|
const undoManager = new UndoManager(
|
||||||
[...snapshotDoc.share.keys()].map(key => {
|
[...snapshotDoc.share.keys()].map(key => {
|
||||||
if (metadata[key] === 'Text') {
|
const type = getMetadata(key);
|
||||||
|
if (type === 'Text') {
|
||||||
return snapshotDoc.getText(key);
|
return snapshotDoc.getText(key);
|
||||||
} else if (metadata[key] === 'Map') {
|
} else if (type === 'Map') {
|
||||||
return snapshotDoc.getMap(key);
|
return snapshotDoc.getMap(key);
|
||||||
} else if (metadata[key] === 'Array') {
|
} else if (type === 'Array') {
|
||||||
return snapshotDoc.getArray(key);
|
return snapshotDoc.getArray(key);
|
||||||
}
|
}
|
||||||
throw new Error('Unknown type');
|
throw new Error('Unknown type');
|
||||||
|
Loading…
Reference in New Issue
Block a user