1<?php 2 3namespace dokuwiki\plugin\prosemirror\parser; 4 5class TableCellNode extends Node 6{ 7 protected $data; 8 9 /** @var Node[] */ 10 protected $subnodes; 11 12 public function __construct($data, Node $parent = null) 13 { 14 if (empty($data['content'])) { 15 $data['content'] = [ 16 [ 17 'type' => 'text', 18 'text' => ' ', 19 ] 20 ]; 21 } 22 $this->data = $data; 23 24 $previousNode = null; 25 foreach ($data['content'] as $nodeData) { 26 try { 27 $newNode = static::getSubNode($nodeData, $this, $previousNode); 28 } catch (\Throwable $e) { 29 var_dump($nodeData); 30 throw $e; 31 } 32 $this->subnodes[] = $newNode; 33 $previousNode = $newNode; 34 } 35 } 36 37 public function toSyntax() 38 { 39 $prefix = '|'; 40 if ($this->isHeaderCell()) { 41 $prefix = '^'; 42 } 43 $doc = ''; 44 foreach ($this->subnodes as $subnode) { 45 $doc .= $subnode->toSyntax(); 46 } 47 [$paddingLeft, $paddingRight] = $this->calculateAlignmentPadding(); 48 return $prefix . $paddingLeft . trim($doc) . $paddingRight; 49 } 50 51 public function isHeaderCell() 52 { 53 return $this->data['type'] === 'table_header'; 54 } 55 56 public function getRowSpan() 57 { 58 return $this->data['attrs']['rowspan']; 59 } 60 61 public function getColSpan() 62 { 63 return $this->data['attrs']['colspan']; 64 } 65 66 /** 67 * Calculate the correct padding to align cell-content left, right or center 68 * 69 * @return String[] [left padding, right padding] 70 */ 71 protected function calculateAlignmentPadding() 72 { 73 if (empty($this->data['attrs']['align'])) { 74 return [' ', ' ']; 75 } 76 if ($this->data['attrs']['align'] === 'right') { 77 return [' ', ' ']; 78 } 79 if ($this->data['attrs']['align'] === 'center') { 80 return [' ', ' ']; 81 } 82 if ($this->data['attrs']['align'] === 'left') { 83 return [' ', ' ']; 84 } 85 return [' ', ' ']; 86 } 87} 88