PluginProbe
TablePress – Tables in WordPress made easy / 3.0.4
TablePress – Tables in WordPress made easy v3.0.4
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 / data / contextmenu.js

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

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