2018-11-08 19:23:39 +05:30
|
|
|
import { Range } from 'monaco-editor';
|
2020-04-22 19:07:51 +05:30
|
|
|
import { throttle } from 'lodash';
|
2018-05-09 12:01:36 +05:30
|
|
|
import DirtyDiffWorker from './diff_worker';
|
|
|
|
import Disposable from '../common/disposable';
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
export const getDiffChangeType = change => {
|
2018-05-09 12:01:36 +05:30
|
|
|
if (change.modified) {
|
|
|
|
return 'modified';
|
|
|
|
} else if (change.added) {
|
|
|
|
return 'added';
|
|
|
|
} else if (change.removed) {
|
|
|
|
return 'removed';
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getDecorator = change => ({
|
2018-11-08 19:23:39 +05:30
|
|
|
range: new Range(change.lineNumber, 1, change.endLineNumber, 1),
|
2018-05-09 12:01:36 +05:30
|
|
|
options: {
|
|
|
|
isWholeLine: true,
|
|
|
|
linesDecorationsClassName: `dirty-diff dirty-diff-${getDiffChangeType(change)}`,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export default class DirtyDiffController {
|
|
|
|
constructor(modelManager, decorationsController) {
|
|
|
|
this.disposable = new Disposable();
|
2018-10-15 14:42:47 +05:30
|
|
|
this.models = new Map();
|
2018-05-09 12:01:36 +05:30
|
|
|
this.editorSimpleWorker = null;
|
|
|
|
this.modelManager = modelManager;
|
|
|
|
this.decorationsController = decorationsController;
|
|
|
|
this.dirtyDiffWorker = new DirtyDiffWorker();
|
|
|
|
this.throttledComputeDiff = throttle(this.computeDiff, 250);
|
|
|
|
this.decorate = this.decorate.bind(this);
|
|
|
|
|
|
|
|
this.dirtyDiffWorker.addEventListener('message', this.decorate);
|
|
|
|
}
|
|
|
|
|
|
|
|
attachModel(model) {
|
2018-10-15 14:42:47 +05:30
|
|
|
if (this.models.has(model.url)) return;
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
model.onChange(() => this.throttledComputeDiff(model));
|
2018-10-15 14:42:47 +05:30
|
|
|
model.onDispose(() => {
|
|
|
|
this.decorationsController.removeDecorations(model);
|
|
|
|
this.models.delete(model.url);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.models.set(model.url, model);
|
2018-05-09 12:01:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
computeDiff(model) {
|
|
|
|
this.dirtyDiffWorker.postMessage({
|
|
|
|
path: model.path,
|
|
|
|
originalContent: model.getOriginalModel().getValue(),
|
|
|
|
newContent: model.getModel().getValue(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
reDecorate(model) {
|
2018-10-15 14:42:47 +05:30
|
|
|
if (this.decorationsController.hasDecorations(model)) {
|
|
|
|
this.decorationsController.decorate(model);
|
|
|
|
} else {
|
|
|
|
this.computeDiff(model);
|
|
|
|
}
|
2018-05-09 12:01:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
decorate({ data }) {
|
|
|
|
const decorations = data.changes.map(change => getDecorator(change));
|
|
|
|
const model = this.modelManager.getModel(data.path);
|
|
|
|
this.decorationsController.addDecorations(model, 'dirtyDiff', decorations);
|
|
|
|
}
|
|
|
|
|
|
|
|
dispose() {
|
|
|
|
this.disposable.dispose();
|
2018-10-15 14:42:47 +05:30
|
|
|
this.models.clear();
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
this.dirtyDiffWorker.removeEventListener('message', this.decorate);
|
|
|
|
this.dirtyDiffWorker.terminate();
|
|
|
|
}
|
|
|
|
}
|