Newer
Older
import React, {
useMemo,
useContext,
useState,
useEffect,
useRef,
createRef,
} from 'react';
import { WaxContext, Icon, useOnClickOutside } from 'wax-prosemirror-core';
display: flex;
`;
const ButtonWrapper = styled.div`
display: flex;
justify-content: center;
align-items: center;
background: #fff;
display: flex;
flex-direction: column;
border: 1px solid #ddd;
border-radius: 0.25rem;
box-shadow: 0 0.2rem 0.4rem rgb(0 0 0 / 10%);
const StyledIcon = styled(Icon)`
height: 18px;
width: 18px;
margin-left: auto;
`;
const Translation = ({ label, defaultTrans }) => {
return (
<>{!isEmpty(i18n) && i18n.exists(label) ? t(label) : defaultTrans}</>
);
};
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
{
label: (
<Translation
defaultTrans="Delete table"
label="Wax.Tables.Delete table"
/>
),
value: 'deleteTable',
},
{
label: (
<Translation
defaultTrans="Add Caption"
label="Wax.Tables.Add Caption"
/>
),
value: 'addCaption',
},
{
label: (
<Translation
defaultTrans="Delete Caption"
label="Wax.Tables.Delete Caption"
/>
),
value: 'deleteCaption',
},
{
label: (
<Translation
defaultTrans="Add column before"
label="Wax.Tables.Add column before"
/>
),
value: 'addColumnBefore',
},
{
label: (
<Translation
defaultTrans="Add column after"
label="Wax.Tables.Add column after"
/>
),
value: 'addColumnAfter',
},
{
label: (
<Translation
defaultTrans="Delete column"
label="Wax.Tables.Delete column"
/>
),
value: 'deleteColumn',
},
{
label: (
<Translation
defaultTrans="Insert row before"
label="Wax.Tables.Insert row before"
/>
),
value: 'addRowBefore',
},
{
label: (
<Translation
defaultTrans="Insert row after"
label="Wax.Tables.Insert row after"
/>
),
value: 'addRowAfter',
},
{
label: (
<Translation defaultTrans="Delete row" label="Wax.Tables.Delete row" />
),
value: 'deleteRow',
},
{
label: (
<Translation
defaultTrans="Merge cells"
label="Wax.Tables.Merge cells"
/>
),
value: 'mergeCells',
},
{
label: (
<Translation defaultTrans="Split cell" label="Wax.Tables.Split cell" />
),
value: 'splitCell',
},
{
label: (
<Translation
defaultTrans="Toggle header column"
label="Wax.Tables.Toggle header column"
/>
),
value: 'toggleHeaderColumn',
},
{
label: (
<Translation
defaultTrans="Toggle header row"
label="Wax.Tables.Toggle header row"
/>
),
value: 'toggleHeaderRow',
},
{
label: (
<Translation
defaultTrans="Toggle header cells"
label="Wax.Tables.Toggle header cells"
/>
),
value: 'toggleHeaderCell',
},
const [isOpen, setIsOpen] = useState(false);
const isDisabled = !item.select(activeView.state);
useEffect(() => {
if (isDisabled) setIsOpen(false);
}, [isDisabled]);
// arrow down
if (e.keyCode === 40) {
if (index === itemRefs.current.length - 1) {
itemRefs.current[0].current.focus();
} else {
itemRefs.current[index + 1].current.focus();
}
}
// arrow up
if (e.keyCode === 38) {
if (index === 0) {
itemRefs.current[itemRefs.current.length - 1].current.focus();
} else {
itemRefs.current[index - 1].current.focus();
}
}
// enter
if (e.keyCode === 13) {
itemRefs.current[index].current.click();
}
// ESC
if (e.keyCode === 27) {
const TableDropDownComponent = useMemo(
() => (
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<ButtonWrapper>
<DropDownButton
aria-controls="table-options"
aria-expanded={isOpen}
aria-haspopup
disabled={isDisabled}
onKeyDown={e => {
if (e.keyCode === 40) {
itemRefs.current[0].current.focus();
}
if (e.keyCode === 27) {
setIsOpen(false);
}
if (e.keyCode === 13 || e.keyCode === 32) {
setIsOpen(true);
}
}}
onMouseDown={openCloseMenu}
type="button"
>
<span>
{!isEmpty(i18n) && i18n.exists('Wax.Tables.Table Options')
? t('Wax.Tables.Table Options')
: 'Table Options'}
</span>
<StyledIcon name="expand" />
</DropDownButton>
</ButtonWrapper>
<DropDownMenu
aria-label="Choose a table action"
id="table-options"
isOpen={isOpen}
role="menu"
>
itemRefs.current[index] = itemRefs.current[index] || createRef();
item.run(
activeView.state,
activeView.dispatch,
tablesFn[option.value],
);
}}
onKeyDown={e => onKeyDown(e, index)}
ref={itemRefs.current[index]}
role="menuitem"
tabIndex="-1"
>
{option.label}
</span>
);
})}
</DropDownMenu>