2022-05-05 08:06:35 +02:00
|
|
|
|
// import {
|
|
|
|
|
// CHAT_INITIAL_PLACEHOLDER_TEXT,
|
|
|
|
|
// CHAT_PLACEHOLDER_TEXT,
|
|
|
|
|
// CHAT_PLACEHOLDER_OFFLINE,
|
|
|
|
|
// } from './constants.js';
|
2020-08-13 10:49:10 +02:00
|
|
|
|
|
2021-07-23 20:20:59 +02:00
|
|
|
|
// Taken from https://stackoverflow.com/a/46902361
|
|
|
|
|
export function getCaretPosition(node) {
|
2022-03-13 23:36:53 +01:00
|
|
|
|
const selection = window.getSelection();
|
|
|
|
|
|
|
|
|
|
if (selection.rangeCount === 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const range = selection.getRangeAt(0);
|
|
|
|
|
const preCaretRange = range.cloneRange();
|
|
|
|
|
const tempElement = document.createElement('div');
|
2021-07-23 20:20:59 +02:00
|
|
|
|
|
|
|
|
|
preCaretRange.selectNodeContents(node);
|
|
|
|
|
preCaretRange.setEnd(range.endContainer, range.endOffset);
|
2022-03-13 23:36:53 +01:00
|
|
|
|
tempElement.appendChild(preCaretRange.cloneContents());
|
|
|
|
|
|
|
|
|
|
return tempElement.innerHTML.length;
|
2020-08-13 11:43:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-17 10:12:21 +02:00
|
|
|
|
// Might not need this anymore
|
2020-08-13 11:43:41 +02:00
|
|
|
|
// Pieced together from parts of https://stackoverflow.com/questions/6249095/how-to-set-caretcursor-position-in-contenteditable-element-div
|
|
|
|
|
export function setCaretPosition(editableDiv, position) {
|
2022-05-05 08:06:35 +02:00
|
|
|
|
const range = document.createRange();
|
|
|
|
|
const sel = window.getSelection();
|
2021-07-20 04:22:29 +02:00
|
|
|
|
range.selectNode(editableDiv);
|
|
|
|
|
range.setStart(editableDiv.childNodes[0], position);
|
|
|
|
|
range.collapse(true);
|
|
|
|
|
|
|
|
|
|
sel.removeAllRanges();
|
|
|
|
|
sel.addRange(range);
|
2020-08-13 11:43:41 +02:00
|
|
|
|
}
|
2020-08-14 13:19:19 +02:00
|
|
|
|
|
2022-05-05 08:06:35 +02:00
|
|
|
|
// export function generatePlaceholderText(isEnabled, hasSentFirstChatMessage) {
|
|
|
|
|
// if (isEnabled) {
|
|
|
|
|
// return hasSentFirstChatMessage
|
|
|
|
|
// ? CHAT_PLACEHOLDER_TEXT
|
|
|
|
|
// : CHAT_INITIAL_PLACEHOLDER_TEXT;
|
|
|
|
|
// }
|
|
|
|
|
// return CHAT_PLACEHOLDER_OFFLINE;
|
|
|
|
|
// }
|
2020-08-16 03:41:31 +02:00
|
|
|
|
|
|
|
|
|
export function extraUserNamesFromMessageHistory(messages) {
|
|
|
|
|
const list = [];
|
|
|
|
|
if (messages) {
|
2022-01-12 22:53:10 +01:00
|
|
|
|
messages
|
2022-05-05 08:06:35 +02:00
|
|
|
|
.filter(m => m.user && m.user.displayName)
|
|
|
|
|
.forEach(message => {
|
2022-01-12 22:53:10 +01:00
|
|
|
|
if (!list.includes(message.user.displayName)) {
|
|
|
|
|
list.push(message.user.displayName);
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-08-16 03:41:31 +02:00
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
2020-08-26 20:56:01 +02:00
|
|
|
|
|
|
|
|
|
// utils from https://gist.github.com/nathansmith/86b5d4b23ed968a92fd4
|
|
|
|
|
/*
|
|
|
|
|
You would call this after getting an element's
|
|
|
|
|
`.innerHTML` value, while the user is typing.
|
|
|
|
|
*/
|
|
|
|
|
export function convertToText(str = '') {
|
|
|
|
|
// Ensure string.
|
|
|
|
|
let value = String(str);
|
|
|
|
|
|
|
|
|
|
// Convert encoding.
|
|
|
|
|
value = value.replace(/ /gi, ' ');
|
|
|
|
|
value = value.replace(/&/gi, '&');
|
|
|
|
|
|
|
|
|
|
// Replace `<br>`.
|
|
|
|
|
value = value.replace(/<br>/gi, '\n');
|
|
|
|
|
|
|
|
|
|
// Replace `<div>` (from Chrome).
|
|
|
|
|
value = value.replace(/<div>/gi, '\n');
|
|
|
|
|
|
|
|
|
|
// Replace `<p>` (from IE).
|
|
|
|
|
value = value.replace(/<p>/gi, '\n');
|
|
|
|
|
|
2021-07-20 04:22:29 +02:00
|
|
|
|
// Cleanup the emoji titles.
|
|
|
|
|
value = value.replace(/\u200C{2}/gi, '');
|
|
|
|
|
|
2020-08-26 20:56:01 +02:00
|
|
|
|
// Trim each line.
|
|
|
|
|
value = value
|
|
|
|
|
.split('\n')
|
2022-05-05 08:06:35 +02:00
|
|
|
|
.map((line = '') => line.trim())
|
2020-08-26 20:56:01 +02:00
|
|
|
|
.join('\n');
|
|
|
|
|
|
|
|
|
|
// No more than 2x newline, per "paragraph".
|
|
|
|
|
value = value.replace(/\n\n+/g, '\n\n');
|
|
|
|
|
|
|
|
|
|
// Clean up spaces.
|
|
|
|
|
value = value.replace(/[ ]+/g, ' ');
|
|
|
|
|
value = value.trim();
|
|
|
|
|
|
|
|
|
|
// Expose string.
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-20 04:22:29 +02:00
|
|
|
|
export function createEmojiMarkup(data, isCustom) {
|
|
|
|
|
const emojiUrl = isCustom ? data.emoji : data.url;
|
2021-07-20 04:23:06 +02:00
|
|
|
|
const emojiName = (
|
2022-05-05 08:06:35 +02:00
|
|
|
|
isCustom ? data.name : data.url.split('\\').pop().split('/').pop().split('.').shift()
|
2021-07-20 04:23:06 +02:00
|
|
|
|
).toLowerCase();
|
2022-05-05 08:06:35 +02:00
|
|
|
|
return `<img class="emoji" alt=":${emojiName}:" title=":${emojiName}:" src="${emojiUrl}"/>`;
|
2021-07-20 04:22:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// trim html white space characters from ends of messages for more accurate counting
|
|
|
|
|
export function trimNbsp(html) {
|
2021-07-20 04:23:06 +02:00
|
|
|
|
return html.replace(/^(?: |\s)+|(?: |\s)+$/gi, '');
|
2021-07-20 04:22:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function emojify(HTML, emojiList) {
|
2021-07-20 04:23:06 +02:00
|
|
|
|
const textValue = convertToText(HTML);
|
2021-07-20 04:22:29 +02:00
|
|
|
|
|
2022-05-12 08:31:31 +02:00
|
|
|
|
// eslint-disable-next-line no-plusplus
|
2022-05-05 08:06:35 +02:00
|
|
|
|
for (let lastPos = textValue.length; lastPos >= 0; lastPos--) {
|
2021-07-20 04:22:29 +02:00
|
|
|
|
const endPos = textValue.lastIndexOf(':', lastPos);
|
|
|
|
|
if (endPos <= 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
const startPos = textValue.lastIndexOf(':', endPos - 1);
|
|
|
|
|
if (startPos === -1) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
const typedEmoji = textValue.substring(startPos + 1, endPos).trim();
|
2022-05-05 08:06:35 +02:00
|
|
|
|
const emojiIndex = emojiList.findIndex(
|
|
|
|
|
emojiItem => emojiItem.name.toLowerCase() === typedEmoji.toLowerCase(),
|
|
|
|
|
);
|
2021-07-20 04:22:29 +02:00
|
|
|
|
|
2022-05-12 08:31:31 +02:00
|
|
|
|
if (emojiIndex !== -1) {
|
2021-07-20 04:23:06 +02:00
|
|
|
|
const emojiImgElement = createEmojiMarkup(emojiList[emojiIndex], true);
|
2022-05-12 08:31:31 +02:00
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
2022-05-05 08:06:35 +02:00
|
|
|
|
HTML = HTML.replace(`:${typedEmoji}:`, emojiImgElement);
|
2021-07-20 04:22:29 +02:00
|
|
|
|
}
|
2020-08-26 20:56:01 +02:00
|
|
|
|
}
|
2021-07-20 04:22:29 +02:00
|
|
|
|
return HTML;
|
2020-08-26 20:56:01 +02:00
|
|
|
|
}
|
2021-11-03 03:27:41 +01:00
|
|
|
|
|
|
|
|
|
// MODERATOR UTILS
|
|
|
|
|
export function checkIsModerator(message) {
|
|
|
|
|
const { user } = message;
|
|
|
|
|
const { scopes } = user;
|
|
|
|
|
|
|
|
|
|
if (!scopes || scopes.length === 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return scopes.includes('MODERATOR');
|
|
|
|
|
}
|