Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/lib/forms/RichEditor.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file is part of React-Invenio-Forms
// Copyright (C) 2022-2026 CERN.
// Copyright (C) 2020 Northwestern University.
// Copyright (C) 2024 KTH Royal Institute of Technology.
// Copyright (C) 2024-2026 KTH Royal Institute of Technology.
//
// React-Invenio-Forms is free software; you can redistribute it and/or modify it
// under the terms of the MIT License; see LICENSE file for more details.
Expand Down Expand Up @@ -134,6 +134,22 @@ export class RichEditor extends Component {
}
};

insertUploadedFile = (location, info = {}) => {
if (!this.editorRef.current) {
return;
}
if (info.alt) {
this.editorRef.current.insertContent(`<img src="${location}" alt="${info.alt}">`);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do images always have an alt provided? I'm wondering if there is not a better way to distinguish between images and files.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason for this change is that meta.filetype was not reliable in this flow. TinyMCE was reporting images as file, so image uploads were not inserted correctly.

If I understand correctly, alt is not used to distinguish images from files; it is only added after the file has already been classified as an image.

If you have a better suggestion, please let me know and I can update the fix.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot think of another way the documented way using meta.filetype.
If this doesn't work with S3, I guess this is the best we can do.

} else {
const text = info.text || location;
this.editorRef.current.insertContent(`<a href="${location}">${text}</a>`);
}
};

getPickerFileType = (file) => {
return file?.type?.startsWith("image/") ? "image" : "file";
};

/**
* This function is called when a a user clicks on the upload icons
* in the Link and Image popup dialogs.
Expand Down Expand Up @@ -162,6 +178,7 @@ export class RichEditor extends Component {

input.onchange = (event) => {
const file = event.target.files[0];
const selectedFileType = this.getPickerFileType(file);
const filename = file.name;

if (this.editorRef.current) {
Expand Down Expand Up @@ -189,9 +206,9 @@ export class RichEditor extends Component {
}

const locationRelative = new URL(json.data.links.download_html).pathname;
if (meta.filetype === "file") {
if (selectedFileType === "file") {
callback(locationRelative, { text: json.data.metadata.original_filename });
} else if (meta.filetype === "image") {
} else if (selectedFileType === "image") {
callback(locationRelative, {
alt: `Description of ${json.data.metadata.original_filename}`,
});
Expand Down Expand Up @@ -219,7 +236,7 @@ export class RichEditor extends Component {
* or on the attach files button next to the files list.
*/
onAttachFiles = () => {
this.filePickerCallback(() => {}, "", "file");
this.filePickerCallback(this.insertUploadedFile, "", { filetype: "file" });
};

mapToEditorLinkList = (files) => {
Expand Down
Loading