File size: 3,686 Bytes
e2f925b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import os
import streamlit.components.v1 as components

_RELEASE = True

if not _RELEASE:
    _st_quill = components.declare_component("streamlit_quill", url="http://localhost:3001")
else:
    parent_dir = os.path.dirname(os.path.abspath(__file__))
    build_dir = os.path.join(parent_dir, "frontend/build")
    _st_quill = components.declare_component("streamlit_quill", path=build_dir)


def st_quill(
    value="",
    placeholder="",
    html=False,
    toolbar=None,
    history=None,
    preserve_whitespace=True,
    readonly=False,
    key=None
):
    """Quill Editor component.

    Parameters
    ----------
    value : any
        The text value of this widget when it first renders. This will be
        cast to str internally.
    placeholder : any
        The text value of this widget when the editor is empty. It will be
        cast to str internally.
    html : bool
        Choose whether component return editor's content as HTML or regular
        text. Return regular text by default.
    toolbar : list or None
        Quill toolbar configuration. For more information, see
        https://quilljs.com/docs/modules/toolbar/.
    history : dict or None
        Quill history configuration. For more information, see
        https://quilljs.com/docs/modules/history/.
    preserve_whitespace : bool
        Choose whether multiple spaces are preserved on copy/paste or trimmed.
        Spaces are preserved by default.
    readonly : bool
        Make the editor read only.
    key: str or None
        An optional key that uniquely identifies this component. If this is
        None, and the component's arguments are changed, the component will
        be re-mounted in the Streamlit frontend and lose its current state.

    Returns
    -------
    str
        The current content of Quill editor.

    """
    if toolbar is None:
        toolbar=[
            [
                "bold", "italic", "underline", "strike",
                {"script": "sub"},
                {"script": "super"},
            ],
            [
                {"background": []},
                {"color": [] },
            ],          
            [
                {"list": "ordered"},
                {"list": "bullet"},
                {"indent": "-1"},
                {"indent": "+1"},
                {"align": []},
            ],
            [
                {"header": 1},
                {"header": 2},
                {"header": [1, 2, 3, 4, 5, 6, False]},
                {"size": ["small", False, "large", "huge"]},
            ],
            [
                "formula", "blockquote", "code", "code-block", "clean"
            ],
            [
                "link", "image"
            ],
            [
                {"font": []}
            ],
        ]
    
    if history is None:
        history={
            "delay": 1000,
            "maxStack": 500,
            "userOnly": False
        }

    return _st_quill(
        defaultValue=str(value),
        placeholder=str(placeholder),
        html=html,
        toolbar=toolbar,
        history=history,
        preserveWhitespace=preserve_whitespace,
        readOnly=readonly or False,
        name=key or "quill",
        key=key,
        default=str(value),
    )


if not _RELEASE:
    import streamlit as st

    st.sidebar.title(":computer: Quill Editor")
    placeholder = st.sidebar.text_input("Placeholder", "Some placeholder text")
    html = st.sidebar.checkbox("Return HTML", False)
    read_only = st.sidebar.checkbox("Read only", False)

    content = st_quill(
        placeholder=placeholder,
        html=html,
        readonly=read_only,
    )

    st.write(content)

# content = st_quill()