Skip to content

Commit

Permalink
#25835 fixed plugin config modal
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofBrylski committed Dec 3, 2024
1 parent f52c37d commit 7a2778b
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 77 deletions.
11 changes: 11 additions & 0 deletions common/helpers.js
Original file line number Diff line number Diff line change
@@ -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;
},
{},
);
};
4 changes: 4 additions & 0 deletions common/plugin-element-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion plugins/common/valid-fields.js → common/valid-fields.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pluginInfo from '../../plugin-manifest.json';
import pluginInfo from '../plugin-manifest.json';

export const validSourceFields = ['richtext'];

Expand Down
44 changes: 0 additions & 44 deletions plugins/common/plugin-helpers.js

This file was deleted.

5 changes: 3 additions & 2 deletions plugins/field-config/plugin-form/index.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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'];

Expand Down
9 changes: 6 additions & 3 deletions plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
(() => {
Expand Down Expand Up @@ -39,10 +40,12 @@ const appendStyles = () => {
}
};

registerFn(pluginInfo, (handler) => {
registerFn(pluginInfo, (handler, _, { getPluginSettings, getLanguage }) => {

Check warning on line 43 in plugins/index.js

View workflow job for this annotation

GitHub Actions / build-release

'getLanguage' is defined but never used. Allowed unused args must match /^_/u
setupSurferSeo();
appendStyles();

const pluginConfig = parsePluginSettings(getPluginSettings());

handler.on('flotiq.plugins.manage::form-schema', (data) =>
handleManagePlugin(data),
);
Expand All @@ -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);
});
});
54 changes: 31 additions & 23 deletions plugins/manage/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
import { getCachedElement } from '../../common/plugin-element-cache.js';
import { getValidFields, validFieldsCacheKey } from '../common/valid-fields';
import {
addObjectToCache,
getCachedElement,

Check warning on line 3 in plugins/manage/index.js

View workflow job for this annotation

GitHub Actions / build-release

'getCachedElement' is defined but never used
} 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;
};
2 changes: 1 addition & 1 deletion plugins/manage/settings-schema.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
8 changes: 5 additions & 3 deletions plugins/sidebar/index.js
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -20,5 +20,7 @@ export const createSidebar = () => {
addElementToCache(contentAnalyzerContainer, containerCacheKey);
}

console.log(formik, pluginConfig);

return contentAnalyzerContainer;
};

0 comments on commit 7a2778b

Please sign in to comment.