Skip to content

Commit

Permalink
Merge pull request #22 from jd-dotlogics/feature/templates-icon
Browse files Browse the repository at this point in the history
Added option to add template icons
  • Loading branch information
mjawad096 authored May 25, 2022
2 parents 51da0c4 + da4e85c commit 6e82a2e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
8 changes: 5 additions & 3 deletions src/App/Http/Controllers/EditorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Facades\File;
use App\Http\Controllers\Controller;
use Dotlogics\Grapesjs\App\Traits\EditorTrait;
use Illuminate\Support\Facades\View;

class EditorController extends Controller
{
Expand Down Expand Up @@ -80,13 +81,14 @@ public function templates(Request $request, $model, $id)
$view_base .= '.';
}

$view = "laravel-grapesjs::{$view_base}{$file_name}";
$content = view("laravel-grapesjs::{$view_base}{$file_name}")->render();

$templates [] = [
'category' => $category,
'id' => $id_prefix . $fileInfo->getFilename(),
'category' => $category,
'label' => $file_name->replace('-', ' ')->title(),
'content' => view($view)->render(),
'media' => app('template-icon')->url(),
'content' => $content,
];
}

Expand Down
47 changes: 39 additions & 8 deletions src/GrapesjsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

namespace Dotlogics\Grapesjs;

use Illuminate\Support\ServiceProvider;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

class GrapesjsServiceProvider extends ServiceProvider
{
public $routeFilePath = '/routes/laravel-grapesjs.php';
public $confiFilePath = 'laravel-grapesjs.php';
public $publicDirPath = 'vendor/laravel-grapesjs';
public $viewDirPath = 'views/vendor/laravel-grapesjs';
public $namespace = 'laravel-grapesjs';
protected $routeFilePath = '/routes/laravel-grapesjs.php';
protected $confiFilePath = 'laravel-grapesjs.php';
protected $publicDirPath = 'vendor/laravel-grapesjs';
protected $viewDirPath = 'views/vendor/laravel-grapesjs';
protected $namespace = 'laravel-grapesjs';

/**
* Register services.
Expand All @@ -37,6 +38,8 @@ public function boot()
if ($this->app->runningInConsole()) {
$this->publishFiles();
}

$this->setupViewDirectives();
}


Expand All @@ -48,7 +51,7 @@ public function boot()
*
* @return void
*/
public function setupRoutes(Router $router)
protected function setupRoutes(Router $router)
{
// by default, use the routes file provided in vendor
$routeFilePathInUse = __DIR__.$this->routeFilePath;
Expand All @@ -61,7 +64,7 @@ public function setupRoutes(Router $router)
$this->loadRoutesFrom($routeFilePathInUse);
}

public function publishFiles()
protected function publishFiles()
{
$this->publishes([
__DIR__.'/config.php' => config_path($this->confiFilePath),
Expand All @@ -75,4 +78,32 @@ public function publishFiles()
__DIR__.'/resources/views' => resource_path($this->viewDirPath),
], [$this->namespace, 'views']);
}

protected function setupViewDirectives()
{
//To Handle error if there no icon defined for any template
$this->app->singleton('template-icon', function($app){
return new class {
public function url(){}
};
});

Blade::directive('templateIcon', function ($args) {
$args = Blade::stripParentheses($args);

return "<?php \$app->singleton('template-icon', function(\$app){
return new class {
protected \$called = false;
public function url() {
if(\$this->called) return null;
\$this->called = true;
return '<img src=\"' . url($args) . '\" style=\"max-width: 100%;max-height: 100%;\" />';
}
};
}); ?>";
});
}
}

0 comments on commit 6e82a2e

Please sign in to comment.