This repository has been archived by the owner on Aug 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 161
/
init.js
152 lines (150 loc) · 5.18 KB
/
init.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
var mime = require('mime'),
remote = require('remote'),
fs = require('fs'),
Menu = remote.require('menu'),
BrowserWindow = remote.require('browser-window'),
AbrDocument = loadComponent('AbrDocument'),
Abricotine = loadComponent('Abricotine'),
config = loadComponent('config'),
argv = remote.process.argv;
module.exports = function () {
function getFileToOpen (argv) {
function isTextFile (path) {
try {
var fileStat = fs.statSync(path);
if (fileStat.isFile()) {
var mimetype = mime.lookup(path);
return (mimetype && mimetype.substr(0,4) === 'text');
}
return false;
}
catch(err) {
console.log(err);
return false;
}
}
for (var i=0; i<argv.length; i++) {
if (isTextFile(argv[i])) {
return argv[i];
}
}
return false;
}
function initUi (config) {
// TODO: mettre ces entrées dans un parent "startup" du fichier config histoire de pas avoir a les énumérer
if (config.autoHideMenuBar) {
Abricotine.execCommand("autoHideMenuBar");
}
if (config.showBlocks) {
Abricotine.execCommand("showBlocks");
}
if (config.showHiddenCharacters) {
Abricotine.execCommand("showHiddenCharacters");
}
if (config.showTocPane) {
Abricotine.execCommand("showTocPane");
}
}
function initMenu (config) {
function preprocessTemplate (element) {
function replaceAttributes (item) {
if (item.command) {
item.click = (function (command, parameters) {
return function () { Abricotine.execCommand(command, parameters); };
})(item.command, item.parameters);
delete item.command;
delete item.parameters;
}
if (item.checked && typeof item.checked === "string") {
item.checked = config[item.checked] || false;
}
if (item.submenu) {
preprocessTemplate(item.submenu);
}
return item;
}
if (element.constructor !== Array) {
return;
}
for (var i=0; i<element.length; i++) {
replaceAttributes(element[i]);
}
return element;
}
function createMenuFromJSON (jsonPath) {
var menuTemplate = loadComponent(jsonPath, 'json'),
preprocessedMenuTemplate = preprocessTemplate(menuTemplate),
menu = Menu.buildFromTemplate(preprocessedMenuTemplate);
return menu;
}
/* Menu */
var menu = createMenuFromJSON('menu');
Menu.setApplicationMenu(menu);
/* Context Menu */
var contextMenu = createMenuFromJSON('contextMenu');
document.addEventListener("contextmenu", function () {
contextMenu.popup(BrowserWindow.getFocusedWindow());
});
/* Reference in Abricotine */
Abricotine.menu = menu;
Abricotine.contextMenu = contextMenu;
}
function initDragAndDrop () {
document.body.ondragover = function () {
return false;
};
document.body.ondragleave = document.body.ondragend = function () {
return false;
};
document.body.ondrop = function (e) {
e.preventDefault();
var file = e.dataTransfer.files[0];
if (file && file.path) {
Abricotine.getCurrentDocument().cmdOpen(file.path);
}
return false;
};
}
function initEvents () {
// ToC links
$("#pane").on("click", "li", function () {
var line = parseInt($(this).attr('data-abricotine-gotoline')),
cm = Abricotine.getCurrentDocument().editor.cm,
doc = cm.doc,
height = cm.getScrollInfo().clientHeight,
top = cm.heightAtLine(line, "local"),
marginTop = 10;
doc.setCursor({
line: line,
ch: null
});
// Scoll to show target at the top of the window
cm.scrollIntoView({
top: top - marginTop,
bottom: top + height - marginTop,
left: 0,
right: 0
});
cm.focus();
});
// Close event
window.onbeforeunload = function(e) {
config.saveUserConfig(Abricotine.config);
return Abricotine.getCurrentDocument().cmdClose();
};
}
if (!window) {
console.error('window is not defined');
}
window.Abricotine = Abricotine;
var fileToOpen = getFileToOpen(argv),
fullConfig = config.getFullConfig(),
abrDoc;
initUi(fullConfig);
initMenu(fullConfig);
initDragAndDrop();
initEvents();
Abricotine.config = fullConfig;
abrDoc = new AbrDocument(fileToOpen);
Abricotine.documents.push(abrDoc);
};