Commit e45bffc1 authored by Denys Mishunov's avatar Denys Mishunov

Making Editor Lite extensible

By making the Editor Lite extensible with extensions, we make sure
the Editor is always kept lean and is extended with the necessary
bits only when needed
parent 8a2c838c
......@@ -88,4 +88,9 @@ export default class Editor {
updateOptions(options = {}) {
this.instance.updateOptions(options);
}
use(exts = []) {
const extensions = Array.isArray(exts) ? exts : [exts];
Object.assign(this, ...extensions);
}
}
---
title: Support extensibility for Editor Lite
merge_request: 35008
author:
type: added
......@@ -115,6 +115,76 @@ describe('Base editor', () => {
});
});
describe('extensions', () => {
const foo1 = jest.fn();
const foo2 = jest.fn();
const bar = jest.fn();
const MyExt1 = {
foo: foo1,
};
const MyExt2 = {
bar,
};
const MyExt3 = {
foo: foo2,
};
beforeEach(() => {
editor.createInstance({ el: editorEl, blobPath, blobContent });
});
afterEach(() => {
editor.model.dispose();
});
it('is extensible with the extensions', () => {
expect(editor.foo).toBeUndefined();
editor.use(MyExt1);
expect(editor.foo).toEqual(foo1);
});
it('does not fail if no extensions supplied', () => {
const spy = jest.spyOn(global.console, 'error');
editor.use();
expect(spy).not.toHaveBeenCalled();
});
it('is extensible with multiple extensions', () => {
expect(editor.foo).toBeUndefined();
expect(editor.bar).toBeUndefined();
editor.use([MyExt1, MyExt2]);
expect(editor.foo).toEqual(foo1);
expect(editor.bar).toEqual(bar);
});
it('uses the last definition of a method in case of an overlap', () => {
editor.use([MyExt1, MyExt2, MyExt3]);
expect(editor).toEqual(
expect.objectContaining({
foo: foo2,
bar,
}),
);
});
it('correctly resolves references withing extensions', () => {
const FunctionExt = {
inst() {
return this.instance;
},
mod() {
return this.model;
},
};
editor.use(FunctionExt);
expect(editor.inst()).toEqual(editor.instance);
expect(editor.mod()).toEqual(editor.model);
});
});
describe('languages', () => {
it('registers custom languages defined with Monaco', () => {
expect(monacoLanguages.getLanguages()).toEqual(
......
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