Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

Commit

Permalink
Merge pull request #196 from zikula-modules/external-plugin-collections
Browse files Browse the repository at this point in the history
Overhaul event handling for external editor plugins
  • Loading branch information
Guite authored Jun 25, 2020
2 parents a1f997d + 04b4b96 commit 88033e1
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 168 deletions.
2 changes: 1 addition & 1 deletion Collection/HelperCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Zikula\ScribiteModule\Collection;

/**
* This class is used as the subject of \Zikula\ScribiteModule\Event\EditorHelperEvent
* This class is used as the subject of `\Zikula\ScribiteModule\Event\EditorHelperEvent`.
* Any module that needs to add page assets (javascript, css) can use an event listener to automatically load their
* helper every time a Scribite editor is loaded.
*/
Expand Down
46 changes: 20 additions & 26 deletions Editor/CKEditor/Collection/PluginCollection.php
Original file line number Diff line number Diff line change
@@ -1,45 +1,40 @@
<?php

declare(strict_types=1);
/**
* Zikula Application Framework

/*
* This file is part of the Zikula package.
*
* Copyright Zikula Foundation - https://ziku.la/
*
* @copyright (c) Zikula Development Team
* @see https://ziku.la
* @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zikula\ScribiteModule\Editor\CKEditor\Collection;

use Zikula\ScribiteModule\Editor\EditorPluginCollectionInterface;

/**
* This class is used as the subject of the event 'moduleplugin.ckeditor.externalplugins'.
* Any module that needs to add *external* editor plugins can use an event listener to automatically load their
* This class is used by the `Zikula\ScribiteModule\Editor\CKEditor\LoadExternalPluginsEvent`.
* Any extension that needs to add *external* editor plugins can use an event listener to automatically load their
* helper every time a Scribite editor is loaded.
* @see http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.resourceManager.html#addExternal
* @see http://ckeditor.com/comment/47922#comment-47922
*/
class PluginCollection
class PluginCollection implements EditorPluginCollectionInterface
{
/**
* stack of plugins
* @var array
* Stack of plugins.
*/
private $plugins;

/**
* PluginCollection constructor.
*/
public function __construct()
{
$this->plugins = [];
}
private $plugins = [];

/**
* add a plugin to the stack
* @param array $plugin
* $helper must have array keys [name, path, file, img] set
* Adds a plugin to the stack.
*
* $plugin must have array keys [name, path, file, img] set.
*/
public function add(array $plugin)
public function add(array $plugin): void
{
if (isset($plugin['name'], $plugin['path'], $plugin['file'], $plugin['img'])) {
$plugin['path'] = rtrim($plugin['path'], '/') . '/'; // ensure there is a trailing slash
Expand All @@ -48,10 +43,9 @@ public function add(array $plugin)
}

/**
* get the helper stack
* @return array
* Gets the plugins stack.
*/
public function getPlugins()
public function getPlugins(): array
{
return $this->plugins;
}
Expand Down
15 changes: 3 additions & 12 deletions Editor/CKEditor/Helper/EditorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
namespace Zikula\ScribiteModule\Editor\CKEditor\Helper;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Zikula\Bundle\CoreBundle\Event\GenericEvent;
use Zikula\ScribiteModule\Editor\CKEditor\Collection\PluginCollection;
use Zikula\ScribiteModule\Editor\EditorHelperInterface;
use Zikula\ScribiteModule\Editor\EditorPluginCollectionInterface;

class EditorHelper implements EditorHelperInterface
{
Expand All @@ -25,9 +25,6 @@ class EditorHelper implements EditorHelperInterface
*/
private $dispatcher;

/**
* @param EventDispatcherInterface $dispatcher
*/
public function __construct(EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
Expand All @@ -44,14 +41,8 @@ public function getParameters(array $parameters = []): array
/**
* {@inheritdoc}
*/
public function getExternalPlugins(): array
public function getPluginCollection(): EditorPluginCollectionInterface
{
if (null === $this->dispatcher) {
throw new \RuntimeException('Dispatcher has not been set.');
}
$event = new GenericEvent(new PluginCollection());
$plugins = $this->dispatcher->dispatch($event, 'moduleplugin.ckeditor.externalplugins')->getSubject()->getPlugins();

return $plugins;
return new PluginCollection();
}
}
5 changes: 2 additions & 3 deletions Editor/EditorHelperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ interface EditorHelperInterface
public function getParameters(): array;

/**
* An array of parameters which will be added to the template within the
* property `externalEditorPlugins`
* A collection containing definitions for external editor plugins
*/
public function getExternalPlugins(): array;
public function getPluginCollection(): EditorPluginCollectionInterface;
}
28 changes: 28 additions & 0 deletions Editor/EditorPluginCollectionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Zikula package.
*
* Copyright Zikula Foundation - https://ziku.la/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zikula\ScribiteModule\Editor;

interface EditorPluginCollectionInterface
{
/**
* Add a plugin to the stack.
* Array structure depends on the editor's requirements.
*/
public function add(array $plugin): void;

/**
* Gets the plugins stack.
*/
public function getPlugins(): array;
}
6 changes: 5 additions & 1 deletion Editor/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Zikula\ScribiteModule\Collection\HelperCollection;
use Zikula\ScribiteModule\Collector\EditorCollector;
use Zikula\ScribiteModule\Event\EditorHelperEvent;
use Zikula\ScribiteModule\Event\LoadExternalPluginsEvent;
use Zikula\ScribiteModule\Helper\AssetHelper;
use Zikula\ThemeModule\Api\ApiInterface\PageAssetApiInterface;

Expand Down Expand Up @@ -115,7 +116,10 @@ public function load($moduleName)
throw new \RuntimeException(sprintf('%s must implement %s', get_class($editorHelper), EditorHelperInterface::class));
}
$additionalEditorParameters = $editorHelper->getParameters();
$additionalExternalEditorPlugins = $editorHelper->getExternalPlugins();
/** @var EditorPluginCollectionInterface $additionalExternalEditorPlugins */
$additionalExternalEditorPlugins = $editorHelper->getPluginCollection();
$event = new LoadExternalPluginsEvent($additionalExternalEditorPlugins, $editorId);
$additionalExternalEditorPlugins = $this->dispatcher->dispatch($event)->getPluginCollection()->getPlugins();
}

