-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTextReplace.js
186 lines (175 loc) · 7.38 KB
/
TextReplace.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
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
// Node imports
// External imports
const robot = require('robotjs');
const {copy, paste} = require('copy-paste');
const {BrowserWindow, ipcMain} = require('electron');
const ejs = require('ejs');
// Internal imports
const logger = require('./logger.js');
// Constants
// Application
// let win;
function generateElement(name, option) {
let htmlString = '<div class="row"><div class="input-field col s12">';
if(option.type.toLowerCase() === 'string' || option.type.toLowerCase() === 'number') {
if(option.selection) {
htmlString += `<select id="${name}" name="${name}" ${option.multiple ? 'multiple ' : ''}${option.required ? 'required ' : ''}>`;
option.selection.forEach(e => {
htmlString += `<option value="${e}">${e}</option>`;
});
htmlString += `</select>`;
} else {
htmlString += `<input class="single-data" type="${option.type.toLowerCase() === 'string' ? 'text' : 'number'}" id="${name}" name="${name}" ${option.required ? 'required ' : ''}>`;
}
} else if(option.type.toLowerCase() === 'date') {
htmlString += `<input type="date" class="datepicker" id="${name}" name="${name}" ${option.required ? 'required ' : ''}>`;
} else if(option.type.toLowerCase() === 'boolean') {
htmlString += `<input type="checkbox" class="boolean-data" id="${name}" name="${name}" ${option.required ? 'required ' : ''}>`
}
htmlString += `<label for="${name}">${option.name}</label></div></div>`;
logger.trace({htmlString, name, option}, 'Created element for option');
return htmlString;
}
function generateForm(map) {
let htmlString = `<form name="expansionForm" id="expansionForm" novalidate>`
for(let name in map.options) {
htmlString += generateElement(name, map.options[name]);
}
htmlString += `<div class="row"><div class="col s12"><button class="btn waves-effect waves-light text-expansion-btn" name="action">Expand<i class="material-icons right">send</i></button></div></div></form>`;
logger.trace({htmlString}, 'Created form');
return htmlString;
}
function generateHTML(map) {
return `
<html>
<head>
<title>${map.name}</title>
<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.2/css/materialize.min.css">
</head>
<body>
${generateForm(map)}
<script>window.$ = window.jQuery = require('${__dirname}/node_modules/jquery/dist/jquery.min.js');</script>
<script>window.Hammer = require('${__dirname}/node_modules/hammerjs/hammer.min.js');</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.2/js/materialize.min.js"></script>
<script>require('${__dirname}/static/form.js');</script>
</body>
</html>`
}
function generateDataURI(map) {
return `data:text/html;charset=utf-8,${encodeURI(generateHTML(map))}`;
}
class TextReplace {
constructor (eventEmitter) {
robot.setKeyboardDelay(1);
this.eventEmitter = eventEmitter;
this.currentlyExpanding = false;
this.eventEmitter.on('expansion', this.replaceText.bind(this));
logger.debug('Finished TextReplace event registration');
}
replaceText(alias, map) {
if(this.currentlyExpanding === false) {
this.currentlyExpanding = true;
logger.info({alias, map}, 'Received expansion event. Locked.');
const argumentCallback = function(options) {
let expansion;
try {
expansion = ejs.render(map.expansion, {options: options, vars: map.vars});
} catch (err) {
logger.error({err});
this.eventEmitter.emit('finished');
this.currentlyExpanding = false;
return;
}
logger.debug({expansion}, 'EJS parsed expansion successfully');
setTimeout(() => {
for(let i = 0; i < alias.length; i++) {
robot.keyTap('backspace');
}
paste((error, contents) => {
copy(expansion, () => {
robot.keyTap('v', 'control');
logger.info('Finished expanding snippet');
setTimeout(() => {
if(contents) {
copy(contents);
}
logger.debug('Sending finished event. Unlocked.');
this.eventEmitter.emit('finished');
this.currentlyExpanding = false;
}, 20);
});
});
}, 100);
}.bind(this);
if(map.options) {
logger.debug('Map options specified. Using getArguments');
this.getArguments(map, argumentCallback);
} else {
logger.debug('Map options not found. Using quick route');
argumentCallback(undefined);
}
}
}
getArguments(map, callback) {
let ipcHandler, closeHandler;
ipcHandler = (event, args) => {
logger.debug({args}, 'Received expansion information from BrowserWindow');
this.win.removeListener('closed', closeHandler);
this.win.once('closed', () => {
this.win = null;
});
try {
this.win.close();
} catch (err) {
logger.error({err});
return;
}
for(let x in args) {
const schemaElement = map.options[x];
const type = schemaElement.type;
if(type.toLowerCase() === 'number') {
if(Array.isArray(args[x])) {
args[x].forEach((e, i) => {
args[x][i] = Number.parseFloat(e);
});
} else {
args[x] = Number.parseFloat(args[x]);
}
} else if(type.toLowerCase() === 'date') {
args[x] = new Date(args[x]);
}
}
logger.debug({args}, 'Finished type conversions');
callback(args);
}
closeHandler = () => {
this.win = null;
ipcMain.removeListener('expansion-data', ipcHandler);
this.currentlyExpanding = false;
this.eventEmitter.emit('finished');
}
try {
this.win = new BrowserWindow();
this.win.once('closed', closeHandler);
this.win.loadURL(generateDataURI(map));
ipcMain.once('expansion-data', ipcHandler);
logger.info('Requesting expansion data from user');
} catch (err) {
logger.error({err});
this.currentlyExpanding = false;
this.eventEmitter.emit('finished');
ipcMain.removeListener('expansion-data', ipcHandler);
this.win.removeListener('closed', closeHandler);
try {
this.win.close();
} catch (err) {
logger.error({err});
return;
}
this.win = null;
}
}
}
// Exports
module.exports = TextReplace;