-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
242 lines (205 loc) · 6.04 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
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
var _ = require('underscore');
var path = require('path');
module.exports = function(params) {
var window = params.window;
var cljs = window.cljs;
var document = window.document;
var lt = window.lt;
/*************************************************************************\
* Misc. Helpers
\*************************************************************************/
/*\
|*| Attempt the given operation, ignoring errors with the given code.
\*/
function ignore(callback, codes) {
if(!_.isArray(codes)) {
codes = [codes];
}
try {
return callback();
} catch (err) {
if(_.contains(codes, err.code)) {
return;
}
throw err;
}
}
/*************************************************************************\
* CLJS Helpers
\*************************************************************************/
/*\
|*| Casts a string into a cljs keyword.
\*/
function toKeyword(name) {
return new cljs.core.Keyword(null, name, name);
}
/*\
|*| Casts an object into a cljs PersistentHashMap.
|*| noConvert: [Boolean] Do not convert string keys into Keywords.
\*/
function toHashMap(obj, noConvert) {
var hash = cljs.core.PersistentHashMap.EMPTY;
_.each(obj, function(value, key) {
if(!noConvert) {
key = toKeyword(key);
}
hash = cljs.core.assoc(hash, key, value);
});
return hash;
}
/*************************************************************************\
* LT Helpers
\*************************************************************************/
/*\
|*| Requires plugin-local files and modules.
\*/
function requireLocal(name, root) {
root = root || params.root;
return ignore(_.partial(require, path.join(root, name)), 'MODULE_NOT_FOUND') || // File include.
ignore(_.partial(require, path.join(root, 'node_modules', name)), 'MODULE_NOT_FOUND') || // Module include
require(name); // Global include
}
/*\
|*| Registers a function as a CodeMirror action.
\*/
function addAction(commandName, command) {
var CodeMirror = window.CodeMirror;
if (!CodeMirror.commands[commandName]) {
CodeMirror.commands[commandName] = command;
}
}
/*\
|*| Invokes a native LT command (anything that is registered with the commandbar).
\*/
function execCommand(name) {
var args = [].slice.call(arguments, 1);
var kw = toKeyword(name);
args.unshift(kw);
return lt.objs.command.exec_BANG_.apply(lt.objs.command.command, args);
}
/*\
|*| Registers a new command with LT.
|*| command {
|*| command: String Name (cast to Keyword)
|*| desc: String Description
|*| exec: function Function to execute.
|*| hidden: [Boolean] Visible in commandbar
|*| }
\*/
function addCommand(command) {
if('command' in command) {
command.command = toKeyword(command.command);
}
if('hidden' in command) {
command.hidden = false;
}
var cmd = toHashMap(command);
lt.objs.command.command.call(null, cmd);
}
/*\
|*| Tells Light Table to listen to and dispatch key events for the given context.
\*/
function enterContext(context) {
var keyword = toKeyword(context);
lt.objs.context.in_BANG_.call(null, keyword, null);
}
/*\
|*| Tells Light Table to cease listening to and dispatching key events for the given context.
\*/
function exitContext(context) {
var keyword = toKeyword(context);
lt.objs.context.out_BANG_.call(null, keyword, null);
}
/*************************************************************************\
* Tab Helpers
\*************************************************************************/
/*\
|*| Gets a list of all open tabs, represented in a 2D array of [group][file]
\*/
function getTabs() {
var tabsets = document.querySelectorAll('#multi .tabset');
return _.map(tabsets, function(tabset) {
return _.map(tabset.querySelectorAll('.list li'), function(tab) {
return tab.getAttribute('title');
});
});
}
/*\
|*| Gets the currently active tab as an html element.
\*/
function getActiveTab() {
return lt.objs.tabs.active_tab();
}
/*\
|*| Gets the filepath of the currently active tab, super hacky.
\*/
function getActiveFile() {
var active = getActiveTab();
if(!active) {
return false;
}
return lt.objs.tabs.__GT_path(active);
}
/*\
|*| Gets the directory containing the current buffer if one exists, or home.
|*| cwd in LT points to LT's directory
\*/
function getActiveDirectory() {
var dir = path.dirname(getActiveFile());
if(!dir || dir === '.') {
dir = '~';
}
return dir + path.sep;
}
/*************************************************************************\
* UI Helpers
\*************************************************************************/
/*\
|*| Opens a synchronous dialog with the given message, returning the result
|*| of user interaction, if any.
\*/
var prompt = window.prompt;
var confirm = window.confirm;
/*\
|*| Toggles an element's visiblity. Returns truthy if
|*| opened and falsy if closed.
|*| @NOTE: This is hacky and behavior is subject to future improvement.
\*/
function showContainer(container) {
var elem = document.querySelector(container);
if(!elem) {
return false;
}
var height = parseInt(elem.style.height, 10);
if(height === 0) {
elem.style.height = 'auto';
} else {
elem.style.height = 0;
}
return !height;
}
module.exports = {
params: params,
ignore: ignore,
// cljs
toKeyword: toKeyword,
toHashMap: toHashMap,
// lt
require: requireLocal,
addAction: addAction,
addCommand: addCommand,
execCommand: execCommand,
enterContext: enterContext,
exitContext: exitContext,
// tab
getActiveTab: getActiveTab,
getTabs: getTabs,
getActiveFile: getActiveFile,
getActiveDirectory: getActiveDirectory,
// UI
prompt: prompt,
confirm: confirm,
showContainer: showContainer
};
return module.exports;
};