2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const autosize = require("autosize");
|
2020-07-24 22:04:07 +02:00
|
|
|
|
2020-07-23 22:41:45 +02:00
|
|
|
const fenced_code = require("../shared/js/fenced_code");
|
2019-07-16 20:19:11 +02:00
|
|
|
|
2021-02-28 00:39:51 +01:00
|
|
|
const channel = require("./channel");
|
2021-02-28 00:35:35 +01:00
|
|
|
const common = require("./common");
|
2021-02-28 00:38:58 +01:00
|
|
|
const compose_pm_pill = require("./compose_pm_pill");
|
2021-02-28 00:37:57 +01:00
|
|
|
const drafts = require("./drafts");
|
2021-02-28 00:42:00 +01:00
|
|
|
const message_viewport = require("./message_viewport");
|
2021-02-28 00:47:56 +01:00
|
|
|
const narrow_state = require("./narrow_state");
|
2020-08-20 21:24:06 +02:00
|
|
|
const people = require("./people");
|
2021-02-28 00:48:19 +01:00
|
|
|
const reload_state = require("./reload_state");
|
2020-08-20 21:24:06 +02:00
|
|
|
|
2020-07-21 21:03:29 +02:00
|
|
|
exports.blur_compose_inputs = function () {
|
2020-07-18 20:57:27 +02:00
|
|
|
$(".message_comp").find("input, textarea, button, #private_message_recipient").trigger("blur");
|
2017-04-14 16:26:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function hide_box() {
|
2020-07-21 21:03:29 +02:00
|
|
|
exports.blur_compose_inputs();
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#stream-message").hide();
|
|
|
|
$("#private-message").hide();
|
2017-04-14 16:26:00 +02:00
|
|
|
$(".new_message_textarea").css("min-height", "");
|
|
|
|
compose_fade.clear_compose();
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".message_comp").hide();
|
2017-04-14 16:26:00 +02:00
|
|
|
$("#compose_controls").show();
|
|
|
|
compose.clear_preview_area();
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_focus_area(msg_type, opts) {
|
|
|
|
// Set focus to "Topic" when narrowed to a stream+topic and "New topic" button clicked.
|
2020-07-15 01:29:15 +02:00
|
|
|
if (msg_type === "stream" && opts.stream && !opts.topic) {
|
|
|
|
return "#stream_message_recipient_topic";
|
2020-07-15 00:34:28 +02:00
|
|
|
} else if (
|
|
|
|
(msg_type === "stream" && opts.stream) ||
|
|
|
|
(msg_type === "private" && opts.private_message_recipient)
|
|
|
|
) {
|
2017-04-14 16:26:00 +02:00
|
|
|
if (opts.trigger === "new topic button") {
|
2020-07-15 01:29:15 +02:00
|
|
|
return "#stream_message_recipient_topic";
|
2017-04-14 16:26:00 +02:00
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
return "#compose-textarea";
|
2017-04-14 16:26:00 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
if (msg_type === "stream") {
|
|
|
|
return "#stream_message_recipient_stream";
|
2017-04-14 16:26:00 +02:00
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
return "#private_message_recipient";
|
2017-04-14 16:26:00 +02:00
|
|
|
}
|
|
|
|
// Export for testing
|
|
|
|
exports._get_focus_area = get_focus_area;
|
|
|
|
|
|
|
|
exports.set_focus = function (msg_type, opts) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const focus_area = get_focus_area(msg_type, opts);
|
2017-04-14 16:26:00 +02:00
|
|
|
if (focus_area === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
if (window.getSelection().toString() === "" || opts.trigger !== "message click") {
|
2019-11-02 00:06:25 +01:00
|
|
|
const elt = $(focus_area);
|
2020-07-20 21:24:26 +02:00
|
|
|
elt.trigger("focus").trigger("select");
|
2017-04-14 16:26:00 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Show the compose box.
|
|
|
|
function show_box(msg_type, opts) {
|
|
|
|
if (msg_type === "stream") {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#private-message").hide();
|
|
|
|
$("#stream-message").show();
|
2017-04-14 16:26:00 +02:00
|
|
|
$("#stream_toggle").addClass("active");
|
|
|
|
$("#private_message_toggle").removeClass("active");
|
|
|
|
} else {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#private-message").show();
|
|
|
|
$("#stream-message").hide();
|
2017-04-14 16:26:00 +02:00
|
|
|
$("#stream_toggle").removeClass("active");
|
|
|
|
$("#private_message_toggle").addClass("active");
|
|
|
|
}
|
2017-11-26 19:58:36 +01:00
|
|
|
$("#compose-send-status").removeClass(common.status_classes).hide();
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#compose").css({visibility: "visible"});
|
2019-02-05 22:12:28 +01:00
|
|
|
// When changing this, edit the 42px in _maybe_autoscroll
|
2017-04-14 16:26:00 +02:00
|
|
|
$(".new_message_textarea").css("min-height", "3em");
|
|
|
|
|
|
|
|
exports.set_focus(msg_type, opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.clear_textarea = function () {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#compose").find("input[type=text], textarea").val("");
|
2017-04-14 16:26:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function clear_box() {
|
|
|
|
compose.clear_invites();
|
|
|
|
|
|
|
|
// TODO: Better encapsulate at-mention warnings.
|
|
|
|
compose.clear_all_everyone_warnings();
|
2017-12-18 16:09:41 +01:00
|
|
|
compose.clear_announce_warnings();
|
2017-10-14 15:18:21 +02:00
|
|
|
compose.clear_private_stream_alert();
|
2017-04-14 16:26:00 +02:00
|
|
|
compose.reset_user_acknowledged_all_everyone_flag();
|
2017-12-18 16:09:41 +01:00
|
|
|
compose.reset_user_acknowledged_announce_flag();
|
2017-04-14 16:26:00 +02:00
|
|
|
|
|
|
|
exports.clear_textarea();
|
2017-11-26 20:37:44 +01:00
|
|
|
$("#compose-textarea").removeData("draft-id");
|
2020-09-04 23:49:49 +02:00
|
|
|
compose_ui.autosize_textarea($("#compose-textarea"));
|
2017-11-26 19:58:36 +01:00
|
|
|
$("#compose-send-status").hide(0);
|
2017-04-14 16:26:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.autosize_message_content = function () {
|
2019-07-16 20:19:11 +02:00
|
|
|
autosize($("#compose-textarea"), {
|
2020-07-20 22:18:43 +02:00
|
|
|
callback() {
|
2019-10-25 09:45:13 +02:00
|
|
|
exports.maybe_scroll_up_selected_message();
|
2018-08-28 22:04:51 +02:00
|
|
|
},
|
|
|
|
});
|
2017-04-14 16:26:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.expand_compose_box = function () {
|
|
|
|
$("#compose_close").show();
|
|
|
|
$("#compose_controls").hide();
|
2020-07-15 01:29:15 +02:00
|
|
|
$(".message_comp").show();
|
2017-04-14 16:26:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.complete_starting_tasks = function (msg_type, opts) {
|
|
|
|
// This is sort of a kitchen sink function, and it's called only
|
|
|
|
// by compose.start() for now. Having this as a separate function
|
|
|
|
// makes testing a bit easier.
|
|
|
|
|
|
|
|
exports.maybe_scroll_up_selected_message();
|
2020-07-04 16:25:41 +02:00
|
|
|
ui_util.change_tab_to("#message_feed_container");
|
2017-04-14 16:26:00 +02:00
|
|
|
compose_fade.start_compose(msg_type);
|
2020-05-12 11:02:13 +02:00
|
|
|
ui_util.decorate_stream_bar(opts.stream, $("#stream-message .message_header_stream"), true);
|
2020-12-11 04:26:23 +01:00
|
|
|
$(document).trigger(new $.Event("compose_started.zulip", opts));
|
2020-07-27 16:07:38 +02:00
|
|
|
exports.update_placeholder_text();
|
2017-04-14 16:26:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.maybe_scroll_up_selected_message = function () {
|
|
|
|
// If the compose box is obscuring the currently selected message,
|
|
|
|
// scroll up until the message is no longer occluded.
|
|
|
|
if (current_msg_list.selected_id() === -1) {
|
|
|
|
// If there's no selected message, there's no need to
|
|
|
|
// scroll the compose box to avoid it.
|
|
|
|
return;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const selected_row = current_msg_list.selected_row();
|
2018-04-27 23:19:58 +02:00
|
|
|
|
|
|
|
if (selected_row.height() > message_viewport.height() - 100) {
|
|
|
|
// For very tall messages whose height is close to the entire
|
|
|
|
// height of the viewport, don't auto-scroll the viewport to
|
|
|
|
// the end of the message (since that makes it feel annoying
|
|
|
|
// to work with very tall messages). See #8941 for details.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
const cover = selected_row.offset().top + selected_row.height() - $("#compose").offset().top;
|
2017-04-14 16:26:00 +02:00
|
|
|
if (cover > 0) {
|
2018-08-29 20:49:47 +02:00
|
|
|
message_viewport.user_initiated_animate_scroll(cover + 20);
|
2017-04-14 16:26:00 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function fill_in_opts_from_current_narrowed_view(msg_type, opts) {
|
2020-02-09 04:15:38 +01:00
|
|
|
return {
|
2018-12-18 19:34:45 +01:00
|
|
|
message_type: msg_type,
|
2020-07-15 01:29:15 +02:00
|
|
|
stream: "",
|
|
|
|
topic: "",
|
|
|
|
private_message_recipient: "",
|
|
|
|
trigger: "unknown",
|
2017-04-14 16:26:00 +02:00
|
|
|
|
2020-02-09 04:15:38 +01:00
|
|
|
// Set default parameters based on the current narrowed view.
|
|
|
|
...narrow_state.set_compose_defaults(),
|
|
|
|
|
|
|
|
...opts,
|
|
|
|
};
|
2017-04-14 16:26:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function same_recipient_as_before(msg_type, opts) {
|
2020-07-15 00:34:28 +02:00
|
|
|
return (
|
|
|
|
compose_state.get_message_type() === msg_type &&
|
|
|
|
((msg_type === "stream" &&
|
|
|
|
opts.stream === compose_state.stream_name() &&
|
|
|
|
opts.topic === compose_state.topic()) ||
|
|
|
|
(msg_type === "private" &&
|
|
|
|
opts.private_message_recipient === compose_state.private_message_recipient()))
|
|
|
|
);
|
2017-04-14 16:26:00 +02:00
|
|
|
}
|
|
|
|
|
2020-07-27 16:07:38 +02:00
|
|
|
exports.update_placeholder_text = function () {
|
2020-07-29 17:32:39 +02:00
|
|
|
// Change compose placeholder text only if compose box is open.
|
|
|
|
if (!$("#compose-textarea").is(":visible")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-27 16:07:38 +02:00
|
|
|
const opts = {
|
|
|
|
message_type: compose_state.get_message_type(),
|
|
|
|
stream: $("#stream_message_recipient_stream").val(),
|
|
|
|
topic: $("#stream_message_recipient_topic").val(),
|
|
|
|
private_message_recipient: compose_pm_pill.get_emails(),
|
|
|
|
};
|
|
|
|
|
|
|
|
$("#compose-textarea").attr("placeholder", compose_ui.compute_placeholder_text(opts));
|
2019-07-23 20:13:43 +02:00
|
|
|
};
|
|
|
|
|
2017-04-14 16:26:00 +02:00
|
|
|
exports.start = function (msg_type, opts) {
|
|
|
|
exports.autosize_message_content();
|
|
|
|
|
2018-08-04 15:40:25 +02:00
|
|
|
if (reload_state.is_in_progress()) {
|
2017-04-14 16:26:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
notifications.clear_compose_notifications();
|
|
|
|
exports.expand_compose_box();
|
|
|
|
|
|
|
|
opts = fill_in_opts_from_current_narrowed_view(msg_type, opts);
|
2019-03-06 22:01:37 +01:00
|
|
|
// If we are invoked by a compose hotkey (c or x) or new topic
|
|
|
|
// button, do not assume that we know what the message's topic or
|
|
|
|
// PM recipient should be.
|
2020-07-15 00:34:28 +02:00
|
|
|
if (opts.trigger === "compose_hotkey" || opts.trigger === "new topic button") {
|
2020-07-15 01:29:15 +02:00
|
|
|
opts.topic = "";
|
|
|
|
opts.private_message_recipient = "";
|
2017-04-14 16:26:00 +02:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const subbed_streams = stream_data.subscribed_subs();
|
2020-07-15 00:34:28 +02:00
|
|
|
if (
|
|
|
|
subbed_streams.length === 1 &&
|
|
|
|
(opts.trigger === "new topic button" ||
|
|
|
|
(opts.trigger === "compose_hotkey" && msg_type === "stream"))
|
|
|
|
) {
|
2019-07-27 08:12:14 +02:00
|
|
|
opts.stream = subbed_streams[0].name;
|
|
|
|
}
|
|
|
|
|
2017-04-14 16:26:00 +02:00
|
|
|
if (compose_state.composing() && !same_recipient_as_before(msg_type, opts)) {
|
|
|
|
// Clear the compose box if the existing message is to a different recipient
|
|
|
|
clear_box();
|
|
|
|
}
|
|
|
|
|
2019-12-02 18:01:31 +01:00
|
|
|
// We set the stream/topic/private_message_recipient
|
|
|
|
// unconditionally here, which assumes the caller will have passed
|
|
|
|
// '' or undefined for these values if they are not appropriate
|
|
|
|
// for this message.
|
|
|
|
//
|
|
|
|
// TODO: Move these into a conditional on message_type, using an
|
|
|
|
// explicit "clear" function for compose_state.
|
2017-04-15 01:15:59 +02:00
|
|
|
compose_state.stream_name(opts.stream);
|
2018-11-15 19:14:16 +01:00
|
|
|
compose_state.topic(opts.topic);
|
2017-04-14 16:26:00 +02:00
|
|
|
|
|
|
|
// Set the recipients with a space after each comma, so it looks nice.
|
2019-12-02 17:53:55 +01:00
|
|
|
compose_state.private_message_recipient(opts.private_message_recipient.replace(/,\s*/g, ", "));
|
2017-04-14 16:26:00 +02:00
|
|
|
|
|
|
|
// If the user opens the compose box, types some text, and then clicks on a
|
2018-11-13 16:27:16 +01:00
|
|
|
// different stream/topic, we want to keep the text in the compose box
|
2017-04-14 16:26:00 +02:00
|
|
|
if (opts.content !== undefined) {
|
2017-04-15 01:15:59 +02:00
|
|
|
compose_state.message_content(opts.content);
|
2017-04-14 16:26:00 +02:00
|
|
|
}
|
|
|
|
|
2017-04-15 01:15:59 +02:00
|
|
|
compose_state.set_message_type(msg_type);
|
2017-04-14 16:26:00 +02:00
|
|
|
|
|
|
|
// Show either stream/topic fields or "You and" field.
|
|
|
|
show_box(msg_type, opts);
|
|
|
|
|
|
|
|
exports.complete_starting_tasks(msg_type, opts);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.cancel = function () {
|
2017-11-26 20:37:44 +01:00
|
|
|
$("#compose-textarea").height(40 + "px");
|
2017-04-14 16:26:00 +02:00
|
|
|
|
|
|
|
if (page_params.narrow !== undefined) {
|
|
|
|
// Never close the compose box in narrow embedded windows, but
|
2018-11-15 19:14:16 +01:00
|
|
|
// at least clear the topic and unfade.
|
2017-04-14 16:26:00 +02:00
|
|
|
compose_fade.clear_compose();
|
|
|
|
if (page_params.narrow_topic !== undefined) {
|
2018-11-04 17:04:17 +01:00
|
|
|
compose_state.topic(page_params.narrow_topic);
|
2017-04-14 16:26:00 +02:00
|
|
|
} else {
|
2018-11-04 17:04:17 +01:00
|
|
|
compose_state.topic("");
|
2017-04-14 16:26:00 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
hide_box();
|
|
|
|
$("#compose_close").hide();
|
|
|
|
clear_box();
|
|
|
|
notifications.clear_compose_notifications();
|
|
|
|
compose.abort_xhr();
|
2020-10-28 11:35:48 +01:00
|
|
|
compose.abort_video_callbacks(undefined);
|
2017-04-15 01:15:59 +02:00
|
|
|
compose_state.set_message_type(false);
|
2018-03-06 15:07:55 +01:00
|
|
|
compose_pm_pill.clear();
|
2020-12-11 04:26:23 +01:00
|
|
|
$(document).trigger("compose_canceled.zulip");
|
2017-04-14 16:26:00 +02:00
|
|
|
};
|
|
|
|
|
2017-04-14 19:27:12 +02:00
|
|
|
exports.respond_to_message = function (opts) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let msg_type;
|
2017-04-14 19:27:12 +02:00
|
|
|
// Before initiating a reply to a message, if there's an
|
|
|
|
// in-progress composition, snapshot it.
|
|
|
|
drafts.update_draft();
|
|
|
|
|
2019-10-26 00:11:05 +02:00
|
|
|
const message = current_msg_list.selected_message();
|
2017-04-14 19:27:12 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
if (message === undefined) {
|
|
|
|
// empty narrow implementation
|
|
|
|
if (
|
|
|
|
!narrow_state.narrowed_by_pm_reply() &&
|
2017-04-21 09:33:05 +02:00
|
|
|
!narrow_state.narrowed_by_stream_reply() &&
|
2020-07-15 00:34:28 +02:00
|
|
|
!narrow_state.narrowed_by_topic_reply()
|
|
|
|
) {
|
2017-12-11 11:39:49 +01:00
|
|
|
compose.nonexistent_stream_reply_error();
|
|
|
|
return;
|
|
|
|
}
|
2019-11-02 00:06:25 +01:00
|
|
|
const current_filter = narrow_state.filter();
|
|
|
|
const first_term = current_filter.operators()[0];
|
|
|
|
const first_operator = first_term.operator;
|
|
|
|
const first_operand = first_term.operand;
|
2017-12-11 11:39:49 +01:00
|
|
|
|
2018-06-06 18:50:09 +02:00
|
|
|
if (first_operator === "stream" && !stream_data.is_subscribed(first_operand)) {
|
2017-12-11 11:39:49 +01:00
|
|
|
compose.nonexistent_stream_reply_error();
|
2017-04-21 09:33:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-06 21:43:17 +02:00
|
|
|
// Set msg_type to stream by default in the case of an empty
|
|
|
|
// home view.
|
2020-07-15 01:29:15 +02:00
|
|
|
msg_type = "stream";
|
2017-04-21 09:33:05 +02:00
|
|
|
if (narrow_state.narrowed_by_pm_reply()) {
|
2020-07-15 01:29:15 +02:00
|
|
|
msg_type = "private";
|
2017-04-21 09:33:05 +02:00
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const new_opts = fill_in_opts_from_current_narrowed_view(msg_type, opts);
|
2017-04-21 09:33:05 +02:00
|
|
|
exports.start(new_opts.message_type, new_opts);
|
2017-04-14 19:27:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-10 02:03:41 +02:00
|
|
|
if (current_msg_list.can_mark_messages_read()) {
|
|
|
|
unread_ops.notify_server_message_read(message);
|
|
|
|
}
|
2017-04-14 19:27:12 +02:00
|
|
|
|
2019-12-02 18:01:31 +01:00
|
|
|
// Important note: A reply_type of 'personal' is for the R hotkey
|
|
|
|
// (replying to a message's sender with a private message). All
|
|
|
|
// other replies can just copy message.type.
|
2020-07-15 01:29:15 +02:00
|
|
|
if (opts.reply_type === "personal" || message.type === "private") {
|
|
|
|
msg_type = "private";
|
2019-12-02 18:01:31 +01:00
|
|
|
} else {
|
|
|
|
msg_type = message.type;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
let stream = "";
|
|
|
|
let topic = "";
|
|
|
|
let pm_recipient = "";
|
2019-12-02 18:01:31 +01:00
|
|
|
if (msg_type === "stream") {
|
2017-04-14 19:27:12 +02:00
|
|
|
stream = message.stream;
|
2020-02-19 00:04:12 +01:00
|
|
|
topic = message.topic;
|
2019-12-02 18:01:31 +01:00
|
|
|
} else {
|
|
|
|
pm_recipient = message.reply_to;
|
2017-04-14 19:27:12 +02:00
|
|
|
if (opts.reply_type === "personal") {
|
|
|
|
// reply_to for private messages is everyone involved, so for
|
|
|
|
// personals replies we need to set the private message
|
|
|
|
// recipient to just the sender
|
2020-02-05 14:30:59 +01:00
|
|
|
pm_recipient = people.get_by_user_id(message.sender_id).email;
|
2017-04-14 19:27:12 +02:00
|
|
|
} else {
|
|
|
|
pm_recipient = people.pm_reply_to(message);
|
|
|
|
}
|
|
|
|
}
|
2019-12-02 18:01:31 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
exports.start(msg_type, {
|
2020-07-20 22:18:43 +02:00
|
|
|
stream,
|
|
|
|
topic,
|
2020-07-15 00:34:28 +02:00
|
|
|
private_message_recipient: pm_recipient,
|
|
|
|
trigger: opts.trigger,
|
|
|
|
});
|
2017-04-14 19:27:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.reply_with_mention = function (opts) {
|
|
|
|
exports.respond_to_message(opts);
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = current_msg_list.selected_message();
|
|
|
|
const mention = people.get_mention_syntax(message.sender_full_name, message.sender_id);
|
2018-02-26 16:04:40 +01:00
|
|
|
compose_ui.insert_syntax_and_focus(mention);
|
2017-04-14 19:27:12 +02:00
|
|
|
};
|
|
|
|
|
2017-04-19 17:21:49 +02:00
|
|
|
exports.on_topic_narrow = function () {
|
|
|
|
if (!compose_state.composing()) {
|
|
|
|
// If our compose box is closed, then just
|
|
|
|
// leave it closed, assuming that the user is
|
|
|
|
// catching up on their feed and not actively
|
|
|
|
// composing.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-25 15:25:31 +02:00
|
|
|
if (compose_state.stream_name() !== narrow_state.stream()) {
|
2017-04-19 17:21:49 +02:00
|
|
|
// If we changed streams, then we only leave the
|
|
|
|
// compose box open if there is content.
|
|
|
|
if (compose_state.has_message_content()) {
|
|
|
|
compose_fade.update_message_list();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, avoid a mix.
|
|
|
|
exports.cancel();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-04 17:04:17 +01:00
|
|
|
if (compose_state.topic() && compose_state.has_message_content()) {
|
2018-04-03 02:43:22 +02:00
|
|
|
// If the user has written something to a different topic,
|
|
|
|
// they probably want that content, so leave compose open.
|
|
|
|
//
|
|
|
|
// This effectively uses the heuristic of whether there is
|
|
|
|
// content in compose to determine whether the user had firmly
|
|
|
|
// decided to compose to the old topic or is just looking to
|
|
|
|
// reply to what they see.
|
|
|
|
compose_fade.update_message_list();
|
2017-04-19 17:21:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-03 02:43:22 +02:00
|
|
|
// If we got this far, then the compose box has the correct stream
|
|
|
|
// filled in, and either compose is empty or no topic was set, so
|
|
|
|
// we should update the compose topic to match the new narrow.
|
|
|
|
// See #3300 for context--a couple users specifically asked for
|
|
|
|
// this convenience.
|
2018-11-04 17:04:17 +01:00
|
|
|
compose_state.topic(narrow_state.topic());
|
2018-04-06 00:21:02 +02:00
|
|
|
compose_fade.set_focused_recipient("stream");
|
|
|
|
compose_fade.update_message_list();
|
2020-07-20 21:24:26 +02:00
|
|
|
$("#compose-textarea").trigger("focus").trigger("select");
|
2017-04-19 17:21:49 +02:00
|
|
|
};
|
|
|
|
|
2018-01-31 04:23:02 +01:00
|
|
|
exports.quote_and_reply = function (opts) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const textarea = $("#compose-textarea");
|
|
|
|
const message_id = current_msg_list.selected_id();
|
|
|
|
const message = current_msg_list.selected_message();
|
2018-01-31 04:23:02 +01:00
|
|
|
|
2019-01-16 16:29:18 +01:00
|
|
|
if (compose_state.has_message_content()) {
|
|
|
|
// The user already started typing a message,
|
|
|
|
// so we won't re-open the compose box.
|
|
|
|
// (If you did re-open the compose box, you
|
|
|
|
// are prone to glitches where you select the
|
|
|
|
// text, plus it's a complicated codepath that
|
|
|
|
// can have other unintended consequences.)
|
|
|
|
//
|
|
|
|
// Note also that we always put the quoted text
|
|
|
|
// above the current text, which explains us
|
|
|
|
// moving the caret below. I think this is what
|
|
|
|
// most users will want, and it's consistent with
|
|
|
|
// the behavior we had on FF before this change
|
|
|
|
// (which may have been an accident of
|
|
|
|
// implementation). If we change this decision,
|
|
|
|
// we'll need to make `insert_syntax_and_focus`
|
|
|
|
// smarter about newlines.
|
|
|
|
textarea.caret(0);
|
|
|
|
} else {
|
|
|
|
exports.respond_to_message(opts);
|
|
|
|
}
|
|
|
|
|
2018-11-01 23:43:29 +01:00
|
|
|
compose_ui.insert_syntax_and_focus("[Quoting…]\n", textarea);
|
|
|
|
|
2019-08-26 08:10:46 +02:00
|
|
|
function replace_content(message) {
|
|
|
|
// Final message looks like:
|
|
|
|
// @_**Iago|5** [said](link to message):
|
|
|
|
// ```quote
|
|
|
|
// message content
|
|
|
|
// ```
|
|
|
|
let content = `@_**${message.sender_full_name}|${message.sender_id}** `;
|
|
|
|
content += `[said](${hash_util.by_conversation_and_time_uri(message)}):\n`;
|
2019-08-26 09:04:34 +02:00
|
|
|
const fence = fenced_code.get_unused_fence(message.raw_content);
|
|
|
|
content += `${fence}quote\n${message.raw_content}\n${fence}`;
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_ui.replace_syntax("[Quoting…]", content, textarea);
|
|
|
|
autosize.update($("#compose-textarea"));
|
2019-01-16 15:56:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (message && message.raw_content) {
|
2019-08-26 08:10:46 +02:00
|
|
|
replace_content(message);
|
2018-11-11 19:36:26 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-31 04:23:02 +01:00
|
|
|
channel.get({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/messages/" + message_id,
|
2018-01-31 04:23:02 +01:00
|
|
|
idempotent: true,
|
2020-07-20 22:18:43 +02:00
|
|
|
success(data) {
|
2018-11-15 08:54:20 +01:00
|
|
|
message.raw_content = data.raw_content;
|
2019-08-26 08:10:46 +02:00
|
|
|
replace_content(message);
|
2018-01-31 04:23:02 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-02-16 15:56:25 +01:00
|
|
|
exports.on_narrow = function (opts) {
|
|
|
|
// We use force_close when jumping between PM narrows with the "p" key,
|
|
|
|
// so that we don't have an open compose box that makes it difficult
|
|
|
|
// to cycle quickly through unread messages.
|
|
|
|
if (opts.force_close) {
|
|
|
|
// This closes the compose box if it was already open, and it is
|
|
|
|
// basically a noop otherwise.
|
|
|
|
exports.cancel();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-30 00:20:10 +01:00
|
|
|
if (opts.trigger === "narrow_to_compose_target") {
|
|
|
|
compose_fade.update_message_list();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-25 15:25:31 +02:00
|
|
|
if (narrow_state.narrowed_by_topic_reply()) {
|
2017-04-19 17:21:49 +02:00
|
|
|
exports.on_topic_narrow();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-16 16:48:31 +02:00
|
|
|
if (compose_state.has_message_content()) {
|
|
|
|
compose_fade.update_message_list();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-25 15:25:31 +02:00
|
|
|
if (narrow_state.narrowed_by_pm_reply()) {
|
2020-07-15 01:29:15 +02:00
|
|
|
opts = fill_in_opts_from_current_narrowed_view("private", opts);
|
2018-05-28 11:44:28 +02:00
|
|
|
// Do not open compose box if triggered by search and invalid recipient
|
|
|
|
// is present.
|
|
|
|
if (opts.trigger === "search" && !opts.private_message_recipient) {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-15 01:29:15 +02:00
|
|
|
exports.start("private");
|
2017-04-16 16:48:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got this far, then we assume the user is now in "reading"
|
|
|
|
// mode, so we close the compose box to make it easier to use navigation
|
|
|
|
// hotkeys and to provide more screen real estate for messages.
|
|
|
|
exports.cancel();
|
|
|
|
};
|
2017-04-14 16:26:00 +02:00
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.compose_actions = exports;
|