Skip to content

Commit

Permalink
Options sections support (please update js part too and check readme)
Browse files Browse the repository at this point in the history
  • Loading branch information
yarovikov committed Oct 18, 2024
1 parent 66ed0ae commit 43463a1
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 60 deletions.
121 changes: 67 additions & 54 deletions src/Editor/AbstractBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,45 +69,56 @@ public function getAttributes(): array
$fields_and_options = [
...$this->fields(),
...$this->options(),
...$this->defaultOptions(),
...$this->defaultOptions()['fields'],
];

if (empty($fields_and_options)) {
return [];
}

foreach ($fields_and_options as $field_or_option) {
$attributes[$field_or_option['name']] = match ($field_or_option['type']) {
'Text', 'Textarea', 'Select', 'ColorPalette', 'RichText' => [
'type' => 'string',
'default' => (string) ($field_or_option['value'] ?? ''),
],
'Image', 'Range' => [
'type' => 'integer',
'default' => (int) ($field_or_option['value'] ?? ''),
],
'Toggle' => [
'type' => 'boolean',
'default' => (bool) ($field_or_option['value'] ?? ''),
],
'Gallery' => [
'type' => 'array',
'default' => array_filter((array) ($field_or_option['value'] ?? [])),
],
'Repeater' => [
'type' => 'array',
'default' => array_map(function (array $item): array {
$item['id'] = substr(hash('sha256', uniqid((string) random_int(1000000000000, 9999999999999), true)), 0, 13);
return $item;
}, array_filter((array) ($field_or_option['value'] ?? []))),
],
default => null,
};
if ('Section' === ($field_or_option['type'] ?? '')) {
foreach ($field_or_option['fields'] as $section_field_or_option) {
$attributes[$section_field_or_option['name']] = $this->getDefaultAttribute($section_field_or_option['type'], $section_field_or_option['value'] ?? '');
}
} else {
$attributes[$field_or_option['name']] = $this->getDefaultAttribute($field_or_option['type'], $field_or_option['value'] ?? '');
}
}

return array_filter($attributes);
}

public function getDefaultAttribute(string $type, mixed $value): ?array
{
return match ($type) {
'Text', 'Textarea', 'Select', 'ColorPalette', 'RichText' => [
'type' => 'string',
'default' => (string) ($value ?? ''),
],
'Image', 'Range' => [
'type' => 'integer',
'default' => (int) ($value ?? ''),
],
'Toggle' => [
'type' => 'boolean',
'default' => (bool) ($value ?? ''),
],
'Gallery' => [
'type' => 'array',
'default' => array_filter((array) ($value ?? [])),
],
'Repeater' => [
'type' => 'array',
'default' => array_map(function (array $item): array {
$item['id'] = substr(hash('sha256', uniqid((string) random_int(1000000000000, 9999999999999), true)), 0, 13);
return $item;
}, array_filter((array) ($value ?? []))),
],
default => null,
};
}

