web: Add setters for rewired variables.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2024-11-08 02:33:34 -08:00
committed by Tim Abbott
parent e2cc125583
commit 52e59a9605
46 changed files with 599 additions and 133 deletions

View File

@@ -70,6 +70,11 @@ const message_render_response_schema = z.object({
});
export let compose_spinner_visible = false;
export function rewire_compose_spinner_visible(value: typeof compose_spinner_visible): void {
compose_spinner_visible = value;
}
export let shift_pressed = false; // true or false
export let code_formatting_button_triggered = false; // true or false
export let compose_textarea_typeahead: Typeahead<TypeaheadSuggestion> | undefined;
@@ -102,20 +107,24 @@ export function is_full_size(): boolean {
return full_size_status;
}
export function autosize_textarea($textarea: JQuery<HTMLTextAreaElement>): void {
export let autosize_textarea = ($textarea: JQuery<HTMLTextAreaElement>): void => {
// Since this supports both compose and file upload, one must pass
// in the text area to autosize.
if (!is_expanded()) {
autosize.update($textarea);
}
};
export function rewire_autosize_textarea(value: typeof autosize_textarea): void {
autosize_textarea = value;
}
export function insert_and_scroll_into_view(
export let insert_and_scroll_into_view = (
content: string,
$textarea: JQuery<HTMLTextAreaElement>,
replace_all = false,
replace_all_without_undo_support = false,
): void {
): void => {
if (replace_all_without_undo_support) {
// setFieldText is very slow and noticeable when inserting 10k+
// characters of text like from a drafted response,
@@ -132,6 +141,12 @@ export function insert_and_scroll_into_view(
$textarea.trigger("blur");
$textarea.trigger("focus");
autosize_textarea($textarea);
};
export function rewire_insert_and_scroll_into_view(
value: typeof insert_and_scroll_into_view,
): void {
insert_and_scroll_into_view = value;
}
function get_focus_area(opts: ComposeTriggeredOptions): string {
@@ -168,7 +183,7 @@ export function set_focus(opts: ComposeTriggeredOptions): void {
}
}
export function smart_insert_inline($textarea: JQuery<HTMLTextAreaElement>, syntax: string): void {
export let smart_insert_inline = ($textarea: JQuery<HTMLTextAreaElement>, syntax: string): void => {
function is_space(c: string | undefined): boolean {
return c === " " || c === "\t" || c === "\n";
}
@@ -200,6 +215,10 @@ export function smart_insert_inline($textarea: JQuery<HTMLTextAreaElement>, synt
}
insert_and_scroll_into_view(syntax, $textarea);
};
export function rewire_smart_insert_inline(value: typeof smart_insert_inline): void {
smart_insert_inline = value;
}
export function smart_insert_block(
@@ -252,12 +271,12 @@ export function smart_insert_block(
insert_and_scroll_into_view(syntax, $textarea);
}
export function insert_syntax_and_focus(
export let insert_syntax_and_focus = (
syntax: string,
$textarea = $<HTMLTextAreaElement>("textarea#compose-textarea"),
mode = "inline",
padding_newlines?: number,
): void {
): void => {
// Generic helper for inserting syntax into the main compose box
// where the cursor was and focusing the area. Mostly a thin
// wrapper around smart_insert_inline and smart_inline_block.
@@ -277,13 +296,17 @@ export function insert_syntax_and_focus(
} else if (mode === "block") {
smart_insert_block($textarea, syntax, padding_newlines);
}
};
export function rewire_insert_syntax_and_focus(value: typeof insert_syntax_and_focus): void {
insert_syntax_and_focus = value;
}
export function replace_syntax(
export let replace_syntax = (
old_syntax: string,
new_syntax: string,
$textarea = $<HTMLTextAreaElement>("textarea#compose-textarea"),
): boolean {
): boolean => {
// The following couple lines are needed to later restore the initial
// logical position of the cursor after the replacement
const prev_caret = $textarea.caret();
@@ -322,6 +345,10 @@ export function replace_syntax(
// Return if anything was actually replaced.
return old_text !== new_text;
};
export function rewire_replace_syntax(value: typeof replace_syntax): void {
replace_syntax = value;
}
export function compute_placeholder_text(opts: ComposePlaceholderOptions): string {
@@ -374,7 +401,7 @@ export function compute_placeholder_text(opts: ComposePlaceholderOptions): strin
return DEFAULT_COMPOSE_PLACEHOLDER;
}
export function set_compose_box_top(set_top: boolean): void {
export let set_compose_box_top = (set_top: boolean): void => {
if (set_top) {
// As `#compose` has `position: fixed` property, we cannot
// make the compose-box to attain the correct height just by
@@ -386,6 +413,10 @@ export function set_compose_box_top(set_top: boolean): void {
} else {
$("#compose").css("top", "");
}
};
export function rewire_set_compose_box_top(value: typeof set_compose_box_top): void {
set_compose_box_top = value;
}
export function make_compose_box_full_size(): void {
@@ -504,11 +535,11 @@ export function position_inside_code_block(content: string, position: number): b
return [...code_blocks].some((code_block) => code_block?.textContent?.includes(unique_insert));
}
export function format_text(
export let format_text = (
$textarea: JQuery<HTMLTextAreaElement>,
type: string,
inserted_content = "",
): void {
): void => {
const italic_syntax = "*";
const bold_syntax = "**";
const bold_and_italic_syntax = "***";
@@ -1156,6 +1187,10 @@ export function format_text(
break;
}
}
};
export function rewire_format_text(value: typeof format_text): void {
format_text = value;
}
/* TODO: This functions don't belong in this module, as they have