-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (67 loc) · 2.06 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
const { ipcRenderer } = require('electron');
const $ = require('jquery');
const fs = require('fs');
function SimpleWriter () {
this.openButton = $('#open');
this.saveButton = $('#save');
this.wordCount = $('#word2');
this.charCount = $('#char2');
this.infoIcon = $('#info');
this.contentWrapper = $('#contentWrapper');
this.stats = $('#stats');
this.openButton.on("click", this.open.bind(this));
this.saveButton.on("click", this.save.bind(this));
ipcRenderer.on('saveConfirmation', this.saveFile.bind(this));
ipcRenderer.on('fileData', this.loadFile.bind(this));
window.addEventListener('keyup', this.wordCount.bind(this));
this.infoIcon.on({
mouseenter: () => {
this.showCount()
},
mouseleave: () => {
this.showWrite()
}
})
}
SimpleWriter.prototype.wordCount = function () {
let content = $('#content').val();
if (content && content.length > 0) {
this.charCount.text(content.length);
this.wordCount.text(content.split(' ').length);
}
}
SimpleWriter.prototype.save = function (event) {
event.preventDefault();
ipcRenderer.send('saveFile');
}
SimpleWriter.prototype.saveFile = function (event, path) {
let content = $('#content').val();
try {
fs.writeFile(path, content, (err) => {
if (err)
alert("An error ocurred creating the file " + err.message);
});
} catch (e) {
alert("File not saved.")
}
}
SimpleWriter.prototype.open = function (event) {
event.preventDefault();
ipcRenderer.send('openFile');
}
SimpleWriter.prototype.loadFile = function (event, data) {
$('#content').val(data);
}
SimpleWriter.prototype.showCount = function () {
$(this.contentWrapper).fadeOut("slow", () => {
$(this.stats).fadeIn();
});
}
SimpleWriter.prototype.showWrite = function () {
$(this.stats).fadeOut("slow", () => {
$(this.contentWrapper).fadeIn();
});
}
window.onload = function () {
window.simpleWriter = new SimpleWriter();
}