Skip to content

Commit

Permalink
Only output list of used blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofandel committed Mar 5, 2024
1 parent 4c04072 commit 38d419f
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/Facades/TwillBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* @method static Collection<Block>getSettingsBlocks
* @method static Collection<Block>getRepeaters
* @method static registerManualBlock(string $blockClass, string $source = Block::SOURCE_APP)
* @method static Collection<Block>generateListOfAllBlocks(bool $settingsOnly = false)
* @method static Collection<Block>getListOfUsedBlocks()
* @method static Collection<Block>generateListOfAvailableBlocks(?array $blocks = null, ?array $groups = null, bool $settingsOnly = false, array|callable $excludeBlocks = [], bool $defaultOrder = false)
*/
class TwillBlocks extends Facade
Expand Down
1 change: 0 additions & 1 deletion src/Http/Controllers/Admin/ModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2262,7 +2262,6 @@ protected function form(?int $id, ?TwillModelContract $item = null): array
'translateTitle' => $this->titleIsTranslatable(),
'permalink' => $this->getIndexOption('permalink', $item),
'createWithoutModal' => ! $itemId && $this->getIndexOption('skipCreateModal'),
'allBlocks' => TwillBlocks::generateListOfAllBlocks()->keyBy('name'),
'form_fields' => $this->repository->getFormFields($item),
'baseUrl' => $baseUrl,
'localizedPermalinkBase' => $localizedPermalinkBase,
Expand Down
1 change: 0 additions & 1 deletion src/Http/Controllers/Admin/SettingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public function index(string $section)
'editableTitle' => false,
'customTitle' => ucfirst($section) . ' settings',
'section' => $section,
'allBlocks' => TwillBlocks::generateListOfAllBlocks()->keyBy('name'),
'form_fields' => $formFields,
'formBuilder' => Form::make(),
'saveUrl' => $this->urlGenerator->route(config('twill.admin_route_name_prefix') . 'settings.update', $section),
Expand Down
19 changes: 16 additions & 3 deletions src/TwillBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,18 @@ class TwillBlocks
public static $manualBlocks = [];

/**
* @return A17\Twill\Services\Blocks\BlockCollection
* @return \A17\Twill\Services\Blocks\BlockCollection
*/
private ?BlockCollection $blockCollection = null;

private array $cropConfigs = [];

private Collection $usedBlocks;

