2020-04-22 19:07:51 +05:30
|
|
|
import { throttle } from 'lodash';
|
2021-03-11 19:13:27 +05:30
|
|
|
import { Range } from 'monaco-editor';
|
2021-09-30 23:02:18 +05:30
|
|
|
import { DEFAULT_DEBOUNCE_AND_THROTTLE_MS } from '~/lib/utils/constants';
|
2018-05-09 12:01:36 +05:30
|
|
|
import Disposable from '../common/disposable';
|
2021-03-11 19:13:27 +05:30
|
|
|
import DirtyDiffWorker from './diff_worker';
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2021-03-08 18:12:59 +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 '';
|
|
|
|
};
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
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();
|
2021-09-30 23:02:18 +05:30
|
|
|
this.throttledComputeDiff = throttle(this.computeDiff, DEFAULT_DEBOUNCE_AND_THROTTLE_MS);
|
2018-05-09 12:01:36 +05:30
|
|
|
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) {
|
2020-06-23 00:09:42 +05:30
|
|
|
const originalModel = model.getOriginalModel();
|
|
|
|
const newModel = model.getModel();
|
|
|
|
|
|
|
|
if (originalModel.isDisposed() || newModel.isDisposed()) return;
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
this.dirtyDiffWorker.postMessage({
|
|
|
|
path: model.path,
|
2020-06-23 00:09:42 +05:30
|
|
|
originalContent: originalModel.getValue(),
|
|
|
|
newContent: newModel.getValue(),
|
2018-05-09 12:01:36 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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 }) {
|
2021-03-08 18:12:59 +05:30
|
|
|
const decorations = data.changes.map((change) => getDecorator(change));
|
2018-05-09 12:01:36 +05:30
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|