Skip to content

Commit c37011b

Browse files
author
Nick Gard
committed
Change setSelection to set cursor to the end of the previous value block OR to the beginning of the pattern
Examples 1) Given a pattern of '----- [[11 1111 ]]-' and a value of '23 45' (the input shows '----- [[23 _45_ ]]-') a) a selection anywhere after '5' will set the cursor immediately after the '5' b) a selection between '3' and '4' will set the cursor immediately after the '3' c) a selection anywhere before '2' will set the cursor immediately before the '2' d) a selection between '2' and '3' will not change 2) Given a pattern of '----- [[11 1111 ]]-' and no value (the input shows '----- [[__ ____ ]]-') a) a selection anywhere will set the cursor to the beginning of the pattern
1 parent 4e2ebf9 commit c37011b

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

lib/index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,6 @@ InputMask.prototype.input = function input(char) {
263263
// History
264264
if (this._historyIndex != null) {
265265
// Took more input after undoing, so blow any subsequent history away
266-
console.log('splice(', this._historyIndex, this._history.length - this._historyIndex, ')')
267266
this._history.splice(this._historyIndex, this._history.length - this._historyIndex)
268267
this._historyIndex = null
269268
}
@@ -461,10 +460,19 @@ InputMask.prototype.setSelection = function setSelection(selection) {
461460
this.selection.start = this.selection.end = this.pattern.firstEditableIndex
462461
return true
463462
}
464-
if (this.selection.end > this.pattern.lastEditableIndex + 1) {
465-
this.selection.start = this.selection.end = this.pattern.lastEditableIndex + 1
466-
return true
463+
// Set selection to the first editable, non-placeholder character before the selection
464+
// OR to the beginning of the pattern
465+
var index = this.selection.start
466+
while (index >= this.pattern.firstEditableIndex) {
467+
if (this.pattern.isEditableIndex(index - 1) &&
468+
this.value[index - 1] !== this.placeholderChar ||
469+
index === this.pattern.firstEditableIndex) {
470+
this.selection.start = this.selection.end = index
471+
break
472+
}
473+
index--
467474
}
475+
return true
468476
}
469477
return false
470478
}

test/index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ test('Pasting with leading static pattern in selection', function(t) {
348348
})
349349

350350
test('Setting selection', function(t) {
351-
t.plan(8)
351+
t.plan(16)
352352

353353
var mask = new InputMask({pattern: '----- [[11 1111 ]]-'})
354354
t.equal(mask.pattern.firstEditableIndex, 8, 'First editable index calculation')
@@ -358,10 +358,24 @@ test('Setting selection', function(t) {
358358
t.deepEqual(mask.selection, {start: 8, end: 8}, 'Cursor placed at first editable character')
359359
// ...or beyond the last editable character
360360
t.true(mask.setSelection({start: 18, end: 18}), 'Cursor after editable region is changed')
361-
t.deepEqual(mask.selection, {start: 15, end: 15}, 'Cursor placed after last editable character')
361+
t.deepEqual(mask.selection, {start: 8, end: 8}, 'Cursor placed after last editable character')
362362
// ...however a selection can span beyond the editable region
363363
t.false(mask.setSelection({start: 0, end: 19}), 'Selection beyond editable region not changed')
364364
t.deepEqual(mask.selection, {start: 0, end: 19}, 'Whole value can be selected')
365+
366+
var mask = new InputMask({pattern: '----- [[11 1111 ]]-', value: '23 45'})
367+
// Setting the selection before the first editable index moves selection to first editable index
368+
t.true(mask.setSelection({start: 0, end: 0}), 'Cursor before editable region, regardless of value, is changed')
369+
t.deepEqual(mask.selection, {start: 8, end: 8}, 'Cursor placed at first editable character, regardless of value')
370+
// Setting the selection within the value works as expected
371+
t.true(mask.setSelection({start: 9, end: 9}), 'Cursor within value is not changed')
372+
t.deepEqual(mask.selection, {start: 9, end: 9}, 'Cursor stays within value')
373+
// Setting the selection after the last editable index moves selection to the next editable index after the last value
374+
t.true(mask.setSelection({start: 18, end: 18}), 'Cursor after editable region is changed, regardless of value')
375+
t.deepEqual(mask.selection, {start: 14, end: 14}, 'Cursor placed at first editable character after last value')
376+
// Setting the selection after (but not within) a value moves selection to the next editable index after the previous value
377+
t.true(mask.setSelection({start: 11, end: 11}), 'Cursor after editable region with values both before and after it is changed')
378+
t.deepEqual(mask.selection, {start: 10, end: 10}, 'Cursor placed at first editable character after prior value')
365379
})
366380

367381
test('History', function(t) {

0 commit comments

Comments
 (0)