2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
|
|
|
|
2021-02-28 00:50:52 +01:00
|
|
|
import * as compose_pm_pill from "./compose_pm_pill";
|
2022-10-21 00:44:55 +02:00
|
|
|
import * as compose_ui from "./compose_ui";
|
2021-02-28 00:38:58 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let message_type = false; // 'stream', 'private', or false-y
|
2023-01-09 16:45:16 +01:00
|
|
|
let recipient_edited_manually = false;
|
|
|
|
|
2023-02-02 05:43:24 +01:00
|
|
|
// We use this variable to keep track of whether user has viewed the topic resolved
|
|
|
|
// banner for the current compose session, for a narrow. This prevents the banner
|
|
|
|
// from popping up for every keystroke while composing.
|
|
|
|
// The variable is reset on sending a message, closing the compose box and changing
|
|
|
|
// the narrow and the user should still be able to see the banner once after
|
|
|
|
// performing these actions
|
|
|
|
let recipient_viewed_topic_resolved_banner = false;
|
|
|
|
|
2023-01-09 16:45:16 +01:00
|
|
|
export function set_recipient_edited_manually(flag) {
|
|
|
|
recipient_edited_manually = flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function is_recipient_edited_manually() {
|
|
|
|
return recipient_edited_manually;
|
|
|
|
}
|
2017-04-15 01:15:59 +02:00
|
|
|
|
2021-02-28 00:50:52 +01:00
|
|
|
export function set_message_type(msg_type) {
|
2017-04-15 01:15:59 +02:00
|
|
|
message_type = msg_type;
|
2021-02-28 00:50:52 +01:00
|
|
|
}
|
2017-04-15 01:15:59 +02:00
|
|
|
|
2021-02-28 00:50:52 +01:00
|
|
|
export function get_message_type() {
|
2017-04-15 01:15:59 +02:00
|
|
|
return message_type;
|
2021-02-28 00:50:52 +01:00
|
|
|
}
|
2017-04-15 01:15:59 +02:00
|
|
|
|
2023-02-02 05:43:24 +01:00
|
|
|
export function set_recipient_viewed_topic_resolved_banner(flag) {
|
|
|
|
recipient_viewed_topic_resolved_banner = flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function has_recipient_viewed_topic_resolved_banner() {
|
|
|
|
return recipient_viewed_topic_resolved_banner;
|
|
|
|
}
|
|
|
|
|
2022-08-04 08:30:55 +02:00
|
|
|
export function recipient_has_topics() {
|
|
|
|
return message_type !== "stream";
|
|
|
|
}
|
|
|
|
|
2021-02-28 00:50:52 +01:00
|
|
|
export function composing() {
|
2017-04-24 20:35:26 +02:00
|
|
|
// This is very similar to get_message_type(), but it returns
|
|
|
|
// a boolean.
|
2020-10-07 12:54:16 +02:00
|
|
|
return Boolean(message_type);
|
2021-02-28 00:50:52 +01:00
|
|
|
}
|
2017-04-15 01:15:59 +02:00
|
|
|
|
2023-01-08 18:45:18 +01:00
|
|
|
function get_or_set(fieldname, keep_leading_whitespace, no_trim) {
|
2022-01-25 11:36:19 +01:00
|
|
|
// We can't hoist the assignment of '$elem' out of this lambda,
|
2017-04-15 01:15:59 +02:00
|
|
|
// because the DOM element might not exist yet when get_or_set
|
|
|
|
// is called.
|
|
|
|
return function (newval) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $elem = $(`#${CSS.escape(fieldname)}`);
|
|
|
|
const oldval = $elem.val();
|
2017-04-15 01:15:59 +02:00
|
|
|
if (newval !== undefined) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$elem.val(newval);
|
2017-04-15 01:15:59 +02:00
|
|
|
}
|
2023-01-08 18:45:18 +01:00
|
|
|
if (no_trim) {
|
|
|
|
return oldval;
|
|
|
|
} else if (keep_leading_whitespace) {
|
|
|
|
return oldval.trimEnd();
|
|
|
|
}
|
|
|
|
return oldval.trim();
|
2017-04-15 01:15:59 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-27 00:22:29 +02:00
|
|
|
export function stream_name() {
|
2022-10-21 00:44:55 +02:00
|
|
|
return compose_ui.compose_stream_widget.value();
|
2022-10-27 00:22:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function set_stream_name(newval) {
|
2022-10-21 00:44:55 +02:00
|
|
|
if (newval !== undefined && newval !== "" && compose_ui.compose_stream_widget) {
|
|
|
|
compose_ui.compose_stream_widget.render(newval);
|
|
|
|
compose_ui.on_compose_select_stream_update(newval);
|
2022-10-27 00:22:29 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-28 00:50:52 +01:00
|
|
|
|
2022-10-27 00:22:29 +02:00
|
|
|
// TODO: Break out setter and getter into their own functions.
|
2021-02-28 00:50:52 +01:00
|
|
|
export const topic = get_or_set("stream_message_recipient_topic");
|
|
|
|
|
2017-11-26 20:37:44 +01:00
|
|
|
// We can't trim leading whitespace in `compose_textarea` because
|
2017-04-15 01:15:59 +02:00
|
|
|
// of the indented syntax for multi-line code blocks.
|
2021-02-28 00:50:52 +01:00
|
|
|
export const message_content = get_or_set("compose-textarea", true);
|
2021-02-27 18:19:48 +01:00
|
|
|
|
2023-01-08 18:45:18 +01:00
|
|
|
const untrimmed_message_content = get_or_set("compose-textarea", true, true);
|
|
|
|
|
2023-02-06 12:24:57 +01:00
|
|
|
function cursor_at_start_of_whitespace_in_compose() {
|
2023-01-08 18:45:18 +01:00
|
|
|
const cursor_position = $("#compose-textarea").caret();
|
|
|
|
return message_content() === "" && cursor_position === 0;
|
|
|
|
}
|
|
|
|
|
2023-02-06 12:24:57 +01:00
|
|
|
export function focus_in_empty_compose(consider_start_of_whitespace_message_empty = false) {
|
2022-01-07 10:21:55 +01:00
|
|
|
// A user trying to press arrow keys in an empty compose is mostly
|
|
|
|
// likely trying to navigate messages. This helper function
|
2023-01-08 18:45:18 +01:00
|
|
|
// decides whether the compose box is empty for this purpose.
|
2023-02-06 12:24:57 +01:00
|
|
|
if (!composing()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We treat the compose box as empty if it's completely empty, or
|
|
|
|
// if the caller requested, if it contains only whitespace and we're
|
|
|
|
// at the start of te compose box.
|
|
|
|
const treat_compose_as_empty =
|
|
|
|
untrimmed_message_content() === "" ||
|
|
|
|
(consider_start_of_whitespace_message_empty && cursor_at_start_of_whitespace_in_compose());
|
|
|
|
if (!treat_compose_as_empty) {
|
2022-01-07 10:21:55 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const focused_element_id = document.activeElement.id;
|
|
|
|
if (focused_element_id === "compose-textarea") {
|
|
|
|
// Focus will be in the compose textarea after sending a
|
|
|
|
// message; this is the most common situation.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the current focus is in one of the recipient inputs, we need
|
|
|
|
// to check whether the input is empty, to avoid accidentally
|
|
|
|
// overriding the browser feature where the Up/Down arrow keys jump
|
|
|
|
// you to the start/end of a non-empty text input.
|
|
|
|
//
|
|
|
|
// Check whether the current input element is empty for each input type.
|
|
|
|
switch (focused_element_id) {
|
|
|
|
case "private_message_recipient":
|
|
|
|
return private_message_recipient().length === 0;
|
|
|
|
case "stream_message_recipient_topic":
|
2023-02-07 01:30:29 +01:00
|
|
|
return topic() === "";
|
2022-10-21 00:44:55 +02:00
|
|
|
case "compose_select_stream_name":
|
2023-02-07 01:30:29 +01:00
|
|
|
return stream_name() === "";
|
2022-01-07 10:21:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2021-02-28 00:50:52 +01:00
|
|
|
}
|
2021-02-27 18:19:48 +01:00
|
|
|
|
2021-02-28 00:50:52 +01:00
|
|
|
export function private_message_recipient(value) {
|
2018-03-06 15:07:55 +01:00
|
|
|
if (typeof value === "string") {
|
|
|
|
compose_pm_pill.set_from_emails(value);
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined;
|
2018-03-06 15:07:55 +01:00
|
|
|
}
|
2020-09-24 07:50:36 +02:00
|
|
|
return compose_pm_pill.get_emails();
|
2021-02-28 00:50:52 +01:00
|
|
|
}
|
2017-04-15 01:15:59 +02:00
|
|
|
|
2021-02-28 00:50:52 +01:00
|
|
|
export function has_message_content() {
|
|
|
|
return message_content() !== "";
|
|
|
|
}
|
2022-03-09 18:29:01 +01:00
|
|
|
|
2022-09-26 21:01:43 +02:00
|
|
|
export function has_full_recipient() {
|
|
|
|
if (message_type === "stream") {
|
|
|
|
return stream_name() !== "" && topic() !== "";
|
|
|
|
}
|
|
|
|
return private_message_recipient() !== "";
|
2022-03-09 18:29:01 +01:00
|
|
|
}
|