Skip to content

Commit

Permalink
revert: feat(sync): add blob engine (#6937)
Browse files Browse the repository at this point in the history
  • Loading branch information
fourdim committed May 26, 2024
1 parent 1678f8e commit 0750990
Show file tree
Hide file tree
Showing 60 changed files with 389 additions and 587 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,6 @@
},
"stackblitz": {
"startCommand": "pnpm run dev"
}
},
"packageManager": "pnpm@9.1.1+sha512.14e915759c11f77eac07faba4d019c193ec8637229e62ec99eefb7cf3c3b75c64447882b7c485142451ee3a6b408059cdfb7b7fa0341b975f12d0f7629c71195"
}
8 changes: 4 additions & 4 deletions packages/blocks/src/__tests__/adapters/html.unit.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { BlockSnapshot } from '@blocksuite/store';
import { MemoryBlobCRUD } from '@blocksuite/store';
import { MemoryBlobManager } from '@blocksuite/store';
import { AssetsManager } from '@blocksuite/store';
import { describe, expect, test } from 'vitest';

Expand Down Expand Up @@ -946,10 +946,10 @@ describe('snapshot to html', () => {
);

const htmlAdapter = new HtmlAdapter();
const blobManager = new MemoryBlobCRUD();
const blobManager = new MemoryBlobManager();
await blobManager.set(
'YXXTjRmLlNyiOUnHb8nAIvUP6V7PAXhwW9F5_tc2LGs=',
new Blob()
new Blob(),
'YXXTjRmLlNyiOUnHb8nAIvUP6V7PAXhwW9F5_tc2LGs='
);
const assets = new AssetsManager({ blob: blobManager });

Expand Down
12 changes: 6 additions & 6 deletions packages/blocks/src/__tests__/adapters/markdown.unit.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { BlockSnapshot, SliceSnapshot } from '@blocksuite/store';
import { AssetsManager, MemoryBlobCRUD } from '@blocksuite/store';
import { AssetsManager, MemoryBlobManager } from '@blocksuite/store';
import { describe, expect, test } from 'vitest';

import { MarkdownAdapter } from '../../_common/adapters/markdown.js';
Expand Down Expand Up @@ -1140,12 +1140,12 @@ hhh
'![](assets/YXXTjRmLlNyiOUnHb8nAIvUP6V7PAXhwW9F5_tc2LGs=.blob "aaa")\n\n';

const mdAdapter = new MarkdownAdapter();
const blobCRUD = new MemoryBlobCRUD();
await blobCRUD.set(
'YXXTjRmLlNyiOUnHb8nAIvUP6V7PAXhwW9F5_tc2LGs=',
new Blob()
const blobManager = new MemoryBlobManager();
await blobManager.set(
new Blob(),
'YXXTjRmLlNyiOUnHb8nAIvUP6V7PAXhwW9F5_tc2LGs='
);
const assets = new AssetsManager({ blob: blobCRUD });
const assets = new AssetsManager({ blob: blobManager });

const target = await mdAdapter.fromBlockSnapshot({
snapshot: blockSnapshot,
Expand Down
10 changes: 5 additions & 5 deletions packages/blocks/src/__tests__/adapters/notion-html.unit.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
AssetsManager,
type BlockSnapshot,
MemoryBlobCRUD,
MemoryBlobManager,
} from '@blocksuite/store';
import { describe, expect, test } from 'vitest';

Expand Down Expand Up @@ -1084,7 +1084,7 @@ describe('notion html to snapshot', () => {
const adapter = new NotionHtmlAdapter();
const rawBlockSnapshot = await adapter.toBlockSnapshot({
file: html,
assets: new AssetsManager({ blob: new MemoryBlobCRUD() }),
assets: new AssetsManager({ blob: new MemoryBlobManager() }),
});
expect(nanoidReplacement(rawBlockSnapshot)).toEqual(blockSnapshot);
});
Expand Down Expand Up @@ -1176,9 +1176,9 @@ describe('notion html to snapshot', () => {
};

const adapter = new NotionHtmlAdapter();
const blobCRUD = new MemoryBlobCRUD();
const key = await blobCRUD.set(new File([], 'README.pdf'));
const assestsManager = new AssetsManager({ blob: blobCRUD });
const blobManager = new MemoryBlobManager();
const key = await blobManager.set(new File([], 'README.pdf'));
const assestsManager = new AssetsManager({ blob: blobManager });
await assestsManager.readFromBlob(key);
const rawBlockSnapshot = await adapter.toBlockSnapshot({
file: html,
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/_common/adapters/attachment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { sha } from '@blocksuite/global/utils';
import type { AssetsManager } from '@blocksuite/store';
import {
BaseAdapter,
Expand All @@ -11,6 +10,7 @@ import {
type FromSliceSnapshotPayload,
type FromSliceSnapshotResult,
nanoid,
sha,
type SliceSnapshot,
type ToBlockSnapshotPayload,
type ToDocSnapshotPayload,
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/_common/adapters/html.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { sha } from '@blocksuite/global/utils';
import type { DeltaInsert } from '@blocksuite/inline';
import type {
FromBlockSnapshotPayload,
Expand All @@ -15,6 +14,7 @@ import {
BlockSnapshotSchema,
getAssetName,
nanoid,
sha,
} from '@blocksuite/store';
import { ASTWalker, BaseAdapter } from '@blocksuite/store';
import {
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/_common/adapters/image.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { sha } from '@blocksuite/global/utils';
import type { AssetsManager } from '@blocksuite/store';
import {
BaseAdapter,
Expand All @@ -11,6 +10,7 @@ import {
type FromSliceSnapshotPayload,
type FromSliceSnapshotResult,
nanoid,
sha,
type SliceSnapshot,
type ToBlockSnapshotPayload,
type ToDocSnapshotPayload,
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/_common/adapters/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { sha } from '@blocksuite/global/utils';
import type { DeltaInsert } from '@blocksuite/inline/types';
import type {
FromBlockSnapshotPayload,
Expand All @@ -19,6 +18,7 @@ import {
type DocSnapshot,
getAssetName,
nanoid,
sha,
type SliceSnapshot,
} from '@blocksuite/store';
import { format } from 'date-fns/format';
Expand Down
3 changes: 2 additions & 1 deletion packages/blocks/src/_common/adapters/notion-html.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isEqual, sha } from '@blocksuite/global/utils';
import { isEqual } from '@blocksuite/global/utils';
import type { DeltaInsert } from '@blocksuite/inline';
import type {
FromBlockSnapshotPayload,
Expand All @@ -16,6 +16,7 @@ import {
type DocSnapshot,
getAssetName,
nanoid,
sha,
type SliceSnapshot,
} from '@blocksuite/store';
import rehypeParse from 'rehype-parse';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export class ExportManager {
if (matchFlavours(block, ['affine:image'])) {
if (!block.sourceId) return;

const blob = await block.doc.blobSync.get(block.sourceId);
const blob = await block.doc.blob.get(block.sourceId);
if (!blob) return;

const blobToImage = (blob: Blob) =>
Expand Down
4 changes: 2 additions & 2 deletions packages/blocks/src/_common/transformers/zip.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { assertExists, sha } from '@blocksuite/global/utils';
import { assertExists } from '@blocksuite/global/utils';
import type {
CollectionInfoSnapshot,
Doc,
DocCollection,
DocSnapshot,
JobMiddleware,
} from '@blocksuite/store';
import { extMimeMap, getAssetName, Job } from '@blocksuite/store';
import { extMimeMap, getAssetName, Job, sha } from '@blocksuite/store';
import JSZip from 'jszip';

import { replaceIdMiddleware, titleMiddleware } from './middlewares.js';
Expand Down
25 changes: 25 additions & 0 deletions packages/blocks/src/_common/utils/filesys.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { BlobManager } from '@blocksuite/store';

// Polyfill for `showOpenFilePicker` API
// See https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/wicg-file-system-access/index.d.ts
// See also https://caniuse.com/?search=showOpenFilePicker
Expand Down Expand Up @@ -221,6 +223,29 @@ export async function getImageFilesFromLocal() {
return imageFiles;
}

export async function uploadImageFromLocal(storage: BlobManager) {
const imageFiles = await openFileOrFiles({
acceptType: 'Images',
multiple: true,
});
if (!imageFiles) return [];
return loadImages(imageFiles, storage);
}

export async function loadImages(images: File[], storage: BlobManager) {
const res: { file: File; sourceId: string }[] = [];
for (let i = 0; i < images.length; i++) {
const file = images[i];
const sourceId = await storage.set(file);
res.push({ file, sourceId });
}
const { saveAttachmentData } = withTempBlobData();
res.forEach(({ file, sourceId }) => {
saveAttachmentData(sourceId, { name: file.name });
});
return res;
}

export function downloadBlob(blob: Blob, name: string) {
const dataURL = URL.createObjectURL(blob);
const tmpLink = document.createElement('a');
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/_common/utils/render-linked-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ async function renderImageAbstract(
const sourceId = (image as ImageBlockModel).sourceId;
if (!sourceId) return;

const storage = card.linkedDoc?.blobSync;
const storage = card.linkedDoc?.blob;
if (!storage) return;

const blob = await storage.get(sourceId);
Expand Down
4 changes: 2 additions & 2 deletions packages/blocks/src/attachment-block/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async function uploadAttachmentBlob(

try {
setAttachmentUploading(blockId);
sourceId = await doc.blobSync.set(blob);
sourceId = await doc.blob.set(blob);
} catch (error) {
console.error(error);
if (error instanceof Error) {
Expand Down Expand Up @@ -84,7 +84,7 @@ async function getAttachmentBlob(model: AttachmentBlockModel) {
}

const doc = model.doc;
let blob = await doc.blobSync.get(sourceId);
let blob = await doc.blob.get(sourceId);

if (blob) {
blob = new Blob([blob], { type: model.type });
Expand Down
6 changes: 3 additions & 3 deletions packages/blocks/src/image-block/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export async function uploadBlobForImage(

try {
setImageUploaded(blockId);
sourceId = await doc.blobSync.set(blob);
sourceId = await doc.blob.set(blob);
} catch (error) {
console.error(error);
if (error instanceof Error) {
Expand Down Expand Up @@ -69,7 +69,7 @@ async function getImageBlob(model: ImageBlockModel) {
}

const doc = model.doc;
const blob = await doc.blobSync.get(sourceId);
const blob = await doc.blob.get(sourceId);

if (!blob) {
return null;
Expand Down Expand Up @@ -123,7 +123,7 @@ export async function fetchImageBlob(block: ImageBlockComponent) {
throw new Error('Image sourceId is missing!');
}

const blob = await doc.blobSync.get(sourceId);
const blob = await doc.blob.get(sourceId);
if (!blob) {
throw new Error('Image blob is missing!');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ export class EdgelessRootBlockComponent extends BlockElement<
const uploadPromises = imageFiles.map(async (file, index) => {
const { point, blockId } = dropInfos[index];

const sourceId = await this.doc.blobSync.set(file);
const sourceId = await this.doc.blob.set(file);
const imageSize = await readImageSize(file);

const center = Vec.toVec(point);
Expand Down Expand Up @@ -502,7 +502,7 @@ export class EdgelessRootBlockComponent extends BlockElement<
let sourceId: string | undefined;
try {
setAttachmentUploading(blockId);
sourceId = await this.doc.blobSync.set(file);
sourceId = await this.doc.blob.set(file);
} catch (error) {
console.error(error);
if (error instanceof Error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import '../../../../_common/components/loader.js';

import { WithDisposable } from '@blocksuite/block-std';
import { sha } from '@blocksuite/global/utils';
import { type DocCollection, extMimeMap } from '@blocksuite/store';
import { type DocCollection, extMimeMap, sha } from '@blocksuite/store';
import { Job } from '@blocksuite/store';
import JSZip from 'jszip';
import { html, LitElement, type PropertyValues } from 'lit';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export class MiniMindmapPreview extends WithDisposable(LitElement) {
id: 'MINI_MINDMAP_TEMPORARY',
schema,
idGenerator: Generator.NanoID,
blobStorages: [],
awarenessSources: [],
};

Expand Down
1 change: 0 additions & 1 deletion packages/framework/global/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"!dist/__tests__"
],
"dependencies": {
"lib0": "^0.2.93",
"zod": "^3.23.8"
}
}
1 change: 0 additions & 1 deletion packages/framework/global/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './assert.js';
export * from './crypto.js';
export * from './disposable.js';
export * from './function.js';
export * from './logger.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, test } from 'vitest';
import * as Y from 'yjs';

import { MemoryBlobCRUD } from '../adapter/index.js';
import { MemoryBlobManager } from '../adapter/index.js';
import { Text } from '../reactive/index.js';
import {
type BlockModel,
Expand Down Expand Up @@ -51,8 +51,8 @@ function createTestOptions() {
}

const transformer = new BaseBlockTransformer();
const blobCRUD = new MemoryBlobCRUD();
const assets = new AssetsManager({ blob: blobCRUD });
const blobManager = new MemoryBlobManager();
const assets = new AssetsManager({ blob: blobManager });

test('model to snapshot', () => {
const options = createTestOptions();
Expand Down
Loading

0 comments on commit 0750990

Please sign in to comment.