2024-04-11 18:22:59 +00:00
|
|
|
import $ from 'jquery';
|
2024-07-07 15:32:30 +00:00
|
|
|
import {handleReply} from './repo-issue.ts';
|
2024-10-23 02:48:04 +00:00
|
|
|
import {getComboMarkdownEditor, initComboMarkdownEditor, ComboMarkdownEditor} from './comp/ComboMarkdownEditor.ts';
|
2024-07-07 15:32:30 +00:00
|
|
|
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-10-23 02:48:04 +00:00
|
|
|
import {triggerUploadStateChanged} from './comp/EditorUpload.ts';
|
2024-04-11 18:22:59 +00: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');
|
|
|
|
|
2024-10-23 02:48:04 +00:00
|
|
|
let comboMarkdownEditor : ComboMarkdownEditor;
|
2024-04-11 18:22:59 +00:00
|
|
|
|
|
|
|
const cancelAndReset = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
showElem(renderContent);
|
|
|
|
hideElem(editContentZone);
|
2024-06-26 17:01:20 +00:00
|
|
|
comboMarkdownEditor.dropzoneReloadFiles();
|
2024-04-11 18:22:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const saveAndRefresh = async (e) => {
|
|
|
|
e.preventDefault();
|
2024-06-26 17:01:20 +00:00
|
|
|
renderContent.classList.add('is-loading');
|
2024-04-11 18:22:59 +00:00
|
|
|
showElem(renderContent);
|
|
|
|
hideElem(editContentZone);
|
|
|
|
try {
|
|
|
|
const params = new URLSearchParams({
|
|
|
|
content: comboMarkdownEditor.value(),
|
|
|
|
context: editContentZone.getAttribute('data-context'),
|
2024-05-27 15:34:18 +00:00
|
|
|
content_version: editContentZone.getAttribute('data-content-version'),
|
2024-04-11 18:22:59 +00:00
|
|
|
});
|
2024-06-26 17:01:20 +00:00
|
|
|
for (const file of comboMarkdownEditor.dropzoneGetFiles() ?? []) {
|
|
|
|
params.append('files[]', file);
|
|
|
|
}
|
2024-04-11 18:22:59 +00:00
|
|
|
|
|
|
|
const response = await POST(editContentZone.getAttribute('data-update-url'), {data: params});
|
|
|
|
const data = await response.json();
|
2024-05-27 15:34:18 +00:00
|
|
|
if (response.status === 400) {
|
|
|
|
showErrorToast(data.errorMessage);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
editContentZone.setAttribute('data-content-version', data.contentVersion);
|
2024-04-11 18:22:59 +00:00
|
|
|
if (!data.content) {
|
2024-06-10 20:49:33 +00:00
|
|
|
renderContent.innerHTML = document.querySelector('#no-content').innerHTML;
|
2024-04-11 18:22:59 +00: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-26 17:01:20 +00:00
|
|
|
comboMarkdownEditor.dropzoneSubmitReload();
|
2024-04-11 18:22:59 +00:00
|
|
|
initMarkupContent();
|
|
|
|
initCommentContent();
|
|
|
|
} catch (error) {
|
2024-06-26 17:01:20 +00:00
|
|
|
showErrorToast(`Failed to save the content: ${error}`);
|
2024-04-11 18:22:59 +00:00
|
|
|
console.error(error);
|
2024-06-26 17:01:20 +00:00
|
|
|
} finally {
|
|
|
|
renderContent.classList.remove('is-loading');
|
2024-04-11 18:22:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
comboMarkdownEditor = getComboMarkdownEditor(editContentZone.querySelector('.combo-markdown-editor'));
|
|
|
|
if (!comboMarkdownEditor) {
|
2024-06-10 20:49:33 +00:00
|
|
|
editContentZone.innerHTML = document.querySelector('#issue-comment-editor-template').innerHTML;
|
2024-10-23 02:48:04 +00:00
|
|
|
const saveButton = editContentZone.querySelector('.ui.primary.button');
|
2024-04-11 18:22:59 +00:00
|
|
|
comboMarkdownEditor = await initComboMarkdownEditor(editContentZone.querySelector('.combo-markdown-editor'));
|
2024-10-23 02:48:04 +00:00
|
|
|
const syncUiState = () => saveButton.disabled = comboMarkdownEditor.isUploading();
|
|
|
|
comboMarkdownEditor.container.addEventListener(ComboMarkdownEditor.EventUploadStateChanged, syncUiState);
|
2024-04-27 14:32:00 +00:00
|
|
|
editContentZone.querySelector('.ui.cancel.button').addEventListener('click', cancelAndReset);
|
2024-10-23 02:48:04 +00:00
|
|
|
saveButton.addEventListener('click', saveAndRefresh);
|
2024-04-11 18:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Show write/preview tab and copy raw content as needed
|
|
|
|
showElem(editContentZone);
|
|
|
|
hideElem(renderContent);
|
2024-06-26 17:01:20 +00:00
|
|
|
// FIXME: ideally here should reload content and attachment list from backend for existing editor, to avoid losing data
|
2024-04-11 18:22:59 +00:00
|
|
|
if (!comboMarkdownEditor.value()) {
|
|
|
|
comboMarkdownEditor.value(rawContent.textContent);
|
|
|
|
}
|
2024-06-22 09:25:04 +00:00
|
|
|
comboMarkdownEditor.switchTabToEditor();
|
2024-04-11 18:22:59 +00:00
|
|
|
comboMarkdownEditor.focus();
|
2024-10-23 02:48:04 +00:00
|
|
|
triggerUploadStateChanged(comboMarkdownEditor.container);
|
2024-04-11 18:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 10:12:31 +00:00
|
|
|
const target = this.getAttribute('data-target');
|
|
|
|
const quote = document.querySelector(`#${target}`).textContent.replace(/\n/g, '\n> ');
|
2024-04-11 18:22:59 +00:00
|
|
|
const content = `> ${quote}\n\n`;
|
2024-06-10 10:12:31 +00:00
|
|
|
|
2024-04-11 18:22:59 +00:00
|
|
|
let editor;
|
2024-06-10 10:12:31 +00:00
|
|
|
if (this.classList.contains('quote-reply-diff')) {
|
2024-06-26 17:01:20 +00:00
|
|
|
const replyBtn = this.closest('.comment-code-cloud').querySelector('button.comment-form-reply');
|
|
|
|
editor = await handleReply(replyBtn);
|
2024-04-11 18:22:59 +00: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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|