1<?php 2 3namespace dokuwiki\plugin\prosemirror\parser; 4 5abstract class LinkNode extends Node implements InlineNodeInterface 6{ 7 8 9 /** @var InlineNodeInterface */ 10 public $previous = null; 11 12 /** @var Node */ 13 protected $parent; 14 15 /** @var TextNode */ 16 protected $textNode = null; 17 18 protected $attrs = []; 19 20 public function __construct($data, Node $parent, Node $previousNode = null) 21 { 22 $this->parent = &$parent; 23 if ($previousNode !== false) { 24 $this->previous = &$previousNode; 25 } 26 27 $this->attrs = $data['attrs']; 28 29 // every inline node needs a TextNode to track marks 30 $this->textNode = new TextNode(['marks' => $data['marks'] ?? null], $parent, $previousNode); 31 } 32 33 34 /** 35 * @param string $markType 36 */ 37 public function increaseMark($markType) 38 { 39 return $this->textNode->increaseMark($markType); 40 } 41 42 public function getStartingNodeMarkScore($markType) 43 { 44 return $this->textNode->getStartingNodeMarkScore($markType); 45 } 46 47 protected function getDefaultLinkSyntax($inner) 48 { 49 $title = ''; 50 $prefix = $this->textNode->getPrefixSyntax(); 51 $postfix = $this->textNode->getPostfixSyntax(); 52 53 if (!empty($this->attrs['data-name'])) { 54 $title = '|' . $this->attrs['data-name']; 55 } elseif (!empty($this->attrs['image-id'])) { 56 $imageAttrs = []; 57 foreach ($this->attrs as $key => $value) { 58 @list ($keyPrefix, $attrKey) = explode('-', $key, 2); 59 if ($keyPrefix === 'image') { 60 $imageAttrs[$attrKey] = $value; 61 } 62 } 63 $imageNode = new ImageNode([ 64 'attrs' => $imageAttrs, 65 'marks' => [], 66 ], $this); 67 $title = '|' . $imageNode->toSyntax(); 68 } 69 70 return $prefix . '[[' . $inner . $title . ']]' . $postfix; 71 } 72 73 /** 74 * @param \renderer_plugin_prosemirror $renderer 75 * @param string $linktype 76 * @param string|array $name 77 * @param array $additionalAttributes 78 */ 79 protected static function renderToJSON( 80 \renderer_plugin_prosemirror $renderer, 81 $linktype, 82 $inner, 83 $name, 84 $additionalAttributes = [] 85 ) { 86 global $ID; 87 $isImage = is_array($name); 88 $linkNode = new \dokuwiki\plugin\prosemirror\schema\Node('link'); 89 $linkNode->attr('data-type', $linktype); 90 $linkNode->attr('data-inner', $inner); 91 if ($isImage) { 92 ImageNode::addAttributes( 93 $linkNode, 94 $name['src'], 95 $name['title'], 96 $name['align'], 97 $name['width'], 98 $name['height'], 99 $name['cache'], 100 null, 101 'image-' 102 ); 103 $linkNode->attr('data-resolvedImage', self::resolveImageTitle( 104 $ID, 105 $name['src'], 106 $name['title'], 107 $name['align'], 108 $name['width'], 109 $name['height'], 110 $name['cache'] 111 )); 112 } else { 113 $linkNode->attr('data-name', $name); 114 } 115 foreach ($additionalAttributes as $attributeName => $attributeValue) { 116 $linkNode->attr($attributeName, $attributeValue); 117 } 118 foreach (array_keys($renderer->getCurrentMarks()) as $mark) { 119 $linkNode->addMark(new \dokuwiki\plugin\prosemirror\schema\Mark($mark)); 120 } 121 $renderer->addToNodestack($linkNode); 122 } 123 124 public static function resolveImageTitle($pageId, $imageId, $title = null, $align = null, $width = null, 125 $height = null, $cache = null) 126 { 127 /** @var \Doku_Renderer_xhtml $xhtml_renderer */ 128 $xhtml_renderer = p_get_renderer('xhtml'); 129 $src = $imageId; 130 if (!media_isexternal($src)) { 131 resolve_mediaid(getNS($pageId), $src, $exists); 132 } 133 return $xhtml_renderer->_media( 134 $src, 135 $title ?: $imageId, 136 $align, 137 $width, 138 $height, 139 $cache 140 ); 141 } 142} 143