From b0a0ae215f45e09acb80e749ccbc652356aeeb40 Mon Sep 17 00:00:00 2001 From: Sharif Naas Date: Sat, 6 Jun 2020 19:50:31 -0700 Subject: [PATCH] js: Extract message_edit_history.js. --- .eslintrc.json | 1 + static/js/bundles/app.js | 1 + static/js/click_handlers.js | 2 +- static/js/global.d.ts | 1 + static/js/message_edit.js | 65 ------------------------------ static/js/message_edit_history.js | 67 +++++++++++++++++++++++++++++++ static/js/message_events.js | 2 +- static/js/popovers.js | 2 +- tools/test-js-with-node | 1 + 9 files changed, 74 insertions(+), 68 deletions(-) create mode 100644 static/js/message_edit_history.js diff --git a/.eslintrc.json b/.eslintrc.json index 7f84246d6d..469f158a13 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -231,6 +231,7 @@ "marked": false, "md5": false, "message_edit": false, + "message_edit_history": false, "message_events": false, "message_fetch": false, "message_flags": false, diff --git a/static/js/bundles/app.js b/static/js/bundles/app.js index 1efefe75f3..f933dbb39e 100644 --- a/static/js/bundles/app.js +++ b/static/js/bundles/app.js @@ -98,6 +98,7 @@ import "../stream_create.js"; import "../stream_edit.js"; import "../subs.js"; import "../message_edit.js"; +import "../message_edit_history.js"; import "../condense.js"; import "../resize.js"; import "../list_render.js"; diff --git a/static/js/click_handlers.js b/static/js/click_handlers.js index 914f9c63e3..99849b97e0 100644 --- a/static/js/click_handlers.js +++ b/static/js/click_handlers.js @@ -164,7 +164,7 @@ exports.initialize = function () { const message_history_cancel_btn = $('#message-history-cancel'); if (page_params.realm_allow_edit_history) { - message_edit.show_history(message); + message_edit_history.show_history(message); message_history_cancel_btn.focus(); } e.stopPropagation(); diff --git a/static/js/global.d.ts b/static/js/global.d.ts index b89df18fb3..175f100efc 100644 --- a/static/js/global.d.ts +++ b/static/js/global.d.ts @@ -68,6 +68,7 @@ declare let local_message: any; declare let localstorage: any; declare let markdown: any; declare let message_edit: any; +declare let message_edit_history: any; declare let message_events: any; declare let message_fetch: any; declare let message_flags: any; diff --git a/static/js/message_edit.js b/static/js/message_edit.js index 150503433e..9dbe84ebcf 100644 --- a/static/js/message_edit.js +++ b/static/js/message_edit.js @@ -1,5 +1,4 @@ const render_message_edit_form = require('../templates/message_edit_form.hbs'); -const render_message_edit_history = require('../templates/message_edit_history.hbs'); const render_topic_edit_form = require('../templates/topic_edit_form.hbs'); const currently_editing_messages = new Map(); @@ -787,70 +786,6 @@ exports.edit_last_sent_message = function () { }); }; -exports.fetch_and_render_message_history = function (message) { - channel.get({ - url: "/json/messages/" + message.id + "/history", - data: {message_id: JSON.stringify(message.id)}, - success: function (data) { - const content_edit_history = []; - let prev_timestamp; - - for (const [index, msg] of data.message_history.entries()) { - // Format timestamp nicely for display - const timestamp = timerender.get_full_time(msg.timestamp); - const item = { - timestamp: moment(timestamp).format("h:mm A"), - display_date: moment(timestamp).format("MMMM D, YYYY"), - }; - if (msg.user_id) { - const person = people.get_by_user_id(msg.user_id); - item.edited_by = person.full_name; - } - - if (index === 0) { - item.posted_or_edited = "Posted by"; - item.body_to_render = msg.rendered_content; - prev_timestamp = timestamp; - item.show_date_row = true; - } else if (msg.prev_topic && msg.prev_content) { - item.posted_or_edited = "Edited by"; - item.body_to_render = msg.content_html_diff; - item.show_date_row = !moment(timestamp).isSame(prev_timestamp, 'day'); - item.topic_edited = true; - item.prev_topic = msg.prev_topic; - item.new_topic = msg.topic; - } else if (msg.prev_topic) { - item.posted_or_edited = "Topic edited by"; - item.topic_edited = true; - item.prev_topic = msg.prev_topic; - item.new_topic = msg.topic; - } else { - // just a content edit - item.posted_or_edited = "Edited by"; - item.body_to_render = msg.content_html_diff; - item.show_date_row = !moment(timestamp).isSame(prev_timestamp, 'day'); - } - - content_edit_history.push(item); - } - $('#message-history').attr('data-message-id', message.id); - $('#message-history').html(render_message_edit_history({ - edited_messages: content_edit_history, - })); - }, - error: function (xhr) { - ui_report.error(i18n.t("Error fetching message edit history"), xhr, - $("#message-history-error")); - }, - }); -}; - -exports.show_history = function (message) { - $('#message-history').html(''); - $('#message-edit-history').modal("show"); - exports.fetch_and_render_message_history(message); -}; - function hide_delete_btn_show_spinner(deleting) { if (deleting) { $('do_delete_message_button').attr('disabled', 'disabled'); diff --git a/static/js/message_edit_history.js b/static/js/message_edit_history.js new file mode 100644 index 0000000000..99860336de --- /dev/null +++ b/static/js/message_edit_history.js @@ -0,0 +1,67 @@ +const render_message_edit_history = require('../templates/message_edit_history.hbs'); + +exports.fetch_and_render_message_history = function (message) { + channel.get({ + url: "/json/messages/" + message.id + "/history", + data: {message_id: JSON.stringify(message.id)}, + success: function (data) { + const content_edit_history = []; + let prev_timestamp; + + for (const [index, msg] of data.message_history.entries()) { + // Format timestamp nicely for display + const timestamp = timerender.get_full_time(msg.timestamp); + const item = { + timestamp: moment(timestamp).format("h:mm A"), + display_date: moment(timestamp).format("MMMM D, YYYY"), + }; + if (msg.user_id) { + const person = people.get_by_user_id(msg.user_id); + item.edited_by = person.full_name; + } + + if (index === 0) { + item.posted_or_edited = "Posted by"; + item.body_to_render = msg.rendered_content; + prev_timestamp = timestamp; + item.show_date_row = true; + } else if (msg.prev_topic && msg.prev_content) { + item.posted_or_edited = "Edited by"; + item.body_to_render = msg.content_html_diff; + item.show_date_row = !moment(timestamp).isSame(prev_timestamp, 'day'); + item.topic_edited = true; + item.prev_topic = msg.prev_topic; + item.new_topic = msg.topic; + } else if (msg.prev_topic) { + item.posted_or_edited = "Topic edited by"; + item.topic_edited = true; + item.prev_topic = msg.prev_topic; + item.new_topic = msg.topic; + } else { + // just a content edit + item.posted_or_edited = "Edited by"; + item.body_to_render = msg.content_html_diff; + item.show_date_row = !moment(timestamp).isSame(prev_timestamp, 'day'); + } + + content_edit_history.push(item); + } + $('#message-history').attr('data-message-id', message.id); + $('#message-history').html(render_message_edit_history({ + edited_messages: content_edit_history, + })); + }, + error: function (xhr) { + ui_report.error(i18n.t("Error fetching message edit history"), xhr, + $("#message-history-error")); + }, + }); +}; + +exports.show_history = function (message) { + $('#message-history').html(''); + $('#message-edit-history').modal("show"); + exports.fetch_and_render_message_history(message); +}; + +window.message_edit_history = exports; diff --git a/static/js/message_events.js b/static/js/message_events.js index e5bd17ad44..fc72f4dd13 100644 --- a/static/js/message_events.js +++ b/static/js/message_events.js @@ -324,7 +324,7 @@ exports.update_messages = function update_messages(events) { // Rerender "Message edit history" if it was open to the edited message. if ($('#message-edit-history').hasClass('in') && msg.id === parseInt($('#message-history').attr('data-message-id'), 10)) { - message_edit.fetch_and_render_message_history(msg); + message_edit_history.fetch_and_render_message_history(msg); } } diff --git a/static/js/popovers.js b/static/js/popovers.js index 2ed63999b2..945fc34324 100644 --- a/static/js/popovers.js +++ b/static/js/popovers.js @@ -1020,7 +1020,7 @@ exports.register_click_handlers = function () { const message_history_cancel_btn = $('#message-history-cancel'); exports.hide_actions_popover(); - message_edit.show_history(message); + message_edit_history.show_history(message); message_history_cancel_btn.focus(); e.stopPropagation(); e.preventDefault(); diff --git a/tools/test-js-with-node b/tools/test-js-with-node index d0ed423e9f..3f698e23ab 100755 --- a/tools/test-js-with-node +++ b/tools/test-js-with-node @@ -72,6 +72,7 @@ EXEMPT_FILES = { 'static/js/local_message.js', 'static/js/localstorage.js', 'static/js/message_edit.js', + 'static/js/message_edit_history.js', 'static/js/message_events.js', 'static/js/message_fetch.js', 'static/js/message_flags.js',