diff --git a/common/helpers.js b/common/helpers.js new file mode 100644 index 0000000..32d3aaf --- /dev/null +++ b/common/helpers.js @@ -0,0 +1,11 @@ +export const parsePluginSettings = (settings) => { + const parsedSettings = JSON.parse(settings || '{}'); + + return (parsedSettings?.surferSeoAnalyzer || []).reduce( + (settings, element) => { + settings[element.content_type] = element; + return settings; + }, + {}, + ); +}; diff --git a/common/plugin-element-cache.js b/common/plugin-element-cache.js index eb43a2c..89fc132 100644 --- a/common/plugin-element-cache.js +++ b/common/plugin-element-cache.js @@ -17,6 +17,10 @@ export const getCachedElement = (key) => { return appRoots[key]; }; +export const addObjectToCache = (key, data = {}) => { + appRoots[key] = data; +}; + export const registerFn = (pluginInfo, callback) => { if (window.FlotiqPlugins?.add) { window.FlotiqPlugins.add(pluginInfo, callback); diff --git a/plugins/common/valid-fields.js b/common/valid-fields.js similarity index 94% rename from plugins/common/valid-fields.js rename to common/valid-fields.js index 54adb65..146edfe 100644 --- a/plugins/common/valid-fields.js +++ b/common/valid-fields.js @@ -1,4 +1,4 @@ -import pluginInfo from '../../plugin-manifest.json'; +import pluginInfo from '../plugin-manifest.json'; export const validSourceFields = ['richtext']; diff --git a/plugins/common/plugin-helpers.js b/plugins/common/plugin-helpers.js deleted file mode 100644 index 46de6f9..0000000 --- a/plugins/common/plugin-helpers.js +++ /dev/null @@ -1,44 +0,0 @@ -const appRoots = {}; - -export const addElementToCache = (element, root, key) => { - appRoots[key] = { - element, - root, - }; - - let detachTimeoutId; - - element.addEventListener('flotiq.attached', () => { - if (detachTimeoutId) { - clearTimeout(detachTimeoutId); - detachTimeoutId = null; - } - }); - - element.addEventListener('flotiq.detached', () => { - detachTimeoutId = setTimeout(() => { - delete appRoots[key]; - }, 50); - }); -}; - -export const getCachedElement = (key) => { - return appRoots[key]; -}; - -export const registerFn = (pluginInfo, callback) => { - if (window.FlotiqPlugins?.add) { - window.FlotiqPlugins.add(pluginInfo, callback); - return; - } - if (!window.initFlotiqPlugins) window.initFlotiqPlugins = []; - window.initFlotiqPlugins.push({ pluginInfo, callback }); -}; - -export const addObjectToCache = (key, data = {}) => { - appRoots[key] = data; -}; - -export const removeRoot = (key) => { - delete appRoots[key]; -}; diff --git a/plugins/field-config/plugin-form/index.js b/plugins/field-config/plugin-form/index.js index 1ad6a1a..66b33c9 100644 --- a/plugins/field-config/plugin-form/index.js +++ b/plugins/field-config/plugin-form/index.js @@ -1,9 +1,9 @@ -import { getCachedElement } from '../../common/plugin-helpers.js'; import { validFieldsCacheKey, validSourceFields, -} from '../../common/valid-fields'; +} from '../../../common/valid-fields.js'; import i18n from '../../../../surferseo-plugin/i18n'; +import { getCachedElement } from '../../../common/plugin-element-cache.js'; const insertSelectOptions = (config, options = [], emptyOptionMessage) => { config.additionalHelpTextClasses = 'break-normal'; @@ -24,6 +24,7 @@ export const handlePluginFormConfig = ({ name, config, formik }) => { if (index == null || !type) return; const ctd = formik.values.surferSeoAnalyzer[index].content_type; const { sourceFields } = getCachedElement(validFieldsCacheKey); + console.log(sourceFields); const keysToClearOnCtdChange = ['source']; diff --git a/plugins/index.js b/plugins/index.js index 0e648c7..66e57f7 100644 --- a/plugins/index.js +++ b/plugins/index.js @@ -4,6 +4,7 @@ import cssString from 'inline:./styles/style.css'; import { handleManagePlugin } from './manage/index.js'; import { handlePluginFormConfig } from './field-config/plugin-form/index.js'; import { createSidebar } from './sidebar/index.js'; +import { parsePluginSettings } from '../common/helpers.js'; const setupSurferSeo = () => { (() => { @@ -39,10 +40,12 @@ const appendStyles = () => { } }; -registerFn(pluginInfo, (handler) => { +registerFn(pluginInfo, (handler, _, { getPluginSettings, getLanguage }) => { setupSurferSeo(); appendStyles(); + const pluginConfig = parsePluginSettings(getPluginSettings()); + handler.on('flotiq.plugins.manage::form-schema', (data) => handleManagePlugin(data), ); @@ -57,7 +60,7 @@ registerFn(pluginInfo, (handler) => { } }); - handler.on('flotiq.form.sidebar-panel::add', () => { - return createSidebar(); + handler.on('flotiq.form.sidebar-panel::add', ({ formik }) => { + return createSidebar(formik, pluginConfig); }); }); diff --git a/plugins/manage/index.js b/plugins/manage/index.js index 31bd044..95d6ebb 100644 --- a/plugins/manage/index.js +++ b/plugins/manage/index.js @@ -1,33 +1,41 @@ -import { getCachedElement } from '../../common/plugin-element-cache.js'; -import { getValidFields, validFieldsCacheKey } from '../common/valid-fields'; +import { + addObjectToCache, + getCachedElement, +} from '../../common/plugin-element-cache.js'; +import { + getValidFields, + validFieldsCacheKey, +} from '../../common/valid-fields.js'; import pluginInfo from '../../plugin-manifest.json'; -import { addObjectToCache, removeRoot } from '../common/plugin-helpers.js'; import { getSchema, getValidator } from './settings-schema.js'; -export const handleManagePlugin = ({ contentTypes, modalInstance }) => { - const formSchemaCacheKey = `${pluginInfo.id}-form-schema`; - let formSchema = getCachedElement(formSchemaCacheKey); +let configCache = null; - if (!formSchema) { - const validFields = getValidFields(contentTypes); - addObjectToCache(validFieldsCacheKey, validFields); +export const handleManagePlugin = ({ plugin, contentTypes, modalInstance }) => { + if (plugin?.id !== pluginInfo.id) return null; - const ctds = contentTypes - ?.filter(({ internal }) => !internal) - ?.map(({ name, label }) => ({ value: name, label })); + if (configCache) return configCache; - const { sourceFieldsKeys } = validFields; + const validFields = getValidFields(contentTypes); + addObjectToCache(validFieldsCacheKey, validFields); - formSchema = { - options: { - disbaledBuildInValidation: true, - onValidate: getValidator(sourceFieldsKeys), - }, - schema: getSchema(ctds), - }; - } + const ctds = contentTypes + ?.filter(({ internal }) => !internal) + ?.map(({ name, label }) => ({ value: name, label })); - modalInstance.promise.then(() => removeRoot(formSchemaCacheKey)); + const { sourceFieldsKeys } = validFields; - return formSchema; + configCache = {}; + + configCache = { + options: { + disbaledBuildInValidation: true, + onValidate: getValidator(sourceFieldsKeys), + }, + schema: getSchema(ctds), + }; + + modalInstance.promise.then(() => (configCache = null)); + + return configCache; }; diff --git a/plugins/manage/settings-schema.js b/plugins/manage/settings-schema.js index 217e043..f714a38 100644 --- a/plugins/manage/settings-schema.js +++ b/plugins/manage/settings-schema.js @@ -1,6 +1,6 @@ import i18n from '../../i18n'; import pluginInfo from '../../plugin-manifest.json'; -import { validSourceFields } from '../common/valid-fields'; +import { validSourceFields } from '../../common/valid-fields.js'; export const getSchema = (contentTypes) => ({ id: pluginInfo.id, diff --git a/plugins/sidebar/index.js b/plugins/sidebar/index.js index 801f10d..e7d7e17 100644 --- a/plugins/sidebar/index.js +++ b/plugins/sidebar/index.js @@ -1,10 +1,10 @@ +import pluginInfo from '../../plugin-manifest.json'; import { addElementToCache, getCachedElement, -} from '../common/plugin-helpers.js'; -import pluginInfo from '../../plugin-manifest.json'; +} from '../../common/plugin-element-cache.js'; -export const createSidebar = () => { +export const createSidebar = (formik, pluginConfig) => { const containerCacheKey = `${pluginInfo.id}-surferseo-content-analyzer-container`; let contentAnalyzerContainer = getCachedElement(containerCacheKey)?.element; @@ -20,5 +20,7 @@ export const createSidebar = () => { addElementToCache(contentAnalyzerContainer, containerCacheKey); } + console.log(formik, pluginConfig); + return contentAnalyzerContainer; };