Skip to content

Commit

Permalink
Bugfixes blocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
haringsrob committed Mar 4, 2022
1 parent e3c4760 commit 4fc4b38
Show file tree
Hide file tree
Showing 13 changed files with 298 additions and 201 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@
],
"aliases": {
"TwillCapsules": "A17\\Twill\\Facades\\TwillCapsules",
"TwillRoutes": "A17\\Twill\\Facades\\TwillRoutes"
"TwillRoutes": "A17\\Twill\\Facades\\TwillRoutes",
"TwillBlocks": "A17\\Twill\\Facades\\TwillBlocks",
"TwillUtil": "A17\\Twill\\Facades\\TwillUtil"
}
}
},
Expand Down
31 changes: 16 additions & 15 deletions src/Commands/GenerateBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
use A17\Twill\Services\Blocks\BlockCollection;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\View\Factory as ViewFactory;

class GenerateBlocks extends Command
{
const NO_BLOCKS_DEFINED = "There are no blocks defined yet. Please refer to https://twill.io/docs/#block-editor-3 in order to create blocks.";
const SCANNING_BLOCKS = "Starting to scan block views directory...";
public const NO_BLOCKS_DEFINED = 'There are no blocks defined yet. Please refer to https://twill.io/docs/#block-editor-3 in order to create blocks.';

public const SCANNING_BLOCKS = 'Starting to scan block views directory...';

/**
* The name and signature of the console command.
Expand All @@ -27,7 +27,7 @@ class GenerateBlocks extends Command
*
* @var string
*/
protected $description = "Generate blocks as single file Vue components from blade views";
protected $description = 'Generate blocks as single file Vue components from blade views';

/**
* @var Filesystem
Expand Down Expand Up @@ -58,7 +58,7 @@ public function __construct(Filesystem $filesystem, ViewFactory $viewFactory)
*/
public function handle()
{
if (!$this->filesystem->exists($path = resource_path('views/admin/blocks'))) {
if (! $this->filesystem->exists(resource_path('views/admin/blocks'))) {
$this->error(self::NO_BLOCKS_DEFINED);

return;
Expand All @@ -83,21 +83,21 @@ public function handle()

$vueBlockPath = $this->makeDirectory(resource_path(config('twill.block_editor.custom_vue_blocks_resource_path', 'assets/js/blocks'))) . '/Block' . Str::title($blockName) . '.vue';

$write = !$this->filesystem->exists($vueBlockPath);
$write = ! $this->filesystem->exists($vueBlockPath);

if (!$write) {
if (! $write) {
$write = $this->confirm("[$vueBlockPath] exists, overwrite?", false);
}

if ($write) {
$this->filesystem->put($vueBlockPath, $vueBlockContent);
$this->info("Block " . Str::title($blockName) . " generated successfully");
$this->info('Block ' . Str::title($blockName) . ' generated successfully');
} else {
$this->info("Skipping block " . Str::title($blockName) . ".");
$this->info('Skipping block ' . Str::title($blockName) . '.');
}
});

$this->info("All blocks have been generated!");
$this->info('All blocks have been generated!');
}

/**
Expand All @@ -108,9 +108,10 @@ public function handle()
*/
public function makeDirectory($directory)
{
if (!$this->filesystem->exists($directory)) {
if (! $this->filesystem->exists($directory)) {
$this->filesystem->makeDirectory($directory, 0755, true);
}

return $directory;
}

Expand All @@ -122,19 +123,19 @@ public function makeDirectory($directory)
*/
private function sanitize($html)
{
$search = array(
$search = [
'/\>[^\S ]+/s', // strip whitespaces after tags, except space
'/[^\S ]+\</s', // strip whitespaces before tags, except space
'/(\s)+/s', // shorten multiple whitespace sequences
'/<!--(.|\s)*?-->/', // Remove HTML comments
);
];

$replace = array(
$replace = [
'>',
'<',
'\\1',
'',
);
];

return preg_replace($search, $replace, Block::removeSpecialBladeTags($html));
}
Expand Down
83 changes: 45 additions & 38 deletions src/Commands/ListBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace A17\Twill\Commands;

use A17\Twill\Facades\TwillBlocks;
use A17\Twill\Services\Blocks\Block;
use A17\Twill\Services\Blocks\BlockCollection;
use Illuminate\Support\Str;

class ListBlocks extends Command
Expand Down Expand Up @@ -32,15 +32,15 @@ class ListBlocks extends Command
/**
* Blocks collection.
*
* @var BlockCollection
* @var A17\Twill\Services\Blocks\BlockCollection
*/
protected $blockCollection;

public function __construct(BlockCollection $blockCollection)
public function __construct()
{
parent::__construct();

$this->blockCollection = $blockCollection;
$this->blockCollection = TwillBlocks::getBlockCollection();
}

protected function displayMissingDirectories(): void
Expand All @@ -53,18 +53,26 @@ protected function displayMissingDirectories(): void
}

/**
* @param \Illuminate\Support\Collection $blockCollection
* @return mixed
* @return Block[]
*/
protected function generateHeaders($blockCollection)
protected function generateHeaders()
{
return $blockCollection
->first()
->keys()
->map(function ($key) {
return Str::studly($key);
})
->toArray();
return [
'Title',
'TitleField',
'HideTitlePrefix',
'Trigger',
'Name',
'Group',
'Type',
'Icon',
'Compiled',
'Source',
'NewFormat',
'File',
'Component',
'Max',
];
}

/**
Expand All @@ -79,35 +87,33 @@ protected function getBlocks()

$typeFiltered = $this->option('blocks') || $this->option('repeaters');

return $this->blockCollection
->collect()
$filteredList = $this->blockCollection
->reject(function (Block $block) use ($sourceFiltered) {
return $sourceFiltered && !$this->option($block->source);
return $sourceFiltered && ! $this->option($block->source);
})
->reject(function (Block $block) use ($typeFiltered) {
return $this->dontPassTextFilter($block) ||
($typeFiltered &&
!$this->option(Str::plural($block->type)));
})
->map(function (Block $block) {
$data = $this->colorize(
$this->option('short') ? $block->toShortList() : $block->toList()
);
// We do not render this.
unset($data['rules'], $data['rulesForTranslatedFields']);
return $data;
})
->sortBy(function ($block) {
return [$block['group'], $block['title']];
! $this->option(Str::plural($block->type)));
})->sortBy(function (Block $block) {
return [$block->group, $block->title];
});
}

/**
* @return \A17\Twill\Services\Blocks\BlockCollection
*/
public function getBlockCollection()
{
return $this->blockCollection;
$list = [];

/** @var Block $block */
foreach ($filteredList as $block) {
$data = $this->colorize(
$this->option('short') ? $block->toShortList() : $block->toList()
);

// We do not render this.
unset($data['rules'], $data['rulesForTranslatedFields']);

$list[] = $data;
}

return collect($list);
}

/**
Expand All @@ -128,7 +134,7 @@ public function handle()
}

$this->table(
$this->generateHeaders($blockCollection),
$this->generateHeaders(),
$blockCollection->toArray()
);
}
Expand All @@ -153,12 +159,13 @@ public function colorize($block)
public function dontPassTextFilter(Block $block)
{
if (filled($filter = $this->argument('filter'))) {
return !$block
return ! $block
->toList()
->reduce(function ($keep, $element) use ($filter) {
if (is_array($element)) {
return false;
}

return $keep ||
Str::contains(
Str::lower($element),
Expand Down
13 changes: 13 additions & 0 deletions src/Facades/TwillBlocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace A17\Twill\Facades;

use Illuminate\Support\Facades\Facade;

class TwillBlocks extends Facade
{
protected static function getFacadeAccessor(): string
{
return \A17\Twill\TwillBlocks::class;
}
}
Loading

0 comments on commit 4fc4b38

Please sign in to comment.