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