strapi-plugin-generic-custom-fields
Version:
This plugin allows you to easily add custom fields to Strapi.
165 lines (164 loc) • 5.57 kB
JavaScript
;
const react = require("react");
const jsxRuntime = require("react/jsx-runtime");
const StrapiIcons = require("@strapi/icons");
const admin = require("@strapi/strapi/admin");
const slugify = require("slugify");
const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
function _interopNamespace(e) {
if (e && e.__esModule) return e;
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
if (e) {
for (const k in e) {
if (k !== "default") {
const d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: () => e[k]
});
}
}
}
n.default = e;
return Object.freeze(n);
}
const StrapiIcons__namespace = /* @__PURE__ */ _interopNamespace(StrapiIcons);
const slugify__default = /* @__PURE__ */ _interopDefault(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 = react.useRef(setPlugin);
react.useEffect(() => {
ref.current(PLUGIN_ID);
}, []);
return null;
};
const PluginIcon = ({ icon }) => {
let Comp = StrapiIcons__namespace[icon];
if (!Comp) {
Comp = StrapiIcons__namespace["PuzzlePiece"];
}
return /* @__PURE__ */ jsxRuntime.jsx(Comp, {});
};
const getTranslation = (id) => `${PLUGIN_ID}.${id}`;
const index = {
async register(app) {
const { get } = admin.getFetchClient();
const customFields = await get(`/${PLUGIN_ID}/config/custom-fields`).then(({ data }) => data);
for (const customField of customFields) {
const customFieldName = slugify__default.default(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
Promise.resolve().then(() => require("./Input-C-YrBkV6.js")).then((module2) => ({
default: module2.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": () => Promise.resolve().then(() => require("./en-B4KWt_jN.js")) }), `./translations/${locale}.json`, 3);
return { data, locale };
} catch {
return { data: {}, locale };
}
})
);
}
};
exports.PLUGIN_ID = PLUGIN_ID;
exports.index = index;