Skip to content
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

Read and reassign selected text marks when editing a link #238

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
45 changes: 34 additions & 11 deletions src/LinkBubbleMenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,40 @@ export default function LinkBubbleMenu({
// full link "mark"
.extendMarkRange("link")
// Update the link href and its text content
.insertContent({
type: "text",
marks: [
{
type: "link",
attrs: {
href: link,
},
},
],
text: text,
.command(({ tr, state }) => {
const existingHref = editor.isActive("link")
? (editor.getAttributes("link").href as string)
: "";

const { selection, schema } = state;

if (existingHref) {
// Get the resolved position from the selection
const resolvedPos = state.doc.resolve(selection.from);
const nodeAfter = resolvedPos.nodeAfter;

if (nodeAfter?.isText) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it we only use this logic below if specifically the next node is text?

And what if there are multiple nodes within the selection, not just two?

// Insert new text without changing the link mark
tr.insertText(text, selection.from, selection.to);

// Set the link separately to ensure the link mark is applied
tr.addMark(
selection.from,
selection.from + text.length,
schema.marks.link.create({ href: link })
);
}
} else {
tr.insertText(text, selection.from, selection.to);

tr.addMark(
selection.from,
selection.from + text.length,
schema.marks.link.create({ href: link })
);
}

return true;
})
// Note that as of "@tiptap/extension-link" 2.0.0-beta.37 when
// `autolink` is on (which we want), adding the link mark directly
Expand Down
Loading