2022-01-28 14:00:11 -07:00
|
|
|
import $ from 'jquery';
|
2021-11-17 11:08:25 -07:00
|
|
|
import {htmlEscape} from 'escape-goat';
|
2024-07-07 09:32:30 -06:00
|
|
|
import {createCodeEditor} from './codeeditor.ts';
|
|
|
|
import {hideElem, queryElems, showElem} from '../utils/dom.ts';
|
|
|
|
import {initMarkupContent} from '../markup/content.ts';
|
|
|
|
import {attachRefIssueContextPopup} from './contextpopup.ts';
|
|
|
|
import {POST} from '../modules/fetch.ts';
|
|
|
|
import {initDropzone} from './dropzone.ts';
|
2021-10-16 11:28:04 -06:00
|
|
|
|
|
|
|
function initEditPreviewTab($form) {
|
2024-04-29 14:53:15 -06:00
|
|
|
const $tabMenu = $form.find('.repo-editor-menu');
|
2021-10-16 11:28:04 -06:00
|
|
|
$tabMenu.find('.item').tab();
|
2024-04-29 14:53:15 -06:00
|
|
|
const $previewTab = $tabMenu.find('a[data-tab="preview"]');
|
2021-10-16 11:28:04 -06:00
|
|
|
if ($previewTab.length) {
|
2024-03-07 00:28:33 -07:00
|
|
|
$previewTab.on('click', async function () {
|
2021-10-16 11:28:04 -06:00
|
|
|
const $this = $(this);
|
|
|
|
let context = `${$this.data('context')}/`;
|
2023-03-24 00:12:23 -06:00
|
|
|
const mode = $this.data('markup-mode') || 'comment';
|
2024-03-16 06:22:16 -06:00
|
|
|
const $treePathEl = $form.find('input#tree_path');
|
|
|
|
if ($treePathEl.length > 0) {
|
|
|
|
context += $treePathEl.val();
|
2021-10-16 11:28:04 -06:00
|
|
|
}
|
|
|
|
context = context.substring(0, context.lastIndexOf('/'));
|
2024-03-07 00:28:33 -07:00
|
|
|
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append('mode', mode);
|
|
|
|
formData.append('context', context);
|
2024-04-29 14:53:15 -06:00
|
|
|
formData.append('text', $form.find('.tab[data-tab="write"] textarea').val());
|
2024-03-16 06:22:16 -06:00
|
|
|
formData.append('file_path', $treePathEl.val());
|
2024-03-07 00:28:33 -07:00
|
|
|
try {
|
|
|
|
const response = await POST($this.data('url'), {data: formData});
|
|
|
|
const data = await response.text();
|
2024-04-29 14:53:15 -06:00
|
|
|
const $previewPanel = $form.find('.tab[data-tab="preview"]');
|
|
|
|
if ($previewPanel.length) {
|
|
|
|
renderPreviewPanelContent($previewPanel, data);
|
|
|
|
}
|
2024-03-07 00:28:33 -07:00
|
|
|
} catch (error) {
|
|
|
|
console.error('Error:', error);
|
|
|
|
}
|
2021-10-16 11:28:04 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 02:27:25 -07:00
|
|
|
export function initRepoEditor() {
|
2024-06-26 11:01:20 -06:00
|
|
|
const dropzoneUpload = document.querySelector('.page-content.repository.editor.upload .dropzone');
|
|
|
|
if (dropzoneUpload) initDropzone(dropzoneUpload);
|
|
|
|
|
|
|
|
const editArea = document.querySelector('.page-content.repository.editor textarea#edit_area');
|
|
|
|
if (!editArea) return;
|
2021-10-16 11:28:04 -06:00
|
|
|
|
2024-06-10 04:12:31 -06:00
|
|
|
for (const el of queryElems('.js-quick-pull-choice-option')) {
|
|
|
|
el.addEventListener('input', () => {
|
|
|
|
if (el.value === 'commit-to-new-branch') {
|
|
|
|
showElem('.quick-pull-branch-name');
|
|
|
|
document.querySelector('.quick-pull-branch-name input').required = true;
|
2023-03-03 15:28:20 -07:00
|
|
|
} else {
|
2024-06-10 04:12:31 -06:00
|
|
|
hideElem('.quick-pull-branch-name');
|
|
|
|
document.querySelector('.quick-pull-branch-name input').required = false;
|
2023-03-03 15:28:20 -07:00
|
|
|
}
|
2024-06-10 04:12:31 -06:00
|
|
|
document.querySelector('#commit-button').textContent = el.getAttribute('data-button-text');
|
2023-03-03 15:28:20 -07:00
|
|
|
});
|
2024-06-10 04:12:31 -06:00
|
|
|
}
|
2021-10-16 11:28:04 -06:00
|
|
|
|
2024-06-10 04:12:31 -06:00
|
|
|
const filenameInput = document.querySelector('#file-name');
|
|
|
|
function joinTreePath() {
|
|
|
|
const parts = [];
|
|
|
|
for (const el of document.querySelectorAll('.breadcrumb span.section')) {
|
|
|
|
const link = el.querySelector('a');
|
|
|
|
parts.push(link ? link.textContent : el.textContent);
|
|
|
|
}
|
|
|
|
if (filenameInput.value) {
|
|
|
|
parts.push(filenameInput.value);
|
|
|
|
}
|
|
|
|
document.querySelector('#tree_path').value = parts.join('/');
|
|
|
|
}
|
|
|
|
filenameInput.addEventListener('input', function () {
|
|
|
|
const parts = filenameInput.value.split('/');
|
2023-03-03 15:28:20 -07:00
|
|
|
if (parts.length > 1) {
|
2021-10-16 11:28:04 -06:00
|
|
|
for (let i = 0; i < parts.length; ++i) {
|
2023-03-03 15:28:20 -07:00
|
|
|
const value = parts[i];
|
2021-10-16 11:28:04 -06:00
|
|
|
if (i < parts.length - 1) {
|
|
|
|
if (value.length) {
|
2024-06-10 04:12:31 -06:00
|
|
|
$(`<span class="section"><a href="#">${htmlEscape(value)}</a></span>`).insertBefore($(filenameInput));
|
|
|
|
$('<div class="breadcrumb-divider">/</div>').insertBefore($(filenameInput));
|
2021-10-16 11:28:04 -06:00
|
|
|
}
|
|
|
|
} else {
|
2024-06-10 04:12:31 -06:00
|
|
|
filenameInput.value = value;
|
2021-10-16 11:28:04 -06:00
|
|
|
}
|
2023-03-03 15:28:20 -07:00
|
|
|
this.setSelectionRange(0, 0);
|
2021-10-16 11:28:04 -06:00
|
|
|
}
|
|
|
|
}
|
2024-06-10 04:12:31 -06:00
|
|
|
joinTreePath();
|
2023-03-03 15:28:20 -07:00
|
|
|
});
|
2024-06-10 04:12:31 -06:00
|
|
|
filenameInput.addEventListener('keydown', function (e) {
|
|
|
|
const sections = queryElems('.breadcrumb span.section');
|
|
|
|
const dividers = queryElems('.breadcrumb .breadcrumb-divider');
|
2023-03-03 15:28:20 -07:00
|
|
|
// Jump back to last directory once the filename is empty
|
2024-06-10 04:12:31 -06:00
|
|
|
if (e.code === 'Backspace' && filenameInput.selectionStart === 0 && sections.length > 0) {
|
2023-03-03 15:28:20 -07:00
|
|
|
e.preventDefault();
|
2024-06-10 04:12:31 -06:00
|
|
|
const lastSection = sections[sections.length - 1];
|
|
|
|
const lastDivider = dividers.length ? dividers[dividers.length - 1] : null;
|
|
|
|
const value = lastSection.querySelector('a').textContent;
|
|
|
|
filenameInput.value = value + filenameInput.value;
|
2023-03-03 15:28:20 -07:00
|
|
|
this.setSelectionRange(value.length, value.length);
|
2024-06-10 04:12:31 -06:00
|
|
|
lastDivider?.remove();
|
|
|
|
lastSection.remove();
|
|
|
|
joinTreePath();
|
2023-03-03 15:28:20 -07:00
|
|
|
}
|
|
|
|
});
|
2021-10-16 11:28:04 -06:00
|
|
|
|
2024-06-10 04:12:31 -06:00
|
|
|
const $form = $('.repository.editor .edit.form');
|
|
|
|
initEditPreviewTab($form);
|
2021-10-16 11:28:04 -06:00
|
|
|
|
2021-11-09 02:27:25 -07:00
|
|
|
(async () => {
|
2024-06-26 11:01:20 -06:00
|
|
|
const editor = await createCodeEditor(editArea, filenameInput);
|
2021-10-16 11:28:04 -06:00
|
|
|
|
2021-11-09 02:27:25 -07:00
|
|
|
// Using events from https://github.com/codedance/jquery.AreYouSure#advanced-usage
|
|
|
|
// to enable or disable the commit button
|
2024-06-10 14:49:33 -06:00
|
|
|
const commitButton = document.querySelector('#commit-button');
|
2021-11-09 02:27:25 -07:00
|
|
|
const $editForm = $('.ui.edit.form');
|
|
|
|
const dirtyFileClass = 'dirty-file';
|
2021-10-16 11:28:04 -06:00
|
|
|
|
2021-11-09 02:27:25 -07:00
|
|
|
// Disabling the button at the start
|
|
|
|
if ($('input[name="page_has_posted"]').val() !== 'true') {
|
2024-03-16 09:08:10 -06:00
|
|
|
commitButton.disabled = true;
|
2021-10-16 11:28:04 -06:00
|
|
|
}
|
|
|
|
|
2021-11-09 02:27:25 -07:00
|
|
|
// Registering a custom listener for the file path and the file content
|
|
|
|
$editForm.areYouSure({
|
|
|
|
silent: true,
|
|
|
|
dirtyClass: dirtyFileClass,
|
|
|
|
fieldSelector: ':input:not(.commit-form-wrapper :input)',
|
2024-03-29 13:24:17 -06:00
|
|
|
change($form) {
|
|
|
|
const dirty = $form[0]?.classList.contains(dirtyFileClass);
|
2024-03-16 09:08:10 -06:00
|
|
|
commitButton.disabled = !dirty;
|
2021-11-09 02:27:25 -07:00
|
|
|
},
|
|
|
|
});
|
2021-10-16 11:28:04 -06:00
|
|
|
|
2021-11-09 02:27:25 -07:00
|
|
|
// Update the editor from query params, if available,
|
|
|
|
// only after the dirtyFileClass initialization
|
|
|
|
const params = new URLSearchParams(window.location.search);
|
|
|
|
const value = params.get('value');
|
|
|
|
if (value) {
|
|
|
|
editor.setValue(value);
|
2021-10-16 11:28:04 -06:00
|
|
|
}
|
2021-11-09 02:27:25 -07:00
|
|
|
|
2024-03-16 09:08:10 -06:00
|
|
|
commitButton?.addEventListener('click', (e) => {
|
2021-11-09 02:27:25 -07:00
|
|
|
// A modal which asks if an empty file should be committed
|
2024-06-26 11:01:20 -06:00
|
|
|
if (!editArea.value) {
|
2021-11-09 02:27:25 -07:00
|
|
|
$('#edit-empty-content-modal').modal({
|
|
|
|
onApprove() {
|
|
|
|
$('.edit.form').trigger('submit');
|
|
|
|
},
|
|
|
|
}).modal('show');
|
2024-03-16 09:08:10 -06:00
|
|
|
e.preventDefault();
|
2021-11-09 02:27:25 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
})();
|
2021-10-16 11:28:04 -06:00
|
|
|
}
|
2023-04-11 21:03:23 -06:00
|
|
|
|
2024-04-29 14:53:15 -06:00
|
|
|
export function renderPreviewPanelContent($previewPanel, data) {
|
|
|
|
$previewPanel.html(data);
|
2023-04-11 21:03:23 -06:00
|
|
|
initMarkupContent();
|
|
|
|
|
2024-04-29 14:53:15 -06:00
|
|
|
const $refIssues = $previewPanel.find('p .ref-issue');
|
2024-03-16 06:22:16 -06:00
|
|
|
attachRefIssueContextPopup($refIssues);
|
2023-04-11 21:03:23 -06:00
|
|
|
}
|