"use strict"; function WebswingClipboard(injector) { var module = this; var api; module.injects = { cfg: 'webswing.config', send: 'socket.send', focusDefault: 'focusManager.focusDefault', }; module.provides = { 'clipboard.cut': cut, 'clipboard.copy': copy, 'clipboard.paste': paste, 'clipboard.displayCopyBar': writeToClipboard, 'clipboard.displayPasteDialog': () => {}, 'clipboard.dispose': close, 'clipboard.register': () => {}, 'clipboard.updateLocalData': () => {} }; module.ready = function () { api = injector.modules.clipboard.injected; }; function cut(event) { copy(event, true); } function copy(event, cut) { if (api.cfg.ieVersion && api.cfg.ieVersion <= 11) { window.clipboardData.setData('Text', ''); } else { event.clipboardData.setData('text/plain', ''); } api.send({ clipboardEvent: { type: cut === true ? 2 : 0 } }); } function paste(event, special) { if (api.cfg.hasControl) { var text = ''; var html = ''; if (api.cfg.ieVersion && api.cfg.ieVersion <= 11) { text = window.clipboardData.getData('Text'); html = text; } else { var data = event.clipboardData || event.originalEvent.clipboardData; text = data.getData('text/plain'); html = data.getData('text/html'); if (data.items != null) { for (var i = 0; i < data.items.length; i++) { if (data.items[i].type.indexOf('image') === 0) { var img = data.items[i]; var reader = new FileReader(); reader.onload = function (event) { sendPasteEvent(special, text, html, event.target.result); }; var imgAsFile = img.getAsFile(); if (imgAsFile) { reader.readAsDataURL(img.getAsFile()); } return; } } } } sendPasteEvent(special, text, html); } } function sendPasteEvent(special, text, html, img) { api.send({ clipboardEvent: { type: 1, special: !!special, text: text != null && text.length !== 0 ? text : undefined, html: html != null && html.length !== 0 ? html : undefined, img: img != null ? img : undefined } }); } function writeToClipboard(data) { var item; if (data.text != null && data.text.length !== 0) { navigator.clipboard.writeText(data.text).then(function() { console.log("Copied to clipboard successfully!"); }, function(e) { console.error("Unable to write to clipboard. :-(" + e ); }); } else if(data.html != null && data.html.length !== 0) { item = [new ClipboardItem({ "text/html": new Blob([data.html], { type: "text/html" }) })]; } else if(data.img != null) { item = [new ClipboardItem({ "image/png": new Blob([data.img], { type: "image/png" }) })]; } if(item != null) { navigator.clipboard.write(item).then(function() { console.log("Copied to clipboard successfully!"); }, function(e) { console.error("Unable to write to clipboard. :-(" + e ); }); } } function close() { api.focusDefault(); } }