UNPKG

@sklinet/strapi-plugin-tinymce

Version:

Strapi custom field with a customized build of TinyMCE richtext editor.

202 lines (201 loc) 4.55 kB
"use strict"; const bootstrap = async ({ strapi: strapi2 }) => { const actions = [ { section: "plugins", displayName: "Access the plugin settings", uid: "settings.read", pluginName: "tinymce" }, { section: "plugins", displayName: "Menu link to plugin settings", uid: "menu-link", pluginName: "tinymce" } ]; await strapi2.admin.services.permission.actionProvider.registerMany(actions); }; const destroy = ({ strapi: strapi2 }) => { }; const name = "@sklinet/strapi-plugin-tinymce"; const pluginPkg = { name }; const PLUGIN_ID = pluginPkg.name.replace(/^(@sklinet\/strapi-)plugin-/i, ""); const register = ({ strapi: strapi2 }) => { strapi2.customFields.register({ name: "tinymce", plugin: PLUGIN_ID, type: "richtext" }); }; const config$3 = { default: {}, validator() { } }; const contentTypes = {}; const admin$1 = { async uploadImage(ctx) { const file = Array.isArray(ctx.request.files.files) ? ctx.request.files.files[0] : ctx.request.files.files; if (!file) { return ctx.badRequest("No file uploaded"); } try { const [createdFile] = await strapi.plugins.upload.services.upload.upload({ data: { fileInfo: { name: file.originalFilename, caption: "", alternativeText: "" } }, files: file }); ctx.body = { location: createdFile.url }; } catch (error) { ctx.internalServerError(`Image upload failed ${error?.message || error?.toString()}`); } } }; const config$2 = { getConfig: async (ctx) => { const { configKey } = ctx.params; const config2 = await strapi.plugin("tinymce").service("config").getConfig(configKey); ctx.send(config2); } }; const settings$2 = { getSettings: async (ctx) => { try { ctx.body = await strapi.plugin("tinymce").service("settings").getSettings(); } catch (err) { ctx.body = err; ctx.throw(500, err); } }, setSettings: async (ctx) => { const { body } = ctx.request; try { await strapi.plugin("tinymce").service("settings").setSettings(body); ctx.body = await strapi.plugin("tinymce").service("settings").getSettings(); } catch (err) { ctx.throw(500, err); } } }; const controllers = { admin: admin$1, config: config$2, settings: settings$2 }; const middlewares = {}; const policies = {}; const admin = { routes: [ { method: "POST", path: "/uploadImage", handler: "admin.uploadImage", // Enforce admin authentication config: { auth: { scope: ["admin::isAuthenticatedAdmin"] } } } ] }; const config$1 = { type: "admin", routes: [ { method: "GET", path: "/config/:configKey", handler: "config.getConfig", config: { policies: [] } } ] }; const settings$1 = { type: "admin", routes: [ { method: "GET", path: "/settings", handler: "settings.getSettings", config: { policies: [], auth: false } }, { method: "POST", path: "/settings", handler: "settings.setSettings", config: { policies: [], auth: false } } ] }; const routes = { admin, config: config$1, settings: settings$1 }; const config = ({ strapi: strapi2 }) => { return { getConfig(key = "editor") { return strapi2.plugin("tinymce").config(key) ?? {}; } }; }; function getPluginStore() { return strapi.store({ environment: "", type: "plugin", name: "tinymce" }); } async function createDefaultConfig() { const pluginStore = getPluginStore(); const value = { apiKey: "" }; await pluginStore.set({ key: "settings", value }); return pluginStore.get({ key: "settings" }); } const settings = () => { return { async getSettings() { const pluginStore = getPluginStore(); let config2 = await pluginStore.get({ key: "settings" }); if (!config2) { config2 = await createDefaultConfig(); } return config2; }, async setSettings(settings2) { const value = settings2; const pluginStore = getPluginStore(); await pluginStore.set({ key: "settings", value }); return pluginStore.get({ key: "settings" }); } }; }; const services = { config, settings }; const index = { register, bootstrap, destroy, config: config$3, controllers, routes, services, contentTypes, policies, middlewares }; module.exports = index;