From e7e286058ce77245ec2e9a4f97cb5a7f359215e7 Mon Sep 17 00:00:00 2001 From: Anna Dabrowska Date: Thu, 14 Sep 2023 10:47:21 +0200 Subject: [PATCH 1/2] untested attempt on fixing #169 This hasn't been tested and might not fully work yet. But it should show what needs to be adjusted. --- action/prosemirror.php | 58 +++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 37 deletions(-) diff --git a/action/prosemirror.php b/action/prosemirror.php index 69f30c9..373b29d 100644 --- a/action/prosemirror.php +++ b/action/prosemirror.php @@ -3,6 +3,7 @@ use dokuwiki\Extension\ActionPlugin; use dokuwiki\Extension\EventHandler; use dokuwiki\Extension\Event; +use dokuwiki\plugin\gallery\classes\Options; use dokuwiki\plugin\gallery\GalleryNode; use dokuwiki\plugin\prosemirror\parser\RootNode; use dokuwiki\plugin\prosemirror\schema\Node; @@ -42,10 +43,25 @@ public function writeDefaultsToJSINFO(Event $event, $param) { global $JSINFO; - /** @var syntax_plugin_gallery $syntax */ - $syntax = plugin_load('syntax', 'gallery'); - $defaults = $syntax->getDataFromParams($syntax->getConf('options')); - $attributes = $this->cleanAttributes($defaults); + $options = new Options(); + $defaults = [ + 'thumbnailsize' => $options->thumbnailWidth . 'x' . $options->thumbnailHeight, + 'imagesize' => $options->lightboxWidth . 'X' . $options->lightboxHeight, + 'cache' => $options->cache, + 'filter' => $options->filter, + 'showname' => $options->showname, + 'showtitle' => $options->showtitle, + 'crop' => $options->crop, + 'direct' => $options->direct, + 'reverse' => $options->reverse, + 'recursive' => $options->recursive, + 'align' => $options->align, + 'cols' => $options->columns, + 'limit' => $options->limit, + 'offset' => $options->offset, + 'paginate' => $options->paginate, + 'sort' => $options->sort, + ]; if (!isset($JSINFO['plugins'])) { $JSINFO['plugins'] = []; @@ -53,7 +69,7 @@ public function writeDefaultsToJSINFO(Event $event, $param) $JSINFO['plugins']['gallery'] = [ 'defaults' => array_map(function ($default) { return ['default' => $default,]; - }, $attributes), + }, $defaults), ]; $JSINFO['plugins']['gallery']['defaults']['namespace'] = ['default' => '']; } @@ -82,7 +98,6 @@ public function renderFromInstructions(Event $event, $param) // FIXME we may have to parse the namespace from the original syntax ? $data = $event->data['data']; $ns = $data['ns']; - $data = $this->cleanAttributes($data); if (cleanID($ns) === $ns) { $ns = ':' . $ns; @@ -94,37 +109,6 @@ public function renderFromInstructions(Event $event, $param) $event->data['renderer']->nodestack->add($node); } - /** - * Slightly rewrite the attributes to the format expected by our schema - * - * @param $data - * - * @return mixed - */ - public function cleanAttributes($data) - { - $data['thumbnailsize'] = $data['tw'] . 'x' . $data['th']; - $data['imagesize'] = $data['iw'] . 'X' . $data['ih']; - if ($data['random']) { - $data['sort'] = 'random'; - } else { - $data['sort'] .= 'sort'; - } - - if ($data['align'] === 1) { - $data['align'] = 'right'; - } elseif ($data['align'] === 2) { - $data['align'] = 'left'; - } else { - $data['align'] = 'center'; - } - - unset($data['tw'], $data['th'], $data['iw'], $data['ih'], $data['random']); - unset($data['ns'], $data['galid']); - - return $data; - } - /** * Render our syntax instructions for prosemirror * From bee18dfe28f3d8b41bc7d8543d96dfcf1a9218f4 Mon Sep 17 00:00:00 2001 From: Anna Dabrowska Date: Tue, 12 Sep 2023 20:56:18 +0200 Subject: [PATCH 2/2] More fixes for prosemirror integration --- GalleryNode.php | 10 +++--- action/prosemirror.php | 82 ++++++++++++++++++++++++++++++------------ 2 files changed, 64 insertions(+), 28 deletions(-) diff --git a/GalleryNode.php b/GalleryNode.php index 09d82e0..5857efc 100644 --- a/GalleryNode.php +++ b/GalleryNode.php @@ -4,6 +4,9 @@ use dokuwiki\plugin\prosemirror\parser\Node; +/** + * Gallery Node in Prosemirror editor + */ class GalleryNode extends Node { protected $parent; @@ -30,12 +33,9 @@ public function __construct($data, Node $parent) */ public function toSyntax() { - /** @var syntax_plugin_gallery $syntax */ - $syntax = plugin_load('syntax', 'gallery'); - $defaults = $syntax->getDataFromParams($syntax->getConf('options')); - /** @var action_plugin_gallery_prosemirror $action */ + /** @var \action_plugin_gallery_prosemirror $action */ $action = plugin_load('action', 'gallery_prosemirror'); - $defaults = $action->cleanAttributes($defaults); + $defaults = $action->getDefaults(); $query = []; $attrs = $this->data['attrs']; if ($attrs['thumbnailsize'] !== $defaults['thumbnailsize']) { diff --git a/action/prosemirror.php b/action/prosemirror.php index 373b29d..316241a 100644 --- a/action/prosemirror.php +++ b/action/prosemirror.php @@ -43,25 +43,7 @@ public function writeDefaultsToJSINFO(Event $event, $param) { global $JSINFO; - $options = new Options(); - $defaults = [ - 'thumbnailsize' => $options->thumbnailWidth . 'x' . $options->thumbnailHeight, - 'imagesize' => $options->lightboxWidth . 'X' . $options->lightboxHeight, - 'cache' => $options->cache, - 'filter' => $options->filter, - 'showname' => $options->showname, - 'showtitle' => $options->showtitle, - 'crop' => $options->crop, - 'direct' => $options->direct, - 'reverse' => $options->reverse, - 'recursive' => $options->recursive, - 'align' => $options->align, - 'cols' => $options->columns, - 'limit' => $options->limit, - 'offset' => $options->offset, - 'paginate' => $options->paginate, - 'sort' => $options->sort, - ]; + $defaults = $this->getDefaults(); if (!isset($JSINFO['plugins'])) { $JSINFO['plugins'] = []; @@ -88,22 +70,24 @@ public function writeDefaultsToJSINFO(Event $event, $param) */ public function renderFromInstructions(Event $event, $param) { - if ($event->data['name'] !== 'gallery') { + if ($event->data['name'] !== 'gallery_main') { return; } $event->preventDefault(); $event->stopPropagation(); $node = new Node('dwplugin_gallery'); - // FIXME we may have to parse the namespace from the original syntax ? $data = $event->data['data']; - $ns = $data['ns']; + // FIXME source can be something other than namespace + [$ns, $options] = $data; if (cleanID($ns) === $ns) { $ns = ':' . $ns; } $node->attr('namespace', $ns); - foreach ($data as $name => $value) { + + $attrs = $this->optionsToAttrs($options); + foreach ($attrs as $name => $value) { $node->attr($name, $value); } $event->data['renderer']->nodestack->add($node); @@ -156,4 +140,56 @@ public function renderAttributesToHTML(Event $event, $param) $html = p_render('xhtml', p_get_instructions($syntax), $info); echo $html; } + + /** + * Get default node attributes from gallery Options object + * + * @return array + */ + public function getDefaults(): array + { + $options = new Options(); + + return [ + 'thumbnailsize' => $options->thumbnailWidth . 'x' . $options->thumbnailHeight, + 'imagesize' => $options->lightboxWidth . 'X' . $options->lightboxHeight, + 'cache' => $options->cache, + 'filter' => $options->filter, + 'showname' => $options->showname, + 'showtitle' => $options->showtitle, + 'crop' => $options->crop, + 'direct' => $options->direct, + 'reverse' => $options->reverse, + 'recursive' => $options->recursive, + 'align' => $options->align, + 'cols' => $options->columns, + 'limit' => $options->limit, + 'offset' => $options->offset, + 'paginate' => $options->paginate, + 'sort' => $options->sort, + ]; + } + + /** + * Convert gallery options to node attributes + * + * @param Options $options + * @return array + */ + protected function optionsToAttrs($options) + { + $attrs = (array)$options; + + $attrs['thumbnailsize'] = $options->thumbnailWidth . 'x' . $options->thumbnailHeight; + $attrs['imagesize'] = $options->lightboxWidth . 'X' . $options->lightboxHeight; + $attrs['cols'] = $options->columns; + + unset($attrs['thumbnailWidth']); + unset($attrs['thumbnailHeight']); + unset($attrs['lightboxWidth']); + unset($attrs['lightboxHeight']); + unset($attrs['columns']); + + return $attrs; + } }