-
Notifications
You must be signed in to change notification settings - Fork 82
/
editor.js
168 lines (157 loc) · 4.49 KB
/
editor.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
var $ = require("./jquery-2.1.4.min");
var fs = require("fs");
var EventEmitter = require('events');
var flumine = require("flumine");
var dialog = require('electron').remote.dialog;
var clipboard = require("clipboard");
require('ace-min-noconflict');
require('ace-min-noconflict/theme-monokai');
var editor;
var EDITOR_FILE_NAME;
var EDITOR_FILE_VALUE;
var emitter = new EventEmitter();
$(window).on("load", function() {
editor = ace.edit("text");
editor.$blockScrolling = Infinity;
if (process.platform === "darwin") {
editor.commands.bindKey("Ctrl-P", "golineup");
}
editor.setTheme("ace/theme/monokai");
setInterval(function() {
if (editor.getValue() !== EDITOR_FILE_VALUE) {
emitter.emit("diff", EDITOR_FILE_NAME);
} else {
emitter.emit("same", EDITOR_FILE_NAME);
}
}, 1000);
var PREV = editor.getValue();
setInterval(function() {
now = editor.getValue();
if (PREV !== now) {
PREV = now;
emitter.emit("change", now);
}
}, 500);
});
var waitEditorReady = flumine(function(d, ok, ng) {
if (editor) {
ok(d);
} else {
var id = setInterval(function() {
if (editor) {
clearInterval(id);
ok(d);
}
}, 200);
}
});
var getFileName = function(forceDialog) {
return flumine(function(d, ok, ng) {
if (!forceDialog && EDITOR_FILE_NAME) {
return ok(EDITOR_FILE_NAME);
} else {
dialog.showSaveDialog({
title: "save file",
filters: [{
name: 'Documents',
extensions: ['txt', 'md', 'text']
}, ],
}, function(fileName) {
if (fileName) {
ok(fileName);
} else {
ok();
}
});
}
});
}
var saveFile = flumine(function(d, ok, ng) {
if (!d) {
return ok("canceled");
}
var code = editor.getValue();
fs.writeFile(d, code, function(err) {
if (err)
return ng(err);
EDITOR_FILE_NAME = d;
EDITOR_FILE_VALUE = code;
return ok(d);
});
});
var copy = waitEditorReady.and(function() {
var text = editor.getCopyText();
clipboard.writeText(text);
});
var app = module.exports = {
open: waitEditorReady.and(function(d, ok, ng) {
var fileName = d[1];
EDITOR_FILE_NAME = fileName;
fs.readFile(fileName, function(err, cont) {
if (err) {
ng(err);
} else {
var code = String(cont);
EDITOR_FILE_VALUE = code;
editor.setValue(code);
editor.navigateLineEnd();
editor.focus();
ok(cont);
}
});
}),
save: waitEditorReady
.and(getFileName(false))
.and(saveFile),
saveAs: waitEditorReady
.and(getFileName(true))
.and(saveFile),
undo: waitEditorReady.and(function() {
editor.undo();
}),
redo: waitEditorReady.and(function() {
editor.redo();
}),
cut: waitEditorReady.and(copy).and(function(d) {
var target = editor.getSelectionRange();
editor.getSession().getDocument().remove(target);
}),
copy: copy,
paste: waitEditorReady.and(function() {
var text = clipboard.readText();
editor.insert(text);
}),
selectAll: waitEditorReady.and(function() {
editor.getSelection().selectAll();
}),
value: waitEditorReady.and(function() {
return editor.getValue();
}),
setError: waitEditorReady.and(function(err) {
var errorInfo = err.message.split(/:/g);
var fileName = errorInfo[0];
var line = errorInfo[1];
var text = errorInfo[3] + errorInfo[4];
editor.getSession().setAnnotations([{
row: line,
type: "error",
text: text,
}]);
}),
clearError: waitEditorReady.through(function() {
editor.getSession().setAnnotations([]);
}),
navigateTo: waitEditorReady.through(function(d) {
editor.navigateTo(d, 0);
editor.focus();
editor.scrollToLine(d, true, true);
}),
insert: waitEditorReady.through(function(d) {
editor.setValue(editor.getValue() + d);
editor.navigateLineEnd();
editor.focus();
}),
on: function(channel, cb) {
emitter.on(channel, cb);
},
};