Code Monkey home page Code Monkey logo

django-prose-editor's Introduction

django-prose-editor

Prose editor for the Django admin based on ProseMirror. Announcement blog post.

About rich text editors

Copied from the django-content-editor documentation.

We have been struggling with rich text editors for a long time. To be honest, I do not think it was a good idea to add that many features to the rich text editor. Resizing images uploaded into a rich text editor is a real pain, and what if you’d like to reuse these images or display them using a lightbox script or something similar? You have to resort to writing loads of JavaScript code which will only work on one browser. You cannot really filter the HTML code generated by the user to kick out ugly HTML code generated by copy-pasting from word. The user will upload 10mb JPEGs and resize them to 50x50 pixels in the rich text editor.

All of this convinced me that offering the user a rich text editor with too much capabilities is a really bad idea. The rich text editor in FeinCMS only has bold, italic, bullets, link and headlines activated (and the HTML code button, because that’s sort of inevitable – sometimes the rich text editor messes up and you cannot fix it other than going directly into the HTML code. Plus, if someone really knows what they are doing, I’d still like to give them the power to shot their own foot).

If this does not seem convincing you can always add your own rich text plugin with a different configuration (or just override the rich text editor initialization template in your own project). We do not want to force our world view on you, it’s just that we think that in this case, more choice has the bigger potential to hurt than to help.

Installation

The first step is to ensure that you have an activated virtualenv for your current project, using something like . .venv/bin/activate.

Install the package into your environment:

pip install django-prose-editor

To include nh3 as optional dependency for sanitized HTML, install the extra "sanitize":

pip install django-prose-editor[sanitize]

Add django_prose_editor to INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    "django_prose_editor",
]

Replace models.TextField with ProseEditorField where appropriate:

from django_prose_editor.fields import ProseEditorField

class Project(models.Model):
    description = ProseEditorField()

Note! No migrations will be generated when switching from and to models.TextField. That's by design. Those migrations are mostly annoying.

Security

ProseMirror does a really good job of only allowing content which confirms to a particular scheme. Of course users can submit what they want, they are not constrainted by the HTML widgets you're using. You should still always sanitize the HTML submitted on the server side. A good way to do this is by using the sanitize argument to the ProseEditorField. You can use the following snippet to always pass HTML through nh3:

from django_prose_editor.sanitized import SanitizedProseEditorField

description = SanitizedProseEditorField()

Install django-prose-editor with the extra "sanitize" to use SanitizedProseEditorField.

Convenience

Sometimes it may be useful to show an excerpt of the HTML field; the ProseEditorField automatically adds a get_*_excerpt method to models which returns the truncated and stripped beginning of your HTML field's content. The name would be Project.get_description_excerpt in the example above.

Customization

It's possible to slightly customize the field or widget by passing an optional config dictionary. The default configuration is:

config = {
    "types": None,        # Allow all nodes and marks
    "history": True,      # Enable undo and redo
    "html": True,         # Add a button which allows editing the raw HTML
    "typographic": True,  # Highlight typographic characters
}

If you only want to support paragraphs, strong, emphasis, sub- and superset and no history or HTML editing you could add the following field:

text = SanitizedProseEditorField(
    config={"types": ["strong", "em", "sub", "sup"]},
)

Paragraphs cannot be removed at the moment. Note that the backend doesn't sanitize the content to ensure that the HTML doesn't contain only the provided tags, that's out of scope for now.

doc, paragraph and text are always in the allowlist.

The supported node types are heading, blockquote, horizontal_rule and hard_break. List nodes are ordered_list, bullet_list and list_item.

The supported mark types are link, strong, em, underline, strikethrough, sub and sup

Usage outside the Django admin

The prose editor can easily be used outside the Django admin. The form field respectively the widget includes the necessary CSS and JavaScript:

from django_prose_editor.fields import ProseEditorFormField

class Form(forms.Form):
    text = ProseEditorFormField()

Or maybe you want to use django_prose_editor.widgets.ProseEditorWidget, but why make it more complicated than necessary.

If you're rendering the form in a template you have to include the form media:

