Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow packages to filter the toolbox #10291

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

riknoll
Copy link
Member

@riknoll riknoll commented Nov 26, 2024

this adds the ability for projects and extensions to filter the toolbox. inspired by https://forum.makecode.com/t/creating-micro-bit-sandbox-restrict-available-blocks/32008

an extension or project can add a "toolboxFilter" entry in pxt.json like so:

{
    "toolboxFilter": {
        "blocks": {
            "device_show_leds": "hidden"
        },
        "namespaces": {
            "pins": "hidden"
        }
    }
}

the possible values for each block or category are "hidden", "visible", and "disabled", though i don't think disabled actually does anything? i'm just matching the values we have available in our current toolbox filtering for tutorials and afaik disabled is just ignored.

toolbox filters are merged starting with the leaf extensions in the dependency tree and moving upwards to the main package. in other words, the top-level project overrides its dependencies, which override their dependencies, etc.

these filters can all be overridden by the filters that exist today. e.g., tutorial blocks filters will always trump project blocks filters.

@riknoll riknoll requested review from kiki-lee, tballmsft and a team November 26, 2024 21:56
@riknoll
Copy link
Member Author

riknoll commented Nov 26, 2024

also, not sure where to document this. maybe here? i'm not sure if anyone reads that page though

@abchatra
Copy link
Collaborator

What does the extension author need to do to filter the toolbox? Can you share a sample extension which does this?

@riknoll
Copy link
Member Author

riknoll commented Nov 26, 2024

@abchatra it's exactly the same as how you do it for a project! simply add the "toolboxFilter" object in the extension's pxt.json.

here's a project that does it:

https://makecode.microbit.org/_JcxUkW3vAcpT

Copy link
Contributor

@srietkerk srietkerk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small nit and a learning question. Looks good!


const deps = mainPkg.sortedDeps();
// sort the dependencies by level and then id. lower level overrides
// higher level
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we have the "lower level overrides higher level" on a line together?

return b.level - a.level;
});

const applyProjectFilter = (projectFilter: pxt.PackageConfig["toolboxFilter"], key: keyof pxt.PackageConfig["toolboxFilter"]) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wanted to ask about this because I don't think I've seen keyof being used before. I looked at the documentation, but just wanted some clarification.

So using keyof here is basically saying, "hey, the type we are expecting has to be the keys defined within this object. If the string (in our case) or type does not exist within this object, we can't accept that parameter". And the benefit of using something like this is that we can use the object itself as a growing type definition if we ever have any fields that we want to add to the toolboxFilter object rather than having to create a separate type definition.

if (filters.blocks[blockId] !== undefined) {
blockFilter = filters.blocks[blockId];
}
else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this change! This is much more readable!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants