Skip to content

Commit

Permalink
Merge pull request #107 from parente/rich_text
Browse files Browse the repository at this point in the history
Implement support for rich_text blocks
  • Loading branch information
imryche authored Jan 13, 2024
2 parents 46d3fe3 + 698453b commit e65a95a
Show file tree
Hide file tree
Showing 9 changed files with 470 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,6 @@ dmypy.json
.pyre/

# venv
venv*/
*venv*/

# End of https://www.gitignore.io/api/python
28 changes: 18 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,16 @@ Here's the list of types of components and corresponding classes:

### Blocks

| Type | Class |
| ------- | ---------- |
| actions | Actions |
| context | Context |
| divider | Divider |
| header | Header |
| image | ImageBlock |
| input | Input |
| section | Section |
| Type | Class |
| --------- | ---------- |
| actions | Actions |
| context | Context |
| divider | Divider |
| header | Header |
| image | ImageBlock |
| input | Input |
| rich_text | RichText |
| section | Section |

### Block elements

Expand All @@ -84,13 +85,20 @@ Here's the list of types of components and corresponding classes:
| conversations_select | ConversationsSelect |
| external_select | ExternalSelect |
| timepicker | TimePicker |
| rich_text_list | RichTextList |
| rich_text_preformatted | RichTextPreformatted |
| rich_text_quote | RichTextQuote |
| rich_text_section | RichTextSection |

### Composition objects

| Type | Class |
|------------|----------------------|
| ---------- | -------------------- |
| plain_text | PlainText |
| mrkdwn | MarkdownText |
| text | Text |
| emoji | Emoji |
| | Style |
| | Confirm |
| | PlainOption |
| | MarkdownOption |
Expand Down
33 changes: 32 additions & 1 deletion blockkit/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
Overflow,
PlainTextInput,
RadioButtons,
RichTextList,
RichTextPreformatted,
RichTextQuote,
RichTextSection,
StaticSelect,
TimePicker,
UsersSelect,
Expand All @@ -33,7 +37,16 @@
validator,
)

__all__ = ["Actions", "Context", "Divider", "Header", "ImageBlock", "Input", "Section"]
__all__ = [
"Actions",
"Context",
"Divider",
"Header",
"ImageBlock",
"Input",
"RichText",
"Section",
]

ActionElement = Union[
Button,
Expand Down Expand Up @@ -175,6 +188,24 @@ def __init__(
_validate_hint = validator("hint", validate_text_length, max_length=2000)


RichTextElement = Union[
RichTextList,
RichTextQuote,
RichTextPreformatted,
RichTextSection,
]


class RichText(Block):
type: str = "rich_text"
elements: List[RichTextElement] = Field(..., min_length=1)

def __init__(
self, *, elements: List[RichTextElement], block_id: Optional[str] = None
):
super().__init__(elements=elements, block_id=block_id)


AccessoryElement = Union[
Button,
Checkboxes,
Expand Down
51 changes: 50 additions & 1 deletion blockkit/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
from pydantic.networks import HttpUrl

from blockkit.components import Component
from blockkit.enums import Style
from blockkit.enums import ListStyle, Style
from blockkit.objects import (
Confirm,
DispatchActionConfig,
Emoji,
Filter,
MarkdownOption,
OptionGroup,
PlainOption,
PlainText,
Text,
)
from blockkit.validators import (
validate_date,
Expand Down Expand Up @@ -45,6 +47,10 @@
"TimePicker",
"UsersSelect",
"NumberInput",
"RichTextPreformatted",
"RichTextQuote",
"RichTextSection",
"RichTextList",
]


Expand Down Expand Up @@ -668,3 +674,46 @@ def __init__(
_validate_placeholder = validator(
"placeholder", validate_text_length, max_length=150
)


RichTextObject = Union[Text, Emoji]


class RichTextPreformatted(Component):
type: str = "rich_text_preformatted"
elements: List[RichTextObject] = Field(..., min_length=1)

def __init__(self, *, elements: List[RichTextObject]):
super().__init__(elements=elements)


class RichTextQuote(Component):
type: str = "rich_text_quote"
elements: List[RichTextObject] = Field(..., min_length=1)

def __init__(self, *, elements: List[RichTextObject]):
super().__init__(elements=elements)


class RichTextSection(Component):
type: str = "rich_text_section"
elements: List[RichTextObject] = Field(..., min_length=1)

def __init__(self, *, elements: List[RichTextObject]):
super().__init__(elements=elements)


class RichTextList(Component):
type: str = "rich_text_list"
elements: List[RichTextSection] = Field(..., min_length=1)
style: Optional[ListStyle] = None
indent: Optional[int] = Field(..., ge=0, le=8)

def __init__(
self,
*,
elements: List[RichTextSection],
style: Optional[ListStyle] = None,
indent: Optional[int] = None,
):
super().__init__(elements=elements, style=style, indent=indent)
7 changes: 6 additions & 1 deletion blockkit/enums.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from enum import Enum

__all__ = ["Style", "TriggerActionsOn", "Include"]
__all__ = ["ListStyle", "Style", "TriggerActionsOn", "Include"]


class ListStyle(str, Enum):
ordered = "ordered"
bullet = "bullet"


class Style(str, Enum):
Expand Down
43 changes: 40 additions & 3 deletions blockkit/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pydantic import AnyUrl, Field, model_validator

from blockkit.components import Component
from blockkit.enums import Include, Style, TriggerActionsOn
from blockkit.enums import Include, Style as ConfirmStyle, TriggerActionsOn
from blockkit.validators import validate_text_length, validator

__all__ = [
Expand All @@ -15,6 +15,9 @@
"MarkdownOption",
"OptionGroup",
"PlainText",
"Text",
"Style",
"Emoji",
]


Expand All @@ -36,12 +39,46 @@ def __init__(self, *, text: str, emoji: Optional[bool] = None):
super().__init__(text=text, emoji=emoji)


class Style(Component):
bold: Optional[bool] = None
italic: Optional[bool] = None
strike: Optional[bool] = None
code: Optional[bool] = None

def __init__(
self,
*,
bold: Optional[bool] = None,
italic: Optional[bool] = None,
strike: Optional[bool] = None,
code: Optional[bool] = None,
):
super().__init__(bold=bold, italic=italic, strike=strike, code=code)


class Text(Component):
type: str = "text"
text: str = Field(..., min_length=1)
style: Optional[Style] = None

def __init__(self, *, text: str, style: Optional[Style] = None):
super().__init__(text=text, style=style)


class Emoji(Component):
type: str = "emoji"
name: str = Field(..., min_length=1)

def __init__(self, *, name: str):
super().__init__(name=name)


class Confirm(Component):
title: Union[PlainText, str]
text: Union[PlainText, MarkdownText, str]
confirm: Union[PlainText, str]
deny: Union[PlainText, str]
style: Optional[Style] = None
style: Optional[ConfirmStyle] = None

def __init__(
self,
Expand All @@ -50,7 +87,7 @@ def __init__(
text: Union[PlainText, MarkdownText, str],
confirm: Union[PlainText, str],
deny: Union[PlainText, str],
style: Optional[Style] = None,
style: Optional[ConfirmStyle] = None,
):
super().__init__(
title=title, text=text, confirm=confirm, deny=deny, style=style
Expand Down
Loading

0 comments on commit e65a95a

Please sign in to comment.