You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.4 KiB

  1. let codeEditor = ace.edit("codeblock-editor");
  2. codeEditor.setTheme("ace/theme/tomorrow_night");
  3. codeEditor.getSession().setMode("ace/mode/markdown");
  4. codeEditor.setOption('fontSize', '14px');
  5. codeEditor.setOption('hScrollBarAlwaysVisible', false);
  6. codeEditor.setOption('wrap', true);
  7. let modelist = ace.require("ace/ext/modelist");
  8. // ACE - Mode Loader
  9. let modelistLoaded = [];
  10. let loadAceMode = (m) => {
  11. return $.ajax({
  12. url: '/js/ace/mode-' + m + '.js',
  13. dataType: "script",
  14. cache: true,
  15. beforeSend: () => {
  16. if(_.includes(modelistLoaded, m)) {
  17. return false;
  18. }
  19. },
  20. success: () => {
  21. modelistLoaded.push(m);
  22. }
  23. });
  24. };
  25. // Vue Code Block instance
  26. let vueCodeBlock = new Vue({
  27. el: '#modal-editor-codeblock',
  28. data: {
  29. modes: modelist.modesByName,
  30. modeSelected: 'text'
  31. },
  32. watch: {
  33. modeSelected: (val, oldVal) => {
  34. loadAceMode(val).done(() => {
  35. ace.require("ace/mode/" + val);
  36. codeEditor.getSession().setMode("ace/mode/" + val);
  37. });
  38. }
  39. },
  40. methods: {
  41. cancel: (ev) => {
  42. mdeModalOpenState = false;
  43. $('#modal-editor-codeblock').slideUp();
  44. },
  45. insertCode: (ev) => {
  46. if(mde.codemirror.doc.somethingSelected()) {
  47. mde.codemirror.execCommand('singleSelection');
  48. }
  49. let codeBlockText = '\n```' + vueCodeBlock.modeSelected + '\n' + codeEditor.getValue() + '\n```\n';
  50. mde.codemirror.doc.replaceSelection(codeBlockText);
  51. vueCodeBlock.cancel();
  52. }
  53. }
  54. });