diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-01-25 12:09:10 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-01-25 12:09:10 +0300 |
commit | b9c00e60c46522bdafd67c4680cf8bfd18269959 (patch) | |
tree | 7d417c7ab1482fe81f1436f30dba280b05182df7 /app/assets/javascripts/editor | |
parent | 7f15f475306e60325cc9e86977f38680987e5c38 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/assets/javascripts/editor')
-rw-r--r-- | app/assets/javascripts/editor/constants.js | 2 | ||||
-rw-r--r-- | app/assets/javascripts/editor/editor_lite.js | 77 |
2 files changed, 50 insertions, 29 deletions
diff --git a/app/assets/javascripts/editor/constants.js b/app/assets/javascripts/editor/constants.js index 8d1a3d17c6e..9981c7f2ca2 100644 --- a/app/assets/javascripts/editor/constants.js +++ b/app/assets/javascripts/editor/constants.js @@ -11,6 +11,8 @@ export const ERROR_INSTANCE_REQUIRED_FOR_EXTENSION = __( 'Editor Lite instance is required to set up an extension.', ); +export const EDITOR_READY_EVENT = 'editor-ready'; + // // EXTENSIONS' CONSTANTS // diff --git a/app/assets/javascripts/editor/editor_lite.js b/app/assets/javascripts/editor/editor_lite.js index 1808f968b8c..a68b44f8204 100644 --- a/app/assets/javascripts/editor/editor_lite.js +++ b/app/assets/javascripts/editor/editor_lite.js @@ -5,7 +5,7 @@ import { defaultEditorOptions } from '~/ide/lib/editor_options'; import { registerLanguages } from '~/ide/utils'; import { joinPaths } from '~/lib/utils/url_utility'; import { clearDomElement } from './utils'; -import { EDITOR_LITE_INSTANCE_ERROR_NO_EL, URI_PREFIX } from './constants'; +import { EDITOR_LITE_INSTANCE_ERROR_NO_EL, URI_PREFIX, EDITOR_READY_EVENT } from './constants'; import { uuids } from '~/diffs/utils/uuids'; export default class EditorLite { @@ -73,6 +73,48 @@ export default class EditorLite { }); } + static prepareInstance(el) { + if (!el) { + throw new Error(EDITOR_LITE_INSTANCE_ERROR_NO_EL); + } + + clearDomElement(el); + + monacoEditor.onDidCreateEditor(() => { + delete el.dataset.editorLoading; + }); + } + + static manageDefaultExtensions(instance, el, extensions) { + EditorLite.loadExtensions(extensions, instance) + .then((modules) => { + if (modules) { + modules.forEach((module) => { + instance.use(module.default); + }); + } + }) + .then(() => { + el.dispatchEvent(new Event(EDITOR_READY_EVENT)); + }) + .catch((e) => { + throw e; + }); + } + + static createEditorModel({ blobPath, blobContent, blobGlobalId, instance } = {}) { + let model = null; + if (!instance) { + return null; + } + const uriFilePath = joinPaths(URI_PREFIX, blobGlobalId, blobPath); + const uri = Uri.file(uriFilePath); + const existingModel = monacoEditor.getModel(uri); + model = existingModel || monacoEditor.createModel(blobContent, undefined, uri); + instance.setModel(model); + return model; + } + /** * Creates a monaco instance with the given options. * @@ -90,25 +132,15 @@ export default class EditorLite { extensions = [], ...instanceOptions } = {}) { - if (!el) { - throw new Error(EDITOR_LITE_INSTANCE_ERROR_NO_EL); - } - - clearDomElement(el); - - const uriFilePath = joinPaths(URI_PREFIX, blobGlobalId, blobPath); - - const model = monacoEditor.createModel(blobContent, undefined, Uri.file(uriFilePath)); - - monacoEditor.onDidCreateEditor(() => { - delete el.dataset.editorLoading; - }); + EditorLite.prepareInstance(el); const instance = monacoEditor.create(el, { ...this.options, ...instanceOptions, }); - instance.setModel(model); + + const model = EditorLite.createEditorModel({ blobGlobalId, blobPath, blobContent, instance }); + instance.onDidDispose(() => { const index = this.instances.findIndex((inst) => inst === instance); this.instances.splice(index, 1); @@ -117,20 +149,7 @@ export default class EditorLite { instance.updateModelLanguage = (path) => EditorLite.updateModelLanguage(path, instance); instance.use = (args) => this.use(args, instance); - EditorLite.loadExtensions(extensions, instance) - .then((modules) => { - if (modules) { - modules.forEach((module) => { - instance.use(module.default); - }); - } - }) - .then(() => { - el.dispatchEvent(new Event('editor-ready')); - }) - .catch((e) => { - throw e; - }); + EditorLite.manageDefaultExtensions(instance, el, extensions); this.instances.push(instance); return instance; |