Skip to content

Commit 2439de0

Browse files
committed
refactoring and added support for moving the slider with arrow keys
1 parent 7c671ed commit 2439de0

1 file changed

Lines changed: 36 additions & 25 deletions

File tree

public/js/libs/numberPicker.js

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ var numberPicker = (function() {
1111
var startReplace, endReplace;
1212

1313
// Bind the numberPicker to change events
14-
slider.bind('change', function(e) {
14+
slider.change(function(e) {
1515
e.preventDefault();
1616

17-
editor.off('cursorActivity', onCursorActivity);
17+
// editor.off('cursorActivity', onCursorActivity);
1818

1919
var cur = editor.getCursor();
2020
var token = editor.getTokenAt(cur);
21+
editor.setCursor({line: cur.line, ch: token.end});
2122

2223
// when cursor is in front of a token, the token is then null,
23-
// so replace token with tokenAhead which is at a ch+1
24+
// replace token with tokenAhead which is at a ch+1
2425
var tokenAhead = editor.getTokenAt({line:cur.line, ch:cur.ch+1});
2526
token = token.type === null ? tokenAhead : token;
2627

@@ -29,29 +30,20 @@ var numberPicker = (function() {
2930

3031
// actual value, the original token value + offset
3132
var value = (tokenOriginalValue + offset) + '';
33+
// console.log('tokenOriginalValue:', tokenOriginalValue);
34+
editor.replaceRange(value, {line:cur.line, ch:startReplace}, {line:cur.line, ch:endReplace});
3235

33-
// console.log('value.length:', value.length);
34-
// console.log('endReplace', (endReplace - startReplace));
35-
36-
if (value.length == (endReplace - startReplace)) {
37-
editor.replaceRange(value, {line:cur.line, ch:startReplace}, {line:cur.line, ch:endReplace});
38-
}
39-
else if(value.length < (endReplace - startReplace)) {
40-
// console.log('value.length < (endReplace - startReplace)');
41-
editor.replaceRange(value, {line:cur.line, ch:startReplace}, {line:cur.line, ch:endReplace});
36+
if(value.length < (endReplace - startReplace)) {
4237
endReplace--;
43-
}
44-
else if(value.length > (endReplace - startReplace)) {
45-
// console.log('value.length > (endReplace - startReplace)');
46-
editor.replaceRange(value, {line:cur.line, ch:startReplace}, {line:cur.line, ch:endReplace});
38+
} else if(value.length > (endReplace - startReplace)) {
4739
endReplace++;
4840
}
4941
});
5042

5143
editor.setOption('onKeyEvent', function(cm, s){
5244
if (s.type == 'keyup') {
5345
showNumberPicker(cm);
54-
} // if (s.type == 'keyup') {
46+
}
5547
});
5648

5749
editor.on('focus', function(cm) {
@@ -65,8 +57,7 @@ var numberPicker = (function() {
6557

6658
function onCursorActivity(cm) {
6759
// console.log('cursorActivity');
68-
sliderDiv.css('visibility', 'hidden');
69-
slider.val(50);
60+
hideSlider(cm);
7061
showNumberPicker(cm);
7162
}
7263

@@ -87,15 +78,16 @@ var numberPicker = (function() {
8778
.css('top', (pos.top - 15) + 'px');
8879
}
8980

90-
// match any ints in the token string
81+
// we don't want to match colors
9182
var string = token.string;
9283
var hex = string.match(/#([A-Fa-f0-9]{6})/g);
9384
if (hex !== null) {
9485
return;
9586
}
9687

88+
// match any ints in the token string
9789
var matches = string.match(/-?\d+/g), match;
98-
if (matches !== null) {
90+
if (matches) {
9991
match = matches[0];
10092
tokenOriginalValue = parseInt(match, 10);
10193

@@ -111,16 +103,35 @@ var numberPicker = (function() {
111103
// check if in the int boudaries
112104
if(cur.ch >= token.start + startInt && cur.ch <= token.start + endInt) {
113105
sliderDiv.css('visibility', 'visible');
106+
107+
// here we could bing right and left keys to
108+
// move the slider with the keyboard
109+
cm.addKeyMap(sliderMap);
110+
114111
} else {
115-
sliderDiv.css('visibility', 'hidden');
116-
slider.val(50);
112+
hideSlider(cm);
117113
}
118114
} else {
119-
sliderDiv.css('visibility', 'hidden');
120-
slider.val(50);
115+
hideSlider(cm);
121116
}
122117
}
123118

119+
var sliderMap = {
120+
Left: function() {
121+
slider.get(0).stepDown(1);
122+
slider.trigger('change');
123+
},
124+
Right: function() {
125+
slider.get(0).stepUp(1);
126+
slider.trigger('change');
127+
}
128+
};
129+
130+
function hideSlider(cm) {
131+
sliderDiv.css('visibility', 'hidden');
132+
slider.val(50);
133+
cm.removeKeyMap(sliderMap);
134+
}
124135
//-------------------------------------------------------
125136
//
126137
// Test mouse pointer over tokens, incomplete implementation

0 commit comments

Comments
 (0)