mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 12:28:42 +00:00
chore: remove unused y-indexeddb (#6728)
This commit is contained in:
@@ -7,10 +7,15 @@ import { listHistoryQuery, recoverDocMutation } from '@affine/graphql';
|
||||
import { assertEquals } from '@blocksuite/global/utils';
|
||||
import { DocCollection } from '@blocksuite/store';
|
||||
import { globalBlockSuiteSchema } from '@toeverything/infra';
|
||||
import { revertUpdate } from '@toeverything/y-indexeddb';
|
||||
import { useEffect, useMemo } from 'react';
|
||||
import useSWRImmutable from 'swr/immutable';
|
||||
import { applyUpdate, encodeStateAsUpdate } from 'yjs';
|
||||
import {
|
||||
applyUpdate,
|
||||
Doc as YDoc,
|
||||
encodeStateAsUpdate,
|
||||
encodeStateVector,
|
||||
UndoManager,
|
||||
} from 'yjs';
|
||||
|
||||
import {
|
||||
useMutateQueryResource,
|
||||
@@ -180,6 +185,43 @@ export const historyListGroupByDay = (histories: DocHistory[]) => {
|
||||
return [...map.entries()];
|
||||
};
|
||||
|
||||
export function revertUpdate(
|
||||
doc: YDoc,
|
||||
snapshotUpdate: Uint8Array,
|
||||
getMetadata: (key: string) => 'Text' | 'Map' | 'Array'
|
||||
) {
|
||||
const snapshotDoc = new YDoc();
|
||||
applyUpdate(snapshotDoc, snapshotUpdate);
|
||||
|
||||
const currentStateVector = encodeStateVector(doc);
|
||||
const snapshotStateVector = encodeStateVector(snapshotDoc);
|
||||
|
||||
const changesSinceSnapshotUpdate = encodeStateAsUpdate(
|
||||
doc,
|
||||
snapshotStateVector
|
||||
);
|
||||
const undoManager = new UndoManager(
|
||||
[...snapshotDoc.share.keys()].map(key => {
|
||||
const type = getMetadata(key);
|
||||
if (type === 'Text') {
|
||||
return snapshotDoc.getText(key);
|
||||
} else if (type === 'Map') {
|
||||
return snapshotDoc.getMap(key);
|
||||
} else if (type === 'Array') {
|
||||
return snapshotDoc.getArray(key);
|
||||
}
|
||||
throw new Error('Unknown type');
|
||||
})
|
||||
);
|
||||
applyUpdate(snapshotDoc, changesSinceSnapshotUpdate);
|
||||
undoManager.undo();
|
||||
const revertChangesSinceSnapshotUpdate = encodeStateAsUpdate(
|
||||
snapshotDoc,
|
||||
currentStateVector
|
||||
);
|
||||
applyUpdate(doc, revertChangesSinceSnapshotUpdate);
|
||||
}
|
||||
|
||||
export const useRestorePage = (
|
||||
docCollection: DocCollection,
|
||||
pageId: string
|
||||
|
||||
Reference in New Issue
Block a user