| @@ -35,8 +35,18 @@ | ||
| 35 | 35 | /** @var CellDefaults */ |
| 36 | 36 | public $cellDefaults; |
| 37 | 37 | |
| 38 | 38 | /** |
| 39 | + * Every attribute the table block carried, untouched. Pro registers | |
| 40 | + * table-level attributes of its own, so its renderer reads them from | |
| 41 | + * here rather than free having to know their names — mirrors | |
| 42 | + * CellData->attrs. | |
| 43 | + * | |
| 44 | + * @var array<string, mixed> | |
| 45 | + */ | |
| 46 | + public $attrs; | |
| 47 | + | |
| 48 | + /** | |
| 39 | 49 | * @param array $data |
| 40 | 50 | * @return self |
| 41 | 51 | */ |
| 42 | 52 | public static function from_array($data) { |
| @@ -43,8 +53,9 @@ | ||
| 43 | 53 | $data = is_array($data) ? $data : []; |
| 44 | 54 | $defaults = Defaults::get_defaults(); |
| 45 | 55 | |
| 46 | 56 | $instance = new self(); |
| 57 | + $instance->attrs = $data; | |
| 47 | 58 | $instance->version = new NumberAttr( |
| 48 | 59 | getOrNull($data['version']), |
| 49 | 60 | $defaults['version'] |
| 50 | 61 | ); |
| @@ -96,24 +107,8 @@ | ||
| 96 | 107 | |
| 97 | 108 | return ['cells' => []]; |
| 98 | 109 | } |
| 99 | 110 | |
| 100 | - /** @return array<int, array{array{int, int}, array<int, mixed>}> */ | |
| 101 | - public static function get_data_defaults() { | |
| 102 | - $defaults = Defaults::get_defaults(); | |
| 103 | - | |
| 104 | - if ( | |
| 105 | - isset($defaults['data']) && | |
| 106 | - is_array($defaults['data']) && | |
| 107 | - isset($defaults['data']['cells']) && | |
| 108 | - is_array($defaults['data']['cells']) | |
| 109 | - ) { | |
| 110 | - return $defaults['data']['cells']; | |
| 111 | - } | |
| 112 | - | |
| 113 | - return []; | |
| 114 | - } | |
| 115 | - | |
| 116 | 111 | /** @return array<int, RowConfig> */ |
| 117 | 112 | private static function parse_row_configs($data) { |
| 118 | 113 | if (!is_array($data)) { |
| 119 | 114 | return []; |
| @@ -136,13 +131,9 @@ | ||
| 136 | 131 | |
| 137 | 132 | $columns = []; |
| 138 | 133 | |
| 139 | 134 | foreach ($data as $column => $column_config) { |
| 140 | - $columns[(int) $column] = ColumnConfig::from_array( | |
| 141 | - $column_config, | |
| 142 | - 'text', | |
| 143 | - true | |
| 144 | - ); | |
| 135 | + $columns[(int) $column] = ColumnConfig::from_array($column_config); | |
| 145 | 136 | } |
| 146 | 137 | |
| 147 | 138 | return $columns; |
| 148 | 139 | } |
| @@ -185,52 +176,8 @@ | ||
| 185 | 176 | return $bindings; |
| 186 | 177 | } |
| 187 | 178 | } |
| 188 | 179 | |
| 189 | -class Data { | |
| 190 | - /** @var array<int, array{array{int, int}, array<int, CellElement>}> */ | |
| 191 | - public $cells; | |
| 192 | - | |
| 193 | - /** @return self */ | |
| 194 | - public static function from_array($data) { | |
| 195 | - $data = is_array($data) ? $data : []; | |
| 196 | - $default_cells = TableAttrs::get_data_defaults(); | |
| 197 | - | |
| 198 | - $instance = new self(); | |
| 199 | - $instance->cells = self::parse_cells(getOrNull($data['cells']), $default_cells); | |
| 200 | - | |
| 201 | - return $instance; | |
| 202 | - } | |
| 203 | - | |
| 204 | - /** @return array<int, array{array{int, int}, array<int, CellElement>}> */ | |
| 205 | - private static function parse_cells($data, $default) { | |
| 206 | - if (!is_array($data)) { | |
| 207 | - return $default; | |
| 208 | - } | |
| 209 | - | |
| 210 | - $cells = []; | |
| 211 | - | |
| 212 | - foreach ($data as $cell) { | |
| 213 | - if (!is_array($cell[0])) { | |
| 214 | - continue; | |
| 215 | - } | |
| 216 | - | |
| 217 | - $coords = [(int) $cell[0][0], (int) $cell[0][1]]; | |
| 218 | - $elements = []; | |
| 219 | - | |
| 220 | - if (is_array($cell[1])) { | |
| 221 | - foreach ($cell[1] as $element) { | |
| 222 | - $elements[] = CellElement::from_array($element); | |
| 223 | - } | |
| 224 | - } | |
| 225 | - | |
| 226 | - $cells[] = [$coords, $elements]; | |
| 227 | - } | |
| 228 | - | |
| 229 | - return $cells; | |
| 230 | - } | |
| 231 | -} | |
| 232 | - | |
| 233 | 180 | class PaginationConfig { |
| 234 | 181 | /** @var BoolAttr */ |
| 235 | 182 | public $enabled; |
| 236 | 183 | |
| @@ -260,39 +207,8 @@ | ||
| 260 | 207 | return $instance; |
| 261 | 208 | } |
| 262 | 209 | } |
| 263 | 210 | |
| 264 | -class SearchConfig { | |
| 265 | - /** @var BoolAttr */ | |
| 266 | - public $enabled; | |
| 267 | - | |
| 268 | - /** @var StringAttr */ | |
| 269 | - public $placeholder; | |
| 270 | - | |
| 271 | - /** @var StringAttr */ | |
| 272 | - public $highlightColor; | |
| 273 | - | |
| 274 | - /** @var StringAttr */ | |
| 275 | - public $position; | |
| 276 | - | |
| 277 | - /** @return self */ | |
| 278 | - public static function from_array($data) { | |
| 279 | - $data = is_array($data) ? $data : []; | |
| 280 | - $table_defaults = TableAttrs::get_table_defaults(); | |
| 281 | - $d = isset($table_defaults['search']) && is_array($table_defaults['search']) | |
| 282 | - ? $table_defaults['search'] | |
| 283 | - : []; | |
| 284 | - | |
| 285 | - $instance = new self(); | |
| 286 | - $instance->enabled = new BoolAttr(getOrNull($data['enabled']), $d['enabled']); | |
| 287 | - $instance->placeholder = new StringAttr(getOrNull($data['placeholder']), $d['placeholder']); | |
| 288 | - $instance->highlightColor = new StringAttr(getOrNull($data['highlightColor']), $d['highlightColor']); | |
| 289 | - $instance->position = new StringAttr(getOrNull($data['position']), $d['position'], ['left', 'right']); | |
| 290 | - | |
| 291 | - return $instance; | |
| 292 | - } | |
| 293 | -} | |
| 294 | - | |
| 295 | 211 | class ResponsiveBreakpointConfig { |
| 296 | 212 | /** @var BoolAttr */ |
| 297 | 213 | public $enabled; |
| 298 | 214 | |
| @@ -444,13 +360,23 @@ | ||
| 444 | 360 | |
| 445 | 361 | /** @var array<string, mixed>|null */ |
| 446 | 362 | public $ribbon; |
| 447 | 363 | |
| 364 | + /** | |
| 365 | + * Every attribute the cell block carried, untouched. Pro registers cell | |
| 366 | + * attributes of its own, so its renderer reads them from here rather | |
| 367 | + * than free having to know their names. | |
| 368 | + * | |
| 369 | + * @var array<string, mixed> | |
| 370 | + */ | |
| 371 | + public $attrs; | |
| 372 | + | |
| 448 | 373 | /** @return self */ |
| 449 | 374 | public static function from_array($data) { |
| 450 | 375 | $data = is_array($data) ? $data : []; |
| 451 | 376 | |
| 452 | 377 | $instance = new self(); |
| 378 | + $instance->attrs = $data; | |
| 453 | 379 | $instance->span = Span::from_array(getOrNull($data['span'])); |
| 454 | 380 | $instance->className = new StringAttr(getOrNull($data['className']), ''); |
| 455 | 381 | $instance->elements = []; |
| 456 | 382 | |
| @@ -486,11 +412,8 @@ | ||
| 486 | 412 | |
| 487 | 413 | /** @var BoolAttr */ |
| 488 | 414 | public $footerEnabled; |
| 489 | 415 | |
| 490 | - /** @var BoolAttr */ | |
| 491 | - public $stickyHeader; | |
| 492 | - | |
| 493 | 416 | /** @var StringAttr */ |
| 494 | 417 | public $caption; |
| 495 | 418 | |
| 496 | 419 | /** @var StringAttr */ |
| @@ -504,8 +427,11 @@ | ||
| 504 | 427 | |
| 505 | 428 | /** @var Sides */ |
| 506 | 429 | public $tableBorder; |
| 507 | 430 | |
| 431 | + /** @var TypographyConfig */ | |
| 432 | + public $typography; | |
| 433 | + | |
| 508 | 434 | /** @var Sides */ |
| 509 | 435 | public $margin; |
| 510 | 436 | |
| 511 | 437 | /** @var Sides */ |
| @@ -516,11 +442,8 @@ | ||
| 516 | 442 | |
| 517 | 443 | /** @var PaginationConfig */ |
| 518 | 444 | public $pagination; |
| 519 | 445 | |
| 520 | - /** @var SearchConfig */ | |
| 521 | - public $search; | |
| 522 | - | |
| 523 | 446 | /** @var ResponsiveConfig */ |
| 524 | 447 | public $responsive; |
| 525 | 448 | |
| 526 | 449 | /** @return self */ |
| @@ -533,9 +456,8 @@ | ||
| 533 | 456 | $instance->cols = new NumberAttr(getOrNull($data['cols']), $d['cols']); |
| 534 | 457 | $instance->className = new StringAttr(getOrNull($data['className']), ''); |
| 535 | 458 | $instance->headerEnabled = new BoolAttr(getOrNull($data['headerEnabled']), $d['headerEnabled']); |
| 536 | 459 | $instance->footerEnabled = new BoolAttr(getOrNull($data['footerEnabled']), $d['footerEnabled']); |
| 537 | - $instance->stickyHeader = new BoolAttr(getOrNull($data['stickyHeader']), $d['stickyHeader']); | |
| 538 | 460 | $instance->caption = new StringAttr(getOrNull($data['caption']), $d['caption']); |
| 539 | 461 | $instance->tableWidth = new StringAttr( |
| 540 | 462 | getOrNull($data['tableWidth']), |
| 541 | 463 | $d['tableWidth'] |
| @@ -549,8 +471,11 @@ | ||
| 549 | 471 | $instance->tableBorder = Sides::from_array( |
| 550 | 472 | getOrNull($data['tableBorder']), |
| 551 | 473 | $d['tableBorder'] |
| 552 | 474 | ); |
| 475 | + $instance->typography = TypographyConfig::from_array( | |
| 476 | + getOrNull($data['typography']) | |
| 477 | + ); | |
| 553 | 478 | $emptySides = ['top' => '', 'right' => '', 'bottom' => '', 'left' => '']; |
| 554 | 479 | $instance->margin = Sides::from_array( |
| 555 | 480 | getOrNull($data['margin']), |
| 556 | 481 | isset($d['margin']) && is_array($d['margin']) ? $d['margin'] : $emptySides |
| @@ -563,9 +488,8 @@ | ||
| 563 | 488 | getOrNull($data['fixedColumnWidths']), |
| 564 | 489 | $d['fixedColumnWidths'] |
| 565 | 490 | ); |
| 566 | 491 | $instance->pagination = PaginationConfig::from_array(getOrNull($data['pagination'])); |
| 567 | - $instance->search = SearchConfig::from_array(getOrNull($data['search'])); | |
| 568 | 492 | $instance->responsive = ResponsiveConfig::from_array(getOrNull($data['responsive'])); |
| 569 | 493 | |
| 570 | 494 | return $instance; |
| 571 | 495 | } |
| @@ -570,27 +494,122 @@ | ||
| 570 | 494 | return $instance; |
| 571 | 495 | } |
| 572 | 496 | } |
| 573 | 497 | |
| 498 | +/** | |
| 499 | + * Table-wide typography: the same options the core Table block offers, set on | |
| 500 | + * the table element so the cells inherit them. | |
| 501 | + */ | |
| 502 | +class TypographyConfig { | |
| 503 | + /** @var StringAttr */ | |
| 504 | + public $fontFamily; | |
| 505 | + | |
| 506 | + /** @var StringAttr */ | |
| 507 | + public $fontSize; | |
| 508 | + | |
| 509 | + /** @var StringAttr */ | |
| 510 | + public $fontStyle; | |
| 511 | + | |
| 512 | + /** @var StringAttr */ | |
| 513 | + public $fontWeight; | |
| 514 | + | |
| 515 | + /** @var StringAttr */ | |
| 516 | + public $lineHeight; | |
| 517 | + | |
| 518 | + /** @var StringAttr */ | |
| 519 | + public $letterSpacing; | |
| 520 | + | |
| 521 | + /** @var StringAttr */ | |
| 522 | + public $textDecoration; | |
| 523 | + | |
| 524 | + /** @var StringAttr */ | |
| 525 | + public $textTransform; | |
| 526 | + | |
| 527 | + /** @return self */ | |
| 528 | + public static function from_array($data) { | |
| 529 | + $data = is_array($data) ? $data : []; | |
| 530 | + $tableDefaults = TableAttrs::get_table_defaults(); | |
| 531 | + $d = isset($tableDefaults['typography']) && is_array($tableDefaults['typography']) | |
| 532 | + ? $tableDefaults['typography'] | |
| 533 | + : []; | |
| 534 | + | |
| 535 | + $instance = new self(); | |
| 536 | + foreach ( | |
| 537 | + [ | |
| 538 | + 'fontFamily', | |
| 539 | + 'fontSize', | |
| 540 | + 'fontStyle', | |
| 541 | + 'fontWeight', | |
| 542 | + 'lineHeight', | |
| 543 | + 'letterSpacing', | |
| 544 | + 'textDecoration', | |
| 545 | + 'textTransform', | |
| 546 | + ] as $property | |
| 547 | + ) { | |
| 548 | + $instance->{$property} = new StringAttr( | |
| 549 | + getOrNull($data[$property]), | |
| 550 | + isset($d[$property]) ? $d[$property] : '' | |
| 551 | + ); | |
| 552 | + } | |
| 553 | + | |
| 554 | + return $instance; | |
| 555 | + } | |
| 556 | + | |
| 557 | + /** | |
| 558 | + * The typography as `property: value` declarations, skipping everything | |
| 559 | + * the user has not set so the theme keeps deciding. | |
| 560 | + * | |
| 561 | + * @return array<int, string> | |
| 562 | + */ | |
| 563 | + public function asStyles() { | |
| 564 | + $properties = [ | |
| 565 | + 'font-family' => $this->fontFamily, | |
| 566 | + 'font-size' => $this->fontSize, | |
| 567 | + 'font-style' => $this->fontStyle, | |
| 568 | + 'font-weight' => $this->fontWeight, | |
| 569 | + 'line-height' => $this->lineHeight, | |
| 570 | + 'letter-spacing' => $this->letterSpacing, | |
| 571 | + 'text-decoration' => $this->textDecoration, | |
| 572 | + 'text-transform' => $this->textTransform, | |
| 573 | + ]; | |
| 574 | + | |
| 575 | + $styles = []; | |
| 576 | + | |
| 577 | + foreach ($properties as $property => $attr) { | |
| 578 | + if ($attr->isNotEmpty()) { | |
| 579 | + $styles[] = $property . ': ' . $attr->asAttr(); | |
| 580 | + } | |
| 581 | + } | |
| 582 | + | |
| 583 | + return $styles; | |
| 584 | + } | |
| 585 | +} | |
| 586 | + | |
| 574 | 587 | class CellStyles { |
| 575 | 588 | /** @var Sides */ |
| 576 | 589 | public $padding; |
| 577 | 590 | |
| 578 | 591 | /** @var StringAttr */ |
| 579 | - public $orientation; | |
| 592 | + public $elementGap; | |
| 580 | 593 | |
| 581 | 594 | /** @var StringAttr */ |
| 582 | - public $elementGap; | |
| 595 | + public $verticalAlign; | |
| 583 | 596 | |
| 584 | 597 | /** @var StringAttr */ |
| 585 | - public $wrap; | |
| 598 | + public $backgroundColor; | |
| 586 | 599 | |
| 587 | 600 | /** @var StringAttr */ |
| 588 | - public $verticalAlign; | |
| 601 | + public $headerBackgroundColor; | |
| 589 | 602 | |
| 590 | 603 | /** @var StringAttr */ |
| 591 | - public $backgroundColor; | |
| 604 | + public $footerBackgroundColor; | |
| 592 | 605 | |
| 606 | + /** @var StringAttr */ | |
| 607 | + public $evenRowBackgroundColor; | |
| 608 | + | |
| 609 | + /** @var StringAttr */ | |
| 610 | + public $oddRowBackgroundColor; | |
| 611 | + | |
| 593 | 612 | /** @var Sides */ |
| 594 | 613 | public $border; |
| 595 | 614 | |
| 596 | 615 | /** @var Corners */ |
| @@ -605,22 +624,12 @@ | ||
| 605 | 624 | $d = TableAttrs::get_cell_style_defaults(); |
| 606 | 625 | |
| 607 | 626 | $instance = new self(); |
| 608 | 627 | $instance->padding = Sides::from_array(getOrNull($data['padding']), $d['padding']); |
| 609 | - $instance->orientation = new StringAttr( | |
| 610 | - getOrNull($data['orientation']), | |
| 611 | - $d['orientation'], | |
| 612 | - ['vertical', 'horizontal'] | |
| 613 | - ); | |
| 614 | 628 | $instance->elementGap = new StringAttr( |
| 615 | 629 | getOrNull($data['elementGap']), |
| 616 | 630 | $d['elementGap'] |
| 617 | 631 | ); |
| 618 | - $instance->wrap = new StringAttr( | |
| 619 | - getOrNull($data['wrap']), | |
| 620 | - $d['wrap'], | |
| 621 | - ['wrap', 'nowrap'] | |
| 622 | - ); | |
| 623 | 632 | $instance->verticalAlign = new StringAttr( |
| 624 | 633 | getOrNull($data['verticalAlign']), |
| 625 | 634 | $d['verticalAlign'], |
| 626 | 635 | ['top', 'middle', 'bottom'] |
| @@ -625,8 +634,12 @@ | ||
| 625 | 634 | $d['verticalAlign'], |
| 626 | 635 | ['top', 'middle', 'bottom'] |
| 627 | 636 | ); |
| 628 | 637 | $instance->backgroundColor = new StringAttr(getOrNull($data['backgroundColor']), $d['backgroundColor']); |
| 638 | + $instance->headerBackgroundColor = new StringAttr(getOrNull($data['headerBackgroundColor']), $d['headerBackgroundColor']); | |
| 639 | + $instance->footerBackgroundColor = new StringAttr(getOrNull($data['footerBackgroundColor']), $d['footerBackgroundColor']); | |
| 640 | + $instance->evenRowBackgroundColor = new StringAttr(getOrNull($data['evenRowBackgroundColor']), $d['evenRowBackgroundColor']); | |
| 641 | + $instance->oddRowBackgroundColor = new StringAttr(getOrNull($data['oddRowBackgroundColor']), $d['oddRowBackgroundColor']); | |
| 629 | 642 | $instance->border = Sides::from_array(getOrNull($data['border']), $d['border']); |
| 630 | 643 | $instance->borderRadius = Corners::from_array(getOrNull($data['borderRadius']), $d['borderRadius']); |
| 631 | 644 | $instance->cells = is_array(getOrNull($data['cells'])) ? $data['cells'] : $d['cells']; |
| 632 | 645 | |
| @@ -661,36 +674,17 @@ | ||
| 661 | 674 | } |
| 662 | 675 | |
| 663 | 676 | class ColumnConfig { |
| 664 | 677 | /** @var StringAttr|null */ |
| 665 | - public $sortable; | |
| 666 | - | |
| 667 | - /** @var StringAttr|null */ | |
| 668 | 678 | public $width; |
| 669 | 679 | |
| 670 | - /** | |
| 671 | - * @param array|null $data | |
| 672 | - * @param string $defaultSortable | |
| 673 | - * @param bool $allowNull | |
| 674 | - * @return self | |
| 675 | - */ | |
| 676 | - public static function from_array($data, $defaultSortable = 'text', $allowNull = false) { | |
| 680 | + /** @return self */ | |
| 681 | + public static function from_array($data) { | |
| 677 | 682 | $data = is_array($data) ? $data : []; |
| 678 | - $sortable = getOrNull($data['sortable']); | |
| 679 | 683 | $width = getOrNull($data['width']); |
| 680 | 684 | |
| 681 | 685 | $instance = new self(); |
| 682 | 686 | |
| 683 | - if ($allowNull && ($sortable === null || $sortable === '')) { | |
| 684 | - $instance->sortable = null; | |
| 685 | - } else { | |
| 686 | - $instance->sortable = new StringAttr( | |
| 687 | - $sortable, | |
| 688 | - $defaultSortable, | |
| 689 | - ['text', 'number', 'date'] | |
| 690 | - ); | |
| 691 | - } | |
| 692 | - | |
| 693 | 687 | if ($width === null || $width === '') { |
| 694 | 688 | $instance->width = null; |
| 695 | 689 | } else { |
| 696 | 690 | $instance->width = new StringAttr($width, ''); |
| @@ -704,8 +698,17 @@ | ||
| 704 | 698 | /** @var StringAttr|null */ |
| 705 | 699 | public $height; |
| 706 | 700 | |
| 707 | 701 | /** |
| 702 | + * Every attribute the row block carried, untouched. Pro registers row | |
| 703 | + * attributes of its own (e.g. backgroundColor), so its renderer reads | |
| 704 | + * them from here rather than free having to know their names. | |
| 705 | + * | |
| 706 | + * @var array<string, mixed> | |
| 707 | + */ | |
| 708 | + public $attrs; | |
| 709 | + | |
| 710 | + /** | |
| 708 | 711 | * @param array|null $data |
| 709 | 712 | * @return self |
| 710 | 713 | */ |
| 711 | 714 | public static function from_array($data) { |
| @@ -712,8 +715,9 @@ | ||
| 712 | 715 | $data = is_array($data) ? $data : []; |
| 713 | 716 | $height = getOrNull($data['height']); |
| 714 | 717 | |
| 715 | 718 | $instance = new self(); |
| 719 | + $instance->attrs = $data; | |
| 716 | 720 | |
| 717 | 721 | if ($height === null || $height === '') { |
| 718 | 722 | $instance->height = null; |
| 719 | 723 | } else { |