Skip to content

Commit

Permalink
Expose the AwaitMount from Markdown.update (#2838)
Browse files Browse the repository at this point in the history
* Expose the AwaitMount from Markdown.update

* Update CHANGELOG.md

---------

Co-authored-by: Will McGugan <willmcgugan@gmail.com>
  • Loading branch information
darrenburns and willmcgugan authored Jun 27, 2023
1 parent d772106 commit fbb11dc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
13 changes: 9 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased


### Added

- Added can-focus pseudo-class to target widgets that may receive focus
- Make `Markdown.update` optionally awaitable https://github.com/Textualize/textual/pull/2838
- Added `default` parameter to `DataTable.add_column` for populating existing rows https://github.com/Textualize/textual/pull/2836
- Added can-focus pseudo-class to target widgets that may receive focus


### Fixed

- Fixed crash when columns were added to populated `DataTable` https://github.com/Textualize/textual/pull/2836
- Fixed issues with opacity on Screens https://github.com/Textualize/textual/issues/2616
- Fixed style problem with selected selections in a non-focused selection list https://github.com/Textualize/textual/issues/2768

### Added

- Added `default` parameter to `DataTable.add_column` for populating existing rows https://github.com/Textualize/textual/pull/2836
- Added can-focus pseudo-class to target widgets that may receive focus

## [0.28.1] - 2023-06-20

Expand Down
15 changes: 8 additions & 7 deletions src/textual/widgets/_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from ..events import Mount
from ..message import Message
from ..reactive import reactive, var
from ..widget import Widget
from ..widget import AwaitMount, Widget
from ..widgets import Static, Tree

TableOfContentsType: TypeAlias = "list[tuple[int, str, str | None]]"
Expand Down Expand Up @@ -635,14 +635,14 @@ async def load(self, path: Path) -> bool:
path: Path to the document.
Returns:
True on success, or False if the document could not be read.
True on success, or False if the document could not be read.
"""
try:
markdown = path.read_text(encoding="utf-8")
except Exception:
return False

self.update(markdown)
await self.update(markdown)
return True

def unhandled_token(self, token: Token) -> MarkdownBlock | None:
Expand All @@ -656,11 +656,14 @@ def unhandled_token(self, token: Token) -> MarkdownBlock | None:
"""
return None

def update(self, markdown: str) -> None:
def update(self, markdown: str) -> AwaitMount:
"""Update the document with new Markdown.
Args:
markdown: A string containing Markdown.
Returns:
An optionally awaitable object. Await this to ensure that all children have been mounted.
"""
output: list[MarkdownBlock] = []
stack: list[MarkdownBlock] = []
Expand All @@ -670,9 +673,7 @@ def update(self, markdown: str) -> None:
else self._parser_factory()
)

content = Text()
block_id: int = 0

table_of_contents: TableOfContentsType = []

for token in parser.parse(markdown):
Expand Down Expand Up @@ -805,7 +806,7 @@ def update(self, markdown: str) -> None:
self.post_message(Markdown.TableOfContentsUpdated(self, table_of_contents))
with self.app.batch_update():
self.query("MarkdownBlock").remove()
self.mount_all(output)
return self.mount_all(output)


class MarkdownTableOfContents(Widget, can_focus_children=True):
Expand Down

0 comments on commit fbb11dc

Please sign in to comment.