1<?php
2
3namespace dokuwiki\plugin\prosemirror\parser;
4
5class ImageNode extends Node implements InlineNodeInterface
6{
7
8    /** @var  Node */
9    protected $parent;
10
11    protected $attrs = [];
12
13    protected $textNode = null;
14
15    public function __construct($data, Node $parent, Node $previousNode = null)
16    {
17        $this->parent = &$parent;
18        $this->attrs = $data['attrs'];
19
20        // every inline node needs a TextNode to track marks
21        $this->textNode = new TextNode(['marks' => $data['marks']], $parent, $previousNode);
22    }
23
24    public function toSyntax()
25    {
26        $title = '';
27        if ($this->attrs['title']) {
28            $title = '|' . $this->attrs['title'];
29        }
30
31        $leftAlign = '';
32        $rightAlign = '';
33        if ($this->attrs['align'] === 'left') {
34            $rightAlign = ' ';
35        } elseif ($this->attrs['align'] === 'right') {
36            $leftAlign = ' ';
37        } elseif ($this->attrs['align'] === 'center') {
38            $leftAlign = ' ';
39            $rightAlign = ' ';
40        }
41
42        $query = [];
43        if ($this->attrs['height']) {
44            $query[] = $this->attrs['width'] . 'x' . $this->attrs['height'];
45        } elseif ($this->attrs['width']) {
46            $query[] = $this->attrs['width'];
47        }
48        if ($this->attrs['linking'] && $this->attrs['linking'] !== 'details') {
49            $query[] = $this->attrs['linking'];
50        }
51        if ($this->attrs['cache'] && $this->attrs['cache'] !== 'cache') {
52            $query[] = $this->attrs['cache'];
53        }
54
55        $queryString = '';
56        if (!empty($query)) {
57            $queryString = '?' . implode('&', $query);
58        }
59
60
61        return '{{' . $leftAlign . $this->attrs['id'] . $queryString . $rightAlign . $title . '}}';
62    }
63
64    public static function render(
65        \renderer_plugin_prosemirror $renderer,
66        $src,
67        $title = null,
68        $align = null,
69        $width = null,
70        $height = null,
71        $cache = null,
72        $linking = null
73    ) {
74        $node = new \dokuwiki\plugin\prosemirror\schema\Node('image');
75
76        self::addAttributes(
77            $node,
78            $src,
79            $title,
80            $align,
81            $width,
82            $height,
83            $cache,
84            $linking
85        );
86
87        foreach (array_keys($renderer->getCurrentMarks()) as $mark) {
88            $node->addMark(new \dokuwiki\plugin\prosemirror\schema\Mark($mark));
89        }
90
91        global $ID;
92        $node->attr('data-resolvedHtml',
93            self::resolveMedia($src, $title, $align, $width, $height, $cache, $linking));
94
95        $renderer->addToNodestack($node);
96    }
97
98    public static function addAttributes(
99        \dokuwiki\plugin\prosemirror\schema\Node $node,
100        $src,
101        $title = null,
102        $align = null,
103        $width = null,
104        $height = null,
105        $cache = null,
106        $linking = null,
107        $prefix = ''
108    ) {
109        $node->attr($prefix . 'src', ml($src));
110        $node->attr($prefix . 'title', $title);
111
112        $class = 'media';
113        if ($align === 'right') {
114            $class = 'mediaright';
115        } elseif ($align === 'left') {
116            $class = 'medialeft';
117        } elseif ($align === 'center') {
118            $class = 'mediacenter';
119        }
120
121        if ($cache !== null && $cache === 'cache') {
122            $cache = null;
123        }
124
125        $node->attr($prefix . 'class', $class);
126        $node->attr($prefix . 'align', $align);
127        $node->attr($prefix . 'width', $width);
128        $node->attr($prefix . 'height', $height);
129        $node->attr($prefix . 'id', $src);
130        $node->attr($prefix . 'cache', $cache);
131        $node->attr($prefix . 'linking', $linking);
132    }
133
134    public static function resolveMedia(
135        $src,
136        $title = null,
137        $align = null,
138        $width = null,
139        $height = null,
140        $cache = null,
141        $linking = null
142    ) {
143        $xhtml_renderer = p_get_renderer('xhtml');
144        if (media_isexternal($src) || link_isinterwiki($src)) {
145            $xhtml_renderer->externalmedia(
146                $src,
147                $title ?: $src,
148                $align,
149                $width,
150                $height,
151                $cache,
152                $linking
153            );
154        } else {
155            $xhtml_renderer->internalmedia(
156                $src,
157                $title ?: $src,
158                $align,
159                $width,
160                $height,
161                $cache,
162                $linking
163            );
164        }
165        return $xhtml_renderer->doc;
166    }
167
168    /**
169     * @param string $markType
170     */
171    public function increaseMark($markType)
172    {
173        return $this->textNode->increaseMark($markType);
174    }
175
176    public function getStartingNodeMarkScore($markType)
177    {
178        return $this->textNode->getStartingNodeMarkScore($markType);
179    }
180}
181