-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
443 lines (383 loc) · 11.9 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
const { ipcRenderer, remote } = require('electron');
*/
const { ipcRenderer } = require('electron');
const { pathToFileURL } = require('url');
/*
const { app } = remote;
*/
const path = require('path');
const tinymce = require('tinymce/tinymce');
require('tinymce/themes/silver');
require('tinymce/plugins/paste');
require('tinymce/plugins/link');
let initialContent = "";
var EditingMode = true;
// Change current working directory
function change_cwd(newPath) {
var pathURL = pathToFileURL(newPath);
var pathHref = pathURL.href;
if (tinymce.activeEditor) {
var doc = tinymce.activeEditor.getDoc(),
head = doc.head,
base;
if (head.getElementsByTagName("base").length == 0) {
base = document.createElement("base");
head.appendChild(base);
} else {
base = head.getElementsByTagName("base")[0];
}
base.setAttribute("href", pathHref + "/");
tinymce.activeEditor.documentBaseURI.setPath(pathURL.pathname + "/");
}
var headx = document.head,
basex;
if (headx.getElementsByTagName("base").length == 0) {
basex = document.createElement("base");
headx.appendChild(basex);
} else {
basex = headx.getElementsByTagName("base")[0];
}
basex.setAttribute("href", pathHref + "/");
}
function set() {
var path = ipcRenderer.sendSync('call-set');
if (path.length > 0) {
change_cwd(path);
}
tinymce.activeEditor.focus();
}
/*
function tes() {
var path = ipcRenderer.sendSync('call-eso');
if (path.length > 0) {
change_cwd(path);
}
tinymce.activeEditor.focus();
}
*/
// Create new file
function newFile() {
//
if (tinymce.editors[0].isDirty()) {
tinymce.activeEditor.windowManager.open({
title: 'Warning', // The dialog's title - displayed in the dialog header
body: {
type: 'panel', // The root body type - a Panel or TabPanel
items: [ // A list of panel components
{
type: 'htmlpanel', // A HTML panel component
html: 'Unsaved changes. Continue without saving?'
}
]
},
onSubmit: function (api) {
ipcRenderer.send('call-new');
api.close();
},
buttons: [ // A list of footer buttons
{
type: 'cancel',
/*
name: 'closeButton',
*/
text: 'Cancel'
},
{
type: 'submit',
text: 'OK'
}
]
});
// Are we sure we want to exit out of the current file?
} else {
tinymce.activeEditor.windowManager.open({
title: 'Warning', // The dialog's title - displayed in the dialog header
body: {
type: 'panel', // The root body type - a Panel or TabPanel
items: [ // A list of panel components
{
type: 'htmlpanel', // A HTML panel component
html: 'Close the current file and create a new one?'
}
]
},
onSubmit: function (api) {
ipcRenderer.send('call-new');
api.close();
},
buttons: [ // A list of footer buttons
{
type: 'cancel',
/*
name: 'closeButton',
*/
text: 'Cancel'
},
{
type: 'submit',
text: 'OK'
}
]
});
}
/*
return;
*/
}
// Open file
function openFile(tofocus) {
//
if (tinymce.editors[0].isDirty()) {
tinymce.activeEditor.windowManager.open({
title: 'Warning', // The dialog's title - displayed in the dialog header
body: {
type: 'panel', // The root body type - a Panel or TabPanel
items: [ // A list of panel components
{
type: 'htmlpanel', // A HTML panel component
html: 'Unsaved changes. Continue without saving?'
}
]
},
onSubmit: function (api) {
ipcRenderer.send('call-load');
api.close();
},
buttons: [ // A list of footer buttons
{
type: 'cancel',
/*
name: 'closeButton',
*/
text: 'Cancel'
},
{
type: 'submit',
text: 'OK'
}
]
});
} else {
ipcRenderer.send('call-load');
if (tofocus) {
tinymce.activeEditor.focus();
}
}
/*
return;
*/
}
// Save file
function saveFile(tofocus) {
ipcRenderer.send('call-save');
if (tofocus) {
tinymce.activeEditor.focus();
}
}
// Save file as
function saveFileAs(tofocus) {
ipcRenderer.send('call-saveAs');
if (tofocus) {
tinymce.activeEditor.focus();
}
}
// Quit
function quit() {
if (tinymce.editors[0].isDirty()) {
tinymce.activeEditor.windowManager.open({
title: 'Warning', // The dialog's title - displayed in the dialog header
body: {
type: 'panel', // The root body type - a Panel or TabPanel
items: [ // A list of panel components
{
type: 'htmlpanel', // A HTML panel component
html: 'Unsaved changes. Continue without saving?'
}
]
},
onSubmit: function () {
ipcRenderer.send('call-quit');
},
buttons: [ // A list of footer buttons
{
type: 'cancel',
text: 'Cancel'
},
{
type: 'submit',
text: 'OK'
}
]
});
} else {
ipcRenderer.send('call-quit');
}
}
tinymce.PluginManager.add('menusave', function (editor, url) {
editor.ui.registry.addMenuItem('menuwd', {
icon: 'home',
text: 'Set Working Directory',
onAction: function () {
set();
}
});
/*
editor.ui.registry.addMenuItem('menuwd', {
icon: 'home',
text: 'Tes Working Directory',
onAction: function () {
tes();
}
});
*/
editor.ui.registry.addMenuItem('menuwens', {
icon: 'new-document',
text: 'New document',
onAction: function () {
newFile();
}
});
editor.ui.registry.addMenuItem('menuload', {
icon: 'browse',
text: 'Open',
onAction: function () {
openFile(true);
}
});
editor.ui.registry.addMenuItem('menusave', {
icon: 'save',
text: 'Save',
onAction: function () {
saveFile(true);
}
});
editor.ui.registry.addMenuItem('menusaveas', {
icon: 'user',
text: 'Save As',
onAction: function () {
saveFileAs(true);
}
});
editor.ui.registry.addMenuItem('menuquit', {
icon: 'close',
text: 'Quit',
onAction: function () {
quit();
}
});
});
// Upon opening new file
ipcRenderer.on('newly-made-file', function (event, path) {
change_cwd(path);
tinymce.editors[0].setContent(initialContent);
tinymce.activeEditor.undoManager.reset();
tinymce.editors[0].setDirty(false);
});
ipcRenderer.on('new-file', function (event, path, /*filename, */data) {
change_cwd(path);
tinymce.editors[0].setContent(data, { format: 'html' });
tinymce.activeEditor.undoManager.reset();
tinymce.editors[0].setDirty(false);
});
// Upon saving file
ipcRenderer.on('saved-file', function (event, pathname/*, filename*/) {
change_cwd(pathname);
tinymce.editors[0].setDirty(false);
ipcRenderer.send('call-save-', tinymce.editors[0].getContent({ format: 'html' }));
});
ipcRenderer.on('action', (event, arg) => {
switch (arg) {
case 'exiting':
if (EditingMode) {
quit();
}
break;
default:
break;
}
});
tinymce.baseURL = pathToFileURL(__dirname).href + "/" + "node_modules/tinymce";
tinymce.init({
selector:'div.tinymce-full',
language: 'zh_CN',
language_url: 'langs/zh_CN.js',
height: "99%",
indent: true,
/*
toolbar: true,
*/
theme: 'silver',
content_css: 'css/editor-area-styles.css',
content_style: "body { font-family: 'Noto Sans Mono CJK TC', 'Noto Sans Mono CJK TC', sans-serif; font-size: 18px; }",
branding: false, // Disable TinyMCE branding in status bar
statusbar: false,
deprecation_warnings: false,
convert_urls: false,
fullpage_default_encoding: 'UTF-8',
fullpage_default_font_size: '18px',
fullpage_default_font_family: "'Noto Sans Mono CJK TC', 'Noto Sans Mono CJK TC', Serif",
fullpage_default_title: " ",
/* ... */
font_formats: 'Andale Mono=andale mono,monospace;' +
'Arial=arial,helvetica,sans-serif;' +
'Arial Black=arial black,sans-serif;' +
'Book Antiqua=book antiqua,palatino,serif;' +
'Comic Sans MS=comic sans ms,sans-serif;' +
'Courier New=courier new,courier,monospace;' +
'Georgia=georgia,palatino,serif;' +
'Helvetica=helvetica,arial,sans-serif;' +
'Noto Sans Mono CJK TC=noto sans mono cjk tc,sans-serif;' +
'Symbol=symbol;' +
'Tahoma=tahoma,arial,helvetica,sans-serif;' +
'Terminal=terminal,monaco,monospace;' +
'Times New Roman=times new roman,times,serif;' +
'Trebuchet MS=trebuchet ms,geneva,sans-serif;' +
'Verdana=verdana,geneva,sans-serif;' +
'Webdings=webdings;' +
'Wingdings=wingdings,zapf dingbats',
fontsize_formats: '14pt 18pt 24pt 36pt',
menu: {
file: { title: 'File', items: 'menuwd menuwens restoredraft menuload menusave menusaveas | print | menuquit' },
edit: { title: 'Edit', items: 'undo redo | cut copy paste | selectall | searchreplace' },
view: { title: 'View', items: 'code | visualaid visualchars visualblocks | preview fullscreen' },
insert: { title: 'View', items: 'image link media template codesample | charmap emoticons hr | pagebreak nonbreaking anchor | insertdatetime' },
format: { title: 'Format', items: 'bold italic underline strikethrough superscript subscript codeformat | formats blockformats fontformats fontsizes align | forecolor backcolor | removeformat' },
tools: { title: 'Tools', items: 'spellcheckerlanguage | wordcount' },
table: { title: 'Table', items: 'inserttable tableprops deletetable row column cell' },
help: { title: 'Help', items: 'help' }
},
extended_valid_elements: 'link[rel|href],' +
'a[class|name|href|target|title|onclick|rel],' +
'script[type|src],' +
'iframe[src|style|width|height|scrolling|marginwidth|marginheight|frameborder],' +
'img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name]',
menubar: 'file edit view insert format tools table tc help',
toolbar: 'undo redo | bold italic underline strikethrough | fontselect fontsizeselect formatselect | alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist',
plugins: 'print preview fullpage paste importcss searchreplace autolink directionality code visualblocks visualchars fullscreen image link media template codesample table charmap hr pagebreak nonbreaking anchor insertdatetime advlist lists wordcount noneditable help charmap quickbars emoticons menusave',
/* autosave, */
file_picker_types: 'file image',
// and here's our custom image picker
file_picker_callback: function (callback, value, meta) {
var filetypex = meta.filetype;
var pathname = ipcRenderer.sendSync('call-get', filetypex);
if (pathname.length > 0) {
var filename = path.basename(pathname);
// Provide file and text for the link dialog
if (filetypex == 'file') {
callback(pathToFileURL(pathname).href, { text: filename });
}
if (filetypex == 'image') {
callback(pathToFileURL(pathname).href, { alt: filename });
}
}
},
init_instance_callback: function (editor) {
// Give edit area focus at start up
tinyMCE.get('editor').getBody().focus();
initialContent = editor.getContent({ format: 'html' });
},
images_dataimg_filter: function (img) {
return img.hasAttribute('internal-blob'); // Note: The images_dataimg_filter option can also be used to specify a filter predicate function for disabling the logic that converts base64 images into blobs while within the editor. Tiny discourages using images_dataimg_filter for this
}
});