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

60 lines
2.0 KiB
JavaScript
Raw Normal View History

2015-10-17 23:49:24 +02:00
'use strict';
var FeatureDetector = require('../util/feature-detector'),
Launcher = require('./launcher'),
AppSettingsModel = require('../models/app-settings-model');
2015-10-29 22:13:15 +01:00
2015-10-17 23:49:24 +02:00
var CopyPaste = {
tryCopy: function() {
try {
var success = document.execCommand('copy');
if (success) {
this.copied();
}
return success;
2015-10-17 23:49:24 +02:00
} 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(); }
});
},
copied: function() {
if (Launcher) {
var clipboardSeconds = AppSettingsModel.instance.get('clipboardSeconds');
if (clipboardSeconds > 0) {
setTimeout(function() {
setTimeout((function (prevText) {
if (Launcher.getClipboardText() === prevText) {
Launcher.clearClipboardText();
}
}).bind(null, Launcher.getClipboardText()), clipboardSeconds * 1000);
}, 0);
}
2016-01-11 18:56:52 +01:00
return clipboardSeconds;
}
2015-10-17 23:49:24 +02:00
}
};
module.exports = CopyPaste;