2017-12-19 05:12:32 -05:00
|
|
|
import _ from 'underscore';
|
2017-11-22 08:39:16 -05:00
|
|
|
import DecorationsController from './decorations/controller';
|
2017-11-17 05:46:26 -05:00
|
|
|
import DirtyDiffController from './diff/controller';
|
2017-11-21 09:22:33 -05:00
|
|
|
import Disposable from './common/disposable';
|
|
|
|
import ModelManager from './common/model_manager';
|
2017-11-29 05:26:35 -05:00
|
|
|
import editorOptions from './editor_options';
|
2017-11-21 09:22:33 -05:00
|
|
|
|
|
|
|
export default class Editor {
|
2017-11-22 08:39:16 -05:00
|
|
|
static create(monaco) {
|
|
|
|
this.editorInstance = new Editor(monaco);
|
2017-11-21 09:22:33 -05:00
|
|
|
|
|
|
|
return this.editorInstance;
|
|
|
|
}
|
2017-11-17 05:46:26 -05:00
|
|
|
|
2017-11-22 08:39:16 -05:00
|
|
|
constructor(monaco) {
|
|
|
|
this.monaco = monaco;
|
2017-11-17 05:46:26 -05:00
|
|
|
this.currentModel = null;
|
|
|
|
this.instance = null;
|
|
|
|
this.dirtyDiffController = null;
|
2017-11-21 09:22:33 -05:00
|
|
|
this.disposable = new Disposable();
|
|
|
|
|
2017-11-22 04:08:30 -05:00
|
|
|
this.disposable.add(
|
2017-11-22 08:39:16 -05:00
|
|
|
this.modelManager = new ModelManager(this.monaco),
|
|
|
|
this.decorationsController = new DecorationsController(this),
|
2017-11-22 04:08:30 -05:00
|
|
|
);
|
2017-12-21 10:05:47 -05:00
|
|
|
|
|
|
|
this.debouncedUpdate = _.debounce(() => {
|
|
|
|
this.updateDimensions();
|
|
|
|
}, 200);
|
|
|
|
window.addEventListener('resize', this.debouncedUpdate, false);
|
2017-11-17 05:46:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
createInstance(domElement) {
|
|
|
|
if (!this.instance) {
|
2017-11-22 04:08:30 -05:00
|
|
|
this.disposable.add(
|
2017-11-22 08:39:16 -05:00
|
|
|
this.instance = this.monaco.editor.create(domElement, {
|
2017-11-22 04:08:30 -05:00
|
|
|
model: null,
|
|
|
|
readOnly: false,
|
|
|
|
contextmenu: true,
|
|
|
|
scrollBeyondLastLine: false,
|
2017-12-21 10:05:47 -05:00
|
|
|
minimap: {
|
|
|
|
enabled: false,
|
|
|
|
},
|
2017-11-22 04:08:30 -05:00
|
|
|
}),
|
2017-11-22 08:39:16 -05:00
|
|
|
this.dirtyDiffController = new DirtyDiffController(
|
|
|
|
this.modelManager, this.decorationsController,
|
|
|
|
),
|
2017-11-22 04:08:30 -05:00
|
|
|
);
|
2017-11-17 05:46:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
createModel(file) {
|
2017-11-21 09:22:33 -05:00
|
|
|
return this.modelManager.addModel(file);
|
2017-11-17 05:46:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
attachModel(model) {
|
|
|
|
this.instance.setModel(model.getModel());
|
|
|
|
this.dirtyDiffController.attachModel(model);
|
|
|
|
|
|
|
|
this.currentModel = model;
|
|
|
|
|
2017-11-29 05:26:35 -05:00
|
|
|
this.instance.updateOptions(editorOptions.reduce((acc, obj) => {
|
|
|
|
Object.keys(obj).forEach((key) => {
|
|
|
|
Object.assign(acc, {
|
|
|
|
[key]: obj[key](model),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return acc;
|
|
|
|
}, {}));
|
|
|
|
|
2017-11-17 05:46:26 -05:00
|
|
|
this.dirtyDiffController.reDecorate(model);
|
|
|
|
}
|
|
|
|
|
|
|
|
clearEditor() {
|
|
|
|
if (this.instance) {
|
|
|
|
this.instance.setModel(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dispose() {
|
2017-11-21 09:22:33 -05:00
|
|
|
this.disposable.dispose();
|
2017-12-21 10:05:47 -05:00
|
|
|
window.removeEventListener('resize', this.debouncedUpdate);
|
2017-11-21 09:22:33 -05:00
|
|
|
|
2017-11-17 05:46:26 -05:00
|
|
|
// dispose main monaco instance
|
|
|
|
if (this.instance) {
|
|
|
|
this.instance = null;
|
|
|
|
}
|
|
|
|
}
|
2017-12-21 10:05:47 -05:00
|
|
|
|
|
|
|
updateDimensions() {
|
|
|
|
this.instance.layout();
|
|
|
|
}
|
|
|
|
|
|
|
|
setPosition({ lineNumber, column }) {
|
|
|
|
this.instance.revealPositionInCenter({
|
|
|
|
lineNumber,
|
|
|
|
column,
|
|
|
|
});
|
|
|
|
this.instance.setPosition({
|
|
|
|
lineNumber,
|
|
|
|
column,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onPositionChange(cb) {
|
|
|
|
this.disposable.add(
|
|
|
|
this.instance.onDidChangeCursorPosition(e => cb(this.instance, e)),
|
|
|
|
);
|
|
|
|
}
|
2017-11-17 05:46:26 -05:00
|
|
|
}
|