/**
* Block data
*
Expand Down Expand Up @@ -219,11 +230,9 @@ public function blockData(): array
return [
'options' => [
...$this->options(),
...$this->defaultOptions(),
],
'fields' => [
...$this->fields(),
$this->defaultOptions(),
],
'fields' => !empty($this->fields()[0]['fields']) ? [...$this->fields()[0]['fields']] : [...$this->fields()],
];
}

Expand All @@ -250,29 +259,33 @@ public function options(): array
public function defaultOptions(): array
{
return [
[
'name' => 'margin_top_desktop',
'type' => 'Text',
'label' => 'Margin Top Desktop',
'value' => static::MARGIN_TOP_DESKTOP,
],
[
'name' => 'margin_top_mobile',
'type' => 'Text',
'label' => 'Margin Top Mobile',
'value' => static::MARGIN_TOP_MOBILE,
],
[
'name' => 'margin_bottom_desktop',
'type' => 'Text',
'label' => 'Margin Bottom Desktop',
'value' => static::MARGIN_BOTTOM_DESKTOP,
],
[
'name' => 'margin_bottom_mobile',
'type' => 'Text',
'label' => 'Margin Bottom Mobile',
'value' => static::MARGIN_BOTTOM_MOBILE,
'name' => __('Default Options'),
'type' => 'Section',
'fields' => [
[
'name' => 'margin_top_desktop',
'type' => 'Text',
'label' => 'Margin Top Desktop',
'value' => static::MARGIN_TOP_DESKTOP,
],
[
'name' => 'margin_top_mobile',
'type' => 'Text',
'label' => 'Margin Top Mobile',
'value' => static::MARGIN_TOP_MOBILE,
],
[
'name' => 'margin_bottom_desktop',
'type' => 'Text',
'label' => 'Margin Bottom Desktop',
'value' => static::MARGIN_BOTTOM_DESKTOP,
],
[
'name' => 'margin_bottom_mobile',
'type' => 'Text',
'label' => 'Margin Bottom Mobile',
'value' => static::MARGIN_BOTTOM_MOBILE,
],
],
];
}
Expand Down
79 changes: 73 additions & 6 deletions src/Editor/GutengoodBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class GutengoodBuilder
{
protected array $components = [];
protected array $repeater = [];
protected array $section = [];

protected function addComponent(string $name, string $type, array $args = []): self
{
Expand All @@ -17,10 +18,23 @@ protected function addComponent(string $name, string $type, array $args = []): s
...$args,
];

if (!empty($this->repeater)) {
$this->repeater['fields'][] = $component;
if (!empty($this->section)) {
if (!empty($this->repeater)) {
$this->repeater['fields'][] = $component;
} else {
$this->section['fields'][] = $component;
}
} else {
$this->components[] = $component;
$this->section = [
'name' => __('Block Options'),
'type' => 'Section',
'open' => true,
];
if (!empty($this->repeater)) {
$this->repeater['fields'][] = $component;
} else {
$this->section['fields'][] = $component;
}
}

return $this;
Expand Down Expand Up @@ -105,6 +119,8 @@ public function addImage(string $name, array $args = []): self
}

/**
* RichText component
*
* @param string $name The name of the component.
* @param array $args (string placeholder, string value)
*
Expand All @@ -129,6 +145,8 @@ public function addRange(string $name, array $args = []): self
}

/**
* Repeater
*
* @param string $name The name of the repeater component.
*
* @return self
Expand All @@ -146,22 +164,71 @@ public function addRepeater(string $name): self

public function endRepeater(): self
{
$this->components[] = $this->repeater;
$this->section['fields'][] = $this->repeater;
$this->repeater = [];

return $this;
}

/**
* Section. Use this as accordion panel for options
*
* @param string $name The name of the section.
* @param array $args (bool open)
*
* @return self
*/
public function addSection(string $name, array $args = []): self
{
if (!empty($this->section)) {
$this->components[] = $this->section;
}

$this->section = [
'name' => $name,
'type' => 'Section',
'fields' => [],
...$args,
];

return $this;
}

public function endSection(): self
{
if (!empty($this->section)) {
$this->components[] = $this->section;
$this->section = [];
}

return $this;
}

public function conditional(string $name, mixed $value): self
{
$index = count($this->components) - 1;
$this->components[$index]['condition'] = ['name' => $name, 'value' => $value];
if (!empty($this->section)) {
if (!empty($this->repeater)) {
$index = count($this->repeater['fields']) - 1;
if ($index >= 0) {
$this->repeater['fields'][$index]['condition'] = ['name' => $name, 'value' => $value];
}
} else {
$index = count($this->section['fields']) - 1;
if ($index >= 0) {
$this->section['fields'][$index]['condition'] = ['name' => $name, 'value' => $value];
}
}
}

return $this;
}

public function build(): array
{
if (!empty($this->section)) {
$this->components[] = $this->section;
}

return $this->components;
}
}

0 comments on commit 43463a1

Please sign in to comment.