Skip to main content

Rich Textarea for Django with CodeMirror and some enhancement

Project description

Introduction

Django-CodeMirror is a Django application to embed the CodeMirror editor.

It was designed to be used in sveedocuments, so it is suited for a ReStructuredText environment but CodeMirror support a large range of syntax coloration modes (PHP, Python, Ruby, Java, HTML, etc..). It is essentialy a jQuery plugin on top of CodeMirror to add some features like :

  • A button bar with keyboard shortcuts to use some syntax element in your text;

  • A maximize mode to resize the editor at full browser size;

  • A preview mode;

  • A quicksave option;

  • Support translations with english and french allready shipped;

  • Compatibility with Django CSRF.

Requires

Your project will have to includes a copy of these Javascript libraries :

Install

Getting CodeMirror

CodeMirror is included as a git submodule, so you can install it from Github. Within the root of the Django-CodeMirror repository do this :

git submodule update --init

This is optionnal, if you want you can download it and install it yourself.

Settings

In your settings file add the app to your installed apps :

INSTALLED_APPS = (
    ...
    'djangocodemirror',
    ...
)

And you will need to have a copy of CodeMirror in your statics directory (see Django staticfiles). The jQuery library must be called by your templates, Django-CodeMirror don’t do it for you.

Usage

DjangoCodeMirror

DjangoCodeMirror is the jQuery plugin on top of CodeMirror, it accepts all CodeMirror options and some additional :

fullscreen

This enable the maximize mode at true. It is enabled by default.

help_link

Help page link to put in button bar if filled. If the string is empty there will be no help button displayed. When clicked the link is opened in a new window.

quicksave_url

When the string is not empty, it is used as the URL to send data in POST request where the view receiver should save the data. This is disabled by default. If the csrf option is enabled, it will be used in the request.

The default sended datas are :

  • nocache : a timestamp used to block some browser caching, this can be ignored;

  • content : the textarea content.

More datas can be sended with the quicksave_datas option.

quicksave_datas

Expect an object {...} whose variables will be sended as data in quicksave request.

Or it can be a string that determine a variable name to find the object in the global context. This is useful if you want to use a variable that can change and not a defined object at page load.

preview_url

When the string is not empty, it is used as the URL to send data in POST request where the view receiver should render the content with a parser. The excepted response must return the HTML fragment rendered. This is disabled by default. If the csrf option is enabled, it will be used in the request.

The default sended datas are :

  • nocache : a timestamp used to block some browser caching, this can be ignored;

  • content : the textarea content.

csrf

Expect a string containing the function name which be used to modify a request to add it the needed token by Django CSRF. The token will be injected in the request headers. A ready to use function is allready shipped.

The function have two required arguments :

  • xhr : the jQuery XMLHTTPRequest to be modified;

  • settings : the settings object used with jQuery.axax().

You should see the option beforeSend of jQuery.axax() for more details, this is where the csrf function is really used.

display_cursor_position

At True it enable the display of current line and column in the bottom right of the editor. This option is enabled by default.

no_tab_char

At True the usage of the tabulation key will not write a tabulation character and spaces will be writed in replacment. The number of spaces will be determined from the tabSize option (default to 4) from CodeMirror.

undo_buttons

At True it display buttons Undo and Redo in the buttons bar. Enabled by default.

settings_cookie

When the string is not empty, it is used as the cookie name where to search settings to overwrite the default ones (of Django-CodeMirror).

search_enabled

Only for your application settings, the plugin doesn’t know of this option. At True this will enable the search & replace feature of CodeMirror. This is enabled by default for DjangoCodeMirrorField and the demo settings.

A full example of these settings with the plugin :

<div>
    <textarea id="id_content" rows="10" cols="40" name="content"></textarea>
    <script language="JavaScript" type="text/javascript">
    //<![CDATA[
        my_datas = {'foo': 'bar'};
        $(document).ready(function() {
            id_content_codemirror_instance = $('#id_content').djangocodemirror({
                "mode": "rst",
                "csrf": "CSRFpass",
                "fullscreen": true,
                "help_link": "/help/",
                "quicksave_url": "/djangocodemirror-sample/quicksave/",
                "quicksave_datas": my_datas,
                "preview_url": "/djangocodemirror-sample/preview/",
                "display_cursor_position": true,
                "no_tab_char": true,
                "undo_buttons": true,
                "settings_cookie": "djancocodemirror_settings",
                "lineNumbers": true
            });
        });
    //]]>
    </script>
