2018-11-08 19:23:39 +05:30
|
|
|
import { editor as monacoEditor, Uri } from 'monaco-editor';
|
2018-05-09 12:01:36 +05:30
|
|
|
import Disposable from './disposable';
|
|
|
|
import eventHub from '../../eventhub';
|
|
|
|
|
|
|
|
export default class Model {
|
2018-11-08 19:23:39 +05:30
|
|
|
constructor(file, head = null) {
|
2018-05-09 12:01:36 +05:30
|
|
|
this.disposable = new Disposable();
|
|
|
|
this.file = file;
|
2018-10-15 14:42:47 +05:30
|
|
|
this.head = head;
|
2018-11-18 11:00:15 +05:30
|
|
|
this.content = file.content !== '' || file.deleted ? file.content : file.raw;
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
this.disposable.add(
|
2018-11-08 19:23:39 +05:30
|
|
|
(this.originalModel = monacoEditor.createModel(
|
2018-10-15 14:42:47 +05:30
|
|
|
head ? head.content : this.file.raw,
|
2018-05-09 12:01:36 +05:30
|
|
|
undefined,
|
2019-03-02 22:35:43 +05:30
|
|
|
new Uri('gitlab', false, `original/${this.path}`),
|
2018-05-09 12:01:36 +05:30
|
|
|
)),
|
2018-11-08 19:23:39 +05:30
|
|
|
(this.model = monacoEditor.createModel(
|
2018-05-09 12:01:36 +05:30
|
|
|
this.content,
|
|
|
|
undefined,
|
2019-03-02 22:35:43 +05:30
|
|
|
new Uri('gitlab', false, this.path),
|
2018-05-09 12:01:36 +05:30
|
|
|
)),
|
|
|
|
);
|
|
|
|
if (this.file.mrChange) {
|
|
|
|
this.disposable.add(
|
2018-11-08 19:23:39 +05:30
|
|
|
(this.baseModel = monacoEditor.createModel(
|
2018-05-09 12:01:36 +05:30
|
|
|
this.file.baseRaw,
|
|
|
|
undefined,
|
2019-03-02 22:35:43 +05:30
|
|
|
new Uri('gitlab', false, `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
|
|
|
}
|
|
|
|
}
|