menu.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. const path = require('path');
  2. const window = require('../../../helper/window');
  3. const file = require('../../../helper/file');
  4. const dialog = require('./dialog');
  5. const menu = {};
  6. menu.new = function() {
  7. window.new(path.join(__dirname, '../../renderer/page/edit/edit.html'), 800, 575);
  8. };
  9. menu.import = function() {
  10. // Select file on user computer
  11. const filter = [
  12. {name: 'JSON', extensions: ['json']},
  13. {name: 'Markdown', extensions: ['md']}
  14. ];
  15. dialog.fileSelector(true, false, filter, (canceled, filepath) => {
  16. // If the user select one file
  17. if (canceled) {
  18. return;
  19. }
  20. // Read informations
  21. const ext = file.getExtension(filepath).toLowerCase();
  22. const content = file.read(filepath);
  23. let data = {};
  24. if (ext === 'md') {
  25. const title = path.basename(filepath);
  26. // Create data from markdown file
  27. data.id = '_' + Math.random().toString(36).substr(2, 9);
  28. data.title = title.substring(0, title.length - 3);
  29. data.content = content;
  30. } else {
  31. data = JSON.parse(content);
  32. // Check if JSON structure is good
  33. if (data.id === undefined || data.title === undefined || data.content === undefined) {
  34. // Error data is invalid
  35. dialog.error('Données invalides', 'Les données dans le fichier JSON sont invalides. Les données ne peuvent pas être importées');
  36. }
  37. }
  38. // Send informations to main window
  39. mainWindow.webContents.send('import-card', data);
  40. });
  41. }
  42. menu.devTool = function (item, focusedWindow) {
  43. if (focusedWindow) {
  44. focusedWindow.toggleDevTools()
  45. }
  46. }
  47. module.exports = menu;