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.
This commit is contained in:
Cory Lynch 2017-04-25 22:30:35 -04:00 committed by Tim Abbott
parent f9d1bff167
commit 81a575e4d5
3 changed files with 19 additions and 6 deletions

View File

@ -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);
}

View File

@ -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.

View File

@ -1294,6 +1294,7 @@ div.focused_table {
.message_edit_content {
line-height: 18px;
resize: vertical!important;
max-height: 24em;
}
.message_edit_countdown_timer {