2021-02-28 00:52:20 +01:00
|
|
|
import Uppy from "@uppy/core";
|
|
|
|
import ProgressBar from "@uppy/progress-bar";
|
|
|
|
import XHRUpload from "@uppy/xhr-upload";
|
2020-08-01 03:43:15 +02:00
|
|
|
|
2021-02-28 00:52:20 +01:00
|
|
|
import * as compose from "./compose";
|
|
|
|
import * as compose_state from "./compose_state";
|
2019-11-21 05:24:55 +01:00
|
|
|
|
2021-02-28 00:52:20 +01:00
|
|
|
export function make_upload_absolute(uri) {
|
2020-01-28 15:26:02 +01:00
|
|
|
if (uri.startsWith(compose.uploads_path)) {
|
2017-11-23 15:00:05 +01:00
|
|
|
// Rewrite the URI to a usable link
|
|
|
|
return compose.uploads_domain + uri;
|
|
|
|
}
|
|
|
|
return uri;
|
2021-02-28 00:52:20 +01:00
|
|
|
}
|
2017-11-23 15:00:05 +01:00
|
|
|
|
2018-02-13 23:26:19 +01:00
|
|
|
// Show the upload button only if the browser supports it.
|
2021-02-28 00:52:20 +01:00
|
|
|
export function feature_check(upload_button) {
|
2019-11-09 02:48:01 +01:00
|
|
|
if (window.XMLHttpRequest && new XMLHttpRequest().upload) {
|
2018-02-13 23:26:19 +01:00
|
|
|
upload_button.removeClass("notdisplayed");
|
|
|
|
}
|
2021-02-28 00:52:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function get_translated_status(file) {
|
2020-04-17 17:37:06 +02:00
|
|
|
const status = i18n.t("Uploading __filename__…", {filename: file.name});
|
|
|
|
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":
|
|
|
|
return "#file_input";
|
|
|
|
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":
|
|
|
|
return $("#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-02-03 23:23:32 +01:00
|
|
|
return $(`#message_edit_content_${CSS.escape(config.row)}`);
|
2020-07-15 02:14:03 +02:00
|
|
|
case "send_button":
|
2021-02-03 23:23:32 +01:00
|
|
|
return $(`#message_edit_content_${CSS.escape(config.row)}`)
|
2020-07-15 00:34:28 +02:00
|
|
|
.closest("#message_edit_form")
|
|
|
|
.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-02-03 23:23:32 +01:00
|
|
|
return `#message_edit_file_input_${CSS.escape(config.row)}`;
|
2020-07-15 02:14:03 +02:00
|
|
|
case "source":
|
|
|
|
return "message-edit-file-input";
|
|
|
|
case "drag_drop_container":
|
|
|
|
return $("#message_edit_form");
|
2020-09-04 21:10:48 +02:00
|
|
|
case "markdown_preview_hide_button":
|
2021-02-03 23:23:32 +01:00
|
|
|
return $(`#undo_markdown_preview_${CSS.escape(config.row)}`);
|
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-02-28 00:52:20 +01:00
|
|
|
export function show_error_message(config, message) {
|
2019-11-21 05:24:55 +01:00
|
|
|
if (!message) {
|
|
|
|
message = i18n.t("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,
|
|
|
|
i18n.t("File and image uploads have been disabled for this organization."),
|
|
|
|
);
|
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();
|
|
|
|
get_item("send_status_message", config).html($("<p>").text(i18n.t("Uploading…")));
|
|
|
|
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: {
|
2020-07-15 01:29:15 +02:00
|
|
|
exceedsSize: i18n.t("This file exceeds maximum allowed size of"),
|
|
|
|
failedToUpload: i18n.t("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: {
|
|
|
|
timedOut: i18n.t("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);
|
2020-04-09 20:19:37 +02:00
|
|
|
event.target.value = "";
|
2019-11-21 05:24:55 +01:00
|
|
|
});
|
|
|
|
|
2021-02-28 00:52:20 +01:00
|
|
|
const drag_drop_container = get_item("drag_drop_container", config);
|
2019-11-21 05:24:55 +01:00
|
|
|
drag_drop_container.on("dragover", (event) => event.preventDefault());
|
|
|
|
drag_drop_container.on("dragenter", (event) => event.preventDefault());
|
|
|
|
|
|
|
|
drag_drop_container.on("drop", (event) => {
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
drag_drop_container.on("paste", (event) => {
|
|
|
|
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("/");
|
2019-11-02 00:06:25 +01:00
|
|
|
const filename = split_uri[split_uri.length - 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
|
|
|
}
|
2021-02-28 00:52:20 +01:00
|
|
|
const absolute_uri = make_upload_absolute(uri);
|
2019-11-21 05:24:55 +01:00
|
|
|
const filename_uri = "[" + filename + "](" + absolute_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", () => {
|
2019-11-21 05:24:55 +01:00
|
|
|
const info = uppy.getState().info;
|
|
|
|
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) => {
|
2019-11-21 05:24:55 +01:00
|
|
|
const message = response ? response.body.msg : null;
|
|
|
|
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
|
|
|
}
|