Add DB attachment editor

This commit is contained in:
Cohee 2024-04-21 18:23:41 +03:00
parent bae74fbbd7
commit 5992c34fb5
2 changed files with 47 additions and 4 deletions

View File

@ -631,19 +631,60 @@ async function openFilePopup(attachment) {
callGenericPopup(modalTemplate, POPUP_TYPE.TEXT, '', { wide: true, large: true }); callGenericPopup(modalTemplate, POPUP_TYPE.TEXT, '', { wide: true, large: true });
} }
/**
* Edit a file attachment in a notepad-like modal.
* @param {FileAttachment} attachment Attachment to edit
* @param {string} source Attachment source
* @param {function} callback Callback function
*/
async function editAttachment(attachment, source, callback) {
const originalFileText = attachment.text || (await getFileAttachment(attachment.url));
const template = $(await renderExtensionTemplateAsync('attachments', 'notepad'));
let editedFileText = originalFileText;
template.find('[name="notepadFileContent"]').val(editedFileText).on('input', function () {
editedFileText = String($(this).val());
});
let editedFileName = attachment.name;
template.find('[name="notepadFileName"]').val(editedFileName).on('input', function () {
editedFileName = String($(this).val());
});
const result = await callGenericPopup(template, POPUP_TYPE.CONFIRM, '', { wide: true, large: true, okButton: 'Save', cancelButton: 'Cancel' });
if (result !== POPUP_RESULT.AFFIRMATIVE) {
return;
}
if (editedFileText === originalFileText && editedFileName === attachment.name) {
return;
}
const nullCallback = () => { };
await deleteAttachment(attachment, source, nullCallback, false);
const file = new File([editedFileText], editedFileName, { type: 'text/plain' });
await uploadFileAttachmentToServer(file, source);
callback();
}
/** /**
* Deletes an attachment from the server and the chat. * Deletes an attachment from the server and the chat.
* @param {FileAttachment} attachment Attachment to delete * @param {FileAttachment} attachment Attachment to delete
* @param {string} source Source of the attachment * @param {string} source Source of the attachment
* @param {function} callback Callback function * @param {function} callback Callback function
* @param {boolean} [confirm=true] If true, show a confirmation dialog
* @returns {Promise<void>} A promise that resolves when the attachment is deleted. * @returns {Promise<void>} A promise that resolves when the attachment is deleted.
*/ */
async function deleteAttachment(attachment, source, callback) { async function deleteAttachment(attachment, source, callback, confirm = true) {
const confirm = await callGenericPopup('Are you sure you want to delete this attachment?', POPUP_TYPE.CONFIRM); if (confirm) {
const result = await callGenericPopup('Are you sure you want to delete this attachment?', POPUP_TYPE.CONFIRM);
if (confirm !== POPUP_RESULT.AFFIRMATIVE) { if (result !== POPUP_RESULT.AFFIRMATIVE) {
return; return;
} }
}
ensureAttachmentsExist(); ensureAttachmentsExist();
@ -719,6 +760,7 @@ async function openAttachmentManager() {
attachmentTemplate.find('.attachmentListItemSize').text(humanFileSize(attachment.size)); attachmentTemplate.find('.attachmentListItemSize').text(humanFileSize(attachment.size));
attachmentTemplate.find('.attachmentListItemCreated').text(new Date(attachment.created).toLocaleString()); attachmentTemplate.find('.attachmentListItemCreated').text(new Date(attachment.created).toLocaleString());
attachmentTemplate.find('.viewAttachmentButton').on('click', () => openFilePopup(attachment)); attachmentTemplate.find('.viewAttachmentButton').on('click', () => openFilePopup(attachment));
attachmentTemplate.find('.editAttachmentButton').on('click', () => editAttachment(attachment, source, renderAttachments));
attachmentTemplate.find('.deleteAttachmentButton').on('click', () => deleteAttachment(attachment, source, renderAttachments)); attachmentTemplate.find('.deleteAttachmentButton').on('click', () => deleteAttachment(attachment, source, renderAttachments));
template.find(sources[source]).append(attachmentTemplate); template.find(sources[source]).append(attachmentTemplate);
} }

View File

@ -102,6 +102,7 @@
<small class="attachmentListItemCreated"></small> <small class="attachmentListItemCreated"></small>
<small class="attachmentListItemSize"></small> <small class="attachmentListItemSize"></small>
<div class="viewAttachmentButton right_menu_button fa-solid fa-magnifying-glass" title="View attachment content"></div> <div class="viewAttachmentButton right_menu_button fa-solid fa-magnifying-glass" title="View attachment content"></div>
<div class="editAttachmentButton right_menu_button fa-solid fa-pencil" title="Edit attachment"></div>
<div class="deleteAttachmentButton right_menu_button fa-solid fa-trash" title="Delete attachment"></div> <div class="deleteAttachmentButton right_menu_button fa-solid fa-trash" title="Delete attachment"></div>
</div> </div>
</div> </div>