2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2024-11-12 03:59:37 +01:00
|
|
|
import * as blueslip from "./blueslip.ts";
|
|
|
|
import * as hash_parser from "./hash_parser.ts";
|
|
|
|
import * as keydown_util from "./keydown_util.ts";
|
2022-09-28 08:27:24 +02:00
|
|
|
|
2017-03-18 21:35:35 +01:00
|
|
|
// Add functions to this that have no non-trivial
|
|
|
|
// dependencies other than jQuery.
|
|
|
|
|
2020-03-27 01:32:21 +01:00
|
|
|
// https://stackoverflow.com/questions/4233265/contenteditable-set-caret-at-the-end-of-the-text-cross-browser
|
2021-06-14 20:29:04 +02:00
|
|
|
export function place_caret_at_end(el: HTMLElement): void {
|
2020-07-22 02:58:29 +02:00
|
|
|
el.focus();
|
2023-04-14 17:31:24 +02:00
|
|
|
if (el instanceof HTMLInputElement) {
|
|
|
|
el.setSelectionRange(el.value.length, el.value.length);
|
|
|
|
} else {
|
|
|
|
const range = document.createRange();
|
|
|
|
range.selectNodeContents(el);
|
|
|
|
range.collapse(false);
|
|
|
|
const sel = window.getSelection();
|
|
|
|
sel?.removeAllRanges();
|
|
|
|
sel?.addRange(range);
|
|
|
|
}
|
2021-02-28 01:00:36 +01:00
|
|
|
}
|
2017-11-13 21:38:00 +01:00
|
|
|
|
2023-04-25 06:38:51 +02:00
|
|
|
export function replace_emoji_with_text($element: JQuery): void {
|
2024-04-03 02:51:03 +02:00
|
|
|
$element
|
|
|
|
.find(".emoji")
|
|
|
|
.text(function () {
|
|
|
|
if ($(this).is("img")) {
|
|
|
|
return $(this).attr("alt") ?? "";
|
|
|
|
}
|
|
|
|
return $(this).text();
|
|
|
|
})
|
|
|
|
.contents()
|
|
|
|
.unwrap();
|
2023-04-25 06:38:51 +02:00
|
|
|
}
|
|
|
|
|
2023-11-27 07:35:08 +01:00
|
|
|
export function change_katex_to_raw_latex($element: JQuery): void {
|
|
|
|
// Find all the span elements with the class "katex"
|
|
|
|
$element.find("span.katex").each(function () {
|
|
|
|
// Find the text within the <annotation> tag
|
|
|
|
const latex_text = $(this).find('annotation[encoding="application/x-tex"]').text();
|
|
|
|
|
|
|
|
// Create a new <span> element with the raw latex wrapped in $$
|
|
|
|
const $latex_span = $("<span>").text("$$" + latex_text + "$$");
|
|
|
|
|
|
|
|
// Replace the current .katex element with the new <span> containing the text
|
|
|
|
$(this).replaceWith($latex_span);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-01-22 20:03:18 +01:00
|
|
|
export function is_user_said_paragraph($element: JQuery): boolean {
|
|
|
|
// Irrespective of language, the user said paragraph has these exact elements:
|
|
|
|
// 1. A user mention
|
|
|
|
// 2. A same server message link ("said")
|
|
|
|
// 3. A colon (:)
|
|
|
|
const $user_mention = $element.find(".user-mention");
|
|
|
|
if ($user_mention.length !== 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const $message_link = $element.find("a[href]").filter((_index, element) => {
|
|
|
|
const href = $(element).attr("href")!;
|
|
|
|
return href ? hash_parser.is_same_server_message_link(href) : false;
|
|
|
|
});
|
|
|
|
if ($message_link.length !== 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const remaining_text = $element
|
|
|
|
.text()
|
|
|
|
.replace($user_mention.text(), "")
|
|
|
|
.replace($message_link.text(), "");
|
|
|
|
return remaining_text.trim() === ":";
|
|
|
|
}
|
|
|
|
|
2024-11-08 11:33:34 +01:00
|
|
|
export let get_collapsible_status_array = ($elements: JQuery): boolean[] =>
|
|
|
|
[...$elements].map(
|
2024-01-22 20:03:18 +01:00
|
|
|
(element) => $(element).is("blockquote") || is_user_said_paragraph($(element)),
|
|
|
|
);
|
2024-11-08 11:33:34 +01:00
|
|
|
|
|
|
|
export function rewire_get_collapsible_status_array(
|
|
|
|
value: typeof get_collapsible_status_array,
|
|
|
|
): void {
|
|
|
|
get_collapsible_status_array = value;
|
2024-01-22 20:03:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function potentially_collapse_quotes($element: JQuery): boolean {
|
|
|
|
const $children = $element.children();
|
|
|
|
const collapsible_status = get_collapsible_status_array($children);
|
|
|
|
|
|
|
|
if (collapsible_status.every(Boolean) || collapsible_status.every((x) => !x)) {
|
|
|
|
// If every element is collapsible or none of them is collapsible,
|
|
|
|
// we don't collapse any element.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const [index, element] of [...$children].entries()) {
|
|
|
|
if (collapsible_status[index]) {
|
|
|
|
if (index > 0 && collapsible_status[index - 1]) {
|
|
|
|
// If the previous element was also collapsible, remove its text
|
|
|
|
// to have a single collapsed block instead of multiple in a row.
|
|
|
|
$(element).text("");
|
|
|
|
} else {
|
|
|
|
// Else, collapse this element.
|
|
|
|
$(element).text("[…]");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-14 20:29:04 +02:00
|
|
|
export function blur_active_element(): void {
|
2017-03-18 21:35:35 +01:00
|
|
|
// this blurs anything that may perhaps be actively focused on.
|
2021-06-14 20:29:04 +02:00
|
|
|
if (document.activeElement instanceof HTMLElement) {
|
|
|
|
document.activeElement.blur();
|
|
|
|
}
|
2021-02-28 01:00:36 +01:00
|
|
|
}
|
2021-03-31 05:36:08 +02:00
|
|
|
|
2021-06-14 20:29:04 +02:00
|
|
|
export function convert_enter_to_click(e: JQuery.KeyDownEvent): void {
|
2022-09-28 08:27:24 +02:00
|
|
|
if (keydown_util.is_enter_event(e)) {
|
2021-05-08 23:11:34 +02:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2021-03-31 05:36:08 +02:00
|
|
|
$(e.currentTarget).trigger("click");
|
|
|
|
}
|
|
|
|
}
|
2021-04-09 09:18:43 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
export function update_unread_count_in_dom($unread_count_elem: JQuery, count: number): void {
|
2021-04-09 09:18:43 +02:00
|
|
|
// This function is used to update unread count in top left corner
|
|
|
|
// elements.
|
2022-01-25 11:36:19 +01:00
|
|
|
const $unread_count_span = $unread_count_elem.find(".unread_count");
|
2021-04-09 09:18:43 +02:00
|
|
|
|
|
|
|
if (count === 0) {
|
2023-04-25 13:03:05 +02:00
|
|
|
$unread_count_span.addClass("hide");
|
2022-01-25 11:36:19 +01:00
|
|
|
$unread_count_span.text("");
|
2021-04-09 09:18:43 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-25 13:03:05 +02:00
|
|
|
$unread_count_span.removeClass("hide");
|
2022-01-25 11:36:19 +01:00
|
|
|
$unread_count_span.text(count);
|
2021-04-09 09:18:43 +02:00
|
|
|
}
|
2022-03-02 23:06:33 +01:00
|
|
|
|
2022-08-05 22:45:20 +02:00
|
|
|
export function update_unread_mention_info_in_dom(
|
|
|
|
$unread_mention_info_elem: JQuery,
|
2023-03-16 19:19:53 +01:00
|
|
|
stream_has_any_unread_mention_messages: boolean,
|
2022-08-05 22:45:20 +02:00
|
|
|
): void {
|
|
|
|
const $unread_mention_info_span = $unread_mention_info_elem.find(".unread_mention_info");
|
|
|
|
if (!stream_has_any_unread_mention_messages) {
|
|
|
|
$unread_mention_info_span.hide();
|
|
|
|
$unread_mention_info_span.text("");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$unread_mention_info_span.show();
|
|
|
|
$unread_mention_info_span.text("@");
|
|
|
|
}
|
|
|
|
|
2022-03-02 23:06:33 +01:00
|
|
|
/**
|
|
|
|
* Parse HTML and return a DocumentFragment.
|
|
|
|
*
|
|
|
|
* Like any consumer of HTML, this function must only be given input
|
|
|
|
* from trusted producers of safe HTML, such as auto-escaping
|
|
|
|
* templates; violating this expectation will introduce bugs that are
|
|
|
|
* likely to be security vulnerabilities.
|
|
|
|
*/
|
|
|
|
export function parse_html(html: string): DocumentFragment {
|
|
|
|
const template = document.createElement("template");
|
|
|
|
template.innerHTML = html;
|
|
|
|
return template.content;
|
|
|
|
}
|
2023-03-21 11:57:40 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle permission denied to play audio by the browser.
|
|
|
|
* This can happen due to two reasons: user denied permission to play audio
|
|
|
|
* unconditionally and browser denying permission to play audio without
|
|
|
|
* any interactive trigger like a button. See
|
|
|
|
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/play for more details.
|
|
|
|
*/
|
2024-10-02 07:20:32 +02:00
|
|
|
export async function play_audio(elem: HTMLAudioElement): Promise<void> {
|
2023-03-21 11:57:40 +01:00
|
|
|
try {
|
|
|
|
await elem.play();
|
|
|
|
} catch (error) {
|
|
|
|
if (!(error instanceof DOMException)) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
blueslip.debug(`Unable to play audio. ${error.name}: ${error.message}`);
|
|
|
|
}
|
|
|
|
}
|
2023-04-05 19:42:00 +02:00
|
|
|
|
|
|
|
export function listener_for_preferred_color_scheme_change(callback: () => void): void {
|
|
|
|
const media_query_list = window.matchMedia("(prefers-color-scheme: dark)");
|
|
|
|
// MediaQueryList.addEventListener is missing in Safari < 14
|
2023-09-08 22:52:44 +02:00
|
|
|
const listener = (): void => {
|
2023-04-05 19:42:00 +02:00
|
|
|
if ($(":root").hasClass("color-scheme-automatic")) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
};
|
2023-09-08 22:52:44 +02:00
|
|
|
if (media_query_list.addEventListener) {
|
2023-04-05 19:42:00 +02:00
|
|
|
media_query_list.addEventListener("change", listener);
|
|
|
|
} else {
|
2024-10-04 01:30:52 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-deprecated
|
2023-09-08 22:52:44 +02:00
|
|
|
media_query_list.addListener(listener);
|
2023-04-05 19:42:00 +02:00
|
|
|
}
|
|
|
|
}
|
2023-11-06 13:57:29 +01:00
|
|
|
|
|
|
|
// Keep the menu icon over which the popover is based off visible.
|
|
|
|
export function show_left_sidebar_menu_icon(element: HTMLElement): void {
|
|
|
|
$(element).closest(".sidebar-menu-icon").addClass("left_sidebar_menu_icon_visible");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the class from element when popover is closed
|
|
|
|
export function hide_left_sidebar_menu_icon(): void {
|
|
|
|
$(".left_sidebar_menu_icon_visible").removeClass("left_sidebar_menu_icon_visible");
|
|
|
|
}
|