ElectronApp.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. window.TEMPLATE_PATH = 'templates';
  2. FeedbackDialog.feedbackUrl = 'https://log.draw.io/email';
  3. (function()
  4. {
  5. // Overrides default mode
  6. App.mode = App.MODE_DEVICE;
  7. // Redirects printing to iframe to avoid document.write
  8. PrintDialog.showPreview = function(preview, print)
  9. {
  10. var iframe = document.createElement('iframe');
  11. document.body.appendChild(iframe);
  12. // Workaround for lost gradients in print output
  13. var getBaseUrl = mxSvgCanvas2D.prototype.getBaseUrl;
  14. mxSvgCanvas2D.prototype.getBaseUrl = function()
  15. {
  16. return '';
  17. };
  18. // Renders print output into iframe and prints
  19. var result = preview.open(null, iframe.contentWindow);
  20. iframe.contentWindow.print();
  21. iframe.parentNode.removeChild(iframe);
  22. mxSvgCanvas2D.prototype.getBaseUrl = getBaseUrl;
  23. return result;
  24. };
  25. var menusInit = Menus.prototype.init;
  26. Menus.prototype.init = function()
  27. {
  28. menusInit.apply(this, arguments);
  29. var editorUi = this.editorUi;
  30. // Replaces file menu to replace openFrom menu with open and rename downloadAs to export
  31. this.put('file', new Menu(mxUtils.bind(this, function(menu, parent)
  32. {
  33. this.addMenuItems(menu, ['new', 'open', '-', 'save', 'saveAs', '-'], parent);
  34. this.addSubmenu('exportAs', menu, parent);
  35. this.addSubmenu('embed', menu, parent);
  36. this.addMenuItems(menu, ['-', 'newLibrary', 'openLibrary', '-', 'documentProperties', 'print'], parent);
  37. })));
  38. this.put('extras', new Menu(mxUtils.bind(this, function(menu, parent)
  39. {
  40. this.addMenuItems(menu, ['copyConnect', 'collapseExpand', '-', 'mathematicalTypesetting', 'autosave', '-',
  41. 'createShape', 'editDiagram', '-', 'online'], parent);
  42. })));
  43. };
  44. // Initializes the user interface
  45. var editorUiInit = EditorUi.prototype.init;
  46. EditorUi.prototype.init = function()
  47. {
  48. editorUiInit.apply(this, arguments);
  49. var editorUi = this;
  50. var graph = this.editor.graph;
  51. global.__emt_isModified = e => {
  52. if (this.getCurrentFile())
  53. return this.getCurrentFile().isModified()
  54. return false
  55. }
  56. // global.__emt_getCurrentFile = e => {
  57. // return this.getCurrentFile()
  58. // }
  59. // Adds support for libraries
  60. this.actions.addAction('newLibrary...', mxUtils.bind(this, function()
  61. {
  62. editorUi.showLibraryDialog(null, null, null, null, App.MODE_DEVICE);
  63. }));
  64. this.actions.addAction('openLibrary...', mxUtils.bind(this, function()
  65. {
  66. editorUi.pickLibrary(App.MODE_DEVICE);
  67. }));
  68. // // Replaces import action
  69. // this.actions.addAction('import...', mxUtils.bind(this, function()
  70. // {
  71. // if (this.getCurrentFile() != null)
  72. // {
  73. // chrome.fileSystem.chooseEntry({type: 'openFile', acceptsAllTypes: true}, mxUtils.bind(this, function(fileEntry)
  74. // {
  75. // if (!chrome.runtime.lastError)
  76. // {
  77. // fileEntry.file(mxUtils.bind(this, function(fileObject)
  78. // {
  79. // if (editorUi.spinner.spin(document.body, mxResources.get('loading')))
  80. // {
  81. // var reader = new FileReader();
  82. //
  83. // reader.onload = function(evt)
  84. // {
  85. // editorUi.spinner.stop();
  86. //
  87. // try
  88. // {
  89. // var data = reader.result;
  90. //
  91. // if (fileObject.type.substring(0, 9) == 'image/png')
  92. // {
  93. // data = editorUi.extractGraphModelFromPng(data);
  94. // }
  95. // else if (fileObject.type.substring(0, 6) == 'image/')
  96. // {
  97. // data = null;
  98. // }
  99. //
  100. // if (data != null)
  101. // {
  102. // graph.setSelectionCells(editorUi.importXml(data));
  103. // }
  104. // else
  105. // {
  106. // var img = new Image();
  107. // img.onload = function()
  108. // {
  109. // editorUi.resizeImage(img, reader.result, function(data2, w, h)
  110. // {
  111. // var pt = graph.getInsertPoint();
  112. // graph.setSelectionCell(graph.insertVertex(null, null, '', pt.x, pt.y, w, h,
  113. // 'shape=image;aspect=fixed;image=' + editorUi.convertDataUri(data2) + ';'));
  114. // }, true);
  115. // };
  116. // img.src = reader.result;
  117. // }
  118. // }
  119. // catch(e)
  120. // {
  121. // console.log(e);
  122. // editorUi.handleError(e);
  123. // }
  124. // };
  125. //
  126. // reader.onerror = function(ev)
  127. // {
  128. // editorUi.spinner.stop();
  129. // editorUi.handleError(ev);
  130. // };
  131. //
  132. // if (fileObject.type.substring(0, 6) == 'image/')
  133. // {
  134. // reader.readAsDataURL(fileObject);
  135. // }
  136. // else
  137. // {
  138. // reader.readAsText(fileObject);
  139. // }
  140. // }
  141. // }));
  142. // }
  143. // else if (chrome.runtime.lastError.message != 'User cancelled')
  144. // {
  145. // editorUi.handleError(chrome.runtime.lastError);
  146. // }
  147. // }));
  148. // }
  149. // }));
  150. // Replaces new action
  151. var oldNew = this.actions.get('new').funct;
  152. this.actions.addAction('new...', mxUtils.bind(this, function()
  153. {
  154. mxLog.debug(this.getCurrentFile());
  155. if (this.getCurrentFile() == null)
  156. {
  157. oldNew();
  158. }
  159. else {
  160. const ipc = require('electron').ipcRenderer
  161. ipc.sendSync('winman', {action: 'newfile', opt: {width: 1600}})
  162. }
  163. }), null, null, 'Ctrl+N');
  164. this.actions.get('open').shortcut = 'Ctrl+O';
  165. // Adds shortcut keys for file operations
  166. editorUi.keyHandler.bindAction(78, true, 'new'); // Ctrl+N
  167. editorUi.keyHandler.bindAction(79, true, 'open'); // Ctrl+O
  168. editorUi.actions.addAction('quickStart...', function()
  169. {
  170. const {shell} = require('electron');
  171. shell.openExternal('https://www.youtube.com/watch?v=8OaMWa4R1SE&t=1');
  172. });
  173. this.actions.addAction('support...', function()
  174. {
  175. const {shell} = require('electron');
  176. shell.openExternal('https://support.draw.io/display/DFCC/draw.io+for+Confluence+Cloud');
  177. });
  178. editorUi.actions.addAction('userManual...', function()
  179. {
  180. const {shell} = require('electron');
  181. shell.openExternal('https://support.draw.io/display/DO/Draw.io+Online+User+Manual');
  182. });
  183. editorUi.actions.addAction('keyboardShortcuts...', function()
  184. {
  185. const electron = require('electron');
  186. const remote = electron.remote;
  187. const BrowserWindow = remote.BrowserWindow;
  188. keyboardWindow = new BrowserWindow({width: 1200, height: 1000});
  189. // and load the index.html of the app.
  190. keyboardWindow.loadURL(`file://${__dirname}/shortcuts.svg`);
  191. // Emitted when the window is closed.
  192. keyboardWindow.on('closed', function()
  193. {
  194. // Dereference the window object, usually you would store windows
  195. // in an array if your app supports multi windows, this is the time
  196. // when you should delete the corresponding element.
  197. keyboardWindow = null;
  198. });
  199. });
  200. }
  201. // Uses local picker
  202. App.prototype.pickFile = function()
  203. {
  204. this.chooseFileEntry(mxUtils.bind(this, function(fileEntry, data)
  205. {
  206. var file = new LocalFile(this, data, '');
  207. file.fileObject = fileEntry;
  208. this.fileLoaded(file);
  209. }));
  210. };
  211. /**
  212. * Selects a library to load from a picker
  213. *
  214. * @param mode the device mode, ignored in this case
  215. */
  216. App.prototype.pickLibrary = function(mode)
  217. {
  218. this.chooseFileEntry(mxUtils.bind(this, function(fileEntry, data)
  219. {
  220. var library = new LocalLibrary(this, data, fileEntry.name);
  221. library.fileObject = fileEntry;
  222. this.loadLibrary(library);
  223. }));
  224. };
  225. // Uses local picker
  226. App.prototype.chooseFileEntry = function(fn)
  227. {
  228. const electron = require('electron');
  229. var remote = electron.remote;
  230. var dialog = remote.dialog;
  231. var paths = dialog.showOpenDialog({properties: [ 'openFile' ] });
  232. if (paths !== undefined && paths[0] != null)
  233. {
  234. var fs = require('fs');
  235. var path = paths[0];
  236. var index = path.lastIndexOf('.png');
  237. var isPng = index > -1 && index == path.length - 4;
  238. var encoding = isPng ? 'base64' : 'utf-8'
  239. fs.readFile(path, encoding, mxUtils.bind(this, function (e, data)
  240. {
  241. if (e)
  242. {
  243. this.handleError(e);
  244. }
  245. else
  246. {
  247. if (isPng)
  248. {
  249. // Detecting png by extension. Would need https://github.com/mscdex/mmmagic
  250. // to do it by inspection
  251. data = this.extractGraphModelFromPng(data, true);
  252. }
  253. var fileEntry = new Object();
  254. fileEntry.path = path;
  255. fileEntry.name = path.replace(/^.*[\\\/]/, '');
  256. fileEntry.type = encoding;
  257. fn(fileEntry, data);
  258. }
  259. }));
  260. }
  261. };
  262. LocalFile.prototype.isAutosave = function()
  263. {
  264. return this.ui.editor.autosave;
  265. };
  266. LocalFile.prototype.isAutosaveOptional = function()
  267. {
  268. return true;
  269. };
  270. LocalLibrary.prototype.isAutosave = function()
  271. {
  272. return true;
  273. };
  274. LocalFile.prototype.getTitle = function()
  275. {
  276. return (this.fileObject != null) ? this.fileObject.name : null;
  277. };
  278. LocalFile.prototype.isRenamable = function()
  279. {
  280. return false;
  281. };
  282. // Restores default implementation of open with autosave
  283. LocalFile.prototype.open = DrawioFile.prototype.open;
  284. LocalFile.prototype.save = function(revision, success, error)
  285. {
  286. DrawioFile.prototype.save.apply(this, arguments);
  287. this.saveFile(revision, success, error);
  288. };
  289. LocalLibrary.prototype.save = function(revision, success, error)
  290. {
  291. this.saveFile(revision, success, error);
  292. };
  293. LocalFile.prototype.saveFile = function(revision, success, error)
  294. {
  295. var fn = mxUtils.bind(this, function()
  296. {
  297. var doSave = mxUtils.bind(this, function(data)
  298. {
  299. if (!this.savingFile)
  300. {
  301. this.savingFile = true;
  302. // Makes sure no changes get lost while the file is saved
  303. var prevModified = this.isModified;
  304. var modified = this.isModified();
  305. this.setModified(false);
  306. var fs = require('fs');
  307. fs.writeFile(this.fileObject.path, data, this.fileObject.encoding, mxUtils.bind(this, function (e)
  308. {
  309. if (e)
  310. {
  311. this.savingFile = false;
  312. this.isModified = prevModified;
  313. this.setModified(modified || this.isModified());
  314. if (error != null)
  315. {
  316. error();
  317. }
  318. }
  319. else
  320. {
  321. this.savingFile = false;
  322. this.isModified = prevModified;
  323. this.contentChanged();
  324. if (success != null)
  325. {
  326. success();
  327. }
  328. }
  329. }));
  330. }
  331. else
  332. {
  333. // TODO, already saving. Need a better error
  334. error();
  335. }
  336. });
  337. if (!/(\.png)$/i.test(this.fileObject.name))
  338. {
  339. doSave(this.getData());
  340. }
  341. else
  342. {
  343. this.ui.exportToCanvas(mxUtils.bind(this, function(canvas)
  344. {
  345. try
  346. {
  347. var data = canvas.toDataURL('image/png');
  348. data = this.ui.writeGraphModelToPng(data, 'zTXt', 'mxGraphModel',
  349. atob(this.ui.editor.graph.compress(mxUtils.getXml(this.ui.editor.getGraphXml()))));
  350. doSave(data, 'base64');
  351. }
  352. catch (e)
  353. {
  354. if (error != null)
  355. {
  356. error(e);
  357. }
  358. }
  359. }), null, null, null, mxUtils.bind(this, function(e)
  360. {
  361. if (error != null)
  362. {
  363. error(e);
  364. }
  365. }));
  366. }
  367. });
  368. if (this.fileObject == null)
  369. {
  370. const electron = require('electron');
  371. var remote = electron.remote;
  372. var dialog = remote.dialog;
  373. var path = dialog.showSaveDialog();
  374. if (path != null)
  375. {
  376. this.fileObject = new Object();
  377. this.fileObject.path = path;
  378. this.fileObject.name = path.replace(/^.*[\\\/]/, '');
  379. this.fileObject.type = 'utf-8';
  380. fn();
  381. }
  382. }
  383. else
  384. {
  385. fn();
  386. }
  387. };
  388. LocalFile.prototype.saveAs = function(title, success, error)
  389. {
  390. const electron = require('electron');
  391. var remote = electron.remote;
  392. var dialog = remote.dialog;
  393. var path = dialog.showSaveDialog();
  394. if (path != null)
  395. {
  396. this.fileObject = new Object();
  397. this.fileObject.path = path;
  398. this.fileObject.name = path.replace(/^.*[\\\/]/, '');
  399. this.fileObject.type = 'utf-8';
  400. this.save(false, success, error);
  401. }
  402. };
  403. App.prototype.saveFile = function(forceDialog)
  404. {
  405. var file = this.getCurrentFile();
  406. if (file != null)
  407. {
  408. if (!forceDialog && file.getTitle() != null)
  409. {
  410. file.save(true, mxUtils.bind(this, function(resp)
  411. {
  412. this.spinner.stop();
  413. this.editor.setStatus(mxUtils.htmlEntities(mxResources.get('allChangesSaved')));
  414. }), mxUtils.bind(this, function(resp)
  415. {
  416. this.editor.setStatus('');
  417. this.handleError(resp, (resp != null) ? mxResources.get('errorSavingFile') : null);
  418. }));
  419. }
  420. else
  421. {
  422. file.saveAs(null, mxUtils.bind(this, function(resp)
  423. {
  424. this.spinner.stop();
  425. this.editor.setStatus(mxUtils.htmlEntities(mxResources.get('allChangesSaved')));
  426. }), mxUtils.bind(this, function(resp)
  427. {
  428. this.editor.setStatus('');
  429. this.handleError(resp, (resp != null) ? mxResources.get('errorSavingFile') : null);
  430. }));
  431. }
  432. }
  433. };
  434. /**
  435. * Translates this point by the given vector.
  436. *
  437. * @param {number} dx X-coordinate of the translation.
  438. * @param {number} dy Y-coordinate of the translation.
  439. */
  440. App.prototype.saveLibrary = function(name, images, file, mode, noSpin, noReload, fn)
  441. {
  442. mode = (mode != null) ? mode : this.mode;
  443. noSpin = (noSpin != null) ? noSpin : false;
  444. noReload = (noReload != null) ? noReload : false;
  445. var xml = this.createLibraryDataFromImages(images);
  446. var error = mxUtils.bind(this, function(resp)
  447. {
  448. this.spinner.stop();
  449. if (fn != null)
  450. {
  451. fn();
  452. }
  453. // Null means cancel by user and is ignored
  454. if (resp != null)
  455. {
  456. this.handleError(resp, mxResources.get('errorSavingFile'));
  457. }
  458. });
  459. // Handles special case for local libraries
  460. if (file == null)
  461. {
  462. file = new LocalLibrary(this, xml, name);
  463. }
  464. if (noSpin || this.spinner.spin(document.body, mxResources.get('saving')))
  465. {
  466. file.setData(xml);
  467. var doSave = mxUtils.bind(this, function()
  468. {
  469. file.save(true, mxUtils.bind(this, function(resp)
  470. {
  471. this.spinner.stop();
  472. this.hideDialog(true);
  473. if (!noReload)
  474. {
  475. this.libraryLoaded(file, images)
  476. }
  477. if (fn != null)
  478. {
  479. fn();
  480. }
  481. }), error);
  482. });
  483. if (name != file.getTitle())
  484. {
  485. var oldHash = file.getHash();
  486. file.rename(name, mxUtils.bind(this, function(resp)
  487. {
  488. // Change hash in stored settings
  489. if (file.constructor != LocalLibrary && oldHash != file.getHash())
  490. {
  491. mxSettings.removeCustomLibrary(oldHash);
  492. mxSettings.addCustomLibrary(file.getHash());
  493. }
  494. // Workaround for library files changing hash so
  495. // the old library cannot be removed from the
  496. // sidebar using the updated file in libraryLoaded
  497. this.removeLibrarySidebar(oldHash);
  498. doSave();
  499. }), error)
  500. }
  501. else
  502. {
  503. doSave();
  504. }
  505. }
  506. };
  507. EditorUi.prototype.saveData = function(filename, format, data, mimeType, base64Encoded)
  508. {
  509. const electron = require('electron');
  510. var remote = electron.remote;
  511. var dialog = remote.dialog;
  512. var path = dialog.showSaveDialog();
  513. if (path != null)
  514. {
  515. this.fileObject = new Object();
  516. this.fileObject.path = path;
  517. this.fileObject.name = path.replace(/^.*[\\\/]/, '');
  518. var isImage = mimeType != null && mimeType.startsWith('image');
  519. this.fileObject.type = base64Encoded ? 'base64' : 'utf-8';
  520. var fs = require('fs');
  521. fs.writeFile(this.fileObject.path, data, this.fileObject.type, mxUtils.bind(this, function (e)
  522. {
  523. if (e)
  524. {
  525. // TODO
  526. }
  527. }));
  528. }
  529. };
  530. EditorUi.prototype.addBeforeUnloadListener = function() {};
  531. })();