Skip to content

Commit

Permalink
1.8.0: Added support to override block views from theme partials. Fix…
Browse files Browse the repository at this point in the history
…ed StaticPage support, fixed Rainlab.Blog support.
  • Loading branch information
FlusherDock1 committed Feb 22, 2022
1 parent 382dc9c commit 5852aec
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 91 deletions.
53 changes: 25 additions & 28 deletions Plugin.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?php namespace ReaZzon\Editor;

use Backend, Event;
use System\Classes\PluginBase;
use ReaZzon\Editor\Console\RefreshStaticPages;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Response;
use ReaZzon\Editor\Classes\Event\ProcessMLFields;
use ReaZzon\Editor\Classes\Exceptions\PluginErrorException;
use System\Classes\PluginBase;

use ReaZzon\Editor\Behaviors\ConvertToHtml;
use ReaZzon\Editor\Classes\Event\ExtendRainLabBlog;
use ReaZzon\Editor\Classes\Event\ExtendRainLabStaticPages;
use ReaZzon\Editor\Classes\Event\ExtendIndicatorNews;
use ReaZzon\Editor\Classes\Event\ExtendLovataGoodNews;
use ReaZzon\Editor\Behaviors\ConvertToHtml;
use ReaZzon\Editor\Classes\Event\ExtendRainLabStaticPages;

/**
* Editor Plugin Information File
Expand All @@ -28,11 +28,11 @@ class Plugin extends PluginBase
public function pluginDetails()
{
return [
'name' => 'reazzon.editor::lang.plugin.name',
'name' => 'reazzon.editor::lang.plugin.name',
'description' => 'reazzon.editor::lang.plugin.description',
'author' => 'Nick Khaetsky',
'icon' => 'icon-pencil-square-o',
'homepage' => 'https://github.com/FlusherDock1/EditorJS'
'author' => 'Nick Khaetsky',
'icon' => 'icon-pencil-square-o',
'homepage' => 'https://github.com/FlusherDock1/EditorJS'
];
}

Expand All @@ -41,8 +41,7 @@ public function pluginDetails()
*/
public function register()
{
parent::register(); // TODO: Change the autogenerated stub

$this->registerConsoleCommand('editor:refresh.static-pages', RefreshStaticPages::class);
$this->registerErrorHandler();
}

Expand All @@ -53,8 +52,6 @@ public function register()
*/
public function boot()
{
parent::boot();

Event::subscribe(ExtendRainLabBlog::class);
Event::subscribe(ExtendRainLabStaticPages::class);
Event::subscribe(ExtendLovataGoodNews::class);
Expand All @@ -71,7 +68,7 @@ public function registerPermissions()
{
return [
'reazzon.editor.access_settings' => [
'tab' => 'reazzon.editor::lang.plugin.name',
'tab' => 'reazzon.editor::lang.plugin.name',
'label' => 'reazzon.editor::lang.permission.access_settings'
],
];
Expand All @@ -86,13 +83,13 @@ public function registerSettings()
{
return [
'settings' => [
'label' => 'reazzon.editor::lang.settings.menu_label',
'label' => 'reazzon.editor::lang.settings.menu_label',
'description' => 'reazzon.editor::lang.settings.menu_description',
'category' => 'reazzon.editor::lang.plugin.name',
'class' => 'ReaZzon\Editor\Models\Settings',
'category' => 'reazzon.editor::lang.plugin.name',
'class' => 'ReaZzon\Editor\Models\Settings',
'permissions' => ['reazzon.editor.access_settings'],
'icon' => 'icon-cog',
'order' => 500,
'icon' => 'icon-cog',
'order' => 500,
]
];
}
Expand Down Expand Up @@ -120,7 +117,8 @@ public function registerMarkupTags()
];
}

