Skip to content

Commit 656e9fa

Browse files
authored
Merge pull request #260 from loancv12/main
2 parents f29718f + 0f1cc4f commit 656e9fa

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

src/components/menus/components/BubbleMenuLink.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ function BubbleMenuLink({ editor, disabled }: BubbleMenuLinkProps) {
2525
}, []);
2626

2727
const onSetLink = (url: string, text?: string, openInNewTab?: boolean) => {
28+
const selection = editor.state.selection;
29+
const { from } = selection;
30+
31+
const insertedLength = text?.length ?? 0;
32+
const newTo = from + insertedLength;
33+
2834
editor
2935
.chain()
3036
.extendMarkRange('link')
@@ -42,7 +48,8 @@ function BubbleMenuLink({ editor, disabled }: BubbleMenuLinkProps) {
4248
],
4349
})
4450
.setLink({ href: url })
45-
.selectNodeForward()
51+
.setTextSelection({ from, to: newTo }) // 👈 Select inserted text
52+
.focus()
4653
.run();
4754

4855
setShowEdit(false);

src/extensions/Link/Link.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import type { EditorView } from '@tiptap/pm/view';
77
import LinkEditPopover from '@/extensions/Link/components/LinkEditPopover';
88
import type { GeneralOptions } from '@/types';
99

10-
export interface LinkOptions extends TiptapLinkOptions, GeneralOptions<LinkOptions> {}
10+
export interface LinkOptions
11+
extends TiptapLinkOptions,
12+
GeneralOptions<LinkOptions> {}
1113

1214
export const Link = /* @__PURE__ */ TiptapLink.extend<LinkOptions>({
1315
inclusive: false,
@@ -39,6 +41,12 @@ export const Link = /* @__PURE__ */ TiptapLink.extend<LinkOptions>({
3941
editor,
4042
action: (value) => {
4143
const { link, text, openInNewTab } = value;
44+
45+
const { state } = editor;
46+
const { from } = state.selection;
47+
const insertedLength = text.length;
48+
const to = from + insertedLength;
49+
4250
editor
4351
.chain()
4452
.extendMarkRange('link')
@@ -56,6 +64,7 @@ export const Link = /* @__PURE__ */ TiptapLink.extend<LinkOptions>({
5664
],
5765
})
5866
.setLink({ href: link })
67+
.setTextSelection({ from, to }) // 👈 Select inserted text
5968
.focus()
6069
.run();
6170
},
@@ -82,7 +91,9 @@ export const Link = /* @__PURE__ */ TiptapLink.extend<LinkOptions>({
8291
}
8392
const $start = doc.resolve(range.from);
8493
const $end = doc.resolve(range.to);
85-
const transaction = tr.setSelection(new TextSelection($start, $end));
94+
const transaction = tr.setSelection(
95+
new TextSelection($start, $end)
96+
);
8697
view.dispatch(transaction);
8798
},
8899
},

0 commit comments

Comments
 (0)