diff --git a/src/components/ui/Editor.tsx b/src/components/ui/Editor.tsx index 1bc0c386..64375217 100644 --- a/src/components/ui/Editor.tsx +++ b/src/components/ui/Editor.tsx @@ -28,10 +28,10 @@ const theme = EditorView.theme({ fontFamily: "var(--font-sans)", fontSize: "1rem", overflow: "auto", - paddingBottom: "200px", + height: "100%", }, ".cm-content": { - minHeight: "400px", + height: "100%", }, "&.cm-editor.cm-focused": { outline: "none", diff --git a/src/css/tailwind.css b/src/css/tailwind.css index ddba9706..502e9473 100644 --- a/src/css/tailwind.css +++ b/src/css/tailwind.css @@ -232,3 +232,7 @@ textarea.input { @apply text-3xl; @apply font-extrabold; } + +.cm-editor { + @apply h-full; +} diff --git a/src/editor/UploadImage.tsx b/src/editor/UploadImage.tsx new file mode 100644 index 00000000..37c8049f --- /dev/null +++ b/src/editor/UploadImage.tsx @@ -0,0 +1,32 @@ +import { EditorSelection } from "@codemirror/state" +import { ICommand, prependExecute, wrapExecute } from "." +import { UploadFile } from "~/lib/upload-file" +import toast from "react-hot-toast" + +export const UploadImage: ICommand = { + name: "image", + label: "Upload Image", + icon: "i-carbon:cloud-upload", + execute: (view) => { + const input = document.createElement("input") + input.type = "file" + input.addEventListener("change", async (e: any) => { + const toastId = toast.loading("Uploading...") + + const file = e.target?.files?.[0] + try { + const { key } = await UploadFile(file, file.name) + wrapExecute({ view, prepend: "", append: `![${file.name}](${key})` }) + + toast.success("Uploaded!", { + id: toastId, + }) + } catch (error) { + if (error instanceof Error) { + toast.error(error.message, { id: toastId }) + } + } + }) + input.click() + }, +} diff --git a/src/editor/index.tsx b/src/editor/index.tsx index d9c1d3b6..d8b82bc9 100644 --- a/src/editor/index.tsx +++ b/src/editor/index.tsx @@ -6,6 +6,7 @@ import { Code } from "./Code" import { CodeBlock } from "./CodeBlock" import { Heading } from "./Heading" import { Image } from "./Image" +import { UploadImage } from "./UploadImage" import { Italic } from "./Italic" import { Link } from "./Link" import { ListOrdered } from "./ListOrdered" @@ -128,6 +129,7 @@ export const toolbars: ICommand[] = [ ListOrdered, Link, Image, + UploadImage, ] export const modeToolbars: ICommand[] = [Preview] diff --git a/src/hooks/useUploadFile.ts b/src/hooks/useUploadFile.ts index b857d1a8..8cf1e31c 100644 --- a/src/hooks/useUploadFile.ts +++ b/src/hooks/useUploadFile.ts @@ -1,22 +1,9 @@ import { useCallback } from "react" -import { Web3Storage } from "web3.storage" -import { IPFS_GATEWAY } from "../lib/env" +import { UploadFile } from "../lib/upload-file" export const useUploadFile = () => { const uploadFile = useCallback(async (blob, filename) => { - const file = new File([blob], filename) - const web3Storage = new Web3Storage({ - token: - "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkaWQ6ZXRocjoweDAyMDIwODZmRjU5OUU0Y0YyMzM4MkUzNjg1Y0NmZUEyOGNBODBCOTAiLCJpc3MiOiJ3ZWIzLXN0b3JhZ2UiLCJpYXQiOjE2NTIzNjM1Njk3NDUsIm5hbWUiOiJVbmlkYXRhIn0.XmsAuXvbTj4BFhZlJK4xXfbd0ltVZJCEhqdYcW_kLOo", - } as any) - const cid = await web3Storage.put([file], { - name: file.name, - maxRetries: 3, - wrapWithDirectory: false, - }) - return { - key: `${IPFS_GATEWAY}${cid}`, - } + return await UploadFile(blob, filename) }, []) return uploadFile diff --git a/src/lib/upload-file.ts b/src/lib/upload-file.ts new file mode 100644 index 00000000..bf2d14bd --- /dev/null +++ b/src/lib/upload-file.ts @@ -0,0 +1,18 @@ +import { Web3Storage } from "web3.storage" +import { IPFS_GATEWAY } from "../lib/env" + +export const UploadFile = async (blob: Blob, filename: string) => { + const file = new File([blob], filename) + const web3Storage = new Web3Storage({ + token: + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkaWQ6ZXRocjoweDAyMDIwODZmRjU5OUU0Y0YyMzM4MkUzNjg1Y0NmZUEyOGNBODBCOTAiLCJpc3MiOiJ3ZWIzLXN0b3JhZ2UiLCJpYXQiOjE2NTIzNjM1Njk3NDUsIm5hbWUiOiJVbmlkYXRhIn0.XmsAuXvbTj4BFhZlJK4xXfbd0ltVZJCEhqdYcW_kLOo", + } as any) + const cid = await web3Storage.put([file], { + name: file.name, + maxRetries: 3, + wrapWithDirectory: false, + }) + return { + key: `${IPFS_GATEWAY}${cid}`, + } +} diff --git a/src/pages/dashboard/[subdomain]/editor.tsx b/src/pages/dashboard/[subdomain]/editor.tsx index 45d244fe..20e7c564 100644 --- a/src/pages/dashboard/[subdomain]/editor.tsx +++ b/src/pages/dashboard/[subdomain]/editor.tsx @@ -197,8 +197,8 @@ export default function SubdomainEditor() {
-
-
+
+
-
+
{previewVisible ? ( ) : ( -
+
)}