Skip to content

Commit

Permalink
Merge pull request nanvel#2 from imposeren/master
Browse files Browse the repository at this point in the history
allow widget to select editor options
  • Loading branch information
nanvel committed Jul 28, 2014
2 parents 767e3e3 + 6e955e2 commit 61ec452
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 43 deletions.
33 changes: 33 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,39 @@ template:

See `testproject <https://github.com/nanvel/django-nicedit/tree/master/testproject>`__ for example.

Usage in admin:

.. code-block:: python
# -*- coding: utf-8 -*-
from django import forms
from django.contrib import admin
from nicedit.widgets import NicEditAdminWidget
from .models import Item
class ItemAdminForm(forms.ModelForm):
class Meta:
model = Item
widgets = {
'text': NicEditAdminWidget(
attrs={'style': 'width: 610px;'},
js_options={"buttonList": [
'save', 'bold', 'italic', 'underline', 'left', 'center',
'right', 'justify', 'ol', 'ul', 'fontSize', # 'fontFamily',
'fontFormat', 'indent', 'outdent', 'image', 'upload', 'link',
'unlink', 'forecolor', 'bgcolor', 'xhtml']}
),
}
class ItemAdmin(admin.ModelAdmin):
form = ItemAdminForm
Contribute
----------

Expand Down
5 changes: 5 additions & 0 deletions nicedit/static/admin/css/nicedit.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

form .aligned .nicEdit-main p {
margin: 0;
padding: 0;
}
Binary file added nicedit/static/img/nicEditorIcons.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 37 additions & 36 deletions nicedit/static/js/nicedit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* NicEdit is distributed under the terms of the MIT license
* For more information visit http://nicedit.com/
* Do not remove this copyright message
*/
*/
var bkExtend = function(){
var args = arguments;
if (args.length == 1) args = [this, args[0]];
Expand Down Expand Up @@ -246,9 +246,9 @@ Function.prototype.closureListener = function() {
};
}



/* START CONFIG */


var nicEditorConfig = bkClass.extend({
buttons : {
'bold' : {name : __('Click to Bold'), command : 'Bold', tags : ['B','STRONG'], css : {'font-weight' : 'bold'}, key : 'b'},
Expand All @@ -266,14 +266,14 @@ var nicEditorConfig = bkClass.extend({
'removeformat' : {name : __('Remove Formatting'), command : 'removeformat', noActive : true},
'indent' : {name : __('Indent Text'), command : 'indent', noActive : true},
'outdent' : {name : __('Remove Indent'), command : 'outdent', noActive : true},
'hr' : {name : __('Horizontal Rule'), command : 'insertHorizontalRule', noActive : true},
'hr' : {name : __('Horizontal Rule'), command : 'insertHorizontalRule', noActive : true}
},
iconsPath : '/static/img/niceditoricons.gif',
buttonList : ['save','bold','italic','underline','left','center','right','justify','ol','ul','fontSize','fontFamily','fontFormat','indent','outdent','image','upload','link','unlink','forecolor','bgcolor','xhtml'],
iconsPath : '/static/img/nicEditorIcons.gif',
buttonList : ['save','bold','italic','underline','left','center','right','justify','ol','ul','fontSize','fontFamily','fontFormat','indent','outdent','image','upload','link','unlink','forecolor','bgcolor'],
iconList : {"xhtml":1,"bgcolor":2,"forecolor":3,"bold":4,"center":5,"hr":6,"indent":7,"italic":8,"justify":9,"left":10,"ol":11,"outdent":12,"removeformat":13,"right":14,"save":25,"strikethrough":16,"subscript":17,"superscript":18,"ul":19,"underline":20,"image":21,"link":22,"unlink":23,"close":24,"arrow":26,"upload":27}

});
/* END CONFIG */
/* END CONFIG */


var nicEditors = {
Expand Down Expand Up @@ -405,7 +405,7 @@ var nicEditor = bkClass.extend({
}

});
nicEditor = nicEditor.extend(bkEvent);
nicEditor = nicEditor.extend(bkEvent);


var nicEditorInstance = bkClass.extend({
Expand Down Expand Up @@ -578,7 +578,7 @@ var nicEditorInstance = bkClass.extend({
document.execCommand(cmd,false,args);
}
});


var nicEditorIFrameInstance = nicEditorInstance.extend({
savedStyles : [],

Expand Down Expand Up @@ -647,7 +647,7 @@ var nicEditorIFrameInstance = nicEditorInstance.extend({
}


});
});
var nicEditorPanel = bkClass.extend({
construct : function(e,options,nicEditor) {
this.elm = e;
Expand Down Expand Up @@ -701,7 +701,7 @@ var nicEditorPanel = bkClass.extend({
remove : function() {
this.elm.remove();
}
});
});
var nicEditorButton = bkClass.extend({

construct : function(e,buttonName,options,nicEditor) {
Expand Down Expand Up @@ -841,7 +841,7 @@ var nicEditorButton = bkClass.extend({
}
}

});
});


var nicPlugin = bkClass.extend({
Expand All @@ -864,8 +864,8 @@ var nicPlugin = bkClass.extend({

init : function() { }
});






/* START CONFIG */
Expand Down Expand Up @@ -924,8 +924,8 @@ var nicEditorPane = bkClass.extend({
this.pane.setContent(c);
}

});

});



var nicEditorAdvancedButton = nicEditorButton.extend({
Expand Down Expand Up @@ -1008,8 +1008,8 @@ var nicEditorAdvancedButton = nicEditorButton.extend({
this.ne.selectedInstance.restoreRng();
}
}
});

});


