11import * as React from "react" ;
2+ import { TextSelection } from "@tiptap/pm/state" ;
23import type { Editor } from "@tiptap/react" ;
34import {
45 Bold ,
@@ -15,6 +16,7 @@ import {
1516
1617import { cn } from "@/shared/lib/cn" ;
1718import { Tooltip , TooltipContent , TooltipTrigger } from "@/shared/ui/tooltip" ;
19+ import { isolateSelectionForBlockFormatting } from "@/features/messages/lib/selectionBlockFormatting" ;
1820import { getEditorSpoilerRangeState } from "@/features/messages/lib/spoilerFormatting" ;
1921import { SPOILER_MARK_NAME } from "@/features/messages/lib/spoilerMark" ;
2022
@@ -29,6 +31,11 @@ type FormattingToolbarProps = {
2931 onLinkButton ?: ( ) => void ;
3032} ;
3133
34+ type FormattingSelectionRange = {
35+ anchor : number ;
36+ head : number ;
37+ } ;
38+
3239type ActiveStates = {
3340 bold : boolean ;
3441 italic : boolean ;
@@ -117,6 +124,9 @@ export const FormattingToolbar = React.memo(function FormattingToolbar({
117124 disabled = false ,
118125 onLinkButton,
119126} : FormattingToolbarProps ) {
127+ const pendingSelectionRef = React . useRef < FormattingSelectionRange | null > (
128+ null ,
129+ ) ;
120130 const [ activeStates , setActiveStates ] = React . useState < ActiveStates | null > (
121131 ( ) => ( editor ? getActiveStates ( editor ) : null ) ,
122132 ) ;
@@ -137,36 +147,83 @@ export const FormattingToolbar = React.memo(function FormattingToolbar({
137147 } ;
138148 } , [ editor ] ) ;
139149
140- const toggleBold = React . useCallback ( ( ) => {
141- editor ?. chain ( ) . focus ( ) . toggleBold ( ) . run ( ) ;
150+ const captureSelection = React . useCallback ( ( ) => {
151+ if ( ! editor || editor . state . selection . empty ) {
152+ pendingSelectionRef . current = null ;
153+ return ;
154+ }
155+
156+ const { anchor, head } = editor . state . selection ;
157+ pendingSelectionRef . current = { anchor, head } ;
142158 } , [ editor ] ) ;
143159
144- const toggleItalic = React . useCallback ( ( ) => {
145- editor ?. chain ( ) . focus ( ) . toggleItalic ( ) . run ( ) ;
160+ const formattingChain = React . useCallback ( ( ) => {
161+ if ( ! editor ) return null ;
162+
163+ const range = pendingSelectionRef . current ;
164+ pendingSelectionRef . current = null ;
165+ const chain = editor . chain ( ) ;
166+
167+ if (
168+ range &&
169+ range . anchor !== range . head &&
170+ range . anchor <= editor . state . doc . content . size &&
171+ range . head <= editor . state . doc . content . size
172+ ) {
173+ chain . command ( ( { tr } ) => {
174+ tr . setSelection ( TextSelection . create ( tr . doc , range . anchor , range . head ) ) ;
175+ return true ;
176+ } ) ;
177+ }
178+
179+ return chain . focus ( ) ;
146180 } , [ editor ] ) ;
147181
182+ const toggleBold = React . useCallback ( ( ) => {
183+ formattingChain ( ) ?. toggleBold ( ) . run ( ) ;
184+ } , [ formattingChain ] ) ;
185+
186+ const toggleItalic = React . useCallback ( ( ) => {
187+ formattingChain ( ) ?. toggleItalic ( ) . run ( ) ;
188+ } , [ formattingChain ] ) ;
189+
148190 const toggleStrike = React . useCallback ( ( ) => {
149- editor ?. chain ( ) . focus ( ) . toggleStrike ( ) . run ( ) ;
150- } , [ editor ] ) ;
191+ formattingChain ( ) ? .toggleStrike ( ) . run ( ) ;
192+ } , [ formattingChain ] ) ;
151193
152194 const toggleCode = React . useCallback ( ( ) => {
153- editor ?. chain ( ) . focus ( ) . toggleCode ( ) . run ( ) ;
154- } , [ editor ] ) ;
195+ formattingChain ( ) ? .toggleCode ( ) . run ( ) ;
196+ } , [ formattingChain ] ) ;
155197
156198 const toggleCodeBlock = React . useCallback ( ( ) => {
157- editor ?. chain ( ) . focus ( ) . toggleCodeBlock ( ) . run ( ) ;
158- } , [ editor ] ) ;
199+ formattingChain ( )
200+ ?. command ( ( { tr } ) => {
201+ isolateSelectionForBlockFormatting ( tr ) ;
202+ return true ;
203+ } )
204+ . toggleCodeBlock ( )
205+ . run ( ) ;
206+ } , [ formattingChain ] ) ;
207+
208+ const restorePendingSelection = React . useCallback ( ( ) => {
209+ formattingChain ( ) ?. run ( ) ;
210+ } , [ formattingChain ] ) ;
159211
160212 const toggleLink = React . useCallback ( ( ) => {
161213 if ( ! editor ) return ;
162214
163- // Preferred path: open the link-edit modal, which handles add, edit, and
164- // remove with proper display-text + URL fields .
215+ // Restore the range captured on pointer-down before opening the modal.
216+ // WKWebView may otherwise collapse it as focus moves to the toolbar .
165217 if ( onLinkButton ) {
218+ restorePendingSelection ( ) ;
166219 onLinkButton ( ) ;
167220 return ;
168221 }
169222
223+ const chain = formattingChain ( ) ;
224+ if ( ! chain ) return ;
225+ chain . run ( ) ;
226+
170227 // Legacy fallback (no modal wired): the native prompts below are a no-op
171228 // in the Tauri WebView, so this path effectively does nothing there.
172229 if ( editor . isActive ( "link" ) ) {
@@ -189,24 +246,37 @@ export const FormattingToolbar = React.memo(function FormattingToolbar({
189246 editor . chain ( ) . focus ( ) . insertContent ( `[${ label } ](${ url } )` ) . run ( ) ;
190247 }
191248 }
192- } , [ editor , onLinkButton ] ) ;
249+ } , [ editor , formattingChain , onLinkButton , restorePendingSelection ] ) ;
193250
194251 const toggleBulletList = React . useCallback ( ( ) => {
195- editor ?. chain ( ) . focus ( ) . toggleBulletList ( ) . run ( ) ;
196- } , [ editor ] ) ;
252+ formattingChain ( )
253+ ?. command ( ( { tr } ) => {
254+ isolateSelectionForBlockFormatting ( tr ) ;
255+ return true ;
256+ } )
257+ . toggleBulletList ( )
258+ . run ( ) ;
259+ } , [ formattingChain ] ) ;
197260
198261 const toggleOrderedList = React . useCallback ( ( ) => {
199- editor ?. chain ( ) . focus ( ) . toggleOrderedList ( ) . run ( ) ;
200- } , [ editor ] ) ;
262+ formattingChain ( )
263+ ?. command ( ( { tr } ) => {
264+ isolateSelectionForBlockFormatting ( tr ) ;
265+ return true ;
266+ } )
267+ . toggleOrderedList ( )
268+ . run ( ) ;
269+ } , [ formattingChain ] ) ;
201270
202271 const toggleBlockquote = React . useCallback ( ( ) => {
203- editor ?. chain ( ) . focus ( ) . toggleBlockquote ( ) . run ( ) ;
204- } , [ editor ] ) ;
272+ formattingChain ( ) ? .toggleBlockquote ( ) . run ( ) ;
273+ } , [ formattingChain ] ) ;
205274
206275 const toggleSpoiler = React . useCallback ( ( ) => {
207276 if ( ! editor ) return ;
277+ restorePendingSelection ( ) ;
208278 toggleSpoilerFormatting ( editor ) ;
209- } , [ editor ] ) ;
279+ } , [ editor , restorePendingSelection ] ) ;
210280
211281 if ( ! editor || ! activeStates ) return null ;
212282
@@ -289,6 +359,7 @@ export const FormattingToolbar = React.memo(function FormattingToolbar({
289359 aria-pressed = { item . active }
290360 disabled = { disabled }
291361 onClick = { ( ) => item . action ( ) }
362+ onMouseDown = { captureSelection }
292363 className = { cn (
293364 "inline-flex h-7 w-7 min-w-7 items-center justify-center rounded-md text-sm font-medium transition-colors" ,
294365 "hover:bg-muted hover:text-foreground" ,
0 commit comments