</div>

The plugin use some additional libraries (allready shipped) :

CodeMirrorWidget

This is the widget to use in your form fields to apply them an instance of DjangoCodeMirror or CodeMirror. It is accessible at djangocodemirror.fields.CodeMirrorWidget.

Usage example on a form field :

from djangocodemirror.fields import CodeMirrorWidget

class CodeMirrorSampleForm(forms.Form):
    content = forms.CharField(label=u"Your content", widget=CodeMirrorWidget)

    def save(self, *args, **kwargs):
        return

The widget accept two additional arguments :

  • codemirror_only A boolean to disable the DjangoCodeMirror usage at benefit of CodeMirror. It is False by default;

  • codemirror_attrs : A dict to define the editor settings. It is empty by default.

  • embed_settings : A boolean to disable automatic embedding of Javascript settings in the widget HTML, you will have to load it manually (using the given template tags);

Another example where the content field will be a CodeMirror editor with enabled line numbers :

from djangocodemirror.fields import CodeMirrorWidget

class CodeMirrorSampleForm(forms.Form):
    content = forms.CharField(label="Your content", widget=CodeMirrorWidget(codemirror_only=True, codemirror_attrs={'lineNumbers':True}))

    def save(self, *args, **kwargs):
        return

Medias

The widget load automatically all his needed medias and static files, you just have to put this in your templates :

{{ form.media }}

This behavior is inherited by DjangoCodeMirrorField and CodeMirrorField.

CodeMirrorField

This inherit from django.forms.CharField to automatically use CodeMirrorWidget as the widget field. The widget set the codemirror_only attribute to True to use only the CodeMirror editor.

It take an additional named argument codemirror_attrs like CodeMirrorWidget, his default value correspond to the default setting of CODEMIRROR_SETTINGS.

from django import forms
from djangocodemirror.fields import CodeMirrorField

class CodeMirrorSampleForm(forms.Form):
    content_codemirror = CodeMirrorField(label=u"Your content", codemirror_attrs={'lineNumbers':True})

    def save(self, *args, **kwargs):
        return

DjangoCodeMirrorField

It is identical as CodeMirrorField but for usage of DjangoCodeMirror as the widget field.

His default value for codemirror_attrs correspond to DJANGOCODEMIRROR_DEFAULT_SETTING.

from django import forms
from djangocodemirror.fields import CodeMirrorField

class CodeMirrorSampleForm(forms.Form):
    content_djangocodemirror = DjangoCodeMirrorField(label=u"Your content", codemirror_attrs={'lineNumbers':True})

    def save(self, *args, **kwargs):
        return

Application settings

All default app settings is located in the settings_local.py file of djangocodemirror, you can modify them in your project settings.

CODEMIRROR_FIELD_INIT_JS

Type : string

HTML code to instantiate CodeMirror in form fields, this is a template string (usable with String.format()) which expect two variable places :

  • {inputid} : Will be the unique field id;

  • {settings} : Will be a JSON string representation of the editor settings.

DJANGOCODEMIRROR_FIELD_INIT_JS

Type : string

This identical to CODEMIRROR_FIELD_INIT_JS but for DjangoCodeMirror usage only.

CODEMIRROR_SETTINGS

Type : dict

The settings schemes to use with CodeMirror and DjangoCodeMirror editors. Each editor form fields use this schemes to get their default settings. Note that these options must be suitable to be transformed by the Python JSON parser.

The default available settings schemes are :

  • default : Only for enable the option to show line numbers;

  • djangocodemirror : Minimal options for DjangoCodeMirror (line numbers and mode rst for ReStructuredText);

  • djangocodemirror_with_preview : Same as djangocodemirror but enable the preview option on preview/;

  • djangocodemirror_sample_demo : Same as djangocodemirror but enable all stuff needed in the Sample demonstration.

DJANGOCODEMIRROR_DEFAULT_SETTING

Type : string

The keyword to use to select the default settings with DjangoCodeMirrorField. Note that CodeMirrorField always use the keyword default to select his default settings.

DJANGOCODEMIRROR_TRANSLATIONS

Type : list or tuple

A list of paths for available translations.

CODEMIRROR_THEMES

Type : list or tuple

A list of paths for available themes to load with CodeMirror. There is actually no loaded theme by default, you will have to set one in your CODEMIRROR_SETTINGS

CODEMIRROR_MODES

Type : list or tuple

