debian-mirror-gitlab/app/assets/javascripts/ide/lib/common/model.js

112 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
/* global monaco */
import Disposable from './disposable';
import eventHub from '../../eventhub';
export default class Model {
2018-10-15 14:42:47 +05:30
constructor(monaco, file, head = null) {
2018-05-09 12:01:36 +05:30
this.monaco = monaco;
this.disposable = new Disposable();
this.file = file;
2018-10-15 14:42:47 +05:30
this.head = head;
2018-05-09 12:01:36 +05:30
this.content = file.content !== '' ? file.content : file.raw;
this.disposable.add(
(this.originalModel = this.monaco.editor.createModel(
2018-10-15 14:42:47 +05:30
head ? head.content : this.file.raw,
2018-05-09 12:01:36 +05:30
undefined,
2018-10-15 14:42:47 +05:30
new this.monaco.Uri(null, null, `original/${this.path}`),
2018-05-09 12:01:36 +05:30
)),
(this.model = this.monaco.editor.createModel(
this.content,
undefined,
2018-10-15 14:42:47 +05:30
new this.monaco.Uri(null, null, this.path),
2018-05-09 12:01:36 +05:30
)),
);
if (this.file.mrChange) {
this.disposable.add(
(this.baseModel = this.monaco.editor.createModel(
this.file.baseRaw,
undefined,
2018-10-15 14:42:47 +05:30
new this.monaco.Uri(null, null, `target/${this.path}`),
2018-05-09 12:01:36 +05:30
)),
);
}
2018-10-15 14:42:47 +05:30
this.events = new Set();
2018-05-09 12:01:36 +05:30
this.updateContent = this.updateContent.bind(this);
2018-10-15 14:42:47 +05:30
this.updateNewContent = this.updateNewContent.bind(this);
2018-05-09 12:01:36 +05:30
this.dispose = this.dispose.bind(this);
eventHub.$on(`editor.update.model.dispose.${this.file.key}`, this.dispose);
2018-10-15 14:42:47 +05:30
eventHub.$on(`editor.update.model.content.${this.file.key}`, this.updateContent);
eventHub.$on(`editor.update.model.new.content.${this.file.key}`, this.updateNewContent);
2018-05-09 12:01:36 +05:30
}
get url() {
return this.model.uri.toString();
}
get language() {
return this.model.getModeId();
}
get eol() {
return this.model.getEOL() === '\n' ? 'LF' : 'CRLF';
}
get path() {
return this.file.key;
}
getModel() {
return this.model;
}
getOriginalModel() {
return this.originalModel;
}
getBaseModel() {
return this.baseModel;
}
setValue(value) {
this.getModel().setValue(value);
}
onChange(cb) {
2018-10-15 14:42:47 +05:30
this.events.add(this.disposable.add(this.model.onDidChangeContent(e => cb(this, e))));
}
onDispose(cb) {
this.events.add(cb);
2018-05-09 12:01:36 +05:30
}
2018-10-15 14:42:47 +05:30
updateContent({ content, changed }) {
2018-05-09 12:01:36 +05:30
this.getOriginalModel().setValue(content);
2018-10-15 14:42:47 +05:30
if (!changed) {
this.getModel().setValue(content);
}
}
updateNewContent(content) {
2018-05-09 12:01:36 +05:30
this.getModel().setValue(content);
}
dispose() {
this.disposable.dispose();
2018-10-15 14:42:47 +05:30
this.events.forEach(cb => {
if (typeof cb === 'function') cb();
});
2018-05-09 12:01:36 +05:30
this.events.clear();
eventHub.$off(`editor.update.model.dispose.${this.file.key}`, this.dispose);
2018-10-15 14:42:47 +05:30
eventHub.$off(`editor.update.model.content.${this.file.key}`, this.updateContent);
eventHub.$off(`editor.update.model.new.content.${this.file.key}`, this.updateNewContent);
2018-05-09 12:01:36 +05:30
}
}