├── .gitignore ├── CODE_OF_CONDUCT.md ├── LICENSE ├── MANIFEST.in ├── README.md ├── mediumeditor ├── __init__.py ├── admin.py ├── apps.py ├── conf.py ├── static │ ├── css │ │ └── mediumeditor │ │ │ └── django-mediumeditor.css │ └── js │ │ └── mediumeditor │ │ └── django-mediumeditor.js ├── tests.py ├── views.py └── widgets.py ├── setup.cfg └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | .Python 10 | env/ 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | *.egg-info/ 23 | .installed.cfg 24 | *.egg 25 | 26 | # PyInstaller 27 | # Usually these files are written by a python script from a template 28 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 29 | *.manifest 30 | *.spec 31 | 32 | # Installer logs 33 | pip-log.txt 34 | pip-delete-this-directory.txt 35 | 36 | # Unit test / coverage reports 37 | htmlcov/ 38 | .tox/ 39 | .coverage 40 | .coverage.* 41 | .cache 42 | nosetests.xml 43 | coverage.xml 44 | *,cover 45 | 46 | # Translations 47 | *.mo 48 | *.pot 49 | 50 | # Django stuff: 51 | *.log 52 | 53 | # Sphinx documentation 54 | docs/_build/ 55 | 56 | # PyBuilder 57 | target/ 58 | 59 | .DS_Store 60 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of MediumEditor, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. 4 | 5 | We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion. 6 | 7 | Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct. 8 | 9 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team. 10 | 11 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. 12 | 13 | This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/) 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Chad Shrock 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include LICENSE 2 | include README.md 3 | include MANIFEST.in 4 | include setup.cfg 5 | recursive-include mediumeditor/static *.css *.js 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Django Medium Editor 2 | Adds [Medium Editor](https://yabwe.github.io/medium-editor/) to [Django](https://www.djangoproject.com/). 3 | 4 | ## Supported Environments 5 | 6 | * Django 1.10+ 7 | * Python 3.6 8 | 9 | ## Installation 10 | 11 | 1. Install **django-mediumeditor** 12 | 13 | via PyPi: 14 | ``` 15 | pip install django-mediumeditor 16 | ``` 17 | via GIT: 18 | 19 | ``` 20 | pip install git+https://github.com/g3rd/django-mediumeditor.git 21 | ``` 22 | 23 | 2. Install app 24 | ``` 25 | INSTALLED_APPS = [ 26 | ... 27 | 'mediumeditor', 28 | ] 29 | ``` 30 | 31 | ## Usage 32 | 33 | ### Django Admin 34 | 35 | Add to Admin class 36 | 37 | ``` 38 | from mediumeditor.admin import MediumEditorAdmin 39 | 40 | @admin.register(MyModel) 41 | class MyModelAdmin(MediumEditorAdmin, admin.ModelAdmin): 42 | ... 43 | mediumeditor_fields = ('my_text_field', ) 44 | ... 45 | ``` 46 | 47 | ### Forms 48 | 49 | 1. Override the Widget for the field 50 | 51 | ``` 52 | from mediumeditor.widgets import MediumEditorTextarea 53 | 54 | class MyForm(forms.ModelForm): 55 | ... 56 | class Meta: 57 | model = MyModel 58 | widgets = { 59 | 'my_text_field': MediumEditorTextarea(), 60 | } 61 | ``` 62 | 63 | 2. In the ```
``` of the template 64 | ``` 65 | {{ form.media }} 66 | ``` 67 | 68 | ### Settings 69 | Optionaly change theme or change medium editor options in your settings.py 70 | ``` 71 | # Theme options `default`, `roman`, `mani`, `flat`, `bootstrap`, `tim`, `beagle` 72 | MEDIUM_EDITOR_THEME = 'beagle' # `default` is default theme 73 | MEDIUM_EDITOR_OPTIONS = {..} 74 | ``` 75 | [Available Options](https://github.com/yabwe/medium-editor/blob/master/OPTIONS.md) 76 | 77 | Example: 78 | ``` 79 | MEDIUM_EDITOR_THEME = 'bootstrap' 80 | MEDIUM_EDITOR_OPTIONS = { 81 | 'toolbar': { 82 | 'static': True, 83 | 'buttons': [ 84 | 'bold', 85 | 'italic', 86 | 'underline', 87 | 'strikethrough', 88 | 'subscript', 89 | 'superscript', 90 | 'h1', 91 | 'h2', 92 | 'h3', 93 | 'h4', 94 | 'h5', 95 | 'h6', 96 | ] 97 | }, 98 | 'paste': { 99 | 'forcePlainText': True, 100 | 'cleanPastedHTML': False, 101 | 'cleanReplacements': [], 102 | 'cleanAttrs': ['class', 'style', 'dir'], 103 | 'cleanTags': ['meta'] 104 | } 105 | } 106 | ``` 107 | 108 | ## Contributing 109 | 110 | [Take out some bugs](https://github.com/g3rd/django-mediumeditor/issues) 111 | 112 | [Report issues or feature requests](https://github.com/g3rd/django-mediumeditor/issues) 113 | 114 | 115 | ## License 116 | MIT: https://github.com/g3rd/django-mediumeditor/blob/master/LICENSE 117 | -------------------------------------------------------------------------------- /mediumeditor/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/g3rd/django-mediumeditor/9b7fca3b87e25e020a26c1dc3a268be45ddb5e79/mediumeditor/__init__.py -------------------------------------------------------------------------------- /mediumeditor/admin.py: -------------------------------------------------------------------------------- 1 | from mediumeditor.widgets import MediumEditorTextarea 2 | 3 | 4 | class MediumEditorAdmin(object): 5 | 6 | def formfield_for_dbfield(self, db_field, **kwargs): 7 | 8 | if not hasattr(self, 'mediumeditor_fields'): 9 | raise ValueError('mediumeditor_fields is required on model') 10 | 11 | if db_field.name in self.mediumeditor_fields: 12 | return db_field.formfield(widget=MediumEditorTextarea()) 13 | return super(MediumEditorAdmin, self).formfield_for_dbfield( 14 | db_field, **kwargs) 15 | -------------------------------------------------------------------------------- /mediumeditor/apps.py: -------------------------------------------------------------------------------- 1 | from __future__ import unicode_literals 2 | 3 | from django.apps import AppConfig 4 | from django.utils.translation import ugettext as _ 5 | 6 | 7 | class MediumEditorConfig(AppConfig): 8 | name = _('medium editor') 9 | -------------------------------------------------------------------------------- /mediumeditor/conf.py: -------------------------------------------------------------------------------- 1 | from django.conf import settings # noqa 2 | from appconf import AppConf 3 | 4 | 5 | class MediumEditorConf(AppConf): 6 | THEME = 'default' 7 | OPTIONS = { 8 | 'toolbar': { 9 | 'static': True, 10 | 'buttons': [ 11 | 'bold', 12 | 'italic', 13 | 'underline', 14 | 'strikethrough', 15 | 'subscript', 16 | 'superscript', 17 | 'anchor', 18 | 'quote', 19 | 'pre', 20 | 'orderedlist', 21 | 'unorderedlist', 22 | 'indent', 23 | 'outdent', 24 | 'justifyLeft', 25 | 'justifyCenter', 26 | 'justifyRight', 27 | 'justifyFull', 28 | 'h1', 29 | 'h2', 30 | 'h3', 31 | 'h4', 32 | 'h5', 33 | 'h6', 34 | ] 35 | } 36 | } 37 | 38 | class Meta: 39 | prefix = 'medium_editor' 40 | -------------------------------------------------------------------------------- /mediumeditor/static/css/mediumeditor/django-mediumeditor.css: -------------------------------------------------------------------------------- 1 | .django-mediumeditor-input { 2 | display: none; 3 | } 4 | 5 | .django-mediumeditor-editable { 6 | border-radius: 4px; 7 | border: 1px solid #ccc; 8 | margin: 0; 9 | padding: 5px 6px; 10 | } 11 | 12 | .change-form .django-mediumeditor-editable { 13 | margin: 0 0 0 170px; 14 | } 15 | 16 | .django-mediumeditor-editable p { 17 | min-height: 1.4em; 18 | } 19 | 20 | .django-mediumeditor-editable.medium-editor-placeholder:after { 21 | left: 6px; 22 | margin: 2px 0; 23 | padding: 2px 0; 24 | top: 5px; 25 | } 26 | -------------------------------------------------------------------------------- /mediumeditor/static/js/mediumeditor/django-mediumeditor.js: -------------------------------------------------------------------------------- 1 | (function() { 2 | function setup() { 3 | var selector = '.django-mediumeditor-editable'; 4 | var editor = new MediumEditor(selector, MediumEditorOptions); 5 | // update the text box 6 | editor.subscribe('blur', function (event, editable) { 7 | // Get the HTML from the editor 8 | var html = editable.innerHTML.trim(); 9 | // Grab the selector for the associated text area 10 | var textareaSelector = '#' + editable.attributes['data-mediumeditor-textarea'].value; 11 | // Update the value of the textarea to the HTML 12 | document.querySelectorAll(textareaSelector)[0].value = html; 13 | }); 14 | // load the data in 15 | var editableElements = document.querySelectorAll(selector); 16 | Array.prototype.forEach.call(editableElements, function(el, i){ 17 | // Grab the selector for the associated text area 18 | var textareaSelector = '#' + el.attributes['data-mediumeditor-textarea'].value; 19 | // Get the stored HTML 20 | var html = document.querySelectorAll(textareaSelector)[0].value; 21 | // Set the value 22 | editor.setContent(html, i); 23 | }); 24 | }; 25 | 26 | // Wait for the DOM to be loaded before configuring the editor 27 | if (document.readyState != 'loading'){ 28 | setup(); 29 | } else { 30 | document.addEventListener('DOMContentLoaded', setup); 31 | } 32 | 33 | })(); 34 | -------------------------------------------------------------------------------- /mediumeditor/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /mediumeditor/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render 2 | 3 | # Create your views here. 4 | -------------------------------------------------------------------------------- /mediumeditor/widgets.py: -------------------------------------------------------------------------------- 1 | from __future__ import unicode_literals 2 | import json 3 | from django import forms 4 | from django.utils.safestring import mark_safe 5 | 6 | from .conf import settings 7 | 8 | 9 | class MediumEditorTextarea(forms.Textarea): 10 | 11 | def render(self, name, value, attrs=None,renderer=None): 12 | 13 | if attrs is None: 14 | attrs = {} 15 | attrs.update({'class': 'django-mediumeditor-input'}) 16 | 17 | identifier = attrs.get('id', 'id_{}'.format(name)) 18 | params = { 19 | 'data-mediumeditor-textarea': identifier, 20 | 'class': 'django-mediumeditor-editable', 21 | 'id': '{}_editable'.format(identifier), 22 | } 23 | param_str = ' '.join('{}="{}"'.format(k, v) for k, v in params.items()) 24 | html = super(MediumEditorTextarea, self).render(name, value, attrs) 25 | options = json.dumps(settings.MEDIUM_EDITOR_OPTIONS) 26 | html = mark_safe(u'''{} 27 | 28 | '''.format(html, param_str, options)) 31 | return html 32 | 33 | class Media: 34 | css = {'all': ( 35 | '//cdn.jsdelivr.net/medium-editor/latest/css/' 36 | 'medium-editor.min.css', 37 | 'css/mediumeditor/django-mediumeditor.css', 38 | '//cdn.jsdelivr.net/medium-editor/latest/css/themes/{}.min.css'.format( 39 | settings.MEDIUM_EDITOR_THEME 40 | ) 41 | )} 42 | js = ( 43 | '//cdn.jsdelivr.net/medium-editor/latest/js/medium-editor.min.js', 44 | 'js/mediumeditor/django-mediumeditor.js', ) 45 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | description-file = README.md 3 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | 3 | long_description = open('README.md').read() 4 | 5 | setup( 6 | name="django-mediumeditor", 7 | version='1.0.0', 8 | packages=["mediumeditor"], 9 | include_package_data=True, 10 | description="Medium Editor widget for Django", 11 | url="https://github.com/g3rd/django-mediumeditor", 12 | author="Chad Shryock", 13 | author_email="chad@aceportfol.io", 14 | license='MIT', 15 | long_description=long_description, 16 | platforms=["any"], 17 | classifiers=[ 18 | "Development Status :: 4 - Beta", 19 | "Environment :: Web Environment", 20 | "Framework :: Django", 21 | "Intended Audience :: Developers", 22 | "Operating System :: OS Independent", 23 | "Programming Language :: Python", 24 | "Topic :: Software Development :: Libraries :: Python Modules", 25 | ], 26 | install_requires=[ 27 | 'django-appconf >= 1.0.2', 28 | ], 29 | ) 30 | --------------------------------------------------------------------------------