2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-07-28 00:14:57 +02:00
|
|
|
const ClipboardJS = require("clipboard");
|
2020-07-28 00:26:58 +02:00
|
|
|
const XDate = require("xdate");
|
2020-07-28 00:14:57 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const render_message_edit_form = require("../templates/message_edit_form.hbs");
|
|
|
|
const render_topic_edit_form = require("../templates/topic_edit_form.hbs");
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2020-02-06 06:23:06 +01:00
|
|
|
const currently_editing_messages = new Map();
|
2019-11-02 00:06:25 +01:00
|
|
|
let currently_deleting_messages = [];
|
2020-02-06 06:24:12 +01:00
|
|
|
const currently_echoing_messages = new Map();
|
2013-05-15 00:22:16 +02:00
|
|
|
|
2020-06-03 16:44:57 +02:00
|
|
|
// These variables are designed to preserve the user's most recent
|
|
|
|
// choices when editing a group of messages, to make it convenient to
|
|
|
|
// move several topics in a row with the same settings.
|
|
|
|
exports.notify_old_thread_default = true;
|
|
|
|
exports.notify_new_thread_default = true;
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const editability_types = {
|
2016-10-22 04:26:35 +02:00
|
|
|
NO: 1,
|
|
|
|
NO_LONGER: 2,
|
|
|
|
// Note: TOPIC_ONLY does not include stream messages with no topic sent
|
|
|
|
// by someone else. You can edit the topic of such a message by editing
|
|
|
|
// the topic of the whole recipient_row it appears in, but you can't
|
|
|
|
// directly edit the topic of such a message.
|
|
|
|
// Similar story for messages whose topic you can change only because
|
|
|
|
// you are an admin.
|
|
|
|
TOPIC_ONLY: 3,
|
2017-01-12 00:17:43 +01:00
|
|
|
FULL: 4,
|
2016-10-22 04:26:35 +02:00
|
|
|
};
|
|
|
|
exports.editability_types = editability_types;
|
|
|
|
|
2018-04-25 23:15:04 +02:00
|
|
|
function is_topic_editable(message, edit_limit_seconds_buffer) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const now = new XDate();
|
2018-04-25 23:15:04 +02:00
|
|
|
edit_limit_seconds_buffer = edit_limit_seconds_buffer || 0;
|
|
|
|
|
2018-10-09 09:54:57 +02:00
|
|
|
if (!page_params.realm_allow_message_editing) {
|
|
|
|
// If message editing is disabled, so is topic editing.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Organization admins and message senders can edit message topics indefinitely.
|
|
|
|
if (page_params.is_admin) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (message.sent_by_me) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!page_params.realm_allow_community_topic_editing) {
|
|
|
|
// If you're another non-admin user, you need community topic editing enabled.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If you're using community topic editing, there's a deadline.
|
2020-07-15 00:34:28 +02:00
|
|
|
return (
|
|
|
|
page_params.realm_community_topic_editing_limit_seconds +
|
|
|
|
edit_limit_seconds_buffer +
|
|
|
|
now.diffSeconds(message.timestamp * 1000) >
|
|
|
|
0
|
|
|
|
);
|
2018-04-25 23:15:04 +02:00
|
|
|
}
|
|
|
|
|
2016-12-05 07:02:18 +01:00
|
|
|
function get_editability(message, edit_limit_seconds_buffer) {
|
2016-10-22 04:26:35 +02:00
|
|
|
edit_limit_seconds_buffer = edit_limit_seconds_buffer || 0;
|
2017-12-03 00:56:17 +01:00
|
|
|
if (!message) {
|
|
|
|
return editability_types.NO;
|
|
|
|
}
|
2018-04-25 23:15:04 +02:00
|
|
|
if (!is_topic_editable(message, edit_limit_seconds_buffer)) {
|
2016-11-10 21:17:30 +01:00
|
|
|
return editability_types.NO;
|
|
|
|
}
|
|
|
|
if (message.failed_request) {
|
2017-07-18 14:44:29 +02:00
|
|
|
// TODO: For completely failed requests, we should be able
|
|
|
|
// to "edit" the message, but it won't really be like
|
|
|
|
// other message updates. This commit changed the result
|
|
|
|
// from FULL to NO, since the prior implementation was
|
|
|
|
// buggy.
|
|
|
|
return editability_types.NO;
|
2016-11-10 21:17:30 +01:00
|
|
|
}
|
2017-07-18 14:44:29 +02:00
|
|
|
|
2016-12-29 23:50:24 +01:00
|
|
|
// Locally echoed messages are not editable, since the message hasn't
|
|
|
|
// finished being sent yet.
|
2017-07-17 16:52:57 +02:00
|
|
|
if (message.locally_echoed) {
|
2016-11-10 21:17:30 +01:00
|
|
|
return editability_types.NO;
|
|
|
|
}
|
2017-07-18 14:44:29 +02:00
|
|
|
|
2016-11-10 21:17:30 +01:00
|
|
|
if (!page_params.realm_allow_message_editing) {
|
2016-10-22 04:26:35 +02:00
|
|
|
return editability_types.NO;
|
|
|
|
}
|
2017-07-18 14:44:29 +02:00
|
|
|
|
2017-12-03 00:56:17 +01:00
|
|
|
if (page_params.realm_message_content_edit_limit_seconds === 0 && message.sent_by_me) {
|
2016-10-22 04:26:35 +02:00
|
|
|
return editability_types.FULL;
|
|
|
|
}
|
|
|
|
|
2020-02-06 06:24:12 +01:00
|
|
|
if (currently_echoing_messages.has(message.id)) {
|
2019-04-22 20:13:23 +02:00
|
|
|
return editability_types.NO;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const now = new XDate();
|
2020-07-15 00:34:28 +02:00
|
|
|
if (
|
|
|
|
page_params.realm_message_content_edit_limit_seconds +
|
|
|
|
edit_limit_seconds_buffer +
|
|
|
|
now.diffSeconds(message.timestamp * 1000) >
|
|
|
|
0 &&
|
|
|
|
message.sent_by_me
|
|
|
|
) {
|
2016-10-22 04:26:35 +02:00
|
|
|
return editability_types.FULL;
|
|
|
|
}
|
2017-12-03 00:56:17 +01:00
|
|
|
|
2016-10-22 04:26:35 +02:00
|
|
|
// time's up!
|
2020-07-15 01:29:15 +02:00
|
|
|
if (message.type === "stream") {
|
2016-10-22 04:26:35 +02:00
|
|
|
return editability_types.TOPIC_ONLY;
|
|
|
|
}
|
|
|
|
return editability_types.NO_LONGER;
|
|
|
|
}
|
|
|
|
exports.get_editability = get_editability;
|
2018-04-25 23:15:04 +02:00
|
|
|
exports.is_topic_editable = is_topic_editable;
|
2013-08-16 23:45:13 +02:00
|
|
|
|
2017-11-26 09:12:10 +01:00
|
|
|
exports.get_deletability = function (message) {
|
|
|
|
if (page_params.is_admin) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!message.sent_by_me) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (message.locally_echoed) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!page_params.realm_allow_message_deleting) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (page_params.realm_message_content_delete_limit_seconds === 0) {
|
|
|
|
// This means no time limit for message deletion.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (page_params.realm_allow_message_deleting) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const now = new XDate();
|
2020-07-15 00:34:28 +02:00
|
|
|
if (
|
|
|
|
page_params.realm_message_content_delete_limit_seconds +
|
|
|
|
now.diffSeconds(message.timestamp * 1000) >
|
|
|
|
0
|
|
|
|
) {
|
2017-11-26 09:12:10 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2018-03-05 18:28:37 +01:00
|
|
|
exports.update_message_topic_editing_pencil = function () {
|
|
|
|
if (page_params.realm_allow_message_editing) {
|
|
|
|
$(".on_hover_topic_edit, .always_visible_topic_edit").show();
|
|
|
|
} else {
|
|
|
|
$(".on_hover_topic_edit, .always_visible_topic_edit").hide();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-23 08:44:55 +01:00
|
|
|
exports.show_topic_edit_spinner = function (row) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const spinner = row.find(".topic_edit_spinner");
|
2018-03-23 08:44:55 +01:00
|
|
|
loading.make_indicator(spinner);
|
2020-07-22 03:52:06 +02:00
|
|
|
spinner.css({height: ""});
|
2018-03-23 08:44:55 +01:00
|
|
|
$(".topic_edit_save").hide();
|
|
|
|
$(".topic_edit_cancel").hide();
|
|
|
|
};
|
|
|
|
|
2020-04-16 12:50:09 +02:00
|
|
|
exports.end_if_focused_on_inline_topic_edit = function () {
|
2020-07-15 01:29:15 +02:00
|
|
|
const focused_elem = $(".topic_edit_form").find(":focus");
|
2020-04-16 12:50:09 +02:00
|
|
|
if (focused_elem.length === 1) {
|
2020-07-20 21:24:26 +02:00
|
|
|
focused_elem.trigger("blur");
|
2020-07-15 01:29:15 +02:00
|
|
|
const recipient_row = focused_elem.closest(".recipient_row");
|
2020-04-16 12:50:09 +02:00
|
|
|
exports.end_inline_topic_edit(recipient_row);
|
|
|
|
}
|
|
|
|
};
|
2020-04-02 19:39:10 +02:00
|
|
|
|
2020-04-16 12:50:09 +02:00
|
|
|
exports.end_if_focused_on_message_row_edit = function () {
|
2020-07-15 01:29:15 +02:00
|
|
|
const focused_elem = $(".message_edit").find(":focus");
|
2020-04-02 19:39:10 +02:00
|
|
|
if (focused_elem.length === 1) {
|
2020-07-20 21:24:26 +02:00
|
|
|
focused_elem.trigger("blur");
|
2020-07-15 01:29:15 +02:00
|
|
|
const row = focused_elem.closest(".message_row");
|
2020-04-13 15:46:41 +02:00
|
|
|
exports.end_message_row_edit(row);
|
2020-04-02 19:39:10 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-19 12:48:19 +02:00
|
|
|
function handle_message_row_edit_keydown(e) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const code = e.keyCode || e.which;
|
2020-04-20 07:23:19 +02:00
|
|
|
switch (code) {
|
2020-07-15 02:14:03 +02:00
|
|
|
case 13:
|
|
|
|
if ($(e.target).hasClass("message_edit_content")) {
|
2020-07-15 00:34:28 +02:00
|
|
|
// Pressing enter to save edits is coupled with enter to send
|
2020-07-15 02:14:03 +02:00
|
|
|
if (composebox_typeahead.should_enter_send(e)) {
|
|
|
|
const row = $(".message_edit_content").filter(":focus").closest(".message_row");
|
|
|
|
const message_edit_save_button = row.find(".message_edit_save");
|
2020-07-22 02:59:06 +02:00
|
|
|
if (message_edit_save_button.prop("disabled")) {
|
2020-07-15 00:34:28 +02:00
|
|
|
// In cases when the save button is disabled
|
|
|
|
// we need to disable save on pressing enter
|
|
|
|
// Prevent default to avoid new-line on pressing
|
|
|
|
// enter inside the textarea in this case
|
2020-07-15 02:14:03 +02:00
|
|
|
e.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
exports.save_message_row_edit(row);
|
|
|
|
e.stopPropagation();
|
2020-04-19 12:48:19 +02:00
|
|
|
e.preventDefault();
|
2020-07-15 02:14:03 +02:00
|
|
|
} else {
|
|
|
|
composebox_typeahead.handle_enter($(e.target), e);
|
2020-04-20 07:23:19 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-07-15 00:34:28 +02:00
|
|
|
} else if (
|
|
|
|
$(e.target).hasClass("message_edit_topic") ||
|
|
|
|
$(e.target).hasClass("message_edit_topic_propagate")
|
|
|
|
) {
|
2020-07-15 02:14:03 +02:00
|
|
|
const row = $(e.target).closest(".message_row");
|
2020-04-13 15:46:41 +02:00
|
|
|
exports.save_message_row_edit(row);
|
2020-04-20 07:23:19 +02:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2019-04-16 03:37:33 +02:00
|
|
|
}
|
2020-07-15 02:14:03 +02:00
|
|
|
return;
|
|
|
|
case 27: // Handle escape keys in the message_edit form.
|
|
|
|
exports.end_if_focused_on_message_row_edit();
|
2020-04-20 07:23:19 +02:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2020-07-15 02:14:03 +02:00
|
|
|
return;
|
|
|
|
default:
|
|
|
|
return;
|
2020-04-19 12:48:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handle_inline_topic_edit_keydown(e) {
|
|
|
|
let row;
|
|
|
|
const code = e.keyCode || e.which;
|
|
|
|
switch (code) {
|
2020-07-15 02:14:03 +02:00
|
|
|
case 13: // Handle enter key in the recipient bar/inline topic edit form
|
|
|
|
row = $(e.target).closest(".recipient_row");
|
|
|
|
exports.save_inline_topic_edit(row);
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
return;
|
|
|
|
case 27: // handle escape
|
|
|
|
exports.end_if_focused_on_inline_topic_edit();
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
return;
|
2016-10-26 01:15:23 +02:00
|
|
|
}
|
2013-08-02 19:25:37 +02:00
|
|
|
}
|
|
|
|
|
2016-07-08 02:25:55 +02:00
|
|
|
function timer_text(seconds_left) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const minutes = Math.floor(seconds_left / 60);
|
|
|
|
const seconds = seconds_left % 60;
|
2016-07-08 02:25:55 +02:00
|
|
|
if (minutes >= 1) {
|
2016-12-03 03:08:47 +01:00
|
|
|
return i18n.t("__minutes__ min to edit", {minutes: minutes.toString()});
|
2016-07-08 02:25:55 +02:00
|
|
|
} else if (seconds_left >= 10) {
|
2020-07-15 00:34:28 +02:00
|
|
|
return i18n.t("__seconds__ sec to edit", {seconds: (seconds - (seconds % 5)).toString()});
|
2016-07-08 02:25:55 +02:00
|
|
|
}
|
2016-12-03 03:08:47 +01:00
|
|
|
return i18n.t("__seconds__ sec to edit", {seconds: seconds.toString()});
|
2016-07-08 02:25:55 +02:00
|
|
|
}
|
|
|
|
|
2016-12-05 07:02:18 +01:00
|
|
|
function edit_message(row, raw_content) {
|
2017-03-08 16:53:57 +01:00
|
|
|
row.find(".message_reactions").hide();
|
2017-03-15 15:26:39 +01:00
|
|
|
condense.hide_message_expander(row);
|
2020-07-15 00:34:28 +02:00
|
|
|
const content_top = row.find(".message_top_line")[0].getBoundingClientRect().top;
|
2013-07-22 20:19:35 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = current_msg_list.get(rows.id(row));
|
2013-08-02 19:25:37 +02:00
|
|
|
|
2016-07-08 02:25:55 +02:00
|
|
|
// We potentially got to this function by clicking a button that implied the
|
|
|
|
// user would be able to edit their message. Give a little bit of buffer in
|
|
|
|
// case the button has been around for a bit, e.g. we show the
|
|
|
|
// edit_content_button (hovering pencil icon) as long as the user would have
|
|
|
|
// been able to click it at the time the mouse entered the message_row. Also
|
|
|
|
// a buffer in case their computer is slow, or stalled for a second, etc
|
|
|
|
// If you change this number also change edit_limit_buffer in
|
2020-06-22 22:57:01 +02:00
|
|
|
// zerver.views.message_edit.update_message_backend
|
2019-11-02 00:06:25 +01:00
|
|
|
const seconds_left_buffer = 5;
|
|
|
|
const editability = get_editability(message, seconds_left_buffer);
|
2020-07-15 00:34:28 +02:00
|
|
|
const is_editable =
|
|
|
|
editability === exports.editability_types.TOPIC_ONLY ||
|
|
|
|
editability === exports.editability_types.FULL;
|
2020-05-08 06:57:19 +02:00
|
|
|
const max_file_upload_size = page_params.max_file_upload_size_mib;
|
2019-11-02 00:06:25 +01:00
|
|
|
let file_upload_enabled = false;
|
2019-05-04 20:24:36 +02:00
|
|
|
|
|
|
|
if (max_file_upload_size > 0) {
|
|
|
|
file_upload_enabled = true;
|
|
|
|
}
|
2016-10-23 03:19:16 +02:00
|
|
|
|
2020-04-08 00:23:15 +02:00
|
|
|
const show_video_chat_button = compose.compute_show_video_chat_button();
|
|
|
|
|
2020-05-06 12:07:34 +02:00
|
|
|
const show_edit_stream = message.is_stream && page_params.is_admin;
|
|
|
|
// current message's stream has been already been added and selected in handlebar
|
2020-07-15 00:34:28 +02:00
|
|
|
const available_streams = show_edit_stream
|
|
|
|
? stream_data.subscribed_subs().filter((s) => s.stream_id !== message.stream_id)
|
|
|
|
: null;
|
|
|
|
|
|
|
|
const form = $(
|
|
|
|
render_message_edit_form({
|
|
|
|
is_stream: message.type === "stream",
|
|
|
|
message_id: message.id,
|
2020-07-20 22:18:43 +02:00
|
|
|
is_editable,
|
2020-07-15 00:34:28 +02:00
|
|
|
is_content_editable: editability === exports.editability_types.FULL,
|
|
|
|
has_been_editable: editability !== editability_types.NO,
|
|
|
|
topic: message.topic,
|
|
|
|
content: raw_content,
|
2020-07-20 22:18:43 +02:00
|
|
|
file_upload_enabled,
|
|
|
|
show_video_chat_button,
|
2020-07-15 00:34:28 +02:00
|
|
|
minutes_to_edit: Math.floor(page_params.realm_message_content_edit_limit_seconds / 60),
|
2020-07-20 22:18:43 +02:00
|
|
|
show_edit_stream,
|
|
|
|
available_streams,
|
2020-07-15 00:34:28 +02:00
|
|
|
stream_id: message.stream_id,
|
|
|
|
stream_name: message.stream,
|
|
|
|
notify_new_thread: exports.notify_new_thread_default,
|
|
|
|
notify_old_thread: exports.notify_old_thread_default,
|
|
|
|
}),
|
|
|
|
);
|
2016-10-23 03:19:16 +02:00
|
|
|
|
2020-07-20 22:18:43 +02:00
|
|
|
const edit_obj = {form, raw_content};
|
2020-02-06 06:23:06 +01:00
|
|
|
currently_editing_messages.set(message.id, edit_obj);
|
2016-10-23 03:19:16 +02:00
|
|
|
current_msg_list.show_edit_message(row, edit_obj);
|
|
|
|
|
2020-07-20 21:26:58 +02:00
|
|
|
form.on("keydown", handle_message_row_edit_keydown);
|
2016-10-23 03:19:16 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
upload.feature_check($("#attach_files_" + rows.id(row)));
|
2017-11-28 22:36:58 +01:00
|
|
|
|
2020-05-06 12:07:34 +02:00
|
|
|
const message_edit_stream = row.find("#select_stream_id_" + message.id);
|
2020-05-12 11:02:13 +02:00
|
|
|
const stream_header_colorblock = row.find(".stream_header_colorblock");
|
2020-07-15 01:29:15 +02:00
|
|
|
const message_edit_content = row.find("textarea.message_edit_content");
|
|
|
|
const message_edit_topic = row.find("input.message_edit_topic");
|
|
|
|
const message_edit_topic_propagate = row.find("select.message_edit_topic_propagate");
|
|
|
|
const message_edit_breadcrumb_messages = row.find("div.message_edit_breadcrumb_messages");
|
|
|
|
const message_edit_countdown_timer = row.find(".message_edit_countdown_timer");
|
|
|
|
const copy_message = row.find(".copy_message");
|
2017-03-26 11:02:37 +02:00
|
|
|
|
2020-05-12 11:02:13 +02:00
|
|
|
ui_util.decorate_stream_bar(message.stream, stream_header_colorblock, false);
|
2020-07-20 21:26:58 +02:00
|
|
|
message_edit_stream.on("change", function () {
|
2020-05-12 11:02:13 +02:00
|
|
|
const stream_name = stream_data.maybe_get_stream_name(parseInt(this.value, 10));
|
|
|
|
ui_util.decorate_stream_bar(stream_name, stream_header_colorblock, false);
|
|
|
|
});
|
|
|
|
|
2016-10-22 02:38:56 +02:00
|
|
|
if (editability === editability_types.NO) {
|
2020-04-16 15:30:31 +02:00
|
|
|
message_edit_content.attr("readonly", "readonly");
|
|
|
|
message_edit_topic.attr("readonly", "readonly");
|
2018-04-05 23:45:12 +02:00
|
|
|
new ClipboardJS(copy_message[0]);
|
2016-10-22 02:38:56 +02:00
|
|
|
} else if (editability === editability_types.NO_LONGER) {
|
|
|
|
// You can currently only reach this state in non-streams. If that
|
|
|
|
// changes (e.g. if we stop allowing topics to be modified forever
|
|
|
|
// in streams), then we'll need to disable
|
|
|
|
// row.find('input.message_edit_topic') as well.
|
2020-04-16 15:30:31 +02:00
|
|
|
message_edit_content.attr("readonly", "readonly");
|
2016-11-02 23:46:34 +01:00
|
|
|
message_edit_countdown_timer.text(i18n.t("View source"));
|
2018-04-05 23:45:12 +02:00
|
|
|
new ClipboardJS(copy_message[0]);
|
2016-10-22 02:38:56 +02:00
|
|
|
} else if (editability === editability_types.TOPIC_ONLY) {
|
2020-04-16 15:30:31 +02:00
|
|
|
message_edit_content.attr("readonly", "readonly");
|
2016-10-23 03:26:28 +02:00
|
|
|
// Hint why you can edit the topic but not the message content
|
2016-11-02 23:46:34 +01:00
|
|
|
message_edit_countdown_timer.text(i18n.t("Topic editing only"));
|
2018-04-05 23:45:12 +02:00
|
|
|
new ClipboardJS(copy_message[0]);
|
2016-10-22 04:26:35 +02:00
|
|
|
} else if (editability === editability_types.FULL) {
|
2017-03-26 11:02:37 +02:00
|
|
|
copy_message.remove();
|
2019-11-02 00:06:25 +01:00
|
|
|
const edit_id = "#message_edit_content_" + rows.id(row);
|
|
|
|
const listeners = resize.watch_manual_resize(edit_id);
|
2017-05-11 18:28:33 +02:00
|
|
|
if (listeners) {
|
2020-02-06 06:23:06 +01:00
|
|
|
currently_editing_messages.get(rows.id(row)).listeners = listeners;
|
2017-05-11 18:28:33 +02:00
|
|
|
}
|
2017-10-13 03:05:31 +02:00
|
|
|
composebox_typeahead.initialize_compose_typeahead(edit_id);
|
2018-08-11 06:43:38 +02:00
|
|
|
compose.handle_keyup(null, $(edit_id).expectOne());
|
2020-07-15 01:29:15 +02:00
|
|
|
$(edit_id).on("keydown", function (event) {
|
2018-07-18 05:59:55 +02:00
|
|
|
compose.handle_keydown(event, $(this).expectOne());
|
|
|
|
});
|
2020-07-15 01:29:15 +02:00
|
|
|
$(edit_id).on("keyup", function (event) {
|
2018-08-11 06:43:38 +02:00
|
|
|
compose.handle_keyup(event, $(this).expectOne());
|
|
|
|
});
|
2016-07-08 02:25:55 +02:00
|
|
|
}
|
|
|
|
|
2016-10-23 03:31:39 +02:00
|
|
|
// Add tooltip
|
2020-07-15 00:34:28 +02:00
|
|
|
if (
|
|
|
|
editability !== editability_types.NO &&
|
|
|
|
page_params.realm_message_content_edit_limit_seconds > 0
|
|
|
|
) {
|
2020-07-15 01:29:15 +02:00
|
|
|
row.find(".message-edit-timer-control-group").show();
|
|
|
|
row.find("#message_edit_tooltip").tooltip({
|
2016-10-23 03:31:39 +02:00
|
|
|
animation: false,
|
2020-07-15 01:29:15 +02:00
|
|
|
placement: "left",
|
2020-07-15 00:34:28 +02:00
|
|
|
template:
|
|
|
|
'<div class="tooltip" role="tooltip"><div class="tooltip-arrow"></div>' +
|
2017-01-12 00:17:43 +01:00
|
|
|
'<div class="tooltip-inner message-edit-tooltip-inner"></div></div>',
|
2016-10-23 03:31:39 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-23 03:35:42 +02:00
|
|
|
// add timer
|
2020-07-15 00:34:28 +02:00
|
|
|
if (
|
|
|
|
editability === editability_types.FULL &&
|
|
|
|
page_params.realm_message_content_edit_limit_seconds > 0
|
|
|
|
) {
|
2016-10-22 02:33:50 +02:00
|
|
|
// Give them at least 10 seconds.
|
|
|
|
// If you change this number also change edit_limit_buffer in
|
2020-06-22 22:57:01 +02:00
|
|
|
// zerver.views.message_edit.update_message_backend
|
2019-11-02 00:06:25 +01:00
|
|
|
const min_seconds_to_edit = 10;
|
|
|
|
const now = new XDate();
|
2020-07-15 00:34:28 +02:00
|
|
|
let seconds_left =
|
|
|
|
page_params.realm_message_content_edit_limit_seconds +
|
2016-10-22 04:26:35 +02:00
|
|
|
now.diffSeconds(message.timestamp * 1000);
|
2016-10-22 02:33:50 +02:00
|
|
|
seconds_left = Math.floor(Math.max(seconds_left, min_seconds_to_edit));
|
2016-10-23 03:35:42 +02:00
|
|
|
|
2016-11-02 23:46:34 +01:00
|
|
|
// I believe this needs to be defined outside the countdown_timer, since
|
2016-10-23 03:35:42 +02:00
|
|
|
// row just refers to something like the currently selected message, and
|
|
|
|
// can change out from under us
|
2020-07-15 01:29:15 +02:00
|
|
|
const message_edit_save = row.find("button.message_edit_save");
|
2016-10-23 03:35:42 +02:00
|
|
|
// Do this right away, rather than waiting for the timer to do its first update,
|
|
|
|
// since otherwise there is a noticeable lag
|
2016-11-02 23:46:34 +01:00
|
|
|
message_edit_countdown_timer.text(timer_text(seconds_left));
|
2020-07-02 01:45:54 +02:00
|
|
|
const countdown_timer = setInterval(() => {
|
2017-02-21 06:41:07 +01:00
|
|
|
seconds_left -= 1;
|
|
|
|
if (seconds_left <= 0) {
|
2016-10-23 03:35:42 +02:00
|
|
|
clearInterval(countdown_timer);
|
2016-11-02 23:46:34 +01:00
|
|
|
message_edit_content.prop("readonly", "readonly");
|
2020-07-15 01:29:15 +02:00
|
|
|
if (message.type === "stream") {
|
2016-11-02 23:46:34 +01:00
|
|
|
message_edit_topic.prop("readonly", "readonly");
|
|
|
|
message_edit_topic_propagate.hide();
|
2020-06-03 16:44:57 +02:00
|
|
|
message_edit_breadcrumb_messages.hide();
|
2016-10-23 03:35:42 +02:00
|
|
|
}
|
2016-10-22 04:26:35 +02:00
|
|
|
// We don't go directly to a "TOPIC_ONLY" type state (with an active Save button),
|
2016-10-23 03:35:42 +02:00
|
|
|
// since it isn't clear what to do with the half-finished edit. It's nice to keep
|
|
|
|
// the half-finished edit around so that they can copy-paste it, but we don't want
|
|
|
|
// people to think "Save" will save the half-finished edit.
|
2016-11-02 23:46:34 +01:00
|
|
|
message_edit_save.addClass("disabled");
|
|
|
|
message_edit_countdown_timer.text(i18n.t("Time's up!"));
|
2016-10-23 03:35:42 +02:00
|
|
|
} else {
|
2016-11-02 23:46:34 +01:00
|
|
|
message_edit_countdown_timer.text(timer_text(seconds_left));
|
2016-10-23 03:35:42 +02:00
|
|
|
}
|
|
|
|
}, 1000);
|
2016-07-08 02:25:55 +02:00
|
|
|
}
|
|
|
|
|
2016-10-22 02:38:56 +02:00
|
|
|
if (!is_editable) {
|
2020-07-20 21:24:26 +02:00
|
|
|
row.find(".message_edit_close").trigger("focus");
|
2020-07-15 01:29:15 +02:00
|
|
|
} else if (message.type === "stream" && message.topic === compose.empty_topic_placeholder()) {
|
|
|
|
message_edit_topic.val("");
|
2020-07-20 21:24:26 +02:00
|
|
|
message_edit_topic.trigger("focus");
|
2016-10-25 22:56:28 +02:00
|
|
|
} else if (editability === editability_types.TOPIC_ONLY) {
|
2020-07-20 21:24:26 +02:00
|
|
|
row.find(".message_edit_topic").trigger("focus");
|
2013-07-17 16:24:54 +02:00
|
|
|
} else {
|
2020-07-20 21:24:26 +02:00
|
|
|
message_edit_content.trigger("focus");
|
2016-11-03 02:03:02 +01:00
|
|
|
// Put cursor at end of input.
|
2019-11-02 00:06:25 +01:00
|
|
|
const contents = message_edit_content.val();
|
2020-07-15 01:29:15 +02:00
|
|
|
message_edit_content.val("");
|
2016-11-03 02:03:02 +01:00
|
|
|
message_edit_content.val(contents);
|
2013-07-17 16:24:54 +02:00
|
|
|
}
|
2013-07-22 20:19:35 +02:00
|
|
|
|
2019-06-25 07:53:57 +02:00
|
|
|
// Scroll to keep the top of the message content text in the same
|
|
|
|
// place visually, adjusting for border and padding.
|
2019-11-02 00:06:25 +01:00
|
|
|
const edit_top = message_edit_content[0].getBoundingClientRect().top;
|
|
|
|
const scroll_by = edit_top - content_top + 5 - 14;
|
2019-06-25 08:26:53 +02:00
|
|
|
|
2013-07-22 20:19:35 +02:00
|
|
|
edit_obj.scrolled_by = scroll_by;
|
2017-03-10 23:48:51 +01:00
|
|
|
message_viewport.scrollTop(message_viewport.scrollTop() + scroll_by);
|
2013-09-04 20:30:33 +02:00
|
|
|
|
2020-05-06 12:07:34 +02:00
|
|
|
const original_stream_id = message.stream_id;
|
|
|
|
const original_topic = message.topic;
|
|
|
|
function set_propagate_selector_display() {
|
|
|
|
const new_topic = message_edit_topic.val();
|
|
|
|
const new_stream_id = parseInt(message_edit_stream.val(), 10);
|
|
|
|
const is_topic_edited = new_topic !== original_topic && new_topic !== "";
|
|
|
|
const is_stream_edited = new_stream_id !== original_stream_id;
|
|
|
|
message_edit_topic_propagate.toggle(is_topic_edited || is_stream_edited);
|
2020-06-03 16:44:57 +02:00
|
|
|
message_edit_breadcrumb_messages.toggle(is_stream_edited);
|
2020-05-06 12:07:34 +02:00
|
|
|
}
|
|
|
|
|
2020-02-27 14:13:35 +01:00
|
|
|
if (!message.locally_echoed) {
|
2020-07-20 21:26:58 +02:00
|
|
|
message_edit_topic.on("keyup", () => {
|
2020-05-06 12:07:34 +02:00
|
|
|
set_propagate_selector_display();
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
message_edit_stream.on("change", () => {
|
2020-05-06 12:07:34 +02:00
|
|
|
set_propagate_selector_display();
|
2013-09-13 18:12:29 +02:00
|
|
|
});
|
2013-09-04 20:30:33 +02:00
|
|
|
}
|
2013-05-15 00:22:16 +02:00
|
|
|
}
|
|
|
|
|
2014-01-02 19:39:22 +01:00
|
|
|
function start_edit_maintaining_scroll(row, content) {
|
|
|
|
edit_message(row, content);
|
2019-11-02 00:06:25 +01:00
|
|
|
const row_bottom = row.height() + row.offset().top;
|
|
|
|
const composebox_top = $("#compose").offset().top;
|
2014-01-02 19:39:22 +01:00
|
|
|
if (row_bottom > composebox_top) {
|
2017-03-10 23:48:51 +01:00
|
|
|
message_viewport.scrollTop(message_viewport.scrollTop() + row_bottom - composebox_top);
|
2014-01-02 19:39:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-06 18:51:05 +02:00
|
|
|
function start_edit_with_content(row, content, edit_box_open_callback) {
|
|
|
|
start_edit_maintaining_scroll(row, content);
|
|
|
|
if (edit_box_open_callback) {
|
|
|
|
edit_box_open_callback();
|
|
|
|
}
|
2018-03-05 09:10:05 +01:00
|
|
|
|
2019-11-21 05:24:55 +01:00
|
|
|
upload.setup_upload({
|
2020-07-15 01:29:15 +02:00
|
|
|
mode: "edit",
|
2019-11-21 05:24:55 +01:00
|
|
|
row: rows.id(row),
|
|
|
|
});
|
2017-06-06 18:51:05 +02:00
|
|
|
}
|
|
|
|
|
2016-11-23 05:06:34 +01:00
|
|
|
exports.start = function (row, edit_box_open_callback) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = current_msg_list.get(rows.id(row));
|
2017-08-28 01:10:55 +02:00
|
|
|
if (message === undefined) {
|
|
|
|
blueslip.error("Couldn't find message ID for edit " + rows.id(row));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-06 18:51:05 +02:00
|
|
|
if (message.raw_content) {
|
|
|
|
start_edit_with_content(row, message.raw_content, edit_box_open_callback);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const msg_list = current_msg_list;
|
2016-12-21 20:31:16 +01:00
|
|
|
channel.get({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/messages/" + message.id,
|
2014-01-07 23:40:31 +01:00
|
|
|
idempotent: true,
|
2020-07-20 22:18:43 +02:00
|
|
|
success(data) {
|
2013-05-15 00:22:16 +02:00
|
|
|
if (current_msg_list === msg_list) {
|
|
|
|
message.raw_content = data.raw_content;
|
2017-06-06 18:51:05 +02:00
|
|
|
start_edit_with_content(row, message.raw_content, edit_box_open_callback);
|
2013-05-15 00:22:16 +02:00
|
|
|
}
|
2017-01-12 00:17:43 +01:00
|
|
|
},
|
2013-05-15 00:22:16 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-08-16 23:45:13 +02:00
|
|
|
exports.start_topic_edit = function (recipient_row) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const form = $(render_topic_edit_form());
|
2020-04-05 20:01:55 +02:00
|
|
|
current_msg_list.show_edit_topic_on_recipient_row(recipient_row, form);
|
2020-07-20 21:26:58 +02:00
|
|
|
form.on("keydown", handle_inline_topic_edit_keydown);
|
2019-11-02 00:06:25 +01:00
|
|
|
const msg_id = rows.id_for_recipient_row(recipient_row);
|
|
|
|
const message = current_msg_list.get(msg_id);
|
2020-02-19 00:04:12 +01:00
|
|
|
let topic = message.topic;
|
2016-08-27 04:47:53 +02:00
|
|
|
if (topic === compose.empty_topic_placeholder()) {
|
2020-07-15 01:29:15 +02:00
|
|
|
topic = "";
|
2013-11-21 00:28:14 +01:00
|
|
|
}
|
2020-07-20 21:24:26 +02:00
|
|
|
form.find(".inline_topic_edit").val(topic).trigger("select").trigger("focus");
|
2013-08-16 23:45:13 +02:00
|
|
|
};
|
|
|
|
|
2013-06-11 18:54:07 +02:00
|
|
|
exports.is_editing = function (id) {
|
2020-02-06 06:23:06 +01:00
|
|
|
return currently_editing_messages.has(id);
|
2013-06-11 18:54:07 +02:00
|
|
|
};
|
|
|
|
|
2020-04-13 15:46:41 +02:00
|
|
|
exports.end_inline_topic_edit = function (row) {
|
|
|
|
current_msg_list.hide_edit_topic_on_recipient_row(row);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.end_message_row_edit = function (row) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = current_msg_list.get(rows.id(row));
|
2020-07-15 00:34:28 +02:00
|
|
|
if (message !== undefined && currently_editing_messages.has(message.id)) {
|
2020-02-06 06:23:06 +01:00
|
|
|
const scroll_by = currently_editing_messages.get(message.id).scrolled_by;
|
2017-03-10 23:48:51 +01:00
|
|
|
message_viewport.scrollTop(message_viewport.scrollTop() - scroll_by);
|
2017-04-26 04:30:35 +02:00
|
|
|
|
|
|
|
// Clean up resize event listeners
|
2020-02-06 06:23:06 +01:00
|
|
|
const listeners = currently_editing_messages.get(message.id).listeners;
|
2019-11-02 00:06:25 +01:00
|
|
|
const edit_box = document.querySelector("#message_edit_content_" + message.id);
|
2017-05-02 00:07:10 +02:00
|
|
|
if (listeners !== undefined) {
|
|
|
|
// Event listeners to cleanup are only set in some edit types
|
|
|
|
edit_box.removeEventListener("mousedown", listeners[0]);
|
|
|
|
document.body.removeEventListener("mouseup", listeners[1]);
|
|
|
|
}
|
2017-04-26 04:30:35 +02:00
|
|
|
|
2020-02-06 06:23:06 +01:00
|
|
|
currently_editing_messages.delete(message.id);
|
2013-05-28 22:13:49 +02:00
|
|
|
current_msg_list.hide_edit_message(row);
|
2019-11-16 09:26:28 +01:00
|
|
|
|
|
|
|
compose.abort_zoom(message.id);
|
2013-05-28 22:13:49 +02:00
|
|
|
}
|
2017-03-15 15:26:39 +01:00
|
|
|
condense.show_message_expander(row);
|
2017-03-08 16:53:57 +01:00
|
|
|
row.find(".message_reactions").show();
|
2017-07-19 15:24:03 +02:00
|
|
|
|
|
|
|
// We have to blur out text fields, or else hotkeys.js
|
|
|
|
// thinks we are still editing.
|
2020-07-20 21:24:26 +02:00
|
|
|
row.find(".message_edit").trigger("blur");
|
2013-05-15 00:22:16 +02:00
|
|
|
};
|
|
|
|
|
2020-04-13 15:46:41 +02:00
|
|
|
exports.save_inline_topic_edit = function (row) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const msg_list = current_msg_list;
|
2020-04-13 15:46:41 +02:00
|
|
|
let message_id = rows.id_for_recipient_row(row);
|
|
|
|
const message = current_msg_list.get(message_id);
|
2019-11-21 00:06:44 +01:00
|
|
|
|
2020-04-13 15:46:41 +02:00
|
|
|
const old_topic = message.topic;
|
|
|
|
const new_topic = row.find(".inline_topic_edit").val();
|
|
|
|
const topic_changed = new_topic !== old_topic && new_topic.trim() !== "";
|
|
|
|
|
|
|
|
if (!topic_changed) {
|
|
|
|
// this means the inline_topic_edit was opened and submitted without
|
|
|
|
// changing anything, therefore, we should just close the inline topic edit.
|
|
|
|
exports.end_inline_topic_edit(row);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-18 18:53:45 +02:00
|
|
|
exports.show_topic_edit_spinner(row);
|
|
|
|
|
2020-04-13 15:46:41 +02:00
|
|
|
if (message.locally_echoed) {
|
|
|
|
if (topic_changed) {
|
2020-07-20 22:18:43 +02:00
|
|
|
echo.edit_locally(message, {new_topic});
|
2020-04-13 15:46:41 +02:00
|
|
|
row = current_msg_list.get_row(message_id);
|
|
|
|
}
|
|
|
|
exports.end_inline_topic_edit(row);
|
|
|
|
return;
|
2019-11-21 00:06:44 +01:00
|
|
|
}
|
2020-04-13 15:46:41 +02:00
|
|
|
|
|
|
|
const request = {
|
|
|
|
message_id: message.id,
|
|
|
|
topic: new_topic,
|
|
|
|
propagate_mode: "change_later",
|
|
|
|
};
|
|
|
|
|
|
|
|
channel.patch({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/messages/" + message.id,
|
2020-04-13 15:46:41 +02:00
|
|
|
data: request,
|
2020-07-20 22:18:43 +02:00
|
|
|
success() {
|
2020-04-13 15:46:41 +02:00
|
|
|
const spinner = row.find(".topic_edit_spinner");
|
|
|
|
loading.destroy_indicator(spinner);
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error(xhr) {
|
2020-04-23 01:38:32 +02:00
|
|
|
const spinner = row.find(".topic_edit_spinner");
|
|
|
|
loading.destroy_indicator(spinner);
|
2020-04-13 12:39:50 +02:00
|
|
|
if (msg_list === current_msg_list) {
|
|
|
|
message_id = rows.id_for_recipient_row(row);
|
|
|
|
const message = channel.xhr_error_message(i18n.t("Error saving edit"), xhr);
|
2020-07-15 01:29:15 +02:00
|
|
|
row.find(".edit_error").text(message).css("display", "inline-block");
|
2020-04-13 12:39:50 +02:00
|
|
|
}
|
2020-04-13 15:46:41 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.save_message_row_edit = function (row) {
|
|
|
|
const msg_list = current_msg_list;
|
|
|
|
let message_id = rows.id(row);
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = current_msg_list.get(message_id);
|
|
|
|
let changed = false;
|
|
|
|
let edit_locally_echoed = false;
|
2019-11-21 00:06:44 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const new_content = row.find(".message_edit_content").val();
|
|
|
|
let topic_changed = false;
|
|
|
|
let new_topic;
|
2020-02-19 00:04:12 +01:00
|
|
|
const old_topic = message.topic;
|
2019-11-21 00:06:44 +01:00
|
|
|
|
2020-05-06 12:07:34 +02:00
|
|
|
let stream_changed = false;
|
|
|
|
let new_stream_id;
|
|
|
|
const old_stream_id = message.stream_id;
|
|
|
|
|
2019-11-21 00:06:44 +01:00
|
|
|
if (message.type === "stream") {
|
2020-04-13 15:46:41 +02:00
|
|
|
new_topic = row.find(".message_edit_topic").val();
|
2019-11-21 00:06:44 +01:00
|
|
|
topic_changed = new_topic !== old_topic && new_topic.trim() !== "";
|
2020-05-06 12:07:34 +02:00
|
|
|
|
|
|
|
new_stream_id = parseInt($("#select_stream_id_" + message_id).val(), 10);
|
|
|
|
stream_changed = new_stream_id !== old_stream_id;
|
2019-11-21 00:06:44 +01:00
|
|
|
}
|
|
|
|
// Editing a not-yet-acked message (because the original send attempt failed)
|
|
|
|
// just results in the in-memory message being changed
|
|
|
|
if (message.locally_echoed) {
|
2020-05-06 12:07:34 +02:00
|
|
|
if (new_content !== message.raw_content || topic_changed || stream_changed) {
|
|
|
|
// `edit_locally` handles the case where `new_topic/new_stream_id` is undefined
|
2019-11-21 00:06:44 +01:00
|
|
|
echo.edit_locally(message, {
|
|
|
|
raw_content: new_content,
|
2020-07-20 22:18:43 +02:00
|
|
|
new_topic,
|
|
|
|
new_stream_id,
|
2019-11-21 00:06:44 +01:00
|
|
|
});
|
|
|
|
row = current_msg_list.get_row(message_id);
|
|
|
|
}
|
2020-04-13 15:46:41 +02:00
|
|
|
exports.end_message_row_edit(row);
|
2019-11-21 00:06:44 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const request = {message_id: message.id};
|
2020-05-06 12:07:34 +02:00
|
|
|
|
|
|
|
if (topic_changed || stream_changed) {
|
2020-07-15 00:34:28 +02:00
|
|
|
const selected_topic_propagation =
|
|
|
|
row.find("select.message_edit_topic_propagate").val() || "change_later";
|
|
|
|
const send_notification_to_old_thread = row
|
|
|
|
.find(".send_notification_to_old_thread")
|
|
|
|
.is(":checked");
|
|
|
|
const send_notification_to_new_thread = row
|
|
|
|
.find(".send_notification_to_new_thread")
|
|
|
|
.is(":checked");
|
2020-02-27 14:13:35 +01:00
|
|
|
request.propagate_mode = selected_topic_propagation;
|
2020-06-03 16:44:57 +02:00
|
|
|
request.send_notification_to_old_thread = send_notification_to_old_thread;
|
|
|
|
request.send_notification_to_new_thread = send_notification_to_new_thread;
|
|
|
|
exports.notify_old_thread_default = send_notification_to_old_thread;
|
|
|
|
exports.notify_new_thread_default = send_notification_to_new_thread;
|
2019-11-21 00:06:44 +01:00
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
|
2020-05-06 12:07:34 +02:00
|
|
|
if (topic_changed) {
|
|
|
|
request.topic = new_topic;
|
|
|
|
}
|
|
|
|
if (stream_changed) {
|
|
|
|
request.stream_id = new_stream_id;
|
|
|
|
}
|
2020-04-13 15:46:41 +02:00
|
|
|
if (new_content !== message.raw_content) {
|
2019-11-21 00:06:44 +01:00
|
|
|
request.content = new_content;
|
|
|
|
changed = true;
|
|
|
|
}
|
2020-04-13 15:46:41 +02:00
|
|
|
|
2019-11-21 00:06:44 +01:00
|
|
|
if (!changed) {
|
|
|
|
// If they didn't change anything, just cancel it.
|
2020-04-13 15:46:41 +02:00
|
|
|
exports.end_message_row_edit(row);
|
2019-11-21 00:06:44 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-04-22 20:13:23 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
if (
|
|
|
|
changed &&
|
|
|
|
!topic_changed &&
|
|
|
|
!stream_changed &&
|
|
|
|
!markdown.contains_backend_only_syntax(new_content)
|
|
|
|
) {
|
2019-04-22 20:13:23 +02:00
|
|
|
// If the topic isn't changed, and the new message content
|
|
|
|
// could have been locally echoed, than we can locally echo
|
|
|
|
// the edit.
|
2020-02-06 06:24:12 +01:00
|
|
|
currently_echoing_messages.set(message_id, {
|
2019-04-22 20:13:23 +02:00
|
|
|
raw_content: new_content,
|
|
|
|
orig_content: message.content,
|
|
|
|
orig_raw_content: message.raw_content,
|
|
|
|
|
|
|
|
// Store flags that are about user interaction with the
|
|
|
|
// message so that echo.edit_locally() can restore these
|
|
|
|
// flags.
|
|
|
|
starred: message.starred,
|
|
|
|
historical: message.historical,
|
|
|
|
collapsed: message.collapsed,
|
|
|
|
|
|
|
|
// These flags are rendering artifacts we'll want if the
|
|
|
|
// edit fails and we need to revert to the original
|
|
|
|
// rendering of the message.
|
|
|
|
alerted: message.alerted,
|
|
|
|
mentioned: message.mentioned,
|
|
|
|
mentioned_me_directly: message.mentioned,
|
2020-02-06 06:24:12 +01:00
|
|
|
});
|
2019-04-22 20:13:23 +02:00
|
|
|
edit_locally_echoed = true;
|
|
|
|
|
|
|
|
// Settings these attributes causes a "SAVING" notice to
|
|
|
|
// briefly appear where "EDITED" would normally appear until
|
|
|
|
// the message is acknowledged by the server.
|
|
|
|
message.local_edit_timestamp = Math.round(new Date().getTime() / 1000);
|
|
|
|
|
2020-02-06 06:24:12 +01:00
|
|
|
echo.edit_locally(message, currently_echoing_messages.get(message_id));
|
2019-04-22 20:13:23 +02:00
|
|
|
|
|
|
|
row = current_msg_list.get_row(message_id);
|
2020-06-11 14:32:12 +02:00
|
|
|
exports.end_message_row_edit(row);
|
2019-04-22 20:13:23 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 00:06:44 +01:00
|
|
|
channel.patch({
|
2020-07-15 01:29:15 +02:00
|
|
|
url: "/json/messages/" + message.id,
|
2019-11-21 00:06:44 +01:00
|
|
|
data: request,
|
2020-07-20 22:18:43 +02:00
|
|
|
success() {
|
2019-04-22 20:13:23 +02:00
|
|
|
if (edit_locally_echoed) {
|
|
|
|
delete message.local_edit_timestamp;
|
2020-02-06 06:24:12 +01:00
|
|
|
currently_echoing_messages.delete(message_id);
|
2019-04-22 20:13:23 +02:00
|
|
|
}
|
2019-11-21 00:06:44 +01:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error(xhr) {
|
2019-11-21 00:06:44 +01:00
|
|
|
if (msg_list === current_msg_list) {
|
2019-04-22 20:13:23 +02:00
|
|
|
message_id = rows.id(row);
|
|
|
|
|
|
|
|
if (edit_locally_echoed) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const echoed_message = message_store.get(message_id);
|
2020-02-06 06:24:12 +01:00
|
|
|
const echo_data = currently_echoing_messages.get(message_id);
|
2019-04-22 20:13:23 +02:00
|
|
|
|
|
|
|
delete echoed_message.local_edit_timestamp;
|
2020-02-06 06:24:12 +01:00
|
|
|
currently_echoing_messages.delete(message_id);
|
2019-04-22 20:13:23 +02:00
|
|
|
|
|
|
|
// Restore the original content.
|
|
|
|
echo.edit_locally(echoed_message, {
|
|
|
|
content: echo_data.orig_content,
|
|
|
|
raw_content: echo_data.orig_raw_content,
|
|
|
|
mentioned: echo_data.mentioned,
|
|
|
|
mentioned_me_directly: echo_data.mentioned_me_directly,
|
|
|
|
alerted: echo_data.alerted,
|
|
|
|
});
|
|
|
|
|
|
|
|
row = current_msg_list.get_row(message_id);
|
2020-06-11 14:32:12 +02:00
|
|
|
if (!exports.is_editing(message_id)) {
|
2019-04-22 20:13:23 +02:00
|
|
|
// Return to the message editing open UI state.
|
|
|
|
start_edit_maintaining_scroll(row, echo_data.orig_raw_content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = channel.xhr_error_message(i18n.t("Error saving edit"), xhr);
|
2019-11-21 00:06:44 +01:00
|
|
|
row.find(".edit_error").text(message).show();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
// The message will automatically get replaced via message_list.update_message.
|
|
|
|
};
|
|
|
|
|
2013-07-05 17:43:56 +02:00
|
|
|
exports.maybe_show_edit = function (row, id) {
|
2020-02-06 06:23:06 +01:00
|
|
|
if (currently_editing_messages.has(id)) {
|
|
|
|
current_msg_list.show_edit_message(row, currently_editing_messages.get(id));
|
2013-05-15 00:22:16 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-23 05:06:34 +01:00
|
|
|
exports.edit_last_sent_message = function () {
|
2019-11-02 00:06:25 +01:00
|
|
|
const msg = current_msg_list.get_last_message_sent_by_me();
|
2017-03-27 18:00:31 +02:00
|
|
|
|
|
|
|
if (!msg) {
|
|
|
|
return;
|
2016-11-23 05:06:34 +01:00
|
|
|
}
|
2017-03-27 18:00:31 +02:00
|
|
|
|
|
|
|
if (!msg.id) {
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("Message has invalid id in edit_last_sent_message.");
|
2017-03-27 18:00:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const msg_editability_type = exports.get_editability(msg, 5);
|
2017-03-27 18:00:31 +02:00
|
|
|
if (msg_editability_type !== editability_types.FULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const msg_row = current_msg_list.get_row(msg.id);
|
2017-03-27 18:00:31 +02:00
|
|
|
if (!msg_row) {
|
|
|
|
// This should never happen, since we got the message above
|
|
|
|
// from current_msg_list.
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.error("Could not find row for id " + msg.id);
|
2017-03-27 18:00:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
current_msg_list.select_id(msg.id, {then_scroll: true, from_scroll: true});
|
|
|
|
|
|
|
|
// Finally do the real work!
|
2017-03-27 19:15:05 +02:00
|
|
|
compose_actions.cancel();
|
2020-07-02 01:45:54 +02:00
|
|
|
exports.start(msg_row, () => {
|
2020-07-20 21:24:26 +02:00
|
|
|
$("#message_edit_content").trigger("focus");
|
2017-03-27 18:00:31 +02:00
|
|
|
});
|
2016-11-23 05:06:34 +01:00
|
|
|
};
|
|
|
|
|
2019-01-24 20:51:10 +01:00
|
|
|
function hide_delete_btn_show_spinner(deleting) {
|
|
|
|
if (deleting) {
|
2020-07-22 02:59:06 +02:00
|
|
|
$("do_delete_message_button").prop("disabled", true);
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#delete_message_modal > div.modal-footer > button").hide();
|
2019-11-02 00:06:25 +01:00
|
|
|
const delete_spinner = $("#do_delete_message_spinner");
|
2020-07-16 22:40:18 +02:00
|
|
|
loading.make_indicator(delete_spinner, {abs_positioned: true});
|
2019-01-24 20:51:10 +01:00
|
|
|
} else {
|
|
|
|
loading.destroy_indicator($("#do_delete_message_spinner"));
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#do_delete_message_button").prop("disabled", false);
|
|
|
|
$("#delete_message_modal > div.modal-footer > button").show();
|
2019-01-24 20:51:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-14 21:14:26 +02:00
|
|
|
exports.delete_message = function (msg_id) {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#delete-message-error").html("");
|
|
|
|
$("#delete_message_modal").modal("show");
|
2020-02-08 04:04:36 +01:00
|
|
|
if (currently_deleting_messages.includes(msg_id)) {
|
2019-01-24 20:51:10 +01:00
|
|
|
hide_delete_btn_show_spinner(true);
|
|
|
|
} else {
|
|
|
|
hide_delete_btn_show_spinner(false);
|
|
|
|
}
|
2020-07-15 00:34:28 +02:00
|
|
|
$("#do_delete_message_button")
|
|
|
|
.off()
|
|
|
|
.on("click", (e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
currently_deleting_messages.push(msg_id);
|
|
|
|
hide_delete_btn_show_spinner(true);
|
|
|
|
channel.del({
|
|
|
|
url: "/json/messages/" + msg_id,
|
2020-07-20 22:18:43 +02:00
|
|
|
success() {
|
2020-07-15 00:34:28 +02:00
|
|
|
$("#delete_message_modal").modal("hide");
|
|
|
|
currently_deleting_messages = currently_deleting_messages.filter(
|
|
|
|
(id) => id !== msg_id,
|
|
|
|
);
|
|
|
|
hide_delete_btn_show_spinner(false);
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error(xhr) {
|
2020-07-15 00:34:28 +02:00
|
|
|
currently_deleting_messages = currently_deleting_messages.filter(
|
|
|
|
(id) => id !== msg_id,
|
|
|
|
);
|
|
|
|
hide_delete_btn_show_spinner(false);
|
|
|
|
ui_report.error(
|
|
|
|
i18n.t("Error deleting message"),
|
|
|
|
xhr,
|
|
|
|
$("#delete-message-error"),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
2017-05-14 21:14:26 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-01-18 17:40:54 +01:00
|
|
|
exports.delete_topic = function (stream_id, topic_name) {
|
|
|
|
channel.post({
|
|
|
|
url: "/json/streams/" + stream_id + "/delete_topic",
|
|
|
|
data: {
|
2020-07-20 22:18:43 +02:00
|
|
|
topic_name,
|
2019-01-18 17:40:54 +01:00
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
success() {
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#delete_topic_modal").modal("hide");
|
2019-01-18 17:40:54 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-08-06 18:01:04 +02:00
|
|
|
exports.handle_narrow_deactivated = function () {
|
2020-02-06 06:23:06 +01:00
|
|
|
for (const [idx, elem] of currently_editing_messages) {
|
2013-05-15 00:22:16 +02:00
|
|
|
if (current_msg_list.get(idx) !== undefined) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const row = current_msg_list.get_row(idx);
|
2013-05-15 00:22:16 +02:00
|
|
|
current_msg_list.show_edit_message(row, elem);
|
|
|
|
}
|
2020-02-06 06:23:06 +01:00
|
|
|
}
|
2018-08-06 18:01:04 +02:00
|
|
|
};
|
2013-05-15 00:22:16 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
exports.move_topic_containing_message_to_stream = function (
|
|
|
|
message_id,
|
|
|
|
new_stream_id,
|
|
|
|
new_topic_name,
|
|
|
|
send_notification_to_new_thread,
|
|
|
|
send_notification_to_old_thread,
|
|
|
|
) {
|
|
|
|
const request = {
|
|
|
|
stream_id: new_stream_id,
|
|
|
|
propagate_mode: "change_all",
|
|
|
|
topic: new_topic_name,
|
2020-07-20 22:18:43 +02:00
|
|
|
send_notification_to_old_thread,
|
|
|
|
send_notification_to_new_thread,
|
2020-02-19 01:38:34 +01:00
|
|
|
};
|
2020-07-15 00:34:28 +02:00
|
|
|
exports.notify_old_thread_default = send_notification_to_old_thread;
|
|
|
|
exports.notify_new_thread_default = send_notification_to_new_thread;
|
|
|
|
channel.patch({
|
|
|
|
url: "/json/messages/" + message_id,
|
|
|
|
data: request,
|
2020-07-20 22:18:43 +02:00
|
|
|
success() {
|
2020-07-15 00:34:28 +02:00
|
|
|
// The main UI will update via receiving the event
|
|
|
|
// from server_events.js.
|
|
|
|
// TODO: This should probably remove a spinner and
|
|
|
|
// close the modal.
|
|
|
|
},
|
2020-07-20 22:18:43 +02:00
|
|
|
error(xhr) {
|
2020-07-15 00:34:28 +02:00
|
|
|
ui_report.error(i18n.t("Error moving the topic"), xhr, $("#home-error"), 4000);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
2020-02-19 01:38:34 +01:00
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.message_edit = exports;
|