Skip to content

Commit 2f877d3

Browse files
authored
Merge pull request #18 from octue/widget-settings
Widget settings
2 parents 09b36ec + cd5ca29 commit 2f877d3

File tree

13 files changed

+407
-205
lines changed

13 files changed

+407
-205
lines changed

.devcontainer/devcontainer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@
7272
"trond-snekvik.simple-rst",
7373
"ms-azuretools.vscode-docker",
7474
"ryanluker.vscode-coverage-gutters",
75-
"ms-python.black-formatter"
75+
"ms-python.black-formatter",
76+
"GitHub.copilot"
7677
]
7778
}
7879
},

.github/workflows/ci.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@ on:
1313

1414
jobs:
1515
check-semantic-version:
16-
uses: octue/.github/.github/workflows/reusable-check-semantic-version.yml@main
16+
uses: octue/workflows/.github/workflows/check-semantic-version.yml@main
17+
with:
18+
path: pyproject.toml
19+
breaking_change_indicated_by: minor
1720

1821
run-tests:
1922
if: "!contains(github.event.head_commit.message, 'skipci')"
2023
strategy:
2124
fail-fast: true
2225
matrix:
23-
python: ['3.9', '3.10', '3.11']
26+
python: ['3.10', '3.11', '3.12']
2427
os: [ubuntu-latest] # [ubuntu-latest, windows-latest, macos-latest] for full coverage but this gets expensive quickly
2528
runs-on: ${{ matrix.os }}
2629

.github/workflows/update-pull-request.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ on:
66
- main
77

