Files

214 lines
9.4 KiB
TypeScript
Raw Permalink Normal View History

2025-07-01 21:44:05 +08:00
import {html, htmlRaw} from '../utils/html.ts';
2026-03-31 23:50:45 +02:00
import {createCodeEditor} from '../modules/codeeditor/main.ts';
import {trimTrailingWhitespaceFromView} from '../modules/codeeditor/utils.ts';
import {hideElem, queryElems, showElem, createElementFromHTML, onInputDebounce} from '../utils/dom.ts';
import {POST} from '../modules/fetch.ts';
import {initDropzone} from './dropzone.ts';
2024-12-04 17:26:54 +08:00
import {confirmModal} from './comp/ConfirmModal.ts';
2024-12-22 01:11:22 +08:00
import {applyAreYouSure, ignoreAreYouSure} from '../vendor/jquery.are-you-sure.ts';
2024-12-04 17:26:54 +08:00
import {fomanticQuery} from '../modules/fomantic/base.ts';
2025-06-21 19:20:51 +08:00
import {submitFormFetchAction} from './common-fetch-action.ts';
2021-10-17 01:28:04 +08:00
2024-12-04 17:26:54 +08:00
function initEditPreviewTab(elForm: HTMLFormElement) {
2025-12-03 03:13:16 +01:00
const elTabMenu = elForm.querySelector('.repo-editor-menu')!;
2024-12-04 17:26:54 +08:00
fomanticQuery(elTabMenu.querySelectorAll('.item')).tab();
2024-12-04 17:26:54 +08:00
const elPreviewTab = elTabMenu.querySelector('a[data-tab="preview"]');
const elPreviewPanel = elForm.querySelector('.tab[data-tab="preview"]');
if (!elPreviewTab || !elPreviewPanel) return;
elPreviewTab.addEventListener('click', async () => {
2025-12-03 03:13:16 +01:00
const elTreePath = elForm.querySelector<HTMLInputElement>('input#tree_path')!;
const previewUrl = elPreviewTab.getAttribute('data-preview-url')!;
2024-12-04 17:26:54 +08:00
const previewContextRef = elPreviewTab.getAttribute('data-preview-context-ref');
let previewContext = `${previewContextRef}/${elTreePath.value}`;
previewContext = previewContext.substring(0, previewContext.lastIndexOf('/'));
const formData = new FormData();
formData.append('mode', 'file');
formData.append('context', previewContext);
2025-12-03 03:13:16 +01:00
formData.append('text', elForm.querySelector<HTMLTextAreaElement>('.tab[data-tab="write"] textarea')!.value);
2024-12-04 17:26:54 +08:00
formData.append('file_path', elTreePath.value);
const response = await POST(previewUrl, {data: formData});
const data = await response.text();
renderPreviewPanelContent(elPreviewPanel, data);
});
2021-10-17 01:28:04 +08:00
}
export function initRepoEditor() {
const dropzoneUpload = document.querySelector<HTMLElement>('.page-content.repository.editor.upload .dropzone');
2024-06-27 01:01:20 +08:00
if (dropzoneUpload) initDropzone(dropzoneUpload);
for (const el of queryElems<HTMLInputElement>(document, '.js-quick-pull-choice-option')) {
2024-06-10 12:12:31 +02:00
el.addEventListener('input', () => {
if (el.value === 'commit-to-new-branch') {
showElem('.quick-pull-branch-name');
2025-12-03 03:13:16 +01:00
document.querySelector<HTMLInputElement>('.quick-pull-branch-name input')!.required = true;
} else {
2024-06-10 12:12:31 +02:00
hideElem('.quick-pull-branch-name');
2025-12-03 03:13:16 +01:00
document.querySelector<HTMLInputElement>('.quick-pull-branch-name input')!.required = false;
}
2025-12-03 03:13:16 +01:00
document.querySelector('#commit-button')!.textContent = el.getAttribute('data-button-text');
});
2024-06-10 12:12:31 +02:00
}
2021-10-17 01:28:04 +08:00
2026-03-31 23:50:45 +02:00
// ATTENTION: two pages have this filename input
// * new/edit file page: there is a code editor
// * upload page: there is no code editor, but a uploader
2025-12-03 03:13:16 +01:00
const filenameInput = document.querySelector<HTMLInputElement>('#file-name')!;
2025-01-13 03:39:15 +08:00
if (!filenameInput) return;
2026-03-31 23:50:45 +02:00
filenameInput.value = filenameInput.defaultValue; // prevent browser from restoring form values on refresh
2024-06-10 12:12:31 +02:00
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);
}
2025-12-03 03:13:16 +01:00
document.querySelector<HTMLInputElement>('#tree_path')!.value = parts.join('/');
2024-06-10 12:12:31 +02:00
}
filenameInput.addEventListener('input', function () {
const parts = filenameInput.value.split('/');
const links = Array.from(document.querySelectorAll('.breadcrumb span.section'));
const dividers = Array.from(document.querySelectorAll('.breadcrumb .breadcrumb-divider'));
let warningDiv = document.querySelector<HTMLDivElement>('.ui.warning.message.flash-message.flash-warning.space-related');
let containSpace = false;
if (parts.length > 1) {
2021-10-17 01:28:04 +08:00
for (let i = 0; i < parts.length; ++i) {
const value = parts[i];
const trimValue = value.trim();
if (trimValue === '..') {
// remove previous tree path
if (links.length > 0) {
2025-12-03 03:13:16 +01:00
const link = links.pop()!;
const divider = dividers.pop()!;
link.remove();
divider.remove();
}
continue;
}
2021-10-17 01:28:04 +08:00
if (i < parts.length - 1) {
if (trimValue.length) {
const linkElement = createElementFromHTML(
2025-07-01 21:44:05 +08:00
html`<span class="section"><a href="#">${value}</a></span>`,
);
const dividerElement = createElementFromHTML(
2025-07-01 21:44:05 +08:00
html`<div class="breadcrumb-divider">/</div>`,
);
links.push(linkElement);
dividers.push(dividerElement);
filenameInput.before(linkElement);
filenameInput.before(dividerElement);
2021-10-17 01:28:04 +08:00
}
} else {
2024-06-10 12:12:31 +02:00
filenameInput.value = value;
2021-10-17 01:28:04 +08:00
}
this.setSelectionRange(0, 0);
containSpace = containSpace || (trimValue !== value && trimValue !== '');
}
}
containSpace = containSpace || Array.from(links).some((link) => {
2025-12-03 03:13:16 +01:00
const value = link.querySelector('a')!.textContent;
return value.trim() !== value;
});
containSpace = containSpace || parts[parts.length - 1].trim() !== parts[parts.length - 1];
if (containSpace) {
if (!warningDiv) {
warningDiv = document.createElement('div');
warningDiv.classList.add('ui', 'warning', 'message', 'flash-message', 'flash-warning', 'space-related');
2025-07-01 21:44:05 +08:00
warningDiv.innerHTML = html`<p>File path contains leading or trailing whitespace.</p>`;
2026-02-20 22:14:29 +01:00
// Change to `block` display because it is set to 'none' in fomantic/build/semantic.css
warningDiv.classList.add('tw-block');
2025-12-03 03:13:16 +01:00
const inputContainer = document.querySelector('.repo-editor-header')!;
inputContainer.insertAdjacentElement('beforebegin', warningDiv);
2021-10-17 01:28:04 +08:00
}
showElem(warningDiv);
} else if (warningDiv) {
hideElem(warningDiv);
2021-10-17 01:28:04 +08:00
}
2024-06-10 12:12:31 +02:00
joinTreePath();
});
2024-06-10 12:12:31 +02:00
filenameInput.addEventListener('keydown', function (e) {
const sections = queryElems(document, '.breadcrumb span.section');
const dividers = queryElems(document, '.breadcrumb .breadcrumb-divider');
// Jump back to last directory once the filename is empty
2024-06-10 12:12:31 +02:00
if (e.code === 'Backspace' && filenameInput.selectionStart === 0 && sections.length > 0) {
e.preventDefault();
2024-06-10 12:12:31 +02:00
const lastSection = sections[sections.length - 1];
const lastDivider = dividers.length ? dividers[dividers.length - 1] : null;
2025-12-03 03:13:16 +01:00
const value = lastSection.querySelector('a')!.textContent;
2024-06-10 12:12:31 +02:00
filenameInput.value = value + filenameInput.value;
this.setSelectionRange(value.length, value.length);
2024-06-10 12:12:31 +02:00
lastDivider?.remove();
lastSection.remove();
joinTreePath();
}
});
2021-10-17 01:28:04 +08:00
2025-12-03 03:13:16 +01:00
const elForm = document.querySelector<HTMLFormElement>('.repository.editor .edit.form')!;
2026-03-31 23:50:45 +02:00
// see the ATTENTION above, on the upload page, there is no editor(textarea)
// so only the filename input above is initialized, the code below (for the code editor) will be skipped
2025-06-21 19:20:51 +08:00
const editArea = document.querySelector<HTMLTextAreaElement>('.page-content.repository.editor textarea#edit_area');
if (!editArea) return;
// Using events from https://github.com/codedance/jquery.AreYouSure#advanced-usage
// to enable or disable the commit button
2025-12-03 03:13:16 +01:00
const commitButton = document.querySelector<HTMLButtonElement>('#commit-button')!;
const dirtyFileClass = 'dirty-file';
2025-06-21 19:20:51 +08:00
const syncCommitButtonState = () => {
const dirty = elForm.classList.contains(dirtyFileClass);
commitButton.disabled = !dirty;
};
// Registering a custom listener for the file path and the file content
// FIXME: it is not quite right here (old bug), it causes double-init, the global areYouSure "dirty" class will also be added
applyAreYouSure(elForm, {
silent: true,
dirtyClass: dirtyFileClass,
fieldSelector: ':input:not(.commit-form-wrapper :input)',
2025-06-21 19:20:51 +08:00
change: syncCommitButtonState,
});
2025-06-21 19:20:51 +08:00
syncCommitButtonState(); // disable the "commit" button when no content changes
2025-01-13 03:39:15 +08:00
2024-12-04 17:26:54 +08:00
initEditPreviewTab(elForm);
2021-10-17 01:28:04 +08:00
(async () => {
2024-06-27 01:01:20 +08:00
const editor = await createCodeEditor(editArea, filenameInput);
2026-03-31 23:50:45 +02:00
filenameInput.addEventListener('input', onInputDebounce(() => editor.updateFilename(filenameInput.value)));
2021-10-17 01:28:04 +08: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) {
2026-03-31 23:50:45 +02:00
editor.view.dispatch({
changes: {from: 0, to: editor.view.state.doc.length, insert: value},
});
2021-10-17 01:28:04 +08:00
}
2025-06-21 19:20:51 +08:00
commitButton.addEventListener('click', async (e) => {
2026-03-31 23:50:45 +02:00
if (editor.trimTrailingWhitespace) {
trimTrailingWhitespaceFromView(editor.view);
}
// A modal which asks if an empty file should be committed
2024-06-27 01:01:20 +08:00
if (!editArea.value) {
e.preventDefault();
2024-12-04 17:26:54 +08:00
if (await confirmModal({
2025-12-03 03:13:16 +01:00
header: elForm.getAttribute('data-text-empty-confirm-header')!,
content: elForm.getAttribute('data-text-empty-confirm-content')!,
2024-12-04 17:26:54 +08:00
})) {
2024-12-22 01:11:22 +08:00
ignoreAreYouSure(elForm);
2025-06-21 19:20:51 +08:00
submitFormFetchAction(elForm);
2024-12-04 17:26:54 +08:00
}
}
});
})();
2021-10-17 01:28:04 +08:00
}
2025-07-01 21:44:05 +08:00
export function renderPreviewPanelContent(previewPanel: Element, htmlContent: string) {
// the content is from the server, so it is safe to use innerHTML
previewPanel.innerHTML = html`<div class="render-content render-preview markup">${htmlRaw(htmlContent)}</div>`;
}