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

Included Magic URL library for automatic link conversion. #40

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
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
40 changes: 36 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ python app/manage.py runserver
Documentation for **django-quill-editor** is located at [https://django-quill-editor.readthedocs.io/](https://django-quill-editor.readthedocs.io/)


## Change toolbar menus`

## Change toolbar configs

Add `QUILL_CONFIGS` to the **settings.py**

```
```python
QUILL_CONFIGS = {
'default':{
'theme': 'snow',
Expand All @@ -80,11 +81,11 @@ QUILL_CONFIGS = {
]
}
}

}

```



## Usage

Add `QuillField` to the **Model class** you want to use
Expand Down Expand Up @@ -213,3 +214,34 @@ def model_form(request):
As an open source project, we welcome contributions.
The code lives on [GitHub](https://github.com/LeeHanYeong/django-quill-editor)



## Distribution (for owners)

### PyPI Release

```shell
poetry install # Install PyPI distribution packages
python deploy.py
```



### Sphinx docs

```shell
brew install sphinx-doc # macOS
```

#### Local

```
cd docs
make html
# ...
# The HTML pages are in _build/html.

cd _build/html
python -m http.server 3001
```

1 change: 1 addition & 0 deletions django_quill/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'theme': 'snow',
'modules': {
'syntax': True,
'magicUrl': True,
'toolbar': [
[
{'font': []},
Expand Down
10 changes: 8 additions & 2 deletions django_quill/fields.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

from django.db import models

from .forms import QuillFormField
Expand All @@ -14,7 +16,7 @@ class FieldQuill:
def __init__(self, instance, field, json_string):
self.instance = instance
self.field = field
self.json_string = json_string
self.json_string = json_string or '{"delta":"","html":""}'
self._committed = True

def __eq__(self, other):
Expand All @@ -31,7 +33,7 @@ def _require_quill(self):

def _get_quill(self):
self._require_quill()
self._quill = Quill(self.json_string)
self._quill = Quill(json.loads(self.json_string))
return self._quill

def _set_quill(self, quill):
Expand Down Expand Up @@ -142,3 +144,7 @@ def get_prep_value(self, value):
if isinstance(value, Quill):
return value.json_string
return value

def value_to_string(self, obj):
value = self.value_from_object(obj)
return self.get_prep_value(value)
5 changes: 4 additions & 1 deletion django_quill/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
)


class QuillFormField(forms.fields.CharField):
class QuillFormField(forms.fields.JSONField):
def __init__(self, *args, **kwargs):
kwargs.update({
'widget': QuillWidget(),
})
super().__init__(*args, **kwargs)

def prepare_value(self, value):
return value.json_string
7 changes: 3 additions & 4 deletions django_quill/quill.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
from json import JSONDecodeError

__all__ = (
'QuillParseError',
Expand All @@ -18,9 +17,9 @@ def __str__(self):
class Quill:
def __init__(self, json_string):
try:
self.json_string = json_string
json_data = json.loads(json_string)
self.json_string = json.dumps(json_string)
json_data = json_string
self.delta = json_data['delta']
self.html = json_data['html']
except (JSONDecodeError, KeyError, TypeError):
except (json.JSONDecodeError, KeyError, TypeError):
raise QuillParseError(json_string)
7 changes: 5 additions & 2 deletions django_quill/templates/django_quill/media.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/highlight.min.js"></script>

<!-- Quill.js -->
<link href="//cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet">
<script src="//cdn.quilljs.com/1.3.6/quill.min.js"></script>
<link href="//cdn.quilljs.com/1.3.7/quill.snow.css" rel="stylesheet">
<script src="//cdn.quilljs.com/1.3.7/quill.min.js"></script>

<!-- Quill Magic Url -->
<script src="https://unpkg.com/[email protected]/dist/index.js"></script>

<!-- Custom -->
<link rel="stylesheet" href="{% static 'django_quill/django_quill.css' %}">
Expand Down
14 changes: 12 additions & 2 deletions django_quill/templates/django_quill/widget.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@
<input id="quill-input-{{ id }}" name="{{ name }}" type="hidden">
<script>
(function () {
Quill.register('modules/magicUrl', MagicUrl);
var wrapper = new QuillWrapper('quill-{{ id }}', 'quill-input-{{ id }}', JSON.parse('{{ config|safe }}'));
{% if quill and quill.delta %}
// try django_quill/quill.py/Quill instance
var contents = JSON.parse('{{ quill.delta|safe|escapejs }}');
wrapper.quill.setContents(contents);
{% elif value %}
wrapper.quill.clipboard.dangerouslyPasteHTML(0, '{{ value|safe }}')
// try Parsing value as JSON
try {
var value = JSON.parse('{{ value|safe|escapejs }}');
wrapper.quill.setContents(JSON.parse(value['delta']));
}
// When a parsing error occurs, the contents are regarded as HTML and the contents of the editor are filled.
catch (e) {
wrapper.quill.clipboard.dangerouslyPasteHTML(0, '{{ value|safe }}')
}
{% endif %}
})();
</script>
</div>
</div>
9 changes: 5 additions & 4 deletions django_quill/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ def default(self, obj):
class QuillWidget(forms.Textarea):
class Media:
js = (
'django_quill/highlight.pack.js',
'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/highlight.min.js',
'django_quill/django_quill.js',
'django_quill/quill.js',
'https://cdn.quilljs.com/1.3.7/quill.min.js',
'https://unpkg.com/[email protected]/dist/index.js',
)
css = {
'all': (
'django_quill/highlight.darcula.min.css',
'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/styles/darcula.min.css',
'django_quill/django_quill.css',
'django_quill/quill.snow.css',
'https://cdn.quilljs.com/1.3.7/quill.snow.css',
)
}

Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ django-quill-editor makes `Quill.js` easy to use on Django Forms and admin sites
pages/using-in-admin
pages/using-as-form
pages/using-as-modelform
pages/change-toolbar-configs

Installation
************
Expand Down
30 changes: 30 additions & 0 deletions docs/pages/change-toolbar-configs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Change toolbar config

> More settings can be found on the official site
> https://quilljs.com/docs/modules/toolbar/

Add `QUILL_CONFIGS` to the **settings.py**

```python
QUILL_CONFIGS = {
'default':{
'theme': 'snow',
'modules': {
'syntax': True,
'toolbar': [
[
{'font': []},
{'header': []},
{'align': []},
'bold', 'italic', 'underline', 'strike', 'blockquote',
{'color': []},
{'background': []},
],
['code-block', 'link'],
['clean'],
]
}
}
}
```

24 changes: 23 additions & 1 deletion docs/pages/using-as-form.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
# Using as Form

Add the CSS and JS to the `<head>` of the template or base template.

There are two ways to add CSS and JS files to a template.

- If there is a **Form** with QuillField added, add `{{ form.media }}` to the `<head>` tag.

```django
<head>
{{ form.media }}
</head>
```

- Or, import CSS and JS files directly using `{% include %}` template tags.

```django
<head>
<!-- django-quill-editor Media -->
{% include 'django_quill/media.html' %}
</head>
```



Add `QuillFormField` to the **Form class** you want to use.

```python
Expand All @@ -25,7 +48,6 @@ def form_view(request):
```



In the template, use the received **Form instance** variable. (in the above case, **'form'**)

```html
Expand Down
Loading