Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Feb 14, 2024
1 parent 1dfda55 commit 8ff75b2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
21 changes: 21 additions & 0 deletions src/View/ComponentSlot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace ProtoneMedia\SpladeCore\View;

use Illuminate\View\ComponentSlot as BaseComponentSlot;

class ComponentSlot extends BaseComponentSlot
{
public function __construct(
string $contents,
array $attributes,
private string $hash
) {
parent::__construct($contents, $attributes);
}

public function getHash(): string
{
return $this->hash;
}
}
45 changes: 27 additions & 18 deletions src/View/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\View\Factory as BaseFactory;
use ProtoneMedia\SpladeCore\AddSpladeToComponentData;
use ProtoneMedia\SpladeCore\ResolveOnce;
use ProtoneMedia\SpladeCore\View\ComponentSlot as CustomComponentSlot;

class Factory extends BaseFactory
{
Expand Down Expand Up @@ -120,39 +121,47 @@ protected function componentData()

$data = parent::componentData();

if (! array_key_exists('spladeBridge', $data)) {
return $data;
}
$templateId = $data['spladeBridge']['template_hash'] ?? null;

/** @var ComponentSlot|null */
$defaultSlot = $data['__laravel_slots']['__default'] ?? null;

$templateId = $this->componentData[count($this->componentStack) + 1]['spladeBridge']['template_hash'] ?? null;
if (! isset($data['__laravel_slots']['slot'])
&& $defaultSlot?->toHtml() !== '###SPLADE-INJECT-HERE###'
&& ! Str::startsWith($defaultSlot?->toHtml(), ['<generic-splade-component', '<!--splade-template-id='])
) {
$data['__laravel_slots']['slot'] = $data['__laravel_slots']['__default'];
$data['__laravel_slots']['__default'] = new ComponentSlot;
}

$data['__laravel_slots'] = collect($data['__laravel_slots'] ?? [])
->mapWithKeys(function (ComponentSlot $slot, $name) use ($templateId) {
if ($slot->isEmpty()) {
return $slot;
}
$slot = new CustomComponentSlot(
$slot->toHtml(),
$slot->attributes->getAttributes(),
$hash = md5(Str::random(32))
);

$hash = md5(Str::random());
$this->originalSlots[$name] = clone $slot;

$this->originalSlots[$hash] = clone $slot;
if ($slot->isEmpty()) {
return [$name => $slot];
}

$vueSlot = '<slot name="'.$hash.'"></slot>';
$vueSlot = '<slot name="'.$slot->getHash().'" '.((string) $slot->attributes).'></slot>';

$componentSlot = new ComponentSlot(
$componentSlot = new CustomComponentSlot(
static::$trackSpladeComponents
? "<!--splade-template-id=\"{$templateId}\"-->{$vueSlot}"
: $vueSlot
: $vueSlot,
[], $hash
);

return [$hash => $componentSlot];
return [$name => $componentSlot];
})
->all();

foreach ($data['__laravel_slots'] as $name => $slot) {
if ($name === '__default') {
$name = 'slot';
}

$data[$name] = $slot;
}

Expand Down Expand Up @@ -228,7 +237,7 @@ public function renderComponent()
$this->pushSpladeTemplate($templateId, $output);

$slotsHtml = collect($this->originalSlots)->map(function ($slot, $name) {
return "<template #{$name}>{$slot->toHtml()}</template>";
return "<template #{$slot->getHash()}>{$slot->toHtml()}</template>";
})->implode("\n");

$genericComponent = "<generic-splade-component {$attrs} :bridge=\"{$spladeBridgeHtml}\">
Expand Down

0 comments on commit 8ff75b2

Please sign in to comment.