refactor: using unified nanoid (#4519)

This commit is contained in:
Alex Yang authored and GitHub committed 2023-09-28 02:53:04 +00:00
1 parent 56f75160f3
commit 603f82ffc2
38 files changed
+258 -183

No files matched your search

+5 -5
View File
@@ -55,16 +55,16 @@
},
"dependencies": {
"@affine/sdk": "workspace:*",
"@blocksuite/blocks": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/global": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/store": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/blocks": "0.0.0-20230926212737-6d4b1569-nightly",
"@blocksuite/global": "0.0.0-20230926212737-6d4b1569-nightly",
"@blocksuite/store": "0.0.0-20230926212737-6d4b1569-nightly",
"jotai": "^2.4.1",
"tinykeys": "^2.1.0",
"zod": "^3.22.2"
},
"devDependencies": {
"@blocksuite/editor": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/lit": "0.0.0-20230921103931-38d8f07a-nightly",
"@blocksuite/editor": "0.0.0-20230926212737-6d4b1569-nightly",
"@blocksuite/lit": "0.0.0-20230926212737-6d4b1569-nightly",
"async-call-rpc": "^6.3.1",
"electron": "link:../../apps/electron/node_modules/electron",
"react": "^18.2.0",
@@ -33,7 +33,7 @@ describe('migration', () => {
}
const blocks = doc.getMap('space:hello-world').toJSON();
const newDoc = migrateToSubdoc(doc);
const subDoc = newDoc.getMap('spaces').get('space:hello-world') as Doc;
const subDoc = newDoc.getMap('spaces').values().next().value as Doc;
const data = (subDoc.toJSON() as any).blocks;
Object.keys(data).forEach(id => {
if (id === 'xyWNqindHH') {
+22 -6
View File
@@ -213,6 +213,7 @@ import { applyUpdate, encodeStateAsUpdate } from 'yjs';
const migrationOrigin = 'affine-migration';
import type { Schema } from '@blocksuite/store';
import { nanoid } from 'nanoid';
type XYWH = [number, number, number, number];
@@ -404,7 +405,11 @@ function updateBlockVersions(versions: YMap<number>) {
}
}
function migrateMeta(oldDoc: YDoc, newDoc: YDoc) {
function migrateMeta(
oldDoc: YDoc,
newDoc: YDoc,
idMap: Record<string, string>
) {
const originalMeta = oldDoc.getMap('space:meta');
const originalVersions = originalMeta.get('versions') as YMap<number>;
const originalPages = originalMeta.get('pages') as YArray<YMap<unknown>>;
@@ -423,24 +428,34 @@ function migrateMeta(oldDoc: YDoc, newDoc: YDoc) {
Array.from(page.entries())
.filter(([key]) => key !== 'subpageIds')
.forEach(([key, value]) => {
map.set(key, value);
if (key === 'id') {
idMap[value] = nanoid();
map.set(key, idMap[value]);
} else {
map.set(key, value);
}
});
return map;
});
pages.push(mapList);
}
function migrateBlocks(oldDoc: YDoc, newDoc: YDoc) {
function migrateBlocks(
oldDoc: YDoc,
newDoc: YDoc,
idMap: Record<string, string>
) {
const spaces = newDoc.getMap('spaces');
const originalMeta = oldDoc.getMap('space:meta');
const originalVersions = originalMeta.get('versions') as YMap<number>;
const originalPages = originalMeta.get('pages') as YArray<YMap<unknown>>;
originalPages.forEach(page => {
const id = page.get('id') as string;
const newId = idMap[id];
const spaceId = id.startsWith('space:') ? id : `space:${id}`;
const originalBlocks = oldDoc.getMap(spaceId) as YMap<unknown>;
const subdoc = new YDoc();
spaces.set(spaceId, subdoc);
spaces.set(newId, subdoc);
const blocks = subdoc.getMap('blocks');
Array.from(originalBlocks.entries()).forEach(([key, value]) => {
const blockData = value.clone();
@@ -461,8 +476,9 @@ export function migrateToSubdoc(oldDoc: YDoc): YDoc {
return oldDoc;
}
const newDoc = new YDoc();
migrateMeta(oldDoc, newDoc);
migrateBlocks(oldDoc, newDoc);
const idMap = {} as Record<string, string>;
migrateMeta(oldDoc, newDoc, idMap);
migrateBlocks(oldDoc, newDoc, idMap);
return newDoc;
}
+1
View File
@@ -36,6 +36,7 @@ export default defineConfig({
'zod',
'react',
'yjs',
'nanoid',
/^jotai/,
/^@blocksuite/,
/^@affine\/templates/,