A list of tuples for the various syntax coloration modes supported by CodeMirror. This list is generated from the available mode files in CodeMirror.

Fields medias

The CodeMirrorWidget widget need some medias to correctly load the editor, all these medias paths are defined in settings and you can change them if needed. Theses paths assume to be in your staticfiles directory (see Django staticfiles).

CODEMIRROR_FILEPATH_LIB

The JavaScript core library of CodeMirror.

CODEMIRROR_FILEPATH_CSS

The CSS file of CodeMirror.

CODEMIRROR_FILEPATH_DIALOG_LIB

The Javascript componant to enable dialogs of CodeMirror.

CODEMIRROR_FILEPATH_DIALOG_CSS

The CSS file used by dialogs componant of CodeMirror.

CODEMIRROR_FILEPATH_SEARCH_LIB

The Javascript componant to enable search and replace in CodeMirror.

CODEMIRROR_FILEPATH_SEARCHCURSOR_LIB

The Javascript componant to enable search highlights in CodeMirror.

DJANGOCODEMIRROR_FILEPATH_LIB

The Javascript core library of DjangoCodeMirror.

DJANGOCODEMIRROR_FILEPATH_TRANSLATION

The Javascript componant to enable translations for DjangoCodeMirror.

DJANGOCODEMIRROR_FILEPATH_CSS

The CSS file of DjangoCodeMirror.

DJANGOCODEMIRROR_FILEPATH_BUTTONS

The Javascript componant of DjangoCodeMirror to define the avalaible buttons in the button bar. Change this path to your own componant if you want to change the button bar.

DJANGOCODEMIRROR_FILEPATH_METHODS

The Javascript componant of DjangoCodeMirror to define the internal methods used with the syntax buttons. If you add some new button in your own button bar, you have to make your own methods file too.

DJANGOCODEMIRROR_FILEPATH_CONSOLE

The Javascript componant of DjangoCodeMirror which define the usage of qTip.

DJANGOCODEMIRROR_FILEPATH_CSRF

The Javascript componant of DjangoCodeMirror used in the editor requests (preview or quicksave) to apply Django CSRF.

DJANGOCODEMIRROR_FILEPATH_COOKIES

Le plugin jQuery pour utiliser accéder aux cookies, nécessaire pour Django CSRF.

QTIP_FILEPATH_LIB

The JavaScript core library of qTip2.

QTIP_FILEPATH_CSS

The CSS file of qTip2.

Template tags

You will need to load the template tags module in your templates like this :

{% load djangocodemirror_inputs %}

Filters

djangocodemirror_input_settings

Get the generated widget settings and return it as JSON. It take the form field as required argument like this :

{{ form.content|djangocodemirror_input_settings }}
djangocodemirror_init_input

Return the HTML tag to embed the Javascript init for a djangocodemirror input field. Take the same argument as djangocodemirror_input_settings.

Sample demonstration

You can rapidly insert Django-CodeMirror in your project in adding djangocodemirror.urls to your project urls.py file. This will use djangocodemirror.views which contains the demonstration views.

urlpatterns = patterns('',
    ...
    (r'^djangocodemirror-sample/', include('djangocodemirror.urls')),
    ...
)

Three views are avalaible :

  • The editor demonstration on djangocodemirror-sample/ using ReStructuredText;

  • The preview view preview/ used in editor demo, it require sveedocuments to work correctly or it will simply return a dummy content. This view accepts only POST request and return an empty response for all request type (like GET);

  • The quicksave view quicksave/ used in editor demo, doesn’t really save anything, just do some validation. It require sveedocuments to work correctly.

  • A public view settings/ usable to edit some settings for the editor. These custom settings will be saved in a cookie.

Internationalization and localization

This application make usage of the Django internationalization system only in his demonstration. However the editor is translated with his own system using a javascript file for each available language.

To add a new language, you will have to add a new javascript file that will register the new available language. Just create a file with this :

DCM_Translations["NAME"] = {
    // Translations goes here
};

Where NAME is the language locale name to register and // Translations goes here must be replaced by the content to translate. To see a full translation see the french version in static/djangocodemirror/djangocodemirror.fr.js where you can see all the string to translate.

You can save your file where you want in your project or application, you will just have to register it in the setting DJANGOCODEMIRROR_TRANSLATIONS.

Project details


Download files

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

Source Distribution

djangocodemirror-0.7.2.tar.gz (3.2 MB view hashes)

Uploaded Source

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