@@ -205,71 +205,74 @@ export const PlainEditor = forwardRef<PlainEditorInterface, Props>(
205205 }
206206 } , [ spellcheck , previousSpellcheck ] )
207207
208- const onRef = ( ref : HTMLTextAreaElement | null ) => {
209- if ( tabObserverDisposer . current || ! ref ) {
210- return
211- }
212-
213- log ( LoggingDomain . NoteView , 'On system editor ref' )
214-
215- /**
216- * Insert 4 spaces when a tab key is pressed, only used when inside of the text editor.
217- * If the shift key is pressed first, this event is not fired.
218- */
219- const editor = document . getElementById ( ElementIds . NoteTextEditor ) as HTMLInputElement
220-
221- if ( ! editor ) {
222- console . error ( 'Editor is not yet mounted; unable to add tab observer.' )
223- return
224- }
208+ const onRef = useCallback (
209+ ( ref : HTMLTextAreaElement | null ) => {
210+ if ( tabObserverDisposer . current || ! ref ) {
211+ return
212+ }
225213
226- tabObserverDisposer . current = application . keyboardService . addCommandHandler ( {
227- element : editor ,
228- command : TAB_COMMAND ,
229- onKeyDown : ( event ) => {
230- if ( document . hidden || note . current . locked || event . shiftKey ) {
231- return
232- }
233- event . preventDefault ( )
234- /** Using document.execCommand gives us undo support */
235- const insertSuccessful = document . execCommand ( 'insertText' , false , '\t' )
236- if ( ! insertSuccessful ) {
237- /** document.execCommand works great on Chrome/Safari but not Firefox */
238- const start = editor . selectionStart || 0
239- const end = editor . selectionEnd || 0
240- const spaces = ' '
241- /** Insert 4 spaces */
242- editor . value = editor . value . substring ( 0 , start ) + spaces + editor . value . substring ( end )
243- /** Place cursor 4 spaces away from where the tab key was pressed */
244- editor . selectionStart = editor . selectionEnd = start + 4
245- }
214+ log ( LoggingDomain . NoteView , 'On system editor ref' )
246215
247- setEditorText ( editor . value )
216+ /**
217+ * Insert 4 spaces when a tab key is pressed, only used when inside of the text editor.
218+ * If the shift key is pressed first, this event is not fired.
219+ */
220+ const editor = document . getElementById ( ElementIds . NoteTextEditor ) as HTMLInputElement
248221
249- void controller . saveAndAwaitLocalPropagation ( {
250- text : editor . value ,
251- bypassDebouncer : true ,
252- isUserModified : true ,
253- } )
254- } ,
255- } )
222+ if ( ! editor ) {
223+ console . error ( 'Editor is not yet mounted; unable to add tab observer.' )
224+ return
225+ }
256226
257- const observer = new MutationObserver ( ( records ) => {
258- for ( const record of records ) {
259- record . removedNodes . forEach ( ( node ) => {
260- if ( node . isEqualNode ( editor ) ) {
261- tabObserverDisposer . current ?.( )
262- tabObserverDisposer . current = undefined
263- observer . disconnect ( )
227+ tabObserverDisposer . current = application . keyboardService . addCommandHandler ( {
228+ element : editor ,
229+ command : TAB_COMMAND ,
230+ onKeyDown : ( event ) => {
231+ if ( document . hidden || note . current . locked || event . shiftKey ) {
232+ return
233+ }
234+ event . preventDefault ( )
235+ /** Using document.execCommand gives us undo support */
236+ const insertSuccessful = document . execCommand ( 'insertText' , false , '\t' )
237+ if ( ! insertSuccessful ) {
238+ /** document.execCommand works great on Chrome/Safari but not Firefox */
239+ const start = editor . selectionStart || 0
240+ const end = editor . selectionEnd || 0
241+ const spaces = ' '
242+ /** Insert 4 spaces */
243+ editor . value = editor . value . substring ( 0 , start ) + spaces + editor . value . substring ( end )
244+ /** Place cursor 4 spaces away from where the tab key was pressed */
245+ editor . selectionStart = editor . selectionEnd = start + 4
264246 }
265- } )
266- }
267- } )
268247
269- observer . observe ( editor . parentElement as HTMLElement , { childList : true } )
248+ setEditorText ( editor . value )
249+
250+ void controller . saveAndAwaitLocalPropagation ( {
251+ text : editor . value ,
252+ bypassDebouncer : true ,
253+ isUserModified : true ,
254+ } )
255+ } ,
256+ } )
257+
258+ const observer = new MutationObserver ( ( records ) => {
259+ for ( const record of records ) {
260+ record . removedNodes . forEach ( ( node ) => {
261+ if ( node . isEqualNode ( editor ) ) {
262+ tabObserverDisposer . current ?.( )
263+ tabObserverDisposer . current = undefined
264+ observer . disconnect ( )
265+ }
266+ } )
267+ }
268+ } )
270269
271- mutationObserver . current = observer
272- }
270+ observer . observe ( editor . parentElement as HTMLElement , { childList : true } )
271+
272+ mutationObserver . current = observer
273+ } ,
274+ [ application . keyboardService , controller ] ,
275+ )
273276
274277 if ( textareaUnloading ) {
275278 return null
0 commit comments