Skip to content
This repository has been archived by the owner on Jun 23, 2023. It is now read-only.

Commit

Permalink
feat: Add support for named block editors
Browse files Browse the repository at this point in the history
  • Loading branch information
pboivin committed Aug 3, 2022
1 parent 6c884a5 commit ba1626d
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 13 deletions.
38 changes: 28 additions & 10 deletions src/HasFormTemplates.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,34 @@ public function getCurrentTemplateOptions()
*/
public function prefillBlockSelection()
{
$i = 1;

foreach ($this->current_template_block_selection as $blockType) {
app(BlockRepository::class)->create([
'blockable_id' => $this->id,
'blockable_type' => static::class,
'position' => $i++,
'content' => '{}',
'type' => $blockType,
]);
$blockSelection = $this->current_template_block_selection ?: [];

if (empty($blockSelection)) {
return;
}

$firstItem = reset($blockSelection);
$blockEditors = [];

if (is_array($firstItem)) {
$blockEditors = $blockSelection;
} else {
$blockEditors['default'] = $blockSelection;
}

foreach ($blockEditors as $editorName => $editorBlocks) {
$i = 1;

foreach ($editorBlocks as $blockType) {
app(BlockRepository::class)->create([
'blockable_id' => $this->id,
'blockable_type' => static::class,
'position' => $i++,
'content' => '{}',
'type' => $blockType,
'editor_name' => $editorName,
]);
}
}
}
}
34 changes: 31 additions & 3 deletions tests/Feature/BlockEditorTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ protected function createArticle($title = 'Lorem ipsum', $template = 'full_artic
]);
}

public function test_can_prefill_block_templates()
public function test_can_prefill_default_block_templates()
{
Article::$TEST_USE_NAMED_BLOCK_SELECTION = false;

$this->assertEquals(0, Article::count());
$this->assertEquals(0, Block::count());

Expand All @@ -51,8 +53,34 @@ public function test_can_prefill_block_templates()
$this->assertEquals(3, $article->blocks->count());

$this->assertEquals(
$article->current_template_block_selection,
$article->blocks->pluck('type')->toArray()
['article-header', 'article-paragraph', 'article-references'],
$article->blocks()->editor('default')->get()->pluck('type')->toArray()
);
}

public function test_can_prefill_named_editor_block_templates()
{
Article::$TEST_USE_NAMED_BLOCK_SELECTION = true;

$this->assertEquals(0, Article::count());
$this->assertEquals(0, Block::count());

$this->createArticle();

$this->assertEquals(1, Article::count());
$this->assertEquals(3, Block::count());

$article = Article::first();
$this->assertEquals(3, $article->blocks->count());

$this->assertEquals(
['article-header', 'article-paragraph'],
$article->blocks()->editor('default')->get()->pluck('type')->toArray()
);

$this->assertEquals(
['article-references'],
$article->blocks()->editor('footer')->get()->pluck('type')->toArray()
);
}
}
29 changes: 29 additions & 0 deletions tests/Stubs/articles/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,33 @@ class Article extends Model implements Sortable
'article-references',
'linked-article',
];

public static $TEST_USE_NAMED_BLOCK_SELECTION = false;

public function setNamedBlockSelection()
{
$this->formTemplates = [
'options' => [
[
'value' => 'full_article',
'label' => 'Full Article',
'block_selection' => [
'default' => ['article-header', 'article-paragraph'],
'footer' => ['article-references'],
],
],
[
'value' => 'linked_article',
'label' => 'Linked Article',
'block_selection' => ['article-header', 'linked-article'],
],
[
'value' => 'empty',
'label' => 'Empty',
'block_selection' => [],
],
],
'default' => 'full_article',
];
}
}
9 changes: 9 additions & 0 deletions tests/Stubs/articles/ArticleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,13 @@ public function __construct(Article $model)
{
$this->model = $model;
}

public function afterSave($object, $fields)
{
if (Article::$TEST_USE_NAMED_BLOCK_SELECTION) {
$object->setNamedBlockSelection();
}

parent::afterSave($object, $fields);
}
}

0 comments on commit ba1626d

Please sign in to comment.