-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.js
292 lines (237 loc) · 11 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
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
/*
* The MIT License (MIT)
* Copyright (c) 2013-2014 Lance Campbell. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint vars: true, plusplus: true, devel: true, regexp: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, brackets, $, window */
define(function (require, exports, module) {
"use strict";
// --- Brackets Modules ---
var PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
CommandManager = brackets.getModule("command/CommandManager"),
EditorManager = brackets.getModule("editor/EditorManager"),
AppInit = brackets.getModule("utils/AppInit"),
Menus = brackets.getModule("command/Menus"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
ViewCommandHandlers = brackets.getModule("view/ViewCommandHandlers"),
MainViewManager = brackets.getModule("view/MainViewManager");
// --- CodeMirror Addons ---
brackets.getModule(["thirdparty/CodeMirror2/addon/display/rulers"]);
// --- Extension modules ---
var Ruler = require("Ruler").Ruler;
// --- Constants ---
var EXTENSION_NAME = "brackets-ruler";
var RULER_COMMAND_NAME = "Column Ruler",
RULER_COMMAND_ID = "lkcampbell.toggleColumnRuler",
RULER_CONTEXT_MENU = "lkcampbell-ruler-context-menu";
var GUIDE_COMMAND_NAME = "Column Guide",
GUIDE_COMMAND_ID = "lkcampbell.toggleColumnGuide",
GUIDE_COLOR = "rgba(128, 128, 128, 0.5)",
GUIDE_LINE_STYLE = "solid",
GUIDE_CLASS = "brackets-ruler-column-guide";
var MIN_COLUMNS = 80;
// --- Extension Preferences ---
var prefs = PreferencesManager.getExtensionPrefs(EXTENSION_NAME);
prefs.definePreference("rulerEnabled", "boolean", false, {
description: "If the value of this preference is true, the column ruler will be visible. If the value is false, the column ruler will be hidden."
});
prefs.definePreference("guideEnabled", "boolean", false, {
description: "If the value of this preference is true, the column guide will be visible. If the value is false, the column guide will be hidden."
});
prefs.definePreference("guidePosition", "number", MIN_COLUMNS, {
description: "The position of the column guide as defined by a column number that is zero or greater. Values that exceed the length of the column ruler are rounded to the highest number on the ruler."
});
prefs.definePreference("guideColor", "string", GUIDE_COLOR, {
description: "The color of the column guide. Can be any valid CSS Color value."
});
prefs.definePreference("guideLineStyle", "string", GUIDE_LINE_STYLE, {
description: 'The line style of the column guide. Values can be one of the following: "solid", "dotted", or "dashed".',
values: ["solid", "dotted", "dashed"]
});
// --- Private Variables ---
var editor = null,
cm = null,
ruler = new Ruler(),
$rulerPanel = null,
isDragging = false;
// --- Private Functions
function applyPrefs() {
var rulerEnabled = prefs.get("rulerEnabled"),
guideEnabled = prefs.get("guideEnabled"),
guidePosition = prefs.get("guidePosition"),
guideColor = prefs.get("guideColor"),
guideLineStyle = prefs.get("guideLineStyle"),
guideClass = "",
guideOptions = [];
// Update Ruler
ruler.setEnabled(rulerEnabled);
// Update Guide
if (cm) {
if (guideEnabled) {
guideOptions.push({
color: guideColor,
lineStyle: guideLineStyle,
column: guidePosition
});
cm.setOption("rulers", guideOptions);
} else {
cm.setOption("rulers", false);
}
// For now, guide will only be visible in single view mode
if (MainViewManager.getPaneCount() !== 1) {
cm.setOption("rulers", false);
}
}
ruler.setGuidePosition(guidePosition);
// Update Menu Settings
CommandManager.get(RULER_COMMAND_ID).setChecked(rulerEnabled);
CommandManager.get(GUIDE_COMMAND_ID).setChecked(guideEnabled);
}
// --- Helper functions ---
function getColumnFromRulerClick(event) {
var $allTickMarks = $(".br-tick-marks").children(),
$targetTickMark = null,
clickX = event.pageX,
targetClass = "",
tickRegExp = /^br-tick-(\d+)$/,
matchResult = [];
$targetTickMark = $allTickMarks.filter(function () {
return $(this).offset().left <= clickX;
}).filter(function () {
return ($(this).offset().left + $(this).outerWidth()) > clickX;
});
// Take care of the edge cases
if ($targetTickMark.length === 0) {
if (clickX < $allTickMarks.first().offset().left) {
$targetTickMark = $allTickMarks.eq(1);
} else {
$targetTickMark = $allTickMarks.eq($allTickMarks.length - 2);
}
}
targetClass = $targetTickMark.attr("class").split(" ")[1];
if (targetClass === "br-tick-mark-left-filler") {
targetClass = $targetTickMark.next().attr("class").split(" ")[1];
} else if (targetClass === "br-tick-mark-right-filler") {
targetClass = $targetTickMark.prev().attr("class").split(" ")[1];
}
matchResult = targetClass.match(tickRegExp);
return parseInt(matchResult[1], 10);
}
// --- Event Handlers ---
function handlePrefsChange() {
applyPrefs();
prefs.save();
}
function handleToggleRuler() {
prefs.set("rulerEnabled", !prefs.get("rulerEnabled"));
}
function handleToggleGuide() {
prefs.set("guideEnabled", !prefs.get("guideEnabled"));
}
function handleDocumentChange() {
editor = EditorManager.getCurrentFullEditor();
cm = editor ? editor._codeMirror : null;
ruler.setEditor(editor);
applyPrefs();
}
function handleRulerDragStop() {
$rulerPanel.off("mousemove");
$rulerPanel.off("mouseup");
$rulerPanel.off("mouseenter");
// No dragging === mouse click: toggle guide
if (!isDragging) {
handleToggleGuide();
}
isDragging = false;
}
function handleRulerDrag(event) {
// When left mouse button is no longer pressed, stop the drag
if (event.which !== 1) {
handleRulerDragStop();
}
isDragging = true;
prefs.set("guidePosition", getColumnFromRulerClick(event));
}
function handleRulerMouseEnter(event) {
if (event.which === 1) {
handleRulerDrag(event);
} else {
handleRulerDragStop();
}
}
function handleRulerDragStart(event) {
// Only react to the left mouse click
if (event.which !== 1) { return; }
$rulerPanel.mousemove(handleRulerDrag);
$rulerPanel.mouseup(handleRulerDragStop);
$rulerPanel.mouseenter(handleRulerMouseEnter);
if (prefs.get("guidePosition") === getColumnFromRulerClick(event)) {
// Possibly a simple mouse click to toggle guide
isDragging = false;
} else {
// Definitely not a toggle, start the drag column reset
isDragging = true;
handleRulerDrag(event);
}
}
// --- Initialize Extension ---
AppInit.appReady(function () {
var viewMenu = Menus.getMenu(Menus.AppMenuBar.VIEW_MENU),
rulerContextMenu = Menus.registerContextMenu(RULER_CONTEXT_MENU);
// Register toggle commands and add them to menus
CommandManager.register(RULER_COMMAND_NAME, RULER_COMMAND_ID, handleToggleRuler);
CommandManager.register(GUIDE_COMMAND_NAME, GUIDE_COMMAND_ID, handleToggleGuide);
if (viewMenu) {
viewMenu.addMenuItem(RULER_COMMAND_ID);
viewMenu.addMenuItem(GUIDE_COMMAND_ID);
}
if (rulerContextMenu) {
rulerContextMenu.addMenuItem(RULER_COMMAND_ID);
rulerContextMenu.addMenuItem(GUIDE_COMMAND_ID);
}
// Load Style Sheet, User Interface, and Event Listeners
ExtensionUtils.loadStyleSheet(module, "ruler.css")
.done(function () {
// Create Ruler UI
$rulerPanel = ruler.createRulerPanel();
// Add Ruler Panel Event Listeners
$rulerPanel.on("contextmenu", function (e) {
rulerContextMenu.open(e);
});
$rulerPanel.mousedown(handleRulerDragStart);
// Add General Event Listeners
MainViewManager.on("currentFileChange", handleDocumentChange);
prefs.on("change", handlePrefsChange);
ViewCommandHandlers.on("fontSizeChange", function () {
ruler.refresh();
});
MainViewManager.on("paneCreate paneDestroy", function () {
// Update ruler and guide visibility.
// For now, they will only be visible in single view mode.
ruler.updateVisibility();
applyPrefs();
});
// Starting up Brackets: Fire a Document Change Event
handleDocumentChange();
});
});
});