Skip to content

Commit c64dd68

Browse files
authored
feat: adding a link to image when pasting a link on selected image node (#194)
1 parent 4d87af5 commit c64dd68

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

src/extensions/markdown/Link/paste-plugin.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {Plugin, TextSelection} from 'prosemirror-state';
22

33
import type {ExtensionDeps, Parser} from '../../../core';
4-
import {isTextSelection} from '../../../utils/selection';
4+
import {isNodeSelection, isTextSelection} from '../../../utils/selection';
55
import {DataTransferType, isIosSafariShare} from '../../behavior/Clipboard/utils';
6+
import {imageType} from '../Image';
67

78
import {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

Comments
 (0)