2021-02-28 01:26:48 +01:00
|
|
|
import autosize from "autosize";
|
2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-02-28 01:26:48 +01:00
|
|
|
import * as people from "./people";
|
|
|
|
import * as user_status from "./user_status";
|
2019-07-16 20:19:11 +02:00
|
|
|
|
2021-02-28 01:26:48 +01:00
|
|
|
export function autosize_textarea(textarea) {
|
2020-09-04 23:49:49 +02:00
|
|
|
// Since this supports both compose and file upload, one must pass
|
|
|
|
// in the text area to autosize.
|
|
|
|
autosize.update(textarea);
|
2021-02-28 01:26:48 +01:00
|
|
|
}
|
2017-04-23 08:51:26 +02:00
|
|
|
|
2021-02-28 01:26:48 +01:00
|
|
|
export function smart_insert(textarea, syntax) {
|
2017-11-09 17:47:54 +01:00
|
|
|
function is_space(c) {
|
2020-07-15 01:29:15 +02:00
|
|
|
return c === " " || c === "\t" || c === "\n";
|
2017-11-09 17:47:54 +01:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const pos = textarea.caret();
|
|
|
|
const before_str = textarea.val().slice(0, pos);
|
|
|
|
const after_str = textarea.val().slice(pos);
|
2017-11-09 17:47:54 +01:00
|
|
|
|
2020-12-22 11:26:39 +01:00
|
|
|
if (
|
|
|
|
pos > 0 &&
|
2019-02-27 22:17:27 +01:00
|
|
|
// If there isn't space either at the end of the content
|
|
|
|
// before the insert or (unlikely) at the start of the syntax,
|
|
|
|
// add one.
|
2020-12-22 11:26:39 +01:00
|
|
|
!is_space(before_str.slice(-1)) &&
|
|
|
|
!is_space(syntax[0])
|
|
|
|
) {
|
|
|
|
syntax = " " + syntax;
|
2017-11-09 17:47:54 +01:00
|
|
|
}
|
|
|
|
|
2019-02-27 22:17:27 +01:00
|
|
|
// If there isn't whitespace either at the end of the syntax or the
|
|
|
|
// start of the content after the syntax, add one.
|
2020-07-15 00:34:28 +02:00
|
|
|
if (
|
|
|
|
!(
|
|
|
|
(after_str.length > 0 && is_space(after_str[0])) ||
|
|
|
|
(syntax.length > 0 && is_space(syntax.slice(-1)))
|
|
|
|
)
|
|
|
|
) {
|
2020-07-15 01:29:15 +02:00
|
|
|
syntax += " ";
|
2018-05-06 21:43:17 +02:00
|
|
|
}
|
2017-11-09 17:57:59 +01:00
|
|
|
|
2020-07-20 21:24:26 +02:00
|
|
|
textarea.trigger("focus");
|
2017-12-08 16:17:20 +01:00
|
|
|
|
|
|
|
// We prefer to use insertText, which supports things like undo better
|
|
|
|
// for rich-text editing features like inserting links. But we fall
|
|
|
|
// back to textarea.caret if the browser doesn't support insertText.
|
|
|
|
if (!document.execCommand("insertText", false, syntax)) {
|
|
|
|
textarea.caret(syntax);
|
|
|
|
}
|
|
|
|
|
2017-11-22 04:43:45 +01:00
|
|
|
// This should just call exports.autosize_textarea, but it's a bit
|
|
|
|
// annoying for the unit tests, so we don't do that.
|
2019-07-16 20:19:11 +02:00
|
|
|
autosize.update(textarea);
|
2021-02-28 01:26:48 +01:00
|
|
|
}
|
2017-11-09 17:32:28 +01:00
|
|
|
|
2021-02-28 01:26:48 +01:00
|
|
|
export function insert_syntax_and_focus(syntax, textarea) {
|
2017-11-09 16:57:58 +01:00
|
|
|
// Generic helper for inserting syntax into the main compose box
|
|
|
|
// where the cursor was and focusing the area. Mostly a thin
|
2017-11-09 17:32:28 +01:00
|
|
|
// wrapper around smart_insert.
|
2017-11-28 22:36:58 +01:00
|
|
|
if (textarea === undefined) {
|
2020-07-15 01:29:15 +02:00
|
|
|
textarea = $("#compose-textarea");
|
2017-11-28 22:36:58 +01:00
|
|
|
}
|
2021-02-28 01:26:48 +01:00
|
|
|
smart_insert(textarea, syntax);
|
|
|
|
}
|
2017-11-09 16:57:58 +01:00
|
|
|
|
2021-02-28 01:26:48 +01:00
|
|
|
export function replace_syntax(old_syntax, new_syntax, textarea) {
|
2018-08-15 03:55:44 +02:00
|
|
|
// Replaces `old_syntax` with `new_syntax` text in the compose box. Due to
|
|
|
|
// the way that JavaScript handles string replacements, if `old_syntax` is
|
|
|
|
// a string it will only replace the first instance. If `old_syntax` is
|
|
|
|
// a RegExp with a global flag, it will replace all instances.
|
|
|
|
|
|
|
|
if (textarea === undefined) {
|
2020-07-15 01:29:15 +02:00
|
|
|
textarea = $("#compose-textarea");
|
2018-08-15 03:55:44 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
textarea.val(
|
|
|
|
textarea.val().replace(
|
|
|
|
old_syntax,
|
|
|
|
() =>
|
|
|
|
// We need this anonymous function to avoid JavaScript's
|
|
|
|
// replace() function treating `$`s in new_syntax as special syntax. See
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Description
|
|
|
|
// for details.
|
|
|
|
new_syntax,
|
|
|
|
),
|
|
|
|
);
|
2021-02-28 01:26:48 +01:00
|
|
|
}
|
2018-08-15 03:55:44 +02:00
|
|
|
|
2021-02-28 01:26:48 +01:00
|
|
|
export function compute_placeholder_text(opts) {
|
2019-07-23 20:13:43 +02:00
|
|
|
// Computes clear placeholder text for the compose box, depending
|
|
|
|
// on what heading values have already been filled out.
|
2019-08-01 21:59:07 +02:00
|
|
|
//
|
|
|
|
// We return text with the stream and topic name unescaped,
|
|
|
|
// because the caller is expected to insert this into the
|
|
|
|
// placeholder field in a way that does HTML escaping.
|
2020-07-15 01:29:15 +02:00
|
|
|
if (opts.message_type === "stream") {
|
2019-07-23 20:13:43 +02:00
|
|
|
if (opts.topic) {
|
2020-07-15 00:34:28 +02:00
|
|
|
return i18n.t("Message #__- stream_name__ > __- topic_name__", {
|
|
|
|
stream_name: opts.stream,
|
|
|
|
topic_name: opts.topic,
|
|
|
|
});
|
2019-07-23 20:13:43 +02:00
|
|
|
} else if (opts.stream) {
|
2019-08-01 21:59:07 +02:00
|
|
|
return i18n.t("Message #__- stream_name__", {stream_name: opts.stream});
|
2019-07-23 20:13:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 02:43:28 +02:00
|
|
|
// For private messages
|
2019-07-23 20:13:43 +02:00
|
|
|
if (opts.private_message_recipient) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const recipient_list = opts.private_message_recipient.split(",");
|
2020-07-15 00:34:28 +02:00
|
|
|
const recipient_names = recipient_list
|
|
|
|
.map((recipient) => {
|
|
|
|
const user = people.get_by_email(recipient);
|
|
|
|
return user.full_name;
|
|
|
|
})
|
|
|
|
.join(", ");
|
2019-07-23 20:13:43 +02:00
|
|
|
|
|
|
|
if (recipient_list.length === 1) {
|
|
|
|
// If it's a single user, display status text if available
|
2019-11-02 00:06:25 +01:00
|
|
|
const user = people.get_by_email(recipient_list[0]);
|
|
|
|
const status = user_status.get_status_text(user.user_id);
|
2019-07-23 20:13:43 +02:00
|
|
|
if (status) {
|
2020-07-15 00:34:28 +02:00
|
|
|
return i18n.t("Message __- recipient_name__ (__- recipient_status__)", {
|
|
|
|
recipient_name: recipient_names,
|
|
|
|
recipient_status: status,
|
|
|
|
});
|
2019-07-23 20:13:43 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-20 22:18:43 +02:00
|
|
|
return i18n.t("Message __- recipient_names__", {recipient_names});
|
2019-07-23 20:13:43 +02:00
|
|
|
}
|
|
|
|
return i18n.t("Compose your message here");
|
2021-02-28 01:26:48 +01:00
|
|
|
}
|