2021-02-24 15:06:20 +01:00
|
|
|
import { __ } from 'i18n';
|
2022-10-30 11:49:56 +01:00
|
|
|
import { _converse, api } from '@converse/headless/core';
|
2021-02-11 22:32:55 +01:00
|
|
|
|
2021-06-15 09:26:06 +02:00
|
|
|
export function clearHistory (jid) {
|
|
|
|
if (_converse.router.history.getFragment() === `converse/chat?jid=${jid}`) {
|
|
|
|
_converse.router.navigate('');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-24 15:48:11 +01:00
|
|
|
export async function clearMessages (chat) {
|
2022-05-11 10:01:18 +02:00
|
|
|
const result = await api.confirm(__('Are you sure you want to clear the messages from this conversation?'));
|
|
|
|
if (result) {
|
2021-02-24 15:06:20 +01:00
|
|
|
await chat.clearMessages();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 16:22:15 +01:00
|
|
|
export async function parseMessageForCommands (chat, text) {
|
2021-02-24 15:06:20 +01:00
|
|
|
const match = text.replace(/^\s*/, '').match(/^\/(.*)\s*$/);
|
|
|
|
if (match) {
|
2022-01-26 16:22:15 +01:00
|
|
|
let handled = false;
|
2022-01-28 12:05:10 +01:00
|
|
|
/**
|
|
|
|
* *Hook* which allows plugins to add more commands to a chat's textbox.
|
|
|
|
* Data provided is the chatbox model and the text typed - {model, text}.
|
|
|
|
* Check `handled` to see if the hook was already handled.
|
|
|
|
* @event _converse#parseMessageForCommands
|
|
|
|
* @example
|
|
|
|
* api.listen.on('parseMessageForCommands', (data, handled) {
|
|
|
|
* if (!handled) {
|
|
|
|
* const command = (data.text.match(/^\/([a-zA-Z]*) ?/) || ['']).pop().toLowerCase();
|
|
|
|
* // custom code comes here
|
|
|
|
* }
|
|
|
|
* return handled;
|
|
|
|
* }
|
|
|
|
*/
|
2022-10-30 11:49:56 +01:00
|
|
|
handled = await api.hook('parseMessageForCommands', { model: chat, text }, handled);
|
2022-01-26 16:22:15 +01:00
|
|
|
if (handled) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-24 15:06:20 +01:00
|
|
|
if (match[1] === 'clear') {
|
|
|
|
clearMessages(chat);
|
|
|
|
return true;
|
|
|
|
} else if (match[1] === 'close') {
|
|
|
|
_converse.chatboxviews.get(chat.get('jid'))?.close();
|
|
|
|
return true;
|
|
|
|
} else if (match[1] === 'help') {
|
2021-02-24 16:34:46 +01:00
|
|
|
chat.set({ 'show_help_messages': false }, { 'silent': true });
|
2021-02-24 15:06:20 +01:00
|
|
|
chat.set({ 'show_help_messages': true });
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 16:22:15 +01:00
|
|
|
return false;
|
2021-02-24 15:06:20 +01:00
|
|
|
}
|
2021-06-03 19:11:32 +02:00
|
|
|
|
|
|
|
export function resetElementHeight (ev) {
|
|
|
|
if (ev.target.value) {
|
|
|
|
const height = ev.target.scrollHeight + 'px';
|
|
|
|
if (ev.target.style.height != height) {
|
|
|
|
ev.target.style.height = 'auto';
|
|
|
|
ev.target.style.height = height;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ev.target.style = '';
|
|
|
|
}
|
|
|
|
}
|