Commit e0527340 authored by Denys Mishunov's avatar Denys Mishunov

Introducing the Diff Editor

Make sure we dispose the instance model

Return either instance or editor from 'use'

Should be useful for chaining

Make sure the model exists before disposing

Introduced the DiffEditor

Abstracted 'createModel' into a static fn

Allow creation of instances without a model

This is useful for the cases when an extension implements
some custom model generation mechanism (for example with
some decorators)

Correct disposal of the DiffEditor models

Prettified the Editor Lite module
parent bc27d459
...@@ -6,7 +6,7 @@ import { registerLanguages } from '~/ide/utils'; ...@@ -6,7 +6,7 @@ import { registerLanguages } from '~/ide/utils';
import { joinPaths } from '~/lib/utils/url_utility'; import { joinPaths } from '~/lib/utils/url_utility';
import { uuids } from '~/diffs/utils/uuids'; import { uuids } from '~/diffs/utils/uuids';
import { clearDomElement } from './utils'; import { clearDomElement } from './utils';
import { EDITOR_LITE_INSTANCE_ERROR_NO_EL, URI_PREFIX, EDITOR_READY_EVENT } from './constants'; import { EDITOR_LITE_INSTANCE_ERROR_NO_EL, URI_PREFIX, EDITOR_READY_EVENT, EDITOR_TYPE_DIFF } from './constants';
export default class EditorLite { export default class EditorLite {
constructor(options = {}) { constructor(options = {}) {
...@@ -102,17 +102,28 @@ export default class EditorLite { ...@@ -102,17 +102,28 @@ export default class EditorLite {
}); });
} }
static createEditorModel({ blobPath, blobContent, blobGlobalId, instance } = {}) { static createEditorModel({
let model = null; blobPath = '',
blobContent = '',
originalBlobContent = null,
blobGlobalId = uuids()[0],
instance = null,
} = {}) {
if (!instance) { if (!instance) {
return null; return null;
} }
const uriFilePath = joinPaths(URI_PREFIX, blobGlobalId, blobPath); const uriFilePath = joinPaths(URI_PREFIX, blobGlobalId, blobPath);
const uri = Uri.file(uriFilePath); const existingModel = monacoEditor.getModel(uriFilePath);
const existingModel = monacoEditor.getModel(uri); const model =
model = existingModel || monacoEditor.createModel(blobContent, undefined, uri); existingModel || monacoEditor.createModel(blobContent, undefined, Uri.file(uriFilePath));
if (originalBlobContent === null) {
instance.setModel(model); instance.setModel(model);
return model; } else {
instance.setModel({
original: monacoEditor.createModel(originalBlobContent, undefined, Uri.file(uriFilePath)),
modified: model,
});
}
} }
/** /**
...@@ -128,33 +139,76 @@ export default class EditorLite { ...@@ -128,33 +139,76 @@ export default class EditorLite {
el = undefined, el = undefined,
blobPath = '', blobPath = '',
blobContent = '', blobContent = '',
originalBlobContent = '',
blobGlobalId = uuids()[0], blobGlobalId = uuids()[0],
extensions = [], extensions = [],
diff = false,
...instanceOptions ...instanceOptions
} = {}) { } = {}) {
EditorLite.prepareInstance(el); EditorLite.prepareInstance(el);
const instance = monacoEditor.create(el, { let instance;
if (!diff) {
instance = monacoEditor.create(el, {
...this.options, ...this.options,
...instanceOptions, ...instanceOptions,
}); });
if (instanceOptions.model !== null) {
EditorLite.createEditorModel({ blobGlobalId, blobPath, blobContent, instance });
}
} else {
instance = monacoEditor.createDiffEditor(el, {
...this.options,
...instanceOptions,
});
if (instanceOptions.model !== null) {
EditorLite.createEditorModel({
blobGlobalId,
originalBlobContent,
blobPath,
blobContent,
instance,
});
}
}
const model = EditorLite.createEditorModel({ blobGlobalId, blobPath, blobContent, instance }); Object.assign(instance, {
updateModelLanguage: (path) => EditorLite.updateModelLanguage(path, instance),
use: (args) => this.use(args, instance),
});
instance.onDidDispose(() => { instance.onDidDispose(() => {
const index = this.instances.findIndex((inst) => inst === instance); const index = this.instances.findIndex((inst) => inst === instance);
this.instances.splice(index, 1); this.instances.splice(index, 1);
model.dispose(); const instanceModel = instance.getModel();
if (instanceModel) {
if (instance.getEditorType() === EDITOR_TYPE_DIFF) {
const { original, modified } = instanceModel;
if (original) {
original.dispose();
}
if (modified) {
modified.dispose();
}
} else {
instanceModel.dispose();
}
}
}); });
instance.updateModelLanguage = (path) => EditorLite.updateModelLanguage(path, instance);
instance.use = (args) => this.use(args, instance);
EditorLite.manageDefaultExtensions(instance, el, extensions); EditorLite.manageDefaultExtensions(instance, el, extensions);
this.instances.push(instance); this.instances.push(instance);
return instance; return instance;
} }
createDiffInstance(args) {
this.createInstance({
...args,
diff: true,
});
}
dispose() { dispose() {
this.instances.forEach((instance) => instance.dispose()); this.instances.forEach((instance) => instance.dispose());
} }
...@@ -168,10 +222,11 @@ export default class EditorLite { ...@@ -168,10 +222,11 @@ export default class EditorLite {
}; };
if (instance) { if (instance) {
initExtensions(instance); initExtensions(instance);
} else { return instance;
}
this.instances.forEach((inst) => { this.instances.forEach((inst) => {
initExtensions(inst); initExtensions(inst);
}); });
} return this;
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment