feat: support upload page icon (#59)

This commit is contained in:
Kilu.He
2025-03-05 14:46:22 +08:00
committed by GitHub
parent 2c322d1dcc
commit 8ac85ebab5
10 changed files with 149 additions and 41 deletions

View File

@@ -996,6 +996,7 @@ export interface ViewMetaProps {
extra?: ViewExtra | null; extra?: ViewExtra | null;
readOnly?: boolean; readOnly?: boolean;
updatePage?: (viewId: string, data: UpdatePagePayload) => Promise<void>; updatePage?: (viewId: string, data: UpdatePagePayload) => Promise<void>;
uploadFile?: (file: File) => Promise<string>;
onEnter?: (text: string) => void; onEnter?: (text: string) => void;
maxWidth?: number; maxWidth?: number;
} }

View File

@@ -0,0 +1,26 @@
export default function LoadingDots({
className,
colors = ['#00b5ff', '#e3006d', '#f7931e'],
}: {
className?: string;
colors?: [string, string, string];
}) {
return (
<div className={className}>
<div
style={{
width: `30px`,
aspectRatio: '2',
background: `
radial-gradient(circle closest-side, ${colors[0]} 90%, transparent) 0% 50%,
radial-gradient(circle closest-side, ${colors[1]} 90%, transparent) 50% 50%,
radial-gradient(circle closest-side, ${colors[2]} 90%, transparent) 100% 50%
`,
backgroundSize: 'calc(100%/3) 50%',
backgroundRepeat: 'no-repeat',
animation: 'dots-loading 1s infinite linear',
}}
/>
</div>
);
}

View File

