Skip to content
This repository has been archived by the owner on Sep 8, 2020. It is now read-only.

ctrl+v #183

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/mask.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ angular.module('ui.mask', [])
oldValue, oldValueUnmasked, oldCaretPosition, oldSelectionLength,
// Used for communicating if a backspace operation should be allowed between
// keydownHandler and eventHandler
preventBackspace;
preventBackspace,
isCtrlV; // detect copy paste

var originalIsEmpty = controller.$isEmpty;
controller.$isEmpty = function(value) {
Expand Down Expand Up @@ -523,6 +524,8 @@ angular.module('ui.mask', [])
var isKeyBackspace = e.which === 8,
caretPos = getCaretPosition(this) - 1 || 0, //value in keydown is pre change so bump caret position back to simulate post change
isCtrlZ = e.which === 90 && e.ctrlKey; //ctrl+z pressed

isCtrlV = e.which === 86 && e.ctrlKey; //ctrl+v pressed

if (isKeyBackspace) {
while(caretPos >= 0) {
Expand Down Expand Up @@ -670,6 +673,8 @@ angular.module('ui.mask', [])
caretPos++;
}
oldCaretPosition = caretPos;
// Move caret to the end in case of paste event
caretPos = isCtrlV ? getMaskedInputLength(valMasked, originalPlaceholder) : caretPos;
setCaretPosition(this, caretPos);
}

Expand Down Expand Up @@ -730,6 +735,19 @@ angular.module('ui.mask', [])
}
return 0;
}

// Get index of the last char that is not equal to placeholder
var getMaskedInputLength = function(masked, placeholder) {
if (masked === placeholder) {
return 0;
}

var i = masked.length;
while(masked[i] === placeholder[i]) {
i--;
}
return i + 1; // place cursor after the found char
};

// https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf
if (!Array.prototype.indexOf) {
Expand Down