Created
March 19, 2022 23:33
-
-
Save christopher-caldwell/53bb281b13ce786e0d8afb671a8f9541 to your computer and use it in GitHub Desktop.
Uploading file from WYSIWYG editor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { EditorConfig } from '@editorjs/editorjs' | |
// The image tool has no types, if using TS you'll need to define them. See below for example | |
import ImageTool from '@editorjs/image' | |
import axios from 'axios' | |
const editorOptions: EditorConfig = { | |
tools: { | |
image: { | |
class: ImageTool, | |
config: { | |
uploader: { | |
async uploadByFile(file: File) { | |
const fileName = await handleImageUpload(file) | |
return { | |
success: 1, | |
file: { | |
url: fileName, | |
}, | |
} | |
}, | |
}, | |
}, | |
}, | |
}, | |
} | |
const getPresignedUrl = async (fileName: string) => { | |
// ... get the URL from your API | |
console.log(fileName) | |
return 'S3_PRESIGNED_URL' | |
} | |
// THis may be different depending on your own path in S3. | |
const constructImageUrl = (fileName: string): string => `https://s3.amazonaws.com/BUCKET_NAME/${fileName}` | |
const handleImageUpload = async (image: File): Promise<string> => { | |
const fileName = image.name | |
const fileType = image.type | |
const presignedUrl = await getPresignedUrl(fileName) | |
await axios.put(presignedUrl, { | |
data: image, | |
headers: { | |
'Content-Type': fileType, | |
}, | |
}) | |
return constructImageUrl(fileName) | |
} | |
// Minimal types to get TS to shut up about EditorJS | |
// declare module '@editorjs/image' { | |
// import { BlockTool } from '@editorjs/editorjs' | |
// export default class Image implements BlockTool { | |
// save(block: HTMLDivElement) | |
// render(): HTMLElement | |
// } | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment