Skip to content

Commit 5558a51

Browse files
fix: Multi-block links (#1565)
* Improved link UX * Small fix
1 parent 31b7ced commit 5558a51

File tree

3 files changed

+35
-29
lines changed

3 files changed

+35
-29
lines changed

packages/core/src/editor/BlockNoteEditor.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ import {
9191
import { Dictionary } from "../i18n/dictionary.js";
9292
import { en } from "../i18n/locales/index.js";
9393

94-
import { Plugin, Transaction } from "@tiptap/pm/state";
94+
import { Plugin, TextSelection, Transaction } from "@tiptap/pm/state";
9595
import { dropCursor } from "prosemirror-dropcursor";
9696
import { EditorView } from "prosemirror-view";
9797
import { ySyncPluginKey } from "y-prosemirror";
@@ -1149,17 +1149,18 @@ export class BlockNoteEditor<
11491149
}
11501150

11511151
const { from, to } = this._tiptapEditor.state.selection;
1152-
1153-
if (!text) {
1154-
text = this._tiptapEditor.state.doc.textBetween(from, to);
1155-
}
1156-
11571152
const mark = this.pmSchema.mark("link", { href: url });
11581153

11591154
this.dispatch(
1160-
this._tiptapEditor.state.tr
1161-
.insertText(text, from, to)
1162-
.addMark(from, from + text.length, mark)
1155+
text
1156+
? this._tiptapEditor.state.tr
1157+
.insertText(text, from, to)
1158+
.addMark(from, from + text.length, mark)
1159+
: this._tiptapEditor.state.tr
1160+
.setSelection(
1161+
TextSelection.create(this._tiptapEditor.state.tr.doc, to)
1162+
)
1163+
.addMark(from, to, mark)
11631164
);
11641165
}
11651166

packages/react/src/components/FormattingToolbar/DefaultButtons/CreateLinkButton.tsx

+9-8
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ export const CreateLinkButton = () => {
7575
}, [editor.prosemirrorView?.dom]);
7676

7777
const update = useCallback(
78-
(url: string, text: string) => {
79-
editor.createLink(url, text);
78+
(url: string) => {
79+
editor.createLink(url);
8080
editor.focus();
8181
},
8282
[editor]
@@ -93,11 +93,7 @@ export const CreateLinkButton = () => {
9393
}
9494
}
9595

96-
if (isTableCellSelection(editor.prosemirrorState.selection)) {
97-
return false;
98-
}
99-
100-
return true;
96+
return !isTableCellSelection(editor.prosemirrorState.selection);
10197
}, [linkInSchema, selectedBlocks, editor.prosemirrorState.selection]);
10298

10399
if (
@@ -128,7 +124,12 @@ export const CreateLinkButton = () => {
128124
<Components.Generic.Popover.Content
129125
className={"bn-popover-content bn-form-popover"}
130126
variant={"form-popover"}>
131-
<EditLinkMenuItems url={url} text={text} editLink={update} />
127+
<EditLinkMenuItems
128+
url={url}
129+
text={text}
130+
editLink={update}
131+
showTextField={false}
132+
/>
132133
</Components.Generic.Popover.Content>
133134
</Components.Generic.Popover.Root>
134135
);

packages/react/src/components/LinkToolbar/EditLinkMenuItems.tsx

+16-12
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ const validateUrl = (url: string) => {
2222
};
2323

2424
export const EditLinkMenuItems = (
25-
props: Pick<LinkToolbarProps, "url" | "text" | "editLink">
25+
props: Pick<LinkToolbarProps, "url" | "text" | "editLink"> & {
26+
showTextField?: boolean;
27+
}
2628
) => {
2729
const Components = useComponentsContext()!;
2830
const dict = useDictionary();
2931

30-
const { url, text, editLink } = props;
32+
const { url, text, editLink, showTextField } = props;
3133

3234
const [currentUrl, setCurrentUrl] = useState<string>(url);
3335
const [currentText, setCurrentText] = useState<string>(text);
@@ -78,16 +80,18 @@ export const EditLinkMenuItems = (
7880
onChange={handleUrlChange}
7981
onSubmit={handleSubmit}
8082
/>
81-
<Components.Generic.Form.TextInput
82-
className={"bn-text-input"}
83-
name="title"
84-
icon={<RiText />}
85-
placeholder={dict.link_toolbar.form.title_placeholder}
86-
value={currentText}
87-
onKeyDown={handleEnter}
88-
onChange={handleTextChange}
89-
onSubmit={handleSubmit}
90-
/>
83+
{showTextField !== false && (
84+
<Components.Generic.Form.TextInput
85+
className={"bn-text-input"}
86+
name="title"
87+
icon={<RiText />}
88+
placeholder={dict.link_toolbar.form.title_placeholder}
89+
value={currentText}
90+
onKeyDown={handleEnter}
91+
onChange={handleTextChange}
92+
onSubmit={handleSubmit}
93+
/>
94+
)}
9195
</Components.Generic.Form.Root>
9296
);
9397
};

0 commit comments

Comments
 (0)