({
editor,
file,
uploadImage,
}: ExecuteUploadFlowParams)
| 22 | } |
| 23 | |
| 24 | export async function executeUploadFlow({ |
| 25 | editor, |
| 26 | file, |
| 27 | uploadImage, |
| 28 | }: ExecuteUploadFlowParams): Promise<void> { |
| 29 | const blobUrl = URL.createObjectURL(file); |
| 30 | |
| 31 | const selectedImageAttrs = getSelectedImageAttrs(editor); |
| 32 | |
| 33 | editor |
| 34 | .chain() |
| 35 | .focus() |
| 36 | .setImage({ ...selectedImageAttrs, src: blobUrl }) |
| 37 | .run(); |
| 38 | |
| 39 | try { |
| 40 | const { url } = await uploadImage(file); |
| 41 | swapImageSrc(editor, blobUrl, url); |
| 42 | } catch (error) { |
| 43 | removeImageBySrc(editor, blobUrl); |
| 44 | console.error( |
| 45 | `Failed to upload image "${file.name}":`, |
| 46 | error instanceof Error ? error : new Error(String(error)), |
| 47 | ); |
| 48 | } finally { |
| 49 | URL.revokeObjectURL(blobUrl); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | function swapImageSrc(editor: Editor, oldSrc: string, newSrc: string): void { |
| 54 | const { state } = editor; |
no test coverage detected