forked from peterflynn/everyscrub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
225 lines (193 loc) · 9.09 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
/*
* Copyright (c) 2012 Peter Flynn.
*
* 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, nomen: true, indent: 4, maxerr: 50, regexp: true, bitwise: true */
/*global define, brackets, $, window */
define(function (require, exports, module) {
'use strict';
// Brackets modules
var EditorManager = brackets.getModule("editor/EditorManager"),
InlineTextEditor = brackets.getModule("editor/InlineTextEditor").InlineTextEditor;
var isMac = (brackets.platform === "mac");
// Utilities
function clip(val, max) {
return (val < 0 ? 0 : (val > max ? max : val));
}
// Scrubbing a single number (with optional suffix)
function SimpleNumberScrub(string) {
// Support numbers with a suffix like "px" or "%"
var extras = /(-?[\d\.]+)([^\d\-\.]*)/.exec(string);
var origStringValue = (extras && extras[1]) || string;
this.suffix = (extras && extras[2]) || "";
this.origValue = parseFloat(origStringValue);
// Increment slower for numbers with decimal (even if it's ".0")
this.increment = (origStringValue.indexOf(".") === -1) ? 1 : 0.1;
}
SimpleNumberScrub.matches = function (token) {
// CodeMirror marks all sorts of CSS tokens as "number", including enums like "auto" and segments of URL paths
return token.className === "number" && !isNaN(parseFloat(token.string));
};
SimpleNumberScrub.prototype.update = function (delta) {
var newVal = this.origValue + (delta * this.increment);
if (this.increment < 1) {
newVal = Math.round(newVal * 10) / 10; // prevent rounding errors from adding extra decimals
}
var str = String(newVal);
if (this.increment < 1 && str.indexOf(".") === -1) {
str += ".0"; // don't jitter to a shorter length when passing a whole number
}
return str + this.suffix;
};
// Scrubbing 3-digit hex color
function Color3Scrub(string) {
this.r = parseInt(string[1], 16);
this.g = parseInt(string[2], 16);
this.b = parseInt(string[3], 16);
}
Color3Scrub.matches = function (token) {
return token.className === "atom" && token.string.match(/#[0-9a-f]{3}$/i);
};
Color3Scrub.prototype.update = function (delta) {
var r = clip(this.r + delta, 15);
var g = clip(this.g + delta, 15);
var b = clip(this.b + delta, 15);
return "#" + r.toString(16) + g.toString(16) + b.toString(16);
};
// Scrubbing 6-digit hex color
function Color6Scrub(string) {
this.r = parseInt(string[1] + string[2], 16);
this.g = parseInt(string[3] + string[4], 16);
this.b = parseInt(string[5] + string[6], 16);
}
Color6Scrub.matches = function (token) {
return token.className === "atom" && token.string.match(/#[0-9a-f]{6}$/i);
};
Color6Scrub.prototype.update = function (delta) {
function force2Digits(str) {
if (str.length === 1) {
str = "0" + str;
}
return str;
}
var r = clip(this.r + delta, 255);
var g = clip(this.g + delta, 255);
var b = clip(this.b + delta, 255);
return "#" + force2Digits(r.toString(16)) + force2Digits(g.toString(16)) + force2Digits(b.toString(16));
};
function parseForScrub(token) {
if (Color3Scrub.matches(token)) {
return new Color3Scrub(token.string);
} else if (Color6Scrub.matches(token)) {
return new Color6Scrub(token.string);
} else if (SimpleNumberScrub.matches(token)) {
return new SimpleNumberScrub(token.string);
}
return null;
}
/** Main scrubbing event handling. Detects number format, adds global move/up listeners, detaches when done */
function handleEditorMouseDown(editor, event) {
// Drag state
var scrubState; // instance of one of the *Scrub classes
var downX; // mousedown pageX
var lastValue; // last value from scrubState.update()
var lastRange; // text range of lastValue in the code
function delta(event) {
var pxDelta = event.pageX - downX;
return (pxDelta / 8) | 0; // "| 0" truncates to int
}
function moveHandler(event) {
var newVal = scrubState.update(delta(event));
if (newVal !== lastValue) {
lastValue = newVal;
editor._codeMirror.replaceRange(newVal, lastRange.start, lastRange.end);
lastRange.end.ch = lastRange.start.ch + newVal.length;
// editor.setSelection(lastRange.start, lastRange.end);
}
}
function upHandler(event) {
$(window.document).off("mousemove", moveHandler);
$(window.document).off("mouseup", upHandler);
}
// coordsChar() returns the closest insertion point, not always char the click was ON.
// -------------------
// | I* X | | * = mousedn
// ------------------- X = coordsChar().ch, interpreted as a char pos
// | | *I X | I = coordsChar().ch, interpreted as a cursor pos / insertion point
// -------------------
var pos = editor._codeMirror.coordsChar({x: event.pageX, y: event.pageY});
var chLeftEdge = editor._codeMirror.charCoords(pos).x;
var mousedownCh = (chLeftEdge <= event.pageX) ? pos.ch : pos.ch - 1;
// ch+1 because getTokenAt() returns the token *ending* at cursor pos 'ch' (char at 'ch' is NOT part of the token)
var token = editor._codeMirror.getTokenAt({line: pos.line, ch: mousedownCh + 1});
// Is this token a value we can scrub? Init value-specific state if so
scrubState = parseForScrub(token);
if (scrubState) {
event.stopPropagation();
event.preventDefault();
downX = event.pageX;
lastValue = token.string;
lastRange = {start: {line: pos.line, ch: token.start}, end: {line: pos.line, ch: token.end}};
$(window.document).mousemove(moveHandler);
$(window.document).mouseup(upHandler);
// editor.setSelection(lastRange.start, lastRange.end);
editor.setCursorPos(lastRange.start.line, lastRange.end.ch);
}
}
/** Finds innermost editor containing the given element */
function editorFromElement(element) {
var result;
var fullEditor = EditorManager.getCurrentFullEditor();
if (fullEditor) {
fullEditor.getInlineWidgets().forEach(function (widget) {
if (widget.htmlContent.contains(element)) {
if (widget instanceof InlineTextEditor) {
widget.editors.forEach(function (editor) {
if (editor.getRootElement().contains(element)) {
result = editor;
}
});
} else {
// Ignore mousedown on inline widgets other than editors
result = null;
}
}
});
if (result !== undefined) {
return result;
} else {
return fullEditor;
}
}
return null;
}
function handleMouseDown(event) {
// We only care about ctrl+drag on Win, cmd+drag on Mac
if (event.which === 1 && ((isMac && event.metaKey) || (!isMac && event.ctrlKey))) {
// Which editor did mousedown occur on (inline vs. full-size vs. no editor open)
var editor = editorFromElement(event.target);
if (editor) {
handleEditorMouseDown(editor, event);
}
}
}
// Init: listen to all mousedowns in the editor area
$("#editor-holder")[0].addEventListener("mousedown", handleMouseDown, true);
});