Skip to main content

Rich Text Plugin for django CMS

Project description

djangocms-text

pypi coverage python django djangocms djangocms4

djangocms-text is a flexible and extensible rich text editing solution for Django CMS. This package is designed as a replacement for djangocms-text-ckeditor, introducing a swappable rich text editor interface and supporting enhanced data storage in both HTML and JSON formats.

Features

  • Swappable Rich Text Editors: Choose and switch between different rich text editors as per project requirements.

  • Customization and Extensions: Easily add new or customized versions of your favorite rich text editors.

  • Enhanced Data Storage: Store content in either HTML or JSON format, offering versatility for different use cases.

  • djangocms-text-ckeditor Compatibility: Initial version includes a port of the CKEditor 4 interface and child plugin functionality. This editor is compatible with the djangocms-text-ckeditor plugin, and can be used as a drop-in replacement.

Installation

Install djangocms-text using pip: pip install djangocms-text.

Build latest development branch using git:

git clone git@github.com:fsbraun/djangocms-text.git
cd djangocms-text
nvm use
npm install
npx webpack --mode development

You then can install the cloned repo using pip install -e /path/to/the/repo/djangocms-text.

Finally, add djangocms_text to your INSTALLED_APPS in your Django project settings:

INSTALLED_APPS = [..., "djangocms_text", ...]

Add an editor frontend to your installed apps (if different from the default TipTap frontend), and set the editor you want to use:

INSTALLED_APPS = [..., "djangocms_text.contrib.text_ckeditor5", ...]
DJANGOCMS_TEXT_EDITOR = "djangocms_text.contrib.text_ckeditor5.ckeditor5"

Upgrading from djangocms-text-ckeditor

djangocms-text’s migrations automatically migrate existing text plugins from djangocms-text-ckeditor. All you have to do is:

  • remove djangocms_text_ckeditor from INSTALLED_APPS

  • add djangocms_text to INSTALLED_APPS (see above)

  • run python -m manage migrate djangocms_text

When transitioning from CKEditor4 to Tiptap as the rich text editor in your project, consider the following points:

  • Switching Editors: The biggest challenge will likely be adapting to the differences between CKEditor4 and the new default rich text editor Tiptap. Tiptap offers a more modern editing experience, but there are important distinctions in how content is handled.

  • No HTML Source Code Editing: Tiptap does not support direct HTML source code editing. While this simplifies the editor for most users, it could be a drawback for those accustomed to manually editing HTML, such as advanced users or developers.

  • Loss of Non-standard Formatting: Formatting created through CKEditor4 plugins or manually added HTML classes may not be preserved. Tiptap stores content in an abstract JSON format and regenerates the HTML upon editing, which can lead to a loss of non-standard formatting. However, this only happens if a field is edited after migration.

  • Potential Workaround: If maintaining CKEditor4 functionality is essential, you could circumvent these issues by using the CKEditor4 backend provided with djangocms-text. This allows you to retain the familiar CKEditor4 behavior while benefiting from other updates.

Usage

After installation, djangocms-text can be used as your rich text editor in Django CMS. It can be used as a drop-in for djangocms-text-ckeditor. Detailed documentation on usage and customization will be provided.

Editors

djangocms-text supports multiple rich text editors, which can be swapped out as needed. The following editors are currently supported:

  • TipTap: A modern rich text editor with a modular architecture, TipTap is currently in development and is the default editor. TipTap does not allow the user to edit HTML directly, which means that some formating options are lost when switching from CKEditor 4 to TipTap.

  • CKEditor 4: The initial version of djangocms-text includes a port of the CKEditor 4 interface and child plugin functionality. This editor is compatible with the djangocms-text-ckeditor plugin, and can be used as a drop-in replacement. It supports inline editing and text-enabled plugins.

  • CKEditor 5: The current version of CKEditor might be supported in a future release of djangocms-text or as a separate package. Its current implementation does not support text-enabled plugins or dynamic attributes.

Contributing

Contributions to djangocms-text are welcome! Please read our contributing guidelines to get started.

License

This project is licensed under the BSD-3-Clause License - see the LICENSE file for details. The CKEditor 5 interface is licensed under the GPL-2.0 License.

Configuration

Rich text editor selection

To select a rich text editor, add the editor’s package to your INSTALLED_APPS and add the setting DJANGOCMS_TEXT_EDITOR to point to the editor’s RTEConfig path.

Example:

INSTALLED_APPS = [
    ...,
    "djangocms_text.contrib.text_ckeditor4",
    ...
]

DJANGOCMS_TEXT_EDITOR = "djangocms_text.contrib.text_ckeditor5.ckeditor4"

Inline editing feature

Inline editing allows editors to directly click on a text plugin and change the contents in django CMS’ edit mode. The CKEditor appears directly around the text field and can be used normally. Changes are saved as soon as the text field leaves focus.

Inline editing requires to encapsulate the HTML text in a <div> in edit mode. This might cause some side effects with a site’s CSS, e.g. direct child rules.

To activate inline editing add the following line in your project’s settings.py:

TEXT_INLINE_EDITING = True

This will add a toggle button to the toolbar to allow to switch inline editing on and off for the current session.

When inline editing is active the editor will save the plugin’s content each time it loses focus. If only text has changed the user can immediately continue to edit. If a text-enabled plugin was changed, added, or removed he page will refresh to update the page tree and get the correctly rendered version of the changed plugin.

Text-enabled plugins

