From 4e436c00f00d72eeb11f46a4c2c3ef1e02c9dcf0 Mon Sep 17 00:00:00 2001 From: John Livingston Date: Thu, 12 Sep 2024 12:07:55 +0200 Subject: [PATCH] New features: announcements WIP (#518): * handling message correction. --- conversejs/builtin.ts | 5 ++++ .../lib/plugins/livechat-announcements.ts | 23 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/conversejs/builtin.ts b/conversejs/builtin.ts index 19ff31f0..08b490c5 100644 --- a/conversejs/builtin.ts +++ b/conversejs/builtin.ts @@ -39,6 +39,11 @@ declare global { sizzle: Function dayjs: Function __: Function + u: { + hasClass: Function + addClass: Function + removeClass: Function + } } } initConversePlugins: typeof initConversePlugins diff --git a/conversejs/lib/plugins/livechat-announcements.ts b/conversejs/lib/plugins/livechat-announcements.ts index 8c52cb67..0f66f045 100644 --- a/conversejs/lib/plugins/livechat-announcements.ts +++ b/conversejs/lib/plugins/livechat-announcements.ts @@ -45,6 +45,8 @@ export const livechatAnnouncementsPlugin = { /** * Overloads the MUCMessageForm to handle the announcement type field (if present) when sending a message. + * + * Also hides the announcement type field if we are correcting a previous message. */ function overrideMUCMessageForm (_converse: any, current: Current): void { const MUCMessageForm = _converse.api.elements.registry['converse-muc-message-form'] @@ -61,6 +63,27 @@ function overrideMUCMessageForm (_converse: any, current: Current): void { } current.announcementType = undefined } + + insertIntoTextArea (...args: any[]): void { + super.insertIntoTextArea(...args) + try { + // FIXME: doing this here is not very clean. + // But that's how ConverseJS adds or removes the 'correction' class to the textarea. + const textarea = this.querySelector('.chat-textarea') + if (!textarea) { return } + const correcting = window.converse.env.u.hasClass('correcting', textarea) + const announcementForm = this.querySelector('.livechat-announcements-form') + const announcementSelect = this.querySelector('[name=livechat-announcements]') + if (correcting) { + if (announcementSelect) { announcementSelect.selectedIndex = 0 } + if (announcementForm) { announcementForm.style.display = 'none' } + } else { + if (announcementForm) { announcementForm.style.display = 'block' } + } + } catch (err) { + console.error(err) + } + } } _converse.api.elements.define('converse-muc-message-form', MUCMessageFormloaded) }