GrapesJS / grapesjs

Free and Open source Web Builder Framework. Next generation tool for building templates without coding
https://grapesjs.com
BSD 3-Clause "New" or "Revised" License
20.78k stars 3.9k forks source link

BUG: Filtering Blocks does not work as described in Docs #5803

Closed rhoenerSBS closed 3 months ago

rhoenerSBS commented 3 months ago

GrapesJS version

What browser are you using?

Chrome v123

Reproducible demo link

https://jsfiddle.net/7xu0efLc/3/

Describe the bug

The GrapesJs docs show that it is supposedly possible to filter the blocks and rerender the BlockManager with the filtered list of blocks. (see docs https://grapesjs.com/docs/api/block_manager.html#render) In the fiddle you can see, that it's not working. Only the Text Block should be visible.

How to reproduce the bug?

  1. Open Fiddle or integrate filter code from linked docs in own project.

What is the expected behavior? BlockManager should be able to render with filtered List of blocks.

What is the current behavior? Blocks are not filtered.

Code of Conduct

artf commented 3 months ago

That method is only valid once the default block manager is already rendered. If you need to rerender at the beginning you can subscribe to the event of block manager activation.

editor.on('command:run:open-blocks', () => {
    const blocks = editor.BlockManager.getAll();
    const filtered = blocks.filter(block => block.getLabel() == 'Text');
    editor.BlockManager.render(filtered);
  });
rhoenerSBS commented 3 months ago

Thank you @artf