forked from Hirse/brackets-outline-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
191 lines (166 loc) · 6.57 KB
/
main.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
define(function (require, exports, module) {
"use strict";
var CommmandManager = brackets.getModule("command/CommandManager");
var Menus = brackets.getModule("command/Menus");
var DocumentManager = brackets.getModule("document/DocumentManager");
var EditorManager = brackets.getModule("editor/EditorManager");
var Resizer = brackets.getModule("utils/Resizer");
var AppInit = brackets.getModule("utils/AppInit");
var ExtensionUtils = brackets.getModule("utils/ExtensionUtils");
var _ = brackets.getModule("thirdparty/lodash");
var Strings = require("strings");
var prefs = require("src/Preferences");
var ListTemplate = require("text!templates/outline.html");
ExtensionUtils.loadStyleSheet(module, "styles/styles.css");
var prefix = "hirse.outline";
var languages = {
JavaScript: require("src/languages/JavaScript"),
CoffeeScript: require("src/languages/CoffeeScript"),
CSS: require("src/languages/CSS"),
SCSS: require("src/languages/CSS"),
LESS: require("src/languages/CSS"),
Stylus: require("src/languages/Stylus"),
PHP: require("src/languages/PHP"),
Ruby: require("src/languages/Ruby"),
Python: require("src/languages/Python"),
Markdown: require("src/languages/Markdown"),
"Markdown (GitHub)": require("src/languages/Markdown"),
XML: require("src/languages/XML"),
HTML: require("src/languages/XML"),
"Embedded Ruby": require("src/languages/XML"),
SVG: require("src/languages/XML"),
Jade: require("src/languages/Jade")
};
function getOutline() {
var $outline = Mustache.render(ListTemplate, {
Strings: Strings
});
$outline = $($outline);
$outline.on("click", "#outline-close", toggleEnabled);
$outline.on("click", "#outline-move", function () {
hideOutline(function () {
prefs.togglePref("sidebar");
updateOutline();
});
});
return $outline;
}
function goToLine(event) {
var currentEditor = EditorManager.getActiveEditor();
currentEditor.setCursorPos(event.data.line, event.data.ch, true);
currentEditor.focus();
}
function updateOutline() {
var doc = DocumentManager.getCurrentDocument();
if (!doc) {
hideOutline();
return;
}
var lang = languages[doc.getLanguage().getName()];
if (!lang) {
hideOutline();
return;
}
showOutline();
var list = lang.getOutlineList(doc.getText(), prefs.get("args"), prefs.get("unnamed"));
if (prefs.get("sort") && lang.compare) {
list.sort(lang.compare);
}
list.forEach(function (entry) {
var $entry = $(document.createElement("li"));
$entry.addClass("outline-entry");
$entry.addClass(entry.classes);
$entry.append(entry.$html);
$entry.click({
line: entry.line,
ch: entry.ch
}, goToLine);
$("#outline-list ul").append($entry);
});
}
function onResize() {
var toolbarPx = $("#main-toolbar:visible").width() || 0;
$(".content").css("right", ($("#outline").width() || 0) + toolbarPx + "px");
}
function showOutline() {
if ($("#outline").length) {
$("#outline-list ul").empty();
return;
}
var $outline = getOutline();
if (prefs.get("sidebar")) {
$("#sidebar").append($outline);
$("#outline").addClass("outline-sidebar");
Resizer.makeResizable($outline, "vert", "top", 75);
} else {
var toolbarPx = $("#main-toolbar:visible").width() || 0;
$(".main-view").append($outline);
$("#outline").css("right", toolbarPx + "px");
$("#outline").addClass("outline-main quiet-scrollbars");
Resizer.makeResizable($outline, "horz", "left", 150);
$outline.on("panelResizeUpdate", onResize);
onResize();
}
Menus.ContextMenu.assignContextMenuToSelector("#outline-settings", settingsMenu);
}
function hideOutline(onHideCompleted) {
if (prefs.get("sidebar")) {
$("#outline").slideUp(function () {
$(this).remove();
onResize();
if (onHideCompleted) {
onHideCompleted();
}
});
} else {
$("#outline").remove();
onResize();
if (onHideCompleted) {
onHideCompleted();
}
}
}
function enableOutline() {
$("#outline-toolbar-icon").addClass("enabled");
EditorManager.on("activeEditorChange", updateOutline);
DocumentManager.on("documentSaved", updateOutline);
updateOutline();
}
function disableOutline() {
hideOutline();
$("#outline-toolbar-icon").removeClass("enabled");
EditorManager.off("activeEditorChange", updateOutline);
DocumentManager.off("documentSaved", updateOutline);
}
function toggleEnabled() {
if (prefs.togglePref("enabled")) {
enableOutline();
} else {
disableOutline();
}
}
/* Create Settings Context Menu */
var settingsMenu = Menus.registerContextMenu("hirse-outline-context-menu");
_.each(prefs.getSettings(), function (status, key) {
var commandName = prefix + "." + key;
var commandString = Strings["COMMAND_" + key.toUpperCase()];
var command = CommmandManager.register(commandString, commandName, function () {
var checked = prefs.togglePref(commandName.split(".")[2]);
command.setChecked(checked);
updateOutline();
});
command.setChecked(status);
settingsMenu.addMenuItem(command);
});
/* Create Toolbar Icon */
$(document.createElement("a"))
.attr("id", "outline-toolbar-icon")
.attr("href", "#")
.attr("title", Strings.TOOLBAR_ICON_TOOLTIP)
.on("click", toggleEnabled)
.appendTo($("#main-toolbar .buttons"));
AppInit.appReady(onResize);
if (prefs.get("enabled")) {
enableOutline();
}
});