1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2026-08-22 08:56:30 +00:00

improve InputRichBlocksTable

This commit is contained in:
2026-08-17 10:46:18 +06:00
parent f9c634ca46
commit 5b6fe8eb96
4 changed files with 98 additions and 5 deletions

View File

@@ -49,6 +49,37 @@ class InputRichBlockUnorderedListBuilder {
fun build(): List<InputRichBlockListItem.Unordered> = items.toList()
}
/** Builder of [RichBlockTableCell]s used inside [InputRichBlockTableBuilder.row]. */
@RichTextDsl
class InputRichBlockTableRowBuilder {
private val cells = mutableListOf<RichBlockTableCell>()
fun cell(
align: String,
valign: String,
isHeader: Boolean? = null,
colspan: Int? = null,
rowspan: Int? = null,
block: RichTextBuilder.() -> Unit
) {
cells.add(RichBlockTableCell(buildRichText(block), isHeader, colspan, rowspan, align, valign))
}
fun build(): List<RichBlockTableCell> = cells.toList()
}
/** Builder of table rows used inside [InputRichBlocksBuilder.table]. */
@RichTextDsl
class InputRichBlockTableBuilder {
private val rows = mutableListOf<List<RichBlockTableCell>>()
fun row(block: InputRichBlockTableRowBuilder.() -> Unit) {
rows.add(InputRichBlockTableRowBuilder().apply(block).build())
}
fun build(): List<List<RichBlockTableCell>> = rows.toList()
}
/**
* Builder of a [List] of [InputRichBlock]s - the root of the rich message input DSL. Text-bearing and container blocks
* have their own DSL functions; media blocks (photo, video, animation, audio, voice note, collage, slideshow, table,
@@ -136,11 +167,11 @@ class InputRichBlocksBuilder {
add(InputRichBlockSlideshow(buildInputRichBlocks(block), caption))
fun table(
cells: List<List<RichBlockTableCell>>,
isBordered: Boolean? = null,
isStriped: Boolean? = null,
caption: RichText? = null
) = add(InputRichBlockTable(cells, isBordered, isStriped, caption))
caption: RichText? = null,
block: InputRichBlockTableBuilder.() -> Unit
) = add(InputRichBlockTable(InputRichBlockTableBuilder().apply(block).build(), isBordered, isStriped, caption))
fun map(
location: StaticLocation,

View File

@@ -78,4 +78,52 @@ class InputRichBlocksDslTest {
assertEquals(RichTextPlain("h1"), (blocks[0] as InputRichBlockSectionHeading).text)
assertEquals(RichTextBold(RichTextPlain("h2")), (blocks[1] as InputRichBlockSectionHeading).text)
}
@Test
fun buildsTable() {
val blocks = buildInputRichBlocks {
table(isBordered = true, isStriped = false, caption = RichTextPlain("caption")) {
row {
cell(align = "left", valign = "top", isHeader = true, colspan = 2) {
bold("heading")
}
}
row {
cell(align = "center", valign = "middle", rowspan = 2) {
plain("value")
}
}
}
}
assertEquals(
listOf(
InputRichBlockTable(
cells = listOf(
listOf(
RichBlockTableCell(
text = RichTextBold(RichTextPlain("heading")),
isHeader = true,
colspan = 2,
align = "left",
valign = "top"
)
),
listOf(
RichBlockTableCell(
text = RichTextPlain("value"),
rowspan = 2,
align = "center",
valign = "middle"
)
)
),
isBordered = true,
isStriped = false,
caption = RichTextPlain("caption")
)
),
blocks
)
}
}