keeweb/app/scripts/util/copy-paste.js

38 lines
1.2 KiB
JavaScript
Raw Normal View History

2015-10-17 23:49:24 +02:00
'use strict';
2015-10-29 22:13:15 +01:00
var FeatureDetector = require('./feature-detector');
2015-10-17 23:49:24 +02:00
var CopyPaste = {
tryCopy: function() {
try {
return document.execCommand('copy');
} catch (e) {
return false;
}
},
2015-10-27 22:57:56 +01:00
createHiddenInput: function(text, pos) {
2015-10-17 23:49:24 +02:00
var hiddenInput = $('<input/>')
.val(text)
2015-10-29 22:13:15 +01:00
.attr({ type: 'text', 'class': pos ? '' : 'hide-by-pos' })
2015-10-27 22:57:56 +01:00
.appendTo(document.body);
2015-10-29 22:13:15 +01:00
if (FeatureDetector.canCopyReadonlyInput()) {
hiddenInput.attr('readonly', true);
}
2015-10-27 22:57:56 +01:00
if (pos) {
2015-10-29 22:13:15 +01:00
hiddenInput.css({ position: 'absolute', zIndex: 100, padding: '0 .6em',
2015-10-27 22:57:56 +01:00
border: 'none', background: 'transparent', color: 'transparent',
left: pos.left, top: pos.top, width: pos.width, height: pos.height });
}
hiddenInput[0].selectionStart = 0;
hiddenInput[0].selectionEnd = text.length;
2015-10-29 22:13:15 +01:00
hiddenInput.focus();
2015-10-17 23:49:24 +02:00
hiddenInput.on({
2015-10-29 22:13:15 +01:00
'copy cut paste': function() { setTimeout(function() { hiddenInput.blur(); }, 0); },
2015-10-17 23:49:24 +02:00
blur: function() { hiddenInput.remove(); }
});
}
};
module.exports = CopyPaste;