Skip to content

Commit

Permalink
Separate block wrapping; don't apply multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
tiberiuichim committed Jul 29, 2021
1 parent f6737ef commit 584e7a9
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,40 @@ import SimpleColorPicker from './Widgets/SimpleColorPicker';

import './styles.less';

/**
* Given a block's config object, it wrapps the view and edit in style wrappers
*/
export const applyStyleWrapperToBlock = (blockConfig) => {
const BaseEditComponent = blockConfig.edit;
let EditComponent = BaseEditComponent;
if (!EditComponent._styleWrapped) {
EditComponent = (props) => (
<BlockStyleWrapperEdit {...props}>
<BaseEditComponent {...props} />
</BlockStyleWrapperEdit>
);
EditComponent.displayName = `<EditBlockWithStyleWrapperFor(${blockConfig.id})>`;
EditComponent._styleWrapped = true;
}

const BaseViewComponent = blockConfig.view;
let ViewComponent = BaseViewComponent;
if (ViewComponent._styleWrapped) {
ViewComponent = (props) => (
<BlockStyleWrapperView {...props}>
<BaseViewComponent {...props} />
</BlockStyleWrapperView>
);
ViewComponent.displayName = `<ViewBlockWithStyleWrapperFor(${blockConfig.id})>`;
ViewComponent._styleWrapped = true;
}
return {
...blockConfig,
view: ViewComponent,
edit: EditComponent,
};
};

const applyConfig = (config) => {
const { settings } = config;
const whitelist = settings.pluggableStylesBlocksWhitelist;
Expand All @@ -23,21 +57,7 @@ const applyConfig = (config) => {
(whitelist ? whitelist.includes(name) : true),
);
okBlocks.forEach((name) => {
const EditComponent = blocksConfig[name].edit;
const ViewComponent = blocksConfig[name].view;
blocksConfig[name].edit = (props) => (
<BlockStyleWrapperEdit {...props}>
<EditComponent {...props} />
</BlockStyleWrapperEdit>
);
blocksConfig[name].edit.displayName = 'EditBlockWithStyleWrapper';

blocksConfig[name].view = (props) => (
<BlockStyleWrapperView {...props}>
<ViewComponent {...props} />
</BlockStyleWrapperView>
);
blocksConfig[name].view.displayName = 'ViewBlockWithStyleWrapper';
blocksConfig[name] = applyStyleWrapperToBlock(blocksConfig[name]);
});

config.widgets.widget.style_select = StyleSelectWidget;
Expand Down

0 comments on commit 584e7a9

Please sign in to comment.