public function __construct()
{
$this->usedBlocks = collect();
}
/**
* Registers a blocks directory.
*
Expand Down Expand Up @@ -347,6 +353,11 @@ public function getAllCropConfigs(): array
return $this->cropConfigs;
}

public function getListOfUsedBlocks(): Collection
{
return $this->usedBlocks;
}

public function generateListOfAllBlocks(bool $settingsOnly = false): Collection
{
return once(function () use ($settingsOnly) {
Expand Down Expand Up @@ -380,7 +391,7 @@ function ($appBlock) use ($block) {
return false;
}
}
return isset($disabledBlocks[$block->name]) || isset($disabledBlocks[ltrim($block->componentClass, '\\')]);
return !(isset($disabledBlocks[$block->name]) || isset($disabledBlocks[ltrim($block->componentClass, '\\')]));
})->sortBy(function (Block $b) use ($customOrder) {
// Sort blocks by custom order then by group and then by name
return ($customOrder[$b->name] ?? $customOrder[ltrim($b->componentClass, '\\')] ?? PHP_INT_MAX) . '-' . $b->group . '-' . $b->name;
Expand All @@ -395,7 +406,7 @@ public function generateListOfAvailableBlocks(
array|callable $excludeBlocks = [],
bool $defaultOrder = false
): Collection {
$globalExcludeBlocks = TwillBlocks::getGloballyExcludedBlocks();
$globalExcludeBlocks = $this->getGloballyExcludedBlocks();

$matchBlock = function ($matcher, $block, $someFn = null) {
if (is_callable($matcher)) {
Expand Down Expand Up @@ -443,6 +454,8 @@ function (Block $block) use ($blocks, $groups, $excludeBlocks, $globalExcludeBlo
$finalList = $finalList->sortBy(fn(Block $block) => $groups[$block->group] ?? PHP_INT_MAX, SORT_NUMERIC);
}
}

$this->usedBlocks = $this->usedBlocks->merge($finalList->keyBy(fn(Block $block) => $block->name));
return $finalList;
}
}
20 changes: 11 additions & 9 deletions views/layouts/form.blade.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
@php use A17\Twill\Facades\TwillBlocks; @endphp
@extends('twill::layouts.main')

@section('appTypeClass', 'body--form')

@push('extra_css')
@if(app()->isProduction())
<link href="{{ twillAsset('main-form.css') }}" rel="preload" as="style" crossorigin/>
<link href="{{ twillAsset('main-form.css') }}" rel="preload" as="style" crossorigin />
@endif

@unless(config('twill.dev_mode', false))
<link href="{{ twillAsset('main-form.css') }}" rel="stylesheet" crossorigin/>
<link href="{{ twillAsset('main-form.css') }}" rel="stylesheet" crossorigin />
@endunless
@endpush

@push('extra_js_head')
@if(app()->isProduction())
<link href="{{ twillAsset('main-form.js') }}" rel="preload" as="script" crossorigin/>
<link href="{{ twillAsset('main-form.js') }}" rel="preload" as="script" crossorigin />
@endif
@endpush

Expand Down Expand Up @@ -136,7 +137,7 @@
:items-in-selects-tables="$users"
label-key="name"
name-pattern="user_%id%_permission"
:list-user="true"/>
:list-user="true" />
</x-twill::formFieldset>
@endcan
@endif
Expand All @@ -155,7 +156,8 @@
</a17-modal>
<a17-editor v-if="editor" ref="editor"
bg-color="{{ config('twill.block_editor.background_color') ?? '#FFFFFF' }}"></a17-editor>
<a17-previewer ref="preview" :breakpoints-config="{{ json_encode(config('twill.preview.breakpoints')) }}"></a17-previewer>
<a17-previewer ref="preview"
:breakpoints-config="{{ json_encode(config('twill.preview.breakpoints')) }}"></a17-previewer>
<a17-dialog ref="warningContentEditor" modal-title="{{ twillTrans('twill::lang.form.dialogs.delete.title') }}"
confirm-label="{{ twillTrans('twill::lang.form.dialogs.delete.confirm') }}">
<p class="modal--tiny-title">
Expand All @@ -173,7 +175,7 @@
restoreUrl: '{{ $restoreUrl ?? '' }}',
availableBlocks: {},
blocks: {},
allAvailableBlocks: {!! json_encode($allBlocks ?? []) !!},
allAvailableBlocks: {!! TwillBlocks::getListOfUsedBlocks() !!},
blockPreviewUrl: '{{ $blockPreviewUrl ?? '' }}',
repeaters: {!! json_encode(($form_fields['repeaters'] ?? []) + ($form_fields['blocksRepeaters'] ?? [])) !!},
fields: [],
Expand Down Expand Up @@ -235,10 +237,10 @@
const checked = mutation.payload.value
if (!isNaN(groupId)) {
const users = groupUserMapping[groupId]
users.forEach(function (userId) {
users.forEach(function(userId) {
// If the user's permission is <= view, it will be updated
const currentPermission = state['form']['fields'].find(function (e) {
return e.name == `user_${userId}_permission`
const currentPermission = state['form']['fields'].find(function(e) {
return e.name === `user_${userId}_permission`
}).value
if (currentPermission === '' || currentPermission === 'view-item') {
const field = {
Expand Down
1 change: 1 addition & 0 deletions views/layouts/main.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
window.STORE.browsers.selected = {}
@stack('vuexStore')
</script>
<script src="{{ twillAsset('chunk-vendors.js') }}"></script>
<script src="{{ twillAsset('chunk-common.js') }}"></script>
Expand Down
4 changes: 1 addition & 3 deletions views/partials/form/utils/_blocks_templates.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
@php
$blocks = \A17\Twill\Facades\TwillBlocks::getBlockCollection()
->collect()
$blocks = \A17\Twill\Facades\TwillBlocks::getListOfUsedBlocks()
->reject(function ($block) {
return $block->compiled ?? false;
});
Expand All @@ -26,4 +25,3 @@
window['{{ config('twill.js_namespace') }}'].STORE.form.availableRepeaters = {!! \A17\Twill\Facades\TwillBlocks::getAvailableRepeaters() ?? '{}' !!}
</script>

0 comments on commit 38d419f

Please sign in to comment.