PluginProbe
Tableberg – Simple Gutenberg Table Block / 1.1.0
Tableberg – Simple Gutenberg Table Block v1.1.0
1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.5 1.0.4 1.0.3 1.0.2 1.0.1 trunk 0.0.2 0.2.1 0.3.2 0.3.3 0.4.1 0.5.0 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 0.5.6 0.5.7 All 42 releases
tableberg / renderer / Table / InnerBlocksAttrsAdapter.php

InnerBlocksAttrsAdapter.php in Tableberg – Simple Gutenberg Table Block 1.1.0, at renderer/Table/InnerBlocksAttrsAdapter.php

173 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Tableberg\Renderer\Table;
4
5 /**
6 * Adapts a v4 native-blocks table (rows -> cells -> elements innerBlocks)
7 * into the v3-shaped attrs array that TableRenderer::render() consumes.
8 * This keeps the whole PHP renderer working unchanged for both formats.
9 */
10 class InnerBlocksAttrsAdapter {
11 const ELEMENT_PREFIX = 'tableberg/';
12
13 /**
14 * @param array $attributes v4 table block attrs.
15 * @param array $innerBlocks Parsed inner blocks (tableberg/row list).
16 * @return array v3-shaped attrs array.
17 */
18 public static function to_attrs($attributes, $innerBlocks) {
19 $attrs = is_array($attributes) ? $attributes : [];
20 $innerBlocks = is_array($innerBlocks) ? $innerBlocks : [];
21
22 $cells = [];
23 $rowConfigs = [];
24 // Positions occupied by a rowSpan/colSpan anchor.
25 $covered = [];
26 $colsCount = 0;
27
28 $row = 0;
29 foreach ($innerBlocks as $rowBlock) {
30 if (
31 !is_array($rowBlock) ||
32 !isset($rowBlock['blockName']) ||
33 $rowBlock['blockName'] !== 'tableberg/row'
34 ) {
35 continue;
36 }
37
38 $rowAttrs = isset($rowBlock['attrs']) && is_array($rowBlock['attrs'])
39 ? $rowBlock['attrs']
40 : [];
41 if (isset($rowAttrs['height']) && is_string($rowAttrs['height']) && $rowAttrs['height'] !== '') {
42 $rowConfigs[$row] = ['height' => $rowAttrs['height']];
43 } else {
44 $rowConfigs[$row] = null;
45 }
46
47 $col = 0;
48 $rowInner = isset($rowBlock['innerBlocks']) && is_array($rowBlock['innerBlocks'])
49 ? $rowBlock['innerBlocks']
50 : [];
51
52 foreach ($rowInner as $cellBlock) {
53 if (
54 !is_array($cellBlock) ||
55 !isset($cellBlock['blockName']) ||
56 $cellBlock['blockName'] !== 'tableberg/cell'
57 ) {
58 continue;
59 }
60
61 // Advance past positions covered by spans from earlier rows.
62 while (isset($covered["{$row},{$col}"])) {
63 $col++;
64 }
65
66 $cellAttrs = isset($cellBlock['attrs']) && is_array($cellBlock['attrs'])
67 ? $cellBlock['attrs']
68 : [];
69
70 $cellEntry = [];
71 foreach (['span', 'styles', 'ribbon', 'className'] as $key) {
72 if (isset($cellAttrs[$key])) {
73 $cellEntry[$key] = $cellAttrs[$key];
74 }
75 }
76
77 $cellEntry['elements'] = self::to_elements(
78 isset($cellBlock['innerBlocks']) && is_array($cellBlock['innerBlocks'])
79 ? $cellBlock['innerBlocks']
80 : []
81 );
82
83 $cells["{$row},{$col}"] = $cellEntry;
84
85 $rowSpan = 1;
86 $colSpan = 1;
87 if (isset($cellAttrs['span']) && is_array($cellAttrs['span'])) {
88 if (isset($cellAttrs['span']['rowSpan']) && is_numeric($cellAttrs['span']['rowSpan'])) {
89 $rowSpan = max(1, (int) $cellAttrs['span']['rowSpan']);
90 }
91 if (isset($cellAttrs['span']['colSpan']) && is_numeric($cellAttrs['span']['colSpan'])) {
92 $colSpan = max(1, (int) $cellAttrs['span']['colSpan']);
93 }
94 }
95
96 for ($dr = 0; $dr < $rowSpan; $dr++) {
97 for ($dc = 0; $dc < $colSpan; $dc++) {
98 if ($dr === 0 && $dc === 0) {
99 continue;
100 }
101 $covered[($row + $dr) . ',' . ($col + $dc)] = true;
102 }
103 }
104
105 $col += $colSpan;
106 if ($col > $colsCount) {
107 $colsCount = $col;
108 }
109 }
110
111 // Trailing covered positions still widen the row.
112 while (isset($covered["{$row},{$col}"])) {
113 $col++;
114 if ($col > $colsCount) {
115 $colsCount = $col;
116 }
117 }
118
119 $row++;
120 }
121
122 $table = isset($attrs['table']) && is_array($attrs['table'])
123 ? $attrs['table']
124 : [];
125 $table['rows'] = $row;
126 $table['cols'] = $colsCount;
127
128 $adapted = $attrs;
129 $adapted['version'] = 3;
130 $adapted['table'] = $table;
131 $adapted['rows'] = $rowConfigs;
132 $adapted['cells'] = $cells;
133
134 return $adapted;
135 }
136
137 /**
138 * @param array $elementBlocks Parsed element blocks inside a cell.
139 * @return array v3 cell elements array.
140 */
141 private static function to_elements($elementBlocks) {
142 $elements = [];
143
144 foreach ($elementBlocks as $elementBlock) {
145 if (
146 !is_array($elementBlock) ||
147 !isset($elementBlock['blockName']) ||
148 strpos($elementBlock['blockName'], self::ELEMENT_PREFIX) !== 0
149 ) {
150 continue;
151 }
152
153 $name = substr($elementBlock['blockName'], strlen(self::ELEMENT_PREFIX));
154 $attrs = isset($elementBlock['attrs']) && is_array($elementBlock['attrs'])
155 ? $elementBlock['attrs']
156 : [];
157
158 $element = ['name' => $name];
159
160 if (isset($attrs['bindings']) && is_array($attrs['bindings']) && !empty($attrs['bindings'])) {
161 $element['bindings'] = $attrs['bindings'];
162 unset($attrs['bindings']);
163 }
164
165 $element['attributes'] = $attrs;
166
167 $elements[] = $element;
168 }
169
170 return $elements;
171 }
172 }
173