-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmain.js
206 lines (184 loc) · 7.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
define(function (require, exports, module) {
"use strict";
/* beautify preserve:start *//* eslint-disable no-multi-spaces */
var DocumentManager = brackets.getModule("document/DocumentManager");
var EditorManager = brackets.getModule("editor/EditorManager");
var PreferencesManager = brackets.getModule("preferences/PreferencesManager");
var ExtensionUtils = brackets.getModule("utils/ExtensionUtils");
var CommandManager = brackets.getModule("command/CommandManager");
var Menus = brackets.getModule("command/Menus");
var prefs = require("src/Preferences");
var OutlineManager = require("src/OutlineManager");
var ToolbarButton = require("src/ToolbarButton");
var Strings = require("strings");
var Autohide = require("src/Autohide");
/* eslint-enable no-multi-spaces *//* beautify preserve:end */
ExtensionUtils.loadStyleSheet(module, "styles/styles.css");
/* beautify preserve:start *//* eslint-disable key-spacing */
var languageMapping = {
JavaScript: "JavaScript",
JSX: "JavaScript",
Haxe: "Haxe",
CoffeeScript: "CoffeeScript",
CSS: "CSS",
SCSS: "CSS",
LESS: "CSS",
Stylus: "Stylus",
PHP: "PHP",
Ruby: "Ruby",
Python: "Python",
Markdown: "Markdown",
"Markdown (GitHub)": "Markdown",
XML: "XML",
HTML: "XML",
"Embedded Ruby": "XML",
SVG: "XML",
"Vue component file": "XML",
Jade: "Jade"
};
/* eslint-enable key-spacing *//* beautify preserve:end */
/**
* Check if the outline is available for the given document.
* @param {Document} document Document taken from the editor.
* @returns {boolean} True, if an outline is available.
*/
function isOutlineAvailable(document) {
return document && document.getLanguage().getName() in languageMapping;
}
/**
* Handler for editor changed.
* Update and show or hide the outline based on availability.
* The update is trigger with a short delay to prevent missclicking on the File Tree.
*/
function handleEditorChange() {
window.setTimeout(function () {
var document = DocumentManager.getCurrentDocument();
if (isOutlineAvailable(document)) {
OutlineManager.setOutlineProvider(languageMapping[document.getLanguage().getName()]);
OutlineManager.updateOutline(document.getText());
if (prefs.get("autohide")) {
Autohide.reset(OutlineManager.showOutline());
} else {
OutlineManager.showOutline();
}
} else {
if (prefs.get("autohide")) {
Autohide.disable();
}
OutlineManager.hideOutline();
}
}, 100);
}
/**
* Handler for document saved.
* Update the outline with the new text.
*/
function handleDocumentSave() {
OutlineManager.updateOutline(DocumentManager.getCurrentDocument().getText());
if (prefs.get("autohide")) {
Autohide.reset();
}
}
/**
* Handler for enabled changed.
* If the outline just got enbaled, set the outline provider, update the outline, and show it.
* If it is not available or got disabled, hide the outline.
*/
function handleEnabledChange() {
ToolbarButton.setEnabled(prefs.get("enabled"));
if (prefs.get("enabled")) {
EditorManager.on("activeEditorChange.outline-list", handleEditorChange);
DocumentManager.on("documentSaved.outline-list", handleDocumentSave);
var document = DocumentManager.getCurrentDocument();
if (isOutlineAvailable(document)) {
OutlineManager.setOutlineProvider(languageMapping[document.getLanguage().getName()]);
OutlineManager.updateOutline(document.getText());
OutlineManager.showOutline();
if (prefs.get("autohide")) {
Autohide.enable();
}
}
} else {
EditorManager.off("activeEditorChange.outline-list", handleEditorChange);
DocumentManager.off("documentSaved.outline-list", handleDocumentSave);
if (prefs.get("autohide")) {
Autohide.disable();
}
OutlineManager.hideOutline();
}
}
/**
* Change the position of the outline.
*/
function handleSidebarChange() {
if (prefs.get("sidebar")) {
OutlineManager.setPosition(OutlineManager.POSITION_SIDEBAR);
if (prefs.get("autohide")) {
Autohide.disable();
}
} else {
OutlineManager.setPosition(OutlineManager.POSITION_TOOLBAR);
if (prefs.get("autohide")) {
Autohide.enable();
}
}
}
/**
* Handler for ToolbarButton.
* Enable or disable the outline.
*/
function handleButtonClick() {
prefs.togglePref("enabled");
}
/**
* Go to the line and character of the selected entry.
* @param {object} event Event with line and ch as properties.
*/
function handleSelect(event) {
var currentEditor = EditorManager.getActiveEditor();
currentEditor.setCursorPos(event.line, event.ch, true);
currentEditor.focus();
}
function handleAutohideChange() {
var document = DocumentManager.getCurrentDocument();
if (prefs.get("autohide") && isOutlineAvailable(document)) {
Autohide.enable();
} else if (!prefs.get("autohide") && !isOutlineAvailable(document)) {
Autohide.disable();
OutlineManager.hideOutline();
} else if (!prefs.get("autohide") && isOutlineAvailable(document)) {
Autohide.disable();
}
}
/**
* Get the current value of the autohide delay from the preferences.
*/
function handleAutohideDelayChange() {
Autohide.setDelay(prefs.get("autohideDelay"));
}
function toggleAutohide() {
var state = prefs.togglePref("autohide");
CommandManager.get("outline.autohide").setChecked(state);
}
prefs.onChange("autohide", handleAutohideChange);
prefs.onChange("autohideDelay", handleAutohideDelayChange);
prefs.onChange("enabled", handleEnabledChange);
prefs.onChange("sidebar", handleSidebarChange);
// Update the position if the no-distractions/pure-code mode is turned on
PreferencesManager.on("change", "noDistractions", function () {
if (!prefs.get("sidebar")) {
OutlineManager.setPosition(OutlineManager.POSITION_TOOLBAR);
if (prefs.get("autohide")) {
Autohide.reset();
}
}
});
ToolbarButton.onClick(handleButtonClick);
OutlineManager.onSelect(handleSelect);
CommandManager.register(Strings.COMMAND_AUTOHIDE, "outline.autohide", toggleAutohide);
CommandManager.register(Strings.COMMAND_AUTOHIDE_DELAY, "outline.autohide.delay", Autohide.showDelayDialog);
var menu = Menus.getMenu(Menus.AppMenuBar.VIEW_MENU);
menu.addMenuItem("outline.autohide");
menu.addMenuItem("outline.autohide.delay");
CommandManager.get("outline.autohide").setChecked(prefs.get("autohide"));
});