From 0eff8aa9ab5c28cf5860121c480f8b7d91fe87d3 Mon Sep 17 00:00:00 2001 From: fantasticit Date: Thu, 16 Jun 2022 16:31:30 +0800 Subject: [PATCH] tiptap: fix edit link when only one letter --- .../src/tiptap/core/menus/link/bubble.tsx | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/packages/client/src/tiptap/core/menus/link/bubble.tsx b/packages/client/src/tiptap/core/menus/link/bubble.tsx index a987b6d2..8fd93121 100644 --- a/packages/client/src/tiptap/core/menus/link/bubble.tsx +++ b/packages/client/src/tiptap/core/menus/link/bubble.tsx @@ -39,18 +39,28 @@ export const LinkBubbleMenu = ({ editor }) => { if (!isInLink) return; - const { $head } = editor.state.selection; + const { $head, from, to } = editor.state.selection; const marks = $head.marks(); - if (!marks.length) return; - const mark = marks[0]; - const node = $head.node($head.depth); - const startPosOfThisLine = $head.pos - (($head.nodeBefore && $head.nodeBefore.nodeSize) || 0); - const endPosOfThisLine = $head.nodeAfter - ? startPosOfThisLine + $head.nodeAfter.nodeSize - : $head.pos - $head.parentOffset + node.content.size; + let start; + let end; + + if (marks.length) { + const mark = marks[0]; + const node = $head.node($head.depth); + const startPosOfThisLine = $head.pos - (($head.nodeBefore && $head.nodeBefore.nodeSize) || 0); + const endPosOfThisLine = $head.nodeAfter + ? startPosOfThisLine + $head.nodeAfter.nodeSize + : $head.pos - $head.parentOffset + node.content.size; + + const { start: startPos, end: endPos } = findMarkPosition(state, mark, startPosOfThisLine, endPosOfThisLine); + start = startPos; + end = endPos; + } else { + start = from; + end = to; + } - const { start, end } = findMarkPosition(state, mark, startPosOfThisLine, endPosOfThisLine); const text = state.doc.textBetween(start, end); setText(text); setFrom(start);