2021-02-28 01:26:48 +01:00
|
|
|
import autosize from "autosize";
|
2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
2021-09-04 17:47:27 +02:00
|
|
|
import {set, wrapSelection} from "text-field-edit";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-07-28 18:23:34 +02:00
|
|
|
import * as common from "./common";
|
2021-04-13 06:51:54 +02:00
|
|
|
import {$t} from "./i18n";
|
2021-11-30 06:26:05 +01:00
|
|
|
import * as loading from "./loading";
|
2021-02-28 01:26:48 +01:00
|
|
|
import * as people from "./people";
|
2021-12-03 07:19:24 +01:00
|
|
|
import * as popover_menus from "./popover_menus";
|
2021-07-28 18:23:34 +02:00
|
|
|
import * as rtl from "./rtl";
|
2021-02-28 01:26:48 +01:00
|
|
|
import * as user_status from "./user_status";
|
2019-07-16 20:19:11 +02:00
|
|
|
|
2021-05-17 13:15:11 +02:00
|
|
|
let full_size_status = false; // true or false
|
|
|
|
|
|
|
|
// Some functions to handle the full size status explicitly
|
|
|
|
export function set_full_size(is_full) {
|
|
|
|
full_size_status = is_full;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function is_full_size() {
|
|
|
|
return full_size_status;
|
|
|
|
}
|
|
|
|
|
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.
|
2021-05-17 13:15:11 +02:00
|
|
|
if (!is_full_size()) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-03-31 13:40:26 +02:00
|
|
|
autosize_textarea(textarea);
|
2021-02-28 01:26:48 +01:00
|
|
|
}
|
2017-11-09 17:32:28 +01:00
|
|
|
|
2021-03-24 21:44:43 +01:00
|
|
|
export function insert_syntax_and_focus(syntax, textarea = $("#compose-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.
|
2021-02-28 01:26:48 +01:00
|
|
|
smart_insert(textarea, syntax);
|
|
|
|
}
|
2017-11-09 16:57:58 +01:00
|
|
|
|
2021-03-24 21:44:43 +01:00
|
|
|
export function replace_syntax(old_syntax, new_syntax, textarea = $("#compose-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.
|
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) {
|
2021-04-13 06:51:54 +02:00
|
|
|
return $t(
|
|
|
|
{defaultMessage: "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) {
|
2021-04-13 06:51:54 +02:00
|
|
|
return $t({defaultMessage: "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) {
|
2021-04-13 06:51:54 +02:00
|
|
|
return $t(
|
|
|
|
{defaultMessage: "Message {recipient_name} ({recipient_status})"},
|
|
|
|
{recipient_name: recipient_names, recipient_status: status},
|
|
|
|
);
|
2019-07-23 20:13:43 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-13 06:51:54 +02:00
|
|
|
return $t({defaultMessage: "Message {recipient_names}"}, {recipient_names});
|
2019-07-23 20:13:43 +02:00
|
|
|
}
|
2021-04-13 06:51:54 +02:00
|
|
|
return $t({defaultMessage: "Compose your message here"});
|
2021-02-28 01:26:48 +01:00
|
|
|
}
|
2021-06-21 23:24:03 +02:00
|
|
|
|
2021-07-15 21:26:16 +02:00
|
|
|
export function set_compose_box_top(set_top) {
|
|
|
|
if (set_top) {
|
|
|
|
// As `#compose` has `position: fixed` property, we cannot
|
|
|
|
// make the compose-box to attain the correct height just by
|
|
|
|
// using CSS. If that wasn't the case, we could have somehow
|
|
|
|
// refactored the HTML so as to consider only the space below
|
|
|
|
// below the `#navbar_alerts` as `height: 100%` of `#compose`.
|
|
|
|
const compose_top =
|
|
|
|
$("#navbar_alerts_wrapper").height() +
|
|
|
|
$(".header").height() +
|
|
|
|
Number.parseInt($(".header").css("paddingBottom"), 10);
|
|
|
|
$("#compose").css("top", compose_top + "px");
|
|
|
|
} else {
|
|
|
|
$("#compose").css("top", "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-17 13:15:11 +02:00
|
|
|
export function make_compose_box_full_size() {
|
|
|
|
set_full_size(true);
|
|
|
|
|
|
|
|
// The autosize should be destroyed for the full size compose
|
|
|
|
// box else it will interfare and shrink its size accordingly.
|
|
|
|
autosize.destroy($("#compose-textarea"));
|
|
|
|
|
|
|
|
$("#compose").addClass("compose-fullscreen");
|
|
|
|
|
2021-07-15 21:26:16 +02:00
|
|
|
// Set the `top` property of compose-box.
|
|
|
|
set_compose_box_top(true);
|
|
|
|
|
2021-05-17 13:15:11 +02:00
|
|
|
$(".collapse_composebox_button").show();
|
|
|
|
$(".expand_composebox_button").hide();
|
|
|
|
$("#compose-textarea").trigger("focus");
|
|
|
|
}
|
|
|
|
|
|
|
|
export function make_compose_box_original_size() {
|
|
|
|
set_full_size(false);
|
|
|
|
|
|
|
|
$("#compose").removeClass("compose-fullscreen");
|
|
|
|
|
2021-07-15 21:26:16 +02:00
|
|
|
// Unset the `top` property of compose-box.
|
|
|
|
set_compose_box_top(false);
|
|
|
|
|
2021-05-17 13:15:11 +02:00
|
|
|
// Again initialise the compose textarea as it was destroyed
|
|
|
|
// when compose box was made full screen
|
|
|
|
autosize($("#compose-textarea"));
|
|
|
|
|
|
|
|
$(".collapse_composebox_button").hide();
|
|
|
|
$(".expand_composebox_button").show();
|
|
|
|
$("#compose-textarea").trigger("focus");
|
|
|
|
}
|
2021-07-28 18:23:34 +02:00
|
|
|
|
|
|
|
export function handle_keydown(event, textarea) {
|
|
|
|
// The event.key property will have uppercase letter if
|
|
|
|
// the "Shift + <key>" combo was used or the Caps Lock
|
|
|
|
// key was on. We turn to key to lowercase so the keybindings
|
|
|
|
// work regardless of whether Caps Lock was on or not.
|
|
|
|
const key = event.key.toLowerCase();
|
2021-06-22 10:31:40 +02:00
|
|
|
let type;
|
|
|
|
if (key === "b") {
|
|
|
|
type = "bold";
|
|
|
|
} else if (key === "i" && !event.shiftKey) {
|
|
|
|
type = "italic";
|
|
|
|
} else if (key === "l" && event.shiftKey) {
|
|
|
|
type = "link";
|
|
|
|
}
|
2021-07-28 18:23:34 +02:00
|
|
|
|
|
|
|
// detect Cmd and Ctrl key
|
|
|
|
const isCmdOrCtrl = common.has_mac_keyboard() ? event.metaKey : event.ctrlKey;
|
|
|
|
|
2021-06-22 10:31:40 +02:00
|
|
|
if (type && isCmdOrCtrl) {
|
|
|
|
format_text(textarea, type);
|
2021-07-28 18:23:34 +02:00
|
|
|
autosize_textarea(textarea);
|
2021-06-22 10:31:40 +02:00
|
|
|
event.preventDefault();
|
2021-07-28 18:23:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function handle_keyup(event, textarea) {
|
|
|
|
// Set the rtl class if the text has an rtl direction, remove it otherwise
|
|
|
|
rtl.set_rtl_class_for_textarea(textarea);
|
|
|
|
}
|
2021-06-22 10:31:40 +02:00
|
|
|
|
|
|
|
export function format_text(textarea, type) {
|
2021-09-04 17:47:27 +02:00
|
|
|
const italic_syntax = "*";
|
|
|
|
const bold_syntax = "**";
|
|
|
|
const bold_and_italic_syntax = "***";
|
|
|
|
let is_selected_text_italic = false;
|
|
|
|
let is_inner_text_italic = false;
|
2021-06-22 10:31:40 +02:00
|
|
|
const field = textarea.get(0);
|
2021-10-01 12:16:44 +02:00
|
|
|
let range = textarea.range();
|
2021-09-04 17:47:27 +02:00
|
|
|
let text = textarea.val();
|
|
|
|
const selected_text = range.text;
|
|
|
|
|
2021-10-01 12:16:44 +02:00
|
|
|
// Remove new line and space around selected text.
|
|
|
|
const left_trim_length = range.text.length - range.text.trimStart().length;
|
|
|
|
const right_trim_length = range.text.length - range.text.trimEnd().length;
|
|
|
|
|
|
|
|
field.setSelectionRange(range.start + left_trim_length, range.end - right_trim_length);
|
|
|
|
range = textarea.range();
|
|
|
|
|
2021-09-04 17:47:27 +02:00
|
|
|
const is_selection_bold = () =>
|
|
|
|
// First check if there are enough characters before/after selection.
|
|
|
|
range.start >= bold_syntax.length &&
|
|
|
|
text.length - range.end >= bold_syntax.length &&
|
|
|
|
// And then if the characters have bold_syntax around them.
|
|
|
|
text.slice(range.start - bold_syntax.length, range.start) === bold_syntax &&
|
|
|
|
text.slice(range.end, range.end + bold_syntax.length) === bold_syntax;
|
|
|
|
|
|
|
|
const is_inner_text_bold = () =>
|
|
|
|
// Check if selected text itself has bold_syntax inside it.
|
|
|
|
range.length > 4 &&
|
|
|
|
selected_text.slice(0, bold_syntax.length) === bold_syntax &&
|
|
|
|
selected_text.slice(-bold_syntax.length) === bold_syntax;
|
2021-06-22 10:31:40 +02:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case "bold":
|
2021-09-04 17:47:27 +02:00
|
|
|
// Ctrl + B: Toggle bold syntax on selection.
|
|
|
|
|
|
|
|
// If the selection is already surrounded by bold syntax,
|
|
|
|
// remove it rather than adding another copy.
|
|
|
|
if (is_selection_bold()) {
|
|
|
|
// Remove the bold_syntax from text.
|
|
|
|
text =
|
|
|
|
text.slice(0, range.start - bold_syntax.length) +
|
|
|
|
text.slice(range.start, range.end) +
|
|
|
|
text.slice(range.end + bold_syntax.length);
|
|
|
|
set(field, text);
|
|
|
|
field.setSelectionRange(
|
|
|
|
range.start - bold_syntax.length,
|
|
|
|
range.end - bold_syntax.length,
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
} else if (is_inner_text_bold()) {
|
|
|
|
// Remove bold syntax inside the selection, if present.
|
|
|
|
text =
|
|
|
|
text.slice(0, range.start) +
|
|
|
|
text.slice(range.start + bold_syntax.length, range.end - bold_syntax.length) +
|
|
|
|
text.slice(range.end);
|
|
|
|
set(field, text);
|
|
|
|
field.setSelectionRange(range.start, range.end - bold_syntax.length * 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we don't have bold syntax, so we add it.
|
|
|
|
wrapSelection(field, bold_syntax);
|
2021-06-22 10:31:40 +02:00
|
|
|
break;
|
|
|
|
case "italic":
|
2021-09-04 17:47:27 +02:00
|
|
|
// Ctrl + I: Toggle italic syntax on selection. This is
|
|
|
|
// much more complex than toggling bold syntax, because of
|
|
|
|
// the following subtle detail: If our selection is
|
|
|
|
// **foo**, toggling italics should add italics, since in
|
|
|
|
// fact it's just bold syntax, even though with *foo* and
|
|
|
|
// ***foo*** should remove italics.
|
|
|
|
|
|
|
|
// If the text is already italic, we remove the italic_syntax from text.
|
|
|
|
if (range.start >= 1 && text.length - range.end >= italic_syntax.length) {
|
|
|
|
// If text has italic_syntax around it.
|
|
|
|
const has_italic_syntax =
|
|
|
|
text.slice(range.start - italic_syntax.length, range.start) === italic_syntax &&
|
|
|
|
text.slice(range.end, range.end + italic_syntax.length) === italic_syntax;
|
|
|
|
|
|
|
|
if (is_selection_bold()) {
|
|
|
|
// If text has bold_syntax around it.
|
|
|
|
if (
|
|
|
|
range.start >= 3 &&
|
|
|
|
text.length - range.end >= bold_and_italic_syntax.length
|
|
|
|
) {
|
|
|
|
// If text is both bold and italic.
|
|
|
|
const has_bold_and_italic_syntax =
|
|
|
|
text.slice(range.start - bold_and_italic_syntax.length, range.start) ===
|
|
|
|
bold_and_italic_syntax &&
|
|
|
|
text.slice(range.end, range.end + bold_and_italic_syntax.length) ===
|
|
|
|
bold_and_italic_syntax;
|
|
|
|
if (has_bold_and_italic_syntax) {
|
|
|
|
is_selected_text_italic = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (has_italic_syntax) {
|
|
|
|
// If text is only italic.
|
|
|
|
is_selected_text_italic = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_selected_text_italic) {
|
|
|
|
// If text has italic syntax around it, we remove the italic syntax.
|
|
|
|
text =
|
|
|
|
text.slice(0, range.start - italic_syntax.length) +
|
|
|
|
text.slice(range.start, range.end) +
|
|
|
|
text.slice(range.end + italic_syntax.length);
|
|
|
|
set(field, text);
|
|
|
|
field.setSelectionRange(
|
|
|
|
range.start - italic_syntax.length,
|
|
|
|
range.end - italic_syntax.length,
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
} else if (
|
|
|
|
selected_text.length > italic_syntax.length * 2 &&
|
|
|
|
// If the selected text contains italic syntax
|
|
|
|
selected_text.slice(0, italic_syntax.length) === italic_syntax &&
|
|
|
|
selected_text.slice(-italic_syntax.length) === italic_syntax
|
|
|
|
) {
|
|
|
|
if (is_inner_text_bold()) {
|
|
|
|
if (
|
|
|
|
selected_text.length > bold_and_italic_syntax.length * 2 &&
|
|
|
|
selected_text.slice(0, bold_and_italic_syntax.length) ===
|
|
|
|
bold_and_italic_syntax &&
|
|
|
|
selected_text.slice(-bold_and_italic_syntax.length) ===
|
|
|
|
bold_and_italic_syntax
|
|
|
|
) {
|
|
|
|
// If selected text is both bold and italic.
|
|
|
|
is_inner_text_italic = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If selected text is only italic.
|
|
|
|
is_inner_text_italic = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_inner_text_italic) {
|
|
|
|
// We remove the italic_syntax from within the selected text.
|
|
|
|
text =
|
|
|
|
text.slice(0, range.start) +
|
|
|
|
text.slice(
|
|
|
|
range.start + italic_syntax.length,
|
|
|
|
range.end - italic_syntax.length,
|
|
|
|
) +
|
|
|
|
text.slice(range.end);
|
|
|
|
set(field, text);
|
|
|
|
field.setSelectionRange(range.start, range.end - italic_syntax.length * 2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
wrapSelection(field, italic_syntax);
|
2021-06-22 10:31:40 +02:00
|
|
|
break;
|
2021-09-08 00:43:38 +02:00
|
|
|
case "link": {
|
2021-06-22 10:31:40 +02:00
|
|
|
// Ctrl + L: Insert a link to selected text
|
|
|
|
wrapSelection(field, "[", "](url)");
|
|
|
|
|
|
|
|
// Change selected text to `url` part of the syntax.
|
|
|
|
// If <text> marks the selected region, we're mapping:
|
|
|
|
// <text> => [text](<url>).
|
|
|
|
const new_start = range.end + "[](".length;
|
|
|
|
const new_end = new_start + "url".length;
|
|
|
|
field.setSelectionRange(new_start, new_end);
|
|
|
|
break;
|
2021-09-08 00:43:38 +02:00
|
|
|
}
|
2021-06-22 10:31:40 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-30 06:26:05 +01:00
|
|
|
|
|
|
|
export function hide_compose_spinner() {
|
|
|
|
$("#compose-send-button .loader").hide();
|
|
|
|
$("#compose-send-button span").show();
|
|
|
|
$("#compose-send-button").removeClass("disable-btn");
|
|
|
|
}
|
|
|
|
|
|
|
|
export function show_compose_spinner() {
|
2021-12-04 06:00:37 +01:00
|
|
|
// Always use white spinner.
|
|
|
|
loading.show_button_spinner($("#compose-send-button .loader"), true);
|
2021-11-30 06:26:05 +01:00
|
|
|
$("#compose-send-button span").hide();
|
|
|
|
$("#compose-send-button").addClass("disable-btn");
|
|
|
|
}
|
2021-12-03 07:19:24 +01:00
|
|
|
|
|
|
|
export function get_compose_click_target(e) {
|
|
|
|
const compose_control_buttons_popover = popover_menus.get_compose_control_buttons_popover();
|
|
|
|
if (compose_control_buttons_popover) {
|
|
|
|
return compose_control_buttons_popover.reference;
|
|
|
|
}
|
|
|
|
return e.target;
|
|
|
|
}
|