2024-04-12 02:22:59 +08:00
|
|
|
import $ from 'jquery';
|
2024-07-07 23:32:30 +08:00
|
|
|
import {handleReply} from './repo-issue.ts';
|
|
|
|
import {getComboMarkdownEditor, initComboMarkdownEditor} from './comp/ComboMarkdownEditor.ts';
|
|
|
|
import {POST} from '../modules/fetch.ts';
|
|
|
|
import {showErrorToast} from '../modules/toast.ts';
|
|
|
|
import {hideElem, showElem} from '../utils/dom.ts';
|
|
|
|
import {attachRefIssueContextPopup} from './contextpopup.ts';
|
|
|
|
import {initCommentContent, initMarkupContent} from '../markup/content.ts';
|
2024-04-12 02:22:59 +08:00
|
|
|
|
|
|
|
async function onEditContent(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
const segment = this.closest('.header').nextElementSibling;
|
|
|
|
const editContentZone = segment.querySelector('.edit-content-zone');
|
|
|
|
const renderContent = segment.querySelector('.render-content');
|
|
|
|
const rawContent = segment.querySelector('.raw-content');
|
|
|
|
|
|
|
|
let comboMarkdownEditor;
|
|
|
|
|
|
|
|
const cancelAndReset = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
showElem(renderContent);
|
|
|
|
hideElem(editContentZone);
|
2024-06-27 01:01:20 +08:00
|
|
|
comboMarkdownEditor.dropzoneReloadFiles();
|
2024-04-12 02:22:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const saveAndRefresh = async (e) => {
|
|
|
|
e.preventDefault();
|
2024-06-27 01:01:20 +08:00
|
|
|
renderContent.classList.add('is-loading');
|
2024-04-12 02:22:59 +08:00
|
|
|
showElem(renderContent);
|
|
|
|
hideElem(editContentZone);
|
|
|
|
try {
|
|
|
|
const params = new URLSearchParams({
|
|
|
|
content: comboMarkdownEditor.value(),
|
|
|
|
context: editContentZone.getAttribute('data-context'),
|
2024-05-27 23:34:18 +08:00
|
|
|
content_version: editContentZone.getAttribute('data-content-version'),
|
2024-04-12 02:22:59 +08:00
|
|
|
});
|
2024-06-27 01:01:20 +08:00
|
|
|
for (const file of comboMarkdownEditor.dropzoneGetFiles() ?? []) {
|
|
|
|
params.append('files[]', file);
|
|
|
|
}
|
2024-04-12 02:22:59 +08:00
|
|
|
|
|
|
|
const response = await POST(editContentZone.getAttribute('data-update-url'), {data: params});
|
|
|
|
const data = await response.json();
|
2024-05-27 23:34:18 +08:00
|
|
|
if (response.status === 400) {
|
|
|
|
showErrorToast(data.errorMessage);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
editContentZone.setAttribute('data-content-version', data.contentVersion);
|
2024-04-12 02:22:59 +08:00
|
|
|
if (!data.content) {
|
2024-06-11 04:49:33 +08:00
|
|
|
renderContent.innerHTML = document.querySelector('#no-content').innerHTML;
|
2024-04-12 02:22:59 +08:00
|
|
|
rawContent.textContent = '';
|
|
|
|
} else {
|
|
|
|
renderContent.innerHTML = data.content;
|
|
|
|
rawContent.textContent = comboMarkdownEditor.value();
|
|
|
|
const refIssues = renderContent.querySelectorAll('p .ref-issue');
|
|
|
|
attachRefIssueContextPopup(refIssues);
|
|
|
|
}
|
|
|
|
const content = segment;
|
|
|
|
if (!content.querySelector('.dropzone-attachments')) {
|
|
|
|
if (data.attachments !== '') {
|
|
|
|
content.insertAdjacentHTML('beforeend', data.attachments);
|
|
|
|
}
|
|
|
|
} else if (data.attachments === '') {
|
|
|
|
content.querySelector('.dropzone-attachments').remove();
|
|
|
|
} else {
|
|
|
|
content.querySelector('.dropzone-attachments').outerHTML = data.attachments;
|
|
|
|
}
|
2024-06-27 01:01:20 +08:00
|
|
|
comboMarkdownEditor.dropzoneSubmitReload();
|
2024-04-12 02:22:59 +08:00
|
|
|
initMarkupContent();
|
|
|
|
initCommentContent();
|
|
|
|
} catch (error) {
|
2024-06-27 01:01:20 +08:00
|
|
|
showErrorToast(`Failed to save the content: ${error}`);
|
2024-04-12 02:22:59 +08:00
|
|
|
console.error(error);
|
2024-06-27 01:01:20 +08:00
|
|
|
} finally {
|
|
|
|
renderContent.classList.remove('is-loading');
|
2024-04-12 02:22:59 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
comboMarkdownEditor = getComboMarkdownEditor(editContentZone.querySelector('.combo-markdown-editor'));
|
|
|
|
if (!comboMarkdownEditor) {
|
2024-06-11 04:49:33 +08:00
|
|
|
editContentZone.innerHTML = document.querySelector('#issue-comment-editor-template').innerHTML;
|
2024-04-12 02:22:59 +08:00
|
|
|
comboMarkdownEditor = await initComboMarkdownEditor(editContentZone.querySelector('.combo-markdown-editor'));
|
2024-04-27 22:32:00 +08:00
|
|
|
editContentZone.querySelector('.ui.cancel.button').addEventListener('click', cancelAndReset);
|
|
|
|
editContentZone.querySelector('.ui.primary.button').addEventListener('click', saveAndRefresh);
|
2024-04-12 02:22:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Show write/preview tab and copy raw content as needed
|
|
|
|
showElem(editContentZone);
|
|
|
|
hideElem(renderContent);
|
2024-06-27 01:01:20 +08:00
|
|
|
// FIXME: ideally here should reload content and attachment list from backend for existing editor, to avoid losing data
|
2024-04-12 02:22:59 +08:00
|
|
|
if (!comboMarkdownEditor.value()) {
|
|
|
|
comboMarkdownEditor.value(rawContent.textContent);
|
|
|
|
}
|
2024-06-22 17:25:04 +08:00
|
|
|
comboMarkdownEditor.switchTabToEditor();
|
2024-04-12 02:22:59 +08:00
|
|
|
comboMarkdownEditor.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function initRepoIssueCommentEdit() {
|
|
|
|
// Edit issue or comment content
|
|
|
|
$(document).on('click', '.edit-content', onEditContent);
|
|
|
|
|
|
|
|
// Quote reply
|
|
|
|
$(document).on('click', '.quote-reply', async function (event) {
|
|
|
|
event.preventDefault();
|
2024-06-10 18:12:31 +08:00
|
|
|
const target = this.getAttribute('data-target');
|
|
|
|
const quote = document.querySelector(`#${target}`).textContent.replace(/\n/g, '\n> ');
|
2024-04-12 02:22:59 +08:00
|
|
|
const content = `> ${quote}\n\n`;
|
2024-06-10 18:12:31 +08:00
|
|
|
|
2024-04-12 02:22:59 +08:00
|
|
|
let editor;
|
2024-06-10 18:12:31 +08:00
|
|
|
if (this.classList.contains('quote-reply-diff')) {
|
2024-06-27 01:01:20 +08:00
|
|
|
const replyBtn = this.closest('.comment-code-cloud').querySelector('button.comment-form-reply');
|
|
|
|
editor = await handleReply(replyBtn);
|
2024-04-12 02:22:59 +08:00
|
|
|
} else {
|
|
|
|
// for normal issue/comment page
|
|
|
|
editor = getComboMarkdownEditor($('#comment-form .combo-markdown-editor'));
|
|
|
|
}
|
|
|
|
if (editor) {
|
|
|
|
if (editor.value()) {
|
|
|
|
editor.value(`${editor.value()}\n\n${content}`);
|
|
|
|
} else {
|
|
|
|
editor.value(content);
|
|
|
|
}
|
|
|
|
editor.focus();
|
|
|
|
editor.moveCursorToEnd();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|