Issue #3704Opened August 13, 2021by throne19864 reactions

BUG: load blocks closed by default?

Question

I am using the following bit of code to collapse all the block categories, unfortunately, this is not working

const categories = editor.BlockManager.getCategories();
categories.each(category => {
	category.set('open', false).on('change:open', opened => {
		opened.get('open') && categories.each(category => {
            category !== opened && category.set('open', false)
        })
	})
})

live demo : https://jsfiddle.net/d4702yoh/2/

What is wrong here?

Answers (3)

artfAugust 14, 20214 reactions

That should be enough editor.Blocks.getCategories().forEach(ct => ct.set('open', false)) but categories are not created until the render of blocks (which happens, by default, when you click the Blocks tab) so you just need to execute that script once are rendered.

ronaldohochAugust 13, 20210 reactions

Hello, i have made it to close all blocks in one plugin, try to adapt to you.:

export default (editor, opt = {}) => {
    const Commands = editor.Commands;

    Commands.add("dina:hide-all-blocks",(editor)=>{
        editor.Panels.getButton("views","open-blocks").set("active",1);
        let categories = editor.BlockManager.getCategories().models;
        categories.map(elem=>{
            elem.view.$el.css("display","none");
        });
    })
};
throne1986August 14, 20210 reactions
const Commands = editor.Commands;

    Commands.add("dina:hide-all-blocks",(editor)=>{
        editor.Panels.getButton("views","open-blocks").set("active",1);
        let categories = editor.BlockManager.getCategories().models;
        categories.map(elem=>{
            elem.view.$el.css("display","none");
        });
    })

still the same here jsfidle : https://jsfiddle.net/46mfqeht/

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.