-
Notifications
You must be signed in to change notification settings - Fork 1
/
notes.js
259 lines (192 loc) · 7.24 KB
/
notes.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
// ---------
// INIT
// ---------
const gui = require("nw.gui"); // TODO : don't know if this will be needed
const os = require('os');
const fs = require('fs');
const path = require('path');
const simpleGit = require('simple-git'); // (const simpleGit = opener.simpleGit -- Does not work);
var util = require('./util_module.js'); // Pragma-git common functions
const pathsep = require('path').sep; // Os-dependent path separator
const settingsDir = os.homedir() + pathsep + '.Pragma-git';
var editor; // Editor object
var filePath;// Path to open file
var fileDir; // Path to folder of open file
var repoName = 'unknown'
var cachedHistory; // Cached history for all versions of this file
var historyNumber = -1; // step back in history. -1 means not in history. First history is at index 0
// Save every 30 seconds
window.setInterval(save, 30 * 1000 );
// Start initiated from notes.html
async function injectIntoNotesJs(document) {
win = gui.Window.get();
filePath = global.arguments[0];
fileDir = path.dirname(filePath);
global.arguments =[]; // Empty this
// Open file
let content = "";
try{
content = fs.readFileSync(filePath,'utf8');
}catch(err){
}
options = {
el: document.querySelector('#editor'),
previewStyle: 'vertical',
height: '100%',
initialValue: content
};
// Disable Google Analytics
usageStatistics: false;
// Add search button to toolbar
button1 = document.createElement('button');
button1.style = "top: -2px; position: absolute; right: 60px";
button1.classList.add('my-icon');
button1.setAttribute("id", 'find-icon');
button1.innerHTML = '<img class="my-img" height="17" width="17" src="images/find_hover.png" >';
button1struct = {
name: 'find',
event: 'clickCustomButton1',
el: button1
};
// Add help button to toolbar
button2 = document.createElement('button');
button2.style = "top: -2px; position: absolute; right: 20px; ";
button2.classList.add('my-icon');
button2.setAttribute("id", 'help-icon');
button2.innerHTML = '<img class="my-img" height="17" width="17" src="images/questionmark_hover.png" >';
button2struct = {
name: 'help',
event: 'clickCustomButton2',
el: button2
};
// Add history button to toolbar
button3 = document.createElement('button');
button3.style = "top: -2px; position: absolute; right: 120px";
button3.classList.add('my-icon');
button3.setAttribute("id", 'history-icon');
button3.innerHTML = '<img class="my-img" height="17" width="17" src="images/history_hover.png" >';
button3struct = {
name: 'help',
event: 'clickCustomButton3',
el: button3
};
// Add buttons to editor options (omit 'scrollSync' function)
options.toolbarItems = [
['heading', 'bold', 'italic', 'strike'],
['hr', 'quote'],
['ul', 'ol', 'task', 'indent', 'outdent'],
['table', 'image', 'link'],
['code', 'codeblock'],
[ button1struct, button2struct, button3struct ],
];
// Initiate Editor
options.initialEditType = global.state.notesWindow.editMode; // Set wysiwyg or markdown
editor = new Editor( options);
// Event listeners
button1.addEventListener('click', () => {
findInNw.showSearchBox();
});
button2.addEventListener('click', () => {
let evt = {};
evt.name='Notes';
parent.opener._callback('help',evt);
});
button3.addEventListener('click', () => {
let evt = {};
evt.name='Notes';
// Note : Action takes place in notes_iframe.html at "Override document-click"
// (this allows open/close menu from icon, and close menu from document-click)
});
// Title
repoName = path.basename( global.state.repos[global.state.repoNumber].localFolder);
document.title = `Notes — ${repoName} `;
if (global.localState.dark){
document.getElementById('editor').classList.add('dark');
document.body.classList.add('dark');
document.body.classList.remove('light');
}
//window.document.body.style.zoom = global.state.zoom;
// Get history
getHistory();
};
function save(){
// Bail out if history menu is open
if ( parent.document.getElementById("historyMenu").style.display == 'block' ){
console.log('NO SAVE -- history menu is open');
return
}
// Bail out if find-in-nw has marked stuff
if (findInNw.total > 0){
console.log('WARNING DID NOT SAVE -- REASON: FIND IS OPEN, AND MARKS ARE INSERTED (Close find to save)');
return
}
let content = "";
try{
content = editor.getMarkdown();
fs.writeFileSync(filePath,content,'utf8');
}catch(err){
console.log('FAILED SAVING FILE = ' + filePath);
console.log(err);
}
}
async function closeWindow(){
// Clear tokens (if find is still open)
findInNw.clearTokens(); // Clear all <mark> inserted in html
// Save file
save();
// Save settings
if ( editor.isWysiwygMode() ){
global.state.notesWindow.editMode = 'wysiwyg';
}else{
global.state.notesWindow.editMode = 'markdown';
}
parent.opener.saveSettings(); // Save settings to file
// Remove from menu
parent.opener.deleteWindowMenu('Notes');
// Commmit .Pragma-git settings dir
await parent.opener.commitSettingsDir('Saved Notes for ' + repoName);
// Mark that closed
global.localState.notesWindow.open = false;
console.log('clicked close window');
}
async function getHistory(){
let file = path.relative(settingsDir, filePath);
let hash; // hash of commit to get
let text; // text in file
// Get hashes for all notes
try{
await simpleGit(settingsDir).log( [ file ], onHistory);
function onHistory(err, result){
console.log(result);
cachedHistory = result.all;
console.log(' ============ Found N = ' + cachedHistory.length);
}
}catch(err){
console.log(err);
}
}
async function getHistoricalNote(){
// Example : oldText = await gitHistoricalNote( 1);
let file = path.relative(settingsDir, filePath);
if ( historyNumber > ( cachedHistory.length - 1 ) ){
historyNumber = cachedHistory.length - 1;
}
if ( historyNumber < 0 ){
historyNumber = -1;
}
let hash = cachedHistory[ historyNumber].hash;
// Read text from file with historyNumber
await simpleGit(settingsDir).show( [ hash + ':' + file ], onCatFile)
function onCatFile(err, res){
text = res;
}
console.log(text);
// Set editor text
editor.setMarkdown(text);
editor.moveCursorToStart();
// Set info text in history menu
let dateTime = cachedHistory[ historyNumber].date;
let date = dateTime.substr(0,10);
let time = dateTime.substr(11,5);
parent.document.getElementById('info').innerHTML = time + '<br>' + date;
}