wagtail / wagtail

A Django content management system focused on flexibility and user experience

Home Page:https://wagtail.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rich text editor crashes with ModelViewSet

hiyoko3m opened this issue · comments

Issue Summary

When a model contains a RichTextField and is used with ModelViewSet, the rich text editor renders properly. However, it crashes when attempting to use functions like Embed, Link, Document, or Image. A JavaScript error occurs simultaneously:

TypeError: Cannot read properties of undefined (reading 'embedsChooser')
    at EmbedModalWorkflowSource.getChooserConfig (draftail.js?v=5038553c:2:271551)
    at EmbedModalWorkflowSource.componentDidMount (draftail.js?v=5038553c:2:247268)
    at cu (vendor.js?v=5038553c:2:740366)
    at Ts (vendor.js?v=5038553c:2:758090)
    at t.unstable_runWithPriority (vendor.js?v=5038553c:2:798938)
    at Za (vendor.js?v=5038553c:2:702060)
    at ks (vendor.js?v=5038553c:2:754749)
    at hs (vendor.js?v=5038553c:2:750896)
    at vendor.js?v=5038553c:2:702351
    at t.unstable_runWithPriority (vendor.js?v=5038553c:2:798938)

Steps to Reproduce

# models.py
from django.db import models
from wagtail.fields import RichTextField

class SomeModel(models.Model):
    rich_text = RichTextField()


# views.py
from wagtail.admin.viewsets.model import ModelViewSet
from .models import SomeModel

class SomeModelViewSet(ModelViewSet):
    model = SomeModel
    form_fields = ['rich_text']
    icon = 'site'
    add_to_admin_menu = True
    copy_view_enabled = False
    inspect_view_enabled = True

somemodel_viewset = SomeModelViewSet("somemodel")


# wagtail_hooks.py
from wagtail import hooks
from .views import somemodel_viewset

@hooks.register("register_admin_viewset")
def register_viewset():
    return somemodel_viewset

I couldn't find any descriptions in the document indicating that RichTextField can be used with ModelViewSet. Therefore, if this behavior simply means that it's not supported, then it's not a bug.

  • I have confirmed that this issue can be reproduced as described on a fresh Wagtail project: yes

Technical details

  • Python version: Python 3.12.1
  • Django version: 5.0.3
  • Wagtail version: 6.0.1
  • Browser version: Chrome 123

I believe this was fixed by #11620

Yes, this is fixed in main by #11620, but it still exists in 6.0 as the backported fix #11754 was a partial fix (it only fixed choosers).

The most minimal fix for 6.0 (in terms of the loaded JS) would be to include the window.chooserUrls inline script and the {% hook_output 'insert_editor_js' %} (for loading additional window.chooserUrls entries). However, since the snippets {create,edit}.html templates also extend from wagtailadmin/generic/form.html and it needs other stuff from _editor_js.html (preview-panel.js, workflow-action.js to name a few), it would be tricky to make this work unless we remove {{ block.super }} or extract the minimal supplements into a separate block that would be emptied out in snippets in favour of _editor_js.html.

Since we also have #11760, and potentially other bugs from not including _editor_js.html e.g. using a SlugInput without urlify.js, it looks like the least intrusive fix would be to just bite the bullet and include _editor_js.html in wagtailadmin/generic/form.html for now.

Thanks for pointing it out! I'm relieved to know that this is related to a known issue.
While I didn't fully understand how the javascript modules are loaded hierarchically, I see that including _editor_js.html is a simple solution for now.
I'll give it a try it and look forward to the next release. Thanks!