From e439902053632fc258c141c253a17d6f9c473d19 Mon Sep 17 00:00:00 2001 From: Peiman Jafari Date: Mon, 16 Oct 2023 11:46:22 -0700 Subject: [PATCH 1/2] Add support for Rich Text Input --- block_conv.go | 10 ++++++++++ block_element.go | 30 ++++++++++++++++++++++++++++++ block_element_test.go | 6 ++++++ 3 files changed, 46 insertions(+) diff --git a/block_conv.go b/block_conv.go index 4ab58de3f..42dcd0c85 100644 --- a/block_conv.go +++ b/block_conv.go @@ -114,6 +114,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": @@ -196,6 +198,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": @@ -298,6 +302,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 { diff --git a/block_element.go b/block_element.go index a70d8f2de..49232d540 100644 --- a/block_element.go +++ b/block_element.go @@ -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" @@ -51,6 +52,7 @@ type Accessory struct { DatePickerElement *DatePickerBlockElement TimePickerElement *TimePickerBlockElement PlainTextInputElement *PlainTextInputBlockElement + RichTextInputElement *RichTextInputBlockElement RadioButtonsElement *RadioButtonsBlockElement SelectElement *SelectBlockElement MultiSelectElement *MultiSelectBlockElement @@ -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: @@ -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. // diff --git a/block_element_test.go b/block_element_test.go index 0c47affc3..19a0c964e 100644 --- a/block_element_test.go +++ b/block_element_test.go @@ -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") From 1b0eb7dcbc83d6aebe842e87b6f187077907050c Mon Sep 17 00:00:00 2001 From: Peiman Jafari Date: Tue, 24 Oct 2023 11:08:27 -0700 Subject: [PATCH 2/2] Add rich_text_input block conv --- block_conv.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/block_conv.go b/block_conv.go index 42dcd0c85..48a9d0e9f 100644 --- a/block_conv.go +++ b/block_conv.go @@ -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{} default: