diff --git a/src/lib/forms/RichEditor.js b/src/lib/forms/RichEditor.js index ea3c34b7..fa8d32d4 100644 --- a/src/lib/forms/RichEditor.js +++ b/src/lib/forms/RichEditor.js @@ -1,7 +1,7 @@ /* * SPDX-FileCopyrightText: 2022-2026 CERN. * SPDX-FileCopyrightText: 2020 Northwestern University. - * SPDX-FileCopyrightText: 2024 KTH Royal Institute of Technology. + * SPDX-FileCopyrightText: 2024-2026 KTH Royal Institute of Technology. * SPDX-License-Identifier: MIT */ import React, { Component } from "react"; @@ -133,6 +133,22 @@ export class RichEditor extends Component { } }; + insertUploadedFile = (location, info = {}) => { + if (!this.editorRef.current) { + return; + } + if (info.alt) { + this.editorRef.current.insertContent(`${info.alt}`); + } else { + const text = info.text || location; + this.editorRef.current.insertContent(`${text}`); + } + }; + + 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. @@ -161,6 +177,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) { @@ -188,9 +205,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}`, }); @@ -218,7 +235,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) => {