File size: 1,583 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const { Menu, MenuItem } = require( 'electron' );

module.exports = function ( { view } ) {
	view.webContents.on( 'context-menu', ( event, params ) => {
		const menu = new Menu();

		const copy = new MenuItem( { label: 'Copy', role: 'copy' } );

		if ( ! params.isEditable ) {
			// If text is not editable, only permit the `Copy` action
			menu.append( copy );
		} else {
			// Add each spelling suggestion
			for ( const suggestion of params.dictionarySuggestions ) {
				menu.append(
					new MenuItem( {
						label: suggestion,
						click: () => view.webContents.replaceMisspelling( suggestion ),
					} )
				);
			}

			// Allow users to add the misspelled word to the dictionary
			if ( params.misspelledWord ) {
				menu.append( new MenuItem( { type: 'separator' } ) );
				menu.append(
					new MenuItem( {
						label: 'Add to Dictionary',
						click: () =>
							view.webContents.session.addWordToSpellCheckerDictionary( params.misspelledWord ),
					} )
				);
			}

			// If text is editable, permit the Select All, Cut, Copy and Paste actions
			const cut = new MenuItem( { label: 'Cut', role: 'cut' } );
			const paste = new MenuItem( { label: 'Paste', role: 'paste' } );
			const selectAll = new MenuItem( { label: 'Select All', role: 'selectAll' } );

			const menuItems = [ selectAll, cut, copy, paste ];

			if ( params && params.dictionarySuggestions && params.dictionarySuggestions.length > 0 ) {
				menu.append( new MenuItem( { type: 'separator' } ) );
			}

			for ( const item of menuItems ) {
				menu.append( item );
			}
		}

		menu.popup();
	} );
};