repo_editor.js 2.8 KB
Newer Older
1
/* global monaco */
J
Jacob Schatz 已提交
2
import Vue from 'vue';
L
Luke "Jared" Bennett 已提交
3 4
import Store from './repo_store';
import Helper from './repo_helper';
5
import monacoLoader from './monaco_loader';
J
Jacob Schatz 已提交
6 7

export default class RepoEditor {
8
  constructor(el) {
J
Jacob Schatz 已提交
9
    this.initMonaco();
10
    Store.ideEl = el;
J
Jacob Schatz 已提交
11 12
  }

J
Jacob Schatz 已提交
13
  addMonacoEvents() {
14
    this.monacoEditor.onMouseUp(RepoEditor.onMonacoEditorMouseUp);
15
    this.monacoEditor.onKeyUp(this.onMonacoEditorKeysPressed.bind(this));
J
Jacob Schatz 已提交
16 17
  }

18
  onMonacoEditorKeysPressed() {
19
    Store.setActiveFileContents(this.monacoEditor.getValue());
20 21
  }

J
Jacob Schatz 已提交
22
  initMonaco() {
23
    monacoLoader(['vs/editor/editor.main'], () => {
24
      this.monacoEditor = monaco.editor.create(Store.ideEl, {
25 26 27 28
        model: null,
        readOnly: true,
        contextmenu: false,
      });
J
Jacob Schatz 已提交
29

30 31
      Store.monacoInstance = this.monacoEditor;

J
Jacob Schatz 已提交
32
      this.initVue();
J
Jacob Schatz 已提交
33
      this.addMonacoEvents();
J
Jacob Schatz 已提交
34 35 36 37 38 39
    });
  }

  initVue() {
    this.vue = new Vue({
      data: () => Store,
L
Luke "Jared" Bennett 已提交
40
      created() {
J
Jacob Schatz 已提交
41
        this.showHide();
L
Luke "Jared" Bennett 已提交
42 43 44 45 46 47

        if (this.blobRaw === '') return;

        const newModel = monaco.editor.createModel(this.blobRaw, 'plaintext');

        this.monacoInstance.setModel(newModel);
J
Jacob Schatz 已提交
48 49
      },

J
Jacob Schatz 已提交
50 51
      methods: {
        showHide() {
52
          if (!this.openedFiles.length || (this.binary && !this.activeFile.raw)) {
53
            this.ideEl.style.display = 'none';
54
          } else {
55
            this.ideEl.style.display = 'inline-block';
56 57
          }
        },
J
Jacob Schatz 已提交
58 59 60
      },

      watch: {
J
Jacob Schatz 已提交
61
        activeLine() {
62
          this.monacoInstance.setPosition({
J
Jacob Schatz 已提交
63
            lineNumber: this.activeLine,
64
            column: 1,
J
Jacob Schatz 已提交
65 66 67
          });
        },

68
        editMode() {
69 70 71
          const panelClassList = document.querySelector('.panel-right').classList;
          let readOnly = true;

72
          if (this.editMode) {
73
            panelClassList.add('edit-mode');
74
          } else {
75 76
            panelClassList.remove('edit-mode');
            readOnly = true;
77
          }
78

79
          this.monacoInstance.updateOptions({
80 81
            readOnly,
          });
82 83 84 85 86 87
        },

        activeFileLabel() {
          this.showHide();
        },

J
Jacob Schatz 已提交
88 89
        isTree() {
          this.showHide();
90 91
        },

J
Jacob Schatz 已提交
92
        openedFiles() {
J
Jacob Schatz 已提交
93 94 95 96 97
          this.showHide();
        },

        binary() {
          this.showHide();
J
Jacob Schatz 已提交
98 99 100
        },

        blobRaw() {
J
Jacob Schatz 已提交
101 102
          this.showHide();

L
Luke "Jared" Bennett 已提交
103 104 105 106 107 108 109 110 111
          if (this.isTree) return;

          this.monacoInstance.setModel(null);

          const languages = monaco.languages.getLanguages();
          const languageID = Helper.getLanguageIDForFile(this.activeFile, languages);
          const newModel = monaco.editor.createModel(this.blobRaw, languageID);

          this.monacoInstance.setModel(newModel);
L
Luke "Jared" Bennett 已提交
112 113
        },
      },
J
Jacob Schatz 已提交
114 115
    });
  }
116 117 118 119 120 121 122

  static onMonacoEditorMouseUp(e) {
    if (e.target.element.className === 'line-numbers') {
      location.hash = `L${e.target.position.lineNumber}`;
      Store.activeLine = e.target.position.lineNumber;
    }
  }
L
Luke "Jared" Bennett 已提交
123
}