fix clipboard on old webkit versions
This commit is contained in:
parent
051128c16f
commit
09d6741a02
2 changed files with 39 additions and 58 deletions
|
|
@ -36,6 +36,7 @@ var useragent = require("../lib/useragent");
|
||||||
var dom = require("../lib/dom");
|
var dom = require("../lib/dom");
|
||||||
var lang = require("../lib/lang");
|
var lang = require("../lib/lang");
|
||||||
var BROKEN_SETDATA = useragent.isChrome < 18;
|
var BROKEN_SETDATA = useragent.isChrome < 18;
|
||||||
|
var USE_IE_MIME_TIPE = useragent.isIE;
|
||||||
|
|
||||||
var TextInput = function(parentNode, host) {
|
var TextInput = function(parentNode, host) {
|
||||||
var text = dom.createElement("textarea");
|
var text = dom.createElement("textarea");
|
||||||
|
|
@ -54,7 +55,6 @@ var TextInput = function(parentNode, host) {
|
||||||
|
|
||||||
var PLACEHOLDER = "\x01\x01";
|
var PLACEHOLDER = "\x01\x01";
|
||||||
|
|
||||||
var cut = false;
|
|
||||||
var copied = false;
|
var copied = false;
|
||||||
var pasted = false;
|
var pasted = false;
|
||||||
var inComposition = false;
|
var inComposition = false;
|
||||||
|
|
@ -186,9 +186,7 @@ var TextInput = function(parentNode, host) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var onSelect = function(e) {
|
var onSelect = function(e) {
|
||||||
if (cut) {
|
if (copied) {
|
||||||
cut = false;
|
|
||||||
} else if (copied) {
|
|
||||||
copied = false;
|
copied = false;
|
||||||
} else if (isAllSelected(text)) {
|
} else if (isAllSelected(text)) {
|
||||||
host.selectAll();
|
host.selectAll();
|
||||||
|
|
@ -244,54 +242,30 @@ var TextInput = function(parentNode, host) {
|
||||||
resetValue();
|
resetValue();
|
||||||
};
|
};
|
||||||
|
|
||||||
var onCut = function(e) {
|
var handleClipboardData = function(e, data) {
|
||||||
var data = host.getCopyText();
|
|
||||||
if (!data) {
|
|
||||||
event.preventDefault(e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var clipboardData = e.clipboardData || window.clipboardData;
|
var clipboardData = e.clipboardData || window.clipboardData;
|
||||||
|
if (!clipboardData || BROKEN_SETDATA)
|
||||||
if (clipboardData && !BROKEN_SETDATA) {
|
|
||||||
// Safari 5 has clipboardData object, but does not handle setData()
|
|
||||||
var supported = clipboardData.setData("Text", data);
|
|
||||||
if (supported) {
|
|
||||||
host.onCut();
|
|
||||||
event.preventDefault(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!supported) {
|
|
||||||
cut = true;
|
|
||||||
text.value = data;
|
|
||||||
text.select();
|
|
||||||
setTimeout(function(){
|
|
||||||
cut = false;
|
|
||||||
resetValue();
|
|
||||||
resetSelection();
|
|
||||||
host.onCut();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var onCopy = function(e) {
|
|
||||||
var data = host.getCopyText();
|
|
||||||
if (!data) {
|
|
||||||
event.preventDefault(e);
|
|
||||||
return;
|
return;
|
||||||
}
|
// using "Text" doesn't work on old webkit but ie needs it
|
||||||
|
// TODO are there other browsers that require "Text"?
|
||||||
var clipboardData = e.clipboardData || window.clipboardData;
|
var mime = USE_IE_MIME_TIPE ? "Text" : "text/plain";
|
||||||
if (clipboardData && !BROKEN_SETDATA) {
|
if (data) {
|
||||||
// Safari 5 has clipboardData object, but does not handle setData()
|
// Safari 5 has clipboardData object, but does not handle setData()
|
||||||
var supported = clipboardData.setData("Text", data);
|
return clipboardData.setData(mime, data);
|
||||||
if (supported) {
|
} else {
|
||||||
host.onCopy();
|
return clipboardData.getData(mime);
|
||||||
event.preventDefault(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (!supported) {
|
}
|
||||||
|
|
||||||
|
var doCopy = function(e, isCut) {
|
||||||
|
var data = host.getCopyText();
|
||||||
|
if (!data)
|
||||||
|
return event.preventDefault(e);
|
||||||
|
|
||||||
|
if (handleClipboardData(e, data)) {
|
||||||
|
isCut ? host.onCut() : host.onCopy();
|
||||||
|
event.preventDefault(e);
|
||||||
|
} else {
|
||||||
copied = true;
|
copied = true;
|
||||||
text.value = data;
|
text.value = data;
|
||||||
text.select();
|
text.select();
|
||||||
|
|
@ -299,16 +273,22 @@ var TextInput = function(parentNode, host) {
|
||||||
copied = false;
|
copied = false;
|
||||||
resetValue();
|
resetValue();
|
||||||
resetSelection();
|
resetSelection();
|
||||||
host.onCopy();
|
isCut ? host.onCut() : host.onCopy();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var onPaste = function(e) {
|
var onCut = function(e) {
|
||||||
var clipboardData = e.clipboardData || window.clipboardData;
|
doCopy(e, true);
|
||||||
|
}
|
||||||
|
|
||||||
if (clipboardData) {
|
var onCopy = function(e) {
|
||||||
var data = clipboardData.getData("Text");
|
doCopy(e, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
var onPaste = function(e) {
|
||||||
|
var data = handleClipboardData(e);
|
||||||
|
if (typeof data == "string") {
|
||||||
if (data)
|
if (data)
|
||||||
host.onPaste(data);
|
host.onPaste(data);
|
||||||
if (useragent.isIE)
|
if (useragent.isIE)
|
||||||
|
|
@ -336,7 +316,7 @@ var TextInput = function(parentNode, host) {
|
||||||
if (!('oncut' in text) || !('oncopy' in text) || !('onpaste' in text)){
|
if (!('oncut' in text) || !('oncopy' in text) || !('onpaste' in text)){
|
||||||
event.addListener(parentNode, "keydown", function(e) {
|
event.addListener(parentNode, "keydown", function(e) {
|
||||||
if ((useragent.isMac && !e.metaKey) || !e.ctrlKey)
|
if ((useragent.isMac && !e.metaKey) || !e.ctrlKey)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
switch (e.keyCode) {
|
switch (e.keyCode) {
|
||||||
case 67:
|
case 67:
|
||||||
|
|
|
||||||
|
|
@ -76,7 +76,8 @@ exports.isLinux = (os == "linux");
|
||||||
// Windows Store JavaScript apps (aka Metro apps written in HTML5 and JavaScript) do not use the "Microsoft Internet Explorer" string in their user agent, but "MSAppHost" instead.
|
// Windows Store JavaScript apps (aka Metro apps written in HTML5 and JavaScript) do not use the "Microsoft Internet Explorer" string in their user agent, but "MSAppHost" instead.
|
||||||
exports.isIE =
|
exports.isIE =
|
||||||
(navigator.appName == "Microsoft Internet Explorer" || navigator.appName.indexOf("MSAppHost") >= 0)
|
(navigator.appName == "Microsoft Internet Explorer" || navigator.appName.indexOf("MSAppHost") >= 0)
|
||||||
&& parseFloat(navigator.userAgent.match(/(?:Trident\/[0-9]+[\.0-9]+;.*rv:|MSIE )([0-9]+[\.0-9]+)/)[1]);
|
? parseFloat((ua.match(/(?:MSIE |Trident\/[0-9]+[\.0-9]+;.*rv:)([0-9]+[\.0-9]+)/)||[])[1])
|
||||||
|
: parseFloat((ua.match(/(?:Trident\/[0-9]+[\.0-9]+;.*rv:)([0-9]+[\.0-9]+)/)||[])[1]); // for ie
|
||||||
|
|
||||||
exports.isOldIE = exports.isIE && exports.isIE < 9;
|
exports.isOldIE = exports.isIE && exports.isIE < 9;
|
||||||
|
|
||||||
|
|
@ -84,7 +85,7 @@ exports.isOldIE = exports.isIE && exports.isIE < 9;
|
||||||
exports.isGecko = exports.isMozilla = window.controllers && window.navigator.product === "Gecko";
|
exports.isGecko = exports.isMozilla = window.controllers && window.navigator.product === "Gecko";
|
||||||
|
|
||||||
// oldGecko == rev < 2.0
|
// oldGecko == rev < 2.0
|
||||||
exports.isOldGecko = exports.isGecko && parseInt((navigator.userAgent.match(/rv\:(\d+)/)||[])[1], 10) < 4;
|
exports.isOldGecko = exports.isGecko && parseInt((ua.match(/rv\:(\d+)/)||[])[1], 10) < 4;
|
||||||
|
|
||||||
// Is this Opera
|
// Is this Opera
|
||||||
exports.isOpera = window.opera && Object.prototype.toString.call(window.opera) == "[object Opera]";
|
exports.isOpera = window.opera && Object.prototype.toString.call(window.opera) == "[object Opera]";
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue