chore: merge blocksuite source code (#9213)

This commit is contained in:
Mirone
2024-12-20 15:38:06 +08:00
committed by GitHub
parent 2c9ef916f4
commit 30200ff86d
2031 changed files with 238888 additions and 229 deletions
@@ -0,0 +1,101 @@
import type {
GfxCommonBlockProps,
GfxElementGeometry,
} from '@blocksuite/block-std/gfx';
import { GfxCompatible } from '@blocksuite/block-std/gfx';
import { BlockModel, defineBlockSchema } from '@blocksuite/store';
import type { EmbedCardStyle } from '../../utils/index.js';
import { AttachmentBlockTransformer } from './attachment-transformer.js';
/**
* When the attachment is uploading, the `sourceId` is `undefined`.
* And we can query the upload status by the `isAttachmentLoading` function.
*
* Other collaborators will see an error attachment block when the blob has not finished uploading.
* This issue can be resolve by sync the upload status through the awareness system in the future.
*
* When the attachment is uploaded, the `sourceId` is the id of the blob.
*
* If there are no `sourceId` and the `isAttachmentLoading` function returns `false`,
* it means that the attachment is failed to upload.
*/
/**
* @deprecated
*/
type BackwardCompatibleUndefined = undefined;
export const AttachmentBlockStyles: EmbedCardStyle[] = [
'cubeThick',
'horizontalThin',
'pdf',
] as const;
export type AttachmentBlockProps = {
name: string;
size: number;
/**
* MIME type
*/
type: string;
caption?: string;
// `loadingKey` was used to indicate whether the attachment is loading,
// which is currently unused but no breaking change is needed.
// The `loadingKey` and `sourceId` should not be existed at the same time.
// loadingKey?: string | null;
sourceId?: string;
/**
* Whether to show the attachment as an embed view.
*/
embed: boolean | BackwardCompatibleUndefined;
style?: (typeof AttachmentBlockStyles)[number];
} & Omit<GfxCommonBlockProps, 'scale'>;
export const defaultAttachmentProps: AttachmentBlockProps = {
name: '',
size: 0,
type: 'application/octet-stream',
sourceId: undefined,
caption: undefined,
embed: false,
style: AttachmentBlockStyles[1],
index: 'a0',
xywh: '[0,0,0,0]',
lockedBySelf: false,
rotate: 0,
};
export const AttachmentBlockSchema = defineBlockSchema({
flavour: 'affine:attachment',
props: (): AttachmentBlockProps => defaultAttachmentProps,
metadata: {
version: 1,
role: 'content',
parent: [
'affine:note',
'affine:surface',
'affine:edgeless-text',
'affine:paragraph',
'affine:list',
],
},
transformer: () => new AttachmentBlockTransformer(),
toModel: () => new AttachmentBlockModel(),
});
export class AttachmentBlockModel
extends GfxCompatible<AttachmentBlockProps>(BlockModel)
implements GfxElementGeometry {}
declare global {
namespace BlockSuite {
interface EdgelessBlockModelMap {
'affine:attachment': AttachmentBlockModel;
}
interface BlockModels {
'affine:attachment': AttachmentBlockModel;
}
}
}
@@ -0,0 +1,17 @@
import type { FromSnapshotPayload, SnapshotNode } from '@blocksuite/store';
import { BaseBlockTransformer } from '@blocksuite/store';
import type { AttachmentBlockProps } from './attachment-model.js';
export class AttachmentBlockTransformer extends BaseBlockTransformer<AttachmentBlockProps> {
override async fromSnapshot(
payload: FromSnapshotPayload
): Promise<SnapshotNode<AttachmentBlockProps>> {
const snapshotRet = await super.fromSnapshot(payload);
const sourceId = snapshotRet.props.sourceId;
if (!payload.assets.isEmpty() && sourceId)
await payload.assets.writeToBlob(sourceId);
return snapshotRet;
}
}
@@ -0,0 +1,2 @@
export * from './attachment-model.js';
export * from './attachment-transformer.js';