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

56 lines
1.9 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 = {
2016-01-22 18:51:36 +01:00
simpleCopy: !!Launcher,
copy: function(text) {
if (Launcher) {
Launcher.setClipboardText(text);
var clipboardSeconds = AppSettingsModel.instance.get('clipboardSeconds');
if (clipboardSeconds > 0) {
2016-07-17 13:30:38 +02:00
setTimeout(() => {
2016-01-22 18:51:36 +01:00
if (Launcher.getClipboardText() === text) {
Launcher.clearClipboardText();
}
}, clipboardSeconds * 1000);
}
2016-01-22 18:51:36 +01:00
return {success: true, seconds: clipboardSeconds};
} else {
try {
if (document.execCommand('copy')) {
return {success: true};
}
} catch (e) { }
2015-10-17 23:49:24 +02:00
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({
2016-07-17 13:30:38 +02:00
'copy cut paste': function() { setTimeout(() => hiddenInput.blur(), 0); },
2015-10-17 23:49:24 +02:00
blur: function() { hiddenInput.remove(); }
});
}
};
module.exports = CopyPaste;