whitecube / laravel-preset

Custom Laravel Preset for the Whitecube workflow
3 stars 1 forks source link

Publishable components #14

Closed toonvandenbos closed 9 months ago

toonvandenbos commented 9 months ago

This PR adds the ability to publish pre-coded component files to a Laravel project.

In order to create a publishable component, one should simply create a new "Publisher" class inside src/Components/Publishers and implement Whitecube\LaravelPreset\Components\PublisherInterface :

namespace Whitecube\LaravelPreset\Components\Publishers;

use Whitecube\LaravelPreset\Components\File;
use Whitecube\LaravelPreset\Components\FilesCollection;
use Whitecube\LaravelPreset\Components\PublisherInterface;

class Wysiwyg implements PublisherInterface
{
    /**
     * Get the component's displayable name.
     */
    public function label(): string
    {
        return 'WYSIWYG section';
    }

    /**
     * Let the publisher prompt for eventual extra input
     * and return a collection of publishable files.
     */
    public function handle(): FilesCollection
    {
        $style = File::makeFromStub(
            stub: 'components/wysiwyg/part.scss',
            destination: resource_path('sass/parts/_wysiwyg.scss'),
        );

        $view = File::makeFromStub(
            stub: 'components/wysiwyg/view.blade.php',
            destination: resource_path('views/components/wysiwyg.blade.php'),
        );

        return FilesCollection::make([$style, $view]);
    }

    /**
     * Get the component's usage instructions
     */
    public function instructions(): ?string
    {
        return "1. Add `@import 'parts/wysiwyg';` to `resources/sass/app.scss`\r\n2. Use the blade component: `<x-wysiwyg><p>Some content</p></x-wysiwyg>`";
    }
}

Most of the heavy-lifting will be achieved inside the publisher's handle() method. For instance, it's a great place to prompt for additional component-specific information and configure the publishable files accordingly.

The handle() method's main purpose is to collect and return the publishable files, that's why this PR provides a File class with several useful methods and features. First, you can choose to create a File instance using one of these methods:

Most of the time, File::makeFromStub should be used in order to keep a clear commit history on the component's original files somewhere in this package's components/[your-component] directory.

These File instances can be manipulated before publication with a few useful methods:

Of course, Laravel Prompts can be used anywhere inside a publisher's handle() method, which is useful for file configuration:

use function Laravel\Prompts\text;

$style = File::makeFromStub(
    stub: 'components/wysiwyg/part.scss',
    destination: resource_path('sass/parts/_wysiwyg.scss'),
);

$width = text(
    label: 'How many columns should the WYSIWYG\'s container width be?',
    default: 10,
    hint: 'Based on a 12 columns grid',
);

$style->replaceVariableValue('wysiwyg_width_columns', $width);

More features could be added to the File classes, but I think this is already a good start for the few upcoming publishable components.

voidgraphics commented 9 months ago

Je crois que j'ai un bug, il ne publie rien :

CleanShot 2023-12-06 at 13 55 19

toonvandenbos commented 9 months ago

@voidgraphics il n'est pas configuré le TxtImg (le handle() est vide, regarde dans src/Components/Publishers/TxtImg.php), tu peux tester avec le WYSIWYG.

voidgraphics commented 9 months ago

Je comprends pas ce qu'on demande ici 😅

CleanShot 2023-12-06 at 13 59 07

Edit : J'ai compris, c'est juste un exemple pour montrer qu'on peut remplacer des tags dans le code