2021-10-27 15:23:28 +05:30
|
|
|
import { editor as monacoEditor, Uri } from 'monaco-editor';
|
2022-01-26 12:08:38 +05:30
|
|
|
import { waitForCSSLoaded } from '~/helpers/startup_css_helper';
|
2020-03-13 15:44:24 +05:30
|
|
|
import { defaultEditorOptions } from '~/ide/lib/editor_options';
|
2021-03-11 19:13:27 +05:30
|
|
|
import languages from '~/ide/lib/languages';
|
2020-05-24 23:13:21 +05:30
|
|
|
import { registerLanguages } from '~/ide/utils';
|
2020-10-24 23:57:45 +05:30
|
|
|
import { joinPaths } from '~/lib/utils/url_utility';
|
2021-06-08 01:23:25 +05:30
|
|
|
import { uuids } from '~/lib/utils/uuids';
|
2021-03-11 19:13:27 +05:30
|
|
|
import {
|
2021-09-30 23:02:18 +05:30
|
|
|
SOURCE_EDITOR_INSTANCE_ERROR_NO_EL,
|
2021-03-11 19:13:27 +05:30
|
|
|
URI_PREFIX,
|
|
|
|
EDITOR_READY_EVENT,
|
|
|
|
EDITOR_TYPE_DIFF,
|
|
|
|
} from './constants';
|
2021-10-27 15:23:28 +05:30
|
|
|
import { clearDomElement, setupEditorTheme, getBlobLanguage } from './utils';
|
2022-01-26 12:08:38 +05:30
|
|
|
import EditorInstance from './source_editor_instance';
|
|
|
|
|
|
|
|
const instanceRemoveFromRegistry = (editor, instance) => {
|
|
|
|
const index = editor.instances.findIndex((inst) => inst === instance);
|
|
|
|
editor.instances.splice(index, 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
const 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();
|
|
|
|
}
|
|
|
|
};
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
export default class SourceEditor {
|
2022-01-26 12:08:38 +05:30
|
|
|
/**
|
|
|
|
* Constructs a global editor.
|
|
|
|
* @param {Object} options - Monaco config options used to create the editor
|
|
|
|
*/
|
2020-03-13 15:44:24 +05:30
|
|
|
constructor(options = {}) {
|
2020-11-24 15:15:51 +05:30
|
|
|
this.instances = [];
|
2022-01-26 12:08:38 +05:30
|
|
|
this.extensionsStore = new Map();
|
2020-03-13 15:44:24 +05:30
|
|
|
this.options = {
|
2021-09-30 23:02:18 +05:30
|
|
|
extraEditorClassName: 'gl-source-editor',
|
2020-03-13 15:44:24 +05:30
|
|
|
...defaultEditorOptions,
|
|
|
|
...options,
|
|
|
|
};
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
setupEditorTheme();
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
registerLanguages(...languages);
|
2020-03-13 15:44:24 +05:30
|
|
|
}
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
static prepareInstance(el) {
|
2020-11-24 15:15:51 +05:30
|
|
|
if (!el) {
|
2021-09-30 23:02:18 +05:30
|
|
|
throw new Error(SOURCE_EDITOR_INSTANCE_ERROR_NO_EL);
|
2020-10-24 23:57:45 +05:30
|
|
|
}
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
clearDomElement(el);
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
monacoEditor.onDidCreateEditor(() => {
|
|
|
|
delete el.dataset.editorLoading;
|
|
|
|
});
|
2021-03-11 19:13:27 +05:30
|
|
|
}
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
static createEditorModel({
|
|
|
|
blobPath,
|
|
|
|
blobContent,
|
|
|
|
blobOriginalContent,
|
|
|
|
blobGlobalId,
|
|
|
|
instance,
|
|
|
|
isDiff,
|
|
|
|
} = {}) {
|
|
|
|
if (!instance) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const uriFilePath = joinPaths(URI_PREFIX, blobGlobalId, blobPath);
|
|
|
|
const uri = Uri.file(uriFilePath);
|
|
|
|
const existingModel = monacoEditor.getModel(uri);
|
|
|
|
const model = existingModel || monacoEditor.createModel(blobContent, undefined, uri);
|
|
|
|
if (!isDiff) {
|
|
|
|
instance.setModel(model);
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
const diffModel = {
|
2021-10-27 15:23:28 +05:30
|
|
|
original: monacoEditor.createModel(blobOriginalContent, getBlobLanguage(model.uri.path)),
|
2021-03-11 19:13:27 +05:30
|
|
|
modified: model,
|
|
|
|
};
|
|
|
|
instance.setModel(diffModel);
|
|
|
|
return diffModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-26 12:08:38 +05:30
|
|
|
* Creates a Source Editor Instance with the given options.
|
|
|
|
* @param {Object} options Options used to initialize the instance.
|
|
|
|
* @param {Element} options.el The element to attach the instance for.
|
2021-03-11 19:13:27 +05:30
|
|
|
* @param {string} options.blobPath The path used as the URI of the model. Monaco uses the extension of this path to determine the language.
|
|
|
|
* @param {string} options.blobContent The content to initialize the monacoEditor.
|
2022-01-26 12:08:38 +05:30
|
|
|
* @param {string} options.blobOriginalContent The original blob's content. Is used when creating a Diff Instance.
|
2021-03-11 19:13:27 +05:30
|
|
|
* @param {string} options.blobGlobalId This is used to help globally identify monaco instances that are created with the same blobPath.
|
2022-01-26 12:08:38 +05:30
|
|
|
* @param {Boolean} options.isDiff Flag to enable creation of a Diff Instance?
|
|
|
|
* @param {...*} options.instanceOptions Configuration options used to instantiate an instance.
|
|
|
|
* @returns {EditorInstance}
|
2021-03-11 19:13:27 +05:30
|
|
|
*/
|
|
|
|
createInstance({
|
|
|
|
el = undefined,
|
|
|
|
blobPath = '',
|
|
|
|
blobContent = '',
|
|
|
|
blobOriginalContent = '',
|
|
|
|
blobGlobalId = uuids()[0],
|
|
|
|
isDiff = false,
|
|
|
|
...instanceOptions
|
|
|
|
} = {}) {
|
2021-09-30 23:02:18 +05:30
|
|
|
SourceEditor.prepareInstance(el);
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
const createEditorFn = isDiff ? 'createDiffEditor' : 'create';
|
2022-01-26 12:08:38 +05:30
|
|
|
const instance = new EditorInstance(
|
2021-03-11 19:13:27 +05:30
|
|
|
monacoEditor[createEditorFn].call(this, el, {
|
|
|
|
...this.options,
|
|
|
|
...instanceOptions,
|
|
|
|
}),
|
2022-01-26 12:08:38 +05:30
|
|
|
this.extensionsStore,
|
2021-03-11 19:13:27 +05:30
|
|
|
);
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
waitForCSSLoaded(() => {
|
|
|
|
instance.layout();
|
|
|
|
});
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
let model;
|
|
|
|
if (instanceOptions.model !== null) {
|
2021-09-30 23:02:18 +05:30
|
|
|
model = SourceEditor.createEditorModel({
|
2021-03-11 19:13:27 +05:30
|
|
|
blobGlobalId,
|
|
|
|
blobOriginalContent,
|
|
|
|
blobPath,
|
|
|
|
blobContent,
|
|
|
|
instance,
|
|
|
|
isDiff,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
instance.onDidDispose(() => {
|
2022-01-26 12:08:38 +05:30
|
|
|
instanceRemoveFromRegistry(this, instance);
|
|
|
|
instanceDisposeModels(this, instance, model);
|
2021-03-11 19:13:27 +05:30
|
|
|
});
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
this.instances.push(instance);
|
2022-03-02 08:16:31 +05:30
|
|
|
el.dispatchEvent(new CustomEvent(EDITOR_READY_EVENT, { detail: { instance } }));
|
2020-11-24 15:15:51 +05:30
|
|
|
return instance;
|
2020-07-28 23:09:34 +05:30
|
|
|
}
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
/**
|
|
|
|
* Create a Diff Instance
|
|
|
|
* @param {Object} args Options to be passed further down to createInstance() with the same signature
|
|
|
|
* @returns {EditorInstance}
|
|
|
|
*/
|
2021-03-11 19:13:27 +05:30
|
|
|
createDiffInstance(args) {
|
|
|
|
return this.createInstance({
|
|
|
|
...args,
|
|
|
|
isDiff: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
/**
|
|
|
|
* Dispose global editor
|
|
|
|
* Automatically disposes all the instances registered for this editor
|
|
|
|
*/
|
2020-11-24 15:15:51 +05:30
|
|
|
dispose() {
|
2021-03-08 18:12:59 +05:30
|
|
|
this.instances.forEach((instance) => instance.dispose());
|
2020-07-28 23:09:34 +05:30
|
|
|
}
|
2020-03-13 15:44:24 +05:30
|
|
|
}
|