Ability to filter blocks
What do you think about the ability to filter the blocks panel? I have a situation where I may have hundreds of custom blocks, and having a built-in way for users to find a specific one would be very helpful. Here's a quick mockup of what I've got in mind: https://i.imgur.com/fGCNY3N.png. Typing in the input would only show the blocks that match the search term, maybe with an "X" button in the input that clears the search term and returns to the full list of blocks.
Is this something that you would be open to adding to the grapesjs core? Or would it be better to add this type of feature through a plugin? I'd be happy to submit a pull request for this if you'd like.
Thanks!
Thanks for the suggestion @ryandeba it'd be cool to have search functionality on all collections, probably even something like this, this will allow filtering also in other modules. I'd rather prefer to keep the UI side away from the core library
Any news regarding the above issue? Such functionality would be very helpful.
@giannis4119 I'm not actively working on this at this time, but I may get back to it at some point in the future.
Hi @artf @ryandeba , can you provide some guide on how I can achieve this ?
Workaround
Make a service and pass blockmanger object in it.
export class BlocksFilter {
filterQuery: string = "";
init = bm => {
const container = bm.getContainer();
const blocksParent = container.querySelector(".gjs-blocks-c");
const filterContainer = document.createElement("div");
filterContainer.style.width = "100%";
filterContainer.style.padding = "5px";
filterContainer.innerHTML = this.getFilterContent();
const input = filterContainer.querySelector("input");
input.addEventListener("input", e => {
this.filterQuery = e.target["value"];
this.updateBlocks(bm);
})
blocksParent.insertBefore(filterContainer, blocksParent.firstChild);
this.updateBlocks(bm);
input.value = this.filterQuery;
}
updateBlocks = (blockManager): void => {
const container = blockManager.getContainer();
const blockViews = container.querySelectorAll(".gjs-block");
for (let i = 0; i < blockViews.length; i++) {
if (new RegExp(this.filterQuery.trim(), 'gi').test(blockViews[i].getAttribute("title"))) {
blockViews[i].style.display = "block";
} else {
blockViews[i].style.display = "none";
}
}
}
getFilterContent = (): string => `
<div class="form-group has-search" style="
position: absolute;
color: #aaa;
z-index: 2;
display: block;
width: 1.375rem;
height: 1.375rem;
line-height: 2.375rem;
text-align: center;
pointer-events: none;
">
<span class="fa fa-search form-control-feedback"></span>
</div>
<input type="text" class="form-control" placeholder="Search" style="padding-left:22px">
`
}
@hiteshgarg1994 I am not able to achieve this Could you please guide me more. Thanks