2021-02-24 15:06:20 +01:00
|
|
|
import { __ } from 'i18n';
|
2022-04-19 21:54:10 +02:00
|
|
|
import { _converse, api } from "@converse/headless/core";
|
2021-04-14 22:56:59 +02:00
|
|
|
import { html } from 'lit';
|
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-11 22:32:55 +01:00
|
|
|
export async function getHeadingDropdownItem (promise_or_data) {
|
|
|
|
const data = await promise_or_data;
|
2021-11-14 09:09:58 +01:00
|
|
|
return data ? html`
|
2021-07-27 11:02:51 +02:00
|
|
|
<a href="#" class="dropdown-item ${data.a_class}" @click=${data.handler} title="${data.i18n_title}">
|
|
|
|
<converse-icon size="1em" color="var(--text-color-lighten-15-percent)" class="fa ${data.icon_class}"></converse-icon>
|
|
|
|
${data.i18n_text}
|
|
|
|
</a>
|
2021-11-14 09:09:58 +01:00
|
|
|
` : '';
|
2021-02-11 22:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getHeadingStandaloneButton (promise_or_data) {
|
|
|
|
const data = await promise_or_data;
|
|
|
|
return html`
|
|
|
|
<a
|
|
|
|
href="#"
|
|
|
|
class="chatbox-btn ${data.a_class} fa ${data.icon_class}"
|
|
|
|
@click=${data.handler}
|
|
|
|
title="${data.i18n_title}"
|
|
|
|
></a>
|
|
|
|
`;
|
|
|
|
}
|
2021-02-24 15:06:20 +01:00
|
|
|
|
2021-02-24 15:48:11 +01:00
|
|
|
export async function clearMessages (chat) {
|
2021-02-24 15:06:20 +01:00
|
|
|
const result = confirm(__('Are you sure you want to clear the messages from this conversation?'));
|
|
|
|
if (result === true) {
|
|
|
|
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-01-26 16:22:15 +01:00
|
|
|
handled = await api.hook('parseMessageForCommands', {model: chat, text}, handled);
|
|
|
|
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 = '';
|
|
|
|
}
|
|
|
|
}
|