.join("");
}
-function clearPreviousErrors(element: WoltlabCoreFileUploadElement): void {
+export function clearPreviousErrors(element: WoltlabCoreFileUploadElement): void {
element.parentElement?.querySelectorAll(".innerError").forEach((x) => x.remove());
}
removeUploadProgress,
trackUploadProgress,
} from "WoltLabSuite/Core/Component/File/File";
+import { clearPreviousErrors } from "WoltLabSuite/Core/Component/File/Upload";
const _data = new Map<string, FileProcessor>();
readonly #imageOnly: boolean;
readonly #singleFileUpload: boolean;
readonly #extraButtons: ExtraButton[];
+ #uploadResolve: undefined | (() => void);
constructor(
fieldId: string,
this.#uploadButton = this.#container.querySelector("woltlab-core-file-upload") as WoltlabCoreFileUploadElement;
this.#uploadButton.addEventListener("uploadStart", (event: CustomEvent<WoltlabCoreFileElement>) => {
+ if (this.#uploadResolve !== undefined) {
+ this.#uploadResolve();
+ }
+
this.#registerFile(event.detail);
});
this.#fileInput = this.#uploadButton.shadowRoot!.querySelector<HTMLInputElement>('input[type="file"]')!;
context.__replace = true;
this.#uploadButton.dataset.context = JSON.stringify(context);
- // Remove the element and all buttons from the dom, but keep them stored in a variable.
- // If the user cancels the dialog or the upload fails, reinsert the old elements and show an error message.
- // If the upload is successful, delete the old file.
this.#replaceElement = element;
this.#unregisterFile(element);
+ clearPreviousErrors(this.#uploadButton);
+
const changeEventListener = () => {
- this.#uploadButton.dataset.context = oldContext;
this.#fileInput.removeEventListener("cancel", cancelEventListener);
+
+ // Wait until the upload starts,
+ // the request to the server is not synchronized with the end of the `change` event.
+ // Otherwise, we would swap the context too soon.
+ void new Promise<void>((resolve) => {
+ this.#uploadResolve = resolve;
+ }).then(() => {
+ this.#uploadResolve = undefined;
+ this.#uploadButton.dataset.context = oldContext;
+ });
};
const cancelEventListener = () => {
this.#uploadButton.dataset.context = oldContext;
define(["require", "exports", "tslib", "WoltLabSuite/Core/Helper/Selector", "WoltLabSuite/Core/Api/Files/Upload", "WoltLabSuite/Core/Api/Files/Chunk/Chunk", "WoltLabSuite/Core/Api/Files/GenerateThumbnails", "WoltLabSuite/Core/Image/Resizer", "WoltLabSuite/Core/Dom/Util", "WoltLabSuite/Core/Language"], function (require, exports, tslib_1, Selector_1, Upload_1, Chunk_1, GenerateThumbnails_1, Resizer_1, Util_1, Language_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
- exports.setup = void 0;
+ exports.setup = exports.clearPreviousErrors = void 0;
Resizer_1 = tslib_1.__importDefault(Resizer_1);
async function upload(element, file) {
const objectType = element.dataset.objectType;
function clearPreviousErrors(element) {
element.parentElement?.querySelectorAll(".innerError").forEach((x) => x.remove());
}
+ exports.clearPreviousErrors = clearPreviousErrors;
async function resizeImage(element, file) {
switch (file.type) {
case "image/jpeg":
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.1
*/
-define(["require", "exports", "tslib", "WoltLabSuite/Core/Language", "WoltLabSuite/Core/Api/Files/DeleteFile", "WoltLabSuite/Core/Dom/Change/Listener", "WoltLabSuite/Core/Component/File/File"], function (require, exports, tslib_1, Language_1, DeleteFile_1, Listener_1, File_1) {
+define(["require", "exports", "tslib", "WoltLabSuite/Core/Language", "WoltLabSuite/Core/Api/Files/DeleteFile", "WoltLabSuite/Core/Dom/Change/Listener", "WoltLabSuite/Core/Component/File/File", "WoltLabSuite/Core/Component/File/Upload"], function (require, exports, tslib_1, Language_1, DeleteFile_1, Listener_1, File_1, Upload_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getValues = exports.FileProcessor = void 0;
#imageOnly;
#singleFileUpload;
#extraButtons;
+ #uploadResolve;
constructor(fieldId, singleFileUpload = false, imageOnly = false, extraButtons = []) {
this.#fieldId = fieldId;
this.#imageOnly = imageOnly;
}
this.#uploadButton = this.#container.querySelector("woltlab-core-file-upload");
this.#uploadButton.addEventListener("uploadStart", (event) => {
+ if (this.#uploadResolve !== undefined) {
+ this.#uploadResolve();
+ }
this.#registerFile(event.detail);
});
this.#fileInput = this.#uploadButton.shadowRoot.querySelector('input[type="file"]');
const context = JSON.parse(oldContext);
context.__replace = true;
this.#uploadButton.dataset.context = JSON.stringify(context);
- // Remove the element and all buttons from the dom, but keep them stored in a variable.
- // If the user cancels the dialog or the upload fails, reinsert the old elements and show an error message.
- // If the upload is successful, delete the old file.
this.#replaceElement = element;
this.#unregisterFile(element);
+ (0, Upload_1.clearPreviousErrors)(this.#uploadButton);
const changeEventListener = () => {
- this.#uploadButton.dataset.context = oldContext;
this.#fileInput.removeEventListener("cancel", cancelEventListener);
+ // Wait until the upload starts,
+ // the request to the server is not synchronized with the end of the `change` event.
+ // Otherwise, we would swap the context too soon.
+ void new Promise((resolve) => {
+ this.#uploadResolve = resolve;
+ }).then(() => {
+ this.#uploadResolve = undefined;
+ this.#uploadButton.dataset.context = oldContext;
+ });
};
const cancelEventListener = () => {
this.#uploadButton.dataset.context = oldContext;
public function hasReachedUploadLimit(IFileProcessor $fileProcessor, array $context): bool
{
- // Replace button from `FileProcessorFormField` called
- if (isset($context["__replace"]) && $context["__replace"] === true) {
+ // Replace button from `FileProcessorFormField` is clicked.
+ if (!empty($context["__replace"])) {
return false;
}