|
1 | 1 | import json |
2 | 2 |
|
| 3 | +from collections.abc import Iterable |
3 | 4 | from typing import Any |
4 | 5 |
|
5 | 6 | from django import forms |
6 | 7 | from django.forms import Media |
7 | 8 | from django.utils.functional import cached_property |
8 | | -from wagtail.blocks import FieldBlock, StructBlock |
| 9 | +from wagtail.blocks import Block, FieldBlock, StructBlock |
9 | 10 | from wagtail.blocks.field_block import CharBlock, FieldBlockAdapter |
10 | 11 |
|
11 | 12 | from .utils import html_table_to_dict |
|
17 | 18 | from wagtail.telepath import register |
18 | 19 |
|
19 | 20 |
|
| 21 | +type BlockDefinitions = Iterable[tuple[str, Block] | list[str | Block]] |
| 22 | + |
| 23 | + |
20 | 24 | class TinyTableFieldBlock(FieldBlock): |
21 | 25 | def __init__( |
22 | 26 | self, *, required: bool = True, help_text: str | None = None, **kwargs: Any |
@@ -86,16 +90,27 @@ class TinyTableBlock(StructBlock): |
86 | 90 | title = CharBlock(required=False) |
87 | 91 | caption = CharBlock(required=False) |
88 | 92 |
|
89 | | - def __init__(self, *, local_blocks=None, search_index=True, **kwargs): |
90 | | - super().__init__(local_blocks=local_blocks, search_index=search_index, **kwargs) |
91 | | - # manually define the data block so we can pass on configuration kwargs |
92 | | - block = TinyTableFieldBlock( |
| 93 | + def __init__( |
| 94 | + self, |
| 95 | + local_blocks: BlockDefinitions | None = None, |
| 96 | + search_index: bool = True, # noqa: FBT001,FBT002 |
| 97 | + *, |
| 98 | + allow_links: bool = False, |
| 99 | + enable_context_menu: bool = False, |
| 100 | + **kwargs |
| 101 | + ) -> None: |
| 102 | + if local_blocks is None: |
| 103 | + local_blocks = () |
| 104 | + |
| 105 | + # Manually define the data block so we can pass on configuration kwargs. |
| 106 | + data_block = TinyTableFieldBlock( |
93 | 107 | required=False, |
94 | | - allow_links=kwargs.get("allow_links", False), |
95 | | - enable_context_menu=kwargs.get("enable_context_menu", False), |
| 108 | + allow_links=allow_links, |
| 109 | + enable_context_menu=enable_context_menu, |
96 | 110 | ) |
97 | | - block.set_name("data") |
98 | | - self.child_blocks["data"] = block |
| 111 | + |
| 112 | + local_blocks = (*local_blocks, ("data", data_block)) |
| 113 | + super().__init__(local_blocks=local_blocks, search_index=search_index, **kwargs) |
99 | 114 |
|
100 | 115 | class Meta: |
101 | 116 | icon = "table" |
|
0 commit comments