Coder Social home page Coder Social logo

non-admin-draftail's Introduction

Non-admin Draftail

Wagtail has an excellent WYSIWYG editor called Draftail. Unfortunately, the editor can only be used on admin pages. But what if you want to use it on non-admin pages, like Django form view pages?

This is where Non-admin Draftail comes to the rescue! The package provides all the necessary magic to set Draftail free from Wagtail admin and make it usable with a regular Django form that doesn't belong to the CMS admin interface. The only requirement is, of course, to have Wagtail installed.

Installation

  1. Install the package from PyPI: pip install wagtail_non_admin_draftail

  2. Add wagtail_non_admin_draftail to INSTALLED_APPS:

    INSTALLED_APPS = [
        ...
        'wagtail_non_admin_draftail',
    ]
  3. Add the following line to the main urls.py of the project:

    path("non-admin-draftail/", include("wagtail_non_admin_draftail.urls", namespace="wagtail_non_admin_draftail")),
  4. Include "wagtail_non_admin_draftail/draftail_media.html" in the <head> of every page that will have the editor. There are many ways to do this. Here's one way to accomplish it:

    a. Add the {% block wagtail_non_admin_draftail_head %} block to the <head> of your base.html file:

    {% load wagtail_non_admin_draftail_tags %}
    <!DOCTYPE html>
    <html>
    <head>
     ...
     {% block wagtail_non_admin_draftail_head %}{% endblock wagtail_non_admin_draftail_head %}
    </head>
    <body></body>
    </html>

    b. Then add wagtail_non_admin_draftail/draftail_media.html to the wagtail_non_admin_draftail_head block on every page that uses the editor.

    For example, if you have a page template called post_edit.html that renders a form with the editor, you need to add the following block to that template:

    {% block wagtail_non_admin_draftail_head %}
      {% include "wagtail_non_admin_draftail/draftail_media.html" %}
      {{ form.media }} # add this line if your template doesn't use "{{ form }}" but fields by themselves
    {% endblock wagtail_non_admin_draftail_head %}
    

    That's it! The Draftail editor should now have all the JS/CSS required to boot up on the page.

Configuration

By default, all images and documents uploaded via Non-admin Draftail are saved in Wagtail's Images/Documents library in the "Public uploads" collection. You can customize the name of the collection by defining a WAGTAIL_NON_ADMIN_DRAFTAIL_PUBLIC_COLLECTION_NAME variable in your main Django settings.py file:

WAGTAIL_NON_ADMIN_DRAFTAIL_PUBLIC_COLLECTION_NAME = "Visitor uploads"

Usage

Assuming the following:

  1. You have a model that has a Wagtail RichTextField:

    from django.db import models
    from wagtail.fields import RichTextField
    
    class JobPost(models.Model):
      title = models.CharField(max_length=255)
      body = RichTextField()
  2. Ensure that job_post_form.html (or whatever template is responsible for rendering the Job post edit form) includes draftail_media.html in the <head> of the page (See step 4 of the Installation instructions above).

  3. Now, when you visit a page with a JobPostForm form, you should see the body field with Draftail editor enabled.

Contributing

How to run the example project locally

To contribute, you'd probably want to run the local project. Here's how to do it:

  1. This project uses Poetry for packaging and dependency management (if you have Poetry installed, skip this step):

    pip install poetry
    
  2. Clone the repo:

    git clone https://github.com/timonweb/wagtail-non-admin-draftail.git
    
  3. Change into the cloned directory:

    cd wagtail-non-admin-draftail
    
  4. Install dependencies with Poetry:

    poetry install
    
  5. Run the project with Poetry:

    poetry run python manage.py runserver
    
  6. Open your browser and go to the test form page: http://127.0.0.1:8000/example/form/.

How to run the test suite

Assuming you have completed steps 1 - 4 above, you can run the pytest test suite with the following command:

poetry run pytest

If tests fail and the installation is fresh, make sure that Playwright (the end-to-end test library we use) is installed. Run the following command to install it:

poetry run python -m playwright install

Created by

Tim Kamanin - A Full Stack Python / JavaScript Developer

non-admin-draftail's People

Contributors

adamchainz avatar dependabot[bot] avatar rupertbaker avatar seb-b avatar timonweb avatar torsellom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

non-admin-draftail's Issues

Draftail editor does not appear.

Draftail editor does not appear on Wagtail 2.15.1 after. following steps 1-3 on webpage.

My problem maybe that I don't have a model on the website to back up the fields and I not using the wagtail forms module. Is there any way that this can work without a corresponding RichText field in a model?

wagtail 4.0 compatiability

I'm trying to upgrade to wagtail 4.0 but I'm seeing the following import error.

 File "/Users/rupertbaker/code/sharedgoals/venv/lib/python3.10/site-packages/non_admin_draftail/urls.py", line 16, in <module>
    from .views.image import image_chooser_and_upload, image_select_format
  File "/Users/rupertbaker/code/sharedgoals/venv/lib/python3.10/site-packages/non_admin_draftail/views/image.py", line 12, in <module>
    from wagtail.images.views.chooser import get_image_result_data
ImportError: cannot import name 'get_image_result_data' from 'wagtail.images.views.chooser' (/Users/rupertbaker/code/sharedgoals/venv/lib/python3.10/site-packages/wagtail/images/views/chooser.py)

After some investigation it appears that the get_image_result_data function has been replaced with a mixin.
wagtail/wagtail@dd892a6#diff-2dc885830e703d17176c1ca0570314d3873e71beb2d035932eed261c20f779e5

I assume there will be other things to update.

Implement a better way for saving in backend ?

Hi !

For my usage purpose, I need to be able to save the content of the editor in my database. So I made this :

from draftjs_exporter.constants import BLOCK_TYPES, ENTITY_TYPES, INLINE_STYLES
from draftjs_exporter.defaults import BLOCK_MAP, STYLE_MAP
from draftjs_exporter.dom import DOM
from draftjs_exporter.html import HTML
from typing import Any, Callable, Dict, List, Mapping, Union

Element = Any
Props = Dict[str, Any]
...
    def link(props: Props) -> Element:
        return DOM.create_element("a", {"href": props["url"]}, props["children"])
        

     exporter = HTML({
            'block_map': BLOCK_MAP,
            'style_map': STYLE_MAP,
            'entity_decorators': {
                'LINK': link,
            }
        })
        model.richtextfield = exporter.render(json.loads(request.POST.get('richtext')))

Which makes the content savable in a richtextfield on any model.
Any idea if there is an easier way to do this ? If there isn't I'd be happy to try and implement something to that effect. For example I assume (maybe wrongly) that this exporter could come bundled with the addon.

Broken new Link modal ?

Issue description

When the modal for a link pops, if you hit the close button, the editor will stay inactive and won't be clickable anymore as if the modal never closed

Steps to reproduce the issue

  1. click on the link button or hit ctrl+k when editing
  2. hit the close button on the modal

What's the expected result?

  • Modal should close and editor should be in the same state as when you opened the modal

What's the actual result?

  • Modal closes, but editor stay in a locked state

Additional details / screenshot

  • jquery event for closing is registered as expected

  • state before :
    image

  • state after correctly entering a link :
    image

  • state when opening the link modal but closing it without entering a link
    image

Document how to go from Draftail editor to htmlCode without going through the server

I'd like to be able when the user hit a button on my page to transform the content of the editor to an actual RichTextField() as generated by Wagtail. If it's already possible please forgive me, I'm kind of a beginner but as of now I can't find anywhere in the doc where I could recover the resulting HTML from the editor current state.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.