11import { Plugin , TextSelection } from 'prosemirror-state' ;
22
33import type { ExtensionDeps , Parser } from '../../../core' ;
4- import { isTextSelection } from '../../../utils/selection' ;
4+ import { isNodeSelection , isTextSelection } from '../../../utils/selection' ;
55import { DataTransferType , isIosSafariShare } from '../../behavior/Clipboard/utils' ;
6+ import { imageType } from '../Image' ;
67
78import { LinkAttr , linkType } from './index' ;
89
@@ -13,23 +14,28 @@ export function linkPasteEnhance({parser}: ExtensionDeps) {
1314 paste ( view , e ) : boolean {
1415 const { state, dispatch} = view ;
1516 const sel = state . selection ;
16- if ( ! isTextSelection ( sel ) ) return false ;
17- const { $from, $to} = sel ;
18- if ( $from . pos !== $to . pos && $from . sameParent ( $to ) ) {
19- const url = getUrl ( e . clipboardData , parser ) ;
20- if ( url ) {
21- const tr = state . tr . addMark (
22- $from . pos ,
23- $to . pos ,
24- linkType ( state . schema ) . create ( {
25- [ LinkAttr . Href ] : url ,
26- } ) ,
27- ) ;
28- dispatch ( tr . setSelection ( TextSelection . create ( tr . doc , $to . pos ) ) ) ;
29- e . preventDefault ( ) ;
30- return true ;
17+ if (
18+ isTextSelection ( sel ) ||
19+ ( isNodeSelection ( sel ) && sel . node . type === imageType ( state . schema ) )
20+ ) {
21+ const { $from, $to} = sel ;
22+ if ( $from . pos !== $to . pos && $from . sameParent ( $to ) ) {
23+ const url = getUrl ( e . clipboardData , parser ) ;
24+ if ( url ) {
25+ const tr = state . tr . addMark (
26+ $from . pos ,
27+ $to . pos ,
28+ linkType ( state . schema ) . create ( {
29+ [ LinkAttr . Href ] : url ,
30+ } ) ,
31+ ) ;
32+ dispatch ( tr . setSelection ( TextSelection . create ( tr . doc , $to . pos ) ) ) ;
33+ e . preventDefault ( ) ;
34+ return true ;
35+ }
3136 }
3237 }
38+
3339 return false ;
3440 } ,
3541 } ,
0 commit comments