// assign disabled textareas to template as a javascript array
Expand Down
46 changes: 20 additions & 26 deletions Editor/Quill/Collection/PluginCollection.php
Original file line number Diff line number Diff line change
@@ -1,55 +1,49 @@
<?php

declare(strict_types=1);
/**
* Zikula Application Framework

/*
* This file is part of the Zikula package.
*
* Copyright Zikula Foundation - https://ziku.la/
*
* @copyright (c) Zikula Development Team
* @see https://ziku.la
* @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zikula\ScribiteModule\Editor\Quill\Collection;

use Zikula\ScribiteModule\Editor\EditorPluginCollectionInterface;

/**
* This class is used as the subject of the event 'moduleplugin.quill.externalplugins'.
* Any module that needs to add *external* editor plugins can use an event listener to automatically load their
* This class is used by the `Zikula\ScribiteModule\Editor\Quill\LoadExternalPluginsEvent`.
* Any extension that needs to add *external* editor plugins can use an event listener to automatically load their
* helper every time a Scribite editor is loaded.
* @see https://quilljs.com/docs/modules/
*/
class PluginCollection
class PluginCollection implements EditorPluginCollectionInterface
{
/**
* stack of plugins
* @var array
* Stack of plugins.
*/
private $plugins;

/**
* PluginCollection constructor.
*/
public function __construct()
{
$this->plugins = [];
}
private $plugins = [];

/**
* add a plugin to the stack
* @param array $plugin
* $helper must have array keys [name, path] set
* Adds a plugin to the stack.
*
* $plugin must have array keys [name, path] set.
*/
public function add(array $plugin)
public function add(array $plugin): void
{
if (isset($plugin['name'], $plugin['path'])) {
$this->plugins[] = $plugin;
}
}

/**
* get the helper stack
* @return array
* Gets the plugins stack.
*/
public function getPlugins()
public function getPlugins(): array
{
return $this->plugins;
}
Expand Down
16 changes: 4 additions & 12 deletions Editor/Quill/Helper/EditorHelper.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Zikula package.
*
Expand All @@ -13,8 +14,8 @@
namespace Zikula\ScribiteModule\Editor\Quill\Helper;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Zikula\Bundle\CoreBundle\Event\GenericEvent;
use Zikula\ScribiteModule\Editor\EditorHelperInterface;
use Zikula\ScribiteModule\Editor\EditorPluginCollectionInterface;
use Zikula\ScribiteModule\Editor\Quill\Collection\PluginCollection;

class EditorHelper implements EditorHelperInterface
Expand All @@ -24,9 +25,6 @@ class EditorHelper implements EditorHelperInterface
*/
private $dispatcher;

/**
* @param EventDispatcherInterface $dispatcher
*/
public function __construct(EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
Expand All @@ -43,14 +41,8 @@ public function getParameters(array $parameters = []): array
/**
* {@inheritdoc}
*/
public function getExternalPlugins(): array
public function getPluginCollection(): EditorPluginCollectionInterface
{
if (null === $this->dispatcher) {
throw new \RuntimeException('Dispatcher has not been set.');
}
$event = new GenericEvent(new PluginCollection());
$plugins = $this->dispatcher->dispatch($event, 'moduleplugin.quill.externalplugins')->getSubject()->getPlugins();

return $plugins;
return new PluginCollection();
}
}
46 changes: 20 additions & 26 deletions Editor/Summernote/Collection/PluginCollection.php
Original file line number Diff line number Diff line change
@@ -1,55 +1,49 @@
<?php

declare(strict_types=1);
/**
* Zikula Application Framework

/*
* This file is part of the Zikula package.
*
* Copyright Zikula Foundation - https://ziku.la/
*
* @copyright (c) Zikula Development Team
* @see https://ziku.la
* @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zikula\ScribiteModule\Editor\Summernote\Collection;

use Zikula\ScribiteModule\Editor\EditorPluginCollectionInterface;

/**
* This class is used as the subject of the event 'moduleplugin.summernote.externalplugins'.
* Any module that needs to add *external* editor plugins can use an event listener to automatically load their
* This class is used by the `Zikula\ScribiteModule\Editor\Summernote\LoadExternalPluginsEvent`.
* Any extension that needs to add *external* editor plugins can use an event listener to automatically load their
* helper every time a Scribite editor is loaded.
* @see http://summernote.org/deep-dive/#module-system
*/
class PluginCollection
class PluginCollection implements EditorPluginCollectionInterface
{
/**
* stack of plugins
* @var array
* Stack of plugins.
*/
private $plugins;

/**
* PluginCollection constructor.
*/
public function __construct()
{
$this->plugins = [];
}
private $plugins = [];

/**
* add a plugin to the stack
* @param array $plugin
* $helper must have array keys [name, path] set
* Adds a plugin to the stack.
*
* $plugin must have array keys [name, path] set.
*/
public function add(array $plugin)
public function add(array $plugin): void
{
if (isset($plugin['name'], $plugin['path'])) {
$this->plugins[] = $plugin;
}
}

/**
* get the helper stack
* @return array
* Gets the plugins stack.
*/
public function getPlugins()
public function getPlugins(): array
{
return $this->plugins;
}
Expand Down
Loading

0 comments on commit 88033e1

Please sign in to comment.