PluginProbe
TablePress – Tables in WordPress made easy / 2.2.5
TablePress – Tables in WordPress made easy v2.2.5
3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 2.4.4 All 44 releases
tablepress / admin / js / edit / contextmenu.js

contextmenu.js in TablePress – Tables in WordPress made easy 2.2.5, at admin/js/edit/contextmenu.js

338 lines 14.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Definition of the contextmenu for Jspreadsheet on the "Edit" screen.
3 *
4 * @package TablePress
5 * @subpackage Views JavaScript
6 * @author Tobias Bäthge
7 * @since 2.0.0
8 */
9
10 /* globals tp */
11 /* eslint-disable jsdoc/check-param-names, jsdoc/valid-types, jsdoc/no-undefined-types */
12
13 /**
14 * WordPress dependencies.
15 */
16 import { __, _x, _n, sprintf } from '@wordpress/i18n';
17
18 /**
19 * Returns the entries for the table editor's context menu.
20 *
21 * @param {[type]} obj [description]
22 * @param {[type]} x [description]
23 * @param {[type]} y [description]
24 * @param {[type]} e [description]
25 * @return {Array} Context menu items.
26 */
27 const contextMenu = ( obj /*, x, y, e */ ) => {
28 const num_rows = tp.editor.options.data.length;
29 const num_columns = tp.editor.options.columns.length;
30 const num_selected_rows = tp.helpers.selection.rows.length;
31 const num_selected_columns = tp.helpers.selection.columns.length;
32 const is_mac = window?.navigator?.platform?.includes( 'Mac' );
33 const meta_key = is_mac ?
34 _x( '', 'keyboard shortcut modifier key on a Mac keyboard', 'tablepress' ) :
35 _x( 'Ctrl+', 'keyboard shortcut modifier key on a non-Mac keyboard', 'tablepress' );
36 const option_key = is_mac ?
37 _x( '', 'keyboard shortcut option key on a Mac keyboard', 'tablepress' ) :
38 _x( 'Alt+', 'keyboard shortcut Alt key on a non-Mac keyboard', 'tablepress' );
39
40 // Call-by-reference object for the cell_merge_allowed() call.
41 const error_message = {
42 text: ''
43 };
44
45 tp.helpers.visibility.update();
46
47 const items = [
48 // Undo/Redo.
49 {
50 title: __( 'Undo', 'tablepress' ),
51 shortcut: sprintf( _x( '%1$sZ', 'keyboard shortcut for Undo', 'tablepress' ), meta_key ),
52 onclick: obj.undo,
53 disabled: ( -1 === obj.historyIndex ),
54 },
55 {
56 title: __( 'Redo', 'tablepress' ),
57 shortcut: sprintf( _x( '%1$sY', 'keyboard shortcut for Redo', 'tablepress' ), meta_key ),
58 onclick: obj.redo,
59 disabled: ( obj.historyIndex === obj.history.length - 1 ),
60 },
61
62 // Cut/Copy/Paste.
63 {
64 type: 'divisor',
65 },
66 {
67 title: __( 'Cut', 'tablepress' ),
68 shortcut: sprintf( _x( '%1$sX', 'keyboard shortcut for Cut', 'tablepress' ), meta_key ),
69 onclick() {
70 /* eslint-disable @wordpress/no-global-active-element */
71 if ( 'TEXTAREA' === document.activeElement.tagName && document.activeElement.selectionStart !== document.activeElement.selectionEnd ) {
72 document.execCommand( 'copy' ); // If text is selected in the actively edited cell, only copy that.
73 const cursorPosition = document.activeElement.selectionStart;
74 document.activeElement.value = document.activeElement.value.slice( 0, document.activeElement.selectionStart ) + document.activeElement.value.slice( document.activeElement.selectionEnd ); // Cut the selected content.
75 document.activeElement.selectionEnd = cursorPosition;
76 } else {
77 obj.copy( true ); // Otherwise, copy highlighted cells.
78 obj.setValue( obj.highlighted, '' ); // Make cell content empty.
79 }
80 /* eslint-enable @wordpress/no-global-active-element */
81 },
82 },
83 {
84 title: __( 'Copy', 'tablepress' ),
85 shortcut: sprintf( _x( '%1$sC', 'keyboard shortcut for Copy', 'tablepress' ), meta_key ),
86 onclick() {
87 if ( 'TEXTAREA' === document.activeElement.tagName && document.activeElement.selectionStart !== document.activeElement.selectionEnd ) { // eslint-disable-line @wordpress/no-global-active-element
88 document.execCommand( 'copy' ); // If text is selected in the actively edited cell, only copy that.
89 } else {
90 obj.copy( true ); // Otherwise, copy highlighted cells.
91 }
92 },
93 },
94 {
95 title: __( 'Paste', 'tablepress' ),
96 shortcut: sprintf( _x( '%1$sV', 'keyboard shortcut for Paste', 'tablepress' ), meta_key ),
97 onclick() {
98 /* eslint-disable @wordpress/no-global-active-element */
99 if ( 'TEXTAREA' === document.activeElement.tagName ) {
100 window.navigator.clipboard.readText().then( ( text ) => {
101 if ( text ) {
102 const cursorPosition = document.activeElement.selectionStart + text.length;
103 document.activeElement.value = document.activeElement.value.slice( 0, document.activeElement.selectionStart ) + text + document.activeElement.value.slice( document.activeElement.selectionEnd ); // Paste at the selection.
104 document.activeElement.selectionEnd = cursorPosition;
105 }
106 } );
107 } else if ( obj.selectedCell ) {
108 window.navigator.clipboard.readText().then( ( text ) => {
109 if ( text ) {
110 obj.paste( obj.selectedCell[0], obj.selectedCell[1], text );
111 }
112 } );
113 }
114 /* eslint-enable @wordpress/no-global-active-element */
115 },
116 // Firefox does not offer the readText() method, so "Paste" needs to be disabled.
117 disabled: ! window?.navigator?.clipboard?.readText,
118 tooltip: ! window?.navigator?.clipboard?.readText ? __( 'Your browser does not allow pasting via the context menu. Use the keyboard shortcut instead.', 'tablepress' ) : '',
119 },
120
121 // Insert Link, Insert Image, Open Advanced Editor.
122 {
123 type: 'divisor',
124 },
125 {
126 title: __( 'Insert Link', 'tablepress' ),
127 shortcut: sprintf( _x( '%1$sL', 'keyboard shortcut for Insert Link', 'tablepress' ), meta_key ),
128 onclick: tp.callbacks.insert_link.open_dialog.bind( null, ( 'TEXTAREA' === document.activeElement.tagName ) ? document.activeElement : null ), // eslint-disable-line @wordpress/no-global-active-element
129 },
130 {
131 title: __( 'Insert Image', 'tablepress' ),
132 shortcut: sprintf( _x( '%1$sI', 'keyboard shortcut for Insert Image', 'tablepress' ), meta_key ),
133 onclick: tp.callbacks.insert_image.open_dialog.bind( null, ( 'TEXTAREA' === document.activeElement.tagName ) ? document.activeElement : null ), // eslint-disable-line @wordpress/no-global-active-element
134 },
135 {
136 title: __( 'Advanced Editor', 'tablepress' ),
137 shortcut: sprintf( _x( '%1$sE', 'keyboard shortcut for Advanced Editor', 'tablepress' ), meta_key ),
138 onclick: tp.callbacks.advanced_editor.open_dialog.bind( null, ( 'TEXTAREA' === document.activeElement.tagName ) ? document.activeElement : null ), // eslint-disable-line @wordpress/no-global-active-element
139 },
140
141 // Duplicate/Insert/Append/Delete.
142 {
143 type: 'divisor',
144 },
145 {
146 title: __( 'Duplicate …', 'tablepress' ),
147 submenu: [
148 {
149 title: _n( 'Duplicate row', 'Duplicate rows', num_selected_rows, 'tablepress' ),
150 onclick: tp.callbacks.insert_duplicate.bind( null, 'duplicate', 'rows' ),
151 },
152 {
153 title: _n( 'Duplicate column', 'Duplicate columns', num_selected_columns, 'tablepress' ),
154 onclick: tp.callbacks.insert_duplicate.bind( null, 'duplicate', 'columns' ),
155 },
156 ],
157 },
158 {
159 title: __( 'Insert …', 'tablepress' ),
160 submenu: [
161 {
162 title: _n( 'Insert row above', 'Insert rows above', num_selected_rows, 'tablepress' ),
163 onclick: tp.callbacks.insert_duplicate.bind( null, 'insert', 'rows', 'before' ),
164 },
165 {
166 title: _n( 'Insert row below', 'Insert rows below', num_selected_rows, 'tablepress' ),
167 onclick: tp.callbacks.insert_duplicate.bind( null, 'insert', 'rows', 'after' ),
168 },
169 {
170 title: _n( 'Insert column on the left', 'Insert columns on the left', num_selected_columns, 'tablepress' ),
171 onclick: tp.callbacks.insert_duplicate.bind( null, 'insert', 'columns', 'before' ),
172 },
173 {
174 title: _n( 'Insert column on the right', 'Insert columns on the right', num_selected_columns, 'tablepress' ),
175 onclick: tp.callbacks.insert_duplicate.bind( null, 'insert', 'columns', 'after' ),
176 },
177 ],
178 },
179 {
180 title: __( 'Append …', 'tablepress' ),
181 submenu: [
182 {
183 title: __( 'Append row', 'tablepress' ),
184 onclick: tp.callbacks.append.bind( null, 'rows', 1 ),
185 },
186 {
187 title: __( 'Append column', 'tablepress' ),
188 onclick: tp.callbacks.append.bind( null, 'columns', 1 ),
189 },
190 ],
191 },
192 {
193 title: __( 'Delete …', 'tablepress' ),
194 submenu: [
195 {
196 title: _n( 'Delete row', 'Delete rows', num_selected_rows, 'tablepress' ),
197 onclick: tp.callbacks.remove.bind( null, 'rows' ),
198 disabled: num_rows === num_selected_rows,
199 tooltip: num_rows === num_selected_rows ? __( 'This option is disabled.', 'tablepress' ) + ' ' + __( 'You can not delete all table rows!', 'tablepress' ) : '',
200 },
201 {
202 title: _n( 'Delete column', 'Delete columns', num_selected_columns, 'tablepress' ),
203 onclick: tp.callbacks.remove.bind( null, 'columns' ),
204 disabled: num_columns === num_selected_columns,
205 tooltip: num_columns === num_selected_columns ? __( 'This option is disabled.', 'tablepress' ) + ' ' + __( 'You can not delete all table columns!', 'tablepress' ) : '',
206 },
207 ],
208 },
209
210 // Move rows/columns, Sort by column.
211 {
212 type: 'divisor',
213 },
214 {
215 title: __( 'Move …', 'tablepress' ),
216 submenu: [
217 {
218 title: _n( 'Move row up', 'Move rows up', num_selected_rows, 'tablepress' ),
219 shortcut: sprintf( _x( '%1$s⇧↑', 'keyboard shortcut for Move up', 'tablepress' ), meta_key ),
220 onclick: tp.callbacks.move.bind( null, 'up', 'rows' ),
221 disabled: ! tp.helpers.move_allowed( 'rows', 'up' ),
222 },
223 {
224 title: _n( 'Move row down', 'Move rows down', num_selected_rows, 'tablepress' ),
225 shortcut: sprintf( _x( '%1$s⇧↓', 'keyboard shortcut for Move down', 'tablepress' ), meta_key ),
226 onclick: tp.callbacks.move.bind( null, 'down', 'rows' ),
227 disabled: ! tp.helpers.move_allowed( 'rows', 'down' ),
228 },
229 {
230 title: _n( 'Move column left', 'Move columns left', num_selected_columns, 'tablepress' ),
231 shortcut: sprintf( _x( '%1$s⇧←', 'keyboard shortcut for Move left', 'tablepress' ), meta_key ),
232 onclick: tp.callbacks.move.bind( null, 'left', 'columns' ),
233 disabled: ! tp.helpers.move_allowed( 'columns', 'left' ),
234 },
235 {
236 title: _n( 'Move column right', 'Move columns right', num_selected_columns, 'tablepress' ),
237 shortcut: sprintf( _x( '%1$s⇧→', 'keyboard shortcut for Move right', 'tablepress' ), meta_key ),
238 onclick: tp.callbacks.move.bind( null, 'right', 'columns' ),
239 disabled: ! tp.helpers.move_allowed( 'columns', 'right' ),
240 },
241 {
242 type: 'divisor',
243 },
244 {
245 title: _n( 'Move row to the top', 'Move rows to the top', num_selected_rows, 'tablepress' ),
246 shortcut: sprintf( _x( '%1$s%2$s⇧↑', 'keyboard shortcut for Move to the top', 'tablepress' ), meta_key, option_key ),
247 onclick: tp.callbacks.move.bind( null, 'top', 'rows' ),
248 disabled: ! tp.helpers.move_allowed( 'rows', 'top' ),
249 },
250 {
251 title: _n( 'Move row to the bottom', 'Move rows to the bottom', num_selected_rows, 'tablepress' ),
252 shortcut: sprintf( _x( '%1$s%2$s⇧↓', 'keyboard shortcut for Move to the bottom', 'tablepress' ), meta_key, option_key ),
253 onclick: tp.callbacks.move.bind( null, 'bottom', 'rows' ),
254 disabled: ! tp.helpers.move_allowed( 'rows', 'bottom' ),
255 },
256 {
257 title: _n( 'Move column to first', 'Move columns to first', num_selected_columns, 'tablepress' ),
258 shortcut: sprintf( _x( '%1$s%2$s⇧←', 'keyboard shortcut for Move to first', 'tablepress' ), meta_key, option_key ),
259 onclick: tp.callbacks.move.bind( null, 'first', 'columns' ),
260 disabled: ! tp.helpers.move_allowed( 'columns', 'first' ),
261 },
262 {
263 title: _n( 'Move column to last', 'Move columns to last', num_selected_columns, 'tablepress' ),
264 shortcut: sprintf( _x( '%1$s%2$s⇧→', 'keyboard shortcut for Move to last', 'tablepress' ), meta_key, option_key ),
265 onclick: tp.callbacks.move.bind( null, 'last', 'columns' ),
266 disabled: ! tp.helpers.move_allowed( 'columns', 'last' ),
267 },
268 ],
269 },
270 {
271 title: __( 'Sort by column …', 'tablepress' ),
272 submenu: [
273 {
274 title: __( 'Sort by column ascending', 'tablepress' ),
275 onclick: tp.callbacks.sort.bind( null, 'asc' ),
276 disabled: 1 !== num_selected_columns,
277 tooltip: 1 !== num_selected_columns ? __( 'This option is disabled because more than one column was selected.', 'tablepress' ) : '',
278 },
279 {
280 title: __( 'Sort by column descending', 'tablepress' ),
281 onclick: tp.callbacks.sort.bind( null, 'desc' ),
282 disabled: 1 !== num_selected_columns,
283 tooltip: 1 !== num_selected_columns ? __( 'This option is disabled because more than one column was selected.', 'tablepress' ) : '',
284 },
285 ],
286 },
287
288 // Hide/Show rows/columns.
289 {
290 type: 'divisor',
291 },
292 {
293 title: __( 'Hide/Show …', 'tablepress' ),
294 submenu: [
295 {
296 title: _n( 'Hide row', 'Hide rows', num_selected_rows, 'tablepress' ),
297 onclick: tp.callbacks.hide_unhide.bind( null, 'hide', 'rows' ),
298 disabled: ! tp.helpers.visibility.selection_contains( 'rows', 1 ),
299 tooltip: ! tp.helpers.visibility.selection_contains( 'rows', 1 ) ? __( 'This option is disabled because no visible rows were selected.', 'tablepress' ) : '',
300 },
301 {
302 title: _n( 'Hide column', 'Hide columns', num_selected_columns, 'tablepress' ),
303 onclick: tp.callbacks.hide_unhide.bind( null, 'hide', 'columns' ),
304 disabled: ! tp.helpers.visibility.selection_contains( 'columns', 1 ),
305 tooltip: ! tp.helpers.visibility.selection_contains( 'columns', 1 ) ? __( 'This option is disabled because no visible columns were selected.', 'tablepress' ) : '',
306 },
307 {
308 title: _n( 'Show row', 'Show rows', num_selected_rows, 'tablepress' ),
309 onclick: tp.callbacks.hide_unhide.bind( null, 'unhide', 'rows' ),
310 disabled: ! tp.helpers.visibility.selection_contains( 'rows', 0 ),
311 tooltip: ! tp.helpers.visibility.selection_contains( 'rows', 0 ) ? __( 'This option is disabled because no hidden rows were selected.', 'tablepress' ) : '',
312 },
313 {
314 title: _n( 'Show column', 'Show columns', num_selected_columns, 'tablepress' ),
315 onclick: tp.callbacks.hide_unhide.bind( null, 'unhide', 'columns' ),
316 disabled: ! tp.helpers.visibility.selection_contains( 'columns', 0 ),
317 tooltip: ! tp.helpers.visibility.selection_contains( 'columns', 0 ) ? __( 'This option is disabled because no hidden columns were selected.', 'tablepress' ) : '',
318 },
319 ],
320 },
321
322 // Merging/Unmerging cells.
323 {
324 type: 'divisor',
325 },
326 {
327 title: __( 'Combine/Merge cells', 'tablepress' ),
328 onclick: tp.callbacks.merge_cells,
329 disabled: ( 1 === num_selected_rows && 1 === num_selected_columns ) || ! tp.helpers.cell_merge_allowed( 'no-alert' ),
330 tooltip: ( 1 === num_selected_rows && 1 === num_selected_columns ) || ! tp.helpers.cell_merge_allowed( 'no-alert', error_message ) ? __( 'This option is disabled.', 'tablepress' ) + ' ' + error_message.text : '',
331 }
332 ];
333
334 return items;
335 };
336
337 export default contextMenu;
338