88
jobs:
9-
update-pull-request:
10-
uses: octue/.github/.github/workflows/reusable-update-pull-request.yml@main
9+
generate-pull-request-description:
10+
uses: octue/workflows/.github/workflows/generate-pull-request-description.yml@main
1111
secrets:
1212
token: ${{ secrets.GITHUB_TOKEN }}
13+
permissions:
14+
contents: read
15+
pull-requests: write

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,88 @@ class MyJSONEditorForm(forms.Form):
5858
json = forms.JSONField(widgets=SvelteJSONEditorWidget())
5959
```
6060

61+
### Global settings
62+
63+
The application allows modifying some properties of svelte-jsoneditor inside the settings.py configuration file in Django. Official documentation is available on [svelte-jsoneditor](https://github.com/josdejong/svelte-jsoneditor#properties) GitHub page. **Note:** Not all options are configurable which are provided by svelte-jsoneditor.
64+
65+
```python
66+
# settings.py
67+
68+
SVELTE_JSONEDITOR = {
69+
"mode": "tree",
70+
"mainMenuBar": True,
71+
"navigationBar": True,
72+
"statusBar": True,
73+
"askToFormat": True,
74+
"readOnly": False,
75+
"indentations": 4,
76+
"tabSize": 4,
77+
"escapeControlCharacters": False,
78+
"flattenColumns": True,
79+
}
80+
```
81+
82+
#### Available properties
83+
84+
| Property | Type | Default |
85+
| ----------------------- | --------------------------- | ------- |
86+
| mode | 'tree' or 'text' or 'table' | 'tree' |
87+
| mainMenuBar | boolean | True |
88+
| navigationBar | boolean | True |
89+
| statusBar | boolean | True |
90+
| askToFormat | boolean | True |
91+
| readOnly | boolean | False |
92+
| indentations | number or string | 4 |
93+
| tabSize | number | 4 |
94+
| escapeControlCharacters | boolean | False |
95+
| flattenColumns | boolean | True |
96+
97+
### Widget properties
98+
99+
`SvelteJSONEditorWidget` has additional argument called `props` which allows to override `SVELTE_JSONEDITOR` settings from settings.py.
100+
101+
```python
102+
# forms.py
103+
104+
from django import forms
105+
106+
107+
class SvelteJsonEditorForm(forms.Form):
108+
my_json = forms.JSONField(widget=SvelteJSONEditorWidget(props={
109+
"readOnly": True
110+
}))
111+
```
112+
113+
#### Custom widget properties in admin form
114+
115+
```python
116+
# admin.py
117+
118+
from django import forms
119+
from django.contrib import admin
120+
121+
from .models import ExampleModel
122+
123+
class CustomForm(forms.ModelForm):
124+
class Meta:
125+
model = ExampleModel
126+
fields = "__all__"
127+
128+
def __init__(self, *args, **kwargs):
129+
super().__init__(*args, **kwargs)
130+
self.fields["some_json_field"].widget = SvelteJSONEditorWidget(props={"readOnly": True})
131+
132+
133+
@admin.register(ExampleModel, ExampleModelAdmin)
134+
class ExampleModelAdmin(admin.ModelAdmin):
135+
form = CustomForm
136+
formfield_overrides = {
137+
models.JSONField: {
138+
"widget": SvelteJSONEditorWidget,
139+
}
140+
}
141+
```
142+
61143
## About Svelte
62144

63145
**You don't need to know or care.** It's the JavaScript framework used to develop the widget - but the widget JS is all pre-built so there are no extra requirements.

django_svelte_jsoneditor/settings.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from django.conf import settings
2+
3+
4+
CONFIG_DEFAULTS = {
5+
"PROPS": {
6+
"mode": "tree",
7+
"mainMenuBar": True,
8+
"navigationBar": True,
9+
"statusBar": True,
10+
"askToFormat": True,
11+
"readOnly": False,
12+
"indentations": 4,
13+
"tabSize": 4,
14+
"escapeControlCharacters": False,
15+
"flattenColumns": True,
16+
}
17+
}
18+
19+
20+
def get_config():
21+
return {
22+
"PROPS": {
23+
**CONFIG_DEFAULTS["PROPS"],
24+
**getattr(settings, "SVELTE_JSONEDITOR", {}).get("PROPS", {}),
25+
}
26+
}

django_svelte_jsoneditor/templates/django_svelte_jsoneditor/widgets/svelte_jsoneditor.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@
88
import { JSONEditor } from '{% static "django_svelte_jsoneditor/js/svelte_jsoneditor.js" %}'
99

1010
const json = JSON.parse(document.getElementById('{{ widget.attrs.id }}').value)
11+
1112
const editor = new JSONEditor({
1213
target: document.getElementById('jsoneditor_{{ widget.attrs.id }}'),
13-
props: {
14+
props: {
1415
content: {
15-
json: json || undefined,
16+
json: json || undefined,
1617
},
1718
onChange: (updatedContent, previousContent, { contentErrors, patchResult }) => {
1819
document.getElementById('{{ widget.attrs.id }}').value = JSON.stringify(updatedContent.json)
1920
}
2021
}
2122
})
23+
24+
editor.updateProps({{ widget.props|safe }})
2225
</script>

django_svelte_jsoneditor/widgets.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1+
import json
12
from django.forms import Textarea
23

4+
from .settings import get_config
5+
36

47
class SvelteJSONEditorWidget(Textarea):
58
template_name = "django_svelte_jsoneditor/widgets/svelte_jsoneditor.html"
69

7-
def __init__(self, attrs=None):
10+
def __init__(self, props=None, attrs=None):
811
if attrs is None:
912
attrs = {}
1013

14+
self.props = {} if props is None else props.copy()
1115
attrs.update({"class": "hidden"})
1216

1317
super().__init__(attrs)
1418

19+
def get_context(self, name, value, attrs):
20+
context = super().get_context(name, value, attrs)
21+
context["widget"].update({"props": json.dumps({**get_config()["PROPS"], **self.props})})
22+
return context
23+
1524
class Media:
1625
css = {"all": ("django_svelte_jsoneditor/css/svelte_jsoneditor.css",)}

docs/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,4 @@
166166
html_show_copyright = True
167167

168168
# Output file base name for HTML help builder.
169-
htmlhelp_basename = "django_gcp_doc"
169+
htmlhelp_basename = "django_svelte_jsoneditor_doc"

0 commit comments

Comments
 (0)