From dbd1cd71fd9d09eca6cf665df5a118ba42f3a276 Mon Sep 17 00:00:00 2001 From: Fangdun Tsai Date: Wed, 23 Oct 2024 02:25:14 +0800 Subject: [PATCH] feat: support opening in peek view --- .../attachment-viewer/styles.css.ts | 4 +- .../modules/peek-view/entities/peek-view.ts | 20 ++++++++++ .../view/attachment-preview/index.tsx | 37 +++++++++++++++++++ .../peek-view/view/peek-view-manager.tsx | 12 ++++++ 4 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 packages/frontend/core/src/modules/peek-view/view/attachment-preview/index.tsx diff --git a/packages/frontend/component/src/components/attachment-viewer/styles.css.ts b/packages/frontend/component/src/components/attachment-viewer/styles.css.ts index 1f5a3d5789342..a32a53d735094 100644 --- a/packages/frontend/component/src/components/attachment-viewer/styles.css.ts +++ b/packages/frontend/component/src/components/attachment-viewer/styles.css.ts @@ -6,6 +6,7 @@ export const viewerContainer = style({ display: 'flex', flexDirection: 'column', width: '100%', + height: '100%', }); export const titlebar = style({ @@ -61,9 +62,6 @@ export const body = style({ backgroundSize: '20px 20px', backgroundImage: `linear-gradient(${cssVarV2('button/grabber/default')} 1px, transparent 1px), linear-gradient(to right, ${cssVarV2('button/grabber/default')} 1px, transparent 1px)`, }, - '&.scrollable': { - overflowY: 'auto', - }, }, }); diff --git a/packages/frontend/core/src/modules/peek-view/entities/peek-view.ts b/packages/frontend/core/src/modules/peek-view/entities/peek-view.ts index 0f17db7d5b31b..7d7c6d0213b56 100644 --- a/packages/frontend/core/src/modules/peek-view/entities/peek-view.ts +++ b/packages/frontend/core/src/modules/peek-view/entities/peek-view.ts @@ -1,5 +1,6 @@ import type { BlockComponent, EditorHost } from '@blocksuite/affine/block-std'; import type { + AttachmentBlockModel, DocMode, EmbedLinkedDocModel, EmbedSyncedDocModel, @@ -44,6 +45,12 @@ export type ImagePeekViewInfo = { blockIds: [string]; }; +export type AttachmentPeekViewInfo = { + type: 'attachment'; + docId: string; + blockIds: [string]; +}; + export type AIChatBlockPeekViewInfo = { type: 'ai-chat-block'; docId: string; @@ -61,6 +68,7 @@ export type ActivePeekView = { info: | DocPeekViewInfo | ImagePeekViewInfo + | AttachmentPeekViewInfo | CustomTemplatePeekViewInfo | AIChatBlockPeekViewInfo; }; @@ -83,6 +91,12 @@ const isImageBlockModel = ( return blockModel.flavour === 'affine:image'; }; +const isAttachmentBlockModel = ( + blockModel: BlockModel +): blockModel is AttachmentBlockModel => { + return blockModel.flavour === 'affine:attachment'; +}; + const isSurfaceRefModel = ( blockModel: BlockModel ): blockModel is SurfaceRefBlockModel => { @@ -147,6 +161,12 @@ function resolvePeekInfoFromPeekTarget( xywh: refModel.xywh, }; } + } else if (isAttachmentBlockModel(blockModel)) { + return { + type: 'attachment', + docId: blockModel.doc.id, + blockIds: [blockModel.id], + }; } else if (isImageBlockModel(blockModel)) { return { type: 'image', diff --git a/packages/frontend/core/src/modules/peek-view/view/attachment-preview/index.tsx b/packages/frontend/core/src/modules/peek-view/view/attachment-preview/index.tsx new file mode 100644 index 0000000000000..4d553a37b5fc9 --- /dev/null +++ b/packages/frontend/core/src/modules/peek-view/view/attachment-preview/index.tsx @@ -0,0 +1,37 @@ +import { AttachmentViewer } from '@affine/component/attachment-viewer'; +import type { PropsWithChildren } from 'react'; +import { Suspense, useEffect, useState } from 'react'; +import type { FallbackProps } from 'react-error-boundary'; +import { ErrorBoundary } from 'react-error-boundary'; + +const ErrorLogger = (props: FallbackProps) => { + console.error('image preview modal error', props.error); + return null; +}; + +export const AttachmentPreviewErrorBoundary = (props: PropsWithChildren) => { + return ( + {props.children} + ); +}; + +export type AttachmentPreviewModalProps = { + docId: string; + blockId: string; +}; + +export const AttachmentPreviewPeekView = ( + props: AttachmentPreviewModalProps +) => { + const [blockId, setBlockId] = useState(props.blockId); + + useEffect(() => { + setBlockId(props.blockId); + }, [props.blockId]); + + return ( + + {blockId ? : null} + + ); +}; diff --git a/packages/frontend/core/src/modules/peek-view/view/peek-view-manager.tsx b/packages/frontend/core/src/modules/peek-view/view/peek-view-manager.tsx index 5ee69feb7eaf4..b1f6a34d223d9 100644 --- a/packages/frontend/core/src/modules/peek-view/view/peek-view-manager.tsx +++ b/packages/frontend/core/src/modules/peek-view/view/peek-view-manager.tsx @@ -6,6 +6,7 @@ import { useEffect, useMemo } from 'react'; import type { ActivePeekView } from '../entities/peek-view'; import { PeekViewService } from '../services/peek-view'; +import { AttachmentPreviewPeekView } from './attachment-preview'; import { DocPeekPreview } from './doc-preview'; import { ImagePreviewPeekView } from './image-preview'; import { @@ -33,6 +34,15 @@ function renderPeekView({ info }: ActivePeekView) { ); } + if (info.type === 'attachment') { + return ( + + ); + } + if (info.type === 'image') { return ( @@ -59,6 +69,8 @@ const renderControls = ({ info }: ActivePeekView) => { ); } + // TODO(@fundon): attachment's controls + if (info.type === 'image') { return null; // image controls are rendered in the image preview }