Skip to content

Commit

Permalink
Merge branch 'feature/runtime-blocks'
Browse files Browse the repository at this point in the history
  • Loading branch information
ifox committed Nov 21, 2019
2 parents 88423d9 + b86e8d2 commit 969e126
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 19 deletions.
5 changes: 4 additions & 1 deletion config/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
'block_views_path' => 'site.blocks', // path where a view file per block type is stored
'block_views_mappings' => [], // custom mapping of block types and views
'block_preview_render_childs' => true, // indicates if childs should be rendered when using repeater in blocks
'block_presenter_path' => null, //Allow to set a custom presenter to a block model
'block_presenter_path' => null, // allow to set a custom presenter to a block model
// Indicates if blocks templates should be inlined in HTML.
// When setting to false, make sure to build Twill with your all your custom blocks.
'inline_blocks_templates' => true,
'blocks' => [
'text' => [
'title' => 'Body text',
Expand Down
10 changes: 10 additions & 0 deletions frontend/js/main-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import attributes from '@/store/modules/attributes'
// mixins
import formatPermalink from '@/mixins/formatPermalink'
import editorMixin from '@/mixins/editor.js'
import BlockMixin from '@/mixins/block'

// configuration
Vue.use(A17Config)
Expand Down Expand Up @@ -101,6 +102,15 @@ importedBlocks.keys().map(block => {
}
})

if (typeof window.TWILL_BLOCKS_COMPONENTS !== 'undefined') {
window.TWILL_BLOCKS_COMPONENTS.map(blockName => {
return Vue.component('a17-block-' + blockName, {
template: '#a17-block-' + blockName,
mixins: [BlockMixin]
})
})
}

/* eslint-disable no-new */
/* eslint no-unused-vars: "off" */
window.vm = new Vue({
Expand Down
1 change: 1 addition & 0 deletions views/layouts/form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@
@stop

@prepend('extra_js')
@includeWhen(config('twill.block_editor.inline_blocks_templates', true), 'twill::partials.form.utils._blocks_templates')
<script src="{{ mix('/assets/admin/js/manifest.js') }}"></script>
<script src="{{ mix('/assets/admin/js/vendor.js') }}"></script>
<script src="{{ mix('/assets/admin/js/main-form.js') }}"></script>
Expand Down
32 changes: 14 additions & 18 deletions views/partials/form/_block_editor.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,23 @@
<a17-content title="{{ $label }}"></a17-content>

@php
if (isset($blocks)) {
$availableBlocks = collect($blocks)->map(function ($block) {
return config('twill.block_editor.blocks.' . $block);
})->filter()->toArray();
}
elseif (isset($group)) {
$blocks = config('twill.block_editor.blocks');
$availableBlocks = array_filter($blocks, function($val) use(&$group) {
if (isset($val['group']) && $val['group'] == $group) {
return true;
}
return false;
});
}
else {
$availableBlocks = config('twill.block_editor.blocks');
}
if (isset($blocks)) {
$allowedBlocks = collect($blocks)->mapWithKeys(function ($block) {
return [$block => config('twill.block_editor.blocks.' . $block)];
})->filter()->toArray();
} elseif (isset($group)) {
$blocks = config('twill.block_editor.blocks');
$allowedBlocks = array_filter($blocks, function ($block) use ($group) {
return isset($block['group']) && $block['group'] === $group;
});
} else {
$allowedBlocks = config('twill.block_editor.blocks');
}
@endphp

@push('vuexStore')
window.STORE.form.content = {!! json_encode(array_values($availableBlocks)) !!}
window.STORE.form.content = {!! json_encode(array_values($allowedBlocks)) !!}
window.STORE.form.blocks = {!! json_encode($form_fields['blocks'] ?? []) !!}

@foreach($form_fields['blocksFields'] ?? [] as $field)
Expand Down
26 changes: 26 additions & 0 deletions views/partials/form/utils/_blocks_templates.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<script>
window.TWILL_BLOCKS_COMPONENTS = []
</script>

@php
$allBlocks = (config('twill.block_editor.blocks') ?? []) + (config('twill.block_editor.repeaters') ?? []);
$blocksForInlineTemplates = collect($allBlocks)->reject(function ($block) {
return $block['compiled'] ?? false;
})->filter(function ($block, $blockName) {
return View::exists($blockName);
});
@endphp

@foreach($blocksForInlineTemplates as $blockName => $block)
<script>
window.TWILL_BLOCKS_COMPONENTS.push('{{ $blockName }}')
</script>
<script type="text/x-template" id="{{ $block['component'] }}">
<div class="block__body">
{!! View::make('admin.blocks.' . $blockName, [
'renderForBlocks' => true
])->render() !!}
</div>
</script>
@endforeach

0 comments on commit 969e126

Please sign in to comment.