@@ -15,6 +15,7 @@ import getCursorOffset from '../../lib/helpers/caret/getCursorOffset';
15
15
import ContentEditable from 'react-contenteditable' ;
16
16
import { sanitize } from 'dompurify' ;
17
17
import focusElement from '../../lib/helpers/focusElement' ;
18
+ import isElementFocused from '../../lib/helpers/isElementFocused' ;
18
19
19
20
const TextBlock = ( props : EditableText ) => {
20
21
const {
@@ -359,7 +360,13 @@ const TextBlock = (props: EditableText) => {
359
360
* Ensure the cursor is never past the completion
360
361
*/
361
362
useEffect ( ( ) => {
362
- if ( ! editableRef . current ) return ;
363
+ if ( ! editableRef . current || ! state . value ) return ;
364
+
365
+ if ( ! isElementFocused ( editableRef . current ) ) {
366
+ setCompletion ( null ) ;
367
+ setCompletionTimeout ( null ) ;
368
+ return ;
369
+ }
363
370
364
371
const caretOffset = getCursorOffset ( editableRef . current ) ;
365
372
@@ -414,6 +421,7 @@ const TextBlock = (props: EditableText) => {
414
421
}
415
422
416
423
setCompletion ( null ) ;
424
+ setCompletionTimeout ( null ) ;
417
425
418
426
const value = saveBlock ( editableRef . current , completion ) ;
419
427
@@ -453,80 +461,7 @@ const TextBlock = (props: EditableText) => {
453
461
/>
454
462
{ slashMenu }
455
463
</ >
456
- )
457
-
458
- // return (
459
- // <ContentEditable
460
- // className={`min-h-[1.2em] outline-none relative whitespace-pre-wrap w-full ${TextStyles[type]}`}
461
- // role="textbox"
462
- // tabIndex={0}
463
- // contentEditable={isAllowedToEdit}
464
- // suppressContentEditableWarning
465
- // ref={isAllowedToEdit ? editableRef as unknown as React.RefObject<typeof ContentEditable> : null}
466
- // id={`block-${blockID}`}
467
- // data-block-index={index}
468
- // onInput={(_) => {
469
- // if (!editableRef.current) return;
470
-
471
- // handlePotentialTypeChange(editableRef.current);
472
- // handlePotentialInlineBlocks(editableRef.current);
473
-
474
- // if (completionTimeout) {
475
- // clearTimeout(completionTimeout);
476
- // }
477
-
478
- // setCompletion(null);
479
-
480
- // if (
481
- // getCursorOffset(editableRef.current) < (editableRef.current.innerText.length - 2)
482
- // || editableRef.current.innerText.length <= 1
483
- // ) return;
484
-
485
- // setCompletionTimeout(
486
- // setTimeout(
487
- // createCompletion,
488
- // 500
489
- // )
490
- // );
491
- // }}
492
- // onBlur={
493
- // (e) => {
494
- // if (completionTimeout) {
495
- // clearTimeout(completionTimeout);
496
- // }
497
-
498
- // setCompletion(null);
499
- // setCompletionTimeout(null);
500
- // saveBlock(e.currentTarget);
501
- // }
502
- // }
503
- // onKeyDown={
504
- // (e) => {
505
- // if (!editableRef.current) return;
506
-
507
- // if (e.code === 'Enter' && !e.shiftKey) {
508
- // e.preventDefault();
509
- // e.currentTarget.blur();
510
- // addBlockAtIndex(index + 1, page, pageData, setPageData);
511
- // } else if (e.code === 'Backspace' && type !== 'text' && getCursorOffset(editableRef.current) === 0) {
512
- // setCurrentBlockType('text');
513
- // editBlock([blockID], 'text', undefined, page);
514
- // } else if (e.code === 'Backspace' && type === 'text' && (editableRef.current.innerText === '' || editableRef.current.innerText === '\n')) {
515
- // removeBlock(index, [blockID], page, pageData, setPageData, true);
516
- // }
517
- // }
518
- // }
519
- // >
520
- // {
521
- // renderInlineBlocks(
522
- // properties.value,
523
- // properties.style
524
- // )
525
- // }
526
- // {completion}
527
- // {slashMenu}
528
- // </ContentEditable>
529
- // );
464
+ ) ;
530
465
} ;
531
466
532
467
export default TextBlock ;
0 commit comments