2022-10-07 00:56:00 +02:00
|
|
|
import {Uppy} from "@uppy/core";
|
2021-02-28 00:52:20 +01:00
|
|
|
import ProgressBar from "@uppy/progress-bar";
|
|
|
|
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
|
|
|
|
2021-02-28 21:32:22 +01:00
|
|
|
import * as compose_actions from "./compose_actions";
|
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
|
|
|
|
2021-02-28 00:52:20 +01:00
|
|
|
export function get_item(key, config) {
|
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");
|
|
|
|
case "send_status_identifier":
|
|
|
|
return "#compose-send-status";
|
|
|
|
case "send_status":
|
|
|
|
return $("#compose-send-status");
|
|
|
|
case "send_status_close_button":
|
|
|
|
return $(".compose-send-status-close");
|
|
|
|
case "send_status_message":
|
|
|
|
return $("#compose-error-msg");
|
|
|
|
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");
|
2020-07-15 02:14:03 +02:00
|
|
|
case "send_status_identifier":
|
2021-02-03 23:23:32 +01:00
|
|
|
return `#message-edit-send-status-${CSS.escape(config.row)}`;
|
2020-07-15 02:14:03 +02:00
|
|
|
case "send_status":
|
2021-02-03 23:23:32 +01:00
|
|
|
return $(`#message-edit-send-status-${CSS.escape(config.row)}`);
|
2020-07-15 02:14:03 +02:00
|
|
|
case "send_status_close_button":
|
2021-02-03 23:23:32 +01:00
|
|
|
return $(`#message-edit-send-status-${CSS.escape(config.row)}`).find(
|
|
|
|
".send-status-close",
|
|
|
|
);
|
2020-07-15 02:14:03 +02:00
|
|
|
case "send_status_message":
|
2021-02-03 23:23:32 +01:00
|
|
|
return $(`#message-edit-send-status-${CSS.escape(config.row)}`).find(".error-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
|
|
|
|
2021-02-28 00:52:20 +01:00
|
|
|
export function hide_upload_status(config) {
|
|
|
|
get_item("send_button", config).prop("disabled", false);
|
|
|
|
get_item("send_status", config).removeClass("alert-info").hide();
|
|
|
|
}
|
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."}),
|
|
|
|
) {
|
2021-02-28 00:52:20 +01:00
|
|
|
get_item("send_button", config).prop("disabled", false);
|
|
|
|
get_item("send_status", config).addClass("alert-error").removeClass("alert-info").show();
|
|
|
|
get_item("send_status_message", config).text(message);
|
|
|
|
}
|
2019-11-21 05:24:55 +01:00
|
|
|
|
2021-02-28 00:52:20 +01:00
|
|
|
export 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);
|
|
|
|
get_item("send_status", config).addClass("alert-info").removeClass("alert-error").show();
|
2021-04-13 06:51:54 +02:00
|
|
|
get_item("send_status_message", config).html($("<p>").text($t({defaultMessage: "Uploading…"})));
|
2021-02-28 00:52:20 +01:00
|
|
|
get_item("send_status_close_button", config).one("click", () => {
|
2021-01-22 22:29:08 +01:00
|
|
|
for (const file of uppy.getFiles()) {
|
2020-07-15 00:34:28 +02:00
|
|
|
compose_ui.replace_syntax(
|
2021-02-28 00:52:20 +01:00
|
|
|
get_translated_status(file),
|
2020-07-15 00:34:28 +02:00
|
|
|
"",
|
2021-02-28 00:52:20 +01:00
|
|
|
get_item("textarea", config),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2021-01-22 22:29:08 +01:00
|
|
|
}
|
2021-02-28 00:52:20 +01:00
|
|
|
compose_ui.autosize_textarea(get_item("textarea", config));
|
2019-11-21 05:24:55 +01:00
|
|
|
uppy.cancelAll();
|
2021-02-28 00:52:20 +01:00
|
|
|
get_item("textarea", config).trigger("focus");
|
2020-07-02 01:45:54 +02:00
|
|
|
setTimeout(() => {
|
2021-02-28 00:52:20 +01:00
|
|
|
hide_upload_status(config);
|
2019-11-21 05:24:55 +01:00
|
|
|
}, 500);
|
|
|
|
});
|
|
|
|
|
|
|
|
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),
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2021-02-28 00:52:20 +01:00
|
|
|
compose_ui.autosize_textarea(get_item("textarea", config));
|
2019-11-21 05:24:55 +01:00
|
|
|
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.
|
2020-04-16 17:11:06 +02:00
|
|
|
break;
|
2018-04-14 11:48:22 +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
|
|
|
|
|
|
|
uppy.use(ProgressBar, {
|
2021-02-28 00:52:20 +01:00
|
|
|
target: get_item("send_status_identifier", config),
|
2019-11-21 05:24:55 +01:00
|
|
|
hideAfterFinish: false,
|
|
|
|
});
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
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;
|
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
|
|
|
}
|
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) => {
|
2019-11-21 05:24:55 +01:00
|
|
|
const uri = response.body.uri;
|
|
|
|
if (uri === undefined) {
|
2018-02-13 22:39:51 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-11-21 05:24:55 +01:00
|
|
|
const split_uri = uri.split("/");
|
2022-01-24 09:05:06 +01:00
|
|
|
const filename = split_uri.at(-1);
|
2020-07-23 10:20:10 +02:00
|
|
|
if (config.mode === "compose" && !compose_state.composing()) {
|
2020-07-15 01:29:15 +02:00
|
|
|
compose_actions.start("stream");
|
2018-02-13 22:39:51 +01:00
|
|
|
}
|
2020-07-29 22:14:07 +02:00
|
|
|
const filename_uri = "[" + filename + "](" + uri + ")";
|
2020-07-15 00:34:28 +02:00
|
|
|
compose_ui.replace_syntax(
|
2021-02-28 00:52:20 +01:00
|
|
|
get_translated_status(file),
|
2020-07-15 00:34:28 +02:00
|
|
|
filename_uri,
|
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));
|
2019-11-21 05:24:55 +01:00
|
|
|
});
|
2018-04-05 04:29:21 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
uppy.on("complete", () => {
|
2020-04-14 16:16:49 +02:00
|
|
|
let uploads_in_progress = false;
|
2021-01-22 22:29:08 +01:00
|
|
|
for (const file of uppy.getFiles()) {
|
2020-04-14 16:16:49 +02:00
|
|
|
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;
|
|
|
|
}
|
2021-01-22 22:29:08 +01:00
|
|
|
}
|
2020-04-14 16:16:49 +02:00
|
|
|
|
2021-02-28 00:52:20 +01:00
|
|
|
const has_errors = get_item("send_status", config).hasClass("alert-error");
|
2020-04-16 18:22:43 +02:00
|
|
|
if (!uploads_in_progress && !has_errors) {
|
2020-07-02 01:45:54 +02:00
|
|
|
setTimeout(() => {
|
2021-02-28 00:52:20 +01:00
|
|
|
hide_upload_status(config);
|
2020-04-14 16:16:49 +02:00
|
|
|
}, 500);
|
|
|
|
}
|
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", () => {
|
2022-06-12 21:43:55 +02:00
|
|
|
// Uppy's `info-visible` event is issued after prepending 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];
|
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.
|
|
|
|
uppy.cancelAll();
|
2021-02-28 00:52:20 +01:00
|
|
|
show_error_message(config, info.message);
|
2019-11-21 05:24:55 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
uppy.on("upload-error", (file, error, response) => {
|
2021-03-24 21:44:43 +01:00
|
|
|
const message = response ? response.body.msg : undefined;
|
2019-11-21 05:24:55 +01:00
|
|
|
uppy.cancelAll();
|
2021-02-28 00:52:20 +01:00
|
|
|
show_error_message(config, message);
|
|
|
|
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
|
|
|
}
|