Skip to content

fix: Multi-block links #1565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions packages/core/src/editor/BlockNoteEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ import {
import { Dictionary } from "../i18n/dictionary.js";
import { en } from "../i18n/locales/index.js";

import { Plugin, Transaction } from "@tiptap/pm/state";
import { Plugin, TextSelection, Transaction } from "@tiptap/pm/state";
import { dropCursor } from "prosemirror-dropcursor";
import { EditorView } from "prosemirror-view";
import { ySyncPluginKey } from "y-prosemirror";
Expand Down Expand Up @@ -1149,17 +1149,18 @@ export class BlockNoteEditor<
}

const { from, to } = this._tiptapEditor.state.selection;

if (!text) {
text = this._tiptapEditor.state.doc.textBetween(from, to);
}

const mark = this.pmSchema.mark("link", { href: url });

this.dispatch(
this._tiptapEditor.state.tr
.insertText(text, from, to)
.addMark(from, from + text.length, mark)
text
? this._tiptapEditor.state.tr
.insertText(text, from, to)
.addMark(from, from + text.length, mark)
: this._tiptapEditor.state.tr
.setSelection(
TextSelection.create(this._tiptapEditor.state.tr.doc, to)
)
.addMark(from, to, mark)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ export const CreateLinkButton = () => {
}, [editor.prosemirrorView?.dom]);

const update = useCallback(
(url: string, text: string) => {
editor.createLink(url, text);
(url: string) => {
editor.createLink(url);
editor.focus();
},
[editor]
Expand All @@ -93,11 +93,7 @@ export const CreateLinkButton = () => {
}
}

if (isTableCellSelection(editor.prosemirrorState.selection)) {
return false;
}

return true;
return !isTableCellSelection(editor.prosemirrorState.selection);
}, [linkInSchema, selectedBlocks, editor.prosemirrorState.selection]);

if (
Expand Down Expand Up @@ -128,7 +124,12 @@ export const CreateLinkButton = () => {
<Components.Generic.Popover.Content
className={"bn-popover-content bn-form-popover"}
variant={"form-popover"}>
<EditLinkMenuItems url={url} text={text} editLink={update} />
<EditLinkMenuItems
url={url}
text={text}
editLink={update}
showTextField={false}
/>
</Components.Generic.Popover.Content>
</Components.Generic.Popover.Root>
);
Expand Down
28 changes: 16 additions & 12 deletions packages/react/src/components/LinkToolbar/EditLinkMenuItems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ const validateUrl = (url: string) => {
};

export const EditLinkMenuItems = (
props: Pick<LinkToolbarProps, "url" | "text" | "editLink">
props: Pick<LinkToolbarProps, "url" | "text" | "editLink"> & {
showTextField?: boolean;
}
) => {
const Components = useComponentsContext()!;
const dict = useDictionary();

const { url, text, editLink } = props;
const { url, text, editLink, showTextField } = props;

const [currentUrl, setCurrentUrl] = useState<string>(url);
const [currentText, setCurrentText] = useState<string>(text);
Expand Down Expand Up @@ -78,16 +80,18 @@ export const EditLinkMenuItems = (
onChange={handleUrlChange}
onSubmit={handleSubmit}
/>
<Components.Generic.Form.TextInput
className={"bn-text-input"}
name="title"
icon={<RiText />}
placeholder={dict.link_toolbar.form.title_placeholder}
value={currentText}
onKeyDown={handleEnter}
onChange={handleTextChange}
onSubmit={handleSubmit}
/>
{showTextField !== false && (
<Components.Generic.Form.TextInput
className={"bn-text-input"}
name="title"
icon={<RiText />}
placeholder={dict.link_toolbar.form.title_placeholder}
value={currentText}
onKeyDown={handleEnter}
onChange={handleTextChange}
onSubmit={handleSubmit}
/>
)}
</Components.Generic.Form.Root>
);
};
Loading