Skip to content

Commit

Permalink
Fix and improve the addStyling helper
Browse files Browse the repository at this point in the history
  • Loading branch information
sneridagh committed Jun 15, 2023
1 parent 47a4414 commit 8df574a
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/helpers/Extensions/withBlockSchemaEnhancer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { defineMessages } from 'react-intl';
import { useIntl } from 'react-intl';
import { find, isEmpty } from 'lodash';
import config from '@plone/volto/registry';
import { cloneDeepSchema } from '@plone/volto/helpers/Utils/Utils';

Expand Down Expand Up @@ -291,20 +292,23 @@ export const EMPTY_STYLES_SCHEMA = {
};

/**
* Creates the `styles` field and fieldset in a schema
* Adds the `styles` field and 'styling' fieldset in a given schema
*/
export const addStyling = ({ schema, formData, intl }) => {
schema.fieldsets.push({
id: 'styling',
title: intl.formatMessage(messages.styling),
fields: ['styles'],
});
if (isEmpty(find(schema.fieldsets, { id: 'styling' }))) {
schema.fieldsets.push({
id: 'styling',
title: intl.formatMessage(messages.styling),
fields: ['styles'],
});

schema.properties.styles = {
widget: 'object',
title: intl.formatMessage(messages.styling),
schema: cloneDeepSchema(EMPTY_STYLES_SCHEMA),
};
}

schema.properties.styles = {
widget: 'object',
title: intl.formatMessage(messages.styling),
schema: EMPTY_STYLES_SCHEMA,
};
return schema;
};

Expand Down
145 changes: 145 additions & 0 deletions src/helpers/Extensions/withBlockSchemaEnhancer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
addExtensionFieldToSchema,
applySchemaEnhancer,
composeSchema,
addStyling,
} from './withBlockSchemaEnhancer';

import config from '@plone/volto/registry';
Expand Down Expand Up @@ -246,3 +247,147 @@ describe('composeSchema', () => {
expect(res).toStrictEqual([6, 9]);
});
});

describe('addStyling', () => {
it('returns an enhanced schema with the styling wrapper object on it', () => {
const intl = { formatMessage: () => 'Styling' };

const schema = {
fieldsets: [
{
id: 'default',
title: 'Default',
fields: [],
},
],
properties: {},
required: [],
};

const result = addStyling({ schema, intl });

expect(result).toStrictEqual({
fieldsets: [
{ id: 'default', title: 'Default', fields: [] },
{ id: 'styling', title: 'Styling', fields: ['styles'] },
],
properties: {
styles: {
widget: 'object',
title: 'Styling',
schema: {
fieldsets: [
{
fields: [],
id: 'default',
title: 'Default',
},
],
properties: {},
required: [],
},
},
},
required: [],
});
});

it('multiple schema enhancers', () => {
const intl = { formatMessage: () => 'Styling' };

const schema1 = {
fieldsets: [
{
id: 'default',
title: 'Default',
fields: [],
},
],
properties: {},
required: [],
};

const schema2 = {
fieldsets: [
{
id: 'default',
title: 'Default',
fields: [],
},
],
properties: {},
required: [],
};

const result = addStyling({ schema: schema1, intl });

// We add some fields to the styling schema
result.properties.styles.schema.properties.align = {
widget: 'align',
title: 'align',
actions: ['left', 'right', 'center'],
default: 'left',
};

result.properties.styles.schema.fieldsets[0].fields = ['align'];

const result2 = addStyling({ schema: schema2, intl });

expect(result).toStrictEqual({
fieldsets: [
{ id: 'default', title: 'Default', fields: [] },
{ id: 'styling', title: 'Styling', fields: ['styles'] },
],
properties: {
styles: {
widget: 'object',
title: 'Styling',
schema: {
fieldsets: [
{
fields: ['align'],
id: 'default',
title: 'Default',
},
],
properties: {
align: {
widget: 'align',
title: 'align',
actions: ['left', 'right', 'center'],
default: 'left',
},
},
required: [],
},
},
},
required: [],
});

expect(result2).toStrictEqual({
fieldsets: [
{ id: 'default', title: 'Default', fields: [] },
{ id: 'styling', title: 'Styling', fields: ['styles'] },
],
properties: {
styles: {
widget: 'object',
title: 'Styling',
schema: {
fieldsets: [
{
fields: [],
id: 'default',
title: 'Default',
},
],
properties: {},
required: [],
},
},
},
required: [],
});
});
});

0 comments on commit 8df574a

Please sign in to comment.