forked from zauberzeug/nicegui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
1,438 additions
and
33 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export default { | ||
template: `<div></div>`, | ||
mounted() { | ||
this.update(this.$el.innerHTML); | ||
}, | ||
methods: { | ||
update(content) { | ||
this.$el.innerHTML = content; | ||
this.$el.querySelectorAll(".mermaid-pre").forEach((pre, i) => { | ||
const code = decodeHtml(pre.children[0].innerHTML); | ||
mermaid.render(`mermaid_${this.$el.id}_${i}`, code, (svg) => (pre.innerHTML = svg)); | ||
}); | ||
}, | ||
}, | ||
}; | ||
|
||
function decodeHtml(html) { | ||
const txt = document.createElement("textarea"); | ||
txt.innerHTML = html; | ||
return txt.value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export default { | ||
template: `<div></div>`, | ||
mounted() { | ||
this.update(this.$el.innerText); | ||
}, | ||
methods: { | ||
update(content) { | ||
mermaid.render("mermaid" + this.$el.id, content, (svg) => (this.$el.innerHTML = svg)); | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from ..dependencies import register_component | ||
from .mixins.content_element import ContentElement | ||
|
||
register_component('mermaid', __file__, 'mermaid.js', ['lib/mermaid.min.js']) | ||
|
||
|
||
class Mermaid(ContentElement): | ||
|
||
def __init__(self, content: str) -> None: | ||
'''Mermaid Diagrams | ||
Renders diagrams and charts written in the Markdown-inspired `Mermaid <https://mermaid.js.org/>`_ language. | ||
:param content: the Mermaid content to be displayed | ||
''' | ||
super().__init__(tag='mermaid', content=content) | ||
|
||
def on_content_change(self, content: str) -> None: | ||
self._props['innerHTML'] = content | ||
self.run_method('update', content) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from nicegui import ui | ||
|
||
from .screen import Screen | ||
|
||
|
||
def test_markdown(screen: Screen): | ||
m = ui.markdown('This is **markdown**') | ||
|
||
screen.open('/') | ||
element = screen.find('This is') | ||
assert element.text == 'This is markdown' | ||
assert element.get_attribute('innerHTML') == 'This is <strong>markdown</strong>' | ||
|
||
m.set_content('New **content**') | ||
element = screen.find('New') | ||
assert element.text == 'New content' | ||
assert element.get_attribute('innerHTML') == 'New <strong>content</strong>' | ||
|
||
|
||
def test_markdown_with_mermaid(screen: Screen): | ||
m = ui.markdown(''' | ||
Mermaid: | ||
```mermaid | ||
graph TD; | ||
Node_A --> Node_B; | ||
``` | ||
''', extras=['mermaid', 'fenced-code-blocks']) | ||
|
||
screen.open('/') | ||
screen.should_contain('Mermaid') | ||
assert screen.find_by_tag('svg').get_attribute('id') == f'mermaid_{m.id}_0' | ||
assert screen.find('Node_A').get_attribute('class') == 'nodeLabel' | ||
|
||
m.set_content(''' | ||
New: | ||
```mermaid | ||
graph TD; | ||
Node_C --> Node_D; | ||
``` | ||
''') | ||
screen.should_contain('New') | ||
assert screen.find('Node_C').get_attribute('class') == 'nodeLabel' | ||
screen.should_not_contain('Node_A') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from nicegui import ui | ||
|
||
from .screen import Screen | ||
|
||
|
||
def test_mermaid(screen: Screen): | ||
m = ui.mermaid(''' | ||
graph TD; | ||
Node_A --> Node_B; | ||
''') | ||
|
||
screen.open('/') | ||
assert screen.find('Node_A').get_attribute('class') == 'nodeLabel' | ||
|
||
m.set_content(''' | ||
graph TD; | ||
Node_C --> Node_D; | ||
''') | ||
assert screen.find('Node_C').get_attribute('class') == 'nodeLabel' | ||
screen.should_not_contain('Node_A') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters