Skip to content

Commit

Permalink
Merge bab8d46 into fa9379a
Browse files Browse the repository at this point in the history
  • Loading branch information
haringsrob committed Feb 23, 2022
2 parents fa9379a + bab8d46 commit a15256c
Show file tree
Hide file tree
Showing 38 changed files with 1,258 additions and 228 deletions.
3 changes: 3 additions & 0 deletions src/Commands/GenerateBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Str;
use Illuminate\View\Factory as ViewFactory;

Expand Down Expand Up @@ -75,7 +76,9 @@ public function handle()
$blockName = str_replace('a17-block-', '', $block->component);
$basename = str_replace('.blade.php', '', $block->fileName);

View::share('TwillUntilConsumed', ['renderForBlocks' => true]);
$vueBlockTemplate = $this->viewFactory->make('admin.blocks.' . $basename, ['renderForBlocks' => true])->render();
View::share('TwillUntilConsumed', []);

$vueBlockContent = $this->viewFactory->make('twill::blocks.builder', [
'render' => $this->sanitize($vueBlockTemplate),
Expand Down
3 changes: 3 additions & 0 deletions src/Http/Controllers/Admin/ModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ public function edit($id, $submoduleId = null)
return View::exists($view);
});

View::share('form', $this->form($id));
return View::make($view, $this->form($id));
}

Expand Down Expand Up @@ -505,6 +506,7 @@ public function create($parentModuleId = null)
return View::exists($view);
});

View::share('form', $this->form(null));
return View::make($view, $this->form(null));
}

Expand Down Expand Up @@ -638,6 +640,7 @@ public function restoreRevision($id)

Session::flash('restoreMessage', twillTrans('twill::lang.publisher.restore-message', ['user' => $revision->byUser, 'date' => $date]));

View::share('form', $this->form($id, $item));
return View::make($view, $this->form($id, $item));
}

Expand Down
2 changes: 2 additions & 0 deletions src/Http/Controllers/Admin/SingletonModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace A17\Twill\Http\Controllers\Admin;

use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\View;

abstract class SingletonModuleController extends ModuleController
{
Expand All @@ -25,6 +26,7 @@ public function editSingleton()

Session::put('pages_back_link', url()->current());

View::share('form', $this->form($item->id));
return view("admin.{$this->moduleName}.form", $this->form($item->id));
}
}
6 changes: 5 additions & 1 deletion src/Services/Blocks/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace A17\Twill\Services\Blocks;

use Exception;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Str;

class Block
Expand Down Expand Up @@ -365,12 +366,15 @@ public function getFileName()
*/
public function render()
{
return BladeCompiler::render(
View::share('TwillUntilConsumed', ['renderForBlocks' => true]);
$block = BladeCompiler::render(
self::removeSpecialBladeTags($this->contents),
[
'renderForBlocks' => true,
]
);
View::share('TwillUntilConsumed', []);
return $block;
}

/**
Expand Down
49 changes: 45 additions & 4 deletions src/TwillServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace A17\Twill;

use A17\Twill\View\Components\BlockEditor;
use A17\Twill\View\Components\Checkbox;
use Exception;
use A17\Twill\Commands\BlockMake;
use A17\Twill\Commands\Build;
Expand Down Expand Up @@ -36,6 +38,7 @@
use Cartalyst\Tags\TagsServiceProvider;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -119,6 +122,11 @@ public function register()
$this->registerProviders();
$this->registerAliases();

// Only works as of laravel 7.
if (self::supportsBladeComponents()) {
Blade::componentNamespace('A17\\Twill\\View\\Components', 'twill');
}

Relation::morphMap([
'users' => User::class,
'media' => Media::class,
Expand All @@ -129,6 +137,10 @@ public function register()
config(['twill.version' => $this->version()]);
}

public static function supportsBladeComponents(): bool {
return (int)explode('.', app()->version())[0] >= 8;
}

/**
* Registers the package service providers.
*
Expand Down Expand Up @@ -169,7 +181,6 @@ private function registerAliases()
if (config('twill.enabled.file-library')) {
$loader->alias('FileService', FileService::class);
}

}

/**
Expand Down Expand Up @@ -333,14 +344,46 @@ private function registerCommands()
* @param string $expression
* @return string
*/
private function includeView($view, $expression)
private function includeView($view, $expression): string
{
[$name] = str_getcsv($expression, ',', '\'');

$partialNamespace = view()->exists('admin.' . $view . $name) ? 'admin.' : 'twill::';

$view = $partialNamespace . $view . $name;

if (
self::supportsBladeComponents() &&
class_exists(Blade::getClassComponentNamespaces()['twill'] . '\\' . Str::studly($name))
) {
$expression = explode(',', $expression);
array_shift($expression);
$expression = implode(',', $expression);
if ($expression === "") {
$expression = '[]';
}
$expression = str_replace("'", "\\'", $expression);

$php = '<?php' . PHP_EOL;
$php .= "\$data = eval('return $expression;');";
$php .= '$attributes = "";';
$php .= 'foreach(array_keys($data) as $attribute) {';
$php .= ' $attributes .= " :$attribute=\'$" . $attribute . "\'";';
$php .= '}' . PHP_EOL;
$php .= 'if ($renderForBlocks ?? false) {';
$php .= ' $attributes .= " :render-for-blocks=\'true\'";';
$php .= '}';
$php .= 'if ($renderForModal ?? false) {';
$php .= ' $attributes .= " :render-for-modal=\'true\'";';
$php .= '}';
$php .= '$name = "' . $name . '";';
$php .= 'echo Blade::render("<x-twill::$name $attributes />", $data); ?>';

return $php;
}

// Legacy behaviour.
// @TODO: Not sure if we should keep this.
$expression = explode(',', $expression);
array_shift($expression);
$expression = "(" . implode(',', $expression) . ")";
Expand Down Expand Up @@ -374,7 +417,6 @@ private function extendBlade()
});

$blade->directive('partialView', function ($expression) {

$expressionAsArray = str_getcsv($expression, ',', '\'');

[$moduleName, $viewName] = $expressionAsArray;
Expand Down Expand Up @@ -432,7 +474,6 @@ private function extendBlade()
$blade->aliasComponent('twill::partials.form.utils._connected_fields', 'formConnectedFields');
$blade->aliasComponent('twill::partials.form.utils._inline_checkboxes', 'formInlineCheckboxes');
}

}

/**
Expand Down
51 changes: 51 additions & 0 deletions src/View/Components/BlockEditor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace A17\Twill\View\Components;

use Illuminate\Support\Str;

class BlockEditor extends TwillFormComponent
{
public $blocks = [];
public $groups = [];
public $group;
public $allowedBlocks;
public $title;
public $trigger;
public $withoutSeparator;

public function __construct(
$label = null,
$name = 'default',
$renderForBlocks = false,
$renderForModal = false,
$trigger = null,
$title = null,
$blocks = [],
$groups = [],
$group = null,
$withoutSeparator = false
) {
parent::__construct($name, $label, $renderForBlocks, $renderForModal);
$this->trigger = $trigger ?? $label ?? twillTrans('twill::lang.fields.block-editor.add-content');
$this->blocks = $blocks;
$this->groups = $groups;
$this->group = $group;
$this->allowedBlocks = generate_list_of_available_blocks(
$this->blocks ?? null,
$this->group ?? $this->groups ?? null
);
$this->title = $title ?? Str::title($name);
$this->withoutSeparator = $withoutSeparator;
}

public function render()
{
return view('twill::partials.form._block_editor', [
'editorName' => [
'label' => $this->title,
'value' => $this->name,
],
]);
}
}
94 changes: 94 additions & 0 deletions src/View/Components/Browser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace A17\Twill\View\Components;

use Illuminate\Support\Str;

class Browser extends TwillFormComponent
{
public $moduleName;
public $modules;
public $endpoints;
public $max;
public $note;
public $fieldNote;
public $browserNote;
public $itemLabel;
public $buttonOnTop;
public $wide;
public $sortable;
public $endpoint;
public $routePrefix;
public $params;

public function __construct(
$label,
$name = null,
$renderForBlocks = false,
$renderForModal = false,
$moduleName = null,
$modules = [],
$endpoints = [],
$endpoint = null,
$max = 1,
$note = null,
$fieldNote = null,
$browserNote = null,
$itemLabel = null,
$buttonOnTop = false,
$wide = false,
$sortable = true,
$routePrefix = null,
$params = []
) {
$name = $name ?? $moduleName;
parent::__construct($name, $label, $renderForBlocks, $renderForModal);
$this->name = $name;
$this->moduleName = $moduleName;
$this->modules = $modules;
$this->endpoints = $endpoints;
$this->endpoint = $endpoint;
$this->max = $max;
$this->note = $note;
$this->fieldNote = $fieldNote;
$this->browserNote = $browserNote;
$this->itemLabel = $itemLabel;
$this->buttonOnTop = $buttonOnTop;
$this->wide = $wide;
$this->sortable = $sortable;
$this->routePrefix = $routePrefix;
$this->params = $params;

$endpointsFromModules = isset($this->modules) ? collect($this->modules)->map(function ($module) {
return [
'label' => $module['label'] ?? ucfirst($module['name']),
'value' => moduleRoute(
$module['name'],
$module['routePrefix'] ?? null,
'browser',
$module['params'] ?? [],
false
),
];
})->toArray() : null;

$this->endpoints = $this->endpoints ?? $endpointsFromModules ?? [];
$this->endpoint = $this->endpoint ?? (!empty($endpoints) ? null : moduleRoute(
$this->moduleName,
$this->routePrefix,
'browser',
$this->params,
false
));

$this->itemLabel = $this->itemLabel ?? strtolower($this->label);

$this->note = $this->note ??
'Add' . ($this->max > 1 ? " up to {$this->max} " . $itemLabel : ' one ' . Str::singular($this->itemLabel));
}

public function render()
{
return view('twill::partials.form._browser');
}
}
45 changes: 45 additions & 0 deletions src/View/Components/Checkbox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace A17\Twill\View\Components;

class Checkbox extends TwillFormComponent
{
public $note;
public $default;
public $fieldsInModal;
public $disabled;
public $border;
public $confirmMessageText;
public $confirmTitleText;
public $requireConfirmation;

public function __construct(
$name,
$label,
$renderForBlocks = false,
$renderForModal = false,
$note = false,
$default = false,
$fieldsInModal = false,
$disabled = false,
$border = false,
$confirmMessageText = false,
$confirmTitleText = false,
$requireConfirmation = false
) {
parent::__construct($name, $label, $renderForBlocks, $renderForModal);
$this->note = $note;
$this->default = $default;
$this->fieldsInModal = $fieldsInModal;
$this->disabled = $disabled;
$this->border = $border;
$this->confirmMessageText = $confirmMessageText;
$this->confirmTitleText = $confirmTitleText;
$this->requireConfirmation = $requireConfirmation;
}

public function render()
{
return view('twill::partials.form._checkbox');
}
}

0 comments on commit a15256c

Please sign in to comment.