From 24bb1078eece00a2e0c5888a3f95b99dc16a71a0 Mon Sep 17 00:00:00 2001 From: Benno Lorenz Date: Tue, 10 Jun 2025 23:06:59 +0200 Subject: [PATCH] Maybe fixed cms issue --- static/admin/config.yml | 1 + static/admin/prefixed-image-widget.js | 54 +++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/static/admin/config.yml b/static/admin/config.yml index 86b7130..7351db1 100644 --- a/static/admin/config.yml +++ b/static/admin/config.yml @@ -1,6 +1,7 @@ backend: name: gitea repo: bennolor/hugoblog # Path to your Gitea repository + branch: master app_id: 10f0fe83-402c-4beb-b732-386fc394b4b5 # The Client ID provided by Gitea api_root: https://gitea.benno-lorenz.com/api/v1 # API URL of your Gitea instance base_url: https://gitea.benno-lorenz.com # Root URL of your Gitea instance diff --git a/static/admin/prefixed-image-widget.js b/static/admin/prefixed-image-widget.js index 0165be6..16028d8 100644 --- a/static/admin/prefixed-image-widget.js +++ b/static/admin/prefixed-image-widget.js @@ -47,6 +47,60 @@ value ? React.createElement("img", { src: value, alt: "", style: { maxWidth: "200px" } }) : null, }; + window.CMS.registerWidget( + PrefixedImageWidget.name, + PrefixedImageWidget.controlComponent, + PrefixedImageWidget.previewComponent + ); +})();(function waitForCMSAndReact() { + if (!(window.CMS && window.React)) { + setTimeout(waitForCMSAndReact, 50); + return; + } + const React = window.React; + + class PrefixedImageControl extends React.Component { + openMediaLibrary = () => { + this.props.mediaLibrary.open({ + allowMultiple: false, + config: { allowMultiple: false }, + controlID: this.props.forID, + value: this.props.value, + field: this.props.field, + onInsert: (files) => { + if (!files || files.length === 0) return; + const file = files[0]; + const prefix = "feature-"; + // If already prefixed, don't double-prefix + const fileName = file.name.startsWith(prefix) ? file.name : prefix + file.name; + const newFile = new File([file], fileName, { type: file.type }); + this.props.mediaLibrary.persistMedia(newFile).then((uploaded) => { + this.props.onChange(uploaded.public_path || uploaded.url); + }); + }, + }); + }; + + render() { + const value = this.props.value || ""; + return ( + React.createElement("div", null, + React.createElement("button", { type: "button", onClick: this.openMediaLibrary }, + value ? "Change Image" : "Add Image" + ), + value && React.createElement("img", { src: value, alt: "", style: { maxWidth: "200px", display: "block", marginTop: "1em" } }) + ) + ); + } + } + + const PrefixedImageWidget = { + name: "prefixed-image", + controlComponent: PrefixedImageControl, + previewComponent: ({ value }) => + value ? React.createElement("img", { src: value, alt: "", style: { maxWidth: "200px" } }) : null, + }; + window.CMS.registerWidget( PrefixedImageWidget.name, PrefixedImageWidget.controlComponent,