Skip to content

Commit 29a1c2a

Browse files
authored
✨ Add native support for block spacing (#311)
1 parent b3c2cfd commit 29a1c2a

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

src/Block.php

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Log1x\AcfComposer;
44

5+
use Exception;
56
use Illuminate\Support\Arr;
67
use Illuminate\Support\Collection;
78
use Illuminate\Support\Str;
@@ -195,6 +196,16 @@ abstract class Block extends Composer implements BlockContract
195196
*/
196197
public $align_content = '';
197198

199+
/**
200+
* The default block spacing.
201+
*
202+
* @var array
203+
*/
204+
public $spacing = [
205+
'padding' => null,
206+
'margin' => null,
207+
];
208+
198209
/**
199210
* The supported block features.
200211
*
@@ -210,7 +221,7 @@ abstract class Block extends Composer implements BlockContract
210221
public $styles = [];
211222

212223
/**
213-
* The block active style.
224+
* The current block style.
214225
*
215226
* @var string
216227
*/
@@ -391,6 +402,17 @@ public function getSupportAttributes(): array
391402
];
392403
}
393404

405+
$spacing = array_filter($this->spacing);
406+
407+
if ($spacing) {
408+
$attributes['style'] = [
409+
'type' => 'object',
410+
'default' => [
411+
'spacing' => $spacing,
412+
],
413+
];
414+
}
415+
394416
return $attributes;
395417
}
396418

@@ -543,12 +565,41 @@ public function compose(): ?self
543565

544566
$this->register(fn () => $this->hasJson()
545567
? register_block_type($this->jsonPath())
546-
: acf_register_block_type($this->settings()->all())
568+
: $this->registerBlockType()
547569
);
548570

549571
return $this;
550572
}
551573

574+
/**
575+
* Register the block type.
576+
*/
577+
public function registerBlockType(): void
578+
{
579+
$block = acf_validate_block_type($this->settings()->all());
580+
$block = apply_filters('acf/register_block_type_args', $block);
581+
582+
if (acf_has_block_type($block['name'])) {
583+
throw new Exception("Block type [{$block['name']}] is already registered.");
584+
}
585+
586+
$block['attributes'] = array_merge(
587+
acf_get_block_type_default_attributes($block),
588+
$block['attributes'] ?? []
589+
);
590+
591+
acf_get_store('block-types')->set($block['name'], $block);
592+
593+
$block['render_callback'] = 'acf_render_block_callback';
594+
595+
register_block_type(
596+
$block['name'],
597+
$block
598+
);
599+
600+
add_action('enqueue_block_editor_assets', 'acf_enqueue_block_assets');
601+
}
602+
552603
/**
553604
* Retrieve the block settings.
554605
*/
@@ -575,6 +626,7 @@ public function settings(): Collection
575626
'post_types' => $this->post_types,
576627
'mode' => $this->mode,
577628
'align' => $this->align,
629+
'attributes' => $this->getSupportAttributes(),
578630
'alignText' => $this->align_text ?? $this->align,
579631
'alignContent' => $this->align_content,
580632
'styles' => $this->getStyles(),
@@ -636,7 +688,6 @@ public function toJson(): string
636688
{
637689
$settings = $this->settings()
638690
->put('name', $this->namespace)
639-
->put('attributes', $this->getSupportAttributes())
640691
->put('acf', [
641692
'blockVersion' => $this->blockVersion,
642693
'mode' => $this->mode,

src/Console/BlockMakeCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class BlockMakeCommand extends MakeCommand
5353
'multiple',
5454
'jsx',
5555
'color' => ['background', 'text', 'gradients'],
56+
'spacing' => ['padding', 'margin'],
5657
];
5758

5859
/**

src/Console/stubs/block.stub

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ class DummyClass extends Block
9191
*/
9292
public $align_content = '';
9393

94+
/**
95+
* The default block spacing.
96+
*
97+
* @var array
98+
*/
99+
public $spacing = [
100+
'padding' => null,
101+
'margin' => null,
102+
];
103+
94104
/**
95105
* The supported block features.
96106
*

0 commit comments

Comments
 (0)