Skip to content

Commit

Permalink
toast
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfeng33 committed Nov 15, 2024
1 parent 302eed8 commit 24f13de
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 110 deletions.
1 change: 1 addition & 0 deletions apps/www/src/lib/plate/create-plate-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
FilePlugin,
ImagePlugin,
MediaEmbedPlugin,
PlaceholderPlugin,
VideoPlugin,
} from '@udecode/plate-media/react';
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import {
FilePlugin,
ImagePlugin,
MediaEmbedPlugin,
PlaceholderPlugin,
VideoPlugin,
} from '@udecode/plate-media/react';

import { ImagePreview } from '@/registry/default/plate-ui/image-preview';
import { MediaFloatingToolbar } from '@/registry/default/plate-ui/media-floating-toolbar';
import { MediaUploadToast } from '@/registry/default/plate-ui/media-upload-toast';

export const mediaPlugins = [
ImagePlugin.extend({
Expand All @@ -28,4 +30,9 @@ export const mediaPlugins = [
CaptionPlugin.configure({
options: { plugins: [ImagePlugin, MediaEmbedPlugin] },
}),
PlaceholderPlugin.configure({
render: {
afterEditable: () => <MediaUploadToast />,
},
}),
] as const;
2 changes: 0 additions & 2 deletions apps/www/src/registry/default/example/playground-demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { Plate, usePlateEditor } from '@udecode/plate-common/react';
import { ExcalidrawPlugin } from '@udecode/plate-excalidraw/react';
import { HEADING_KEYS } from '@udecode/plate-heading';
import { ListPlugin, TodoListPlugin } from '@udecode/plate-list/react';
import { PlaceholderPlugin } from '@udecode/plate-media/react';
import { NormalizeTypesPlugin } from '@udecode/plate-normalizers';
import { PlaywrightPlugin } from '@udecode/plate-playwright';
import { TablePlugin } from '@udecode/plate-table/react';
Expand Down Expand Up @@ -59,7 +58,6 @@ export const usePlaygroundEditor = (id: any = '') => {
},
}),
SingleLinePlugin,
PlaceholderPlugin,
// Testing
PlaywrightPlugin.configure({
enabled: process.env.NODE_ENV !== 'production',
Expand Down
4 changes: 0 additions & 4 deletions apps/www/src/registry/default/plate-ui/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import {
} from '@udecode/plate-common/react';
import { cva } from 'class-variance-authority';

import { useUploadErrorToast } from './media-placeholder-element';

const editorContainerVariants = cva(
'relative w-full cursor-text overflow-y-auto caret-primary selection:bg-brand/25 [&_.slate-selection-area]:border [&_.slate-selection-area]:border-brand/25 [&_.slate-selection-area]:bg-brand/15',
{
Expand All @@ -39,8 +37,6 @@ export const EditorContainer = ({
const editor = useEditorRef();
const containerRef = useEditorContainerRef();

useUploadErrorToast();

return (
<div
id={editor.uid}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { cn } from '@udecode/cn';
import {
findNodePath,
useEditorPlugin,
useEditorRef,
withHOC,
withRef,
} from '@udecode/plate-common/react';
Expand All @@ -19,13 +18,11 @@ import {
ImagePlugin,
PlaceholderPlugin,
PlaceholderProvider,
UploadErrorCode,
VideoPlugin,
updateUploadHistory,
} from '@udecode/plate-media/react';
import { insertNodes, removeNodes, withoutSavingHistory } from '@udecode/slate';
import { AudioLines, FileUp, Film, ImageIcon } from 'lucide-react';
import { toast } from 'sonner';
import { useFilePicker } from 'use-file-picker';

import { useUploadFile } from '../lib/uploadthing';
Expand Down Expand Up @@ -268,61 +265,3 @@ export function formatBytes(
: (sizes[i] ?? 'Bytes')
}`;
}

export const useUploadErrorToast = () => {
const editor = useEditorRef();

const uploadError = editor.useOption(PlaceholderPlugin, 'error');

useEffect(() => {
if (!uploadError) return;

const { code, data } = uploadError;

switch (code) {
case UploadErrorCode.INVALID_FILE_SIZE: {
toast.error(
`The size of files ${data.files
.map((f) => f.name)
.join(', ')} is invalid`
);

break;
}
case UploadErrorCode.INVALID_FILE_TYPE: {
toast.error(
`The type of files ${data.files
.map((f) => f.name)
.join(', ')} is invalid`
);

break;
}
case UploadErrorCode.TOO_LARGE: {
toast.error(
`The size of files ${data.files
.map((f) => f.name)
.join(', ')} is too large than ${data.maxFileSize}`
);

break;
}
case UploadErrorCode.TOO_LESS_FILES: {
toast.error(
`The mini um number of files is ${data.minFileCount} for ${data.fileType}`
);

break;
}
case UploadErrorCode.TOO_MANY_FILES: {
toast.error(
`The maximum number of files is ${data.maxFileCount} ${
data.fileType ? `for ${data.fileType}` : ''
}`
);

break;
}
}
}, [uploadError]);
};
69 changes: 69 additions & 0 deletions apps/www/src/registry/default/plate-ui/media-upload-toast.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { useEffect } from 'react';

import { useEditorRef } from '@udecode/plate-core/react';
import { PlaceholderPlugin, UploadErrorCode } from '@udecode/plate-media/react';
import { toast } from 'sonner';

export const useUploadErrorToast = () => {
const editor = useEditorRef();

const uploadError = editor.useOption(PlaceholderPlugin, 'error');

useEffect(() => {
if (!uploadError) return;

const { code, data } = uploadError;

switch (code) {
case UploadErrorCode.INVALID_FILE_SIZE: {
toast.error(
`The size of files ${data.files
.map((f) => f.name)
.join(', ')} is invalid`
);

break;
}
case UploadErrorCode.INVALID_FILE_TYPE: {
toast.error(
`The type of files ${data.files
.map((f) => f.name)
.join(', ')} is invalid`
);

break;
}
case UploadErrorCode.TOO_LARGE: {
toast.error(
`The size of files ${data.files
.map((f) => f.name)
.join(', ')} is too large than ${data.maxFileSize}`
);

break;
}
case UploadErrorCode.TOO_LESS_FILES: {
toast.error(
`The mini um number of files is ${data.minFileCount} for ${data.fileType}`
);

break;
}
case UploadErrorCode.TOO_MANY_FILES: {
toast.error(
`The maximum number of files is ${data.maxFileCount} ${
data.fileType ? `for ${data.fileType}` : ''
}`
);

break;
}
}
}, [uploadError]);
};

export const MediaUploadToast = () => {
useUploadErrorToast();

return null;
};
32 changes: 0 additions & 32 deletions apps/www/src/registry/default/plate-ui/undo-toolbar-button.tsx

This file was deleted.

14 changes: 3 additions & 11 deletions packages/media/src/react/media/MediaEmbedPlugin.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ExtendConfig } from '@udecode/plate-common';

import { toTPlatePlugin } from '@udecode/plate-common/react';
import { focusEditor, toTPlatePlugin } from '@udecode/plate-common/react';

import {
type BaseMediaEmbedConfig,
Expand All @@ -14,17 +14,9 @@ type MediaEmbedConfig = ExtendConfig<
isFloatingOpen?: boolean;
mediaType?: string | null;
url?: string;
},
{
media_embed: MediaEmbedApi;
}
>;

export type MediaEmbedApi = {
hideFloating: () => void;
openFloating: (mediaType: string) => void;
};

export const MediaEmbedPlugin = toTPlatePlugin<MediaEmbedConfig>(
BaseMediaEmbedPlugin,
{
Expand All @@ -34,7 +26,7 @@ export const MediaEmbedPlugin = toTPlatePlugin<MediaEmbedConfig>(
url: '',
},
}
).extendTransforms(({ api, editor, getOptions, setOptions }) => ({
).extendTransforms(({ editor, getOptions, setOptions }) => ({
embed: (url: string) => {
setOptions({ isFloatingOpen: false, url });

Expand All @@ -44,6 +36,6 @@ export const MediaEmbedPlugin = toTPlatePlugin<MediaEmbedConfig>(

insertImage(editor, url);

api.media_embed.hideFloating();
focusEditor(editor);
},
}));

0 comments on commit 24f13de

Please sign in to comment.