11import { ImagePlaceholderPluginKey , findPlaceholder } from './imagePlaceholderPlugin.js' ;
22import { handleImageUpload as handleImageUploadDefault } from './handleImageUpload.js' ;
33import { processUploadedImage } from './processUploadedImage.js' ;
4+ import { insertNewRelationship } from '@core/super-converter/docx-helpers/document-rels.js' ;
45
56export const startImageUpload = async ( { editor, view, file } ) => {
6- // Handler from config or default
7- let imageUploadHandler =
7+ const imageUploadHandler =
88 typeof editor . options . handleImageUpload === 'function'
99 ? editor . options . handleImageUpload
1010 : handleImageUploadDefault ;
1111
12- let fileSizeMb = ( file . size / ( 1024 * 1024 ) ) . toFixed ( 4 ) ;
12+ let fileSizeMb = Number ( ( file . size / ( 1024 * 1024 ) ) . toFixed ( 4 ) ) ;
1313
1414 if ( fileSizeMb > 5 ) {
1515 window . alert ( 'Image size must be less than 5MB' ) ;
@@ -29,6 +29,16 @@ export const startImageUpload = async ({ editor, view, file }) => {
2929 return ;
3030 }
3131
32+ await uploadImage ( {
33+ editor,
34+ view,
35+ file,
36+ size : { width, height } ,
37+ uploadHandler : imageUploadHandler ,
38+ } ) ;
39+ } ;
40+
41+ export async function uploadImage ( { editor, view, file, size, uploadHandler } ) {
3242 // A fresh object to act as the ID for this upload
3343 let id = { } ;
3444
@@ -52,45 +62,63 @@ export const startImageUpload = async ({ editor, view, file }) => {
5262 tr . setMeta ( ImagePlaceholderPluginKey , imageMeta ) ;
5363 view . dispatch ( tr ) ;
5464
55- imageUploadHandler ( file ) . then (
56- ( url ) => {
57- let fileName = file . name . replace ( ' ' , '_' ) ;
58- let placeholderPos = findPlaceholder ( view . state , id ) ;
59-
60- // If the content around the placeholder has been deleted,
61- // drop the image
62- if ( placeholderPos == null ) {
63- return ;
64- }
65-
66- // Otherwise, insert it at the placeholder's position, and remove
67- // the placeholder
68- let removeMeta = { type : 'remove' , id } ;
69-
70- let mediaPath = `word/media/${ fileName } ` ;
71- let imageNode = schema . nodes . image . create ( {
72- src : mediaPath ,
73- size : { width, height } ,
74- } ) ;
75-
76- editor . storage . image . media = Object . assign ( editor . storage . image . media , { [ mediaPath ] : url } ) ;
77-
78- // If we are in collaboration, we need to share the image with other clients
79- if ( editor . options . ydoc ) {
80- editor . commands . addImageToCollaboration ( { mediaPath, fileData : url } ) ;
81- }
82-
83- view . dispatch (
84- view . state . tr
85- . replaceWith ( placeholderPos , placeholderPos , imageNode ) // or .insert(placeholderPos, imageNode)
86- . setMeta ( ImagePlaceholderPluginKey , removeMeta ) ,
87- ) ;
88- } ,
89- ( ) => {
90- let removeMeta = { type : 'remove' , id } ;
91-
92- // On failure, just clean up the placeholder
93- view . dispatch ( tr . setMeta ( ImagePlaceholderPluginKey , removeMeta ) ) ;
94- } ,
95- ) ;
96- } ;
65+ try {
66+ let url = await uploadHandler ( file ) ;
67+
68+ let fileName = file . name . replace ( ' ' , '_' ) ;
69+ let placeholderPos = findPlaceholder ( view . state , id ) ;
70+
71+ // If the content around the placeholder has been deleted,
72+ // drop the image
73+ if ( placeholderPos == null ) {
74+ return ;
75+ }
76+
77+ // Otherwise, insert it at the placeholder's position, and remove
78+ // the placeholder
79+ let removeMeta = { type : 'remove' , id } ;
80+
81+ let mediaPath = `word/media/${ fileName } ` ;
82+
83+ let rId = null ;
84+ if ( editor . options . mode === 'docx' ) {
85+ const [ , path ] = mediaPath . split ( 'word/' ) ; // Path without 'word/' part.
86+ const id = addImageRelationship ( { editor, path } ) ;
87+ if ( id ) rId = id ;
88+ }
89+
90+ let imageNode = schema . nodes . image . create ( {
91+ src : mediaPath ,
92+ size,
93+ rId,
94+ } ) ;
95+
96+ editor . storage . image . media = Object . assign ( editor . storage . image . media , { [ mediaPath ] : url } ) ;
97+
98+ // If we are in collaboration, we need to share the image with other clients
99+ if ( editor . options . ydoc ) {
100+ editor . commands . addImageToCollaboration ( { mediaPath, fileData : url } ) ;
101+ }
102+
103+ view . dispatch (
104+ view . state . tr
105+ . replaceWith ( placeholderPos , placeholderPos , imageNode ) // or .insert(placeholderPos, imageNode)
106+ . setMeta ( ImagePlaceholderPluginKey , removeMeta ) ,
107+ ) ;
108+ } catch {
109+ let removeMeta = { type : 'remove' , id } ;
110+ // On failure, just clean up the placeholder
111+ view . dispatch ( tr . setMeta ( ImagePlaceholderPluginKey , removeMeta ) ) ;
112+ }
113+ }
114+
115+ function addImageRelationship ( { editor, path } ) {
116+ const target = path ;
117+ const type = 'image' ;
118+ try {
119+ const id = insertNewRelationship ( target , type , editor ) ;
120+ return id ;
121+ } catch {
122+ return null ;
123+ }
124+ }
0 commit comments