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";
|
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
|
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
|
|
|
|
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
|
|
|
|
|
|
|
function get_or_set(fieldname, keep_leading_whitespace) {
|
|
|
|
// We can't hoist the assignment of 'elem' out of this lambda,
|
|
|
|
// because the DOM element might not exist yet when get_or_set
|
|
|
|
// is called.
|
|
|
|
return function (newval) {
|
2021-02-03 23:23:32 +01:00
|
|
|
const elem = $(`#${CSS.escape(fieldname)}`);
|
2019-11-02 00:06:25 +01:00
|
|
|
const oldval = elem.val();
|
2017-04-15 01:15:59 +02:00
|
|
|
if (newval !== undefined) {
|
|
|
|
elem.val(newval);
|
|
|
|
}
|
2020-10-07 11:55:58 +02:00
|
|
|
return keep_leading_whitespace ? oldval.trimEnd() : oldval.trim();
|
2017-04-15 01:15:59 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Break out setters and getter into their own functions.
|
2021-02-28 00:50:52 +01:00
|
|
|
export const stream_name = get_or_set("stream_message_recipient_stream");
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-02-28 00:50:52 +01:00
|
|
|
export function focus_in_empty_compose() {
|
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
|
|
|
|
// decides whether the compose box is "empty" for this purpose.
|
|
|
|
if (!composing() || message_content() !== "") {
|
|
|
|
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":
|
|
|
|
case "stream_message_recipient_stream":
|
|
|
|
return document.activeElement.value === "";
|
|
|
|
}
|
|
|
|
|
|
|
|
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() !== "";
|
|
|
|
}
|