moe-window.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * This file is part of Moeditor.
  3. *
  4. * Copyright (c) 2016 Menci <huanghaorui301@gmail.com>
  5. * Copyright (c) 2016 lucaschimweg
  6. *
  7. * Moeditor is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Moeditor is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Moeditor. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. 'use strict';
  21. const BrowserWindow = require('electron').BrowserWindow,
  22. dialog = require('electron').dialog,
  23. MoeditorAction = require('./moe-action'),
  24. MoeditorFile = require('./moe-file');
  25. class MoeditorWindow {
  26. constructor(path,defName) {
  27. moeApp.hexoWindow = this;
  28. log.info ('new HexoEditor.');
  29. if (MoeditorFile.isDirectory(path)) {
  30. this.directory = path
  31. this.fileName = '';
  32. this.fileContent = this.content = '';
  33. } else {
  34. this.directory = require('path').dirname(path);
  35. this.fileName = path;
  36. this.fileContent = this.content = MoeditorFile.read(path).toString();
  37. }
  38. this.ID = require('moment')().format('YYYYMMDDHHmmssSSS');
  39. this.defName = defName;
  40. this.isSaved = (path !== defName);
  41. this.changed = path === defName;
  42. const debug = (moeApp.flag.debug | moeApp.config.get('debug')) != 0;
  43. var conf = {
  44. icon: Const.path + "/icons/HexoEditor.ico",
  45. autoHideMenuBar: true,
  46. width: 1000 ,
  47. height: 600 ,
  48. webPreferences: {
  49. zoomFactor: moeApp.config.get('scale-factor')
  50. },
  51. show: debug
  52. };
  53. if (process.platform == 'darwin') conf.titleBarStyle = 'hidden-inset';
  54. else conf.frame = false;
  55. this.window = new BrowserWindow(conf);
  56. this.window.hexoeditorWindow = this;
  57. this.registerEvents();
  58. this.window.loadURL('file://' + Const.path + '/views/main/index.html');
  59. if (debug) {
  60. this.window.webContents.openDevTools();
  61. }
  62. }
  63. registerEvents() {
  64. this.window.on('close', (e) => {
  65. if (moeApp.windows.length && this.changed) {
  66. const choice = dialog.showMessageBox(
  67. this.window,
  68. {
  69. type: 'question',
  70. buttons: [__("Yes"), __("No"), __("Cancel")],
  71. title: __("Confirm"),
  72. cancelId: -1,
  73. message: __("Save changes to file?")
  74. }
  75. );
  76. if (choice == 0) {
  77. if (!MoeditorAction.save(this.window))
  78. e.preventDefault();
  79. return;
  80. } else if (choice == 2 || choice == -1) {
  81. e.preventDefault();
  82. return;
  83. } else{
  84. var fs = require('fs');
  85. if (!this.isSaved && fs.existsSync(this.fileName))
  86. fs.unlinkSync(this.fileName);
  87. }
  88. }
  89. if (this.window == moeApp.getShellServer().lastWindow)
  90. process.nextTick(moeApp.getShellServer().kill,false);
  91. const index = moeApp.windows.indexOf(this);
  92. if (index !== -1) moeApp.windows.splice(index, 1);
  93. if ( !moeApp.windows.length) {
  94. process.nextTick(moeApp.getShellServer().kill,false);
  95. setTimeout(app.quit,200)
  96. }
  97. });
  98. }
  99. }
  100. module.exports = MoeditorWindow;