Skip to content

Commit d0d3810

Browse files
authored
✨ Add view attribute bag support to blocks (#317)
2 parents 6f95562 + b686ebc commit d0d3810

File tree

5 files changed

+81
-23
lines changed

5 files changed

+81
-23
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<script type="text/javascript">
2-
acf.addFilter('acf_blocks_parse_node_attr', (current, node) => node.name.startsWith('x-') ? node : current);
2+
acf.addFilter('acf_blocks_parse_node_attr', (current, node) => (node.name.startsWith('x-') || node.name.startsWith('@')) ? node : current);
33
</script>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
@verbatim
2+
wp.hooks.addFilter(
3+
'blocks.getBlockDefaultClassName',
4+
'acf-composer/block-slug-classname',
5+
(className, blockName) => {
6+
if (! blockName.startsWith('acf/')) {
7+
return className;
8+
}
9+
10+
const list = (className || '').split(/\\s+/);
11+
12+
const classes = list.reduce((acc, current) => {
13+
acc.push(current);
14+
15+
if (current.startsWith('wp-block-acf-')) {
16+
acc.push(
17+
current.replace('wp-block-acf-', 'wp-block-')
18+
);
19+
}
20+
21+
return acc;
22+
}, []);
23+
24+
return classes.join(' ');
25+
}
26+
);
27+
@endverbatim

src/AcfComposer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,16 @@ protected function handleBlocks(): void
201201
}
202202

203203
if (is_admin() || has_block($composer->namespace)) {
204-
method_exists($composer, 'assets') && $composer->assets($composer->block);
204+
method_exists($composer, 'assets') && $composer->assets((array) $composer->block ?? []);
205205
}
206206
}
207207
}
208208
});
209209

210+
add_action('enqueue_block_editor_assets', function () {
211+
wp_add_inline_script('wp-blocks', view('acf-composer::block-editor-filters')->render());
212+
});
213+
210214
add_action('acf_block_render_template', function ($block, $content, $is_preview, $post_id, $wp_block, $context) {
211215
if (! class_exists($composer = $block['render_template'] ?? '')) {
212216
return;

src/Block.php

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Support\Arr;
77
use Illuminate\Support\Collection;
88
use Illuminate\Support\Str;
9+
use Illuminate\View\ComponentAttributeBag;
910
use Log1x\AcfComposer\Concerns\InteractsWithBlade;
1011
use Log1x\AcfComposer\Contracts\Block as BlockContract;
1112
use WP_Block_Supports;
@@ -374,6 +375,30 @@ public function getStyles(): array
374375
return $styles->all();
375376
}
376377

378+
/**
379+
* Retrieve the block supports.
380+
*/
381+
public function getSupports(): array
382+
{
383+
$supports = $this->collect($this->supports)
384+
->mapWithKeys(fn ($value, $key) => [Str::camel($key) => $value])
385+
->merge($this->supports);
386+
387+
$typography = $supports->get('typography', []);
388+
389+
if ($supports->has('alignText')) {
390+
$typography['textAlign'] = $supports->get('alignText');
391+
392+
$supports->forget(['alignText', 'align_text']);
393+
}
394+
395+
if ($typography) {
396+
$supports->put('typography', $typography);
397+
}
398+
399+
return $supports->all();
400+
}
401+
377402
/**
378403
* Retrieve the block support attributes.
379404
*/
@@ -388,28 +413,29 @@ public function getSupportAttributes(): array
388413
];
389414
}
390415

391-
if ($this->align_text) {
392-
$attributes['alignText'] = [
393-
'type' => 'string',
394-
'default' => $this->align_text,
395-
];
396-
}
397-
398416
if ($this->align_content) {
399417
$attributes['alignContent'] = [
400418
'type' => 'string',
401419
'default' => $this->align_content,
402420
];
403421
}
404422

423+
$styles = [];
424+
425+
if ($this->align_text) {
426+
$styles['typography']['textAlign'] = $this->align_text;
427+
}
428+
405429
$spacing = array_filter($this->spacing);
406430

407431
if ($spacing) {
432+
$styles['spacing'] = $spacing;
433+
}
434+
435+
if ($styles) {
408436
$attributes['style'] = [
409437
'type' => 'object',
410-
'default' => [
411-
'spacing' => $spacing,
412-
],
438+
'default' => $styles,
413439
];
414440
}
415441

@@ -448,7 +474,7 @@ public function getClasses(): string
448474
return str_replace(
449475
acf_slugify($this->namespace),
450476
$this->slug,
451-
$supports['class'] ?? "wp-block-{$this->slug}"
477+
$supports['class'] ?? ''
452478
);
453479
}
454480

@@ -579,13 +605,6 @@ public function settings(): Collection
579605
return $this->settings;
580606
}
581607

582-
if ($this->supports) {
583-
$this->supports = $this->collect($this->supports)
584-
->mapWithKeys(fn ($value, $key) => [Str::camel($key) => $value])
585-
->merge($this->supports)
586-
->all();
587-
}
588-
589608
$settings = Collection::make([
590609
'name' => $this->slug,
591610
'title' => $this->getName(),
@@ -600,7 +619,7 @@ public function settings(): Collection
600619
'alignText' => $this->align_text ?? $this->align,
601620
'alignContent' => $this->align_content,
602621
'styles' => $this->getStyles(),
603-
'supports' => $this->supports,
622+
'supports' => $this->getSupports(),
604623
'textdomain' => $this->getTextDomain(),
605624
'acf_block_version' => $this->blockVersion,
606625
'api_version' => 2,
@@ -726,7 +745,15 @@ public function render($block, $content = '', $preview = false, $post_id = 0, $w
726745
$this->style = $this->getStyle();
727746
$this->inlineStyle = $this->getInlineStyle();
728747

729-
return $this->view($this->view, ['block' => $this]);
748+
$attributes = (new ComponentAttributeBag)
749+
->class($this->classes)
750+
->style($this->inlineStyle)
751+
->filter(fn ($value) => filled($value) && $value !== ';');
752+
753+
return $this->view($this->view, [
754+
'block' => $this,
755+
'attributes' => $attributes,
756+
]);
730757
}
731758

732759
/**

src/Console/stubs/views/block.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div class="{{ $block->classes }}" style="{{ $block->inlineStyle }}">
1+
<div {{ $attributes }}>
22
@if ($items)
33
<ul>
44
@foreach ($items as $item)

0 commit comments

Comments
 (0)