Search code examples
javascriptdjangockeditormaterial-design-litedjango-ckeditor

Django, django-ckeditor, Google MDL: Cannot read property 'getSelection' of undefined


I am using Django 1.11 and django-ckeditor for text fields in admin panel, and also for some inputs outside the admin (in public forms). As a CSS framework - Google material design lite.

When I trying to integrate ckeditor to public forms, charfield with ckeditor does not working at all. Something like I can't focus to this field, and when I click to any of ckeditor control elements I get errors

Uncaught TypeError: Cannot read property 'getSelection' of undefined
at CKEDITOR.dom.selection.getNative (ckeditor.js:445)
    at CKEDITOR.dom.selection (ckeditor.js:443)
    at a.CKEDITOR.editor.getSelection (ckeditor.js:440)
    at CKEDITOR.plugins.undo.Image (ckeditor.js:1174)
    at CKEDITOR.plugins.undo.UndoManager.save (ckeditor.js:1169)
    at a.b (ckeditor.js:1164)
    at a.n (ckeditor.js:10)
    at a.CKEDITOR.event.CKEDITOR.event.fire (ckeditor.js:12)
    at a.CKEDITOR.editor.CKEDITOR.editor.fire (ckeditor.js:13)
    at a.execCommand (ckeditor.js:271)

And in Firefox:

TypeError: this.document.getWindow(...).$ is undefined
getNative http://127.0.0.1:8000/static/ckeditor/ckeditor/ckeditor.js:445:29
    CKEDITOR.dom.selection http://127.0.0.1:8000/static/ckeditor/ckeditor/ckeditor.js:443:54
    CKEDITOR.editor.prototype.getSelection http://127.0.0.1:8000/static/ckeditor/ckeditor/ckeditor.js:440:319
    CKEDITOR.plugins.undo.Image http://127.0.0.1:8000/static/ckeditor/ckeditor/ckeditor.js:1174:458
    save http://127.0.0.1:8000/static/ckeditor/ckeditor/ckeditor.js:1169:123
    b http://127.0.0.1:8000/static/ckeditor/ckeditor/ckeditor.js:1164:291
    n http://127.0.0.1:8000/static/ckeditor/ckeditor/ckeditor.js:10:222
    CKEDITOR.event.prototype</<.fire</< http://127.0.0.1:8000/static/ckeditor/ckeditor/ckeditor.js:12:42
    CKEDITOR.editor.prototype.fire http://127.0.0.1:8000/static/ckeditor/ckeditor/ckeditor.js:13:212
    execCommand http://127.0.0.1:8000/static/ckeditor/ckeditor/ckeditor.js:271:120
    CKEDITOR.ui.button/<.click< http://127.0.0.1:8000/static/ckeditor/ckeditor/ckeditor.js:654:417
    execute http://127.0.0.1:8000/static/ckeditor/ckeditor/ckeditor.js:655:478
    render/q< http://127.0.0.1:8000/static/ckeditor/ckeditor/ckeditor.js:656:324
    addFunction/< http://127.0.0.1:8000/static/ckeditor/ckeditor/ckeditor.js:31:216
    callFunction http://127.0.0.1:8000/static/ckeditor/ckeditor/ckeditor.js:31:332
    onclick http://127.0.0.1:8000/accounts/trip/2/:1:1

ckeditor.js 445 line:

x?this.document.$.selection:this.document.getWindow().$.getSelection()},getType:x?function(){var a=this._.cache;if(a.type)return a.type;var b=CKEDITOR.SELECTION_NONE;try{var c=this.getNative(),d=c.type;"Text"==d&&(b=CKEDITOR.SELECTION_TEXT);"Control"==d&&(b=CKEDITOR.SELECTION_ELEMENT);c.createRange().parentElement()&&(b=CKEDITOR.SELECTION_TEXT)}catch(e){}return a.type=b}:function(){var a=this._.cache;if(a.type)return a.type;var b=CKEDITOR.SELECTION_TEXT,c=this.getNative();if(!c||!c.rangeCount)b=CKEDITOR.SELECTION_NONE;

Forms.py:

from ckeditor_uploader.widgets import CKEditorUploadingWidget


class PaymentForm(forms.Form):
    comments = forms.CharField(widget=CKEditorUploadingWidget())

To integrate ckeditor js scripts I'm using {{ form.media }} in template.

First of all I tried to search info about this errors and any know fixes, but any of founded doesn't worked for me. Also I found some similar questions with ckeditor here, but without any working solution.

When I checked all js scripts, I found that problem in Javascript of Google material design lite css framework https://github.com/google/material-design-lite

When material.min.js are integrated to page scripts - ckeditor does not work. Not minified framework js: https://code.getmdl.io/1.3.0/material.js

PS: This is my first question ever made here, sorry for any inconvenience.

Thanks for any help!


Solution

  • Made some changes to make django-ckeditor work with google material design lite framework. If needed, you can use this version of django-ckeditor (my fork): https://github.com/wardal/django-ckeditor while we waiting official answer and solutions here: https://github.com/django-ckeditor/django-ckeditor/issues/429