feat: upload image button and drop to upload image
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -232,3 +232,7 @@ textarea.input {
|
||||
@apply text-3xl;
|
||||
@apply font-extrabold;
|
||||
}
|
||||
|
||||
.cm-editor {
|
||||
@apply h-full;
|
||||
}
|
||||
|
||||
@@ -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: `` })
|
||||
|
||||
toast.success("Uploaded!", {
|
||||
id: toastId,
|
||||
})
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
toast.error(error.message, { id: toastId })
|
||||
}
|
||||
}
|
||||
})
|
||||
input.click()
|
||||
},
|
||||
}
|
||||
@@ -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]
|
||||
|
||||
@@ -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<UploadFile>(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
|
||||
|
||||
@@ -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}`,
|
||||
}
|
||||
}
|
||||
@@ -197,8 +197,8 @@ export default function SubdomainEditor() {
|
||||
</header>
|
||||
<div className="h-screen pt-14 flex w-full">
|
||||
<div className="h-full overflow-auto w-full">
|
||||
<div className="max-w-screen-md mx-auto py-5 px-5">
|
||||
<div>
|
||||
<div className="h-full max-w-screen-md mx-auto py-5 px-5 flex flex-col">
|
||||
<div className="h-12">
|
||||
<input
|
||||
type="text"
|
||||
name="title"
|
||||
@@ -213,11 +213,11 @@ export default function SubdomainEditor() {
|
||||
placeholder="Title goes here.."
|
||||
/>
|
||||
</div>
|
||||
<div className="mt-5">
|
||||
<div className="mt-5 flex-1">
|
||||
{previewVisible ? (
|
||||
<EditorPreview content={content}></EditorPreview>
|
||||
) : (
|
||||
<div ref={editorRef}></div>
|
||||
<div className="h-full" ref={editorRef}></div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user