fix copy and paste issue in Safari
Safari would not fire a "copy" event if the selection is empty
This commit is contained in:
parent
d16c6cf385
commit
fb917b3965
1 changed files with 30 additions and 10 deletions
|
|
@ -16,25 +16,40 @@ var TextInput = function(parentNode, host) {
|
||||||
style.top = "-10000px";
|
style.top = "-10000px";
|
||||||
parentNode.appendChild(text);
|
parentNode.appendChild(text);
|
||||||
|
|
||||||
|
var PLACEHOLDER = String.fromCharCode(0);
|
||||||
|
sendText();
|
||||||
|
|
||||||
var inCompostion = false;
|
var inCompostion = false;
|
||||||
|
var copied = false;
|
||||||
|
|
||||||
|
function sendText() {
|
||||||
|
if (!copied) {
|
||||||
|
var value = text.value;
|
||||||
|
if (value) {
|
||||||
|
if (value.charCodeAt(value.length-1) == PLACEHOLDER.charCodeAt(0))
|
||||||
|
host.onTextInput(value.slice(0, -1));
|
||||||
|
else
|
||||||
|
host.onTextInput(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
copied = false;
|
||||||
|
|
||||||
|
// Safari doesn't fire copy events if no text is selected
|
||||||
|
text.value = PLACEHOLDER;
|
||||||
|
text.select();
|
||||||
|
}
|
||||||
|
|
||||||
var onTextInput = function(e) {
|
var onTextInput = function(e) {
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
if (!inCompostion) {
|
if (!inCompostion)
|
||||||
if (text.value)
|
sendText();
|
||||||
host.onTextInput(text.value);
|
|
||||||
text.value = "";
|
|
||||||
}
|
|
||||||
}, 0);
|
}, 0);
|
||||||
};
|
};
|
||||||
|
|
||||||
var onCompositionStart = function(e) {
|
var onCompositionStart = function(e) {
|
||||||
inCompostion = true;
|
inCompostion = true;
|
||||||
|
sendText();
|
||||||
if (text.value)
|
|
||||||
host.onTextInput(text.value);
|
|
||||||
text.value = "";
|
text.value = "";
|
||||||
|
|
||||||
host.onCompositionStart();
|
host.onCompositionStart();
|
||||||
setTimeout(onCompositionUpdate, 0);
|
setTimeout(onCompositionUpdate, 0);
|
||||||
};
|
};
|
||||||
|
|
@ -50,11 +65,14 @@ var TextInput = function(parentNode, host) {
|
||||||
};
|
};
|
||||||
|
|
||||||
var onCopy = function() {
|
var onCopy = function() {
|
||||||
|
copied = true;
|
||||||
text.value = host.getCopyText();
|
text.value = host.getCopyText();
|
||||||
text.select();
|
text.select();
|
||||||
|
copied = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
var onCut = function() {
|
var onCut = function() {
|
||||||
|
copied = true;
|
||||||
text.value = host.getCopyText();
|
text.value = host.getCopyText();
|
||||||
host.onCut();
|
host.onCut();
|
||||||
text.select();
|
text.select();
|
||||||
|
|
@ -78,10 +96,12 @@ var TextInput = function(parentNode, host) {
|
||||||
|
|
||||||
event.addListener(text, "focus", function() {
|
event.addListener(text, "focus", function() {
|
||||||
host.onFocus();
|
host.onFocus();
|
||||||
|
text.select();
|
||||||
});
|
});
|
||||||
|
|
||||||
this.focus = function() {
|
this.focus = function() {
|
||||||
text.focus();
|
text.focus();
|
||||||
|
text.select();
|
||||||
};
|
};
|
||||||
|
|
||||||
this.blur = function() {
|
this.blur = function() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue