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 b256607
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 12 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
14 changes: 13 additions & 1 deletion 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->values();
}

public function generateListOfAllBlocks(bool $settingsOnly = false): Collection
{
return once(function () use ($settingsOnly) {
Expand Down Expand Up @@ -443,6 +454,7 @@ function (Block $block) use ($blocks, $groups, $excludeBlocks, $globalExcludeBlo
$finalList = $finalList->sortBy(fn(Block $block) => $groups[$block->group] ?? PHP_INT_MAX, SORT_NUMERIC);
}
}
$this->usedBlocks->merge($finalList->keyBy(fn(Block $block) => $block->name));
return $finalList;
}
}
23 changes: 14 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,6 @@
restoreUrl: '{{ $restoreUrl ?? '' }}',
availableBlocks: {},
blocks: {},
allAvailableBlocks: {!! json_encode($allBlocks ?? []) !!},
blockPreviewUrl: '{{ $blockPreviewUrl ?? '' }}',
repeaters: {!! json_encode(($form_fields['repeaters'] ?? []) + ($form_fields['blocksRepeaters'] ?? [])) !!},
fields: [],
Expand Down Expand Up @@ -224,6 +225,10 @@
window['{{ config('twill.js_namespace') }}'].STORE.groupUserMapping = {!! isset($groupUserMapping) ? json_encode($groupUserMapping) : '[]' !!};
@stop

@push('vuexStore')
window['{{config('twill.js_namespace')}}.STORE.form.allAvailableBlocks'] = {!! json_encode(TwillBlocks::getListOfUsedBlocks()) !!};
@endpush

@prepend('extra_js')
@includeWhen(config('twill.block_editor.inline_blocks_templates', true), 'twill::partials.form.utils._blocks_templates')
<script src="{{ twillAsset('main-form.js') }}" crossorigin></script>
Expand All @@ -235,10 +240,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

0 comments on commit b256607

Please sign in to comment.