Skip to content

Commit

Permalink
Add RichTextBlock support to Block Kit Kotlin DSL builder (#1376)
Browse files Browse the repository at this point in the history
Co-authored-by: Kazuhiro Sera <[email protected]>
  • Loading branch information
KENNYSOFT and seratch authored Sep 20, 2024
1 parent e5565e7 commit c442278
Show file tree
Hide file tree
Showing 23 changed files with 1,165 additions and 23 deletions.
15 changes: 10 additions & 5 deletions json-logs/samples/api/views.open.json
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,8 @@
},
"unicode": ""
}
]
],
"border": 123
},
{
"type": "rich_text_section",
Expand Down Expand Up @@ -1003,7 +1004,8 @@
},
"unicode": ""
}
]
],
"border": 123
},
{
"type": "rich_text_section",
Expand Down Expand Up @@ -1554,7 +1556,8 @@
},
"unicode": ""
}
]
],
"border": 123
},
{
"type": "rich_text_section",
Expand Down Expand Up @@ -1691,7 +1694,8 @@
}
]
}
]
],
"border": 123
},
{
"type": "rich_text_section",
Expand Down Expand Up @@ -2104,7 +2108,8 @@
},
"unicode": ""
}
]
],
"border": 123
},
{
"type": "rich_text_section",
Expand Down
15 changes: 10 additions & 5 deletions json-logs/samples/api/views.publish.json
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,8 @@
},
"unicode": ""
}
]
],
"border": 123
},
{
"type": "rich_text_section",
Expand Down Expand Up @@ -1003,7 +1004,8 @@
},
"unicode": ""
}
]
],
"border": 123
},
{
"type": "rich_text_section",
Expand Down Expand Up @@ -1554,7 +1556,8 @@
},
"unicode": ""
}
]
],
"border": 123
},
{
"type": "rich_text_section",
Expand Down Expand Up @@ -1691,7 +1694,8 @@
}
]
}
]
],
"border": 123
},
{
"type": "rich_text_section",
Expand Down Expand Up @@ -2104,7 +2108,8 @@
},
"unicode": ""
}
]
],
"border": 123
},
{
"type": "rich_text_section",
Expand Down
15 changes: 10 additions & 5 deletions json-logs/samples/api/views.push.json
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,8 @@
},
"unicode": ""
}
]
],
"border": 123
},
{
"type": "rich_text_section",
Expand Down Expand Up @@ -1003,7 +1004,8 @@
},
"unicode": ""
}
]
],
"border": 123
},
{
"type": "rich_text_section",
Expand Down Expand Up @@ -1554,7 +1556,8 @@
},
"unicode": ""
}
]
],
"border": 123
},
{
"type": "rich_text_section",
Expand Down Expand Up @@ -1691,7 +1694,8 @@
}
]
}
]
],
"border": 123
},
{
"type": "rich_text_section",
Expand Down Expand Up @@ -2104,7 +2108,8 @@
},
"unicode": ""
}
]
],
"border": 123
},
{
"type": "rich_text_section",
Expand Down
15 changes: 10 additions & 5 deletions json-logs/samples/api/views.update.json
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,8 @@
},
"unicode": ""
}
]
],
"border": 123
},
{
"type": "rich_text_section",
Expand Down Expand Up @@ -1003,7 +1004,8 @@
},
"unicode": ""
}
]
],
"border": 123
},
{
"type": "rich_text_section",
Expand Down Expand Up @@ -1554,7 +1556,8 @@
},
"unicode": ""
}
]
],
"border": 123
},
{
"type": "rich_text_section",
Expand Down Expand Up @@ -1691,7 +1694,8 @@
}
]
}
]
],
"border": 123
},
{
"type": "rich_text_section",
Expand Down Expand Up @@ -2104,7 +2108,8 @@
},
"unicode": ""
}
]
],
"border": 123
},
{
"type": "rich_text_section",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.slack.api.model.kotlin_extension.block

import com.slack.api.model.block.RichTextBlock
import com.slack.api.model.kotlin_extension.block.element.container.MultiRichTextElementContainer
import com.slack.api.model.kotlin_extension.block.element.dsl.RichTextElementDsl

@BlockLayoutBuilder
class RichTextBlockBuilder private constructor(
private val elementsContainer: MultiRichTextElementContainer
) : Builder<RichTextBlock>, RichTextElementDsl by elementsContainer {
private var blockId: String? = null

constructor() : this(MultiRichTextElementContainer())

/**
* A string acting as a unique identifier for a block. You can use this `block_id` when you receive an interaction
* payload to identify the source of the action. If not specified, one will be generated. Maximum length for this
* field is 255 characters. `block_id` should be unique for each message and each iteration of a message. If a
* message is updated, use a new `block_id`.
*
* @see <a href="https://api.slack.com/reference/block-kit/blocks#rich_text">Rich text block documentation</a>
*/
fun blockId(id: String) {
blockId = id
}

/**
* An array of rich text objects - `rich_text_section`, `rich_text_list`, `rich_text_preformatted`,
* and `rich_text_quote`.
*
* @see <a href="https://api.slack.com/reference/block-kit/blocks#rich_text">Rich text block documentation</a>
*/
fun elements(builder: RichTextElementDsl.() -> Unit) {
elementsContainer.apply(builder)
}

override fun build(): RichTextBlock {
return RichTextBlock.builder()
.blockId(blockId)
.elements(elementsContainer.underlying)
.build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.slack.api.model.kotlin_extension.block.composition.container

import com.slack.api.model.block.element.RichTextElement
import com.slack.api.model.block.element.RichTextSectionElement
import com.slack.api.model.block.element.RichTextSectionElement.LimitedTextStyle
import com.slack.api.model.block.element.RichTextSectionElement.TextStyle
import com.slack.api.model.kotlin_extension.block.composition.dsl.RichTextObjectDsl
import com.slack.api.model.kotlin_extension.block.element.BroadcastRange

class MultiRichTextObjectContainer : RichTextObjectDsl {
val underlying = mutableListOf<RichTextElement>()

override fun broadcast(range: BroadcastRange, style: LimitedTextStyle?) {
underlying += RichTextSectionElement.Broadcast.builder()
.range(range.value)
.style(style)
.build()
}

override fun color(value: String, style: LimitedTextStyle?) {
underlying += RichTextSectionElement.Color.builder()
.value(value)
.style(style)
.build()
}

override fun channel(channelId: String, style: LimitedTextStyle?) {
underlying += RichTextSectionElement.Channel.builder()
.channelId(channelId)
.style(style)
.build()
}

override fun date(timestamp: Int, format: String, style: TextStyle?, url: String?, fallback: String?) {
underlying += RichTextSectionElement.Date.builder()
.timestamp(timestamp)
.format(format)
.style(style)
.url(url)
.fallback(fallback)
.build()
}

override fun emoji(name: String, skinTone: Int?, style: LimitedTextStyle?) {
underlying += RichTextSectionElement.Emoji.builder()
.name(name)
.skinTone(skinTone)
.style(style)
.build()
}

override fun link(url: String, text: String?, unsafe: Boolean?, style: TextStyle?) {
underlying += RichTextSectionElement.Link.builder()
.url(url)
.text(text)
.unsafe(unsafe)
.style(style)
.build()
}

override fun text(text: String, style: TextStyle?) {
underlying += RichTextSectionElement.Text.builder()
.text(text)
.style(style)
.build()
}

override fun user(userId: String, style: LimitedTextStyle?) {
underlying += RichTextSectionElement.User.builder()
.userId(userId)
.style(style)
.build()
}

override fun usergroup(usergroupId: String, style: LimitedTextStyle?) {
underlying += RichTextSectionElement.UserGroup.builder()
.usergroupId(usergroupId)
.style(style)
.build()
}
}
Loading

0 comments on commit c442278

Please sign in to comment.