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

Create-block package: External Project Template Custom prompts? #47524

Open
eduwass opened this issue Jan 28, 2023 · 6 comments · May be fixed by #67806
Open

Create-block package: External Project Template Custom prompts? #47524

eduwass opened this issue Jan 28, 2023 · 6 comments · May be fixed by #67806
Assignees
Labels
Needs Dev Ready for, and needs developer efforts [Status] In Progress Tracking issues with work in progress [Tool] Create Block /packages/create-block [Type] Enhancement A suggestion for improvement.

Comments

@eduwass
Copy link
Contributor

eduwass commented Jan 28, 2023

What problem does this address?

When creating a new block or plugin using the @create-block package we are limited to a certain number of parameters that we are prompted, it would be awesome if we could add more to the list, based on our custom configuration.

See: https://github.com/WordPress/gutenberg/blob/trunk/packages/create-block/lib/prompts.js

If this is already possible and I'm missing something, please let me know!

What is your proposed solution?

In this file: https://github.com/WordPress/gutenberg/blob/trunk/packages/create-block/lib/templates.js#L220

There is a getDefaultValues and getPrompts, maybe when someone defines params DefaultValues that are not predefined, we still prompt for them so they are later available in mustache when scaffolding the block?

@talldan talldan added [Type] Enhancement A suggestion for improvement. [Tool] Create Block /packages/create-block labels Jan 30, 2023
@gziolo
Copy link
Member

gziolo commented Feb 7, 2023

@eduwass, great thinking. That has crossed my mind a few times to allow defining custom prompts in external templates. I'd be more than happy to see a proof of concept for how that would work best from the perspective of someone willing to offer additional customizations for the tool.

@gziolo gziolo added the Needs Dev Ready for, and needs developer efforts label Feb 7, 2023
@eduwass
Copy link
Contributor Author

eduwass commented Feb 7, 2023

Thanks @gziolo appreciate it, if I have some free time from work to put aside into this I'll look into it.

@ryanwelcher
Copy link
Contributor

+1 to this idea. These additional prompts could be defined in the variants for the template being used. @eduwass do you have an example of a field that you'd want to add?

@eduwass
Copy link
Contributor Author

eduwass commented Dec 10, 2024

Hi @ryanwelcher

So I opened this a long time ago, but since you pinged me here and I had some time I've played around with the idea today in a POC.

You can see the fork here.

Quick Demo

I think this contains a couple of examples of how I'd envision this working:

000445.mp4
Heres the code for: my-template/index.js

/**
 * Dependencies
 */
const { join } = require("path");

module.exports = {
    defaultValues: {
        title: "My Template",
        description: "My Template Description",
        icon: "admin-post",
        // Default values for custom prompts
        analyticsId: 'UA-11111-2',
        colorScheme: 'default',
        enableAdvancedFeatures: false,
        customCssPrefix: 'default-prefix-',
    },
    customPrompts: {
        analyticsId: {
            type: 'input',
            name: 'analyticsId',
            message: 'Enter your Google Analytics ID (optional, format: UA-XXXXX-Y):',
            validate(input) {
                if (!input) return true; // Optional
                if (!/^UA-\d+-\d+$/.test(input)) {
                    return 'Invalid Google Analytics ID format. Should be like UA-XXXXX-Y';
                }
                return true;
            }
        },
        colorScheme: {
            type: 'list',
            name: 'colorScheme',
            message: 'Choose a color scheme for your block:',
            choices: [
                { name: 'Default Theme Colors', value: 'default' },
                { name: 'Custom Palette', value: 'custom' },
                { name: 'Monochrome', value: 'mono' }
            ],
            default: 'default'
        },
        enableAdvancedFeatures: {
            type: 'confirm',
            name: 'enableAdvancedFeatures',
            message: 'Would you like to enable advanced block features?',
            default: false
        },
        customCssPrefix: {
            type: 'input',
            name: 'customCssPrefix',
            message: 'Enter a custom CSS prefix for your block (optional):',
            validate(input) {
                if (!input) return true; // Optional
                if (!/^[a-z][a-z0-9-]*$/.test(input)) {
                    return 'CSS prefix must start with a letter and contain only lowercase letters, numbers, and hyphens';
                }
                return true;
            },
            filter(input) {
                return input.toLowerCase();
            }
        }
    },
    // Make sure template files are found
    pluginTemplatesPath: join(__dirname, 'files', 'plugin'),
    blockTemplatesPath: join(__dirname, 'files', 'block'),
};

So far...

  • Successfully got the prompts working and displaying in the CLI interface
  • Values populating into the .mustache template files

This is still barebones but just tried to get a working proof-of-concept as a starting point.

Open Questions/Challenges

Several areas still need investigation and testing:

  1. blockAnswers vs pluginAnswers handling
  2. How variants should be properly handled
  3. Interactive mode behavior when no slug is provided

What do you think? Any thoughts on the above points?

Thanks!

@ryanwelcher
Copy link
Contributor

This is great start! I'd open a PR based on your fork and we can go from there.

@eduwass
Copy link
Contributor Author

eduwass commented Dec 10, 2024

@ryanwelcher OK great, I just created the initial PR here: #67806

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Dev Ready for, and needs developer efforts [Status] In Progress Tracking issues with work in progress [Tool] Create Block /packages/create-block [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants