1
1
import { Link as LinkIcon } from '@styled-icons/material/Link' ;
2
2
import {
3
3
ELEMENT_LINK ,
4
+ deleteText ,
4
5
getEditorString ,
5
6
getNode ,
6
7
getSelectionText ,
@@ -9,7 +10,7 @@ import {
9
10
setNodes ,
10
11
someNode ,
11
12
} from '@udecode/plate' ;
12
- import React , { useCallback , useMemo , useState } from 'react' ;
13
+ import React , { useCallback , useMemo } from 'react' ;
13
14
14
15
import useMediaInsert from '@staticcms/core/lib/hooks/useMediaInsert' ;
15
16
import useUUID from '@staticcms/core/lib/hooks/useUUID' ;
@@ -21,7 +22,7 @@ import type { Collection, MarkdownField, MediaPath } from '@staticcms/core/inter
21
22
import type { MdLinkElement } from '@staticcms/markdown/plate/plateTypes' ;
22
23
import type { TText } from '@udecode/plate' ;
23
24
import type { FC } from 'react' ;
24
- import type { Path } from 'slate' ;
25
+ import type { Location } from 'slate' ;
25
26
26
27
export interface InsertLinkToolbarButtonProps {
27
28
variant : 'button' | 'menu' ;
@@ -38,25 +39,25 @@ const InsertLinkToolbarButton: FC<InsertLinkToolbarButtonProps> = ({
38
39
currentValue,
39
40
disabled,
40
41
} ) => {
41
- const [ selection , setSelection ] = useState < Path > ( ) ;
42
42
const editor = useMdPlateEditorState ( ) ;
43
43
const handleInsert = useCallback (
44
44
( { path : newUrl , alt : newText } : MediaPath < string > ) => {
45
- if ( isNotEmpty ( newUrl ) && selection ) {
45
+ const selectionPoint = editor . selection ?. focus . path ;
46
+ if ( isNotEmpty ( newUrl ) && selectionPoint ) {
46
47
const text = isNotEmpty ( newText ) ? newText : newUrl ;
47
- const linkAt = getNode < MdLinkElement > ( editor , selection ) ;
48
+ const linkAt = getNode < MdLinkElement > ( editor , selectionPoint ) ;
48
49
49
50
if ( linkAt && linkAt . type === ELEMENT_LINK ) {
50
51
if ( newUrl !== linkAt . url || text !== linkAt . children [ 0 ] . text ) {
51
52
setNodes < MdLinkElement > (
52
53
editor ,
53
54
{ url : newUrl , children : [ { text : newText } ] } ,
54
- { at : selection } ,
55
+ { at : selectionPoint } ,
55
56
) ;
56
57
57
- if ( text !== getEditorString ( editor , selection ) ) {
58
+ if ( text !== getEditorString ( editor , selectionPoint ) ) {
58
59
replaceNodeChildren < TText > ( editor , {
59
- at : selection ,
60
+ at : selectionPoint ,
60
61
nodes : { text } ,
61
62
insertOptions : {
62
63
select : true ,
@@ -68,32 +69,29 @@ const InsertLinkToolbarButton: FC<InsertLinkToolbarButtonProps> = ({
68
69
return ;
69
70
}
70
71
72
+ console . log ( 'editor.selection' , editor . selection ) ;
73
+
74
+ deleteText ( editor , {
75
+ at : editor . selection as unknown as Location ,
76
+ } ) ;
77
+
71
78
insertLink (
72
79
editor ,
73
80
{ url : newUrl , text } ,
74
81
{
75
- at : selection ,
82
+ at : editor . selection as unknown as Location ,
76
83
} ,
77
84
) ;
78
- const newSelection = [ ...selection ] ;
79
- const lastIndex = newSelection . pop ( ) ?? 0 ;
80
- setSelection ( [ ...newSelection , lastIndex + 1 ] ) ;
81
85
}
82
86
} ,
83
- [ editor , selection ] ,
87
+ [ editor ] ,
84
88
) ;
85
89
86
90
const chooseUrl = useMemo ( ( ) => field . choose_url ?? true , [ field . choose_url ] ) ;
87
91
88
92
const isLink = ! ! editor ?. selection && someNode ( editor , { match : { type : ELEMENT_LINK } } ) ;
89
93
90
- const selectedText : string = useMemo ( ( ) => {
91
- if ( ! editor . selection ) {
92
- return '' ;
93
- }
94
-
95
- return getSelectionText ( editor ) ;
96
- } , [ editor ] ) ;
94
+ const selectedText = ! editor . selection ? '' : getSelectionText ( editor ) ;
97
95
98
96
const controlID = useUUID ( ) ;
99
97
const openMediaLibrary = useMediaInsert (
@@ -106,21 +104,19 @@ const InsertLinkToolbarButton: FC<InsertLinkToolbarButtonProps> = ({
106
104
) ;
107
105
108
106
const handleOpenMediaLibrary = useCallback ( ( ) => {
109
- setSelection ( editor . selection ?. focus . path ) ;
110
107
openMediaLibrary ( ) ;
111
- } , [ editor . selection , openMediaLibrary ] ) ;
108
+ } , [ openMediaLibrary ] ) ;
112
109
113
- return (
110
+ return ! isLink ? (
114
111
< ToolbarButton
115
112
label = "Link"
116
113
tooltip = "Insert link"
117
114
icon = { LinkIcon }
118
115
onClick = { handleOpenMediaLibrary }
119
- active = { isLink }
120
116
disabled = { disabled }
121
117
variant = { variant }
122
118
/>
123
- ) ;
119
+ ) : null ;
124
120
} ;
125
121
126
122
export default InsertLinkToolbarButton ;
0 commit comments