@@ -82,72 +82,59 @@ class SelectionTooltip implements PluginView {
82
82
private normalizeUrl ;
83
83
84
84
private view : EditorView ;
85
+ private show : boolean ;
85
86
86
87
private textNode ?: ReturnType < typeof getTextNode > ;
87
88
private textNodeRef : HTMLElement | undefined ;
88
89
89
- private isTooltipOpen = false ;
90
90
private manualHidden = false ;
91
91
92
92
private renderItem ?: RendererItem ;
93
- private selectionTooltipProps : SelectionTooltipViewBaseProps = { show : false } ;
94
93
95
94
constructor ( view : EditorView , deps : ExtensionDeps ) {
96
95
this . normalizeUrl = normalizeUrlFactory ( deps ) ;
97
96
98
97
this . view = view ;
98
+ this . show = false ;
99
99
100
- this . update ( view , null ) ;
100
+ this . update ( view ) ;
101
101
}
102
102
103
- update ( view : EditorView , prevState ?: EditorState | null ) {
103
+ update ( view : EditorView ) {
104
104
if ( ! view . dom . parentNode ) {
105
105
this . hideTooltip ( ) ;
106
106
return ;
107
107
}
108
108
109
109
const { state} = view ;
110
110
111
- if ( prevState && prevState . doc . eq ( state . doc ) && prevState . selection . eq ( state . selection ) )
112
- return ;
113
-
114
- this . textNode = getTextNode ( view . state ) ;
111
+ this . textNode = getTextNode ( state ) ;
115
112
116
113
const prevRef = this . textNodeRef ;
117
- this . updateTextNodeRef ( ) ;
118
-
119
- if ( ! this . textNode || ! this . textNodeRef ) {
120
- this . hideTooltip ( ) ;
121
- return ;
122
- }
114
+ this . textNodeRef = this . view . dom . getElementsByClassName ( className ) [ 0 ] as
115
+ | HTMLElement
116
+ | undefined ;
123
117
124
118
if ( prevRef !== this . textNodeRef ) {
125
119
this . manualHidden = false ;
126
120
}
127
121
128
- if ( this . manualHidden ) {
122
+ if ( this . manualHidden || ! this . textNode || ! this . textNodeRef ) {
129
123
this . hideTooltip ( ) ;
130
- } else {
131
- this . renderTooltip ( {
132
- show : true ,
133
- domElem : this . textNodeRef ,
134
- onCancel : ( ) => this . cancelPopup ( ) ,
135
- attrs : this . getMarkAttrs ( ) ,
136
- onChange : this . changeAttrs . bind ( this ) ,
137
- onOpenChange : this . onOpenChange ,
138
- } ) ;
124
+ return ;
139
125
}
126
+
127
+ this . showTooltip ( ) ;
140
128
}
141
129
142
130
destroy ( ) {
143
- this . isTooltipOpen = false ;
144
- this . selectionTooltipProps = { show : false } ;
131
+ this . show = false ;
145
132
this . renderItem ?. remove ( ) ;
146
133
this . renderItem = undefined ;
147
134
}
148
135
149
136
onEscapeDown ( ) : boolean {
150
- if ( this . isTooltipOpen ) {
137
+ if ( this . show ) {
151
138
this . removePlaceholderLink ( this . textNode ) ;
152
139
this . manualHidden = true ;
153
140
this . hideTooltip ( ) ;
@@ -157,38 +144,37 @@ class SelectionTooltip implements PluginView {
157
144
return false ;
158
145
}
159
146
160
- private updateTextNodeRef ( ) {
161
- const decoElem = this . view . dom . getElementsByClassName ( className ) [ 0 ] ;
162
- this . textNodeRef = decoElem as HTMLElement | undefined ;
163
- }
164
-
165
147
private getMarkAttrs ( ) {
166
148
const { textNode} = this ;
167
149
const linkMark = textNode && findMark ( textNode . node , linkType ( this . view . state . schema ) ) ;
168
150
return linkMark ?. attrs ;
169
151
}
170
152
153
+ private renderTooltip ( ) {
154
+ this . renderItem = this . renderItem ?? this . createRenderItem ( ) ;
155
+ this . renderItem . rerender ( ) ;
156
+ }
157
+
171
158
private hideTooltip ( ) {
172
- this . renderTooltip ( { show : false } ) ;
159
+ this . show = false ;
160
+ this . renderTooltip ( ) ;
173
161
}
174
162
175
- private renderTooltip ( props : SelectionTooltipViewBaseProps ) {
176
- this . isTooltipOpen = props . show ;
177
- this . selectionTooltipProps = props ;
178
- this . renderItem = this . renderItem ?? this . createRenderItem ( ) ;
179
- this . renderItem . rerender ( ) ;
163
+ private showTooltip ( ) {
164
+ this . show = true ;
165
+ this . renderTooltip ( ) ;
180
166
}
181
167
182
168
private onOpenChange : NonNullable < PopupProps [ 'onOpenChange' ] > = ( open , _e , reason ) => {
183
169
if ( open ) return ;
184
170
if ( reason === 'escape-key' ) {
185
171
this . cancelPopup ( ) ;
186
172
} else {
187
- this . onOutisdeClick ( ) ;
173
+ this . onOutsideClick ( ) ;
188
174
}
189
175
} ;
190
176
191
- private onOutisdeClick = ( ) => {
177
+ private onOutsideClick = ( ) => {
192
178
this . removePlaceholderLink ( this . textNode ) ;
193
179
this . hideTooltip ( ) ;
194
180
this . manualHidden = true ;
@@ -239,7 +225,14 @@ class SelectionTooltip implements PluginView {
239
225
private createRenderItem ( ) {
240
226
return getReactRendererFromState ( this . view . state ) . createItem ( 'link-tooltip' , ( ) => (
241
227
< ErrorLoggerBoundary >
242
- < SelectionTooltipView { ...this . selectionTooltipProps } />
228
+ < SelectionTooltipView
229
+ show = { this . show }
230
+ domElem = { this . textNodeRef as HTMLElement }
231
+ onCancel = { this . cancelPopup . bind ( this ) }
232
+ attrs = { this . getMarkAttrs ( ) }
233
+ onChange = { this . changeAttrs . bind ( this ) }
234
+ onOpenChange = { this . onOpenChange }
235
+ />
243
236
</ ErrorLoggerBoundary >
244
237
) ) ;
245
238
}
0 commit comments