@@ -1,4 +1,5 @@
import FileDropzone from '@/components/_shared/file-dropzone/FileDropzone'; import FileDropzone from '@/components/_shared/file-dropzone/FileDropzone';
import LoadingDots from '@/components/_shared/LoadingDots';
import { notify } from '@/components/_shared/notify'; import { notify } from '@/components/_shared/notify';
import React, { useCallback } from 'react'; import React, { useCallback } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
@@ -10,7 +11,9 @@ export function UploadImage({ onDone, uploadAction }: {
uploadAction?: (file: File) => Promise<string> uploadAction?: (file: File) => Promise<string>
}) { }) {
const { t } = useTranslation(); const { t } = useTranslation();
const [loading, setLoading] = React.useState(false);
const handleFileChange = useCallback(async(files: File[]) => { const handleFileChange = useCallback(async(files: File[]) => {
setLoading(true);
const file = files[0]; const file = files[0];
if(!file) return; if(!file) return;
@@ -28,6 +31,8 @@ export function UploadImage({ onDone, uploadAction }: {
} catch(e: any) { } catch(e: any) {
notify.error(e.message); notify.error(e.message);
onDone?.(URL.createObjectURL(file)); onDone?.(URL.createObjectURL(file));
} finally {
setLoading(false);
} }
}, [onDone, uploadAction]); }, [onDone, uploadAction]);
@@ -39,6 +44,10 @@ export function UploadImage({ onDone, uploadAction }: {
onChange={handleFileChange} onChange={handleFileChange}
accept={ALLOWED_IMAGE_EXTENSIONS.join(',')} accept={ALLOWED_IMAGE_EXTENSIONS.join(',')}
/> />
{loading &&
<div className={'absolute bg-bg-body z-10 opacity-90 flex items-center inset-0 justify-center w-full h-full'}>
<LoadingDots />
</div>}
</div> </div>
); );

View File

@@ -1,6 +1,7 @@
import { ViewIconType } from '@/application/types'; import { ViewIconType } from '@/application/types';
import { EmojiPicker } from '@/components/_shared/emoji-picker'; import { EmojiPicker } from '@/components/_shared/emoji-picker';
import IconPicker from '@/components/_shared/icon-picker/IconPicker'; import IconPicker from '@/components/_shared/icon-picker/IconPicker';
import { UploadImage } from '@/components/_shared/image-upload';
import { Popover } from '@/components/_shared/popover'; import { Popover } from '@/components/_shared/popover';
import { TabPanel, ViewTab, ViewTabs } from '@/components/_shared/tabs/ViewTabs'; import { TabPanel, ViewTab, ViewTabs } from '@/components/_shared/tabs/ViewTabs';
import { Button } from '@mui/material'; import { Button } from '@mui/material';
@@ -20,16 +21,20 @@ function ChangeIconPopover ({
removeIcon, removeIcon,
anchorPosition, anchorPosition,
hideRemove, hideRemove,
uploadEnabled,
onUploadFile,
}: { }: {
open: boolean, open: boolean,
anchorEl?: HTMLElement | null, anchorEl?: HTMLElement | null,
anchorPosition?: PopoverProps['anchorPosition'], anchorPosition?: PopoverProps['anchorPosition'],
onClose: () => void, onClose: () => void,
defaultType: 'emoji' | 'icon', defaultType: 'emoji' | 'icon' | 'upload',
emojiEnabled?: boolean, emojiEnabled?: boolean,
uploadEnabled?: boolean,
iconEnabled?: boolean, iconEnabled?: boolean,
popoverProps?: Partial<PopoverProps>, popoverProps?: Partial<PopoverProps>,
onSelectIcon?: (icon: { ty: ViewIconType, value: string, color?: string, content?: string }) => void, onSelectIcon?: (icon: { ty: ViewIconType, value: string, color?: string, content?: string }) => void,
onUploadFile?: (file: File) => Promise<string>,
removeIcon?: () => void, removeIcon?: () => void,
hideRemove?: boolean, hideRemove?: boolean,
}) { }) {
@@ -80,6 +85,16 @@ function ChangeIconPopover ({
/> />
) )
} }
{
uploadEnabled && (
<ViewTab
className={'flex items-center flex-row justify-center gap-1.5'}
value={'upload'}
label={'Upload'}
data-testid="upload-tab"
/>
)
}
</ViewTabs> </ViewTabs>
{!hideRemove && <Button {!hideRemove && <Button
@@ -126,6 +141,24 @@ function ChangeIconPopover ({
hideRemove hideRemove
/> />
</TabPanel>} </TabPanel>}
{uploadEnabled && <TabPanel
index={'upload'}
value={value}
>
<div className={'pt-4 relative pb-2'}>
<UploadImage
onDone={(url) => {
onSelectIcon?.({
ty: ViewIconType.URL,
value: url,
});
handleClose();
}}
uploadAction={onUploadFile}
/>
</div>
</TabPanel>}
</Popover> </Popover>
); );
} }

View File

