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

55 lines
1.8 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 = {
simpleCopy: !!(Launcher && Launcher.clipboardSupported),
2016-01-22 18:51:36 +01:00
copy: function(text) {
if (this.simpleCopy) {
2016-01-22 18:51:36 +01:00
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);
}
2019-08-16 23:05:39 +02:00
return { success: true, seconds: clipboardSeconds };
2016-01-22 18:51:36 +01:00
} else {
try {
if (document.execCommand('copy')) {
2019-08-16 23:05:39 +02:00
return { success: true };
2016-01-22 18:51:36 +01:00
}
2019-08-16 23:05:39 +02:00
} 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({
2019-08-16 23:05:39 +02:00
'copy cut paste': function() {
setTimeout(() => hiddenInput.blur(), 0);
},
blur: function() {
hiddenInput.remove();
}
2015-10-17 23:49:24 +02:00
});
}
};
module.exports = CopyPaste;