Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set focus on Redactor when clicking on Redactor's main div #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions suit_redactor/tests/widgets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.test import TestCase
from suit_redactor.widgets import RedactorWidget
from suit_redactor.widgets import RedactorWidget, FOCUS_SCRIPT
from django.contrib.admin.templatetags.admin_static import static


Expand All @@ -21,8 +21,8 @@ def test_RedactorWidget_output(self):
output = widget.render(name, value)
self.assertHTMLEqual(output, (
'<textarea cols="40" name="%s" rows="10">%s</textarea><script '
'type="text/javascript">$("#id_%s").redactor({});</script>' % (
name, value, name)))
'type="text/javascript">$("#id_%s").redactor({});%s</script>' % (
name, value, name, FOCUS_SCRIPT)))

def test_RedactorWidget_output_with_editor_options(self):
widget = RedactorWidget(editor_options={'iframe': True})
Expand All @@ -32,8 +32,8 @@ def test_RedactorWidget_output_with_editor_options(self):
self.assertHTMLEqual(output, (
'<textarea cols="40" name="%s" rows="10">%s</textarea><script '
'type="text/javascript">$("#id_%s").redactor({"iframe": '
'true});</script>' % (
name, value, name)))
'true});%s</script>' % (
name, value, name, FOCUS_SCRIPT)))

def test_RedactorWidget_media(self):
widget = RedactorWidget()
Expand Down
8 changes: 6 additions & 2 deletions suit_redactor/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
try: import json
except ImportError: import django.utils.simplejson as json

FOCUS_SCRIPT = """
$('.redactor_box').click(function(e){if(!$(e.srcElement).is('.redactor_toolbar')){$('.redactor_editor').focus();}});
"""


class RedactorWidget(Textarea):
class Media:
Expand All @@ -21,6 +25,6 @@ def __init__(self, attrs=None, editor_options={}):
def render(self, name, value, attrs=None):
output = super(RedactorWidget, self).render(name, value, attrs)
output += mark_safe(
'<script type="text/javascript">$("#id_%s").redactor(%s);</script>'
% (name, json.dumps(self.editor_options)))
'<script type="text/javascript">$("#id_%s").redactor(%s);%s</script>'
% (name, json.dumps(self.editor_options), FOCUS_SCRIPT))
return output