Skip to content

Commit

Permalink
add plugin to upload files as links
Browse files Browse the repository at this point in the history
  • Loading branch information
goapunk authored and philli-m committed Nov 6, 2023
1 parent e47c9e7 commit 2c041e4
Show file tree
Hide file tree
Showing 7 changed files with 4,479 additions and 1,546 deletions.
5,985 changes: 4,451 additions & 1,534 deletions django_ckeditor_5/package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion django_ckeditor_5/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"@ckeditor/ckeditor5-theme-lark": "^39.0.2",
"@ckeditor/ckeditor5-ui": "^39.0.2",
"@ckeditor/ckeditor5-upload": "^39.0.2",
"@ckeditor/ckeditor5-word-count": "^39.0.2"
"@ckeditor/ckeditor5-word-count": "^39.0.2",
"@liqd/ckeditor5-file-uploader": "github:liqd/ckeditor5-file-uploader"
},
"devDependencies": {
"@ckeditor/ckeditor5-core": "^39.0.2",
Expand Down
13 changes: 11 additions & 2 deletions django_ckeditor_5/static/django_ckeditor_5/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ function createEditors() {
const upload_url = document.getElementById(
`${script_id}-ck-editor-5-upload-url`
).getAttribute('data-upload-url');
const upload_file_types = JSON.parse(document.getElementById(
`${script_id}-ck-editor-5-upload-url`
).getAttribute('data-upload-file-types'));
const csrf_cookie_name = document.getElementById(
`${script_id}-ck-editor-5-upload-url`
).getAttribute('data-csrf_cookie_name');
Expand All @@ -51,10 +54,16 @@ function createEditors() {
}
);
config.simpleUpload = {
'uploadUrl': upload_url, 'headers': {
'uploadUrl': upload_url,
'headers': {
'X-CSRFToken': getCookie(csrf_cookie_name),
}
},
};

config.fileUploader = {
'fileTypes': upload_file_types
};

ClassicEditor.create(
allEditors[i],
config
Expand Down
10 changes: 6 additions & 4 deletions django_ckeditor_5/static/django_ckeditor_5/src/ckeditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,20 @@ import ListProperties from '@ckeditor/ckeditor5-list/src/listproperties';
import SourceEditing from '@ckeditor/ckeditor5-source-editing/src/sourceediting';
import GeneralHtmlSupport from '@ckeditor/ckeditor5-html-support/src/generalhtmlsupport';
import ImageInsert from '@ckeditor/ckeditor5-image/src/imageinsert';
import {TableCaption} from "@ckeditor/ckeditor5-table";
import { TableCaption } from '@ckeditor/ckeditor5-table';
import WordCount from '@ckeditor/ckeditor5-word-count/src/wordcount';
import Mention from '@ckeditor/ckeditor5-mention/src/mention';
import { Style } from '@ckeditor/ckeditor5-style';
import { HorizontalLine } from '@ckeditor/ckeditor5-horizontal-line';
import {LinkImage} from "@ckeditor/ckeditor5-link";
import { LinkImage } from '@ckeditor/ckeditor5-link';
import { FileUploader } from '@liqd/ckeditor5-file-uploader';


export default class ClassicEditor extends ClassicEditorBase {
}

ClassicEditor.builtinPlugins = [
Essentials,
UploadAdapter,
CodeBlock,
Autoformat,
Bold,
Expand Down Expand Up @@ -92,5 +93,6 @@ ClassicEditor.builtinPlugins = [
Mention,
Style,
HorizontalLine,
LinkImage
LinkImage,
FileUploader,
];
2 changes: 1 addition & 1 deletion django_ckeditor_5/templates/django_ckeditor_5/widget.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
{% if errors %}
{{ errors }}
{% endif %}
<input type="hidden" id="{{script_id}}-ck-editor-5-upload-url" data-upload-url="{{upload_url}}" data-csrf_cookie_name="{{ csrf_cookie_name }}">
<input type="hidden" id="{{script_id}}-ck-editor-5-upload-url" data-upload-url="{{upload_url}}" data-upload-file-types="{{upload_file_types}}" data-csrf_cookie_name="{{ csrf_cookie_name }}">

<span>{{ config|json_script:script_id }}</span>
11 changes: 7 additions & 4 deletions django_ckeditor_5/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,13 @@ def handle_uploaded_file(user, f):
def upload_file(request):
if request.method == "POST" and request.user.is_staff:
form = UploadFileForm(request.POST, request.FILES)
try:
image_verify(request.FILES["upload"])
except NoImageException as ex:
return JsonResponse({"error": {"message": f"{ex}"}})
allow_all_file_types = getattr(settings, "CKEDITOR_5_ALLOW_ALL_FILE_TYPES", False)

if not allow_all_file_types:
try:
image_verify(request.FILES['upload'])
except NoImageException as ex:
return JsonResponse({"error": {"message": f"{ex}"}})
if form.is_valid():
url = handle_uploaded_file(request.user, request.FILES["upload"])
return JsonResponse({"url": url})
Expand Down
1 change: 1 addition & 0 deletions django_ckeditor_5/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.forms.renderers import get_default_renderer
from django.urls import reverse
from django.utils.safestring import mark_safe
import json

if get_version() >= "4.0":
from django.utils.translation import gettext_lazy as _
Expand Down

0 comments on commit 2c041e4

Please sign in to comment.