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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

171 lines
5.4 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2023-03-17 16:20:25 +05:30
import { renderGFM } from '~/behaviors/markdown/render_gfm';
2022-01-26 12:08:38 +05:30
import { SourceEditorExtension } from '~/editor/extensions/source_editor_extension_base';
2021-09-30 23:02:18 +05:30
import { FileTemplateExtension } from '~/editor/extensions/source_editor_file_template_ext';
2022-07-16 23:28:13 +05:30
import { ToolbarExtension } from '~/editor/extensions/source_editor_toolbar_ext';
2021-09-30 23:02:18 +05:30
import SourceEditor from '~/editor/source_editor';
2023-05-27 22:25:52 +05:30
import { createAlert } from '~/alert';
2021-03-11 19:13:27 +05:30
import axios from '~/lib/utils/axios_utils';
import { addEditorMarkdownListeners } from '~/lib/utils/text_markdown';
2021-02-22 17:27:13 +05:30
import { insertFinalNewline } from '~/lib/utils/text_utility';
2021-03-11 19:13:27 +05:30
import TemplateSelectorMediator from '../blob/file_template_mediator';
import { BLOB_EDITOR_ERROR, BLOB_PREVIEW_ERROR } from './constants';
2020-07-28 23:09:34 +05:30
2017-08-17 22:00:37 +05:30
export default class EditBlob {
2019-02-15 15:39:39 +05:30
// The options object has:
2021-10-27 15:23:28 +05:30
// assetsPath, filePath, currentAction, projectId, isMarkdown, previewMarkdownPath
2019-02-15 15:39:39 +05:30
constructor(options) {
this.options = options;
2021-01-03 14:25:43 +05:30
this.configureMonacoEditor();
2023-01-13 00:05:48 +05:30
this.isMarkdown = this.options.isMarkdown;
this.markdownLivePreviewOpened = false;
2021-01-03 14:25:43 +05:30
2023-01-13 00:05:48 +05:30
if (this.isMarkdown) {
2021-10-27 15:23:28 +05:30
this.fetchMarkdownExtension();
2021-01-03 14:25:43 +05:30
}
this.initModePanesAndLinks();
this.initFileSelectors();
this.initSoftWrap();
this.editor.focus();
2020-07-28 23:09:34 +05:30
}
2022-01-26 12:08:38 +05:30
async fetchMarkdownExtension() {
try {
const [
{ EditorMarkdownExtension: MarkdownExtension },
{ EditorMarkdownPreviewExtension: MarkdownLivePreview },
] = await Promise.all([
import('~/editor/extensions/source_editor_markdown_ext'),
import('~/editor/extensions/source_editor_markdown_livepreview_ext'),
]);
2022-07-16 23:28:13 +05:30
this.markdownExtensions = this.editor.use([
2022-01-26 12:08:38 +05:30
{ definition: MarkdownExtension },
{
definition: MarkdownLivePreview,
setupOptions: { previewMarkdownPath: this.options.previewMarkdownPath },
},
]);
} catch (e) {
2022-11-25 23:54:43 +05:30
createAlert({
2022-01-26 12:08:38 +05:30
message: `${BLOB_EDITOR_ERROR}: ${e}`,
});
}
addEditorMarkdownListeners(this.editor);
2021-10-27 15:23:28 +05:30
}
2020-07-28 23:09:34 +05:30
configureMonacoEditor() {
2021-01-03 14:25:43 +05:30
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');
2017-08-17 22:00:37 +05:30
2021-09-30 23:02:18 +05:30
const rootEditor = new SourceEditor();
2017-08-17 22:00:37 +05:30
2021-01-03 14:25:43 +05:30
this.editor = rootEditor.createInstance({
el: editorEl,
blobPath: fileNameEl.value,
blobContent: editorEl.innerText,
});
2022-07-16 23:28:13 +05:30
this.editor.use([
2023-05-27 22:25:52 +05:30
{ definition: ToolbarExtension },
2022-07-16 23:28:13 +05:30
{ definition: SourceEditorExtension },
{ definition: FileTemplateExtension },
]);
2017-08-17 22:00:37 +05:30
2021-01-03 14:25:43 +05:30
fileNameEl.addEventListener('change', () => {
this.editor.updateModelLanguage(fileNameEl.value);
});
2017-08-17 22:00:37 +05:30
2021-01-03 14:25:43 +05:30
form.addEventListener('submit', () => {
2021-02-22 17:27:13 +05:30
fileContentEl.value = insertFinalNewline(this.editor.getValue());
2021-01-03 14:25:43 +05:30
});
2022-07-16 23:28:13 +05:30
// onDidChangeModelLanguage is part of the native Monaco API
// https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.IStandaloneCodeEditor.html#onDidChangeModelLanguage
this.editor.onDidChangeModelLanguage(({ newLanguage = '', oldLanguage = '' }) => {
if (newLanguage === 'markdown') {
this.fetchMarkdownExtension();
} else if (oldLanguage === 'markdown') {
this.editor.unuse(this.markdownExtensions);
}
});
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');
2021-03-08 18:12:59 +05:30
this.$editModeLinks.on('click', (e) => this.editModeLinkClickHandler(e));
2017-08-17 22:00:37 +05:30
}
2023-01-13 00:05:48 +05:30
toggleMarkdownPreview(toOpen) {
if (toOpen !== this.markdownLivePreviewOpened) {
this.editor.markdownPreview?.eventEmitter.fire();
this.markdownLivePreviewOpened = !this.markdownLivePreviewOpened;
}
}
2017-08-17 22:00:37 +05:30
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');
2023-01-13 00:05:48 +05:30
if (this.isMarkdown) {
this.toggleMarkdownPreview(paneId === '#preview');
} else {
this.$editModePanes.hide();
currentPane.show();
if (paneId === '#preview') {
this.$toggleButton.hide();
axios
.post(currentLink.data('previewUrl'), {
content: this.editor.getValue(),
})
.then(({ data }) => {
currentPane.empty().append(data);
2023-03-17 16:20:25 +05:30
renderGFM(currentPane.get(0));
2023-01-13 00:05:48 +05:30
})
.catch(() =>
createAlert({
message: 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() {
2021-01-03 14:25:43 +05:30
this.isSoftWrapped = true;
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);
2021-01-03 14:25:43 +05:30
this.editor.updateOptions({ wordWrap: this.isSoftWrapped ? 'on' : 'off' });
2017-08-17 22:00:37 +05:30
}
}