Skip to content

Commit

Permalink
Merge pull request #1240 from peimanja/rich-text-input
Browse files Browse the repository at this point in the history
Add support for Rich Text Input
  • Loading branch information
parsley42 committed Feb 10, 2024
2 parents f6b09b8 + 1b0eb7d commit 9d132f3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
12 changes: 12 additions & 0 deletions block_conv.go
Expand Up @@ -66,6 +66,8 @@ func (b *Blocks) UnmarshalJSON(data []byte) error {
block = &InputBlock{}
case "rich_text":
block = &RichTextBlock{}
case "rich_text_input":
block = &RichTextBlock{}
case "section":
block = &SectionBlock{}
case "video":
Expand Down Expand Up @@ -116,6 +118,8 @@ func (b *InputBlock) UnmarshalJSON(data []byte) error {
e = &DateTimePickerBlockElement{}
case "plain_text_input":
e = &PlainTextInputBlockElement{}
case "rich_text_input":
e = &RichTextInputBlockElement{}
case "email_text_input":
e = &EmailTextInputBlockElement{}
case "url_text_input":
Expand Down Expand Up @@ -198,6 +202,8 @@ func (b *BlockElements) UnmarshalJSON(data []byte) error {
blockElement = &DateTimePickerBlockElement{}
case "plain_text_input":
blockElement = &PlainTextInputBlockElement{}
case "rich_text_input":
blockElement = &RichTextInputBlockElement{}
case "email_text_input":
blockElement = &EmailTextInputBlockElement{}
case "url_text_input":
Expand Down Expand Up @@ -300,6 +306,12 @@ func (a *Accessory) UnmarshalJSON(data []byte) error {
return err
}
a.PlainTextInputElement = element.(*PlainTextInputBlockElement)
case "rich_text_input":
element, err := unmarshalBlockElement(r, &RichTextInputBlockElement{})
if err != nil {
return err
}
a.RichTextInputElement = element.(*RichTextInputBlockElement)
case "radio_buttons":
element, err := unmarshalBlockElement(r, &RadioButtonsBlockElement{})
if err != nil {
Expand Down
30 changes: 30 additions & 0 deletions block_element.go
Expand Up @@ -12,6 +12,7 @@ const (
METDatetimepicker MessageElementType = "datetimepicker"
METPlainTextInput MessageElementType = "plain_text_input"
METRadioButtons MessageElementType = "radio_buttons"
METRichTextInput MessageElementType = "rich_text_input"
METEmailTextInput MessageElementType = "email_text_input"
METURLTextInput MessageElementType = "url_text_input"
METNumber MessageElementType = "number_input"
Expand Down Expand Up @@ -51,6 +52,7 @@ type Accessory struct {
DatePickerElement *DatePickerBlockElement
TimePickerElement *TimePickerBlockElement
PlainTextInputElement *PlainTextInputBlockElement
RichTextInputElement *RichTextInputBlockElement
RadioButtonsElement *RadioButtonsBlockElement
SelectElement *SelectBlockElement
MultiSelectElement *MultiSelectBlockElement
Expand All @@ -73,6 +75,8 @@ func NewAccessory(element BlockElement) *Accessory {
return &Accessory{TimePickerElement: element.(*TimePickerBlockElement)}
case *PlainTextInputBlockElement:
return &Accessory{PlainTextInputElement: element.(*PlainTextInputBlockElement)}
case *RichTextInputBlockElement:
return &Accessory{RichTextInputElement: element.(*RichTextInputBlockElement)}
case *RadioButtonsBlockElement:
return &Accessory{RadioButtonsElement: element.(*RadioButtonsBlockElement)}
case *SelectBlockElement:
Expand Down Expand Up @@ -509,6 +513,32 @@ func NewPlainTextInputBlockElement(placeholder *TextBlockObject, actionID string
}
}

// RichTextInputBlockElement creates a field where allows users to enter formatted text
// in a WYSIWYG composer, offering the same messaging writing experience as in Slack
// More Information: https://api.slack.com/reference/block-kit/block-elements#rich_text_input
type RichTextInputBlockElement struct {
Type MessageElementType `json:"type"`
ActionID string `json:"action_id,omitempty"`
Placeholder *TextBlockObject `json:"placeholder,omitempty"`
InitialValue string `json:"initial_value,omitempty"`
DispatchActionConfig *DispatchActionConfig `json:"dispatch_action_config,omitempty"`
FocusOnLoad bool `json:"focus_on_load,omitempty"`
}

// ElementType returns the type of the Element
func (s RichTextInputBlockElement) ElementType() MessageElementType {
return s.Type
}

// NewRichTextInputBlockElement returns an instance of a rich-text input element
func NewRichTextInputBlockElement(placeholder *TextBlockObject, actionID string) *RichTextInputBlockElement {
return &RichTextInputBlockElement{
Type: METRichTextInput,
ActionID: actionID,
Placeholder: placeholder,
}
}

// CheckboxGroupsBlockElement defines an element which allows users to choose
// one or more items from a list of possible options.
//
Expand Down
6 changes: 6 additions & 0 deletions block_element_test.go
Expand Up @@ -148,6 +148,12 @@ func TestNewPlainTextInputBlockElement(t *testing.T) {

}

func TestNewRichTextInputBlockElement(t *testing.T) {
richTextInputElement := NewRichTextInputBlockElement(nil, "test")
assert.Equal(t, string(richTextInputElement.Type), "rich_text_input")
assert.Equal(t, richTextInputElement.ActionID, "test")
}

func TestNewEmailTextInputBlockElement(t *testing.T) {
emailTextInputElement := NewEmailTextInputBlockElement(nil, "example@example.com")

Expand Down

0 comments on commit 9d132f3

Please sign in to comment.