Commit b278d8cb authored by Denys Mishunov's avatar Denys Mishunov

Refactored the Editor Lite a bit

parent ec351d2c
...@@ -6,7 +6,12 @@ import { registerLanguages } from '~/ide/utils'; ...@@ -6,7 +6,12 @@ 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, EDITOR_TYPE_DIFF } 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 = {}) {
...@@ -105,7 +110,7 @@ export default class EditorLite { ...@@ -105,7 +110,7 @@ export default class EditorLite {
static createEditorModel({ static createEditorModel({
blobPath, blobPath,
blobContent, blobContent,
originalBlobContent, blobOriginalContent,
blobGlobalId, blobGlobalId,
instance, instance,
} = {}) { } = {}) {
...@@ -113,20 +118,33 @@ export default class EditorLite { ...@@ -113,20 +118,33 @@ export default class EditorLite {
return null; return null;
} }
const uriFilePath = joinPaths(URI_PREFIX, blobGlobalId, blobPath); const uriFilePath = joinPaths(URI_PREFIX, blobGlobalId, blobPath);
const existingModel = monacoEditor.getModel(uriFilePath); const uri = Uri.file(uriFilePath);
const model = const existingModel = monacoEditor.getModel(uri);
existingModel || monacoEditor.createModel(blobContent, undefined, Uri.file(uriFilePath)); const model = existingModel || monacoEditor.createModel(blobContent, undefined, uri);
if (!originalBlobContent) { if (!blobOriginalContent) {
instance.setModel(model); instance.setModel(model);
} else { } else {
instance.setModel({ instance.setModel({
original: monacoEditor.createModel(originalBlobContent, undefined, Uri.file(uriFilePath)), original: monacoEditor.createModel(blobOriginalContent, undefined, uri),
modified: model, modified: model,
}); });
} }
return instance.getModel(); return instance.getModel();
} }
static decorateInstance = (inst) => {
const decoratedInstance = inst;
decoratedInstance.updateModelLanguage = (path) => EditorLite.updateModelLanguage(path, inst);
decoratedInstance.use = (exts = []) => {
const extensions = Array.isArray(exts) ? exts : [exts];
extensions.forEach((extension) => {
EditorLite.mixIntoInstance(extension, decoratedInstance);
});
return decoratedInstance;
};
return decoratedInstance;
};
/** /**
* Creates a monaco instance with the given options. * Creates a monaco instance with the given options.
* *
...@@ -140,7 +158,7 @@ export default class EditorLite { ...@@ -140,7 +158,7 @@ export default class EditorLite {
el = undefined, el = undefined,
blobPath = '', blobPath = '',
blobContent = '', blobContent = '',
originalBlobContent = '', blobOriginalContent = '',
blobGlobalId = uuids()[0], blobGlobalId = uuids()[0],
extensions = [], extensions = [],
diff = false, diff = false,
...@@ -148,37 +166,24 @@ export default class EditorLite { ...@@ -148,37 +166,24 @@ export default class EditorLite {
} = {}) { } = {}) {
EditorLite.prepareInstance(el); EditorLite.prepareInstance(el);
let instance;
let model; let model;
const createEditorFn = diff ? 'createDiffEditor' : 'create';
if (!diff) { const instance = EditorLite.decorateInstance(
instance = monacoEditor.create(el, { monacoEditor[createEditorFn].call(this, el, {
...this.options,
...instanceOptions,
});
if (instanceOptions.model !== null) {
model = EditorLite.createEditorModel({ blobGlobalId, blobPath, blobContent, instance });
}
} else {
instance = monacoEditor.createDiffEditor(el, {
...this.options, ...this.options,
...instanceOptions, ...instanceOptions,
}); }),
);
if (instanceOptions.model !== null) { if (instanceOptions.model !== null) {
model = EditorLite.createEditorModel({ model = EditorLite.createEditorModel({
blobGlobalId, blobGlobalId,
originalBlobContent, blobOriginalContent,
blobPath, blobPath,
blobContent, blobContent,
instance, 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);
...@@ -200,6 +205,7 @@ export default class EditorLite { ...@@ -200,6 +205,7 @@ export default class EditorLite {
model.dispose(); model.dispose();
} }
}); });
EditorLite.manageDefaultExtensions(instance, el, extensions); EditorLite.manageDefaultExtensions(instance, el, extensions);
this.instances.push(instance); this.instances.push(instance);
...@@ -217,19 +223,9 @@ export default class EditorLite { ...@@ -217,19 +223,9 @@ export default class EditorLite {
this.instances.forEach((instance) => instance.dispose()); this.instances.forEach((instance) => instance.dispose());
} }
use(exts = [], instance = null) { use(exts) {
const extensions = Array.isArray(exts) ? exts : [exts];
const initExtensions = (inst) => {
extensions.forEach((extension) => {
EditorLite.mixIntoInstance(extension, inst);
});
};
if (instance) {
initExtensions(instance);
return instance;
}
this.instances.forEach((inst) => { this.instances.forEach((inst) => {
initExtensions(inst); inst.use(exts);
}); });
return this; return this;
} }
......
...@@ -45,18 +45,24 @@ describe('Base editor', () => { ...@@ -45,18 +45,24 @@ describe('Base editor', () => {
describe('instance of the Editor', () => { describe('instance of the Editor', () => {
let modelSpy; let modelSpy;
let instanceSpy; let instanceSpy;
let use;
let setModel; let setModel;
let getModel;
let dispose; let dispose;
let modelsStorage; let modelsStorage;
beforeEach(() => { beforeEach(() => {
setModel = jest.fn(); setModel = jest.fn();
getModel = jest.fn();
dispose = jest.fn(); dispose = jest.fn();
use = jest.fn();
modelsStorage = new Map(); modelsStorage = new Map();
modelSpy = jest.spyOn(monacoEditor, 'createModel').mockImplementation(() => fakeModel); modelSpy = jest.spyOn(monacoEditor, 'createModel').mockImplementation(() => fakeModel);
instanceSpy = jest.spyOn(monacoEditor, 'create').mockImplementation(() => ({ instanceSpy = jest.spyOn(monacoEditor, 'create').mockImplementation(() => ({
setModel, setModel,
getModel,
dispose, dispose,
use,
onDidDispose: jest.fn(), onDidDispose: jest.fn(),
})); }));
jest.spyOn(monacoEditor, 'getModel').mockImplementation((uri) => { jest.spyOn(monacoEditor, 'getModel').mockImplementation((uri) => {
...@@ -471,9 +477,14 @@ describe('Base editor', () => { ...@@ -471,9 +477,14 @@ describe('Base editor', () => {
const eventSpy = jest.fn().mockImplementation(() => { const eventSpy = jest.fn().mockImplementation(() => {
calls.push('event'); calls.push('event');
}); });
const useSpy = jest.spyOn(editor, 'use').mockImplementation(() => { const useSpy = jest.fn().mockImplementation(() => {
calls.push('use'); calls.push('use');
}); });
jest.spyOn(EditorLite, 'decorateInstance').mockImplementation((inst) => {
const decoratedInstance = inst;
decoratedInstance.use = useSpy;
return decoratedInstance;
});
editorEl.addEventListener(EDITOR_READY_EVENT, eventSpy); editorEl.addEventListener(EDITOR_READY_EVENT, eventSpy);
instance = instanceConstructor('foo, bar'); instance = instanceConstructor('foo, bar');
await waitForPromises(); await waitForPromises();
...@@ -507,12 +518,6 @@ describe('Base editor', () => { ...@@ -507,12 +518,6 @@ describe('Base editor', () => {
expect(inst1.alpha()).toEqual(alphaRes); expect(inst1.alpha()).toEqual(alphaRes);
expect(inst2.alpha()).toEqual(alphaRes); expect(inst2.alpha()).toEqual(alphaRes);
}); });
it('extends specific instance if it has been passed', () => {
editor.use(AlphaExt, inst2);
expect(inst1.alpha).toBeUndefined();
expect(inst2.alpha()).toEqual(alphaRes);
});
}); });
}); });
......
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