From ba1626df804b18d3316d7439935c886b4babee5a Mon Sep 17 00:00:00 2001 From: Patrick Boivin Date: Wed, 3 Aug 2022 09:54:28 -0400 Subject: [PATCH] feat: Add support for named block editors --- src/HasFormTemplates.php | 38 ++++++++++++++++------ tests/Feature/BlockEditorTemplateTest.php | 34 +++++++++++++++++-- tests/Stubs/articles/Article.php | 29 +++++++++++++++++ tests/Stubs/articles/ArticleRepository.php | 9 +++++ 4 files changed, 97 insertions(+), 13 deletions(-) diff --git a/src/HasFormTemplates.php b/src/HasFormTemplates.php index 13a36fe..7929889 100644 --- a/src/HasFormTemplates.php +++ b/src/HasFormTemplates.php @@ -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, + ]); + } } } } diff --git a/tests/Feature/BlockEditorTemplateTest.php b/tests/Feature/BlockEditorTemplateTest.php index 83eba6f..99adb20 100644 --- a/tests/Feature/BlockEditorTemplateTest.php +++ b/tests/Feature/BlockEditorTemplateTest.php @@ -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()); @@ -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() ); } } diff --git a/tests/Stubs/articles/Article.php b/tests/Stubs/articles/Article.php index 2c23ad4..fb8c9e5 100644 --- a/tests/Stubs/articles/Article.php +++ b/tests/Stubs/articles/Article.php @@ -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', + ]; + } } diff --git a/tests/Stubs/articles/ArticleRepository.php b/tests/Stubs/articles/ArticleRepository.php index c94a62c..4fac06d 100644 --- a/tests/Stubs/articles/ArticleRepository.php +++ b/tests/Stubs/articles/ArticleRepository.php @@ -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); + } }