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

51 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-11-26 20:44:53 +01:00
const Backbone = require('backbone');
2017-01-31 07:50:28 +01:00
const Launcher = require('./launcher');
const AppSettingsModel = require('../models/app-settings-model');
2015-10-29 22:13:15 +01:00
2017-01-31 07:50:28 +01:00
const CopyPaste = {
2016-01-22 18:51:36 +01:00
simpleCopy: !!Launcher,
copy: function(text) {
if (Launcher) {
Launcher.setClipboardText(text);
2017-01-31 07:50:28 +01:00
const clipboardSeconds = AppSettingsModel.instance.get('clipboardSeconds');
2016-01-22 18:51:36 +01:00
if (clipboardSeconds > 0) {
2017-11-26 20:44:53 +01:00
const clearClipboard = () => {
2016-01-22 18:51:36 +01:00
if (Launcher.getClipboardText() === text) {
Launcher.clearClipboardText();
}
2017-11-26 20:44:53 +01:00
};
Backbone.on('main-window-will-close', clearClipboard);
setTimeout(() => {
clearClipboard();
Backbone.off('main-window-will-close', clearClipboard);
2016-01-22 18:51:36 +01:00
}, 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;
}
},
createHiddenInput: function(text) {
2017-01-31 07:50:28 +01:00
const hiddenInput = $('<input/>')
2015-10-17 23:49:24 +02:00
.val(text)
.attr({ type: 'text', 'class': 'hide-by-pos' })
2015-10-27 22:57:56 +01:00
.appendTo(document.body);
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;