Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

fix: selected text is replaced with link

thecodrrb5d10a4b783d
diff --git a/packages/editor/src/toolbar/tools/link.tsx b/packages/editor/src/toolbar/tools/link.tsx
index 0c3e5d0fa..ae4cdc30f 100644
--- a/packages/editor/src/toolbar/tools/link.tsx
+++ b/packages/editor/src/toolbar/tools/link.tsx
@@ -36,14 +36,18 @@ export function AddLink(props: ToolProps) {
let commandChain = editor.current?.chain().focus();
if (!commandChain) return;
+ const isSelection = !editor.current?.state.selection.empty;
+
commandChain
.extendMarkRange("link")
.toggleLink({ href, target: "_blank" })
.insertContent(text || href)
- .focus()
- .unsetMark("link")
- .insertContent(" ")
- .run();
+ .focus();
+
+ if (!isSelection)
+ commandChain = commandChain.unsetMark("link").insertContent(" ");
+
+ commandChain.run();
}, []);
if (isActive) return <EditLink {...props} />;
@@ -52,8 +56,10 @@ export function AddLink(props: ToolProps) {
{...props}
onDone={onDone}
onClick={() => {
- let { from, to } = editor.state.selection;
- const selectedText = editor.state.doc.textBetween(from, to);
+ if (!editor.current) return;
+ const { state } = editor.current;
+ let { from, to } = state.selection;
+ const selectedText = state.doc.textBetween(from, to);
return { text: selectedText };
}}
/>