2022-10-07 00:56:00 +02:00
|
|
|
import {Uppy} from "@uppy/core";
|
2021-02-28 00:52:20 +01:00
|
|
|
import XHRUpload from "@uppy/xhr-upload";
|
2021-03-11 05:43:45 +01:00
|
|
|
import $ from "jquery";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2023-01-09 07:43:39 +01:00
|
|
|
import render_upload_banner from "../templates/compose_banner/upload_banner.hbs";
|
|
|
|
|
2021-02-28 21:32:22 +01:00
|
|
|
import * as compose_actions from "./compose_actions";
|
2023-04-27 07:37:38 +02:00
|
|
|
import * as compose_banner from "./compose_banner";
|
2021-02-28 00:52:20 +01:00
|
|
|
import * as compose_state from "./compose_state";
|
2021-02-28 01:26:48 +01:00
|
|
|
import * as compose_ui from "./compose_ui";
|
2021-03-25 23:20:18 +01:00
|
|
|
import {csrf_token} from "./csrf";
|
2021-04-13 06:51:54 +02:00
|
|
|
import {$t} from "./i18n";
|
2021-03-25 22:35:45 +01:00
|
|
|
import {page_params} from "./page_params";
|
2019-11-21 05:24:55 +01:00
|
|
|
|
2018-02-13 23:26:19 +01:00
|
|
|
// Show the upload button only if the browser supports it.
|
2022-01-25 11:36:19 +01:00
|
|
|
export function feature_check($upload_button) {
|
2021-03-05 17:27:53 +01:00
|
|
|
if (window.XMLHttpRequest && new window.XMLHttpRequest().upload) {
|
2022-01-25 11:36:19 +01:00
|
|
|
$upload_button.removeClass("notdisplayed");
|
2018-02-13 23:26:19 +01:00
|
|
|
}
|
2021-02-28 00:52:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function get_translated_status(file) {
|
2021-04-13 06:51:54 +02:00
|
|
|
const status = $t({defaultMessage: "Uploading {filename}…"}, {filename: file.name});
|
2020-04-17 17:37:06 +02:00
|
|
|
return "[" + status + "]()";
|
2021-02-28 00:52:20 +01:00
|
|
|
}
|
2018-02-13 23:26:19 +01:00
|
|
|
|
2023-03-28 07:54:27 +02:00
|
|
|
export function get_item(key, config, file_id) {
|
2019-11-21 05:24:55 +01:00
|
|
|
if (!config) {
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error("Missing config");
|
2019-11-21 05:24:55 +01:00
|
|
|
}
|
|
|
|
if (config.mode === "compose") {
|
|
|
|
switch (key) {
|
2020-07-15 02:14:03 +02:00
|
|
|
case "textarea":
|
|
|
|
return $("#compose-textarea");
|
|
|
|
case "send_button":
|
|
|
|
return $("#compose-send-button");
|
2023-01-09 07:43:39 +01:00
|
|
|
case "banner_container":
|
|
|
|
return $("#compose_banners");
|
2023-01-09 08:09:35 +01:00
|
|
|
case "upload_banner_identifier":
|
2023-03-28 07:54:27 +02:00
|
|
|
return `#compose_banners .upload_banner.file_${CSS.escape(file_id)}`;
|
2023-01-09 08:09:35 +01:00
|
|
|
case "upload_banner":
|
2023-03-28 07:54:27 +02:00
|
|
|
return $(`#compose_banners .upload_banner.file_${CSS.escape(file_id)}`);
|
2023-06-06 14:37:19 +02:00
|
|
|
case "upload_banner_cancel_button":
|
|
|
|
return $(
|
|
|
|
`#compose_banners .upload_banner.file_${CSS.escape(
|
|
|
|
file_id,
|
|
|
|
)} .upload_banner_cancel_button`,
|
|
|
|
);
|
2023-06-23 01:08:44 +02:00
|
|
|
case "upload_banner_hide_button":
|
2023-03-28 07:54:27 +02:00
|
|
|
return $(
|
|
|
|
`#compose_banners .upload_banner.file_${CSS.escape(
|
|
|
|
file_id,
|
2023-05-15 19:57:31 +02:00
|
|
|
)} .main-view-banner-close-button`,
|
2023-03-28 07:54:27 +02:00
|
|
|
);
|
2023-01-09 08:09:35 +01:00
|
|
|
case "upload_banner_message":
|
2023-03-28 07:54:27 +02:00
|
|
|
return $(`#compose_banners .upload_banner.file_${CSS.escape(file_id)} .upload_msg`);
|
2020-07-15 02:14:03 +02:00
|
|
|
case "file_input_identifier":
|
2021-04-20 16:14:55 +02:00
|
|
|
return "#compose .file_input";
|
2020-07-15 02:14:03 +02:00
|
|
|
case "source":
|
|
|
|
return "compose-file-input";
|
|
|
|
case "drag_drop_container":
|
|
|
|
return $("#compose");
|
2020-09-04 21:10:48 +02:00
|
|
|
case "markdown_preview_hide_button":
|
2021-04-20 19:50:01 +02:00
|
|
|
return $("#compose .undo_markdown_preview");
|
2020-07-15 02:14:03 +02:00
|
|
|
default:
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error(`Invalid key name for mode "${config.mode}"`);
|
2019-11-21 05:24:55 +01:00
|
|
|
}
|
|
|
|
} else if (config.mode === "edit") {
|
|
|
|
if (!config.row) {
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error("Missing row in config");
|
2019-11-21 05:24:55 +01:00
|
|
|
}
|
|
|
|
switch (key) {
|
2020-07-15 02:14:03 +02:00
|
|
|
case "textarea":
|
2021-04-20 19:50:01 +02:00
|
|
|
return $(`#edit_form_${CSS.escape(config.row)} .message_edit_content`);
|
2020-07-15 02:14:03 +02:00
|
|
|
case "send_button":
|
2021-04-20 19:50:01 +02:00
|
|
|
return $(`#edit_form_${CSS.escape(config.row)} .message_edit_content`)
|
2021-04-21 16:20:16 +02:00
|
|
|
.closest(".message_edit_form")
|
2020-07-15 00:34:28 +02:00
|
|
|
.find(".message_edit_save");
|
2023-01-09 07:43:39 +01:00
|
|
|
case "banner_container":
|
2023-03-28 07:54:27 +02:00
|
|
|
return $(`#edit_form_${CSS.escape(config.row)} .edit_form_banners`);
|
2023-01-09 08:09:35 +01:00
|
|
|
case "upload_banner_identifier":
|
2023-03-28 07:54:27 +02:00
|
|
|
return `#edit_form_${CSS.escape(config.row)} .upload_banner.file_${CSS.escape(
|
|
|
|
file_id,
|
|
|
|
)}`;
|
2023-01-09 08:09:35 +01:00
|
|
|
case "upload_banner":
|
2023-03-28 07:54:27 +02:00
|
|
|
return $(
|
|
|
|
`#edit_form_${CSS.escape(config.row)} .upload_banner.file_${CSS.escape(
|
|
|
|
file_id,
|
|
|
|
)}`,
|
|
|
|
);
|
2023-06-06 14:37:19 +02:00
|
|
|
case "upload_banner_cancel_button":
|
|
|
|
return $(
|
|
|
|
`#edit_form_${CSS.escape(config.row)} .upload_banner.file_${CSS.escape(
|
|
|
|
file_id,
|
|
|
|
)} .upload_banner_cancel_button`,
|
|
|
|
);
|
2023-06-23 01:08:44 +02:00
|
|
|
case "upload_banner_hide_button":
|
2023-01-09 07:43:39 +01:00
|
|
|
return $(
|
2023-03-28 07:54:27 +02:00
|
|
|
`#edit_form_${CSS.escape(config.row)} .upload_banner.file_${CSS.escape(
|
|
|
|
file_id,
|
2023-05-15 19:57:31 +02:00
|
|
|
)} .main-view-banner-close-button`,
|
2021-02-03 23:23:32 +01:00
|
|
|
);
|
2023-01-09 08:09:35 +01:00
|
|
|
case "upload_banner_message":
|
2023-03-28 07:54:27 +02:00
|
|
|
return $(
|
|
|
|
`#edit_form_${CSS.escape(config.row)} .upload_banner.file_${CSS.escape(
|
|
|
|
file_id,
|
|
|
|
)} .upload_msg`,
|
|
|
|
);
|
2020-07-15 02:14:03 +02:00
|
|
|
case "file_input_identifier":
|
2021-04-20 16:14:55 +02:00
|
|
|
return `#edit_form_${CSS.escape(config.row)} .file_input`;
|
2020-07-15 02:14:03 +02:00
|
|
|
case "source":
|
|
|
|
return "message-edit-file-input";
|
|
|
|
case "drag_drop_container":
|
2021-10-29 13:59:36 +02:00
|
|
|
return $(`#zfilt${CSS.escape(config.row)} .message_edit_form`);
|
2020-09-04 21:10:48 +02:00
|
|
|
case "markdown_preview_hide_button":
|
2021-04-20 19:50:01 +02:00
|
|
|
return $(`#edit_form_${CSS.escape(config.row)} .undo_markdown_preview`);
|
2020-07-15 02:14:03 +02:00
|
|
|
default:
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error(`Invalid key name for mode "${config.mode}"`);
|
2019-11-21 05:24:55 +01:00
|
|
|
}
|
|
|
|
} else {
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error("Invalid upload mode!");
|
2018-02-13 22:48:48 +01:00
|
|
|
}
|
2021-02-28 00:52:20 +01:00
|
|
|
}
|
2018-02-13 22:48:48 +01:00
|
|
|
|
2023-03-28 07:54:27 +02:00
|
|
|
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);
|
|
|
|
}
|
2023-01-09 07:43:39 +01:00
|
|
|
}
|
|
|
|
|
2023-06-06 14:37:19 +02:00
|
|
|
function add_upload_banner(
|
|
|
|
config,
|
|
|
|
banner_type,
|
|
|
|
banner_text,
|
|
|
|
file_id,
|
|
|
|
is_upload_process_tracker = false,
|
|
|
|
) {
|
2023-03-17 18:53:24 +01:00
|
|
|
const new_banner = render_upload_banner({
|
2023-01-09 07:43:39 +01:00
|
|
|
banner_type,
|
2023-06-06 14:37:19 +02:00
|
|
|
is_upload_process_tracker,
|
2023-01-09 07:43:39 +01:00
|
|
|
banner_text,
|
2023-03-28 07:54:27 +02:00
|
|
|
file_id,
|
2023-01-09 07:43:39 +01:00
|
|
|
});
|
2023-05-02 11:05:37 +02:00
|
|
|
compose_banner.append_compose_banner_to_banner_list(
|
|
|
|
new_banner,
|
|
|
|
get_item("banner_container", config),
|
|
|
|
);
|
2021-02-28 00:52:20 +01:00
|
|
|
}
|
2019-11-21 05:24:55 +01:00
|
|
|
|
2021-04-13 06:51:54 +02:00
|
|
|
export function show_error_message(
|
|
|
|
config,
|
|
|
|
message = $t({defaultMessage: "An unknown error occurred."}),
|
2023-03-28 07:54:27 +02:00
|
|
|
file_id = null,
|
2021-04-13 06:51:54 +02:00
|
|
|
) {
|
2021-02-28 00:52:20 +01:00
|
|
|
get_item("send_button", config).prop("disabled", false);
|
2023-03-28 07:54:27 +02:00
|
|
|
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");
|
|
|
|
}
|
2021-02-28 00:52:20 +01:00
|
|
|
}
|
2019-11-21 05:24:55 +01:00
|
|
|
|
2023-01-09 07:43:39 +01:00
|
|
|
export async function upload_files(uppy, config, files) {
|
2019-11-21 05:24:55 +01:00
|
|
|
if (files.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-08 06:57:19 +02:00
|
|
|
if (page_params.max_file_upload_size_mib === 0) {
|
2021-02-28 00:52:20 +01:00
|
|
|
show_error_message(
|
2020-07-15 00:34:28 +02:00
|
|
|
config,
|
2021-04-13 06:51:54 +02:00
|
|
|
$t({
|
|
|
|
defaultMessage: "File and image uploads have been disabled for this organization.",
|
|
|
|
}),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2019-11-21 05:24:55 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-09-04 21:10:48 +02:00
|
|
|
|
|
|
|
// If we're looking at a markdown preview, switch back to the edit
|
|
|
|
// UI. This is important for all the later logic around focus
|
|
|
|
// (etc.) to work correctly.
|
|
|
|
//
|
|
|
|
// We implement this transition through triggering a click on the
|
|
|
|
// toggle button to take advantage of the existing plumbing for
|
|
|
|
// handling the compose and edit UIs.
|
2021-02-28 00:52:20 +01:00
|
|
|
if (get_item("markdown_preview_hide_button", config).is(":visible")) {
|
|
|
|
get_item("markdown_preview_hide_button", config).trigger("click");
|
2020-09-04 21:10:48 +02:00
|
|
|
}
|
|
|
|
|
2021-02-28 00:52:20 +01:00
|
|
|
get_item("send_button", config).prop("disabled", true);
|
2019-11-21 05:24:55 +01:00
|
|
|
|
|
|
|
for (const file of files) {
|
|
|
|
try {
|
2020-07-15 00:34:28 +02:00
|
|
|
compose_ui.insert_syntax_and_focus(
|
2021-02-28 00:52:20 +01:00
|
|
|
get_translated_status(file),
|
|
|
|
get_item("textarea", config),
|
2023-04-05 18:21:45 +02:00
|
|
|
"block",
|
|
|
|
1,
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2021-02-28 00:52:20 +01:00
|
|
|
compose_ui.autosize_textarea(get_item("textarea", config));
|
2023-03-28 07:54:27 +02:00
|
|
|
file.id = uppy.addFile({
|
2021-02-28 00:52:20 +01:00
|
|
|
source: get_item("source", config),
|
2019-11-21 05:24:55 +01:00
|
|
|
name: file.name,
|
|
|
|
type: file.type,
|
|
|
|
data: file,
|
|
|
|
});
|
2020-10-07 10:18:48 +02:00
|
|
|
} catch {
|
2019-11-21 05:24:55 +01:00
|
|
|
// Errors are handled by info-visible and upload-error event callbacks.
|
2023-03-28 07:54:27 +02:00
|
|
|
continue;
|
2018-04-14 11:48:22 +02:00
|
|
|
}
|
2023-03-28 07:54:27 +02:00
|
|
|
|
|
|
|
add_upload_banner(
|
|
|
|
config,
|
|
|
|
"info",
|
|
|
|
$t({defaultMessage: "Uploading {filename}…"}, {filename: file.name}),
|
|
|
|
file.id,
|
2023-06-06 14:37:19 +02:00
|
|
|
true,
|
2023-03-28 07:54:27 +02:00
|
|
|
);
|
2023-06-06 14:37:19 +02:00
|
|
|
get_item("upload_banner_cancel_button", config, file.id).one("click", () => {
|
2023-03-28 07:54:27 +02:00
|
|
|
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);
|
2023-06-06 14:37:19 +02:00
|
|
|
});
|
2023-06-23 01:08:44 +02:00
|
|
|
get_item("upload_banner_hide_button", config, file.id).one("click", () => {
|
2023-06-06 14:37:19 +02:00
|
|
|
hide_upload_banner(uppy, config, file.id);
|
2023-03-28 07:54:27 +02:00
|
|
|
});
|
2019-11-21 05:24:55 +01:00
|
|
|
}
|
2021-02-28 00:52:20 +01:00
|
|
|
}
|
2018-04-14 11:48:22 +02:00
|
|
|
|
2021-02-28 00:52:20 +01:00
|
|
|
export function setup_upload(config) {
|
2020-12-11 04:26:23 +01:00
|
|
|
const uppy = new Uppy({
|
2019-11-21 05:24:55 +01:00
|
|
|
debug: false,
|
|
|
|
autoProceed: true,
|
|
|
|
restrictions: {
|
2020-05-08 06:57:19 +02:00
|
|
|
maxFileSize: page_params.max_file_upload_size_mib * 1024 * 1024,
|
2019-11-21 05:24:55 +01:00
|
|
|
},
|
|
|
|
locale: {
|
|
|
|
strings: {
|
2022-06-12 21:43:55 +02:00
|
|
|
exceedsSize: $t(
|
|
|
|
{
|
|
|
|
defaultMessage:
|
|
|
|
"%'{file}' exceeds the maximum file size for attachments ({variable} MB).",
|
|
|
|
},
|
|
|
|
{variable: `${page_params.max_file_upload_size_mib}`},
|
|
|
|
),
|
2021-04-13 06:51:54 +02:00
|
|
|
failedToUpload: $t({defaultMessage: "Failed to upload %'{file}'"}),
|
2019-11-21 05:24:55 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
uppy.setMeta({
|
|
|
|
csrfmiddlewaretoken: csrf_token,
|
|
|
|
});
|
2020-07-15 00:34:28 +02:00
|
|
|
uppy.use(XHRUpload, {
|
|
|
|
endpoint: "/json/user_uploads",
|
|
|
|
formData: true,
|
|
|
|
fieldName: "file",
|
|
|
|
// Number of concurrent uploads
|
|
|
|
limit: 5,
|
|
|
|
locale: {
|
|
|
|
strings: {
|
2021-04-13 06:51:54 +02:00
|
|
|
timedOut: $t({
|
|
|
|
defaultMessage: "Upload stalled for %'{seconds}' seconds, aborting.",
|
|
|
|
}),
|
2019-11-21 05:24:55 +01:00
|
|
|
},
|
2020-07-02 02:16:03 +02:00
|
|
|
},
|
2020-07-15 00:34:28 +02:00
|
|
|
});
|
2019-11-21 05:24:55 +01:00
|
|
|
|
2023-03-28 07:54:27 +02:00
|
|
|
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}%`,
|
2023-01-09 07:06:16 +01:00
|
|
|
});
|
2019-11-21 05:24:55 +01:00
|
|
|
});
|
|
|
|
|
2021-02-28 00:52:20 +01:00
|
|
|
$("body").on("change", get_item("file_input_identifier", config), (event) => {
|
2019-11-21 05:24:55 +01:00
|
|
|
const files = event.target.files;
|
2021-02-28 00:52:20 +01:00
|
|
|
upload_files(uppy, config, files);
|
2022-11-22 11:16:58 +01:00
|
|
|
get_item("textarea", config).trigger("focus");
|
2020-04-09 20:19:37 +02:00
|
|
|
event.target.value = "";
|
2019-11-21 05:24:55 +01:00
|
|
|
});
|
|
|
|
|
2023-05-02 21:54:11 +02:00
|
|
|
const $banner_container = get_item("banner_container", config);
|
|
|
|
$banner_container.on(
|
2023-03-28 07:54:27 +02:00
|
|
|
"click",
|
2023-05-15 19:57:31 +02:00
|
|
|
".upload_banner.file_generic_error .main-view-banner-close-button",
|
2023-03-28 07:54:27 +02:00
|
|
|
(event) => {
|
|
|
|
event.preventDefault();
|
|
|
|
$(event.target).parents(".upload_banner").remove();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
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());
|
2019-11-21 05:24:55 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$drag_drop_container.on("drop", (event) => {
|
2019-11-21 05:24:55 +01:00
|
|
|
event.preventDefault();
|
|
|
|
const files = event.originalEvent.dataTransfer.files;
|
2023-04-12 13:10:40 +02:00
|
|
|
if (config.mode === "compose" && !compose_state.composing()) {
|
|
|
|
compose_actions.respond_to_message({trigger: "file drop or paste"});
|
|
|
|
}
|
2021-02-28 00:52:20 +01:00
|
|
|
upload_files(uppy, config, files);
|
2019-11-21 05:24:55 +01:00
|
|
|
});
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$drag_drop_container.on("paste", (event) => {
|
2019-11-21 05:24:55 +01:00
|
|
|
const clipboard_data = event.clipboardData || event.originalEvent.clipboardData;
|
|
|
|
if (!clipboard_data) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const items = clipboard_data.items;
|
|
|
|
const files = [];
|
|
|
|
for (const item of items) {
|
|
|
|
if (item.kind !== "file") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const file = item.getAsFile();
|
|
|
|
files.push(file);
|
2018-02-13 22:39:51 +01:00
|
|
|
}
|
2023-04-12 13:10:40 +02:00
|
|
|
if (config.mode === "compose" && !compose_state.composing()) {
|
|
|
|
compose_actions.respond_to_message({trigger: "file drop or paste"});
|
|
|
|
}
|
2021-02-28 00:52:20 +01:00
|
|
|
upload_files(uppy, config, files);
|
2019-11-21 05:24:55 +01:00
|
|
|
});
|
2017-11-23 15:00:05 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
uppy.on("upload-success", (file, response) => {
|
2023-04-09 05:22:23 +02:00
|
|
|
const url = response.body.uri;
|
|
|
|
if (url === undefined) {
|
2018-02-13 22:39:51 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-04-09 05:22:23 +02:00
|
|
|
const split_url = url.split("/");
|
|
|
|
const filename = split_url.at(-1);
|
|
|
|
const filename_url = "[" + filename + "](" + url + ")";
|
2020-07-15 00:34:28 +02:00
|
|
|
compose_ui.replace_syntax(
|
2021-02-28 00:52:20 +01:00
|
|
|
get_translated_status(file),
|
2023-04-09 05:22:23 +02:00
|
|
|
filename_url,
|
2021-02-28 00:52:20 +01:00
|
|
|
get_item("textarea", config),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2021-02-28 00:52:20 +01:00
|
|
|
compose_ui.autosize_textarea(get_item("textarea", config));
|
2018-04-05 04:29:21 +02:00
|
|
|
|
2023-03-28 07:54:27 +02:00
|
|
|
// 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);
|
2019-11-21 05:24:55 +01:00
|
|
|
});
|
2017-11-23 15:00:05 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
uppy.on("info-visible", () => {
|
2023-03-28 07:54:27 +02:00
|
|
|
// Uppy's `info-visible` event is issued after appending the
|
2022-06-12 21:43:55 +02:00
|
|
|
// 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.
|
2023-03-28 07:54:27 +02:00
|
|
|
const infoList = uppy.getState().info;
|
2023-05-11 21:49:21 +02:00
|
|
|
const info = infoList.at(-1);
|
2019-11-21 05:24:55 +01:00
|
|
|
if (info.type === "error" && info.message === "No Internet connection") {
|
|
|
|
// server_events already handles the case of no internet.
|
|
|
|
return;
|
2018-02-13 23:29:17 +01:00
|
|
|
}
|
2019-11-21 05:24:55 +01:00
|
|
|
|
|
|
|
if (info.type === "error" && info.details === "Upload Error") {
|
|
|
|
// The server errors come under 'Upload Error'. But we can't handle them
|
|
|
|
// here because info object don't contain response.body.msg received from
|
|
|
|
// the server. Server errors are hence handled by on('upload-error').
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.type === "error") {
|
|
|
|
// The remaining errors are mostly frontend errors like file being too large
|
|
|
|
// for upload.
|
2021-02-28 00:52:20 +01:00
|
|
|
show_error_message(config, info.message);
|
2019-11-21 05:24:55 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-06-29 21:59:08 +02:00
|
|
|
uppy.on("upload-error", (file, _error, response) => {
|
2021-03-24 21:44:43 +01:00
|
|
|
const message = response ? response.body.msg : undefined;
|
2023-03-28 07:54:27 +02:00
|
|
|
show_error_message(config, message, file.id);
|
2021-02-28 00:52:20 +01:00
|
|
|
compose_ui.replace_syntax(get_translated_status(file), "", get_item("textarea", config));
|
|
|
|
compose_ui.autosize_textarea(get_item("textarea", config));
|
2019-11-21 05:24:55 +01:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
uppy.on("restriction-failed", (file) => {
|
2021-02-28 00:52:20 +01:00
|
|
|
compose_ui.replace_syntax(get_translated_status(file), "", get_item("textarea", config));
|
|
|
|
compose_ui.autosize_textarea(get_item("textarea", config));
|
2020-03-07 12:44:17 +01:00
|
|
|
});
|
|
|
|
|
2019-11-21 05:24:55 +01:00
|
|
|
return uppy;
|
2021-02-28 00:52:20 +01:00
|
|
|
}
|