Skip to content

Commit 09b36ec

Browse files
authored
Merge pull request #12 from octue/feat/svelte-jsoneditor-widget
FEA: svelte editor widget
2 parents 4df3cfa + c4af1b2 commit 09b36ec

File tree

10 files changed

+211
-8
lines changed

10 files changed

+211
-8
lines changed

README.md

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,54 @@ A plug in widget for django's JSONField that allows manipulation and display of
99

1010
This app is a replacement for `django-jsoneditor` (which uses a deprecated version of the widget, `jsoneditor` - you can [see the differences here](https://github.com/josdejong/svelte-jsoneditor#differences-between-josdejongsvelte-jsoneditor-and-josdejongjsoneditor)). You can read about why we're not directly contributing to `django-jsoneditor` [here](https://github.com/nnseva/django-jsoneditor/issues/71), the two projects might merge in the future.
1111

12-
## Documentation...
12+
## Documentation
1313

14-
(working on it)
14+
### Installation
15+
16+
To get an application installed in your project, you need to add `django_svelte_jsoneditor` into `INSTALLED_APPS` in `settings.py` file.
17+
18+
```python
19+
# settings.py
20+
21+
INSTALLED_APPS = [
22+
# Other Django applications
23+
"django_svelte_jsoneditor",
24+
]
25+
```
26+
27+
### Usage
28+
29+
The application contains new widget called `SvelteJSONEditorWidget` which adds editor capabilities to JSON fields in Django. Below you can see an example, of how to override the default widget for the JSON field (in this case textarea widget).
30+
31+
```python
32+
# admin.py
33+
34+
from django.contrib import admin
35+
from django_svelte_jsoneditor.widgets import SvelteJSONEditorWidget
36+
37+
from .models import MyModel
38+
39+
40+
@admin.register(MyModel)
41+
class MyModelAdmin(admin.ModelAdmin):
42+
formfield_overrides = {
43+
models.JSONField: {
44+
"widget": SvelteJSONEditorWidget,
45+
}
46+
}
47+
```
48+
49+
Another example is how to create a new Django form integrating `SvelteJSONEditorWidget` replacing `Textarea` widget.
50+
51+
```python
52+
# forms.py
53+
from django import forms
54+
from django_svelte_jsoneditor.widgets import SvelteJSONEditorWidget
55+
56+
57+
class MyJSONEditorForm(forms.Form):
58+
json = forms.JSONField(widgets=SvelteJSONEditorWidget())
59+
```
1560

1661
## About Svelte
1762

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.svelte-jsoneditor-wrapper {
2+
min-height: 480px;
3+
width: 640px;
4+
}
5+
6+
.jse-modal tr:nth-child(2n+1),
7+
.jse-modal tr:nth-child(2n) {
8+
background-color: inherit;
9+
border-bottom: inherit;
10+
}
11+
12+
table.jse-transform-wizard tr:nth-child(2n) {
13+
background-color: inherit;
14+
}
15+
16+
.jse-modal td,
17+
.jse-modal th {
18+
background-color: inherit;
19+
border-bottom: inherit;
20+
font-size: inherit;
21+
line-height: inherit;
22+
vertical-align: inherit;
23+
}

django_svelte_jsoneditor/static/django_svelte_jsoneditor/js/svelte_jsoneditor.js

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{% load static %}
2+
3+
<div id="jsoneditor_{{ widget.attrs.id }}" class="svelte-jsoneditor-wrapper"></div>
4+
5+
<textarea name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>{% if widget.value %}{{ widget.value }}{% endif %}</textarea>
6+
7+
<script type="module">
8+
import { JSONEditor } from '{% static "django_svelte_jsoneditor/js/svelte_jsoneditor.js" %}'
9+
10+
const json = JSON.parse(document.getElementById('{{ widget.attrs.id }}').value)
11+
const editor = new JSONEditor({
12+
target: document.getElementById('jsoneditor_{{ widget.attrs.id }}'),
13+
props: {
14+
content: {
15+
json: json || undefined,
16+
},
17+
onChange: (updatedContent, previousContent, { contentErrors, patchResult }) => {
18+
document.getElementById('{{ widget.attrs.id }}').value = JSON.stringify(updatedContent.json)
19+
}
20+
}
21+
})
22+
</script>

django_svelte_jsoneditor/widgets.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from django.forms import Textarea
2+
3+
4+
class SvelteJSONEditorWidget(Textarea):
5+
template_name = "django_svelte_jsoneditor/widgets/svelte_jsoneditor.html"
6+
7+
def __init__(self, attrs=None):
8+
if attrs is None:
9+
attrs = {}
10+
11+
attrs.update({"class": "hidden"})
12+
13+
super().__init__(attrs)
14+
15+
class Media:
16+
css = {"all": ("django_svelte_jsoneditor/css/svelte_jsoneditor.css",)}

manage.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#!/usr/bin/env python
22
"""Management script for the project."""
33

4-
from __future__ import absolute_import, unicode_literals
5-
64
import sys
75

86

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "django-svelte-jsoneditor"
3-
version = "0.1.2"
3+
version = "0.2.0"
44
description = "A widget for django's JSONField using the latest-and-greatest Json Editor"
55
authors = ["Tom Clark <[email protected]>"]
66
license = "MIT"

tests/server/example/admin.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,48 @@
11
from django.contrib import admin
2+
from django.db import models
3+
from django_svelte_jsoneditor.widgets import SvelteJSONEditorWidget
24

35
from tests.server.example.models import (
46
ExampleBlankJsonFieldModel,
57
ExampleJsonFieldModel,
68
ExampleUneditableJsonFieldModel,
9+
ExampleMultipleJsonFieldModel,
710
)
811

912

1013
class ExampleJsonFieldModelAdmin(admin.ModelAdmin):
11-
"""A basic admin panel to demonstrate the direct upload storage behaviour"""
14+
formfield_overrides = {
15+
models.JSONField: {
16+
"widget": SvelteJSONEditorWidget,
17+
}
18+
}
1219

1320

1421
class ExampleBlankJsonFieldModelAdmin(admin.ModelAdmin):
15-
"""A basic admin panel to demonstrate the direct upload storage behaviour where blank=True"""
22+
formfield_overrides = {
23+
models.JSONField: {
24+
"widget": SvelteJSONEditorWidget,
25+
}
26+
}
1627

1728

1829
class ExampleUneditableJsonFieldModelAdmin(admin.ModelAdmin):
19-
"""A basic admin panel to demonstrate the direct upload storage behaviour where blank=True"""
30+
formfield_overrides = {
31+
models.JSONField: {
32+
"widget": SvelteJSONEditorWidget,
33+
}
34+
}
35+
36+
37+
class ExampleMultipleJsonFieldModelAdmin(admin.ModelAdmin):
38+
formfield_overrides = {
39+
models.JSONField: {
40+
"widget": SvelteJSONEditorWidget,
41+
}
42+
}
2043

2144

2245
admin.site.register(ExampleJsonFieldModel, ExampleJsonFieldModelAdmin)
2346
admin.site.register(ExampleBlankJsonFieldModel, ExampleBlankJsonFieldModelAdmin)
2447
admin.site.register(ExampleUneditableJsonFieldModel, ExampleUneditableJsonFieldModelAdmin)
48+
admin.site.register(ExampleMultipleJsonFieldModel, ExampleMultipleJsonFieldModelAdmin)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from django.db import migrations, models
2+
3+
4+
class Migration(migrations.Migration):
5+
dependencies = [
6+
("example", "0001_initial"),
7+
]
8+
9+
operations = [
10+
migrations.CreateModel(
11+
name="ExampleMultipleJsonFieldModel",
12+
fields=[
13+
(
14+
"id",
15+
models.AutoField(
16+
auto_created=True,
17+
primary_key=True,
18+
serialize=False,
19+
verbose_name="ID",
20+
),
21+
),
22+
("my_json1", models.JSONField()),
23+
("my_json2", models.JSONField()),
24+
("my_json3", models.JSONField()),
25+
],
26+
),
27+
]

tests/server/example/models.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,18 @@ class Meta:
4343
"""Metaclass defining this model to reside in the example app"""
4444

4545
app_label = "example"
46+
47+
48+
class ExampleMultipleJsonFieldModel(Model):
49+
"""
50+
Multiple JSON fields in one model to test correct behaviour of JavaScript
51+
"""
52+
53+
my_json1 = JSONField()
54+
my_json2 = JSONField()
55+
my_json3 = JSONField()
56+
57+
class Meta:
58+
"""Metaclass defining this model to reside in the example app"""
59+
60+
app_label = "example"

0 commit comments

Comments
 (0)