Skip to content

Commit

Permalink
Merge pull request #1651 from zauberzeug/editor
Browse files Browse the repository at this point in the history
Introduce ui.editor
  • Loading branch information
rodja authored Sep 21, 2023
2 parents e1a5420 + 0f117e9 commit 3edcb78
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
25 changes: 25 additions & 0 deletions nicegui/elements/editor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from typing import Any, Callable, Optional

from .mixins.disableable_element import DisableableElement
from .mixins.value_element import ValueElement


class Editor(ValueElement, DisableableElement):

def __init__(self,
*,
placeholder: Optional[str] = None,
value: str = '',
on_change: Optional[Callable[..., Any]] = None,
) -> None:
"""Editor
A WYSIWYG editor based on `Quasar's QEditor <https://quasar.dev/vue-components/editor>`_.
The value is a string containing the formatted text as HTML code.
:param value: initial value
:param on_change: callback to be invoked when the value changes
"""
super().__init__(tag='q-editor', value=value, on_value_change=on_change)
if placeholder is not None:
self._props['placeholder'] = placeholder
2 changes: 2 additions & 0 deletions nicegui/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'date',
'dialog',
'echart',
'editor',
'expansion',
'grid',
'html',
Expand Down Expand Up @@ -118,6 +119,7 @@
from .elements.date import Date as date
from .elements.dialog import Dialog as dialog
from .elements.echart import EChart as echart
from .elements.editor import Editor as editor
from .elements.expansion import Expansion as expansion
from .elements.grid import Grid as grid
from .elements.html import Html as html
Expand Down
14 changes: 14 additions & 0 deletions tests/test_editor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

from nicegui import ui

from .screen import Screen


def test_editor(screen: Screen):
editor = ui.editor(placeholder='Type something here')
ui.markdown().bind_content_from(editor, 'value', backward=lambda v: f'HTML code:\n```\n{v}\n```')

screen.open('/')
screen.find_by_class('q-editor__content').click()
screen.type('Hello\nworld!')
screen.should_contain('Hello<div>world!</div>')
1 change: 1 addition & 0 deletions website/documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def captions_and_overlays_demo():
load_demo(ui.scene)
load_demo(ui.tree)
load_demo(ui.log)
load_demo(ui.editor)
load_demo(ui.code)
load_demo(ui.json_editor)

Expand Down
7 changes: 7 additions & 0 deletions website/more_documentation/editor_documentation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from nicegui import ui


def main_demo() -> None:
editor = ui.editor(placeholder='Type something here')
ui.markdown().bind_content_from(editor, 'value',
backward=lambda v: f'HTML code:\n```\n{v}\n```')

0 comments on commit 3edcb78

Please sign in to comment.