Skip to content

Commit

Permalink
Merge pull request #171 from blockeraai/bug/block-variation-and-displ…
Browse files Browse the repository at this point in the history
…ay-sync 🏁

Bug: Group block variation control bug with updating `Layout Section → Flex Layout` control
  • Loading branch information
rezaelahidev authored Sep 14, 2024
2 parents 23aec1a + 974e24b commit 6908fc8
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 55 deletions.
8 changes: 8 additions & 0 deletions packages/blocks/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
## Unreleased

### Bug Fixes

- Fixed an issue with test assertions.

### Miscellaneous

- Updated the changelog for the blockera/blocks-core package.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ describe('Group Block → Variation Switch Compatibility', () => {
);
});

cy.getParentContainer('Display')
.first()
.within(() => {
cy.getByAriaLabel('Flex')
.invoke('attr', 'aria-pressed')
.should('eq', 'true');
});

cy.getParentContainer('Flex Layout')
.first()
.within(() => {
cy.getByAriaLabel('Row')
.invoke('attr', 'aria-checked')
.should('eq', 'true');
});

//
// Test 3: Change Variation to `group-stack`
//
Expand All @@ -116,6 +132,22 @@ describe('Group Block → Variation Switch Compatibility', () => {
);
});

cy.getParentContainer('Display')
.first()
.within(() => {
cy.getByAriaLabel('Flex')
.invoke('attr', 'aria-pressed')
.should('eq', 'true');
});

cy.getParentContainer('Flex Layout')
.first()
.within(() => {
cy.getByAriaLabel('Column')
.invoke('attr', 'aria-checked')
.should('eq', 'true');
});

//
// Test 4: Change Variation to `group-grid`
//
Expand Down Expand Up @@ -143,6 +175,14 @@ describe('Group Block → Variation Switch Compatibility', () => {
);
});

cy.getParentContainer('Display')
.first()
.within(() => {
cy.getByAriaLabel('Grid')
.invoke('attr', 'aria-pressed')
.should('eq', 'true');
});

//
// Test 5: Change Variation to `group`
//
Expand All @@ -169,6 +209,18 @@ describe('Group Block → Variation Switch Compatibility', () => {
getSelectedBlock(data, 'blockeraFlexLayout')?.alignItems
);
});

cy.getParentContainer('Display')
.first()
.within(() => {
cy.getByAriaLabel('Grid')
.invoke('attr', 'aria-pressed')
.should('eq', 'false');

cy.getByAriaLabel('Flex')
.invoke('attr', 'aria-pressed')
.should('eq', 'false');
});
});

it('Variation Switch (`To WP` Compatibility)', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Strange "0" character after Layout block section [[🔗 Bug](https://community.blockera.ai/bugs-mdhyb8nc/post/strange-0-after-layout-block-section-tXhcZXMGMn631EH)]
- Strange bug of flex layout that makes issue for inner blocks flex layout.
- Wrong style generation for blocks in gap feature [[🔗 Bug](https://community.blockera.ai/bugs-mdhyb8nc/post/wrong-style-generation-for-blocks-in-gap-feature-nkRSbYvjaK226Rh)]
- Refactored BlockBase component to fix compatibility with WordPress problems. [[🔗 Bug](https://community.blockera.ai/bugs-mdhyb8nc/post/group-block-variation-control-bug-with-updating-layout-section---flex-MSd1lhFdxAGDd2c)]

### Improvements

Expand Down
71 changes: 16 additions & 55 deletions packages/editor/js/extensions/components/block-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,37 +110,29 @@ export const BlockBase: ComponentType<BlockBaseProps> = memo(
currentInnerBlockState,
} = useExtensionsStore({ name, clientId });

const {
selectedBlock,
activeVariation,
availableAttributes,
isActiveBlockExtensions,
} = useSelect((select) => {
const { isActiveBlockExtensions, getActiveBlockVariation } = select(
'blockera/extensions'
);

const { getBlockType } = select('core/blocks');
const { getSelectedBlock } = select('core/block-editor');

return {
activeVariation: getActiveBlockVariation(),
selectedBlock: (getSelectedBlock() || {})?.name,
isActiveBlockExtensions: isActiveBlockExtensions(),
availableAttributes: getBlockType(name)?.attributes,
};
});

const isSelectedBlock = (name: string): boolean =>
selectedBlock === name;
const { availableAttributes, isActiveBlockExtensions } = useSelect(
(select) => {
const { isActiveBlockExtensions, getActiveBlockVariation } =
select('blockera/extensions');

const { getBlockType } = select('core/blocks');
const { getSelectedBlock } = select('core/block-editor');

return {
activeVariation: getActiveBlockVariation(),
selectedBlock: (getSelectedBlock() || {})?.name,
isActiveBlockExtensions: isActiveBlockExtensions(),
availableAttributes: getBlockType(name)?.attributes,
};
}
);

const [isActive, setActive] = useState(isActiveBlockExtensions);

const {
changeExtensionCurrentBlock: setCurrentBlock,
changeExtensionCurrentBlockState: setCurrentState,
changeExtensionInnerBlockState: setInnerBlockState,
setExtensionsActiveBlockVariation: setActiveBlockVariation,
} = dispatch('blockera/extensions') || {};

const { getDeviceType } = select('blockera/editor');
Expand Down Expand Up @@ -287,37 +279,6 @@ export const BlockBase: ComponentType<BlockBaseProps> = memo(
[currentAttributes]
);

// While change active block variation, we should clean up blockeraCompatId because we need to running compatibilities again.
useEffect(() => {
// We should not try to clean up blockeraCompatId while not selected block because still not executing compatibility on current block.
if (!isSelectedBlock(name)) {
return;
}

// Create mutable constant to prevent directly change to immutable state constant.
let filteredAttributes = { ...attributes };

if (!isEquals(activeVariation, activeBlockVariation)) {
setActiveBlockVariation(activeBlockVariation);

filteredAttributes = {
...attributes,
blockeraCompatId: '',
};

// Prevent redundant set state!
if (isEquals(attributes, filteredAttributes)) {
return;
}

// Prevent of bad set state!
if (!isEquals(attributes, filteredAttributes)) {
updateAttributes(filteredAttributes);
}
}
// eslint-disable-next-line
}, [activeBlockVariation]);

const originalAttributes = useMemo(() => {
return omitWithPattern(
omit(_attributes, ['content']),
Expand Down

0 comments on commit 6908fc8

Please sign in to comment.