Skip to content

Commit

Permalink
feat: support opening in peek view
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Oct 22, 2024
1 parent 953aa36 commit dbd1cd7
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const viewerContainer = style({
display: 'flex',
flexDirection: 'column',
width: '100%',
height: '100%',
});

export const titlebar = style({
Expand Down Expand Up @@ -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',
},
},
});

Expand Down
20 changes: 20 additions & 0 deletions packages/frontend/core/src/modules/peek-view/entities/peek-view.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { BlockComponent, EditorHost } from '@blocksuite/affine/block-std';
import type {
AttachmentBlockModel,
DocMode,
EmbedLinkedDocModel,
EmbedSyncedDocModel,
Expand Down Expand Up @@ -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;
Expand All @@ -61,6 +68,7 @@ export type ActivePeekView = {
info:
| DocPeekViewInfo
| ImagePeekViewInfo
| AttachmentPeekViewInfo
| CustomTemplatePeekViewInfo
| AIChatBlockPeekViewInfo;
};
Expand All @@ -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 => {
Expand Down Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
@@ -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 (
<ErrorBoundary fallbackRender={ErrorLogger}>{props.children}</ErrorBoundary>
);
};

export type AttachmentPreviewModalProps = {
docId: string;
blockId: string;
};

export const AttachmentPreviewPeekView = (
props: AttachmentPreviewModalProps
) => {
const [blockId, setBlockId] = useState<string | null>(props.blockId);

useEffect(() => {
setBlockId(props.blockId);
}, [props.blockId]);

return (
<AttachmentPreviewErrorBoundary>
<Suspense>{blockId ? <AttachmentViewer /> : null}</Suspense>
</AttachmentPreviewErrorBoundary>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -33,6 +34,15 @@ function renderPeekView({ info }: ActivePeekView) {
);
}

if (info.type === 'attachment') {
return (
<AttachmentPreviewPeekView
docId={info.docId}
blockId={info.blockIds[0]}
/>
);
}

if (info.type === 'image') {
return (
<ImagePreviewPeekView docId={info.docId} blockId={info.blockIds[0]} />
Expand All @@ -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
}
Expand Down

0 comments on commit dbd1cd7

Please sign in to comment.