@@ -41,6 +41,14 @@ import {
4141 findBlockElementFromSelection ,
4242} from "~/utils/renderTextSelectionPopup" ;
4343
44+ const debounce = ( fn : ( ) => void , delay = 250 ) => {
45+ let timeout : number ;
46+ return ( ) => {
47+ clearTimeout ( timeout ) ;
48+ timeout = window . setTimeout ( fn , delay ) ;
49+ } ;
50+ } ;
51+
4452export const initObservers = async ( {
4553 onloadArgs,
4654} : {
@@ -247,14 +255,11 @@ export const initObservers = async ({
247255 }
248256 } ;
249257
250- const nodeCreationPopoverListener = ( ) => {
258+ const nodeCreationPopoverListener = debounce ( ( ) => {
251259 const isTextSelectionPopupEnabled =
252260 onloadArgs . extensionAPI . settings . get ( "text-selection-popup" ) !== false ;
253261
254- if ( ! isTextSelectionPopupEnabled ) {
255- removeTextSelectionPopup ( ) ;
256- return ;
257- }
262+ if ( ! isTextSelectionPopupEnabled ) return ;
258263
259264 const selection = window . getSelection ( ) ;
260265
@@ -275,6 +280,7 @@ export const initObservers = async ({
275280 if ( blockElement ) {
276281 const textarea = blockElement . querySelector ( "textarea" ) ;
277282 if ( ! textarea ) return ;
283+
278284 renderTextSelectionPopup ( {
279285 extensionAPI : onloadArgs . extensionAPI ,
280286 blockElement,
@@ -283,7 +289,7 @@ export const initObservers = async ({
283289 } else {
284290 removeTextSelectionPopup ( ) ;
285291 }
286- } ;
292+ } , 150 ) ;
287293
288294 return {
289295 observers : [
0 commit comments