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

const RepoEditor = {
  data: () => Store,

9 10
  template: '<div id="ide"></div>',

L
Luke "Jared" Bennett 已提交
11
  mounted() {
12 13 14 15 16
    const monacoInstance = this.monaco.editor.create(this.$el, {
      model: null,
      readOnly: true,
      contextmenu: false,
    });
L
Luke "Jared" Bennett 已提交
17

18
    Store.monacoInstance = monacoInstance;
L
Luke "Jared" Bennett 已提交
19

20
    this.addMonacoEvents();
L
Luke "Jared" Bennett 已提交
21

22 23
    Helper.getContent().then(() => {
      this.showHide();
L
Luke "Jared" Bennett 已提交
24

25
      if (this.blobRaw === '') return;
L
Luke "Jared" Bennett 已提交
26

27
      const newModel = this.monaco.editor.createModel(this.blobRaw, 'plaintext');
L
Luke "Jared" Bennett 已提交
28

29 30
      this.monacoInstance.setModel(newModel);
    }).catch(Helper.loadingError);
L
Luke "Jared" Bennett 已提交
31 32 33 34 35 36 37 38 39 40 41 42
  },

  methods: {
    showHide() {
      if (!this.openedFiles.length || (this.binary && !this.activeFile.raw)) {
        this.$el.style.display = 'none';
      } else {
        this.$el.style.display = 'inline-block';
      }
    },

    addMonacoEvents() {
L
Luke "Jared" Bennett 已提交
43 44
      this.monacoInstance.onMouseUp(this.onMonacoEditorMouseUp);
      this.monacoInstance.onKeyUp(this.onMonacoEditorKeysPressed.bind(this));
L
Luke "Jared" Bennett 已提交
45 46 47
    },

    onMonacoEditorKeysPressed() {
L
Luke "Jared" Bennett 已提交
48
      Store.setActiveFileContents(this.monacoInstance.getValue());
L
Luke "Jared" Bennett 已提交
49 50 51 52 53 54 55
    },

    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 已提交
56
    },
L
Luke "Jared" Bennett 已提交
57 58 59 60 61 62 63 64 65 66 67
  },

  watch: {
    activeLine() {
      this.monacoInstance.setPosition({
        lineNumber: this.activeLine,
        column: 1,
      });
    },

    editMode() {
68
      const readOnly = !this.editMode;
L
Luke "Jared" Bennett 已提交
69

70
      Store.readOnly = readOnly;
L
Luke "Jared" Bennett 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

      this.monacoInstance.updateOptions({
        readOnly,
      });
    },

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

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

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

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

    blobRaw() {
      this.showHide();

      if (this.isTree) return;

      this.monacoInstance.setModel(null);

100
      const languages = this.monaco.languages.getLanguages();
L
Luke "Jared" Bennett 已提交
101
      const languageID = Helper.getLanguageIDForFile(this.activeFile, languages);
102
      const newModel = this.monaco.editor.createModel(this.blobRaw, languageID);
L
Luke "Jared" Bennett 已提交
103 104 105 106 107 108

      this.monacoInstance.setModel(newModel);
    },
  },
};

109
function repoEditorLoader() {
110 111 112 113 114 115 116 117
  return new Promise((resolve) => {
    monacoLoader(['vs/editor/editor.main'], () => {
      Store.monaco = monaco;

      resolve(RepoEditor);
    });
  });
}
L
Luke "Jared" Bennett 已提交
118

119 120 121 122
export {
  RepoEditor as default,
  repoEditorLoader,
};