@@ -12,7 +12,15 @@ import type {FileUploadHandler} from '../../../../utils';
1212import { clipboardUtils } from '../../../behavior/Clipboard' ;
1313import { DataTransferType } from '../../../behavior/Clipboard/utils' ;
1414import { ImageAttr , ImgSizeAttr , imageType } from '../../../specs' ;
15- import { type CreateImageNodeOptions , isImageNode } from '../utils' ;
15+ import {
16+ type CreateImageNodeOptions ,
17+ checkSvg ,
18+ findImageNode ,
19+ getImageSize ,
20+ getImageSizeNew ,
21+ isImageNode ,
22+ loadImageFromUrl ,
23+ } from '../utils' ;
1624
1725import { ImagesUploadProcess } from './upload' ;
1826
@@ -82,15 +90,77 @@ export const ImagePaste: ExtensionAuto<ImagePasteOptions> = (builder, opts) => {
8290
8391 e . preventDefault ( ) ;
8492
93+ const isSvg = checkSvg ( imageUrl ) ;
94+
95+ const trackingId = `img-${ Math . random ( ) . toString ( 36 ) . slice ( 2 ) } ` ;
96+
8597 const imageNode = imageType ( view . state . schema ) . create ( {
8698 src : imageUrl ,
8799 alt : title ,
100+ id : trackingId ,
88101 } ) ;
89102
90103 const tr = view . state . tr . replaceSelectionWith ( imageNode ) ;
91104 view . dispatch ( tr . scrollIntoView ( ) ) ;
92105 logger . event ( { event : 'paste-url-as-image' } ) ;
93106
107+ loadImageFromUrl ( imageUrl )
108+ . then ( ( img ) =>
109+ opts ?. enableNewImageSizeCalculation || isSvg
110+ ? getImageSizeNew ( img )
111+ : getImageSize ( img ) ,
112+ )
113+ . then (
114+ ( sizes : {
115+ [ ImgSizeAttr . Height ] ?: string ;
116+ [ ImgSizeAttr . Width ] ?: string ;
117+ } ) => {
118+ const currentState = view . state ;
119+
120+ const imageResult = findImageNode (
121+ currentState . doc ,
122+ trackingId ,
123+ ) ;
124+
125+ if ( imageResult === null ) {
126+ logger . error ( {
127+ event : 'img-node-not-found' ,
128+ trackingId,
129+ } ) ;
130+ return ;
131+ }
132+
133+ const { pos : targetPos } = imageResult ;
134+
135+ const updateTr = currentState . tr
136+ . setNodeAttribute (
137+ targetPos ,
138+ ImgSizeAttr . Height ,
139+ sizes . height ,
140+ )
141+ . setNodeAttribute (
142+ targetPos ,
143+ ImgSizeAttr . Width ,
144+ sizes . width ,
145+ )
146+ . setNodeAttribute ( targetPos , 'id' , null ) ;
147+
148+ view . dispatch ( updateTr ) ;
149+
150+ logger . event ( {
151+ event : 'img-dimensions-updated' ,
152+ position : targetPos ,
153+ sizes,
154+ } ) ;
155+ } ,
156+ )
157+ . catch ( ( error ) => {
158+ logger . error ( {
159+ event : 'img-dimensions-load-failed' ,
160+ error : error . message ,
161+ } ) ;
162+ } ) ;
163+
94164 return true ;
95165 }
96166
0 commit comments