repo_editor.vue 2.7 KB
Newer Older
1
<script>
L
Luke "Jared" Bennett 已提交
2
/* global monaco */
3
import Store from '../stores/repo_store';
4
import Service from '../services/repo_service';
5
import Helper from '../helpers/repo_helper';
L
Luke "Jared" Bennett 已提交
6 7 8 9

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

J
Jacob Schatz 已提交
10
  destroyed() {
J
Jacob Schatz 已提交
11 12 13
    if (Helper.monacoInstance) {
      Helper.monacoInstance.destroy();
    }
J
Jacob Schatz 已提交
14 15
  },

L
Luke "Jared" Bennett 已提交
16
  mounted() {
17
    Service.getRaw(this.activeFile.raw_path)
J
Jacob Schatz 已提交
18 19 20
      .then((rawResponse) => {
        Store.blobRaw = rawResponse.data;
        Store.activeFile.plain = rawResponse.data;
21

J
Jacob Schatz 已提交
22 23 24 25 26
        const monacoInstance = Helper.monaco.editor.create(this.$el, {
          model: null,
          readOnly: false,
          contextmenu: false,
        });
L
Luke "Jared" Bennett 已提交
27

J
Jacob Schatz 已提交
28
        Helper.monacoInstance = monacoInstance;
L
Luke "Jared" Bennett 已提交
29

J
Jacob Schatz 已提交
30
        this.addMonacoEvents();
L
Luke "Jared" Bennett 已提交
31

J
Jacob Schatz 已提交
32 33 34
        this.setupEditor();
      })
      .catch(Helper.loadingError);
L
Luke "Jared" Bennett 已提交
35 36 37
  },

  methods: {
J
Jacob Schatz 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51
    setupEditor() {
      this.showHide();

      Helper.setMonacoModelFromLanguage();
    },

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

L
Luke "Jared" Bennett 已提交
52
    addMonacoEvents() {
J
Jacob Schatz 已提交
53 54
      Helper.monacoInstance.onMouseUp(this.onMonacoEditorMouseUp);
      Helper.monacoInstance.onKeyUp(this.onMonacoEditorKeysPressed.bind(this));
L
Luke "Jared" Bennett 已提交
55 56 57
    },

    onMonacoEditorKeysPressed() {
J
Jacob Schatz 已提交
58
      Store.setActiveFileContents(Helper.monacoInstance.getValue());
L
Luke "Jared" Bennett 已提交
59 60 61
    },

    onMonacoEditorMouseUp(e) {
J
Jacob Schatz 已提交
62
      if (!e.target.position) return;
J
Jacob Schatz 已提交
63
      const lineNumber = e.target.position.lineNumber;
J
Jacob Schatz 已提交
64
      if (e.target.element.classList.contains('line-numbers')) {
J
Jacob Schatz 已提交
65 66
        location.hash = `L${lineNumber}`;
        Store.activeLine = lineNumber;
J
Jacob Schatz 已提交
67 68 69 70 71

        Helper.monacoInstance.setPosition({
          lineNumber: this.activeLine,
          column: 1,
        });
L
Luke "Jared" Bennett 已提交
72
      }
L
Luke "Jared" Bennett 已提交
73
    },
L
Luke "Jared" Bennett 已提交
74 75 76
  },

  watch: {
77 78
    dialog: {
      handler(obj) {
L
Luke "Jared" Bennett 已提交
79
        const newObj = obj;
J
Jacob Schatz 已提交
80 81
        if (newObj.status) {
          newObj.status = false;
J
Jacob Schatz 已提交
82
          this.openedFiles = this.openedFiles.map((file) => {
L
Luke "Jared" Bennett 已提交
83
            const f = file;
L
Luke "Jared" Bennett 已提交
84
            if (f.active) {
85 86 87 88
              this.blobRaw = f.plain;
            }
            f.changed = false;
            delete f.newContent;
L
Luke "Jared" Bennett 已提交
89 90

            return f;
91 92
          });
          this.editMode = false;
J
Jacob Schatz 已提交
93
          Store.toggleBlobView();
94 95 96 97 98
        }
      },
      deep: true,
    },

L
Luke "Jared" Bennett 已提交
99
    blobRaw() {
J
Jacob Schatz 已提交
100 101 102
      if (Helper.monacoInstance && !this.isTree) {
        this.setupEditor();
      }
L
Luke "Jared" Bennett 已提交
103 104
    },
  },
B
Bryce Johnson 已提交
105 106 107 108 109
  computed: {
    shouldHideEditor() {
      return !this.openedFiles.length || (this.binary && !this.activeFile.raw);
    },
  },
L
Luke "Jared" Bennett 已提交
110 111
};

112 113
export default RepoEditor;
</script>
L
Luke "Jared" Bennett 已提交
114

115
<template>
B
Bryce Johnson 已提交
116
<div id="ide" v-if='!shouldHideEditor'></div>
117
</template>