-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RichTextBlock support to Block Kit Kotlin DSL builder (#1376)
Co-authored-by: Kazuhiro Sera <[email protected]>
- Loading branch information
Showing
23 changed files
with
1,165 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...ension/src/main/kotlin/com/slack/api/model/kotlin_extension/block/RichTextBlockBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
...ck/api/model/kotlin_extension/block/composition/container/MultiRichTextObjectContainer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
Oops, something went wrong.