Skip to content

Commit

Permalink
Add block events to allow hooking of data
Browse files Browse the repository at this point in the history
  • Loading branch information
Muffinman committed Feb 6, 2025
1 parent 1e260ca commit 0955b13
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 3 deletions.
49 changes: 46 additions & 3 deletions src/Components/DropBlockEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

namespace Jeffreyvr\DropBlockEditor\Components;

use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Str;
use Jeffreyvr\DropBlockEditor\Blocks\Block;
use Jeffreyvr\DropBlockEditor\Events\BlockClone;
use Jeffreyvr\DropBlockEditor\Events\BlockCloned;
use Jeffreyvr\DropBlockEditor\Events\BlockDeleted;
use Jeffreyvr\DropBlockEditor\Events\BlockInsert;
use Jeffreyvr\DropBlockEditor\Events\BlockInserted;
use Jeffreyvr\DropBlockEditor\Parsers\Parse;
use Livewire\Component;

Expand Down Expand Up @@ -39,6 +45,8 @@ class DropBlockEditor extends Component
'refreshComponent' => '$refresh',
];

protected ?Dispatcher $events = null;

public function canUndo(): bool
{
return $this->historyIndex > 0;
Expand Down Expand Up @@ -124,10 +132,20 @@ public function cloneBlock(): void
{
$clone = $this->activeBlocks[$this->activeBlockIndex];

if ($this->events) {
$event = new BlockClone($clone);
$this->events->dispatch($event);
$clone = $event->getBlock();
}

$this->activeBlocks[] = $clone;

$this->activeBlockIndex = array_key_last($this->activeBlocks);

if ($this->events) {
$this->events->dispatch(new BlockCloned($this->activeBlocks[$this->activeBlockIndex]));
}

$this->recordInHistory();
}

Expand All @@ -137,8 +155,14 @@ public function deleteBlock(): void

$this->activeBlockIndex = false;

$deleted = $this->activeBlocks[$activeBlockId];

unset($this->activeBlocks[$activeBlockId]);

if ($this->events) {
$this->events->dispatch(new BlockDeleted($deleted));
}

$this->recordInHistory();
}

Expand All @@ -157,6 +181,11 @@ public function getActiveBlock(): bool|Block
->data($this->activeBlocks[$this->activeBlockIndex]['data']);
}

public function boot(?Dispatcher $events = null): void
{
$this->events = $events;
}

public function mount(): void
{
$this->parsers = config('dropblockeditor.parsers', []);
Expand Down Expand Up @@ -185,11 +214,21 @@ public function reorder($ids): void

public function insertBlock($id, $index = null, $placement = null): void
{
if ($index === null) {
$block = $this->blocks[$id];
$block = $this->blocks[$id];

if ($this->events) {
$event = new BlockInsert($block);
$this->events->dispatch($event);
$block = $event->getBlock();
}

if ($index === null) {
$this->activeBlocks[] = $block;

if ($this->events) {
$this->events->dispatch(new BlockInserted($block));
}

return;
}

Expand All @@ -199,7 +238,11 @@ public function insertBlock($id, $index = null, $placement = null): void
$newIndex = $index + 1;
}

$this->activeBlocks = array_merge(array_slice($this->activeBlocks, 0, $newIndex), [$this->blocks[$id]], array_slice($this->activeBlocks, $newIndex));
$this->activeBlocks = array_merge(array_slice($this->activeBlocks, 0, $newIndex), [$block], array_slice($this->activeBlocks, $newIndex));

if ($this->events) {
$this->events->dispatch(new BlockInserted($block));
}

$this->recordInHistory();
}
Expand Down
28 changes: 28 additions & 0 deletions src/Events/BlockClone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Jeffreyvr\DropBlockEditor\Events;

use Illuminate\Foundation\Events\Dispatchable;

class BlockClone
{
use Dispatchable;

/**
* Create a new event instance.
*/
public function __construct(protected array $block = [])
{
//
}

public function setBlock(array $block): void
{
$this->block = $block;
}

public function getBlock(): array
{
return $this->block;
}
}
23 changes: 23 additions & 0 deletions src/Events/BlockCloned.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Jeffreyvr\DropBlockEditor\Events;

use Illuminate\Foundation\Events\Dispatchable;

class BlockCloned
{
use Dispatchable;

/**
* Create a new event instance.
*/
public function __construct(protected array $block = [])
{
//
}

public function getBlock(): array
{
return $this->block;
}
}
23 changes: 23 additions & 0 deletions src/Events/BlockDeleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Jeffreyvr\DropBlockEditor\Events;

use Illuminate\Foundation\Events\Dispatchable;

class BlockDeleted
{
use Dispatchable;

/**
* Create a new event instance.
*/
public function __construct(protected array $block = [])
{
//
}

public function getBlock(): array
{
return $this->block;
}
}
28 changes: 28 additions & 0 deletions src/Events/BlockInsert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Jeffreyvr\DropBlockEditor\Events;

use Illuminate\Foundation\Events\Dispatchable;

class BlockInsert
{
use Dispatchable;

/**
* Create a new event instance.
*/
public function __construct(protected array $block = [])
{
//
}

public function setBlock(array $block): void
{
$this->block = $block;
}

public function getBlock(): array
{
return $this->block;
}
}
23 changes: 23 additions & 0 deletions src/Events/BlockInserted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Jeffreyvr\DropBlockEditor\Events;

use Illuminate\Foundation\Events\Dispatchable;

class BlockInserted
{
use Dispatchable;

/**
* Create a new event instance.
*/
public function __construct(protected array $block = [])
{
//
}

public function getBlock(): array
{
return $this->block;
}
}

0 comments on commit 0955b13

Please sign in to comment.