More fixes for prosemirror integration

This commit is contained in:
Anna Dabrowska
2023-09-12 20:56:18 +02:00
parent e7e286058c
commit bee18dfe28
2 changed files with 64 additions and 28 deletions

View File

@ -4,6 +4,9 @@ namespace dokuwiki\plugin\gallery;
use dokuwiki\plugin\prosemirror\parser\Node;
/**
* Gallery Node in Prosemirror editor
*/
class GalleryNode extends Node
{
protected $parent;
@ -30,12 +33,9 @@ class GalleryNode extends Node
*/
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']) {

View File

@ -43,25 +43,7 @@ class action_plugin_gallery_prosemirror extends ActionPlugin
{
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 @@ class action_plugin_gallery_prosemirror extends ActionPlugin
*/
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 @@ class action_plugin_gallery_prosemirror extends ActionPlugin
$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;
}
}