2023-11-03 22:45:56 +01:00
|
|
|
// Move chat functions here from script.js (eventually)
|
|
|
|
|
2023-12-20 16:03:37 +01:00
|
|
|
import css from '../lib/css-parser.mjs';
|
2023-11-03 22:45:56 +01:00
|
|
|
import {
|
2023-11-18 18:23:58 +01:00
|
|
|
addCopyToCodeBlocks,
|
|
|
|
appendMediaToMessage,
|
|
|
|
callPopup,
|
2024-04-04 23:39:54 +02:00
|
|
|
characters,
|
2023-11-03 22:45:56 +01:00
|
|
|
chat,
|
2023-11-18 18:23:58 +01:00
|
|
|
eventSource,
|
|
|
|
event_types,
|
2023-11-03 22:45:56 +01:00
|
|
|
getCurrentChatId,
|
2023-11-29 16:51:30 +01:00
|
|
|
getRequestHeaders,
|
2023-11-03 22:45:56 +01:00
|
|
|
hideSwipeButtons,
|
2023-11-18 18:23:58 +01:00
|
|
|
name2,
|
2024-04-04 23:39:54 +02:00
|
|
|
reloadCurrentChat,
|
2023-11-14 20:37:37 +01:00
|
|
|
saveChatDebounced,
|
2024-04-04 23:39:54 +02:00
|
|
|
saveSettingsDebounced,
|
2023-11-03 22:45:56 +01:00
|
|
|
showSwipeButtons,
|
2024-04-04 23:39:54 +02:00
|
|
|
this_chid,
|
2023-12-02 19:04:51 +01:00
|
|
|
} from '../script.js';
|
2024-04-04 23:39:54 +02:00
|
|
|
import { selected_group } from './group-chats.js';
|
|
|
|
import { power_user } from './power-user.js';
|
2023-11-29 16:51:30 +01:00
|
|
|
import {
|
|
|
|
extractTextFromHTML,
|
|
|
|
extractTextFromMarkdown,
|
|
|
|
extractTextFromPDF,
|
|
|
|
getBase64Async,
|
|
|
|
getStringHash,
|
|
|
|
humanFileSize,
|
|
|
|
saveBase64AsFile,
|
2023-12-02 19:04:51 +01:00
|
|
|
} from './utils.js';
|
2023-11-29 16:51:30 +01:00
|
|
|
|
|
|
|
const fileSizeLimit = 1024 * 1024 * 10; // 10 MB
|
|
|
|
|
|
|
|
const converters = {
|
|
|
|
'application/pdf': extractTextFromPDF,
|
|
|
|
'text/html': extractTextFromHTML,
|
|
|
|
'text/markdown': extractTextFromMarkdown,
|
2023-12-02 20:11:06 +01:00
|
|
|
};
|
2023-11-18 18:23:58 +01:00
|
|
|
|
2023-11-29 16:51:30 +01:00
|
|
|
function isConvertible(type) {
|
|
|
|
return Object.keys(converters).includes(type);
|
|
|
|
}
|
2023-11-03 22:45:56 +01:00
|
|
|
|
|
|
|
/**
|
2024-04-12 09:03:50 +02:00
|
|
|
* Mark a range of messages as hidden ("is_system") or not.
|
|
|
|
* @param {number} start Starting message ID
|
|
|
|
* @param {number} end Ending message ID (inclusive)
|
|
|
|
* @param {boolean} unhide If true, unhide the messages instead.
|
2024-04-12 13:18:59 +02:00
|
|
|
* @returns {Promise<void>}
|
2023-11-03 22:45:56 +01:00
|
|
|
*/
|
2024-04-12 09:03:50 +02:00
|
|
|
export async function hideChatMessageRange(start, end, unhide) {
|
|
|
|
if (!getCurrentChatId()) return;
|
2023-11-03 22:45:56 +01:00
|
|
|
|
2024-04-12 09:03:50 +02:00
|
|
|
if (isNaN(start)) return;
|
|
|
|
if (!end) end = start;
|
|
|
|
const hide = !unhide;
|
2023-11-03 22:45:56 +01:00
|
|
|
|
2024-04-12 09:03:50 +02:00
|
|
|
for (let messageId = start; messageId <= end; messageId++) {
|
|
|
|
const message = chat[messageId];
|
|
|
|
if (!message) continue;
|
2023-11-03 22:45:56 +01:00
|
|
|
|
2024-04-12 09:03:50 +02:00
|
|
|
const messageBlock = $(`.mes[mesid="${messageId}"]`);
|
|
|
|
if (!messageBlock.length) continue;
|
2023-11-03 22:45:56 +01:00
|
|
|
|
2024-04-12 09:03:50 +02:00
|
|
|
message.is_system = hide;
|
|
|
|
messageBlock.attr('is_system', String(hide));
|
|
|
|
}
|
2023-11-03 22:45:56 +01:00
|
|
|
|
|
|
|
// Reload swipes. Useful when a last message is hidden.
|
|
|
|
hideSwipeButtons();
|
|
|
|
showSwipeButtons();
|
|
|
|
|
2023-11-14 20:37:37 +01:00
|
|
|
saveChatDebounced();
|
2023-11-03 22:45:56 +01:00
|
|
|
}
|
|
|
|
|
2024-04-12 13:05:58 +02:00
|
|
|
/**
|
|
|
|
* Mark message as hidden (system message).
|
|
|
|
* @deprecated Use hideChatMessageRange.
|
|
|
|
* @param {number} messageId Message ID
|
|
|
|
* @param {JQuery<Element>} _messageBlock Unused
|
2024-04-12 13:18:59 +02:00
|
|
|
* @returns {Promise<void>}
|
2024-04-12 13:05:58 +02:00
|
|
|
*/
|
|
|
|
export async function hideChatMessage(messageId, _messageBlock) {
|
2024-04-12 13:18:59 +02:00
|
|
|
return hideChatMessageRange(messageId, messageId, false);
|
2024-04-12 13:05:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark message as visible (non-system message).
|
|
|
|
* @deprecated Use hideChatMessageRange.
|
|
|
|
* @param {number} messageId Message ID
|
|
|
|
* @param {JQuery<Element>} _messageBlock Unused
|
2024-04-12 13:18:59 +02:00
|
|
|
* @returns {Promise<void>}
|
2024-04-12 13:05:58 +02:00
|
|
|
*/
|
|
|
|
export async function unhideChatMessage(messageId, _messageBlock) {
|
2024-04-12 13:18:59 +02:00
|
|
|
return hideChatMessageRange(messageId, messageId, true);
|
2024-04-12 13:05:58 +02:00
|
|
|
}
|
|
|
|
|
2023-11-18 18:23:58 +01:00
|
|
|
/**
|
|
|
|
* Adds a file attachment to the message.
|
|
|
|
* @param {object} message Message object
|
2023-11-29 16:51:30 +01:00
|
|
|
* @returns {Promise<void>} A promise that resolves when file is uploaded.
|
2023-11-18 18:23:58 +01:00
|
|
|
*/
|
|
|
|
export async function populateFileAttachment(message, inputId = 'file_form_input') {
|
|
|
|
try {
|
|
|
|
if (!message) return;
|
|
|
|
if (!message.extra) message.extra = {};
|
|
|
|
const fileInput = document.getElementById(inputId);
|
|
|
|
if (!(fileInput instanceof HTMLInputElement)) return;
|
|
|
|
const file = fileInput.files[0];
|
|
|
|
if (!file) return;
|
|
|
|
|
2023-11-29 16:51:30 +01:00
|
|
|
const fileBase64 = await getBase64Async(file);
|
|
|
|
let base64Data = fileBase64.split(',')[1];
|
|
|
|
|
2023-11-18 18:23:58 +01:00
|
|
|
// If file is image
|
|
|
|
if (file.type.startsWith('image/')) {
|
|
|
|
const extension = file.type.split('/')[1];
|
2023-11-29 16:51:30 +01:00
|
|
|
const imageUrl = await saveBase64AsFile(base64Data, name2, file.name, extension);
|
2023-11-18 18:23:58 +01:00
|
|
|
message.extra.image = imageUrl;
|
|
|
|
message.extra.inline_image = true;
|
|
|
|
} else {
|
2023-11-29 16:51:30 +01:00
|
|
|
const slug = getStringHash(file.name);
|
|
|
|
const uniqueFileName = `${Date.now()}_${slug}.txt`;
|
|
|
|
|
|
|
|
if (isConvertible(file.type)) {
|
|
|
|
try {
|
|
|
|
const converter = converters[file.type];
|
|
|
|
const fileText = await converter(file);
|
|
|
|
base64Data = window.btoa(unescape(encodeURIComponent(fileText)));
|
|
|
|
} catch (error) {
|
2023-11-29 23:01:59 +01:00
|
|
|
toastr.error(String(error), 'Could not convert file');
|
2023-11-29 16:51:30 +01:00
|
|
|
console.error('Could not convert file', error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const fileUrl = await uploadFileAttachment(uniqueFileName, base64Data);
|
|
|
|
|
|
|
|
if (!fileUrl) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-18 18:23:58 +01:00
|
|
|
message.extra.file = {
|
2023-11-29 16:51:30 +01:00
|
|
|
url: fileUrl,
|
2023-11-18 18:23:58 +01:00
|
|
|
size: file.size,
|
|
|
|
name: file.name,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Could not upload file', error);
|
|
|
|
} finally {
|
|
|
|
$('#file_form').trigger('reset');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-29 16:51:30 +01:00
|
|
|
/**
|
|
|
|
* Uploads file to the server.
|
|
|
|
* @param {string} fileName
|
|
|
|
* @param {string} base64Data
|
|
|
|
* @returns {Promise<string>} File URL
|
|
|
|
*/
|
|
|
|
export async function uploadFileAttachment(fileName, base64Data) {
|
|
|
|
try {
|
2023-12-04 18:40:53 +01:00
|
|
|
const result = await fetch('/api/files/upload', {
|
2023-11-29 16:51:30 +01:00
|
|
|
method: 'POST',
|
|
|
|
headers: getRequestHeaders(),
|
|
|
|
body: JSON.stringify({
|
|
|
|
name: fileName,
|
|
|
|
data: base64Data,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!result.ok) {
|
|
|
|
const error = await result.text();
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
const responseData = await result.json();
|
2023-12-05 23:55:52 +01:00
|
|
|
return responseData.path;
|
2023-11-29 16:51:30 +01:00
|
|
|
} catch (error) {
|
2023-11-29 23:01:59 +01:00
|
|
|
toastr.error(String(error), 'Could not upload file');
|
2023-11-29 16:51:30 +01:00
|
|
|
console.error('Could not upload file', error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Downloads file from the server.
|
|
|
|
* @param {string} url File URL
|
|
|
|
* @returns {Promise<string>} File text
|
|
|
|
*/
|
|
|
|
export async function getFileAttachment(url) {
|
|
|
|
try {
|
|
|
|
const result = await fetch(url, {
|
|
|
|
method: 'GET',
|
|
|
|
cache: 'force-cache',
|
|
|
|
headers: getRequestHeaders(),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!result.ok) {
|
|
|
|
const error = await result.text();
|
|
|
|
throw new Error(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
const text = await result.text();
|
|
|
|
return text;
|
|
|
|
} catch (error) {
|
|
|
|
toastr.error(error, 'Could not download file');
|
|
|
|
console.error('Could not download file', error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-18 18:23:58 +01:00
|
|
|
/**
|
|
|
|
* Validates file to make sure it is not binary or not image.
|
|
|
|
* @param {File} file File object
|
|
|
|
* @returns {Promise<boolean>} True if file is valid, false otherwise.
|
|
|
|
*/
|
|
|
|
async function validateFile(file) {
|
|
|
|
const fileText = await file.text();
|
|
|
|
const isImage = file.type.startsWith('image/');
|
|
|
|
const isBinary = /^[\x00-\x08\x0E-\x1F\x7F-\xFF]*$/.test(fileText);
|
|
|
|
|
|
|
|
if (!isImage && file.size > fileSizeLimit) {
|
|
|
|
toastr.error(`File is too big. Maximum size is ${humanFileSize(fileSizeLimit)}.`);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If file is binary
|
2023-11-29 16:51:30 +01:00
|
|
|
if (isBinary && !isImage && !isConvertible(file.type)) {
|
2023-11-18 18:23:58 +01:00
|
|
|
toastr.error('Binary files are not supported. Select a text file or image.');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hasPendingFileAttachment() {
|
|
|
|
const fileInput = document.getElementById('file_form_input');
|
|
|
|
if (!(fileInput instanceof HTMLInputElement)) return false;
|
|
|
|
const file = fileInput.files[0];
|
|
|
|
return !!file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays file information in the message sending form.
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
async function onFileAttach() {
|
|
|
|
const fileInput = document.getElementById('file_form_input');
|
|
|
|
if (!(fileInput instanceof HTMLInputElement)) return;
|
|
|
|
const file = fileInput.files[0];
|
|
|
|
if (!file) return;
|
|
|
|
|
|
|
|
const isValid = await validateFile(file);
|
|
|
|
|
|
|
|
// If file is binary
|
|
|
|
if (!isValid) {
|
|
|
|
$('#file_form').trigger('reset');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$('#file_form .file_name').text(file.name);
|
|
|
|
$('#file_form .file_size').text(humanFileSize(file.size));
|
|
|
|
$('#file_form').removeClass('displayNone');
|
|
|
|
|
|
|
|
// Reset form on chat change
|
|
|
|
eventSource.once(event_types.CHAT_CHANGED, () => {
|
|
|
|
$('#file_form').trigger('reset');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes file from message.
|
|
|
|
* @param {number} messageId Message ID
|
|
|
|
*/
|
|
|
|
async function deleteMessageFile(messageId) {
|
|
|
|
const confirm = await callPopup('Are you sure you want to delete this file?', 'confirm');
|
|
|
|
|
|
|
|
if (!confirm) {
|
|
|
|
console.debug('Delete file cancelled');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const message = chat[messageId];
|
|
|
|
|
|
|
|
if (!message?.extra?.file) {
|
|
|
|
console.debug('Message has no file');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete message.extra.file;
|
|
|
|
$(`.mes[mesid="${messageId}"] .mes_file_container`).remove();
|
|
|
|
saveChatDebounced();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens file from message in a modal.
|
|
|
|
* @param {number} messageId Message ID
|
|
|
|
*/
|
|
|
|
async function viewMessageFile(messageId) {
|
2023-11-29 16:51:30 +01:00
|
|
|
const messageFile = chat[messageId]?.extra?.file;
|
2023-11-18 18:23:58 +01:00
|
|
|
|
2023-11-29 16:51:30 +01:00
|
|
|
if (!messageFile) {
|
2023-11-18 18:23:58 +01:00
|
|
|
console.debug('Message has no file or it is empty');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-29 16:51:30 +01:00
|
|
|
const fileText = messageFile.text || (await getFileAttachment(messageFile.url));
|
|
|
|
|
2023-11-18 18:23:58 +01:00
|
|
|
const modalTemplate = $('<div><pre><code></code></pre></div>');
|
2023-11-29 16:51:30 +01:00
|
|
|
modalTemplate.find('code').addClass('txt').text(fileText);
|
2023-11-18 18:23:58 +01:00
|
|
|
modalTemplate.addClass('file_modal');
|
|
|
|
addCopyToCodeBlocks(modalTemplate);
|
|
|
|
|
2023-12-04 20:28:36 +01:00
|
|
|
callPopup(modalTemplate, 'text', '', { wide: true, large: true });
|
2023-11-18 18:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts a file embed into the message.
|
|
|
|
* @param {number} messageId
|
|
|
|
* @param {JQuery<HTMLElement>} messageBlock
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
function embedMessageFile(messageId, messageBlock) {
|
|
|
|
const message = chat[messageId];
|
|
|
|
|
|
|
|
if (!message) {
|
|
|
|
console.warn('Failed to find message with id', messageId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$('#embed_file_input')
|
|
|
|
.off('change')
|
|
|
|
.on('change', parseAndUploadEmbed)
|
|
|
|
.trigger('click');
|
|
|
|
|
|
|
|
async function parseAndUploadEmbed(e) {
|
|
|
|
const file = e.target.files[0];
|
|
|
|
if (!file) return;
|
|
|
|
|
|
|
|
const isValid = await validateFile(file);
|
|
|
|
|
|
|
|
if (!isValid) {
|
|
|
|
$('#file_form').trigger('reset');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await populateFileAttachment(message, 'embed_file_input');
|
|
|
|
appendMediaToMessage(message, messageBlock);
|
|
|
|
saveChatDebounced();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-12 00:08:47 +01:00
|
|
|
/**
|
|
|
|
* Appends file content to the message text.
|
|
|
|
* @param {object} message Message object
|
|
|
|
* @param {string} messageText Message text
|
|
|
|
* @returns {Promise<string>} Message text with file content appended.
|
|
|
|
*/
|
|
|
|
export async function appendFileContent(message, messageText) {
|
|
|
|
if (message.extra?.file) {
|
|
|
|
const fileText = message.extra.file.text || (await getFileAttachment(message.extra.file.url));
|
|
|
|
|
|
|
|
if (fileText) {
|
|
|
|
const fileWrapped = `\`\`\`\n${fileText}\n\`\`\`\n\n`;
|
|
|
|
message.extra.fileLength = fileWrapped.length;
|
|
|
|
messageText = fileWrapped + messageText;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return messageText;
|
|
|
|
}
|
|
|
|
|
2023-12-20 16:03:37 +01:00
|
|
|
/**
|
|
|
|
* Replaces style tags in the message text with custom tags with encoded content.
|
|
|
|
* @param {string} text
|
|
|
|
* @returns {string} Encoded message text
|
|
|
|
* @copyright https://github.com/kwaroran/risuAI
|
|
|
|
*/
|
|
|
|
export function encodeStyleTags(text) {
|
|
|
|
const styleRegex = /<style>(.+?)<\/style>/gms;
|
|
|
|
return text.replaceAll(styleRegex, (_, match) => {
|
|
|
|
return `<custom-style>${escape(match)}</custom-style>`;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanitizes custom style tags in the message text to prevent DOM pollution.
|
|
|
|
* @param {string} text Message text
|
|
|
|
* @returns {string} Sanitized message text
|
|
|
|
* @copyright https://github.com/kwaroran/risuAI
|
|
|
|
*/
|
|
|
|
export function decodeStyleTags(text) {
|
|
|
|
const styleDecodeRegex = /<custom-style>(.+?)<\/custom-style>/gms;
|
|
|
|
|
|
|
|
return text.replaceAll(styleDecodeRegex, (_, style) => {
|
|
|
|
try {
|
2024-04-17 06:15:55 +02:00
|
|
|
let styleCleaned = unescape(style).replaceAll(/<br\/>/g, '');
|
|
|
|
const ast = css.parse(styleCleaned);
|
2023-12-20 16:03:37 +01:00
|
|
|
const rules = ast?.stylesheet?.rules;
|
|
|
|
if (rules) {
|
|
|
|
for (const rule of rules) {
|
|
|
|
|
|
|
|
if (rule.type === 'rule') {
|
|
|
|
if (rule.selectors) {
|
|
|
|
for (let i = 0; i < rule.selectors.length; i++) {
|
|
|
|
let selector = rule.selectors[i];
|
|
|
|
if (selector) {
|
|
|
|
let selectors = (selector.split(' ') ?? []).map((v) => {
|
|
|
|
if (v.startsWith('.')) {
|
|
|
|
return '.custom-' + v.substring(1);
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}).join(' ');
|
|
|
|
|
|
|
|
rule.selectors[i] = '.mes_text ' + selectors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return `<style>${css.stringify(ast)}</style>`;
|
|
|
|
} catch (error) {
|
|
|
|
return `CSS ERROR: ${error}`;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-04-04 23:39:54 +02:00
|
|
|
async function openExternalMediaOverridesDialog() {
|
|
|
|
const entityId = getCurrentEntityId();
|
|
|
|
|
|
|
|
if (!entityId) {
|
|
|
|
toastr.info('No character or group selected');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const template = $('#forbid_media_override_template > .forbid_media_override').clone();
|
|
|
|
template.find('.forbid_media_global_state_forbidden').toggle(power_user.forbid_external_images);
|
|
|
|
template.find('.forbid_media_global_state_allowed').toggle(!power_user.forbid_external_images);
|
|
|
|
|
|
|
|
if (power_user.external_media_allowed_overrides.includes(entityId)) {
|
|
|
|
template.find('#forbid_media_override_allowed').prop('checked', true);
|
|
|
|
}
|
|
|
|
else if (power_user.external_media_forbidden_overrides.includes(entityId)) {
|
|
|
|
template.find('#forbid_media_override_forbidden').prop('checked', true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
template.find('#forbid_media_override_global').prop('checked', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
callPopup(template, 'text', '', { wide: false, large: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getCurrentEntityId() {
|
|
|
|
if (selected_group) {
|
|
|
|
return String(selected_group);
|
|
|
|
}
|
|
|
|
|
|
|
|
return characters[this_chid]?.avatar ?? null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isExternalMediaAllowed() {
|
|
|
|
const entityId = getCurrentEntityId();
|
|
|
|
if (!entityId) {
|
|
|
|
return !power_user.forbid_external_images;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (power_user.external_media_allowed_overrides.includes(entityId)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (power_user.external_media_forbidden_overrides.includes(entityId)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !power_user.forbid_external_images;
|
|
|
|
}
|
|
|
|
|
2023-11-18 18:23:58 +01:00
|
|
|
jQuery(function () {
|
|
|
|
$(document).on('click', '.mes_hide', async function () {
|
2023-11-03 22:45:56 +01:00
|
|
|
const messageBlock = $(this).closest('.mes');
|
|
|
|
const messageId = Number(messageBlock.attr('mesid'));
|
2024-04-12 13:18:59 +02:00
|
|
|
await hideChatMessageRange(messageId, messageId, false);
|
2023-11-03 22:45:56 +01:00
|
|
|
});
|
|
|
|
|
2023-11-18 18:23:58 +01:00
|
|
|
$(document).on('click', '.mes_unhide', async function () {
|
2023-11-03 22:45:56 +01:00
|
|
|
const messageBlock = $(this).closest('.mes');
|
|
|
|
const messageId = Number(messageBlock.attr('mesid'));
|
2024-04-12 09:03:50 +02:00
|
|
|
await hideChatMessageRange(messageId, messageId, true);
|
2023-11-03 22:45:56 +01:00
|
|
|
});
|
2023-11-18 18:23:58 +01:00
|
|
|
|
|
|
|
$(document).on('click', '.mes_file_delete', async function () {
|
|
|
|
const messageBlock = $(this).closest('.mes');
|
|
|
|
const messageId = Number(messageBlock.attr('mesid'));
|
|
|
|
await deleteMessageFile(messageId);
|
|
|
|
});
|
|
|
|
|
|
|
|
$(document).on('click', '.mes_file_open', async function () {
|
|
|
|
const messageBlock = $(this).closest('.mes');
|
|
|
|
const messageId = Number(messageBlock.attr('mesid'));
|
|
|
|
await viewMessageFile(messageId);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Do not change. #attachFile is added by extension.
|
|
|
|
$(document).on('click', '#attachFile', function () {
|
|
|
|
$('#file_form_input').trigger('click');
|
|
|
|
});
|
|
|
|
|
|
|
|
$(document).on('click', '.mes_embed', function () {
|
|
|
|
const messageBlock = $(this).closest('.mes');
|
|
|
|
const messageId = Number(messageBlock.attr('mesid'));
|
|
|
|
embedMessageFile(messageId, messageBlock);
|
|
|
|
});
|
|
|
|
|
2023-12-03 01:01:06 +01:00
|
|
|
$(document).on('click', '.editor_maximize', function () {
|
2023-12-07 14:29:40 +01:00
|
|
|
const broId = $(this).attr('data-for');
|
2023-12-03 01:01:06 +01:00
|
|
|
const bro = $(`#${broId}`);
|
2023-12-10 15:48:25 +01:00
|
|
|
const withTab = $(this).attr('data-tab');
|
2023-12-03 01:01:06 +01:00
|
|
|
|
|
|
|
if (!bro.length) {
|
|
|
|
console.error('Could not find editor with id', broId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-03 02:21:11 +01:00
|
|
|
const wrapper = document.createElement('div');
|
|
|
|
wrapper.classList.add('height100p', 'wide100p', 'flex-container');
|
|
|
|
wrapper.classList.add('flexFlowColumn', 'justifyCenter', 'alignitemscenter');
|
2023-12-03 01:01:06 +01:00
|
|
|
const textarea = document.createElement('textarea');
|
|
|
|
textarea.value = String(bro.val());
|
2023-12-03 02:21:11 +01:00
|
|
|
textarea.classList.add('height100p', 'wide100p');
|
2023-12-10 15:48:25 +01:00
|
|
|
textarea.addEventListener('input', function () {
|
2023-12-03 01:01:06 +01:00
|
|
|
bro.val(textarea.value).trigger('input');
|
2023-12-10 15:48:25 +01:00
|
|
|
});
|
2023-12-03 02:21:11 +01:00
|
|
|
wrapper.appendChild(textarea);
|
2023-12-03 01:01:06 +01:00
|
|
|
|
2023-12-10 15:48:25 +01:00
|
|
|
if (withTab) {
|
|
|
|
textarea.addEventListener('keydown', (evt) => {
|
|
|
|
if (evt.key == 'Tab' && !evt.shiftKey && !evt.ctrlKey && !evt.altKey) {
|
|
|
|
evt.preventDefault();
|
|
|
|
const start = textarea.selectionStart;
|
|
|
|
const end = textarea.selectionEnd;
|
|
|
|
if (end - start > 0 && textarea.value.substring(start, end).includes('\n')) {
|
|
|
|
const lineStart = textarea.value.lastIndexOf('\n', start);
|
|
|
|
const count = textarea.value.substring(lineStart, end).split('\n').length - 1;
|
|
|
|
textarea.value = `${textarea.value.substring(0, lineStart)}${textarea.value.substring(lineStart, end).replace(/\n/g, '\n\t')}${textarea.value.substring(end)}`;
|
|
|
|
textarea.selectionStart = start + 1;
|
|
|
|
textarea.selectionEnd = end + count;
|
|
|
|
} else {
|
|
|
|
textarea.value = `${textarea.value.substring(0, start)}\t${textarea.value.substring(end)}`;
|
|
|
|
textarea.selectionStart = start + 1;
|
|
|
|
textarea.selectionEnd = end + 1;
|
|
|
|
}
|
|
|
|
} else if (evt.key == 'Tab' && evt.shiftKey && !evt.ctrlKey && !evt.altKey) {
|
|
|
|
evt.preventDefault();
|
|
|
|
const start = textarea.selectionStart;
|
|
|
|
const end = textarea.selectionEnd;
|
|
|
|
const lineStart = textarea.value.lastIndexOf('\n', start);
|
|
|
|
const count = textarea.value.substring(lineStart, end).split('\n\t').length - 1;
|
|
|
|
textarea.value = `${textarea.value.substring(0, lineStart)}${textarea.value.substring(lineStart, end).replace(/\n\t/g, '\n')}${textarea.value.substring(end)}`;
|
|
|
|
textarea.selectionStart = start - 1;
|
|
|
|
textarea.selectionEnd = end - count;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-03 02:21:11 +01:00
|
|
|
callPopup(wrapper, 'text', '', { wide: true, large: true });
|
2023-12-03 01:01:06 +01:00
|
|
|
});
|
|
|
|
|
2023-12-09 15:01:43 +01:00
|
|
|
$(document).on('click', 'body.documentstyle .mes .mes_text', function () {
|
|
|
|
if ($('.edit_textarea').length) return;
|
|
|
|
$(this).closest('.mes').find('.mes_edit').trigger('click');
|
|
|
|
});
|
|
|
|
|
2024-04-04 23:39:54 +02:00
|
|
|
$(document).on('click', '.open_media_overrides', openExternalMediaOverridesDialog);
|
|
|
|
$(document).on('input', '#forbid_media_override_allowed', function () {
|
|
|
|
const entityId = getCurrentEntityId();
|
|
|
|
if (!entityId) return;
|
|
|
|
power_user.external_media_allowed_overrides.push(entityId);
|
|
|
|
power_user.external_media_forbidden_overrides = power_user.external_media_forbidden_overrides.filter((v) => v !== entityId);
|
|
|
|
saveSettingsDebounced();
|
|
|
|
reloadCurrentChat();
|
|
|
|
});
|
|
|
|
$(document).on('input', '#forbid_media_override_forbidden', function () {
|
|
|
|
const entityId = getCurrentEntityId();
|
|
|
|
if (!entityId) return;
|
|
|
|
power_user.external_media_forbidden_overrides.push(entityId);
|
|
|
|
power_user.external_media_allowed_overrides = power_user.external_media_allowed_overrides.filter((v) => v !== entityId);
|
|
|
|
saveSettingsDebounced();
|
|
|
|
reloadCurrentChat();
|
|
|
|
});
|
|
|
|
$(document).on('input', '#forbid_media_override_global', function () {
|
|
|
|
const entityId = getCurrentEntityId();
|
|
|
|
if (!entityId) return;
|
|
|
|
power_user.external_media_allowed_overrides = power_user.external_media_allowed_overrides.filter((v) => v !== entityId);
|
|
|
|
power_user.external_media_forbidden_overrides = power_user.external_media_forbidden_overrides.filter((v) => v !== entityId);
|
|
|
|
saveSettingsDebounced();
|
|
|
|
reloadCurrentChat();
|
|
|
|
});
|
|
|
|
|
2023-11-18 18:23:58 +01:00
|
|
|
$('#file_form_input').on('change', onFileAttach);
|
|
|
|
$('#file_form').on('reset', function () {
|
|
|
|
$('#file_form').addClass('displayNone');
|
|
|
|
});
|
2023-12-02 20:11:06 +01:00
|
|
|
});
|