-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.js
322 lines (287 loc) · 8.1 KB
/
browser.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
/* jshint node:true */
'use strict';
const path = require('path');
const $ = require('jquery');
const keycode = require('keycode');
const ipcRenderer = require('electron').ipcRenderer;
const BPromise = require('bluebird');
const spawn = require('child_process').spawn;
const fork = require('child_process').fork;
const spawnToOutput = require('./spawnToOutput');
const modal = require('./miniModal');
const shortcuts = require('./shortcuts');
// Used for heuristic detection of first usable span when measuring line height
const MAX_LINE_HEIGHT = 32;
// Globally-used data for app state.
var appData = {
lineHeight: 16,
commitData: [],
commitLines: {},
symbols: {},
searchMode: null,
currentMatch: null,
matches: [],
currentCommit: null
};
BPromise.config({
// Enable warnings.
warnings: true,
// Enable long stack traces.
longStackTraces: true,
// Enable cancellation.
cancellation: false
});
// var settings = { };
function commitMessage(message, options) {
if (typeof(options) === 'undefined') {
options = {
gitk: true
};
}
var html =
`<pre>
${message}
</pre>`;
if (options.gitk) {
html = html + '<button class="gitk">View commit in GitK</button>';
}
modal(html, {});
}
function appMessage(text, goodBadUgly) {
var message = $('#app-message');
if (!text) {
message.hide();
return;
}
goodBadUgly = goodBadUgly || 'good';
message.removeClass('bad good').addClass(goodBadUgly);
message.text(text).removeClass('hidden').show();
setTimeout(function() {
message.animate({opacity: 0}, { complete: function() { message.hide().css('opacity', 1); }});
}, 1000);
}
function buildSymbols($sourceElt) {
$sourceElt.find('.hljs-title').each((idx, elt) => {
var name = $(elt).text();
appData.symbols[name] = appData.symbols[name] || [];
appData.symbols[name].push($(elt));
});
}
function findMatches(input) {
if (!input || input === '') {
return [];
}
var rx = input.split('').join('.*');
rx = new RegExp(rx, (input.match(/[A-Z]/) ? '' : 'i'));
return Object.keys(appData.symbols).filter(k => k.match(rx));
}
function calcLineHeight($sourceElt) {
$sourceElt.find('span').each((idx, elt) => {
var $this = $(elt);
if ($this.height() < MAX_LINE_HEIGHT) {
appData.lineHeight = $this.height();
return false;
}
return true;
});
}
function highlightCommit(commit) {
$('.lines li').removeClass('current');
appData.commitLines[commit.hash].forEach(l => {
$('.lines li').get(l).classList.add('current');
});
}
function openFile(fileName) {
appMessage(`Loading ${fileName}`, 'good');
var gitBlame = require('./gitblame');
gitBlame(fileName)
.then(function(data) {
$('title').text(path.resolve(fileName));
appData.commitData = data.commitData;
var source = data.sourceLines.join("\n");
var $sourceElt = $('.source pre code');
$sourceElt.text(source);
data.commitData.forEach((commit, idx) => {
appData.commitLines[commit.hash] = appData.commitLines[commit.hash] || [];
appData.commitLines[commit.hash].push(idx);
});
var html = '';
for (let i = 1; i <= data.sourceLines.length; i++) {
let cls = '';
if (data.commitData[i-1].hash === "0000000000000000000000000000000000000000") {
cls = 'class="uncommitted"';
}
html += `<li ${cls}>${i}</li>`;
}
$('.lines').html(html);
var worker = fork(`${__dirname}/hlworker.js`);
worker.on('message', (msg) => {
$sourceElt.html(msg.result);
calcLineHeight($sourceElt);
buildSymbols($sourceElt);
appMessage();
});
worker.send({
source: source
});
});
}
function gitLog(commit) {
return spawnToOutput('git', ['log', '-n1', commit]);
}
function showSearch(type) {
appData.searchMode = type;
$('.search')
.removeClass('hidden')
.find('input')
.val('')
.attr('placeholder', type)
.focus();
}
function hideSearch() {
$('.search')
.addClass('hidden')
.find('.matches')
.addClass('hidden');
}
function showMatches(matches, formatMatch) {
appData.currentMatch = 0;
appData.matches = matches;
var $search = $('.search');
var $matches = $search.find('.matches');
$matches.empty();
var html = '';
matches.forEach((m) => {
var txt = '';
if (typeof(formatMatch) === 'function') {
txt = `<li>${formatMatch(m)}</li>`;
} else {
txt = `<li>${m}</li>`;
}
html += txt;
});
$matches.html(html);
$matches.find('li:first').addClass('current');
$matches.removeClass('hidden');
}
function gotoLine(n) {
$(window).scrollTop(
n * appData.lineHeight - $('.source pre code').offset().top
- $(window).innerHeight() / 2
);
}
$(function() {
// if (localStorage.settings) {
// try {
// var loadedSettings = JSON.parse(localStorage.settings);
// settings = loadedSettings;
// } catch(ex) {
// console.error('Failed to parse stored settings', localStorage.settings);
// }
// }
ipcRenderer.on('flame-command-line', function(event, args) {
args = args.slice(2); // Remove node, electron
openFile(args[0]);
});
$(document.body).on('click', 'a.external.link',function(e) {
e.preventDefault();
var href = this.href;
const shell = require('electron').shell;
shell.openExternal(href);
});
function getSourceLine(clientY) {
var line = clientY - $('.source').offset().top + $(window).scrollTop();
line /= appData.lineHeight;
line = Math.floor(line);
return line;
}
$('.source').on('mousemove', function(e) {
$('.lines li').css('color', '');
var line = getSourceLine(e.clientY);
var note = $('.lines li').get(line);
if (note) {
$(note).css('color', 'yellow');
}
});
$(document).on('click', '.gitk', (e) => {
if (appData.currentCommit) {
modal.close();
spawn('gitk', ['--select-commit=' + appData.currentCommit.hash]);
}
});
$('.source').on('click', function(e) {
var line = getSourceLine(e.clientY);
var note = appData.commitData[line];
if (note) {
appData.currentCommit = note;
highlightCommit(note);
if (note.hash.match(/^0+$/)) {
commitMessage('Not yet committed', {gitk: false});
return;
}
gitLog(note.hash)
.then(function(message) {
console.log(note);
commitMessage(message);
});
}
});
$('.search').on('keyup', 'input', (e) => {
var $this = $(e.target);
var input = $this.val();
switch (keycode(e)) {
case 'enter':
if (appData.searchMode === '@' && appData.matches.length) {
var match = appData.matches[appData.currentMatch];
var elt = appData.symbols[match][0];
$(window).scrollTop(elt.offset().top - $(window).innerHeight() / 2);
hideSearch();
} else {
hideSearch();
}
break;
case 'esc':
hideSearch();
break;
default:
if (appData.searchMode === '@' && keycode(e) && keycode(e).match(/^([a-z$_.-])$/)) {
showMatches(findMatches(input));
$this.focus();
} else if(appData.searchMode === ':') {
gotoLine(input);
} else {
return true;
}
}
e.preventDefault();
return false;
});
shortcuts.listen(document);
shortcuts.register('ctrl+g', (e) => {
showSearch(':');
});
shortcuts.register('ctrl+r', (e) => {
showSearch('@');
});
shortcuts.register('F12', (e) => {
ipcRenderer.send('flame-show-dev-tools');
});
var matchesNav = function(e) {
if ($('.search').hasClass('hidden')) {
return true;
}
var delta = (keycode(e) === 'down') ? 1 : -1;
var $matches = $('.matches');
$matches.find('li').removeClass('current');
appData.currentMatch += delta;
if (appData.currentMatch < 0) {
appData.currentMatch = appData.matches.length - 1;
}
if (appData.currentMatch >= appData.matches.length) {
appData.currentMatch = 0;
}
$($matches.find('li').get(appData.currentMatch)).addClass('current');
};
shortcuts.register('up', matchesNav);
shortcuts.register('down', matchesNav);
});