This repository has been archived by the owner on Feb 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
114 lines (104 loc) · 3.17 KB
/
index.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
var noop = function(){};
var defaults = require('lodash.defaults');
var isEmpty = require('lodash.isempty');
var isFunction = require('lodash.isfunction');
var isArray = require('lodash.isarray');
var cloneDeep = require('lodash.clonedeep');
var BrowserWindow = require('electron').BrowserWindow;
var Menu = require('electron').Menu;
var DEFAULT_MAIN_TPL = [{
label: 'Undo',
role: 'undo'
}, {
label: 'Redo',
role: 'redo'
}, {
type: 'separator'
}, {
label: 'Cut',
role: 'cut'
}, {
label: 'Copy',
role: 'copy'
}, {
label: 'Paste',
role: 'paste'
}, {
label: 'Paste and Match Style',
click: function() {
BrowserWindow.getFocusedWindow().webContents.pasteAndMatchStyle();
}
}, {
label: 'Select All',
role: 'selectall'
}];
var DEFAULT_SUGGESTIONS_TPL = [
{
label: 'No suggestions',
click: noop
}, {
type: 'separator'
}
];
/**
* if passed a function, invoke it and pass a clone of the default (for safe mutations)
* if passed an array, use as is
* otherwise, just return a clone of the default
* @param val {*}
* @param defaultVal {Array}
* @returns {Array}
*/
function getTemplate(val, defaultVal) {
if(isFunction(val)) {
return val(cloneDeep(defaultVal));
}
else if(isArray(val)) {
return val;
}
else {
return cloneDeep(defaultVal);
}
}
/**
* Builds a context menu suitable for showing in a text editor.
*
* @param {Object=} selection - An object describing the current text selection.
* @property {Boolean=false} isMisspelled - `true` if the selection is
* misspelled, `false` if it is spelled correctly or is not text.
* @property {Array<String>=[]} spellingSuggestions - An array of suggestions
* to show to correct the misspelling. Ignored if `isMisspelled` is `false`.
* @param {Function|Array} mainTemplate - Optional. If it's an array, use as is.
* If it's a function, used to customize the template of always-present menu items.
* Receives the default template as a parameter. Should return a template.
* @param {Function|Array} suggestionsTemplate - Optional. If it's an array, use as is.
* If it's a function, used to customize the template of spelling suggestion items.
* Receives the default suggestions template as a parameter. Should return a template.
* @return {Menu}
*/
var buildEditorContextMenu = function(selection, mainTemplate, suggestionsTemplate) {
selection = defaults({}, selection, {
isMisspelled: false,
spellingSuggestions: []
});
var template = getTemplate(mainTemplate, DEFAULT_MAIN_TPL);
var suggestionsTpl = getTemplate(suggestionsTemplate, DEFAULT_SUGGESTIONS_TPL);
if (selection.isMisspelled) {
var suggestions = selection.spellingSuggestions;
if (isEmpty(suggestions)) {
template.unshift.apply(template, suggestionsTpl);
} else {
template.unshift.apply(template, suggestions.map(function(suggestion) {
return {
label: suggestion,
click: function() {
BrowserWindow.getFocusedWindow().webContents.replaceMisspelling(suggestion);
}
};
}).concat({
type: 'separator'
}));
}
}
return Menu.buildFromTemplate(template);
};
module.exports = buildEditorContextMenu;