Welcome to mirror list, hosted at ThFree Co, Russian Federation.

editor_lite.js « editor « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8711f6e65af13fad03cb2e1f016ba6a540f3e34f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { editor as monacoEditor, languages as monacoLanguages, Uri } from 'monaco-editor';
import whiteTheme from '~/ide/lib/themes/white';
import { defaultEditorOptions } from '~/ide/lib/editor_options';
import { clearDomElement } from './utils';

export default class Editor {
  constructor(options = {}) {
    this.editorEl = null;
    this.blobContent = '';
    this.blobPath = '';
    this.instance = null;
    this.model = null;
    this.options = {
      ...defaultEditorOptions,
      ...options,
    };

    Editor.setupMonacoTheme();
  }

  static setupMonacoTheme() {
    monacoEditor.defineTheme('white', whiteTheme);
    monacoEditor.setTheme('white');
  }

  createInstance({ el = undefined, blobPath = '', blobContent = '' } = {}) {
    if (!el) return;
    this.editorEl = el;
    this.blobContent = blobContent;
    this.blobPath = blobPath;

    clearDomElement(this.editorEl);

    this.model = monacoEditor.createModel(
      this.blobContent,
      undefined,
      new Uri('gitlab', false, this.blobPath),
    );

    monacoEditor.onDidCreateEditor(this.renderEditor.bind(this));

    this.instance = monacoEditor.create(this.editorEl, this.options);
    this.instance.setModel(this.model);
  }

  dispose() {
    return this.instance && this.instance.dispose();
  }

  renderEditor() {
    delete this.editorEl.dataset.editorLoading;
  }

  updateModelLanguage(path) {
    if (path === this.blobPath) return;
    this.blobPath = path;
    const ext = `.${path.split('.').pop()}`;
    const language = monacoLanguages
      .getLanguages()
      .find(lang => lang.extensions.indexOf(ext) !== -1);
    const id = language ? language.id : 'plaintext';
    monacoEditor.setModelLanguage(this.model, id);
  }

  getValue() {
    return this.model.getValue();
  }
}