summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/repo/lib/common/model.js
blob: 77d36b4f52c9bf81491027402f1b360806fab685 (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
/* global monaco */

export default class Model {
  constructor(file) {
    this.file = file;
    this.content = file.content !== '' ? file.content : file.raw;
    this.originalModel = monaco.editor.createModel(
      this.content,
      undefined,
      new monaco.Uri(null, null, `original/${this.file.path}`),
    );
    this.model = monaco.editor.createModel(
      this.content,
      undefined,
      new monaco.Uri(null, null, this.file.path),
    );
    this.disposers = new Map();
  }

  get url() {
    return this.model.uri.toString();
  }

  getModel() {
    return this.model;
  }

  getOriginalModel() {
    return this.originalModel;
  }

  onChange(cb) {
    this.disposers.set(
      this.file.path,
      this.model.onDidChangeContent(e => cb(this.model, e)),
    );
  }

  dispose() {
    this.model.dispose();
    this.originalModel.dispose();

    this.disposers.forEach(disposer => disposer.dispose());
    this.disposers.clear();
  }
}