Skip to content

Commit

Permalink
Merge pull request #60 from hunghg255/update-post-upload
Browse files Browse the repository at this point in the history
  • Loading branch information
hunghg255 authored Oct 8, 2024
2 parents 4be9da0 + 6ba701a commit 1d1821b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docs/extensions/ImageUpload/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ const extensions = [
}),// [!code ++]
];
```

## Props

- `upload` - A function that returns a promise that resolves to the image URL.
- `postUpload` - A function that is called after the image is uploaded. It receives the image URL as an argument.
3 changes: 3 additions & 0 deletions playground/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ const extensions = [
}, 500)
})
},
postUpload: async (url) => {
return url
},
}),
Video,
VideoUpload.configure({
Expand Down
2 changes: 2 additions & 0 deletions src/extensions/ImageUpload/ImageUpload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {

export interface ImageUploadOptions {
upload: (file: File) => Promise<string>
postUpload?: (url: string) => Promise<string>
acceptMimes: string[]
maxSize: number
}
Expand Down Expand Up @@ -122,6 +123,7 @@ export const ImageUpload = Node.create<ImageUploadOptions>({
const uploadFn = createImageUpload({
validateFn: validateFile,
onUpload: this.options.upload,
postUpload: this.options.postUpload,
})

return [
Expand Down
1 change: 1 addition & 0 deletions src/extensions/ImageUpload/components/ImageUploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function ImageUploader(props: any) {
return true
},
onUpload: uploadOptions.upload,
postUpload: uploadOptions.postUpload,
})
uploadFn([file], props.editor.view, props.getPos())
}
Expand Down
9 changes: 7 additions & 2 deletions src/plugins/image-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ function findPlaceholder(state: EditorState, id: string): number | null {
export interface ImageUploadOptions {
validateFn?: (file: File) => boolean
onUpload: (file: File) => Promise<string | object>
postUpload?: (src: string) => Promise<string>
}

export type UploadFn = (files: File[], view: EditorView, pos: number) => void

export function createImageUpload({ validateFn, onUpload }: ImageUploadOptions): UploadFn {
export function createImageUpload({ validateFn, onUpload, postUpload }: ImageUploadOptions): UploadFn {
return (files, view, pos) => {
for (const file of files) {
if (validateFn && !validateFn(file)) {
Expand All @@ -90,7 +91,11 @@ export function createImageUpload({ validateFn, onUpload }: ImageUploadOptions):
view.dispatch(tr)

onUpload(file).then(
(src) => {
async (src) => {
if (postUpload && typeof src === 'string') {
src = await postUpload(src)
}

const { schema } = view.state
let placeholderPos = findPlaceholder(view.state, id)
if (placeholderPos === null) {
Expand Down

0 comments on commit 1d1821b

Please sign in to comment.