edit.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. const init = require('./init');
  2. const SimpleMDE = require('simplemde');
  3. // URL Parameters
  4. const GET = parseURLParameter();
  5. // When document is ready launch js script on the DOM
  6. $(document).ready(() => {init.exec(loadMarkdownEditor, GET)});
  7. /* --- Functions --- */
  8. function loadMarkdownEditor() {
  9. return new SimpleMDE({
  10. placeholder: 'Écrire ici ...',
  11. spellChecker: false,
  12. toolbar: [
  13. 'bold', 'italic', 'heading', '|',
  14. 'code', 'quote', 'unordered-list', 'ordered-list', '|',
  15. 'link', 'image', 'table', '|',
  16. 'preview', 'guide'
  17. ],
  18. previewRender: (md) => {
  19. setTimeout(() => {PR.prettyPrint();}, 250);
  20. return md.toHTML();
  21. }
  22. });
  23. }
  24. function closeWindow() {
  25. console.log('ici');
  26. ipcRenderer.send('refresh-card');
  27. remote.getCurrentWindow().close();
  28. }
  29. function validCard() {
  30. if ($('#title').val().trim() === '') {
  31. M.toast({
  32. html: 'Le titre est vide',
  33. classes: 'red'
  34. });
  35. } else if (init.mdEditor.value().trim() === '') {
  36. M.toast({
  37. html: 'Le contenu est vide',
  38. classes: 'red'
  39. });
  40. } else {
  41. saveCard();
  42. }
  43. }
  44. function saveCard() {
  45. const title = $('#title').val();
  46. const content = init.mdEditor.value()
  47. // Update
  48. if (GET.id) {
  49. DataService.update(GET.id, title, content);
  50. }
  51. // Create
  52. else {
  53. DataService.add(title, content);
  54. }
  55. // Close window
  56. closeWindow();
  57. }
  58. async function deleteCard() {
  59. // If update card
  60. if (GET.id) {
  61. // Remove card
  62. const result = await DataService.remove(GET.id);
  63. if (result) {
  64. // Close window
  65. closeWindow();
  66. }
  67. }
  68. }