From 81a575e4d509af68b0dcf8419212444e6c883d0d Mon Sep 17 00:00:00 2001 From: Cory Lynch Date: Tue, 25 Apr 2017 22:30:35 -0400 Subject: [PATCH] Add cleanup for resize listeners on message editing. I changed the watch_manual_resize function to return the listener functions it creates, and then these are used to remove the event listeners before the edit box is hidden. --- static/js/message_edit.js | 10 +++++++++- static/js/resize.js | 14 +++++++++----- static/styles/zulip.css | 1 + 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/static/js/message_edit.js b/static/js/message_edit.js index df4f4cffd0..0ec8a6d8c0 100644 --- a/static/js/message_edit.js +++ b/static/js/message_edit.js @@ -220,7 +220,8 @@ function edit_message(row, raw_content) { initClipboard(copy_message[0]); } else if (editability === editability_types.FULL) { copy_message.remove(); - resize.watch_manual_resize("#message_edit_content"); + var listeners = resize.watch_manual_resize("#message_edit_content"); + currently_editing_messages[rows.id(row)].listeners = listeners; composebox_typeahead.initialize_compose_typeahead("#message_edit_content", {emoji: true, stream: true}); } @@ -360,6 +361,13 @@ exports.end = function (row) { currently_editing_messages[message.id] !== undefined) { var scroll_by = currently_editing_messages[message.id].scrolled_by; message_viewport.scrollTop(message_viewport.scrollTop() - scroll_by); + + // Clean up resize event listeners + var listeners = currently_editing_messages[message.id].listeners; + var edit_box = document.querySelector("#message_edit_content"); + edit_box.removeEventListener("mousedown", listeners[0]); + document.body.removeEventListener("mouseup", listeners[1]); + delete currently_editing_messages[message.id]; current_msg_list.hide_edit_message(row); } diff --git a/static/js/resize.js b/static/js/resize.js index 24ed490961..965cd33612 100644 --- a/static/js/resize.js +++ b/static/js/resize.js @@ -166,21 +166,22 @@ function left_userlist_get_new_heights() { } exports.watch_manual_resize = function (element) { - (function on_box_resize(cb) { + return (function on_box_resize(cb) { var meta = { box: document.querySelector(element), height: null, mousedown: false, }; - meta.box.addEventListener("mousedown", function () { + var box_handler = function () { meta.mousedown = true; meta.height = meta.box.clientHeight; - }); + }; + meta.box.addEventListener("mousedown", box_handler); // If the user resizes the textarea manually, we use the // callback to stop autosize from adjusting the height. - document.body.addEventListener("mouseup", function () { + var body_handler = function () { if (meta.mousedown === true) { meta.mousedown = false; if (meta.height !== meta.box.clientHeight) { @@ -188,7 +189,10 @@ exports.watch_manual_resize = function (element) { cb.call(meta.box, meta.height); } } - }); + }; + document.body.addEventListener("mouseup", body_handler); + + return [box_handler, body_handler]; }(function (height) { // This callback disables autosize on the textarea. It // will be re-enabled when this component is next opened. diff --git a/static/styles/zulip.css b/static/styles/zulip.css index c69b3136b2..0690280773 100644 --- a/static/styles/zulip.css +++ b/static/styles/zulip.css @@ -1294,6 +1294,7 @@ div.focused_table { .message_edit_content { line-height: 18px; resize: vertical!important; + max-height: 24em; } .message_edit_countdown_timer {