djangocms-text supports text-enabled plugins, not all rich text editor frontends will, however.

If you have created a plugin that you want to use within Text plugins you can make them appear in the dropdown by making them text_enabled. This means that you assign the property text_enabled of a plugin to True, the default value is False. Here is a very simple implementation:

class MyTextPlugin(TextPlugin):
    name = "My text plugin"
    model = MyTextModel
    text_enabled = True

When the plugin is picked up, it will be available in the CMS Plugins dropdown (puzzle icon), which you can find in the editor. This makes it very easy for users to insert special content in a user-friendly Text block, which they are familiar with.

The plugin will even be previewed in the text editor. Pro-tip: make sure your plugin provides its own icon_alt method. That way, if you have many text_enabled-plugins, it can display a hint about it. For example, if you created a plugin which displays prices of configurable product, it can display a tooltip with the name of that product.

For more information about extending the CMS with plugins, read django-cms doc on how to do this.

Text-enabled plugins can have their own icons with djangocms-text. If the plugin class has a text_icon property, it should contain a SVG source code of an icon. The icon will be displayed in the CMS plugin pulldown menu, or in the toolbar.

class MyTextPlugin(TextPlugin):
    name = "My text plugin"
    model = MyTextModel
    text_enabled = True
    text_icon = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8zm-1-13h2v6h-2zm0 8h2v2h-2z"/></svg>'

You can also configure text-enabled plugins to be directly accessible from the rich text editor toolbar by adding the plugin’s name to the toolbar configuration, e.g. "LinkPlugin". T

Default content in Placeholder

You can use TextPlugin in “default_plugins” (see docs about the CMS_PLACEHOLDER_CONF setting). TextPlugin requires just one value: body where you write your default HTML content. If you want to add some “default children” to your automagically added plugin (i.e. a LinkPlugin), you have to put children references in the body. References are "%(_tag_child_<order>)s" with the inserted order of children. For example:

CMS_PLACEHOLDER_CONF = {
    'content': {
        'name' : _('Content'),
        'plugins': ['TextPlugin', 'LinkPlugin'],
        'default_plugins':[
            {
                'plugin_type':'TextPlugin',
                'values':{
                    'body':'<p>Great websites : %(_tag_child_1)s and %(_tag_child_2)s</p>'
                },
                'children':[
                    {
                        'plugin_type':'LinkPlugin',
                        'values':{
                            'name':'django',
                            'url':'https://www.djangoproject.com/'
                        },
                    },
                    {
                        'plugin_type':'LinkPlugin',
                        'values':{
                            'name':'django-cms',
                            'url':'https://www.django-cms.org'
                        },
                    },
                ]
            },
        ]
    }
}

Configurable sanitizer

djangocms-text uses nh3 to sanitize HTML to avoid security issues and to check for correct HTML code. Sanitisation may strip tags useful for some use cases such as iframe; you may customize the tags and attributes allowed by overriding the TEXT_ADDITIONAL_ATTRIBUTES setting:

TEXT_ADDITIONAL_ATTRIBUTES = {
    'iframe': {'scrolling', 'allowfullscreen', 'frameborder'},
}

Note that the TEXT_ADDITIONAL_ATTRIBUTES setting is a dictionary, where the keys are the tag names and the values are sets of attribute names.

If you have settings in the style of djangocms-text-ckeditor, which utilizes both TEXT_ADDITIONAL_TAGS and TEXT_ADDITIONAL_ATTRIBUTES, those will be translated for you automatically, but you will get a warning from the Django checks framework at server startup.

NOTE: Some frontend editors will pre-sanitize your text before passing it to the web server, rendering the above settings useless.

To completely disable the feature, set TEXT_HTML_SANITIZE = False.

Development

pre-commit hooks

The repo uses pre-commit git hooks to run tools which ensure code quality.

To utilise this, run pip install pre-commit and then pre-commit install.

Building the JavaScript

djangocms-text distributes a javascript bundle required for the plugin to work, which contains frontend editors themselves and all the necessary plugins for functioning within CMS. To build the bundle you need to have to install dependencies with nvm use, npm install and then to run npx webpack.

Acknowledgments

  • Special thanks to the Django CMS community and all contributors to the djangocms-text-ckeditor project.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

djangocms_text-0.3.0.tar.gz (5.8 MB view details)

Uploaded Source

Built Distribution

djangocms_text-0.3.0-py3-none-any.whl (6.7 MB view details)

Uploaded Python 3

File details

Details for the file djangocms_text-0.3.0.tar.gz.

File metadata

  • Download URL: djangocms_text-0.3.0.tar.gz
  • Upload date:
  • Size: 5.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for djangocms_text-0.3.0.tar.gz
Algorithm Hash digest
SHA256 4dc38c6bd3bd7f295c0b564118815f90b2c4aa7f2995aa65b125087ad04a5418
MD5 85b2ecd5f6eaa3ac78b484710bf40d72
BLAKE2b-256 1a9c42faa818247b6bc11d340b47cd1b80ca83bf4e181676586763729225930d

See more details on using hashes here.

Provenance

File details

Details for the file djangocms_text-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for djangocms_text-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f9adcbb9df2bc263e470ddee40095e571bd5e37ee943afcb9209a81f855c25c6
MD5 b722a352591f73a0aaf7cdcbb81f148c
BLAKE2b-256 88a9d885315f167433a80f5416ba2dedd22df7a9a237b65b0e5854a56e95b0e2

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page