public function convertJsonToHtml($field) {
public function convertJsonToHtml($field)
{
return (new ConvertToHtml)->convertJsonToHtml($field);
}

Expand Down Expand Up @@ -185,8 +183,8 @@ public function registerEditorBlocks()
'class' => 'ImageTool',
'config' => [
'endpoints' => [
'byFile' => config('app.url').'/editorjs/plugins/image/uploadFile',
'byUrl' => config('app.url').'/editorjs/plugins/image/fetchUrl',
'byFile' => config('app.url') . '/editorjs/plugins/image/uploadFile',
'byUrl' => config('app.url') . '/editorjs/plugins/image/fetchUrl',
]
]
],
Expand Down Expand Up @@ -230,7 +228,7 @@ public function registerEditorBlocks()
'settings' => [
'class' => 'AttachesTool',
'config' => [
'endpoint' => config('app.url').'/editorjs/plugins/attaches',
'endpoint' => config('app.url') . '/editorjs/plugins/attaches',
]
],
'validation' => [
Expand Down Expand Up @@ -496,12 +494,11 @@ public function registerEditorBlocks()
*/
private function registerErrorHandler(): void
{
\App::error(function(PluginErrorException $exception) {
return app(ResponseFactory::class)
->make(
$exception->render(),
$exception->getCode()
);
\App::error(function (PluginErrorException $exception) {
return app(ResponseFactory::class)->make(
$exception->render(),
$exception->getCode()
);
});
}
}
62 changes: 51 additions & 11 deletions behaviors/ConvertToHtml.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php namespace ReaZzon\Editor\Behaviors;

use View;
use EditorJS\EditorJS;
use \Cms\Classes\Theme;
use \Cms\Classes\Partial;
use \Cms\Classes\Controller;

/**
* Class ConvertableBehavior
Expand All @@ -9,6 +13,8 @@
*/
class ConvertToHtml extends ConvertableBehavior
{
const FOLDER_NAME = 'editorjs';

/**
* @var array Settings for validation
*/
Expand All @@ -24,29 +30,63 @@ class ConvertToHtml extends ConvertableBehavior
* @param string $jsonField
* @return string
*/
public function convertJsonToHtml($jsonField)
public function convertJsonToHtml(string $jsonField): string
{
$this->getTemplatePartialsOptions();
return $this->startConverter($jsonField);
}

public static function getTemplatePartialsOptions(): array
{
$blocksPartials = [];
$theme = Theme::getEditTheme();
$partials = Partial::listInTheme($theme, true);

foreach ($partials as $partial) {
$partialFilePath = $partial->getBaseFileName();
if (strpos($partialFilePath, self::FOLDER_NAME) !== false) {
$blockName = basename($partialFilePath);
$blocksPartials[$blockName] = $partialFilePath;
}
}

return $blocksPartials;
}

public static function renderFromThemePartial($partial, $data)
{
try {
return (new Controller)->renderPartial($partial, $data);
} catch (\Exception $e) {
trace_log($e);
return false;
}
}

/**
* @param \EditorJS\EditorJS $editor
* @param EditorJS $editor
* @param array $blocks
* @return mixed|string
* @return string
*/
public function processBlocks($editor, $blocks)
public function processBlocks($editor, $blocks): string
{
$htmlOutput = '<div class="editor-js-content">';
$blocksPartials = $this->getTemplatePartialsOptions();
$htmlOutput = '';

foreach ($blocks as $block) {
$viewBlock = array_get($this->blocksViews, $block['type']);
if (!empty($viewBlock)) {
$htmlOutput .= View::make($viewBlock, $block['data']);
$blockType = strtolower($block['type']);
if (array_key_exists($blockType, $blocksPartials)) {
$htmlOutput .= $this->renderFromThemePartial($blocksPartials[$blockType], ['block' => $block]);
} else {
try {
$viewPath = array_get($this->blocksViews, $block['type']);
$htmlOutput .= View::make($viewPath, $block['data']);
} catch (\Exception $ex) {
trace_log($ex);
}
}
}

$htmlOutput .= '</div>';

return html_entity_decode($htmlOutput);
}
}
}
2 changes: 0 additions & 2 deletions behaviors/ConvertableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ public function prepareBlocks()
continue;
}



/**
* @var string $block
* @var array $section
Expand Down
6 changes: 6 additions & 0 deletions classes/event/ExtendRainLabBlog.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public function subscribe($event)
return $model->convertJsonToHtml($model->getAttribute('content'));
}
});

$model->bindEvent('model.translate.resolveComputedFields', function ($locale) use ($model) {
return [
'content_html' => $model->convertJsonToHtml($model->asExtension('TranslatableModel')->getAttributeTranslated('content', $locale))
];
});
});
}
}
Expand Down
41 changes: 0 additions & 41 deletions classes/helper/ConverterHelper.php

This file was deleted.

52 changes: 52 additions & 0 deletions console/RefreshStaticPages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php namespace ReaZzon\Editor\Console;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

/**
* RefreshStaticPages Command
*/
class RefreshStaticPages extends Command
{
/**
* @var string name is the console command name
*/
protected $name = 'editor:refresh.static-pages';

/**
* @var string description is the console command description
*/
protected $description = 'Resaving all static pages';

/**
* handle executes the console command
*/
public function handle()
{
$this->info('Start of resaving...');
$pages = \RainLab\Pages\Classes\Page::sortBy('title')->lists('title', 'baseFileName');

foreach($pages as $fileName => $fileTitle) {
$page = \RainLab\Pages\Classes\Page::loadCached(\Cms\Classes\Theme::getActiveTheme(), $fileName);
$page->save();
}
$this->info('End of resaving...');
}

/**
* getArguments get the console command arguments
*/
protected function getArguments()
{
return [];
}

/**
* getOptions get the console command options
*/
protected function getOptions()
{
return [];
}
}
2 changes: 1 addition & 1 deletion formwidgets/editorjs/assets/js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ if("function" == typeof define && define.amd) {
if (this.$textarea.val().length > 0 && this.isJson(this.$textarea.val()) === true) {
parameters.data = JSON.parse(this.$textarea.val())
}

console.log('editor.js Init')
this.$editor = new EditorJS(parameters);
}

Expand Down
2 changes: 1 addition & 1 deletion formwidgets/editorjs/assets/js/tools/table.js

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions formwidgets/mleditorjs/assets/js/mleditorjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
var MLEditor = function (element, options) {
this.options = options
this.$el = $(element)
this.parameters = null
this.$form = this.$el.closest('form')
this.$textarea = $(options.textareaElement)
this.$editor = null
this.$editorjs = $('[data-control=editorjs]:first', this.$el)
this.toolSettings = this.$el.data('settings')
this.$locale = $('[data-editor-active-locale]', this.$el)
Expand Down Expand Up @@ -72,8 +70,6 @@
this.currentLocale = locale;
this.$locale.val(this.currentLocale);

editor.clear();

let jsonData = null;

// setPrepare
Expand All @@ -85,7 +81,11 @@
}
}

if (jsonData === null) return;
if (jsonData === null || (jsonData.blocks !== undefined && jsonData.blocks.length === 0)) {
editor.clear()
return;
}

editor.blocks.render(jsonData);
}
}
Expand Down Expand Up @@ -139,4 +139,4 @@
$('[data-control="mleditorjs"]').MLEditor();
})

}(window.jQuery);
}(window.jQuery);
Loading

0 comments on commit 5852aec

Please sign in to comment.