vsdxImporter.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. var mxIsElectron = navigator.userAgent != null &&
  2. navigator.userAgent.toLowerCase().indexOf(' electron/') > -1 &&
  3. navigator.userAgent.indexOf(' draw.io/') > -1;
  4. /**
  5. * Adds meta tag to the page.
  6. */
  7. function mxmeta(content, httpEquiv)
  8. {
  9. try
  10. {
  11. var s = document.createElement('meta');
  12. s.setAttribute('content', content);
  13. s.setAttribute('http-equiv', httpEquiv);
  14. var t = document.getElementsByTagName('meta')[0];
  15. t.parentNode.insertBefore(s, t);
  16. }
  17. catch (e)
  18. {
  19. // ignore
  20. }
  21. };
  22. function doImport(vsdxBuff, callback, error, file, customParam)
  23. {
  24. EditorUi.prototype.createUi = function(){};
  25. EditorUi.prototype.addTrees = function(){};
  26. EditorUi.prototype.updateActionStates = function(){};
  27. var editorUi = new EditorUi();
  28. var blob = file? file : new Blob([vsdxBuff], {type: 'application/octet-stream'});
  29. editorUi.importVisio(blob, callback, error, file? file.name : null, customParam);
  30. };
  31. if (mxIsElectron)
  32. {
  33. mxmeta('default-src \'self\'; script-src \'self\'; connect-src \'self\' https://*.draw.io https://*.diagrams.net https://fonts.googleapis.com https://fonts.gstatic.com; img-src * data:; media-src *; font-src *; frame-src \'none\'; style-src \'self\' \'unsafe-inline\' https://fonts.googleapis.com; base-uri \'none\';child-src \'self\';object-src \'none\';', 'Content-Security-Policy');
  34. electron.registerMsgListener('import', (vsdxBuff) =>
  35. {
  36. doImport(vsdxBuff, function(xml)
  37. {
  38. electron.sendMessage('import-success', xml);
  39. },
  40. function()
  41. {
  42. electron.sendMessage('import-error');
  43. });
  44. });
  45. }
  46. window.addEventListener('load', function()
  47. {
  48. document.getElementById('fileUpload').addEventListener('change', function()
  49. {
  50. const curFiles = this.files;
  51. if(curFiles.length > 0)
  52. {
  53. function createDoneDiv(msg)
  54. {
  55. var doneDiv = document.createElement('div');
  56. doneDiv.id = 'doneDiv';
  57. doneDiv.innerHTML = msg;
  58. document.body.appendChild(doneDiv);
  59. };
  60. doImport(null, function(xml)
  61. {
  62. window.importResXML = xml;
  63. createDoneDiv('success');
  64. }, function(err)
  65. {
  66. console.log(err)
  67. createDoneDiv('error');
  68. }, curFiles[0], window.customParam);
  69. }
  70. });
  71. });