Skip to content

Commit bd52bee

Browse files
authored
fix(Link): fixed pasting link to empty selection (#528)
1 parent 5a82a6c commit bd52bee

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Plugin, TextSelection} from 'prosemirror-state';
1+
import {Plugin, TextSelection, type Transaction} from 'prosemirror-state';
22

33
import type {ExtensionDeps, Parser} from '../../../core';
44
import {isNodeSelection, isTextSelection} from '../../../utils/selection';
@@ -14,28 +14,48 @@ export function linkPasteEnhance({markupParser: parser}: ExtensionDeps) {
1414
paste(view, e): boolean {
1515
const {state, dispatch} = view;
1616
const sel = state.selection;
17+
let tr: Transaction | null = null;
18+
1719
if (
1820
isTextSelection(sel) ||
1921
(isNodeSelection(sel) && sel.node.type === imageType(state.schema))
2022
) {
2123
const {$from, $to} = sel;
22-
if ($from.pos !== $to.pos && $from.sameParent($to)) {
24+
if ($from.pos === $to.pos) {
2325
const url = getUrl(e.clipboardData, parser);
2426
if (url) {
25-
const tr = state.tr.addMark(
27+
const linkMarkType = linkType(state.schema);
28+
tr = state.tr.replaceSelectionWith(
29+
state.schema.text(url, [
30+
...$from
31+
.marks()
32+
.filter((mark) => mark.type !== linkMarkType),
33+
linkMarkType.create({[LinkAttr.Href]: url}),
34+
]),
35+
false,
36+
);
37+
}
38+
} else if ($from.sameParent($to)) {
39+
const url = getUrl(e.clipboardData, parser);
40+
if (url) {
41+
tr = state.tr.addMark(
2642
$from.pos,
2743
$to.pos,
2844
linkType(state.schema).create({
2945
[LinkAttr.Href]: url,
3046
}),
3147
);
32-
dispatch(tr.setSelection(TextSelection.create(tr.doc, $to.pos)));
33-
e.preventDefault();
34-
return true;
48+
tr.setSelection(TextSelection.create(tr.doc, $to.pos));
3549
}
3650
}
3751
}
3852

53+
if (tr) {
54+
dispatch(tr.scrollIntoView());
55+
e.preventDefault();
56+
return true;
57+
}
58+
3959
return false;
4060
},
4161
},

0 commit comments

Comments
 (0)