Skip to content

Commit 3eea07f

Browse files
committed
uploadthing
1 parent 80acd3b commit 3eea07f

File tree

11 files changed

+219
-34
lines changed

11 files changed

+219
-34
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
OPENAI_API_KEY=
2+
UPLOADTHING_TOKEN=

templates/plate-playground-template/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ cp .env.example .env.local
4141
Configure `.env.local`:
4242

4343
- `OPENAI_API_KEY` – OpenAI API key ([get one here](https://platform.openai.com/account/api-keys))
44+
- `UPLOADTHING_TOKEN` – UploadThing API key ([get one here](https://uploadthing.com/dashboard)) You can also using your own backend
4445

4546
Start the development server:
4647

templates/plate-playground-template/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
"@udecode/plate-table": "^40.0.0",
7474
"@udecode/plate-toggle": "^40.0.0",
7575
"@udecode/plate-trailing-block": "^40.0.0",
76+
"@uploadthing/react": "7.1.0",
77+
"uploadthing": "7.2.0",
7678
"zod": "^3.23.8",
7779
"react-player": "^2.16.0",
7880
"sonner": "^1.5.0",

templates/plate-playground-template/pnpm-lock.yaml

+124
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { FileRouter } from 'uploadthing/next';
2+
3+
import { createUploadthing } from 'uploadthing/next';
4+
5+
const f = createUploadthing();
6+
7+
// FileRouter for your app, can contain multiple FileRoutes
8+
export const ourFileRouter = {
9+
// Define as many FileRoutes as you like, each with a unique routeSlug
10+
imageUploader: f(['image', 'text', 'blob', 'pdf', 'video', 'audio'])
11+
// Set permissions and file types for this FileRoute
12+
.middleware(async ({ req }) => {
13+
// This code runs on your server before upload
14+
15+
// Whatever is returned here is accessible in onUploadComplete as `metadata`
16+
return {};
17+
})
18+
.onUploadComplete(({ file, metadata }) => {
19+
// This code RUNS ON YOUR SERVER after upload
20+
21+
// !!! Whatever is returned here is sent to the clientside `onClientUploadComplete` callback
22+
return { file };
23+
}),
24+
} satisfies FileRouter;
25+
26+
export type OurFileRouter = typeof ourFileRouter;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { createRouteHandler } from 'uploadthing/next';
2+
3+
import { ourFileRouter } from './core';
4+
5+
// Export routes for Next App Router
6+
export const { GET, POST } = createRouteHandler({
7+
router: ourFileRouter,
8+
9+
// Apply an (optional) custom config:
10+
// config: { ... },
11+
});

templates/plate-playground-template/src/components/plate-ui/image-element.tsx

+14-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import React from 'react';
44

55
import { cn, withRef } from '@udecode/cn';
6-
import { withHOC } from '@udecode/plate-common/react';
6+
import { useEditorRef, withHOC } from '@udecode/plate-common/react';
7+
import { useDraggable, useDraggableState } from '@udecode/plate-dnd';
78
import { Image, ImagePlugin, useMediaState } from '@udecode/plate-media/react';
89
import { ResizableProvider, useResizableStore } from '@udecode/plate-resizable';
910

@@ -20,10 +21,19 @@ export const ImageElement = withHOC(
2021
ResizableProvider,
2122
withRef<typeof PlateElement>(
2223
({ children, className, nodeProps, ...props }, ref) => {
24+
const editor = useEditorRef();
25+
2326
const { align = 'center', focused, readOnly, selected } = useMediaState();
2427

2528
const width = useResizableStore().get.width();
2629

30+
const state = editor.plugins.dnd
31+
? useDraggableState({ element: props.element })
32+
: ({} as any);
33+
34+
const { isDragging } = state;
35+
const { handleRef } = useDraggable(state);
36+
2737
return (
2838
<MediaPopover plugin={ImagePlugin}>
2939
<PlateElement
@@ -44,10 +54,12 @@ export const ImageElement = withHOC(
4454
options={{ direction: 'left' }}
4555
/>
4656
<Image
57+
ref={handleRef}
4758
className={cn(
4859
'block w-full max-w-full cursor-pointer object-cover px-0',
4960
'rounded-sm',
50-
focused && selected && 'ring-2 ring-ring ring-offset-2'
61+
focused && selected && 'ring-ring ring-2 ring-offset-2',
62+
isDragging && 'opacity-50'
5163
)}
5264
alt=""
5365
{...nodeProps}

templates/plate-playground-template/src/components/plate-ui/media-placeholder-element.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export const MediaPlaceholderElement = withHOC(
7373
const { api } = useEditorPlugin(PlaceholderPlugin);
7474

7575
const { isUploading, progress, uploadFile, uploadedFile, uploadingFile } =
76-
useUploadFile();
76+
useUploadFile('imageUploader');
7777

7878
const loading = isUploading && uploadingFile;
7979

Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export * from './handle-error';
22

3+
export * from './uploadthing';
4+
35
export * from './use-upload-file';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import type { OurFileRouter } from '@/app/api/uploadthing/core';
2+
3+
import { generateReactHelpers } from '@uploadthing/react';
4+
5+
export const { uploadFiles, useUploadThing } =
6+
generateReactHelpers<OurFileRouter>();

0 commit comments

Comments
 (0)