moe-action.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 {dialog} = require('electron'),
  22. MoeditorFile = require('./moe-file'),
  23. log = log4js.getLogger('action.js'),
  24. moment = require('moment'),
  25. fs = require('fs'),
  26. path = require('path');
  27. let lastDir = '';
  28. class MoeditorAction {
  29. static openNew() {
  30. let windows = require('electron').BrowserWindow.getAllWindows();
  31. let hexoWindow, i;
  32. for (i = windows.length - 1; i > -1; i--) {
  33. hexoWindow = windows[i];
  34. if (hexoWindow.hexoeditorWindow && hexoWindow.hexoeditorWindow.content.length < 1 &&!hexoWindow.hexoeditorWindow.changed) {
  35. hexoWindow.focus();
  36. break;
  37. }
  38. }
  39. if (i < 0)
  40. moeApp.open();
  41. }
  42. static openNewHexo() {
  43. let notOpened = false;
  44. try {
  45. let hexoDir = moeApp.config.get('hexo-root-dir');
  46. if( hexoDir && fs.existsSync(hexoDir)){
  47. let templateFile = path.resolve(hexoDir, 'scaffolds', 'post.md');
  48. let content = '' +
  49. '---\n' +
  50. 'title: {{ title }}\n' +
  51. 'date: {{ date }}\n' +
  52. 'categories: \n' +
  53. 'tags: \n' +
  54. '---';
  55. let fileDir = path.resolve(hexoDir, 'source', '_posts');
  56. if (fs.statSync(fileDir).isDirectory()) {
  57. let nowDate, fileName, count = 0;
  58. do {
  59. nowDate = moment().format('YYYY-MM-DD HH:mm:ss');
  60. if (count > 0)
  61. nowDate = nowDate + count;
  62. count += 1;
  63. fileName = path.resolve(fileDir, nowDate.replace(/[-: ]/g, '') + '.md');
  64. if (count > 50) {
  65. break;
  66. }
  67. } while (fs.existsSync(fileName));
  68. if (fs.existsSync(templateFile)) {
  69. content = fs.readFileSync(templateFile).toString()
  70. .replace(/title:\s+\{\{[^\}]+\}\}/, 'title: ' + nowDate.replace(/[-: ]/g, ''))
  71. .replace(/date:\s+/, 'date: ' + nowDate)
  72. .replace(/\{\{[^\}]+\}\}/g, '');
  73. }
  74. lastDir = fileDir;
  75. MoeditorFile.write(fileName, content);
  76. if (fs.existsSync(fileName)) {
  77. let hexoWindow = require('electron').BrowserWindow.getFocusedWindow();
  78. if (typeof hexoWindow.hexoeditorWindow == 'undefined' || hexoWindow.hexoeditorWindow.changed || hexoWindow.hexoeditorWindow.content) {
  79. app.addRecentDocument(fileName);
  80. moeApp.open(fileName,fileName);
  81. } else {
  82. hexoWindow.hexoeditorWindow.defName = fileName;
  83. hexoWindow.hexoeditorWindow.fileName = fileName;
  84. hexoWindow.hexoeditorWindow.directory = lastDir;
  85. hexoWindow.hexoeditorWindow.fileContent = hexoWindow.hexoeditorWindow.content = MoeditorFile.read(fileName).toString();
  86. hexoWindow.hexoeditorWindow.changed = false;
  87. hexoWindow.hexoeditorWindow.window.setDocumentEdited(false);
  88. hexoWindow.hexoeditorWindow.window.setRepresentedFilename(hexoWindow.hexoeditorWindow.fileName);
  89. hexoWindow.hexoeditorWindow.window.webContents.send('refresh-editor', {});
  90. app.addRecentDocument(fileName);
  91. }
  92. notOpened = false;
  93. }
  94. }
  95. }
  96. } catch (e) {
  97. log.error(e)
  98. } finally {
  99. if (notOpened)
  100. moeApp.open();
  101. }
  102. }
  103. static open() {
  104. const files = dialog.showOpenDialog(
  105. {
  106. defaultPath: lastDir,
  107. properties: ['openFile'/*, 'multiSelections'*/],
  108. filters: [
  109. {name: __("Markdown Documents"), extensions: ['md', 'mkd', 'markdown']},
  110. {name: __("All Files"), extensions: ['*']}
  111. ]
  112. }
  113. );
  114. if (typeof files == 'undefined') return;
  115. let filename = files[0];
  116. if (filename) {
  117. let windows = require('electron').BrowserWindow.getAllWindows();
  118. let hexoWindow, i;
  119. for (i = windows.length - 1; i > -1; i--) {
  120. hexoWindow = windows[i];
  121. if (hexoWindow.hexoeditorWindow) {
  122. if (hexoWindow.hexoeditorWindow.fileName == filename) {
  123. hexoWindow.focus();
  124. break;
  125. } else if (hexoWindow.hexoeditorWindow.fileName == '' && !hexoWindow.hexoeditorWindow.changed) {
  126. try {
  127. hexoWindow.hexoeditorWindow.fileName = filename;
  128. hexoWindow.hexoeditorWindow.directory = lastDir;
  129. hexoWindow.hexoeditorWindow.fileContent = hexoWindow.hexoeditorWindow.content = MoeditorFile.read(filename).toString();
  130. hexoWindow.hexoeditorWindow.changed = false;
  131. hexoWindow.hexoeditorWindow.window.setDocumentEdited(false);
  132. hexoWindow.hexoeditorWindow.window.setRepresentedFilename(hexoWindow.hexoeditorWindow.fileName);
  133. hexoWindow.hexoeditorWindow.window.webContents.send('refresh-editor', {});
  134. app.addRecentDocument(filename);
  135. hexoWindow.focus();
  136. break;
  137. } catch (e) {
  138. log.error(e);
  139. }
  140. }
  141. }
  142. }
  143. if (i < 0) {
  144. app.addRecentDocument(filename);
  145. moeApp.open(filename);
  146. }
  147. }
  148. }
  149. static save(hexoWindow) {
  150. if (typeof hexoWindow == 'undefined') hexoWindow = require('electron').BrowserWindow.getFocusedWindow();
  151. if (typeof hexoWindow.hexoeditorWindow == 'undefined') return false;
  152. if (typeof hexoWindow.hexoeditorWindow.fileName == 'undefined' || hexoWindow.hexoeditorWindow.fileName == '') {
  153. return MoeditorAction.saveAs(hexoWindow);
  154. } else {
  155. try {
  156. MoeditorFile.write(hexoWindow.hexoeditorWindow.fileName, hexoWindow.hexoeditorWindow.content);
  157. hexoWindow.hexoeditorWindow.isSaved = true;
  158. hexoWindow.hexoeditorWindow.fileContent = hexoWindow.hexoeditorWindow.content;
  159. hexoWindow.hexoeditorWindow.changed = false;
  160. hexoWindow.hexoeditorWindow.window.setDocumentEdited(false);
  161. hexoWindow.hexoeditorWindow.window.setRepresentedFilename(hexoWindow.hexoeditorWindow.fileName);
  162. hexoWindow.hexoeditorWindow.window.webContents.send('pop-message', {
  163. type: 'success',
  164. content: __('Saved successfully.')
  165. });
  166. moeApp.addRecentDocument(hexoWindow.hexoeditorWindow.fileName);
  167. return true;
  168. } catch (e) {
  169. hexoWindow.hexoeditorWindow.window.webContents.send('pop-message', {
  170. type: 'error',
  171. content: __('Can\'t save file') + ', ' + e.toString()
  172. });
  173. log.error('Can\'t save file: ' + e.toString());
  174. return false;
  175. }
  176. }
  177. return false;
  178. }
  179. static saveAs(hexoWindow) {
  180. if (typeof hexoWindow == 'undefined') hexoWindow = require('electron').BrowserWindow.getFocusedWindow();
  181. if (typeof hexoWindow.hexoeditorWindow == 'undefined') return false;
  182. const fileName = dialog.showSaveDialog(hexoWindow,
  183. {
  184. defaultPath: lastDir,
  185. filters: [
  186. {name: __("Markdown Documents"), extensions: ['md', 'mkd', 'markdown']},
  187. {name: __("All Files"), extensions: ['*']}
  188. ]
  189. }
  190. );
  191. if (typeof fileName == 'undefined') return false;
  192. lastDir = path.dirname(fileName);
  193. try {
  194. MoeditorFile.write(fileName, hexoWindow.hexoeditorWindow.content);
  195. hexoWindow.hexoeditorWindow.isSaved = true;
  196. hexoWindow.hexoeditorWindow.directory = lastDir;
  197. hexoWindow.hexoeditorWindow.fileContent = hexoWindow.hexoeditorWindow.content;
  198. hexoWindow.hexoeditorWindow.fileName = fileName;
  199. hexoWindow.hexoeditorWindow.changed = false;
  200. moeApp.addRecentDocument(fileName);
  201. hexoWindow.hexoeditorWindow.window.setDocumentEdited(false);
  202. hexoWindow.hexoeditorWindow.window.setRepresentedFilename(fileName);
  203. hexoWindow.hexoeditorWindow.window.webContents.send('pop-message', {
  204. type: 'success',
  205. content: __('Saved successfully.')
  206. });
  207. hexoWindow.hexoeditorWindow.window.webContents.send('set-title', fileName);
  208. return true;
  209. } catch (e) {
  210. hexoWindow.hexoeditorWindow.window.webContents.send('pop-message', {
  211. type: 'error',
  212. content: __('Can\'t save file') + ', ' + e.toString()
  213. });
  214. log.error('Can\'t save file: ' + e.toString());
  215. return false;
  216. }
  217. }
  218. static exportAsHTML(hexoWindow, f) {
  219. if (typeof hexoWindow == 'undefined') hexoWindow = require('electron').BrowserWindow.getFocusedWindow();
  220. if (typeof hexoWindow.hexoeditorWindow == 'undefined') return;
  221. const fileName = dialog.showSaveDialog(hexoWindow,
  222. {
  223. defaultPath: lastDir,
  224. filters: [
  225. {name: __("HTML Documents"), extensions: ['html', 'htm']},
  226. ]
  227. }
  228. );
  229. if (typeof fileName == 'undefined') return;
  230. lastDir = path.dirname(fileName);
  231. f((s) => {
  232. try {
  233. hexoWindow.hexoeditorWindow.window.webContents.send('pop-message', {
  234. type: 'info',
  235. content: __('Exporting as HTML, please wait ...')
  236. });
  237. MoeditorFile.write(fileName, s);
  238. const {shell} = require('electron');
  239. shell.openItem(fileName);
  240. } catch (e) {
  241. hexoWindow.hexoeditorWindow.window.webContents.send('pop-message', {
  242. type: 'error',
  243. content: __('Can\'t export as HTML') + ', ' + e.toString()
  244. });
  245. log.error('Can\'t export as HTML: ' + e.toString());
  246. }
  247. });
  248. }
  249. static exportAsPDF(hexoWindow, f) {
  250. if (typeof hexoWindow == 'undefined') hexoWindow = require('electron').BrowserWindow.getFocusedWindow();
  251. if (typeof hexoWindow.hexoeditorWindow == 'undefined') return;
  252. const fileName = dialog.showSaveDialog(hexoWindow,
  253. {
  254. defaultPath: lastDir,
  255. filters: [
  256. {name: __("PDF Documents"), extensions: ['pdf']},
  257. ]
  258. }
  259. );
  260. if (typeof fileName == 'undefined') return;
  261. lastDir = path.dirname(fileName);
  262. f((s) => {
  263. let errorHandler = (e) => {
  264. hexoWindow.hexoeditorWindow.window.webContents.send('pop-message', {
  265. type: 'error',
  266. content: __('Can\'t export as PDF') + ', ' + e.toString()
  267. });
  268. log.warn('Can\'t export as PDF: ' + e.toString());
  269. }
  270. try {
  271. hexoWindow.hexoeditorWindow.window.webContents.send('pop-message', {
  272. type: 'info',
  273. content: __('Exporting as PDF, please wait ...')
  274. });
  275. const exportPDF = require('./moe-pdf');
  276. exportPDF({s: s, path: fileName}, errorHandler);
  277. } catch (e) {
  278. errorHandler(e);
  279. }
  280. });
  281. }
  282. }
  283. module.exports = MoeditorAction;