Commit dd9fea06 authored by Andrew Fontaine's avatar Andrew Fontaine

Merge branch '292498/editor-lite-diff-editor' into 'master'

Editor Lite: support creation of a DiffInstance

See merge request gitlab-org/gitlab!51470
parents 7d0cd6bb 327e2b2a
...@@ -13,6 +13,9 @@ export const ERROR_INSTANCE_REQUIRED_FOR_EXTENSION = __( ...@@ -13,6 +13,9 @@ export const ERROR_INSTANCE_REQUIRED_FOR_EXTENSION = __(
export const EDITOR_READY_EVENT = 'editor-ready'; export const EDITOR_READY_EVENT = 'editor-ready';
export const EDITOR_TYPE_CODE = 'vs.editor.ICodeEditor';
export const EDITOR_TYPE_DIFF = 'vs.editor.IDiffEditor';
// //
// EXTENSIONS' CONSTANTS // EXTENSIONS' CONSTANTS
// //
......
...@@ -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 } 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 = {}) {
...@@ -29,15 +34,12 @@ export default class EditorLite { ...@@ -29,15 +34,12 @@ export default class EditorLite {
monacoEditor.setTheme(theme ? themeName : DEFAULT_THEME); monacoEditor.setTheme(theme ? themeName : DEFAULT_THEME);
} }
static updateModelLanguage(path, instance) { static getModelLanguage(path) {
if (!instance) return;
const model = instance.getModel();
const ext = `.${path.split('.').pop()}`; const ext = `.${path.split('.').pop()}`;
const language = monacoLanguages const language = monacoLanguages
.getLanguages() .getLanguages()
.find((lang) => lang.extensions.indexOf(ext) !== -1); .find((lang) => lang.extensions.indexOf(ext) !== -1);
const id = language ? language.id : 'plaintext'; return language ? language.id : 'plaintext';
monacoEditor.setModelLanguage(model, id);
} }
static pushToImportsArray(arr, toImport) { static pushToImportsArray(arr, toImport) {
...@@ -102,17 +104,91 @@ export default class EditorLite { ...@@ -102,17 +104,91 @@ export default class EditorLite {
}); });
} }
static createEditorModel({ blobPath, blobContent, blobGlobalId, instance } = {}) { static createEditorModel({
let model = null; blobPath,
blobContent,
blobOriginalContent,
blobGlobalId,
instance,
isDiff,
} = {}) {
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 uri = Uri.file(uriFilePath);
const existingModel = monacoEditor.getModel(uri); const existingModel = monacoEditor.getModel(uri);
model = existingModel || monacoEditor.createModel(blobContent, undefined, uri); const model = existingModel || monacoEditor.createModel(blobContent, undefined, uri);
instance.setModel(model); if (!isDiff) {
return model; instance.setModel(model);
return model;
}
const diffModel = {
original: monacoEditor.createModel(
blobOriginalContent,
EditorLite.getModelLanguage(model.uri.path),
),
modified: model,
};
instance.setModel(diffModel);
return diffModel;
}
static convertMonacoToELInstance = (inst) => {
const editorLiteInstanceAPI = {
updateModelLanguage: (path) => {
return EditorLite.instanceUpdateLanguage(inst, path);
},
use: (exts = []) => {
return EditorLite.instanceApplyExtension(inst, exts);
},
};
const handler = {
get(target, prop, receiver) {
if (Reflect.has(editorLiteInstanceAPI, prop)) {
return editorLiteInstanceAPI[prop];
}
return Reflect.get(target, prop, receiver);
},
};
return new Proxy(inst, handler);
};
static instanceUpdateLanguage(inst, path) {
const lang = EditorLite.getModelLanguage(path);
const model = inst.getModel();
return monacoEditor.setModelLanguage(model, lang);
}
static instanceApplyExtension(inst, exts = []) {
const extensions = [].concat(exts);
extensions.forEach((extension) => {
EditorLite.mixIntoInstance(extension, inst);
});
return inst;
}
static instanceRemoveFromRegistry(editor, instance) {
const index = editor.instances.findIndex((inst) => inst === instance);
editor.instances.splice(index, 1);
}
static instanceDisposeModels(editor, instance, model) {
const instanceModel = instance.getModel() || model;
if (!instanceModel) {
return;
}
if (instance.getEditorType() === EDITOR_TYPE_DIFF) {
const { original, modified } = instanceModel;
if (original) {
original.dispose();
}
if (modified) {
modified.dispose();
}
} else {
instanceModel.dispose();
}
} }
/** /**
...@@ -128,26 +204,38 @@ export default class EditorLite { ...@@ -128,26 +204,38 @@ export default class EditorLite {
el = undefined, el = undefined,
blobPath = '', blobPath = '',
blobContent = '', blobContent = '',
blobOriginalContent = '',
blobGlobalId = uuids()[0], blobGlobalId = uuids()[0],
extensions = [], extensions = [],
isDiff = false,
...instanceOptions ...instanceOptions
} = {}) { } = {}) {
EditorLite.prepareInstance(el); EditorLite.prepareInstance(el);
const instance = monacoEditor.create(el, { const createEditorFn = isDiff ? 'createDiffEditor' : 'create';
...this.options, const instance = EditorLite.convertMonacoToELInstance(
...instanceOptions, monacoEditor[createEditorFn].call(this, el, {
}); ...this.options,
...instanceOptions,
}),
);
const model = EditorLite.createEditorModel({ blobGlobalId, blobPath, blobContent, instance }); let model;
if (instanceOptions.model !== null) {
model = EditorLite.createEditorModel({
blobGlobalId,
blobOriginalContent,
blobPath,
blobContent,
instance,
isDiff,
});
}
instance.onDidDispose(() => { instance.onDidDispose(() => {
const index = this.instances.findIndex((inst) => inst === instance); EditorLite.instanceRemoveFromRegistry(this, instance);
this.instances.splice(index, 1); EditorLite.instanceDisposeModels(this, instance, model);
model.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);
...@@ -155,23 +243,21 @@ export default class EditorLite { ...@@ -155,23 +243,21 @@ export default class EditorLite {
return instance; return instance;
} }
createDiffInstance(args) {
return this.createInstance({
...args,
isDiff: true,
});
}
dispose() { dispose() {
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]; this.instances.forEach((inst) => {
const initExtensions = (inst) => { inst.use(exts);
extensions.forEach((extension) => { });
EditorLite.mixIntoInstance(extension, inst); return this;
});
};
if (instance) {
initExtensions(instance);
} else {
this.instances.forEach((inst) => {
initExtensions(inst);
});
}
} }
} }
---
title: 'Editor Lite: support for Diff Instance'
merge_request: 51470
author:
type: added
This diff is collapsed.
...@@ -7,6 +7,7 @@ import { ...@@ -7,6 +7,7 @@ import {
import Editor from '~/ide/lib/editor'; import Editor from '~/ide/lib/editor';
import { createStore } from '~/ide/stores'; import { createStore } from '~/ide/stores';
import { defaultEditorOptions } from '~/ide/lib/editor_options'; import { defaultEditorOptions } from '~/ide/lib/editor_options';
import { EDITOR_TYPE_DIFF } from '~/editor/constants';
import { file } from '../helpers'; import { file } from '../helpers';
describe('Multi-file editor library', () => { describe('Multi-file editor library', () => {
...@@ -125,7 +126,7 @@ describe('Multi-file editor library', () => { ...@@ -125,7 +126,7 @@ describe('Multi-file editor library', () => {
}); });
it('sets original & modified when diff editor', () => { it('sets original & modified when diff editor', () => {
jest.spyOn(instance.instance, 'getEditorType').mockReturnValue('vs.editor.IDiffEditor'); jest.spyOn(instance.instance, 'getEditorType').mockReturnValue(EDITOR_TYPE_DIFF);
jest.spyOn(instance.instance, 'setModel').mockImplementation(() => {}); jest.spyOn(instance.instance, 'setModel').mockImplementation(() => {});
instance.attachModel(model); instance.attachModel(model);
......
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