__init__.py 3.76 KB
Newer Older
Fabien Amarger's avatar
Fabien Amarger committed
1
2
3
4
5
"""cubicweb-editorjs application package

Add editorjs format for RichString
"""

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
import copy

from logilab.common.decorators import monkeypatch

from cubicweb import cwconfig, entity
from cubicweb.web import formfields, formwidgets as fw
from cubicweb.web.views import forms

from yams import constraints

NEW_PERSISTENT_OPTIONS = []
for option in cwconfig.PERSISTENT_OPTIONS:
    if option[0] == "default-text-format":
        option = ("default-text-format", copy.deepcopy(option[1]))
        option[1]["choices"] += ("application/vnd.cubicweb.editorjs",)
    NEW_PERSISTENT_OPTIONS.append(option)


formfields.EditableFileField.editable_formats += ("application/vnd.cubicweb.editorjs",)

forms.FieldsForm.needs_js += ("cubes.editorjs.js",)

constraints.FormatConstraint.regular_formats += ("application/vnd.cubicweb.editorjs",)


def use_editorjs(rich_text_field, form):
    """return True if editor.js should be used to edit entity's attribute named
    `attr`
    """
    return rich_text_field.format(form) == "application/vnd.cubicweb.editorjs"


class EditorJS(fw.TextArea):
    """EditorJS enabled <textarea>, will return a unicode string containing
    HTML formated text.
    """

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.attrs["cubicweb:type"] = "editorjs"

    def _render(self, form, field, renderer):
        return super()._render(form, field, renderer)


old_get_widget = formfields.RichTextField.get_widget
52
53


54
55
56
57
58
59
60
61
62
@monkeypatch(formfields.RichTextField)
def get_widget(self, form):
    if self.widget is None:
        if use_editorjs(self, form):
            return EditorJS()
    return old_get_widget(self, form)


old_get_format_field = formfields.RichTextField.get_format_field
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
@monkeypatch(formfields.RichTextField)
def get_format_field(self, form):
    if self.format_field:
        return self.format_field
    # we have to cache generated field since it's use as key in the
    # context dictionary
    req = form._cw
    try:
        return req.data[self]
    except KeyError:
        fkwargs = {"eidparam": self.eidparam, "role": self.role}
        if use_editorjs(self, form):
            fkwargs["widget"] = fw.HiddenInput()
            fkwargs["value"] = "application/vnd.cubicweb.editorjs"
            fkwargs["eidparam"] = self.eidparam
            field = formfields.StringField(name=self.name + "_format", **fkwargs)
            req.data[self] = field
            return field
        else:
            return old_get_format_field(self, form)


old_printable_value = entity.Entity.printable_value
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
@monkeypatch(entity.Entity)
def printable_value(
    self,
    attr,
    value=entity._marker,
    attrtype=None,
    format="text/html",
    displaytime=True,
):  # XXX cw_printable_value
    """return a displayable value (i.e. unicode string) which may contains
    html tags
    """
    attr = str(attr)
    if value is entity._marker:
        value = getattr(self, attr)
    if isinstance(value, str):
        value = value.strip()
    if value is None or value == "":  # don't use "not", 0 is an acceptable value
        return ""
    if attrtype is None:
        attrtype = self.e_schema.destination(attr)
    props = self.e_schema.rdef(attr)
    if attrtype == "String":
        # internalinalized *and* formatted string such as schema
        # description...
        if props.internationalizable:
            value = self._cw._(value)
        attrformat = self.cw_attr_metadata(attr, "format")
        if attrformat:
            if attrformat == "application/vnd.cubicweb.editorjs":
                self._cw.add_js("cubes.editorjs.js")
                return f'<textarea cubicweb:type="editorjs" cubicweb:mode="read">{value}</textarea>'
122
    return old_printable_value(self, attr, value, attrtype, format, displaytime)
123

Fabien Amarger's avatar
Fabien Amarger committed
124
125
126

def includeme(config):
    pass