debian-mirror-gitlab/app/assets/javascripts/blob_edit/edit_blob.js

157 lines
4.8 KiB
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
/* global ace */
2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2018-03-17 18:26:18 +05:30
import axios from '~/lib/utils/axios_utils';
import createFlash from '~/flash';
2020-07-28 23:09:34 +05:30
import { BLOB_EDITOR_ERROR, BLOB_PREVIEW_ERROR } from './constants';
2017-08-17 22:00:37 +05:30
import TemplateSelectorMediator from '../blob/file_template_mediator';
2018-12-13 13:39:08 +05:30
import getModeByFileExtension from '~/lib/utils/ace_utils';
2019-02-15 15:39:39 +05:30
import { addEditorMarkdownListeners } from '~/lib/utils/text_markdown';
2017-08-17 22:00:37 +05:30
2020-07-28 23:09:34 +05:30
const monacoEnabledGlobally = window.gon.features?.monacoBlobs;
2017-08-17 22:00:37 +05:30
export default class EditBlob {
2019-02-15 15:39:39 +05:30
// The options object has:
// assetsPath, filePath, currentAction, projectId, isMarkdown
constructor(options) {
this.options = options;
2020-07-28 23:09:34 +05:30
this.options.monacoEnabled = this.options.monacoEnabled ?? monacoEnabledGlobally;
const { isMarkdown, monacoEnabled } = this.options;
return Promise.resolve()
.then(() => {
return monacoEnabled ? this.configureMonacoEditor() : this.configureAceEditor();
})
.then(() => {
this.initModePanesAndLinks();
this.initFileSelectors();
this.initSoftWrap();
if (isMarkdown) {
addEditorMarkdownListeners(this.editor);
}
this.editor.focus();
})
.catch(() => createFlash(BLOB_EDITOR_ERROR));
}
configureMonacoEditor() {
const EditorPromise = import(
/* webpackChunkName: 'monaco_editor_lite' */ '~/editor/editor_lite'
);
const MarkdownExtensionPromise = this.options.isMarkdown
? import('~/editor/editor_markdown_ext')
: Promise.resolve(false);
return Promise.all([EditorPromise, MarkdownExtensionPromise])
.then(([EditorModule, MarkdownExtension]) => {
const EditorLite = EditorModule.default;
const editorEl = document.getElementById('editor');
const fileNameEl =
document.getElementById('file_path') || document.getElementById('file_name');
const fileContentEl = document.getElementById('file-content');
const form = document.querySelector('.js-edit-blob-form');
this.editor = new EditorLite();
if (MarkdownExtension) {
this.editor.use(MarkdownExtension.default);
}
this.editor.createInstance({
el: editorEl,
blobPath: fileNameEl.value,
blobContent: editorEl.innerText,
});
fileNameEl.addEventListener('change', () => {
this.editor.updateModelLanguage(fileNameEl.value);
});
form.addEventListener('submit', () => {
fileContentEl.value = this.editor.getValue();
});
})
.catch(() => createFlash(BLOB_EDITOR_ERROR));
2017-08-17 22:00:37 +05:30
}
2019-02-15 15:39:39 +05:30
configureAceEditor() {
2020-07-28 23:09:34 +05:30
const { filePath, assetsPath } = this.options;
2017-08-17 22:00:37 +05:30
ace.config.set('modePath', `${assetsPath}/ace`);
ace.config.loadModule('ace/ext/searchbox');
2018-12-13 13:39:08 +05:30
ace.config.loadModule('ace/ext/modelist');
2017-08-17 22:00:37 +05:30
this.editor = ace.edit('editor');
// This prevents warnings re: automatic scrolling being logged
this.editor.$blockScrolling = Infinity;
2018-12-13 13:39:08 +05:30
if (filePath) {
this.editor.getSession().setMode(getModeByFileExtension(filePath));
2017-08-17 22:00:37 +05:30
}
}
2019-02-15 15:39:39 +05:30
initFileSelectors() {
const { currentAction, projectId } = this.options;
2017-08-17 22:00:37 +05:30
this.fileTemplateMediator = new TemplateSelectorMediator({
currentAction,
editor: this.editor,
2018-12-05 23:21:45 +05:30
projectId,
2017-08-17 22:00:37 +05:30
});
}
initModePanesAndLinks() {
this.$editModePanes = $('.js-edit-mode-pane');
this.$editModeLinks = $('.js-edit-mode a');
this.$editModeLinks.on('click', e => this.editModeLinkClickHandler(e));
}
editModeLinkClickHandler(e) {
e.preventDefault();
const currentLink = $(e.target);
const paneId = currentLink.attr('href');
const currentPane = this.$editModePanes.filter(paneId);
this.$editModeLinks.parent().removeClass('active hover');
currentLink.parent().addClass('active hover');
this.$editModePanes.hide();
currentPane.fadeIn(200);
if (paneId === '#preview') {
this.$toggleButton.hide();
2018-12-05 23:21:45 +05:30
axios
.post(currentLink.data('previewUrl'), {
content: this.editor.getValue(),
})
.then(({ data }) => {
currentPane.empty().append(data);
currentPane.renderGFM();
})
2020-07-28 23:09:34 +05:30
.catch(() => createFlash(BLOB_PREVIEW_ERROR));
2016-09-13 17:45:13 +05:30
}
2017-08-17 22:00:37 +05:30
this.$toggleButton.show();
return this.editor.focus();
}
initSoftWrap() {
2020-07-28 23:09:34 +05:30
this.isSoftWrapped = Boolean(this.options.monacoEnabled);
2017-08-17 22:00:37 +05:30
this.$toggleButton = $('.soft-wrap-toggle');
2020-07-28 23:09:34 +05:30
this.$toggleButton.toggleClass('soft-wrap-active', this.isSoftWrapped);
2017-08-17 22:00:37 +05:30
this.$toggleButton.on('click', () => this.toggleSoftWrap());
}
toggleSoftWrap() {
this.isSoftWrapped = !this.isSoftWrapped;
this.$toggleButton.toggleClass('soft-wrap-active', this.isSoftWrapped);
2020-07-28 23:09:34 +05:30
if (this.options.monacoEnabled) {
this.editor.updateOptions({ wordWrap: this.isSoftWrapped ? 'on' : 'off' });
} else {
this.editor.getSession().setUseWrapMode(this.isSoftWrapped);
}
2017-08-17 22:00:37 +05:30
}
}