Skip to content

Commit

Permalink
feat(core): PDF preview
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Oct 28, 2024
1 parent 81029db commit 162958f
Show file tree
Hide file tree
Showing 21 changed files with 1,201 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/frontend/apps/android/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@affine/core": "workspace:*",
"@affine/i18n": "workspace:*",
"@blocksuite/affine": "0.0.0-canary-20241028080828",
"@blocksuite/icons": "^2.1.67",
"@blocksuite/icons": "^2.1.69",
"@capacitor/android": "^6.1.2",
"@capacitor/core": "^6.1.2",
"@sentry/react": "^8.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export const workbenchViewIconNameSchema = z.enum([
'page',
'edgeless',
'journal',
'attachment',
'pdf',
]);

export const workbenchViewMetaSchema = z.object({
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/apps/ios/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@affine/core": "workspace:*",
"@affine/i18n": "workspace:*",
"@blocksuite/affine": "0.0.0-canary-20241028080828",
"@blocksuite/icons": "^2.1.67",
"@blocksuite/icons": "^2.1.69",
"@capacitor/app": "^6.0.1",
"@capacitor/browser": "^6.0.3",
"@capacitor/core": "^6.1.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/apps/mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@affine/core": "workspace:*",
"@affine/i18n": "workspace:*",
"@blocksuite/affine": "0.0.0-canary-20241028080828",
"@blocksuite/icons": "^2.1.67",
"@blocksuite/icons": "^2.1.69",
"@sentry/react": "^8.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
8 changes: 6 additions & 2 deletions packages/frontend/component/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"peerDependencies": {
"@blocksuite/affine": "*",
"@blocksuite/icons": "2.1.67"
"@blocksuite/icons": "*"
},
"dependencies": {
"@affine/cli": "workspace:*",
Expand All @@ -24,6 +24,7 @@
"@affine/i18n": "workspace:*",
"@atlaskit/pragmatic-drag-and-drop": "^1.2.1",
"@atlaskit/pragmatic-drag-and-drop-hitbox": "^1.0.3",
"@blocksuite/icons": "2.1.69",
"@emotion/react": "^11.11.4",
"@emotion/styled": "^11.11.5",
"@radix-ui/react-avatar": "^1.0.4",
Expand All @@ -40,11 +41,14 @@
"@radix-ui/react-toast": "^1.1.5",
"@radix-ui/react-tooltip": "^1.0.7",
"@radix-ui/react-visually-hidden": "^1.1.0",
"@toeverything/pdf-viewer": "^0.1.0",
"@toeverything/theme": "^1.0.11",
"@vanilla-extract/dynamic": "^2.1.0",
"check-password-strength": "^2.0.10",
"clsx": "^2.1.0",
"dayjs": "^1.11.10",
"file-type": "^19.1.0",
"filesize": "^10.1.6",
"jotai": "^2.8.0",
"lit": "^3.1.2",
"lodash-es": "^4.17.21",
Expand All @@ -57,13 +61,13 @@
"react-paginate": "^8.2.0",
"react-router-dom": "^6.22.3",
"react-transition-state": "^2.1.1",
"react-virtuoso": "^4.7.8",
"sonner": "^1.4.41",
"swr": "^2.2.5",
"zod": "^3.22.4"
},
"devDependencies": {
"@blocksuite/affine": "0.0.0-canary-20241028080828",
"@blocksuite/icons": "2.1.69",
"@chromatic-com/storybook": "^3.0.0",
"@storybook/addon-essentials": "^8.2.9",
"@storybook/addon-interactions": "^8.2.9",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { AttachmentBlockModel } from '@blocksuite/blocks';
import { ArrowDownBigIcon, PageIcon } from '@blocksuite/icons/rc';
import clsx from 'clsx';

import { Button } from '../../ui/button';
import * as styles from './styles.css';

interface ErrorProps {
model: AttachmentBlockModel;
ext: string;
isPDF: boolean;
}

export const Error = ({ ext }: ErrorProps) => {
return (
<div className={clsx([styles.body, styles.error])}>
<PageIcon />
<h3 className={styles.errorTitle}>Unable to preview this file</h3>
<p className={styles.errorMessage}>.{ext} file type not supported.</p>
<div className={styles.errorBtns}>
<Button variant="primary" prefix={<ArrowDownBigIcon />}>
Download
</Button>
<Button>Retry</Button>
</div>
</div>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { AttachmentBlockModel } from '@blocksuite/blocks';
import { filesize } from 'filesize';
import { useMemo } from 'react';

import { Error } from './error';
import * as styles from './styles.css';
import { Titlebar } from './titlebar';
import { Viewer } from './viewer';

export type AttachmentViewerProps = {
model: AttachmentBlockModel;
};

export const AttachmentViewer = ({ model }: AttachmentViewerProps) => {
const props = useMemo(() => {
const pieces = model.name.split('.');
const ext = pieces.pop() || '';
const name = pieces.join('.');
const isPDF = ext === 'pdf';
const size = filesize(model.size);
return { model, name, ext, size, isPDF };
}, [model]);

return (
<div className={styles.viewerContainer}>
<Titlebar {...props} />
{props.isPDF ? <Viewer {...props} /> : <Error {...props} />}
</div>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import { cssVarV2 } from '@toeverything/theme/v2';
import { style } from '@vanilla-extract/css';

export const viewerContainer = style({
position: 'relative',
display: 'flex',
flexDirection: 'column',
width: '100%',
height: '100%',
});

export const titlebar = style({
display: 'flex',
justifyContent: 'space-between',
height: '52px',
padding: '10px 8px',
background: cssVarV2('layer/background/primary'),
fontSize: '12px',
fontWeight: 400,
color: cssVarV2('text/secondary'),
borderTopWidth: '0.5px',
borderTopStyle: 'solid',
borderTopColor: cssVarV2('layer/insideBorder/border'),
});

export const titlebarChild = style({
selectors: {
[`${titlebar} > &`]: {
display: 'flex',
gap: '12px',
alignItems: 'center',
paddingLeft: '12px',
paddingRight: '12px',
},
'&.zoom:not(.show)': {
display: 'none',
},
},
});

export const titlebarName = style({
display: 'flex',
});

export const body = style({
display: 'flex',
flex: 1,
position: 'relative',
selectors: {
'&:before': {
position: 'absolute',
// position: 'fixed',
content: '',
top: 0,
right: 0,
bottom: 0,
left: 0,
// width: '100%',
// height: '100%',
// zIndex: -1,
},
'&:not(.gridding):before': {
backgroundColor: cssVarV2('layer/background/secondary'),
},
'&.gridding:before': {
opacity: 0.25,
backgroundSize: '20px 20px',
backgroundImage: `linear-gradient(${cssVarV2('button/grabber/default')} 1px, transparent 1px), linear-gradient(to right, ${cssVarV2('button/grabber/default')} 1px, transparent 1px)`,
},
},
});

export const virtuoso = style({
width: '100%',
});

export const error = style({
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
gap: '4px',
});

export const errorTitle = style({
fontSize: '15px',
fontWeight: 500,
lineHeight: '24px',
color: cssVarV2('text/primary'),
marginTop: '12px',
});

export const errorMessage = style({
fontSize: '12px',
fontWeight: 500,
lineHeight: '20px',
color: cssVarV2('text/tertiary'),
});

export const errorBtns = style({
display: 'flex',
flexDirection: 'column',
gap: '10px',
marginTop: '28px',
});

export const viewerPage = style({
margin: '20px auto',
maxWidth: 'calc(100% - 40px)',
background: cssVarV2('layer/white'),
boxSizing: 'border-box',
borderWidth: '1px',
borderStyle: 'solid',
borderColor: cssVarV2('layer/insideBorder/border'),
boxShadow:
'0px 4px 20px 0px var(--transparent-black-200, rgba(0, 0, 0, 0.10))',
});

export const thumbnails = style({
position: 'absolute',
boxSizing: 'border-box',
width: '120px',
padding: '12px 0',
right: '30px',
bottom: '30px',
borderRadius: '8px',
borderWidth: '1px',
borderStyle: 'solid',
borderColor: cssVarV2('layer/insideBorder/border'),
backgroundColor: cssVarV2('layer/background/primary'),
boxShadow: cssVarV2('shadow/overlayPanelShadow/2-color'),
fontSize: '12px',
fontWeight: 500,
lineHeight: '20px',
color: cssVarV2('text/secondary'),
});

export const thumbnailsPages = style({
display: 'flex',
flexDirection: 'column',
// gap: '12px',
selectors: {
'&.collapsed': {
display: 'none',
},
'&:not(.collapsed)': {
marginBottom: '8px',
},
},
});

export const thumbnailsPage = style({
margin: '0px 12px 12px',
display: 'flex',
overflow: 'clip',
// width: '100%',
borderRadius: '4px',
borderWidth: '1px',
borderStyle: 'solid',
borderColor: cssVarV2('layer/insideBorder/border'),
selectors: {
'&.selected': {
borderColor: '#29A3FA',
},
},
});

export const thumbnailsIndicator = style({
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
padding: '0 12px',
});
Loading

0 comments on commit 162958f

Please sign in to comment.