@@ -16,7 +16,7 @@ import React, { Suspense, useCallback, useMemo } from 'react';
import { useSearchParams } from 'react-router-dom'; import { useSearchParams } from 'react-router-dom';
import ViewMetaPreview from 'src/components/view-meta/ViewMetaPreview'; import ViewMetaPreview from 'src/components/view-meta/ViewMetaPreview';
function DatabaseView ({ viewMeta, ...props }: ViewComponentProps) { function DatabaseView({ viewMeta, uploadFile, ...props }: ViewComponentProps) {
const [search, setSearch] = useSearchParams(); const [search, setSearch] = useSearchParams();
const outline = useAppOutline(); const outline = useAppOutline();
const iidIndex = viewMeta.viewId; const iidIndex = viewMeta.viewId;
@@ -87,6 +87,7 @@ function DatabaseView ({ viewMeta, ...props }: ViewComponentProps) {
{...viewMeta} {...viewMeta}
readOnly={props.readOnly} readOnly={props.readOnly}
updatePage={props.updatePage} updatePage={props.updatePage}
uploadFile={uploadFile}
/>} />}
<Suspense fallback={skeleton}> <Suspense fallback={skeleton}>

View File

@@ -40,7 +40,7 @@ function ViewItem({ view, width, level = 0, renderExtra, expandIds, toggleExpand
const selectedViewId = useAppViewId(); const selectedViewId = useAppViewId();
const viewId = view.view_id; const viewId = view.view_id;
const selected = selectedViewId === viewId; const selected = selectedViewId === viewId;
const { updatePage } = useAppHandlers(); const { updatePage, uploadFile } = useAppHandlers();
const isExpanded = expandIds.includes(viewId); const isExpanded = expandIds.includes(viewId);
const [hovered, setHovered] = React.useState<boolean>(false); const [hovered, setHovered] = React.useState<boolean>(false);
@@ -57,6 +57,11 @@ function ViewItem({ view, width, level = 0, renderExtra, expandIds, toggleExpand
/></span>; /></span>;
}, [isExpanded, level, toggleExpand, viewId]); }, [isExpanded, level, toggleExpand, viewId]);
const onUploadFile = useCallback(async(file: File) => {
if(!uploadFile) return Promise.reject();
return uploadFile(viewId, file);
}, [uploadFile, viewId]);
const renderItem = useMemo(() => { const renderItem = useMemo(() => {
if(!view) return null; if(!view) return null;
@@ -165,6 +170,8 @@ function ViewItem({ view, width, level = 0, renderExtra, expandIds, toggleExpand
onClose={() => { onClose={() => {
setIconPopoverAnchorEl(null); setIconPopoverAnchorEl(null);
}} }}
uploadEnabled
onUploadFile={onUploadFile}
popoverProps={popoverProps} popoverProps={popoverProps}
onSelectIcon={(icon) => { onSelectIcon={(icon) => {
if(icon.ty === ViewIconType.Icon) { if(icon.ty === ViewIconType.Icon) {

View File

@@ -38,13 +38,28 @@ function MorePageActions({ view, onClose }: {
const { const {
updatePage, updatePage,
uploadFile,
} = useAppHandlers(); } = useAppHandlers();
const { t } = useTranslation(); const { t } = useTranslation();
const handleChangeIcon = useCallback(async (icon: { ty: ViewIconType, value: string }) => { const viewId = view.view_id;
const onUploadFile = useCallback(async(file: File) => {
if(!uploadFile) return Promise.reject();
return uploadFile(viewId, file);
}, [uploadFile, viewId]);
const handleChangeIcon = useCallback(async(icon: { ty: ViewIconType, value: string, color?: string }) => {
try { try {
await updatePage?.(view.view_id, { await updatePage?.(view.view_id, {
icon: icon, icon: icon.ty === ViewIconType.Icon ? {
ty: ViewIconType.Icon,
value: JSON.stringify({
color: icon.color,
groupName: icon.value.split('/')[0],
iconName: icon.value.split('/')[1],
}),
} : icon,
name: view.name, name: view.name,
extra: view.extra || {}, extra: view.extra || {},
}); });
@@ -114,7 +129,7 @@ function MorePageActions({ view, onClose }: {
</Button> </Button>
<Suspense fallback={null}> <Suspense fallback={null}>
<ChangeIconPopover <ChangeIconPopover
iconEnabled={false} iconEnabled
defaultType={'emoji'} defaultType={'emoji'}
open={openIconPopover} open={openIconPopover}
anchorEl={iconPopoverAnchorEl} anchorEl={iconPopoverAnchorEl}
@@ -122,6 +137,8 @@ function MorePageActions({ view, onClose }: {
onClose?.(); onClose?.();
setIconPopoverAnchorEl(null); setIconPopoverAnchorEl(null);
}} }}
onUploadFile={onUploadFile}
uploadEnabled
popoverProps={popoverProps} popoverProps={popoverProps}
onSelectIcon={handleChangeIcon} onSelectIcon={handleChangeIcon}
removeIcon={handleRemoveIcon} removeIcon={handleRemoveIcon}

View File

@@ -24,6 +24,7 @@ export const Document = (props: DocumentProps) => {
updatePage, updatePage,
onRendered, onRendered,
onEditorConnected, onEditorConnected,
uploadFile,
} = props; } = props;
const blockId = search.get('blockId') || undefined; const blockId = search.get('blockId') || undefined;
@@ -76,6 +77,7 @@ export const Document = (props: DocumentProps) => {
updatePage={updatePage} updatePage={updatePage}
onEnter={readOnly ? undefined : handleEnter} onEnter={readOnly ? undefined : handleEnter}
maxWidth={988} maxWidth={988}
uploadFile={uploadFile}
/> />
<Suspense fallback={<EditorSkeleton />}> <Suspense fallback={<EditorSkeleton />}>
<div className={'flex justify-center w-full'}> <div className={'flex justify-center w-full'}>

View File

@@ -15,6 +15,7 @@ function AddIconCover({
setIconAnchorEl, setIconAnchorEl,
maxWidth, maxWidth,
visible, visible,
onUploadFile,
}: { }: {
visible: boolean; visible: boolean;
hasIcon: boolean; hasIcon: boolean;
@@ -24,6 +25,7 @@ function AddIconCover({
iconAnchorEl: HTMLElement | null; iconAnchorEl: HTMLElement | null;
setIconAnchorEl: (el: HTMLElement | null) => void; setIconAnchorEl: (el: HTMLElement | null) => void;
maxWidth?: number; maxWidth?: number;
onUploadFile: (file: File) => Promise<string>;
}) { }) {
const { t } = useTranslation(); const { t } = useTranslation();
@@ -80,6 +82,8 @@ function AddIconCover({
setIconAnchorEl(null); setIconAnchorEl(null);
onUpdateIcon?.({ ty: ViewIconType.Emoji, value: '' }); onUpdateIcon?.({ ty: ViewIconType.Emoji, value: '' });
}} }}
uploadEnabled
onUploadFile={onUploadFile}
/> />
</> </>

View File

@@ -2,7 +2,7 @@ import { CoverType, ViewIconType, ViewLayout, ViewMetaCover, ViewMetaIcon, ViewM
import { notify } from '@/components/_shared/notify'; import { notify } from '@/components/_shared/notify';
import TitleEditable from '@/components/view-meta/TitleEditable'; import TitleEditable from '@/components/view-meta/TitleEditable';
import ViewCover from '@/components/view-meta/ViewCover'; import ViewCover from '@/components/view-meta/ViewCover';
import React, { lazy, Suspense, useEffect, useMemo } from 'react'; import React, { lazy, Suspense, useCallback, useEffect, useMemo } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import PageIcon from '@/components/_shared/view-icon/PageIcon'; import PageIcon from '@/components/_shared/view-icon/PageIcon';
@@ -18,6 +18,7 @@ export function ViewMetaPreview ({
updatePage, updatePage,
onEnter, onEnter,
maxWidth, maxWidth,
uploadFile,
}: ViewMetaProps) { }: ViewMetaProps) {
const [iconAnchorEl, setIconAnchorEl] = React.useState<null | HTMLElement>(null); const [iconAnchorEl, setIconAnchorEl] = React.useState<null | HTMLElement>(null);
const [cover, setCover] = React.useState<ViewMetaCover | null>(coverProp || null); const [cover, setCover] = React.useState<ViewMetaCover | null>(coverProp || null);
@@ -121,6 +122,11 @@ export function ViewMetaPreview ({
} }
}, [extra, icon, name, updatePage, viewId]); }, [extra, icon, name, updatePage, viewId]);
const onUploadFile = useCallback(async(file: File) => {
if(!uploadFile) return Promise.reject();
return uploadFile(file);
}, [uploadFile]);
const ref = React.useRef<HTMLDivElement>(null); const ref = React.useRef<HTMLDivElement>(null);
useEffect(() => { useEffect(() => {
@@ -174,8 +180,10 @@ export function ViewMetaPreview ({
maxWidth={maxWidth} maxWidth={maxWidth}
iconAnchorEl={iconAnchorEl} iconAnchorEl={iconAnchorEl}
setIconAnchorEl={setIconAnchorEl} setIconAnchorEl={setIconAnchorEl}
onUploadFile={onUploadFile}
/></Suspense>} /></Suspense>}
</div> </div>
<div <div
className={`relative mb-6 flex items-center overflow-visible w-full justify-center`} className={`relative mb-6 flex items-center overflow-visible w-full justify-center`}