Skip to content

Commit

Permalink
new hooks included
Browse files Browse the repository at this point in the history
  • Loading branch information
surajsinghbisht054 committed Dec 1, 2024
1 parent f5e8c97 commit c152e9a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ python manage.py migrate
python manage.py collectstatic
```


### 6. Add static tag
```html
<head>
....
{{form.media}}
....
</head>
```

## Configuration

### Advanced Configuration Options
Expand Down
16 changes: 15 additions & 1 deletion django_editorjs2/blogapp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,18 @@ def callback_before_return_response(response):
Callback fixes the response before returning it.
"""
print(response)
return response
return response

def editorjs_field_preview_callback(value):
"""
Preprocess value for widget preview.
"""
print(value)
return value

def editorjs_field_save_callback(value):
"""
Preprocess value before saving it.
"""
print(value)
return value
18 changes: 18 additions & 0 deletions django_editorjs2/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
from django_editorjs2.block_processor import converter
from django.conf import settings
from django.utils.module_loading import import_string
import json

converter.image_link_preprocessor = lambda x: x
converter.download_link_preprocessor = lambda x: x
editorjs_field_preview_callback = lambda x: x
editorjs_field_save_callback = lambda x: x

converter.extra_attributes = {
'paragraph': {},
'header': {},
Expand All @@ -32,6 +37,12 @@
if 'extra_attributes' in settings.DJANGO_EDITORJS2_CONFIG:
converter.extra_attributes.update(settings.DJANGO_EDITORJS2_CONFIG['extra_attributes'])

if 'editorjs_field_preview_callback' in settings.DJANGO_EDITORJS2_CONFIG:
editorjs_field_preview_callback = import_string(settings.DJANGO_EDITORJS2_CONFIG['editorjs_field_preview_callback'])

if 'editorjs_field_save_callback' in settings.DJANGO_EDITORJS2_CONFIG:
editorjs_field_save_callback = import_string(settings.DJANGO_EDITORJS2_CONFIG['editorjs_field_save_callback'])


class EditorJsWidget(forms.Widget):
template_name = "django_editorjs2/widget/editorjs.html"
Expand All @@ -51,11 +62,18 @@ class Media:
static("django_editorjs2/quote.editorjs.min.js"),
static("django_editorjs2/table.editorjs.min.js"),
)

def format_value(self, value):
return editorjs_field_preview_callback(json.loads(value or {}))


class EditorJSFormField(JSONFormField):
def __init__(self, *args, **kwargs):
kwargs["widget"] = EditorJsWidget
super().__init__(*args, **kwargs)

def clean(self, value):
return editorjs_field_save_callback(super().clean(value))


class EditorJSField(models.JSONField):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@
}
).then(response => response.json())
}
const initial_json = JSON.parse(JSON.parse(document.getElementById('{{editorJs_initial_json}}').textContent))|| {};
console.log(initial_json);
const initial_json = JSON.parse(document.getElementById('{{editorJs_initial_json}}').textContent)|| {};
const editor = new EditorJS({
holder: 'editorjs-{{ widget.name }}',
autofocus: true,
Expand Down
4 changes: 4 additions & 0 deletions example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,8 @@
# before returning the response, the response object is passed to this function
"callback_before_return_response": "django_editorjs2.blogapp.utils.callback_before_return_response",

# widget
"editorjs_field_preview_callback": "django_editorjs2.blogapp.utils.editorjs_field_preview_callback",
"editorjs_field_save_callback": "django_editorjs2.blogapp.utils.editorjs_field_save_callback",

}

0 comments on commit c152e9a

Please sign in to comment.