Skip to content

Commit 9da3e59

Browse files
committed
fix: add link on commented text removes the comment highlight (2573)
1 parent af3737a commit 9da3e59

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

packages/core/src/extensions/LinkToolbar/LinkToolbar.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,38 @@ export const LinkToolbarExtension = createExtension(({ editor }) => {
7979
) {
8080
editor.transact((tr) => {
8181
const pmSchema = getPmSchema(tr);
82-
const { range } = getMarkAtPos(position + 1, "link") || {
82+
83+
const linkInfo = getMarkAtPos(position + 1, "link") || {
8384
range: {
8485
from: tr.selection.from,
8586
to: tr.selection.to,
8687
},
8788
};
89+
90+
const { range } = linkInfo;
91+
8892
if (!range) {
8993
return;
9094
}
95+
const existingMarks =
96+
tr.doc.nodeAt(Math.max(range.from, range.to - 1))?.marks ?? [];
97+
9198
tr.insertText(text, range.from, range.to);
99+
100+
for (const existingMark of existingMarks) {
101+
if (existingMark.type.name !== "link") {
102+
tr.addMark(range.from, range.from + text.length, existingMark);
103+
}
104+
}
105+
92106
tr.addMark(
93107
range.from,
94108
range.from + text.length,
95109
pmSchema.mark("link", { href: url }),
96110
);
97-
});
98-
editor.prosemirrorView.focus();
111+
});
112+
113+
editor.prosemirrorView.focus();
99114
},
100115
deleteLink(position = editor.transact((tr) => tr.selection.anchor)) {
101116
editor.transact((tr) => {

0 commit comments

Comments
 (0)