<form method="post">
  {{ form.errors }} {# Always makes sense #}
  {{ form.media }}  {# This is the important line! #}
  {{ form.as_div }}
  <button type="submit">send</button>
</form>

Note that the form media isn't django-prose-editor specific, that's a Django feature.

The django-prose-editor CSS uses the following CSS custom properties.

  • --prose-editor-background
  • --prose-editor-foreground
  • --prose-editor-border-color
  • --prose-editor-active-color
  • --prose-editor-disabled-color

If you do not set them, they get their value from the following properties that are defined in the Django admin's CSS:

  • --border-color
  • --body-fg
  • --body-bg
  • --primary

You should set these properties with appropriate values to use django-prose-editor outside the admin in your site.

In addition, you may optionally set a --prose-editor-typographic property to control the color of typographic characters when shown.

django-prose-editor's People

Contributors

matthiask avatar carltongibson avatar plenaerts avatar

Stargazers

Bruno C. Vellutini avatar Giannis Terzopoulos avatar Corentin Bettiol avatar  avatar Can Evgin avatar Di M Dub avatar zft9xgy avatar Mohammad Pourmohammadi Fallah avatar Adrian Kurowski avatar __moonlit_grace__ avatar  avatar Nikolaus Schlemm avatar Rovshan Mammadli avatar Derek Payton avatar Christian Reksten-Monsen avatar Andrés Usán avatar Alexander Goldovsky avatar Abhijeet Pal avatar Michael H avatar  avatar Mark Hamblin avatar  avatar  avatar Jaques Yang avatar  avatar Pablo Diego Silva da Silva avatar York Schickl avatar  avatar Leonard avatar Délita Makanda avatar  avatar Josh Karamuth avatar James Gordon avatar Karthic Raghupathi avatar Mahmoud Ezzat avatar Marc G avatar xg avatar Henning avatar  avatar Kevin Bowen avatar Shatalov Vadim avatar Martin Svoboda avatar aksonai avatar Martin Kjellberg avatar Miguel Lopez avatar Dustin Wyatt avatar Mathieu Kniewallner avatar Bruno Melo avatar Joseph Riddle avatar Ahmed Shahwan avatar J.C. avatar  avatar Sezer Bozkır avatar YvanKOB avatar Michael Wheeler avatar  avatar Matías Agustín Méndez avatar Clarence Vinzcent Reyes avatar Julian C. avatar jET avatar Petr Stříbný avatar Anton Zh avatar Andrew Miller avatar yahya albashar avatar Will Vincent avatar Artur Barseghyan avatar Ryan Cheley avatar Tobi DEGNON avatar Benedikt Willi avatar Basel J. Hamadeh avatar Jochen Wersdörfer avatar Anton Alekseev avatar Vicente Reyes avatar  avatar  avatar turulomio avatar Shitty Girl avatar Josh Thomas avatar  avatar Lewis Kori avatar Jeff Triplett avatar  avatar

Watchers

 avatar  avatar David McCallie avatar  avatar

django-prose-editor's Issues

Explicit Color Setting for the editor

Is it possible to remove:

.ProseMirror{color:#000!important}

i'm having to manually remove that so that dark and light modes work correctly in my backend. i feel like this should just inherit the color set by the interface.

What do I put in the HTML head?

I can't find what js or css files that I need to put in head of my templates to get the widgets to work. Can you update documentation with an example?

insert media

i didn't see anything about inserting pictures or video
and i didn't find that in default options can you help me about it?

Passing tags and attributes to nh3.clean()

Hi @matthiask — thanks for the great package, as always 🎁

I have a question about the nh3 sanitizer:

def _nh3_sanitizer():
from nh3 import clean
return lambda x: _actually_empty(clean(x))

Here we’re using clean but I wonder if we could pass the tags and attributes arguments as well?

Deriving those from the types used to instantiate the prose editor would allow precisely limiting the allowed HTML, I’m hoping.

What do you think?

Prose editor display can be improve on admin panel

First, thank for create this, your work and dedication.

The prose editor is display like this in the admin panel:
Captura de pantalla 2024-07-18 a las 22 38 48

What i suggest is to set min-height and a margin-bottom to improve first look.

src/css/editor.css

.ProseMirror {
  /* 
 Existing attributes
*/
  min-height: 30vh;
}

.prose-editor {
 /* 
 Existing attributes
*/
  margin-bottom: 10px;
}

In my opinion this will display a better text box.

Captura de pantalla 2024-07-18 a las 23 11 47

Whats the best way to render this content on the template?

So, sorry for open an issue on this simple question.

Right now im using this model:

content = SanitizedProseEditorField()

And i render in django template like this:

{{obj.content | safe}}

Is this the right way to display the content?

Thank in advance.

Menu not correct display

image

menu of heading is show if hover first element and then it overlap other elements

is not as in your blog post

image

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.