1
1
import { findParentNodeClosestToPos , posToDOMRect } from "@tiptap/core" ;
2
- import { useMemo } from "react" ;
2
+ import { useMemo , useCallback , useEffect , useState } from "react" ;
3
3
import { makeStyles } from "tss-react/mui" ;
4
4
import type { Except } from "type-fest" ;
5
5
import { type PopoverVirtualElement } from "@mui/material" ;
@@ -13,14 +13,8 @@ import { useRichTextEditorContext } from "../context";
13
13
import TableMenuControls , {
14
14
type TableMenuControlsProps ,
15
15
} from "./table-menu-controls" ;
16
- import useDebouncedFocus from "../hooks/use-debounced-focus" ;
17
- import DebounceRender , {
18
- type DebounceRenderProps ,
19
- } from "../utils/debounce-render" ;
20
16
21
17
export type TableBubbleMenuProps = {
22
- disableDebounce ?: boolean ;
23
- DebounceProps ?: Except < DebounceRenderProps , "children" > ;
24
18
labels ?: TableMenuControlsProps [ "labels" ] ;
25
19
} & Partial < Except < ControlledBubbleMenuProps , "open" | "editor" | "children" > > ;
26
20
@@ -34,15 +28,12 @@ const useStyles = makeStyles({
34
28
} ) ) ;
35
29
36
30
export default function TableBubbleMenu ( {
37
- disableDebounce = false ,
38
- DebounceProps,
39
31
labels,
40
32
...controlledBubbleMenuProps
41
33
} : TableBubbleMenuProps ) {
42
34
const editor = useRichTextEditorContext ( ) ;
43
35
const { classes } = useStyles ( ) ;
44
-
45
- const isEditorFocusedDebounced = useDebouncedFocus ( { editor } ) ;
36
+ const [ isManuallyHidden , setIsManuallyHidden ] = useState ( false ) ;
46
37
47
38
const bubbleMenuAnchorEl = useMemo (
48
39
( ) =>
@@ -61,7 +52,7 @@ export default function TableBubbleMenu({
61
52
nearestTableParent . pos
62
53
) as Maybe < HTMLElement | undefined > ;
63
54
64
- const tableDomNode = wrapperDomNode ?. querySelector ( "admin " ) ;
55
+ const tableDomNode = wrapperDomNode ?. querySelector ( "table " ) ;
65
56
if ( tableDomNode ) {
66
57
return tableDomNode . getBoundingClientRect ( ) ;
67
58
}
@@ -77,18 +68,52 @@ export default function TableBubbleMenu({
77
68
[ editor ]
78
69
) ;
79
70
71
+ const handleClose = useCallback ( ( ) => {
72
+ setIsManuallyHidden ( true ) ;
73
+ if ( editor ) {
74
+ editor . commands . blur ( ) ;
75
+ }
76
+ } , [ editor ] ) ;
77
+
78
+ useEffect ( ( ) => {
79
+ if ( ! editor ?. isActive ( "table" ) ) {
80
+ setIsManuallyHidden ( false ) ;
81
+ }
82
+ } , [ editor ?. isActive ( "table" ) ] ) ;
83
+
84
+ useEffect ( ( ) => {
85
+ const handleKeyDown = ( event : KeyboardEvent ) => {
86
+ if ( event . key === "Escape" && editor ?. isActive ( "table" ) ) {
87
+ handleClose ( ) ;
88
+ }
89
+ } ;
90
+
91
+ document . addEventListener ( "keydown" , handleKeyDown ) ;
92
+ return ( ) => {
93
+ document . removeEventListener ( "keydown" , handleKeyDown ) ;
94
+ } ;
95
+ } , [ editor , handleClose ] ) ;
96
+
80
97
if ( ! editor ?. isEditable ) {
81
98
return null ;
82
99
}
83
100
84
101
const controls = (
85
- < TableMenuControls className = { classes . controls } labels = { labels } />
102
+ < TableMenuControls
103
+ className = { classes . controls }
104
+ labels = { labels }
105
+ onClose = { handleClose }
106
+ />
86
107
) ;
87
108
109
+ // Логика показа меню - активная таблица и не скрыто вручную
110
+ const shouldShowMenu = editor . isActive ( "table" ) && ! isManuallyHidden ;
111
+
88
112
return (
89
113
< ControlledBubbleMenu
90
114
editor = { editor }
91
- open = { isEditorFocusedDebounced && editor . isActive ( "table" ) }
115
+ open = { shouldShowMenu }
116
+ onClose = { handleClose }
92
117
anchorEl = { bubbleMenuAnchorEl as PopoverVirtualElement }
93
118
placement = { {
94
119
anchorOrigin : { vertical : "top" , horizontal : "left" } ,
@@ -104,11 +129,7 @@ export default function TableBubbleMenu({
104
129
flipPadding = { { top : 35 , left : 8 , right : 8 , bottom : - Infinity } }
105
130
{ ...controlledBubbleMenuProps }
106
131
>
107
- { disableDebounce ? (
108
- controls
109
- ) : (
110
- < DebounceRender { ...DebounceProps } > { controls } </ DebounceRender >
111
- ) }
132
+ { controls }
112
133
</ ControlledBubbleMenu >
113
134
) ;
114
135
}
0 commit comments