compose: Refactor autosize_textarea to work while editing messages.

Previously, compose_ui.autosize_textarea didn't work while editing
messages in many cases (uploading files, typeaheads, keydown handling,
etc.).

Refactored the autosize_textarea function in compose_ui to work
while editing messages too and added appropriate argument for the
introduced function parameter at all occurences of the function
use.

Also, updated the corresponding test cases.
This commit is contained in:
Priyansh Garg
2020-09-05 03:19:49 +05:30
committed by Tim Abbott
parent abe876a4a4
commit 6684247147
7 changed files with 38 additions and 13 deletions

View File

@@ -1,5 +1,7 @@
"use strict";
const autosize = require("autosize");
zrequire("compose_ui");
const people = zrequire("people");
zrequire("user_status");
@@ -71,6 +73,27 @@ function make_textbox(s) {
return widget;
}
run_test("autosize_textarea", () => {
const textarea_autosized = {};
function fake_autosize_update(textarea) {
textarea_autosized.textarea = textarea;
textarea_autosized.autosized = true;
}
with_field(autosize, "update", fake_autosize_update, () => {
// Call autosize_textarea for the compose box
compose_ui.autosize_textarea($("#compose-textarea"));
assert.equal(textarea_autosized.textarea, $("#compose-textarea"));
assert(textarea_autosized.autosized);
// Call autosize_textarea with an argument
compose_ui.autosize_textarea($("#message_edit_content_65"));
assert.equal(textarea_autosized.textarea, $("#message_edit_content_65"));
assert(textarea_autosized.autosized);
});
});
run_test("insert_syntax_and_focus", () => {
$("#compose-textarea").val("xyz ");
$("#compose-textarea").caret = function (syntax) {

View File

@@ -299,7 +299,7 @@ exports.nonexistent_stream_reply_error = nonexistent_stream_reply_error;
function clear_compose_box() {
$("#compose-textarea").val("").trigger("focus");
drafts.delete_draft_after_send();
compose_ui.autosize_textarea();
compose_ui.autosize_textarea($("#compose-textarea"));
$("#compose-send-status").hide(0);
$("#compose-send-button").prop("disabled", false);
$("#sending-indicator").hide();
@@ -781,7 +781,7 @@ exports.handle_keydown = function (event, textarea) {
}
}
compose_ui.autosize_textarea();
compose_ui.autosize_textarea(textarea);
return;
}
};

View File

@@ -92,7 +92,7 @@ function clear_box() {
exports.clear_textarea();
$("#compose-textarea").removeData("draft-id");
compose_ui.autosize_textarea();
compose_ui.autosize_textarea($("#compose-textarea"));
$("#compose-send-status").hide(0);
}

View File

@@ -4,8 +4,10 @@ const autosize = require("autosize");
const people = require("./people");
exports.autosize_textarea = function () {
autosize.update($("#compose-textarea"));
exports.autosize_textarea = function (textarea) {
// Since this supports both compose and file upload, one must pass
// in the text area to autosize.
autosize.update(textarea);
};
exports.smart_insert = function (textarea, syntax) {

View File

@@ -880,7 +880,7 @@ exports.content_typeahead_selected = function (item, event) {
}
textbox.val(beginning + rest);
textbox.caret(beginning.length, beginning.length);
compose_ui.autosize_textarea();
compose_ui.autosize_textarea(textbox);
};
show_flatpickr(this.$element[0], on_timestamp_selection, timestamp);
return beginning + rest;
@@ -891,7 +891,7 @@ exports.content_typeahead_selected = function (item, event) {
setTimeout(() => {
textbox.caret(beginning.length, beginning.length);
// Also, trigger autosize to check if compose box needs to be resized.
compose_ui.autosize_textarea();
compose_ui.autosize_textarea(textbox);
}, 0);
return beginning + rest;
};

View File

@@ -201,7 +201,7 @@ exports.restore_draft = function (draft_id) {
compose_args.topic = "";
}
compose_actions.start(compose_args.type, compose_args);
compose_ui.autosize_textarea();
compose_ui.autosize_textarea($("#compose-textarea"));
$("#compose-textarea").data("draft-id", draft_id);
};

View File

@@ -143,7 +143,7 @@ exports.upload_files = function (uppy, config, files) {
exports.get_item("textarea", config),
);
});
compose_ui.autosize_textarea();
compose_ui.autosize_textarea(exports.get_item("textarea", config));
uppy.cancelAll();
exports.get_item("textarea", config).trigger("focus");
setTimeout(() => {
@@ -157,7 +157,7 @@ exports.upload_files = function (uppy, config, files) {
exports.get_translated_status(file),
exports.get_item("textarea", config),
);
compose_ui.autosize_textarea();
compose_ui.autosize_textarea(exports.get_item("textarea", config));
uppy.addFile({
source: exports.get_item("source", config),
name: file.name,
@@ -256,7 +256,7 @@ exports.setup_upload = function (config) {
filename_uri,
exports.get_item("textarea", config),
);
compose_ui.autosize_textarea();
compose_ui.autosize_textarea(exports.get_item("textarea", config));
});
uppy.on("complete", () => {
@@ -313,7 +313,7 @@ exports.setup_upload = function (config) {
"",
exports.get_item("textarea", config),
);
compose_ui.autosize_textarea();
compose_ui.autosize_textarea(exports.get_item("textarea", config));
});
uppy.on("restriction-failed", (file) => {
@@ -322,7 +322,7 @@ exports.setup_upload = function (config) {
"",
exports.get_item("textarea", config),
);
compose_ui.autosize_textarea();
compose_ui.autosize_textarea(exports.get_item("textarea", config));
});
return uppy;