mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 04:18:54 +00:00
feat: add page preview (#2620)
This commit is contained in:
@@ -13,6 +13,7 @@ import { useAFFiNEI18N } from '@affine/i18n/hooks';
|
|||||||
import { EdgelessIcon, PageIcon } from '@blocksuite/icons';
|
import { EdgelessIcon, PageIcon } from '@blocksuite/icons';
|
||||||
import type { PageMeta } from '@blocksuite/store';
|
import type { PageMeta } from '@blocksuite/store';
|
||||||
import { useBlockSuitePageMeta } from '@toeverything/hooks/use-block-suite-page-meta';
|
import { useBlockSuitePageMeta } from '@toeverything/hooks/use-block-suite-page-meta';
|
||||||
|
import { getPagePreviewText } from '@toeverything/hooks/use-block-suite-page-preview';
|
||||||
import { useAtom } from 'jotai';
|
import { useAtom } from 'jotai';
|
||||||
import type React from 'react';
|
import type React from 'react';
|
||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
@@ -123,6 +124,8 @@ export const BlockSuitePageList: React.FC<BlockSuitePageListProps> = ({
|
|||||||
|
|
||||||
if (listType === 'trash') {
|
if (listType === 'trash') {
|
||||||
const pageList: TrashListData[] = list.map(pageMeta => {
|
const pageList: TrashListData[] = list.map(pageMeta => {
|
||||||
|
const page = blockSuiteWorkspace.getPage(pageMeta.id);
|
||||||
|
const preview = page ? getPagePreviewText(page) : undefined;
|
||||||
return {
|
return {
|
||||||
icon: isPreferredEdgeless(pageMeta.id) ? (
|
icon: isPreferredEdgeless(pageMeta.id) ? (
|
||||||
<EdgelessIcon />
|
<EdgelessIcon />
|
||||||
@@ -131,6 +134,7 @@ export const BlockSuitePageList: React.FC<BlockSuitePageListProps> = ({
|
|||||||
),
|
),
|
||||||
pageId: pageMeta.id,
|
pageId: pageMeta.id,
|
||||||
title: pageMeta.title,
|
title: pageMeta.title,
|
||||||
|
preview,
|
||||||
createDate: new Date(pageMeta.createDate),
|
createDate: new Date(pageMeta.createDate),
|
||||||
trashDate: pageMeta.trashDate
|
trashDate: pageMeta.trashDate
|
||||||
? new Date(pageMeta.trashDate)
|
? new Date(pageMeta.trashDate)
|
||||||
@@ -153,10 +157,13 @@ export const BlockSuitePageList: React.FC<BlockSuitePageListProps> = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const pageList: ListData[] = list.map(pageMeta => {
|
const pageList: ListData[] = list.map(pageMeta => {
|
||||||
|
const page = blockSuiteWorkspace.getPage(pageMeta.id);
|
||||||
|
const preview = page ? getPagePreviewText(page) : undefined;
|
||||||
return {
|
return {
|
||||||
icon: isPreferredEdgeless(pageMeta.id) ? <EdgelessIcon /> : <PageIcon />,
|
icon: isPreferredEdgeless(pageMeta.id) ? <EdgelessIcon /> : <PageIcon />,
|
||||||
pageId: pageMeta.id,
|
pageId: pageMeta.id,
|
||||||
title: pageMeta.title,
|
title: pageMeta.title,
|
||||||
|
preview,
|
||||||
favorite: !!pageMeta.favorite,
|
favorite: !!pageMeta.favorite,
|
||||||
isPublicPage: !!pageMeta.isPublic,
|
isPublicPage: !!pageMeta.isPublic,
|
||||||
createDate: new Date(pageMeta.createDate),
|
createDate: new Date(pageMeta.createDate),
|
||||||
|
|||||||
@@ -5,11 +5,15 @@ import { atom } from 'jotai';
|
|||||||
|
|
||||||
const weakMap = new WeakMap<Page, Atom<string>>();
|
const weakMap = new WeakMap<Page, Atom<string>>();
|
||||||
|
|
||||||
const getPagePreviewText = (page: Page) => {
|
export const getPagePreviewText = (page: Page) => {
|
||||||
|
// TODO this is incorrect, since the order of blocks is not guaranteed
|
||||||
const paragraphBlocks = page.getBlockByFlavour(
|
const paragraphBlocks = page.getBlockByFlavour(
|
||||||
'affine:paragraph'
|
'affine:paragraph'
|
||||||
) as ParagraphBlockModel[];
|
) as ParagraphBlockModel[];
|
||||||
const text = paragraphBlocks.map(block => block.text.toString()).join('\n');
|
const text = paragraphBlocks
|
||||||
|
.slice(0, 10)
|
||||||
|
.map(block => block.text.toString())
|
||||||
|
.join('\n');
|
||||||
return text.slice(0, 30);
|
return text.slice(0, 30);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user