Skip to content

Commit

Permalink
meh
Browse files Browse the repository at this point in the history
  • Loading branch information
markhuot committed Jan 9, 2024
1 parent 466f1bd commit 23353ae
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 165 deletions.
8 changes: 5 additions & 3 deletions src/actions/AddComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function handle(
// Check if we can be added here
$parent = (new GetParentFromPath)->handle($elementId, $fieldId, $path);
$slot = $parent?->getType()->getSlotDefinition($slotName);

Check failure on line 31 in src/actions/AddComponent.php

View workflow job for this annotation

GitHub Actions / Run PHPStan

Using nullsafe method call on non-nullable type markhuot\keystone\models\Component. Use -> instead.
throw_if($slot && ! $slot->allows($type), 'Not allowed here');
throw_if($slot && !$slot->allows($type), 'Not allowed here');

Check failure on line 32 in src/actions/AddComponent.php

View workflow job for this annotation

GitHub Actions / Run PHPStan

Left side of && is always true.

// Create the data
$componentData = new ComponentData;
Expand Down Expand Up @@ -76,8 +76,10 @@ protected function createDefaultChildrenFor(Component $component): Collection
*/
protected function createDefaultsFor(Component $component, Collection $slotDefaults): Collection
{
return $slotDefaults->map(fn ($defaults, $slotName) => collect($defaults)->map(fn ($config, $index) => $this->createChild($component, $index, $slotName, $config)
)
return $slotDefaults->map(
fn ($defaults, $slotName) => collect($defaults)->map(
fn ($config, $index) => $this->createChild($component, $index, $slotName, $config)
)
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/actions/GetParentFromPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

class GetParentFromPath
{
public function handle(int $elementId, int $fieldId, ?string $path): ?Component
public function handle(int $elementId, int $fieldId, ?string $path): Component
{
$parentId = last(explode('/', $path));

return $parentId ? Component::findOne([
'elementId' => $elementId,
'fieldId' => $fieldId,
'id' => $parentId,
]) : null;
]) : (new Component)->setType('keystone/fragment');
}
}
4 changes: 2 additions & 2 deletions src/base/ComponentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class ComponentType
protected ?string $name = null;

// https://phosphoricons.com
protected string $icon = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256"><rect width="256" height="256" fill="none"/><path d="M64,216a8,8,0,0,1-8-8V165.31a28,28,0,1,1,0-50.62V72a8,8,0,0,1,8-8h46.69a28,28,0,1,1,50.61,0H208a8,8,0,0,1,8,8v42.69a28,28,0,1,0,0,50.62V208a8,8,0,0,1-8,8Z" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="12"/></svg>';
protected string $icon = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256"><path d="M64,216a8,8,0,0,1-8-8V165.31a28,28,0,1,1,0-50.62V72a8,8,0,0,1,8-8h46.69a28,28,0,1,1,50.61,0H208a8,8,0,0,1,8,8v42.69a28,28,0,1,0,0,50.62V208a8,8,0,0,1-8,8Z" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="12"/></svg>';

/**
* The category the icon will display under when adding new components
Expand Down Expand Up @@ -90,7 +90,7 @@ public function getSlotDefinitions(): Collection
return $this->getSchema()[1];
}

public function getSlotDefinition(?string $slot)
public function getSlotDefinition(?string $slot): SlotDefinition
{
return $this->getSlotDefinitions()
->where(fn ($defn) => $defn->getName() === $slot)
Expand Down
14 changes: 12 additions & 2 deletions src/base/SlotDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ public function isCollapsed(): bool

public function allows(string $type): bool
{
if (! empty($this->whitelist)) {
if (!empty($this->whitelist)) {
return array_search($type, $this->whitelist) !== false;
}

if (! empty($this->blacklist)) {
if (!empty($this->blacklist)) {
return array_search($type, $this->blacklist) === false;
}

Expand All @@ -75,11 +75,21 @@ public function getName(): ?string
return $this->name;
}

public function hasWhitelist(): bool
{
return count($this->whitelist) > 0;
}

public function getWhitelist(): array
{
return $this->whitelist;
}

public function hasBlacklist(): bool
{
return count($this->blacklist) > 0;
}

public function getBlacklist(): array
{
return $this->blacklist;
Expand Down
4 changes: 4 additions & 0 deletions src/config/keystone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

return [
];
20 changes: 19 additions & 1 deletion src/controllers/ComponentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use markhuot\keystone\actions\GetComponentType;
use markhuot\keystone\actions\GetParentFromPath;
use markhuot\keystone\actions\MoveComponent;
use markhuot\keystone\base\ComponentType;
use markhuot\keystone\base\SlotDefinition;
use markhuot\keystone\behaviors\BodyParamObjectBehavior;
use markhuot\keystone\models\Component;
use markhuot\keystone\models\http\AddComponentRequest;
Expand All @@ -24,6 +26,22 @@ public function actionAdd()
{
$data = $this->request->getQueryParamObjectOrFail(AddComponentRequest::class);
$parent = (new GetParentFromPath)->handle($data->element->id, $data->field->id, $data->path);
$groups = (new GetComponentType())->all()
->filter(function ($t) use ($parent, $data) {
$slot = $parent->getType()->getSlotDefinition($data->slot);
$isDependent = ($t->getExport('isDependent') ?? false) === true;

if (! $slot->hasWhitelist() && $isDependent) {
return false;
}

if (! $slot->allows($t->getHandle())) {
return false;
}

return true;
})
->groupBy(fn ($t) => $t->getCategory())->sortKeys();

return $this->asCpScreen()
->title('Add component')
Expand All @@ -34,7 +52,7 @@ public function actionAdd()
'path' => $data->path,
'slot' => $data->slot,
'parent' => $parent,
'groups' => (new GetComponentType())->all()->groupBy(fn ($t) => $t->getCategory())->sortKeys(),
'groups' => $groups,
'sortOrder' => $data->sortOrder,
]);
}
Expand Down
Loading

0 comments on commit 23353ae

Please sign in to comment.