From 993284f9c135ecc2029cec65aba4059c928f14f7 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Tue, 30 Apr 2024 00:06:14 +0300 Subject: [PATCH] #2164 Disable-able data bank attachments --- public/scripts/chats.js | 54 +++++++++++++++++-- public/scripts/extensions.js | 10 ++++ .../extensions/attachments/manager.html | 12 +++-- .../scripts/extensions/attachments/style.css | 10 ++++ 4 files changed, 78 insertions(+), 8 deletions(-) diff --git a/public/scripts/chats.js b/public/scripts/chats.js index fd1bdcca1..3187276d7 100644 --- a/public/scripts/chats.js +++ b/public/scripts/chats.js @@ -685,6 +685,30 @@ async function downloadAttachment(attachment) { URL.revokeObjectURL(url); } +/** + * Removes an attachment from the disabled list. + * @param {FileAttachment} attachment Attachment to enable + * @param {function} callback Success callback + */ +function enableAttachment(attachment, callback) { + ensureAttachmentsExist(); + extension_settings.disabled_attachments = extension_settings.disabled_attachments.filter(url => url !== attachment.url); + saveSettingsDebounced(); + callback(); +} + +/** + * Adds an attachment to the disabled list. + * @param {FileAttachment} attachment Attachment to disable + * @param {function} callback Success callback + */ +function disableAttachment(attachment, callback) { + ensureAttachmentsExist(); + extension_settings.disabled_attachments.push(attachment.url); + saveSettingsDebounced(); + callback(); +} + /** * Moves a file attachment to a different source. * @param {FileAttachment} attachment Attachment to moves @@ -752,11 +776,25 @@ async function deleteAttachment(attachment, source, callback, confirm = true) { break; } + if (Array.isArray(extension_settings.disabled_attachments) && extension_settings.disabled_attachments.includes(attachment.url)) { + extension_settings.disabled_attachments = extension_settings.disabled_attachments.filter(url => url !== attachment.url); + saveSettingsDebounced(); + } + const silent = confirm === false; await deleteFileFromServer(attachment.url, silent); callback(); } +/** + * Determines if the attachment is disabled. + * @param {FileAttachment} attachment Attachment to check + * @returns {boolean} True if attachment is disabled, false otherwise. + */ +function isAttachmentDisabled(attachment) { + return extension_settings.disabled_attachments.some(url => url === attachment?.url); +} + /** * Opens the attachment manager. */ @@ -806,7 +844,9 @@ async function openAttachmentManager() { const sortedAttachmentList = attachments.slice().filter(filterFn).sort(sortFn); for (const attachment of sortedAttachmentList) { + const isDisabled = isAttachmentDisabled(attachment); const attachmentTemplate = template.find('.attachmentListItemTemplate .attachmentListItem').clone(); + attachmentTemplate.toggleClass('disabled', isDisabled); attachmentTemplate.find('.attachmentFileIcon').attr('title', attachment.url); attachmentTemplate.find('.attachmentListItemName').text(attachment.name); attachmentTemplate.find('.attachmentListItemSize').text(humanFileSize(attachment.size)); @@ -816,6 +856,8 @@ async function openAttachmentManager() { attachmentTemplate.find('.deleteAttachmentButton').on('click', () => deleteAttachment(attachment, source, renderAttachments)); attachmentTemplate.find('.downloadAttachmentButton').on('click', () => downloadAttachment(attachment)); attachmentTemplate.find('.moveAttachmentButton').on('click', () => moveAttachment(attachment, source, renderAttachments)); + attachmentTemplate.find('.enableAttachmentButton').toggle(isDisabled).on('click', () => enableAttachment(attachment, renderAttachments)); + attachmentTemplate.find('.disableAttachmentButton').toggle(!isDisabled).on('click', () => disableAttachment(attachment, renderAttachments)); template.find(sources[source]).append(attachmentTemplate); } } @@ -1117,6 +1159,10 @@ export async function uploadFileAttachmentToServer(file, target) { } function ensureAttachmentsExist() { + if (!Array.isArray(extension_settings.disabled_attachments)) { + extension_settings.disabled_attachments = []; + } + if (!Array.isArray(extension_settings.attachments)) { extension_settings.attachments = []; } @@ -1137,7 +1183,7 @@ function ensureAttachmentsExist() { } /** - * Gets all currently available attachments. + * Gets all currently available attachments. Ignores disabled attachments. * @returns {FileAttachment[]} List of attachments */ export function getDataBankAttachments() { @@ -1146,11 +1192,11 @@ export function getDataBankAttachments() { const chatAttachments = chat_metadata.attachments ?? []; const characterAttachments = extension_settings.character_attachments?.[characters[this_chid]?.avatar] ?? []; - return [...globalAttachments, ...chatAttachments, ...characterAttachments]; + return [...globalAttachments, ...chatAttachments, ...characterAttachments].filter(x => !isAttachmentDisabled(x)); } /** - * Gets all attachments for a specific source. + * Gets all attachments for a specific source. Includes disabled attachments. * @param {string} source Attachment source * @returns {FileAttachment[]} List of attachments */ @@ -1165,6 +1211,8 @@ export function getDataBankAttachmentsForSource(source) { case ATTACHMENT_SOURCE.CHARACTER: return extension_settings.character_attachments?.[characters[this_chid]?.avatar] ?? []; } + + return []; } /** diff --git a/public/scripts/extensions.js b/public/scripts/extensions.js index 6f309df69..6e1fceb30 100644 --- a/public/scripts/extensions.js +++ b/public/scripts/extensions.js @@ -145,8 +145,18 @@ const extension_settings = { variables: { global: {}, }, + /** + * @type {import('./chats.js').FileAttachment[]} + */ attachments: [], + /** + * @type {Record} + */ character_attachments: {}, + /** + * @type {string[]} + */ + disabled_attachments: [], }; let modules = []; diff --git a/public/scripts/extensions/attachments/manager.html b/public/scripts/extensions/attachments/manager.html index 95f507f7a..ea1ce807d 100644 --- a/public/scripts/extensions/attachments/manager.html +++ b/public/scripts/extensions/attachments/manager.html @@ -106,11 +106,13 @@
-
-
-
-
-
+
+
+
+
+
+
+
diff --git a/public/scripts/extensions/attachments/style.css b/public/scripts/extensions/attachments/style.css index ed13e8bd5..3c46e7cc9 100644 --- a/public/scripts/extensions/attachments/style.css +++ b/public/scripts/extensions/attachments/style.css @@ -19,6 +19,16 @@ padding: 10px; } +.attachmentListItem.disabled .attachmentListItemName { + text-decoration: line-through; + opacity: 0.75; +} + +.attachmentListItem.disabled .attachmentFileIcon { + opacity: 0.75; + cursor: not-allowed; +} + .attachmentListItemSize { min-width: 4em; text-align: right;