strapi-plugin-generic-custom-fields
Version:
This plugin allows you to easily add custom fields to Strapi.
146 lines (145 loc) • 4.76 kB
JavaScript
import { useRef, useEffect } from "react";
import { jsx } from "react/jsx-runtime";
import * as StrapiIcons from "@strapi/icons";
import { getFetchClient } from "@strapi/strapi/admin";
import slugify from "slugify";
const __variableDynamicImportRuntimeHelper = (glob, path, segs) => {
const v = glob[path];
if (v) {
return typeof v === "function" ? v() : Promise.resolve(v);
}
return new Promise((_, reject) => {
(typeof queueMicrotask === "function" ? queueMicrotask : setTimeout)(
reject.bind(
null,
new Error(
"Unknown variable dynamic import: " + path + (path.split("/").length !== segs ? ". Note that variables only represent file names one level deep." : "")
)
)
);
});
};
const strapi = {
name: "generic-custom-fields"
};
const packageJson = {
strapi
};
const PLUGIN_ID = packageJson.strapi.name;
const Initializer = ({ setPlugin }) => {
const ref = useRef(setPlugin);
useEffect(() => {
ref.current(PLUGIN_ID);
}, []);
return null;
};
const PluginIcon = ({ icon }) => {
let Comp = StrapiIcons[icon];
if (!Comp) {
Comp = StrapiIcons["PuzzlePiece"];
}
return /* @__PURE__ */ jsx(Comp, {});
};
const getTranslation = (id) => `${PLUGIN_ID}.${id}`;
const index = {
async register(app) {
const { get } = getFetchClient();
const customFields = await get(`/${PLUGIN_ID}/config/custom-fields`).then(({ data }) => data);
for (const customField of customFields) {
const customFieldName = slugify(customField.name, { lower: true });
app.customFields.register({
name: customFieldName,
pluginId: PLUGIN_ID,
type: "string",
intlLabel: {
id: `${PLUGIN_ID}.${customFieldName}.label`,
defaultMessage: customField.name
},
intlDescription: {
id: `${PLUGIN_ID}.${customFieldName}.description`,
defaultMessage: customField.description || customField.name
},
icon: () => PluginIcon({
icon: customField.icon || "PuzzlePiece"
}),
components: {
Input: () => (
// @ts-expect-error module is a React component
import("./Input-DVWHXhB5.mjs").then((module) => ({
default: module.Input
}))
)
},
options: {
advanced: [
{
sectionTitle: {
id: "global.settings",
defaultMessage: "Settings"
},
items: [
{
name: "required",
type: "checkbox",
intlLabel: {
id: getTranslation("options.advanced.requiredField"),
defaultMessage: "Required field"
},
description: {
id: getTranslation("options.advanced.requiredField.description"),
defaultMessage: "You won't be able to create an entry if this field is empty"
}
},
{
name: "unique",
type: "checkbox",
intlLabel: {
id: getTranslation("options.advanced.uniqueField"),
defaultMessage: "Unique field"
},
description: {
id: getTranslation("options.advanced.uniqueField.description"),
defaultMessage: "You won't be able to create an entry if there is an existing entry with identical content"
}
},
{
name: "private",
type: "checkbox",
intlLabel: {
id: getTranslation("options.advanced.privateField"),
defaultMessage: "Private field"
},
description: {
id: getTranslation("options.advanced.privateField.description"),
defaultMessage: "This field will not show up in the API response"
}
}
]
}
]
}
});
}
app.registerPlugin({
id: PLUGIN_ID,
initializer: Initializer,
name: PLUGIN_ID
});
},
registerTrads({ locales }) {
return Promise.all(
locales.map(async (locale) => {
try {
const { default: data } = await __variableDynamicImportRuntimeHelper(/* @__PURE__ */ Object.assign({ "./translations/en.json": () => import("./en-Byx4XI2L.mjs") }), `./translations/${locale}.json`, 3);
return { data, locale };
} catch {
return { data: {}, locale };
}
})
);
}
};
export {
PLUGIN_ID as P,
index as i
};