diff --git a/web/src/upload.js b/web/src/upload.js index ca35a1a118..40216ff6f8 100644 --- a/web/src/upload.js +++ b/web/src/upload.js @@ -23,7 +23,7 @@ export function get_translated_status(file) { return "[" + status + "]()"; } -export function get_item(key, config) { +export function get_item(key, config, file_id) { if (!config) { throw new Error("Missing config"); } @@ -36,13 +36,17 @@ export function get_item(key, config) { case "banner_container": return $("#compose_banners"); case "upload_banner_identifier": - return "#compose_banners .upload_banner"; + return `#compose_banners .upload_banner.file_${CSS.escape(file_id)}`; case "upload_banner": - return $("#compose_banners .upload_banner"); + return $(`#compose_banners .upload_banner.file_${CSS.escape(file_id)}`); case "upload_banner_close_button": - return $("#compose_banners .upload_banner .compose_banner_close_button"); + return $( + `#compose_banners .upload_banner.file_${CSS.escape( + file_id, + )} .compose_banner_close_button`, + ); case "upload_banner_message": - return $("#compose_banners .upload_banner .upload_msg"); + return $(`#compose_banners .upload_banner.file_${CSS.escape(file_id)} .upload_msg`); case "file_input_identifier": return "#compose .file_input"; case "source": @@ -66,19 +70,29 @@ export function get_item(key, config) { .closest(".message_edit_form") .find(".message_edit_save"); case "banner_container": - return $(`#edit_form_${CSS.escape(config.row)} .banners`); + return $(`#edit_form_${CSS.escape(config.row)} .edit_form_banners`); case "upload_banner_identifier": - return `#edit_form_${CSS.escape(config.row)} .upload_banner`; + return `#edit_form_${CSS.escape(config.row)} .upload_banner.file_${CSS.escape( + file_id, + )}`; case "upload_banner": - return $(`#edit_form_${CSS.escape(config.row)} .upload_banner`); + return $( + `#edit_form_${CSS.escape(config.row)} .upload_banner.file_${CSS.escape( + file_id, + )}`, + ); case "upload_banner_close_button": return $( - `#edit_form_${CSS.escape( - config.row, - )} .upload_banner .compose_banner_close_button`, + `#edit_form_${CSS.escape(config.row)} .upload_banner.file_${CSS.escape( + file_id, + )} .compose_banner_close_button`, ); case "upload_banner_message": - return $(`#edit_form_${CSS.escape(config.row)} .upload_banner .upload_msg`); + return $( + `#edit_form_${CSS.escape(config.row)} .upload_banner.file_${CSS.escape( + file_id, + )} .upload_msg`, + ); case "file_input_identifier": return `#edit_form_${CSS.escape(config.row)} .file_input`; case "source": @@ -95,36 +109,18 @@ export function get_item(key, config) { } } -export function hide_upload_banner(config) { - get_item("send_button", config).prop("disabled", false); - get_item("upload_banner", config).remove(); +export function hide_upload_banner(uppy, config, file_id) { + get_item("upload_banner", config, file_id).remove(); + if (uppy.getFiles().length === 0) { + get_item("send_button", config).prop("disabled", false); + } } -function show_upload_banner(config, banner_type, banner_text) { - // We only show one upload banner at a time per compose box, - // and all uploads are combined into the same progress bar. - // TODO: It would be nice to separate the error banner into - // a different element, so that we can show it at the same - // time as the upload bar and other uploads can still continue - // when an error occurs. - const $upload_banner = get_item("upload_banner", config); - if ($upload_banner.length) { - if (banner_type === "error") { - // Hide moving bar so that it doesn't do the 1s transition to 0 - const $moving_bar = $(`${get_item("upload_banner_identifier", config)} .moving_bar`); - $moving_bar.hide(); - $upload_banner.removeClass("info").addClass("error"); - // Show it again once the animation is complete. - setTimeout(() => $moving_bar.show(), 1000); - } else { - $upload_banner.removeClass("error").addClass("info"); - } - get_item("upload_banner_message", config).text(banner_text); - return; - } +function add_upload_banner(config, banner_type, banner_text, file_id) { const new_banner = render_upload_banner({ banner_type, banner_text, + file_id, }); get_item("banner_container", config).append(new_banner); } @@ -132,9 +128,19 @@ function show_upload_banner(config, banner_type, banner_text) { export function show_error_message( config, message = $t({defaultMessage: "An unknown error occurred."}), + file_id = null, ) { get_item("send_button", config).prop("disabled", false); - show_upload_banner(config, "error", message); + if (file_id) { + $(`${get_item("upload_banner_identifier", config, file_id)} .moving_bar`).hide(); + get_item("upload_banner", config, file_id).removeClass("info").addClass("error"); + get_item("upload_banner_message", config).text(message); + } else { + // We still use a "file_id" (that's not actually related to a file) + // to differentiate this banner from banners that *are* associated + // with files. This is notably relevant for the close click handler. + add_upload_banner(config, "error", message, "generic_error"); + } } export async function upload_files(uppy, config, files) { @@ -163,20 +169,6 @@ export async function upload_files(uppy, config, files) { } get_item("send_button", config).prop("disabled", true); - show_upload_banner(config, "info", $t({defaultMessage: "Uploading…"})); - get_item("upload_banner_close_button", config).one("click", () => { - for (const file of uppy.getFiles()) { - compose_ui.replace_syntax( - get_translated_status(file), - "", - get_item("textarea", config), - ); - } - compose_ui.autosize_textarea(get_item("textarea", config)); - uppy.cancelAll(); - get_item("textarea", config).trigger("focus"); - hide_upload_banner(config); - }); for (const file of files) { try { @@ -187,7 +179,7 @@ export async function upload_files(uppy, config, files) { 1, ); compose_ui.autosize_textarea(get_item("textarea", config)); - uppy.addFile({ + file.id = uppy.addFile({ source: get_item("source", config), name: file.name, type: file.type, @@ -195,8 +187,27 @@ export async function upload_files(uppy, config, files) { }); } catch { // Errors are handled by info-visible and upload-error event callbacks. - break; + continue; } + + add_upload_banner( + config, + "info", + $t({defaultMessage: "Uploading {filename}…"}, {filename: file.name}), + file.id, + ); + get_item("upload_banner_close_button", config, file.id).one("click", () => { + compose_ui.replace_syntax( + get_translated_status(file), + "", + get_item("textarea", config), + ); + compose_ui.autosize_textarea(get_item("textarea", config)); + get_item("textarea", config).trigger("focus"); + + uppy.removeFile(file.id); + hide_upload_banner(uppy, config, file.id); + }); } } @@ -238,13 +249,10 @@ export function setup_upload(config) { }, }); - uppy.on("progress", (progress) => { - // When upload is complete, it resets to 0, but we want to see it at 100%. - if (progress === 0) { - return; - } - $(`${get_item("upload_banner_identifier", config)} .moving_bar`).css({ - width: `${progress}%`, + uppy.on("upload-progress", (file, progress) => { + const percent_complete = (100 * progress.bytesUploaded) / progress.bytesTotal; + $(`${get_item("upload_banner_identifier", config, file.id)} .moving_bar`).css({ + width: `${percent_complete}%`, }); }); @@ -255,6 +263,26 @@ export function setup_upload(config) { event.target.value = ""; }); + // These are close-click handlers for error banners that aren't associated + // with a particular file. + $("#compose_banners").on( + "click", + ".upload_banner.file_generic_error .compose_banner_close_button", + (event) => { + event.preventDefault(); + $(event.target).parents(".upload_banner").remove(); + }, + ); + + $("#edit_form_banners").on( + "click", + ".upload_banner.file_generic_error .compose_banner_close_button", + (event) => { + event.preventDefault(); + $(event.target).parents(".upload_banner").remove(); + }, + ); + const $drag_drop_container = get_item("drag_drop_container", config); $drag_drop_container.on("dragover", (event) => event.preventDefault()); $drag_drop_container.on("dragenter", (event) => event.preventDefault()); @@ -299,42 +327,27 @@ export function setup_upload(config) { get_item("textarea", config), ); compose_ui.autosize_textarea(get_item("textarea", config)); - }); - uppy.on("complete", () => { - let uploads_in_progress = false; - for (const file of uppy.getFiles()) { - if (file.progress.uploadComplete) { - // The uploaded files should be removed since uppy don't allow files in the store - // to be re-uploaded again. - uppy.removeFile(file.id); - } else { - // Happens when user tries to upload files when there is already an existing batch - // being uploaded. So when the first batch of files complete, the second batch would - // still be in progress. - uploads_in_progress = true; - } - } - - const has_errors = get_item("upload_banner", config).hasClass("error"); - if (!uploads_in_progress && !has_errors) { - // Hide upload status for 100ms after the 1s transition to 100% - // so that the user can see the progress bar at 100%. - setTimeout(() => { - hide_upload_banner(config); - }, 1100); - } + // The uploaded files should be removed since uppy doesn't allow files in the store + // to be re-uploaded again. + uppy.removeFile(file.id); + // Hide upload status after waiting 100ms after the 1s transition to 100% + // so that the user can see the progress bar at 100%. + setTimeout(() => { + hide_upload_banner(uppy, config, file.id); + }, 1100); }); uppy.on("info-visible", () => { - // Uppy's `info-visible` event is issued after prepending the + // Uppy's `info-visible` event is issued after appending the // notice details into the list of event events accessed via // uppy.getState().info. Extract the notice details so that we // can potentially act on the error. // // TODO: Ideally, we'd be using the `.error()` hook or // something, not parsing error message strings. - const info = uppy.getState().info[0]; + const infoList = uppy.getState().info; + const info = infoList[infoList.length - 1]; if (info.type === "error" && info.message === "No Internet connection") { // server_events already handles the case of no internet. return; @@ -350,17 +363,13 @@ export function setup_upload(config) { if (info.type === "error") { // The remaining errors are mostly frontend errors like file being too large // for upload. - // TODO: It would be nice to keep the other uploads going if one fails, - // and show both an error message and the upload bar. - uppy.cancelAll(); show_error_message(config, info.message); } }); uppy.on("upload-error", (file, error, response) => { const message = response ? response.body.msg : undefined; - uppy.cancelAll(); - show_error_message(config, message); + show_error_message(config, message, file.id); compose_ui.replace_syntax(get_translated_status(file), "", get_item("textarea", config)); compose_ui.autosize_textarea(get_item("textarea", config)); }); diff --git a/web/templates/compose_banner/upload_banner.hbs b/web/templates/compose_banner/upload_banner.hbs index e877700fdd..de2b2ca6c0 100644 --- a/web/templates/compose_banner/upload_banner.hbs +++ b/web/templates/compose_banner/upload_banner.hbs @@ -1,4 +1,4 @@ -