Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for rich text input #20

Merged
merged 3 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions block.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type BlockAction struct {
Type ActionType `json:"type"`
Text TextBlockObject `json:"text"`
Value string `json:"value"`
RichTextValue RichTextBlock `json:"rich_text_value"`
ActionTs string `json:"action_ts"`
SelectedOption OptionBlockObject `json:"selected_option"`
SelectedOptions []OptionBlockObject `json:"selected_options"`
Expand Down
12 changes: 12 additions & 0 deletions block_conv.go
Original file line number Diff line number Diff line change
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 "call":
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 = &EmailInputBlockElement{}
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 = &EmailInputBlockElement{}
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
36 changes: 33 additions & 3 deletions block_element.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
METEmailInput MessageElementType = "email_text_input"
METNumberInput MessageElementType = "number_input"
METURLInput MessageElementType = "url_text_input"
METRichTextInput MessageElementType = "rich_text_input"

MixedElementImage MixedElementType = "mixed_image"
MixedElementText MixedElementType = "mixed_text"
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 @@ -470,9 +474,9 @@ func (s NumberInputBlockElement) ElementType() MessageElementType {
// NewNumberInputBlockElement returns an instance of a number input.
func NewNumberInputBlockElement(placeholder *TextBlockObject, actionID string, isDecimalAllowed bool) *NumberInputBlockElement {
return &NumberInputBlockElement{
Type: METNumberInput,
ActionID: actionID,
Placeholder: placeholder,
Type: METNumberInput,
ActionID: actionID,
Placeholder: placeholder,
IsDecimalAllowed: isDecimalAllowed,
}
}
Expand Down Expand Up @@ -512,6 +516,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,
}
}

// URLInputBlockElement creates a field where a user can enter a URL.
// URL input elements are currently only available in modals.
//
Expand Down
5 changes: 5 additions & 0 deletions block_element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,12 @@ func TestNewEmailInputBlockElement(t *testing.T) {

assert.Equal(t, string(emailInputElement.Type), "email_text_input")
assert.Equal(t, emailInputElement.ActionID, "test")
}

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 TestNewNumberInputBlockElement(t *testing.T) {
Expand Down
Loading