Skip to content

Commit ca52d7c

Browse files
authored
Merge pull request #170 from splitbrain/pmfix
Fixes #169
2 parents 91c916b + bee18df commit ca52d7c

File tree

2 files changed

+66
-46
lines changed

2 files changed

+66
-46
lines changed

GalleryNode.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use dokuwiki\plugin\prosemirror\parser\Node;
66

7+
/**
8+
* Gallery Node in Prosemirror editor
9+
*/
710
class GalleryNode extends Node
811
{
912
protected $parent;
@@ -30,12 +33,9 @@ public function __construct($data, Node $parent)
3033
*/
3134
public function toSyntax()
3235
{
33-
/** @var syntax_plugin_gallery $syntax */
34-
$syntax = plugin_load('syntax', 'gallery');
35-
$defaults = $syntax->getDataFromParams($syntax->getConf('options'));
36-
/** @var action_plugin_gallery_prosemirror $action */
36+
/** @var \action_plugin_gallery_prosemirror $action */
3737
$action = plugin_load('action', 'gallery_prosemirror');
38-
$defaults = $action->cleanAttributes($defaults);
38+
$defaults = $action->getDefaults();
3939
$query = [];
4040
$attrs = $this->data['attrs'];
4141
if ($attrs['thumbnailsize'] !== $defaults['thumbnailsize']) {

action/prosemirror.php

Lines changed: 61 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use dokuwiki\Extension\ActionPlugin;
44
use dokuwiki\Extension\EventHandler;
55
use dokuwiki\Extension\Event;
6+
use dokuwiki\plugin\gallery\classes\Options;
67
use dokuwiki\plugin\gallery\GalleryNode;
78
use dokuwiki\plugin\prosemirror\parser\RootNode;
89
use dokuwiki\plugin\prosemirror\schema\Node;
@@ -42,18 +43,15 @@ public function writeDefaultsToJSINFO(Event $event, $param)
4243
{
4344
global $JSINFO;
4445

45-
/** @var syntax_plugin_gallery $syntax */
46-
$syntax = plugin_load('syntax', 'gallery');
47-
$defaults = $syntax->getDataFromParams($syntax->getConf('options'));
48-
$attributes = $this->cleanAttributes($defaults);
46+
$defaults = $this->getDefaults();
4947

5048
if (!isset($JSINFO['plugins'])) {
5149
$JSINFO['plugins'] = [];
5250
}
5351
$JSINFO['plugins']['gallery'] = [
5452
'defaults' => array_map(function ($default) {
5553
return ['default' => $default,];
56-
}, $attributes),
54+
}, $defaults),
5755
];
5856
$JSINFO['plugins']['gallery']['defaults']['namespace'] = ['default' => ''];
5957
}
@@ -72,59 +70,29 @@ public function writeDefaultsToJSINFO(Event $event, $param)
7270
*/
7371
public function renderFromInstructions(Event $event, $param)
7472
{
75-
if ($event->data['name'] !== 'gallery') {
73+
if ($event->data['name'] !== 'gallery_main') {
7674
return;
7775
}
7876
$event->preventDefault();
7977
$event->stopPropagation();
8078

8179
$node = new Node('dwplugin_gallery');
82-
// FIXME we may have to parse the namespace from the original syntax ?
8380
$data = $event->data['data'];
84-
$ns = $data['ns'];
85-
$data = $this->cleanAttributes($data);
81+
// FIXME source can be something other than namespace
82+
[$ns, $options] = $data;
8683

8784
if (cleanID($ns) === $ns) {
8885
$ns = ':' . $ns;
8986
}
9087
$node->attr('namespace', $ns);
91-
foreach ($data as $name => $value) {
88+
89+
$attrs = $this->optionsToAttrs($options);
90+
foreach ($attrs as $name => $value) {
9291
$node->attr($name, $value);
9392
}
9493
$event->data['renderer']->nodestack->add($node);
9594
}
9695

97-
/**
98-
* Slightly rewrite the attributes to the format expected by our schema
99-
*
100-
* @param $data
101-
*
102-
* @return mixed
103-
*/
104-
public function cleanAttributes($data)
105-
{
106-
$data['thumbnailsize'] = $data['tw'] . 'x' . $data['th'];
107-
$data['imagesize'] = $data['iw'] . 'X' . $data['ih'];
108-
if ($data['random']) {
109-
$data['sort'] = 'random';
110-
} else {
111-
$data['sort'] .= 'sort';
112-
}
113-
114-
if ($data['align'] === 1) {
115-
$data['align'] = 'right';
116-
} elseif ($data['align'] === 2) {
117-
$data['align'] = 'left';
118-
} else {
119-
$data['align'] = 'center';
120-
}
121-
122-
unset($data['tw'], $data['th'], $data['iw'], $data['ih'], $data['random']);
123-
unset($data['ns'], $data['galid']);
124-
125-
return $data;
126-
}
127-
12896
/**
12997
* Render our syntax instructions for prosemirror
13098
*
@@ -172,4 +140,56 @@ public function renderAttributesToHTML(Event $event, $param)
172140
$html = p_render('xhtml', p_get_instructions($syntax), $info);
173141
echo $html;
174142
}
143+
144+
/**
145+
* Get default node attributes from gallery Options object
146+
*
147+
* @return array
148+
*/
149+
public function getDefaults(): array
150+
{
151+
$options = new Options();
152+
153+
return [
154+
'thumbnailsize' => $options->thumbnailWidth . 'x' . $options->thumbnailHeight,
155+
'imagesize' => $options->lightboxWidth . 'X' . $options->lightboxHeight,
156+
'cache' => $options->cache,
157+
'filter' => $options->filter,
158+
'showname' => $options->showname,
159+
'showtitle' => $options->showtitle,
160+
'crop' => $options->crop,
161+
'direct' => $options->direct,
162+
'reverse' => $options->reverse,
163+
'recursive' => $options->recursive,
164+
'align' => $options->align,
165+
'cols' => $options->columns,
166+
'limit' => $options->limit,
167+
'offset' => $options->offset,
168+
'paginate' => $options->paginate,
169+
'sort' => $options->sort,
170+
];
171+
}
172+
173+
/**
174+
* Convert gallery options to node attributes
175+
*
176+
* @param Options $options
177+
* @return array
178+
*/
179+
protected function optionsToAttrs($options)
180+
{
181+
$attrs = (array)$options;
182+
183+
$attrs['thumbnailsize'] = $options->thumbnailWidth . 'x' . $options->thumbnailHeight;
184+
$attrs['imagesize'] = $options->lightboxWidth . 'X' . $options->lightboxHeight;
185+
$attrs['cols'] = $options->columns;
186+
187+
unset($attrs['thumbnailWidth']);
188+
unset($attrs['thumbnailHeight']);
189+
unset($attrs['lightboxWidth']);
190+
unset($attrs['lightboxHeight']);
191+
unset($attrs['columns']);
192+
193+
return $attrs;
194+
}
175195
}

0 commit comments

Comments
 (0)