Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sync CKEditor content with textarea for form validation (#251) #259

Merged
merged 2 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 5 additions & 1 deletion example/blog/articles/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ 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 %}
Loading