text_editor.js 884 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import * as CodeMirror from "codemirror";
  2. import "codemirror/addon/edit/closebrackets";
  3. import "codemirror/addon/edit/matchbrackets";
  4. import "codemirror/addon/edit/matchbrackets";
  5. import "codemirror/addon/selection/active-line";
  6. let codeEditor = null;
  7. export function initTextEditor (element) {
  8. let id = element;
  9. if (element[0] == '#') {
  10. id = element.substring(1);
  11. }
  12. codeEditor = CodeMirror.fromTextArea(document.getElementById(id), {
  13. value: "",
  14. lineNumbers: true,
  15. matchBrackets: true,
  16. autoCloseBrackets: true,
  17. fixedGutter: true,
  18. styleActiveLine: true
  19. });
  20. }
  21. export function disable (flag) {
  22. codeEditor.setOption({readOnly: flag})
  23. updateEditor();
  24. }
  25. export function updateEditor () {
  26. codeEditor.refresh();
  27. }
  28. export function setCode (code) {
  29. codeEditor.setValue(code);
  30. }
  31. export function getCode () {
  32. return codeEditor.getValue();
  33. }