var message_edit = (function () { var exports = {}; var currently_editing_messages = {}; // Returns true if the edit task should end. exports.save = function (row, from_topic_edited_only) { var msg_list = current_msg_list; var message_id; if (row.hasClass('recipient_row')) { message_id = rows.id_for_recipient_row(row); } else { message_id = rows.id(row); } var message = current_msg_list.get(message_id); var changed = false; var new_content = row.find(".message_edit_content").val(); var topic_changed = false; var new_topic; if (message.type === "stream") { new_topic = row.find(".message_edit_topic").val(); topic_changed = (new_topic !== message.subject && new_topic.trim() !== ""); } // Editing a not-yet-acked message (because the original send attempt failed) // just results in the in-memory message being changed if (message.local_id !== undefined) { // No changes if (new_content === message.raw_content && !topic_changed) { return true; } echo.edit_locally(message, new_content, topic_changed ? new_topic : undefined); return true; } var request = {message_id: message.id}; if (topic_changed) { request.subject = new_topic; if (feature_flags.propagate_topic_edits) { var selected_topic_propagation = row.find("select.message_edit_topic_propagate").val() || "change_later"; request.propagate_mode = selected_topic_propagation; } changed = true; } if (new_content !== message.raw_content && !from_topic_edited_only) { request.content = new_content; message.is_me_message = new_content.lastIndexOf('/me', 0) === 0; changed = true; } if (!changed) { // If they didn't change anything, just cancel it. return true; } channel.post({ url: '/json/update_message', data: request, success: function (data) { if (msg_list === current_msg_list) { return true; } }, error: function (xhr, error_type, xhn) { var message = channel.xhr_error_message("Error saving edit", xhr); row.find(".edit_error").text(message).show(); } }); // The message will automatically get replaced when it arrives. }; function handle_edit_keydown(from_topic_edited_only, e) { var row, code = e.keyCode || e.which; if (e.target.id === "message_edit_content" && code === 13 && (e.metaKey || e.ctrlKey)) { row = $(".message_edit_content").filter(":focus").closest(".message_row"); if (message_edit.save(row, from_topic_edited_only) === true) { message_edit.end(row); } } else if (e.target.id === "message_edit_topic" && code === 13) { // Hitting enter in topic field isn't so great. e.stopPropagation(); e.preventDefault(); } } function timer_text(seconds_left) { var minutes = Math.floor(seconds_left / 60); var seconds = seconds_left % 60; if (minutes >= 1) { return i18n.t("__minutes__ min to edit", {'minutes': minutes.toString()}); } else if (seconds_left >= 10) { return i18n.t("__seconds__ sec to edit", {'seconds': (seconds - seconds % 5).toString()}); } return i18n.t("__seconds__ sec to edit", {'seconds': seconds.toString()}); } function edit_message (row, raw_content) { var content_top = row.find('.message_content')[0] .getBoundingClientRect().top; var message = current_msg_list.get(rows.id(row)); var edit_row = row.find(".message_edit"); var form = $(templates.render('message_edit_form', {is_stream: message.is_stream, topic: message.subject, content: raw_content, minutes_to_edit: Math.floor(page_params.realm_message_content_edit_limit_seconds / 60)})); var edit_obj = {form: form, raw_content: raw_content}; var original_topic = message.subject; current_msg_list.show_edit_message(row, edit_obj); form.keydown(_.partial(handle_edit_keydown, false)); // 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 // zerver.views.messages.update_message_backend var seconds_left_buffer = 5; var now = new XDate(); var seconds_left = page_params.realm_message_content_edit_limit_seconds + now.diffSeconds(message.timestamp * 1000); var can_edit_content = (page_params.realm_message_content_edit_limit_seconds === 0) || (seconds_left + seconds_left_buffer > 0); if (!can_edit_content) { row.find('textarea.message_edit_content').attr("disabled","disabled"); } // If we allow editing at all, give them at least 10 seconds to do it. // If you change this number also change edit_limit_buffer in // zerver.views.messages.update_message_backend var min_seconds_to_edit = 10; seconds_left = Math.floor(Math.max(seconds_left, min_seconds_to_edit)); if (page_params.realm_message_content_edit_limit_seconds > 0) { row.find('.message-edit-timer-control-group').show(); $('#message_edit_tooltip').tooltip({ animation: false, placement: 'left', template: '