Skip to content

Commit 3fa57a6

Browse files
committed
Update the customization section
1 parent c376dd6 commit 3fa57a6

File tree

1 file changed

+50
-22
lines changed

1 file changed

+50
-22
lines changed

README.rst

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -91,40 +91,68 @@ above.
9191
Customization
9292
=============
9393

94-
It's possible to slightly customize the field or widget by passing an optional
95-
``config`` dictionary. The default configuration is:
94+
**NOTE!** The previous way of customizing the editor is still supported, but
95+
it's not recommended (and documented) anymore.
96+
97+
The editor can be customized using presets; the way to do this is by adding a list of presets to your Django settings:
9698

9799
.. code-block:: python
98100
99-
config = {
100-
"types": None, # Allow all nodes and marks
101-
"history": True, # Enable undo and redo
102-
"html": True, # Add a button which allows editing the raw HTML
103-
"typographic": True, # Highlight typographic characters
104-
"headingLevels": [1, 2, 3, 4, 5], # Available heading levels
101+
DJANGO_PROSE_EDITOR_PRESETS = {
102+
"announcements": {
103+
"script": "prose-editors/announcements.js",
104+
},
105105
}
106106
107-
If you only want to support paragraphs, strong, emphasis, sub- and superset and
108-
no history or HTML editing you could add the following field:
107+
The preset can be selected when instantiating the field:
109108

110109
.. code-block:: python
111110
112-
text = SanitizedProseEditorField(
113-
config={"types": ["strong", "em", "sub", "sup"]},
114-
)
111+
text = ProseEditorField(_("text"), preset="announcements")
112+
113+
The announcements file is expected to initialize the editor by using the
114+
``DjangoProseEditor`` library which is available on the browser ``window``
115+
global. The example looks a bit involved, and it is -- but, this unlocks the
116+
capability to add project-specific extensions without having to rebuild the
117+
editor foundations:
118+
119+
.. code-block:: javascript
120+
121+
// "announcements" is the name of the preset.
122+
const marker = "data-django-prose-editor-announcements"
123+
124+
function createEditor(textarea) {
125+
if (textarea.closest(".prose-editor")) return
126+
const config = JSON.parse(textarea.getAttribute(marker))
127+
128+
const {
129+
// Always recommended:
130+
Document, Dropcursor, Gapcursor, Paragraph, HardBreak, Text,
115131
116-
Paragraphs cannot be removed at the moment. Note that the backend doesn't
117-
sanitize the content to ensure that the HTML doesn't contain only the provided
118-
tags, that's out of scope for now.
132+
// Add support for a few marks:
133+
Bold, Italic, Subscript, Superscript, Link,
119134
120-
``doc``, ``paragraph`` and ``text`` are always in the allowlist.
135+
// A menu is always nice:
136+
Menu,
121137
122-
The supported node types are ``heading``, ``blockquote``, ``horizontal_rule``
123-
and ``hard_break``. List nodes are ``ordered_list``, ``bullet_list`` and
124-
``list_item``.
138+
// Helper which knows how to attach a prose editor to a textarea,
139+
// either textareas which exist already on page load and also those
140+
// added through the Django admin's inlines mechanism:
141+
createTextareaEditor,
142+
} = window.DjangoProseEditor
143+
144+
const extensions = [
145+
Document, Dropcursor, Gapcursor, Paragraph, HardBreak, Text,
146+
147+
Bold, Italic, Subscript, Superscript, Link.configure({ openOnClick: false }),
148+
149+
Menu.configure({ config }),
150+
]
151+
152+
return createTextareaEditor(textarea, extensions)
153+
}
125154
126-
The supported mark types are ``link``, ``strong``, ``em``, ``underline``,
127-
``strikethrough``, ``sub`` and ``sup``
155+
window.DjangoProseEditor.initializeEditors(createEditor, `[${marker}]`)
128156
129157
130158
Usage outside the Django admin

0 commit comments

Comments
 (0)