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

136 lines
3.5 KiB
JavaScript
Raw Normal View History

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';
2020-06-23 00:09:42 +05:30
import { trimTrailingWhitespace, insertFinalNewline } from '../../utils';
import { defaultModelOptions } from '../editor_options';
2018-05-09 12:01:36 +05:30
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;
2020-06-23 00:09:42 +05:30
this.options = { ...defaultModelOptions };
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 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);
}
2020-06-23 00:09:42 +05:30
updateOptions(obj = {}) {
Object.assign(this.options, obj);
this.model.updateOptions(obj);
this.applyCustomOptions();
}
applyCustomOptions() {
this.updateNewContent(
Object.entries(this.options).reduce((content, [key, value]) => {
switch (key) {
case 'endOfLine':
this.model.pushEOL(value);
return this.model.getValue();
case 'insertFinalNewline':
return value ? insertFinalNewline(content) : content;
case 'trimTrailingWhitespace':
return value ? trimTrailingWhitespace(content) : content;
default:
return content;
}
}, this.model.getValue()),
);
}
2018-05-09 12:01:36 +05:30
dispose() {
2020-06-23 00:09:42 +05:30
if (!this.model.isDisposed()) this.applyCustomOptions();
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);
2020-06-23 00:09:42 +05:30
this.disposable.dispose();
2018-05-09 12:01:36 +05:30
}
}