moe-app.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * This file is part of Moeditor.
  3. *
  4. * Copyright (c) 2016 Menci <huanghaorui301@gmail.com>
  5. * Copyright (c) 2015 Thomas Brouard (for codes from Abricotine)
  6. * Copyright (c) 2016 lucaschimweg
  7. *
  8. * Moeditor is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * Moeditor is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with Moeditor. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. 'use strict';
  22. global.log4js = require('log4js');
  23. const MoeditorWindow = require('./moe-window'),
  24. MoeditorAction = require('./moe-action'),
  25. MoeditorFile = require('./moe-file'),
  26. shortcut = require('electron-localshortcut'),
  27. MoeditorLocale = require('./moe-l10n'),
  28. MoeditorAbout = require('./moe-about'),
  29. MoeditorSettings = require('./moe-settings'),
  30. fs = require('fs'),
  31. path = require('path');
  32. let shellServer = false;
  33. let uploadServer = false;
  34. class MoeditorApplication {
  35. constructor() {
  36. this.windows = new Array();
  37. this.hexoWindow = null;
  38. global.appDataPath = path.join(require('os').homedir(),'Documents','HexoEditor');
  39. log4js.configure({
  40. appenders: {
  41. out: {type: 'stdout', level: 'all'},//设置是否在控制台打印日志
  42. debugs: {type: 'file', filename: path.join(appDataPath,'logs','debug.log'), level: 'debug',maxLogSize: 1024*1024*5},
  43. infos: {type: 'file', filename: path.join(appDataPath,'logs','log.log'), level: 'debug',maxLogSize: 1024*1024*5},
  44. errors: { type: 'file', filename: path.join(appDataPath,'logs','error.log') ,maxLogSize: 1024*1024*2},
  45. debug: { type: 'logLevelFilter', appender: 'debugs', level: 'debug' },
  46. info: { type: 'logLevelFilter', appender: 'infos', level: 'info' },
  47. error: { type: 'logLevelFilter', appender: 'errors', level: 'error' }
  48. },
  49. categories: {
  50. default: {appenders: ['out', 'info', 'debug','error'], level: 'debug'}
  51. }
  52. })
  53. global.log = log4js.getLogger('log');
  54. }
  55. open(fileName, defName) {
  56. if (typeof fileName === 'undefined') {
  57. this.windows.push(new MoeditorWindow(process.cwd(), defName));
  58. } else {
  59. this.windows.push(new MoeditorWindow(fileName, defName));
  60. }
  61. }
  62. getShellServer() {
  63. if (!shellServer){
  64. log.info('create shellServer');
  65. shellServer = new (require('./tool/hexo-shell'))();
  66. }
  67. return shellServer;
  68. }
  69. getUploadServer() {
  70. if (!uploadServer) {
  71. log.info('create uploadServer');
  72. uploadServer = new (require('./tool/hexo-uploadServer'))();
  73. }
  74. return uploadServer;
  75. }
  76. run() {
  77. global.Const = require('./moe-const');
  78. app.setName(Const.name);
  79. const Configstore = require('configstore');
  80. this.config = new Configstore(Const.name, require('./moe-config-default'));
  81. this.Const = Const;
  82. this.locale = new MoeditorLocale();
  83. global.__ = str => this.locale.get(str);
  84. this.flag = new Object();
  85. moeApp.appDataPath = appDataPath;
  86. const a = process.argv;
  87. if (a[0].endsWith('electron') && a[1] == '.') a.shift(), a.shift();
  88. else a.shift();
  89. var docs = a.filter((s) => {
  90. if (s == '--debug') moeApp.flag.debug = true;
  91. else if (s == '--about') moeApp.flag.about = true;
  92. else if (s == '--settings') moeApp.flag.settings = true;
  93. try {
  94. return s.substring(0, 2) !== '--' && (MoeditorFile.isTextFile(s) || MoeditorFile.isDirectory(s));
  95. } catch (e) {
  96. return false;
  97. }
  98. });
  99. if (moeApp.flag.about) {
  100. MoeditorAbout();
  101. return;
  102. }
  103. if (moeApp.flag.settings) {
  104. this.listenSettingChanges();
  105. MoeditorSettings();
  106. return;
  107. }
  108. if (typeof this.osxOpenFile === 'string') docs.push(this.osxOpenFile);
  109. if (docs.length == 0) this.open();
  110. else for (var i = 0; i < docs.length; i++) {
  111. docs[i] = path.resolve(docs[i]);
  112. this.addRecentDocument(docs[i]);
  113. this.open(docs[i]);
  114. }
  115. if (process.platform === 'darwin') this.registerAppMenu();
  116. else this.registerShortcuts();
  117. this.listenSettingChanges();
  118. }
  119. registerAppMenu() {
  120. require('./moe-menu')(
  121. {
  122. fileNew: (hexoWindow) => {
  123. MoeditorAction.openNew();
  124. },
  125. fileNewHexo: (hexoWindow) => {
  126. MoeditorAction.openNewHexo();
  127. },
  128. fileOpen: (hexoWindow) => {
  129. MoeditorAction.open();
  130. },
  131. fileSave: (hexoWindow) => {
  132. MoeditorAction.save(hexoWindow);
  133. },
  134. fileSaveAs: (hexoWindow) => {
  135. MoeditorAction.saveAs(hexoWindow);
  136. },
  137. fileExportHTML: (hexoWindow) => {
  138. hexoWindow.webContents.send('action-export-html');
  139. },
  140. fileExportPDF: (hexoWindow) => {
  141. hexoWindow.webContents.send('action-export-pdf');
  142. },
  143. modeToRead: (hexoWindow) => {
  144. hexoWindow.webContents.send('change-edit-mode', 'read');
  145. },
  146. modeToWrite: (hexoWindow) => {
  147. hexoWindow.webContents.send('change-edit-mode', 'write');
  148. },
  149. modeToPreview: (hexoWindow) => {
  150. hexoWindow.webContents.send('change-edit-mode', 'preview');
  151. },
  152. about: (hexoWindow) => {
  153. MoeditorAbout();
  154. },
  155. settings: (hexoWindow) => {
  156. MoeditorSettings();
  157. }
  158. }
  159. );
  160. }
  161. registerShortcuts() {
  162. shortcut.register('CmdOrCtrl + N', () => {
  163. MoeditorAction.openNew();
  164. });
  165. shortcut.register('CmdOrCtrl + H', () => {
  166. MoeditorAction.openNewHexo();
  167. });
  168. shortcut.register('CmdOrCtrl + O', () => {
  169. MoeditorAction.open();
  170. });
  171. shortcut.register('CmdOrCtrl + S', () => {
  172. MoeditorAction.save();
  173. });
  174. shortcut.register('CmdOrCtrl + Shift + S', () => {
  175. MoeditorAction.saveAs();
  176. });
  177. shortcut.register('CmdOrCtrl + R', () => {
  178. let hexoWindow = require('electron').BrowserWindow.getFocusedWindow();
  179. if (hexoWindow) hexoWindow.webContents.send('change-edit-mode', 'read');
  180. });
  181. shortcut.register('CmdOrCtrl + W', () => {
  182. let hexoWindow = require('electron').BrowserWindow.getFocusedWindow();
  183. if (hexoWindow) hexoWindow.webContents.send('change-edit-mode', 'write');
  184. });
  185. shortcut.register('CmdOrCtrl + P', () => {
  186. let hexoWindow = require('electron').BrowserWindow.getFocusedWindow();
  187. if (hexoWindow) hexoWindow.webContents.send('change-edit-mode', 'preview');
  188. });
  189. shortcut.register('Alt + C', () => {
  190. let hexoWindow = require('electron').BrowserWindow.getFocusedWindow();
  191. if (hexoWindow) hexoWindow.webContents.send('change-edit-mode', 'changepreview');
  192. });
  193. shortcut.register('CmdOrCtrl + Alt + Shift + R', () => {
  194. hexoWindow.reload();
  195. });
  196. shortcut.register('CmdOrCtrl + Alt + S', () => {
  197. MoeditorSettings();
  198. });
  199. shortcut.register('CmdOrCtrl + Alt + Shift + F12', () => {
  200. let hexoWindow = require('electron').BrowserWindow.getFocusedWindow();
  201. hexoWindow.webContents.openDevTools();
  202. });
  203. }
  204. listenSettingChanges() {
  205. const ipcMain = require('electron').ipcMain;
  206. ipcMain.on('setting-changed', (e, arg) => {
  207. for (const window of require('electron').BrowserWindow.getAllWindows()) {
  208. window.webContents.send('setting-changed', arg);
  209. }
  210. });
  211. }
  212. addRecentDocument(path) {
  213. app.addRecentDocument(path);
  214. }
  215. getHighlightThemesDir() {
  216. const currTheme = this.config.get('render-theme')
  217. let themedir = 'github'
  218. if (!(currTheme == '*GitHub' || currTheme == '*No Theme')) {
  219. if (currTheme.startsWith('*'))
  220. themedir = currTheme.slice(1).toLowerCase();
  221. else
  222. themedir = currTheme.toLowerCase();
  223. }
  224. themedir = path.join(moeApp.Const.path + '/views/highlightThemes/', themedir);
  225. return (fs.existsSync(themedir) ? themedir : '');
  226. }
  227. getHexo() {
  228. return this.hexo;
  229. }
  230. setHexo(hexo) {
  231. this.hexo = hexo;
  232. }
  233. getLog4js(){
  234. return log4js;
  235. }
  236. }
  237. MoeditorApplication.count = 0;
  238. module.exports = MoeditorApplication;