2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2022-09-28 08:27:24 +02:00
|
|
|
import * as keydown_util from "./keydown_util";
|
|
|
|
|
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();
|
2017-11-13 21:38:00 +01:00
|
|
|
|
2021-06-25 22:56:06 +02:00
|
|
|
const range = document.createRange();
|
|
|
|
range.selectNodeContents(el);
|
|
|
|
range.collapse(false);
|
|
|
|
const sel = window.getSelection();
|
2021-06-14 20:29:04 +02:00
|
|
|
sel?.removeAllRanges();
|
|
|
|
sel?.addRange(range);
|
2021-02-28 01:00:36 +01:00
|
|
|
}
|
2017-11-13 21:38:00 +01:00
|
|
|
|
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) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$unread_count_span.hide();
|
|
|
|
$unread_count_span.text("");
|
2021-04-09 09:18:43 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$unread_count_span.show();
|
|
|
|
$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,
|
|
|
|
stream_has_any_unread_mention_messages: Boolean,
|
|
|
|
): 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;
|
|
|
|
}
|