Skip to content

Commit c442278

Browse files
KENNYSOFTseratch
andauthored
Add RichTextBlock support to Block Kit Kotlin DSL builder (#1376)
Co-authored-by: Kazuhiro Sera <[email protected]>
1 parent e5565e7 commit c442278

File tree

23 files changed

+1165
-23
lines changed

23 files changed

+1165
-23
lines changed

json-logs/samples/api/views.open.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,8 @@
449449
},
450450
"unicode": ""
451451
}
452-
]
452+
],
453+
"border": 123
453454
},
454455
{
455456
"type": "rich_text_section",
@@ -1003,7 +1004,8 @@
10031004
},
10041005
"unicode": ""
10051006
}
1006-
]
1007+
],
1008+
"border": 123
10071009
},
10081010
{
10091011
"type": "rich_text_section",
@@ -1554,7 +1556,8 @@
15541556
},
15551557
"unicode": ""
15561558
}
1557-
]
1559+
],
1560+
"border": 123
15581561
},
15591562
{
15601563
"type": "rich_text_section",
@@ -1691,7 +1694,8 @@
16911694
}
16921695
]
16931696
}
1694-
]
1697+
],
1698+
"border": 123
16951699
},
16961700
{
16971701
"type": "rich_text_section",
@@ -2104,7 +2108,8 @@
21042108
},
21052109
"unicode": ""
21062110
}
2107-
]
2111+
],
2112+
"border": 123
21082113
},
21092114
{
21102115
"type": "rich_text_section",

json-logs/samples/api/views.publish.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,8 @@
449449
},
450450
"unicode": ""
451451
}
452-
]
452+
],
453+
"border": 123
453454
},
454455
{
455456
"type": "rich_text_section",
@@ -1003,7 +1004,8 @@
10031004
},
10041005
"unicode": ""
10051006
}
1006-
]
1007+
],
1008+
"border": 123
10071009
},
10081010
{
10091011
"type": "rich_text_section",
@@ -1554,7 +1556,8 @@
15541556
},
15551557
"unicode": ""
15561558
}
1557-
]
1559+
],
1560+
"border": 123
15581561
},
15591562
{
15601563
"type": "rich_text_section",
@@ -1691,7 +1694,8 @@
16911694
}
16921695
]
16931696
}
1694-
]
1697+
],
1698+
"border": 123
16951699
},
16961700
{
16971701
"type": "rich_text_section",
@@ -2104,7 +2108,8 @@
21042108
},
21052109
"unicode": ""
21062110
}
2107-
]
2111+
],
2112+
"border": 123
21082113
},
21092114
{
21102115
"type": "rich_text_section",

json-logs/samples/api/views.push.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,8 @@
449449
},
450450
"unicode": ""
451451
}
452-
]
452+
],
453+
"border": 123
453454
},
454455
{
455456
"type": "rich_text_section",
@@ -1003,7 +1004,8 @@
10031004
},
10041005
"unicode": ""
10051006
}
1006-
]
1007+
],
1008+
"border": 123
10071009
},
10081010
{
10091011
"type": "rich_text_section",
@@ -1554,7 +1556,8 @@
15541556
},
15551557
"unicode": ""
15561558
}
1557-
]
1559+
],
1560+
"border": 123
15581561
},
15591562
{
15601563
"type": "rich_text_section",
@@ -1691,7 +1694,8 @@
16911694
}
16921695
]
16931696
}
1694-
]
1697+
],
1698+
"border": 123
16951699
},
16961700
{
16971701
"type": "rich_text_section",
@@ -2104,7 +2108,8 @@
21042108
},
21052109
"unicode": ""
21062110
}
2107-
]
2111+
],
2112+
"border": 123
21082113
},
21092114
{
21102115
"type": "rich_text_section",

json-logs/samples/api/views.update.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,8 @@
449449
},
450450
"unicode": ""
451451
}
452-
]
452+
],
453+
"border": 123
453454
},
454455
{
455456
"type": "rich_text_section",
@@ -1003,7 +1004,8 @@
10031004
},
10041005
"unicode": ""
10051006
}
1006-
]
1007+
],
1008+
"border": 123
10071009
},
10081010
{
10091011
"type": "rich_text_section",
@@ -1554,7 +1556,8 @@
15541556
},
15551557
"unicode": ""
15561558
}
1557-
]
1559+
],
1560+
"border": 123
15581561
},
15591562
{
15601563
"type": "rich_text_section",
@@ -1691,7 +1694,8 @@
16911694
}
16921695
]
16931696
}
1694-
]
1697+
],
1698+
"border": 123
16951699
},
16961700
{
16971701
"type": "rich_text_section",
@@ -2104,7 +2108,8 @@
21042108
},
21052109
"unicode": ""
21062110
}
2107-
]
2111+
],
2112+
"border": 123
21082113
},
21092114
{
21102115
"type": "rich_text_section",
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.slack.api.model.kotlin_extension.block
2+
3+
import com.slack.api.model.block.RichTextBlock
4+
import com.slack.api.model.kotlin_extension.block.element.container.MultiRichTextElementContainer
5+
import com.slack.api.model.kotlin_extension.block.element.dsl.RichTextElementDsl
6+
7+
@BlockLayoutBuilder
8+
class RichTextBlockBuilder private constructor(
9+
private val elementsContainer: MultiRichTextElementContainer
10+
) : Builder<RichTextBlock>, RichTextElementDsl by elementsContainer {
11+
private var blockId: String? = null
12+
13+
constructor() : this(MultiRichTextElementContainer())
14+
15+
/**
16+
* A string acting as a unique identifier for a block. You can use this `block_id` when you receive an interaction
17+
* payload to identify the source of the action. If not specified, one will be generated. Maximum length for this
18+
* field is 255 characters. `block_id` should be unique for each message and each iteration of a message. If a
19+
* message is updated, use a new `block_id`.
20+
*
21+
* @see <a href="https://api.slack.com/reference/block-kit/blocks#rich_text">Rich text block documentation</a>
22+
*/
23+
fun blockId(id: String) {
24+
blockId = id
25+
}
26+
27+
/**
28+
* An array of rich text objects - `rich_text_section`, `rich_text_list`, `rich_text_preformatted`,
29+
* and `rich_text_quote`.
30+
*
31+
* @see <a href="https://api.slack.com/reference/block-kit/blocks#rich_text">Rich text block documentation</a>
32+
*/
33+
fun elements(builder: RichTextElementDsl.() -> Unit) {
34+
elementsContainer.apply(builder)
35+
}
36+
37+
override fun build(): RichTextBlock {
38+
return RichTextBlock.builder()
39+
.blockId(blockId)
40+
.elements(elementsContainer.underlying)
41+
.build()
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.slack.api.model.kotlin_extension.block.composition.container
2+
3+
import com.slack.api.model.block.element.RichTextElement
4+
import com.slack.api.model.block.element.RichTextSectionElement
5+
import com.slack.api.model.block.element.RichTextSectionElement.LimitedTextStyle
6+
import com.slack.api.model.block.element.RichTextSectionElement.TextStyle
7+
import com.slack.api.model.kotlin_extension.block.composition.dsl.RichTextObjectDsl
8+
import com.slack.api.model.kotlin_extension.block.element.BroadcastRange
9+
10+
class MultiRichTextObjectContainer : RichTextObjectDsl {
11+
val underlying = mutableListOf<RichTextElement>()
12+
13+
override fun broadcast(range: BroadcastRange, style: LimitedTextStyle?) {
14+
underlying += RichTextSectionElement.Broadcast.builder()
15+
.range(range.value)
16+
.style(style)
17+
.build()
18+
}
19+
20+
override fun color(value: String, style: LimitedTextStyle?) {
21+
underlying += RichTextSectionElement.Color.builder()
22+
.value(value)
23+
.style(style)
24+
.build()
25+
}
26+
27+
override fun channel(channelId: String, style: LimitedTextStyle?) {
28+
underlying += RichTextSectionElement.Channel.builder()
29+
.channelId(channelId)
30+
.style(style)
31+
.build()
32+
}
33+
34+
override fun date(timestamp: Int, format: String, style: TextStyle?, url: String?, fallback: String?) {
35+
underlying += RichTextSectionElement.Date.builder()
36+
.timestamp(timestamp)
37+
.format(format)
38+
.style(style)
39+
.url(url)
40+
.fallback(fallback)
41+
.build()
42+
}
43+
44+
override fun emoji(name: String, skinTone: Int?, style: LimitedTextStyle?) {
45+
underlying += RichTextSectionElement.Emoji.builder()
46+
.name(name)
47+
.skinTone(skinTone)
48+
.style(style)
49+
.build()
50+
}
51+
52+
override fun link(url: String, text: String?, unsafe: Boolean?, style: TextStyle?) {
53+
underlying += RichTextSectionElement.Link.builder()
54+
.url(url)
55+
.text(text)
56+
.unsafe(unsafe)
57+
.style(style)
58+
.build()
59+
}
60+
61+
override fun text(text: String, style: TextStyle?) {
62+
underlying += RichTextSectionElement.Text.builder()
63+
.text(text)
64+
.style(style)
65+
.build()
66+
}
67+
68+
override fun user(userId: String, style: LimitedTextStyle?) {
69+
underlying += RichTextSectionElement.User.builder()
70+
.userId(userId)
71+
.style(style)
72+
.build()
73+
}
74+
75+
override fun usergroup(usergroupId: String, style: LimitedTextStyle?) {
76+
underlying += RichTextSectionElement.UserGroup.builder()
77+
.usergroupId(usergroupId)
78+
.style(style)
79+
.build()
80+
}
81+
}

0 commit comments

Comments
 (0)