var nicButtonTips = bkClass.extend({
construct : function(nicEditor) {
Expand Down Expand Up @@ -1039,8 +1039,8 @@ var nicButtonTips = bkClass.extend({
}
}
});
nicEditors.registerPlugin(nicButtonTips);

nicEditors.registerPlugin(nicButtonTips);



/* START CONFIG */
Expand Down Expand Up @@ -1174,8 +1174,8 @@ var nicEditorFontFormatSelect = nicEditorSelect.extend({
}
});

nicEditors.registerPlugin(nicPlugin,nicSelectOptions);

nicEditors.registerPlugin(nicPlugin,nicSelectOptions);



/* START CONFIG */
Expand Down Expand Up @@ -1221,8 +1221,8 @@ var nicLinkButton = nicEditorAdvancedButton.extend({
}
});

nicEditors.registerPlugin(nicPlugin,nicLinkOptions);

nicEditors.registerPlugin(nicPlugin,nicLinkOptions);



/* START CONFIG */
Expand Down Expand Up @@ -1279,8 +1279,8 @@ var nicEditorBgColorButton = nicEditorColorButton.extend({
}
});

nicEditors.registerPlugin(nicPlugin,nicColorOptions);

nicEditors.registerPlugin(nicPlugin,nicColorOptions);



/* START CONFIG */
Expand Down Expand Up @@ -1327,8 +1327,8 @@ var nicImageButton = nicEditorAdvancedButton.extend({
});

nicEditors.registerPlugin(nicPlugin,nicImageOptions);






/* START CONFIG */
Expand All @@ -1352,8 +1352,8 @@ var nicEditorSaveButton = nicEditorButton.extend({
}
});

nicEditors.registerPlugin(nicPlugin,nicSaveOptions);

nicEditors.registerPlugin(nicPlugin,nicSaveOptions);



/* START CONFIG */
Expand All @@ -1365,7 +1365,8 @@ var nicUploadOptions = {
};
/* END CONFIG */

var nicUploadButton = nicEditorAdvancedButton.extend({
var nicUploadButton = nicEditorAdvancedButton.extend({
nicURI : 'http://api.imgur.com/2/upload.json',
errorText : 'Failed to upload image',

addPane : function() {
Expand Down Expand Up @@ -1413,7 +1414,7 @@ var nicUploadButton = nicEditorAdvancedButton.extend({
fd.append("image", file);
fd.append("csrfmiddlewaretoken", document.getElementsByName("csrfmiddlewaretoken")[0].value);
var xhr = new XMLHttpRequest();
xhr.open("POST", this.ne.options.uploadURI);
xhr.open("POST", this.ne.options.uploadURI || this.nicURI);

xhr.onload = function() {
try {
Expand Down Expand Up @@ -1459,8 +1460,8 @@ var nicUploadButton = nicEditorAdvancedButton.extend({
});

nicEditors.registerPlugin(nicPlugin,nicUploadOptions);






/* START CONFIG */
Expand Down Expand Up @@ -1490,5 +1491,5 @@ var nicCodeButton = nicEditorAdvancedButton.extend({
});

nicEditors.registerPlugin(nicPlugin,nicCodeOptions);



123 changes: 122 additions & 1 deletion nicedit/static/js/nicedit.min.js

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions nicedit/templates/admin/base_site.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends "admin/base.html" %}
{% load i18n %}
{% load staticfiles %}

{% block title %}{{ title }} | {% trans 'Django site admin' %}{% endblock %}

{% block extrastyle %}
{{ block.super }}
<link rel="stylesheet" type="text/css" href="{% static "admin/css/nicedit.css" %}">
{% endblock %}

{% block branding %}
<h1 id="site-name">{% trans 'Django administration' %}</h1>
{% endblock %}

{% block nav-global %}{% endblock %}
22 changes: 16 additions & 6 deletions nicedit/widgets.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-
import json
from django import forms
from django.conf import settings
from django.contrib.staticfiles.storage import staticfiles_storage
Expand All @@ -7,22 +9,30 @@

class NicEditWidget(forms.Textarea):

def __init__(self, *args, **kwargs):
self.js_options = kwargs.pop('js_options', {})
super(NicEditWidget, self).__init__(*args, **kwargs)

class Media:
js = (
staticfiles_storage.url('js/nicedit.min.js'),
)

def render(self, name, value, attrs=None):

rendered = super(NicEditWidget, self).render(name, value, attrs=attrs)
return rendered + mark_safe(u'''
<script>
new nicEditor({uploadURI: '%s'}).panelInstance('id_%s');
</script>''' % (reverse('nicedit_upload'), name))
new nicEditor(%s).panelInstance('id_%s');
</script>''' % (self.js_options, name))


class NicEditAdminWidget(NicEditWidget):

def render(self, name, value, attrs=None):
if not isinstance(self.js_options, (unicode, str)):
self.js_options['uploadURI'] = reverse('nicedit_upload')
self.js_options = json.dumps(self.js_options)
rendered = super(NicEditWidget, self).render(name, value, attrs=attrs)
return rendered + mark_safe(u'''
<script>
Expand All @@ -33,6 +43,6 @@ def render(self, name, value, attrs=None):
container.style.float = 'left';
ta.parentNode.insertBefore(container, ta);
container.appendChild(ta);
new nicEditor({uploadURI: '%s'}).panelInstance(ta);
new nicEditor(%s).panelInstance(ta);
}
</script>''' % (name, reverse('nicedit_upload')))
</script>''' % (name, self.js_options))

0 comments on commit 61ec452

Please sign in to comment.