reworked custom widget
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 39s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 39s
This commit is contained in:
@@ -1,28 +1,35 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
|
|
||||||
class PrefixedImageControl extends React.Component {
|
class PrefixedImageControl extends React.Component {
|
||||||
handleChange = async (event) => {
|
openMediaLibrary = () => {
|
||||||
const file = event.target.files[0];
|
this.props.mediaLibrary.open({
|
||||||
if (!file) return;
|
allowMultiple: false,
|
||||||
|
config: { allowMultiple: false },
|
||||||
// Add your prefix here
|
controlID: this.props.forID,
|
||||||
const prefix = "feature-";
|
value: this.props.value,
|
||||||
const newFile = new File([file], prefix + file.name, { type: file.type });
|
field: this.props.field,
|
||||||
|
onInsert: (files) => {
|
||||||
// Use Decap's media library to persist the file
|
if (!files || files.length === 0) return;
|
||||||
const { onChange, mediaPaths, field } = this.props;
|
const file = files[0];
|
||||||
const uploaded = await this.props.mediaLibrary.persistMedia(newFile);
|
const prefix = "feature-";
|
||||||
|
// If already prefixed, don't double-prefix
|
||||||
// Save the public path
|
const fileName = file.name.startsWith(prefix) ? file.name : prefix + file.name;
|
||||||
onChange(uploaded.public_path || uploaded.url);
|
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() {
|
render() {
|
||||||
const value = this.props.value || "";
|
const value = this.props.value || "";
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<input type="file" accept="image/*" onChange={this.handleChange} />
|
<button type="button" onClick={this.openMediaLibrary}>
|
||||||
{value && <img src={value} alt="" style={{ maxWidth: "200px" }} />}
|
{value ? "Change Image" : "Add Image"}
|
||||||
|
</button>
|
||||||
|
{value && <img src={value} alt="" style={{ maxWidth: "200px", display: "block", marginTop: "1em" }} />}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -35,4 +42,8 @@ const PrefixedImageWidget = {
|
|||||||
value ? <img src={value} alt="" style={{ maxWidth: "200px" }} /> : null,
|
value ? <img src={value} alt="" style={{ maxWidth: "200px" }} /> : null,
|
||||||
};
|
};
|
||||||
|
|
||||||
window.CMS.registerWidget(PrefixedImageWidget.name, PrefixedImageWidget.controlComponent, PrefixedImageWidget.previewComponent);
|
window.CMS.registerWidget(
|
||||||
|
PrefixedImageWidget.name,
|
||||||
|
PrefixedImageWidget.controlComponent,
|
||||||
|
PrefixedImageWidget.previewComponent
|
||||||
|
);
|
||||||
Reference in New Issue
Block a user