Skip to content

Commit 2b00839

Browse files
committed
feat: When editing links and closing the link edit panel restore the cursor position to the position where we left.
1 parent 7011525 commit 2b00839

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/extensions/link.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ async function link_panel({ app }) {
8282
}
8383
focus_handler(link_panel);
8484

85+
// Store the current cursor position.
86+
// While extending the selection below the cursor position is changed and
87+
// we want it back where we left.
88+
const last_cursor_position = app.editor.state.selection.$head.pos;
89+
8590
const reinit = () => {
8691
const link_href = link_panel.querySelector("[name=tiptap-href]");
8792
const link_text = link_panel.querySelector("[name=tiptap-text]");
@@ -130,9 +135,6 @@ async function link_panel({ app }) {
130135

131136
const update_callback = (set_focus) => {
132137
const cmd = app.editor.chain();
133-
if (set_focus === true) {
134-
cmd.focus();
135-
}
136138
const link_text_value = (link_text ? link_text.value : text_content) || "";
137139
cmd.command(async ({ tr }) => {
138140
// create = update
@@ -146,6 +148,10 @@ async function link_panel({ app }) {
146148
.text(link_text_value)
147149
.mark([mark]);
148150
tr.replaceSelectionWith(link_node, false);
151+
if (set_focus === true) {
152+
// Set the cursor back to the position where we left.
153+
cmd.focus().setTextSelection(last_cursor_position);
154+
}
149155
return true;
150156
});
151157
cmd.run();

0 commit comments

Comments
 (0)