Skip to content

Commit

Permalink
fix: sync CKEditor content with textarea for form validation (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
hvlads committed Oct 12, 2024
1 parent 25e4c37 commit 5373f6b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
17 changes: 16 additions & 1 deletion django_ckeditor_5/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions django_ckeditor_5/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "webpack --mode development",
"prod": "webpack --mode production",
"prod": "webpack --mode production"
},
"author": "",
"license": "BSD-3-Clause",
Expand Down Expand Up @@ -47,7 +47,8 @@
"@ckeditor/ckeditor5-theme-lark": "^43.2.0",
"@ckeditor/ckeditor5-ui": "^43.2.0",
"@ckeditor/ckeditor5-upload": "^43.2.0",
"@ckeditor/ckeditor5-word-count": "^43.2.0"
"@ckeditor/ckeditor5-word-count": "^43.2.0",
"@pikulinpw/ckeditor5-fullscreen": "^1.0.1"
},
"devDependencies": {
"@ckeditor/ckeditor5-core": "^43.2.0",
Expand Down
6 changes: 5 additions & 1 deletion django_ckeditor_5/static/django_ckeditor_5/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ function createEditors(element = document.body) {
editorEl,
config
).then(editor => {
const textarea = document.querySelector(`#${editorEl.id}`);
editor.model.document.on('change:data', () => {
textarea.value = editor.getData();
});
if (editor.plugins.has('WordCount')) {
const wordCountPlugin = editor.plugins.get('WordCount');
const wordCountWrapper = element.querySelector(`#${script_id}-word-count`);
Expand Down Expand Up @@ -156,7 +160,7 @@ document.addEventListener("DOMContentLoaded", () => {
createEditors();

if (typeof django === "object" && django.jQuery) {
django.jQuery(document).on("formset:added", () => {createEditors()});
django.jQuery(document).on("formset:added", () => {createEditors();});
}

const observer = new MutationObserver((mutations) => {
Expand Down
2 changes: 1 addition & 1 deletion example/blog/articles/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Meta:
fields = ("author", "text")
widgets = {
"text": CKEditor5Widget(
attrs={"class": "django_ckeditor_5"},
attrs={"class": "django_ckeditor_5", "name": "message", "required": True},
config_name="comment",
),
}
Expand Down
12 changes: 7 additions & 5 deletions example/blog/articles/templates/articles/article_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ <h1>{{ object.title }}</h1>
{% endfor %}
</div>
</div>
<form method="POST">
{% csrf_token %}
{{ form.text }}
<button type="submit">Submit comment</button>
</form>
<form method="POST">
{% csrf_token %}
<label for="{{ form.author.id_for_label }}">Author</label>
{{ form.author }}
{{ form.text }}
<button type="submit">Submit comment</button>
</form>
{% endblock %}

0 comments on commit 5373f6b

Please sign in to comment.