Skip to content

Commit

Permalink
Upgrade to codemirror 6
Browse files Browse the repository at this point in the history
  • Loading branch information
inducer committed Jul 26, 2024
1 parent f0ca458 commit ea36dc0
Show file tree
Hide file tree
Showing 22 changed files with 1,084 additions and 401 deletions.
24 changes: 24 additions & 0 deletions accounts/migrations/0014_alter_user_editor_mode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.2.14 on 2024-07-24 19:16

from django.db import migrations, models


def switch_sublime_to_default(apps, schema_editor):
User = apps.get_model("accounts", "User")
User.objects.filter(editor_mode="sublime").update(editor_mode="default")


class Migration(migrations.Migration):

dependencies = [
('accounts', '0013_alter_user_email_alter_user_username'),
]

operations = [
migrations.RunPython(switch_sublime_to_default),
migrations.AlterField(
model_name='user',
name='editor_mode',
field=models.CharField(choices=[('default', 'Default'), ('emacs', 'Emacs'), ('vim', 'Vim')], default='default', help_text="Which key bindings you prefer when editing larger amounts of text or code. (If you do not understand what this means, leave it as 'Default'.)", max_length=20, verbose_name='Editor mode'),
),
]
1 change: 0 additions & 1 deletion accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ class User(AbstractBaseUser, PermissionsMixin):
"leave it as 'Default'.)"),
choices=(
("default", _("Default")),
("sublime", "Sublime text"),
("emacs", "Emacs"),
("vim", "Vim"),
),
Expand Down
5 changes: 1 addition & 4 deletions course/exam.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,7 @@ def __init__(self, course, editor_mode, *args, **kwargs):

from course.utils import get_codemirror_widget
cm_widget, _cm_help_text = get_codemirror_widget(
language_mode={"name": "markdown", "xml": True},
dependencies=("xml",),
language_mode="markdown",
interaction_mode=editor_mode)

help_text = (gettext('Enter <a href="http://documen.tician.de/'
Expand Down Expand Up @@ -374,8 +373,6 @@ def __init__(self, course, editor_mode, *args, **kwargs):
required=False,
widget=forms.PasswordInput())

self.style_codemirror_widget()

self.helper.add_input(
Submit(
"issue",
Expand Down
14 changes: 6 additions & 8 deletions course/page/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -941,17 +941,15 @@ def __init__(self, point_value, *args,
create_default_point_scale(point_value)),
label=_("Grade points"))

from codemirror import CodeMirrorJavascript

from course.utils import get_codemirror_widget
from course.utils import JsLiteral, get_codemirror_widget
cm_widget, cm_help_text = get_codemirror_widget(
language_mode="markdown",
interaction_mode=editor_interaction_mode,
additional_keys={
"Ctrl-Space":
CodeMirrorJavascript("rlUtils.goToNextPointsField"),
"Shift-Ctrl-Space":
CodeMirrorJavascript("rlUtils.goToPreviousPointsField"),
"Ctrl-;":
JsLiteral("rlUtils.goToNextPointsField"),
"Shift-Ctrl-;":
JsLiteral("rlUtils.goToPreviousPointsField"),
})
self.fields["feedback_text"] = forms.CharField(
widget=cm_widget,
Expand All @@ -964,7 +962,7 @@ def __init__(self, point_value, *args,
"RELATE-flavored Markdown</a>. "
"See RELATE documentation for automatic computation of point "
"count from <tt>[pts:N/N]</tt> and <tt>[pts:N]</tt>. "
"Use Ctrl-Space/Ctrl-Shift-Space "
"Use Ctrl-Semicolon/Ctrl-Shift-Semicolon "
"to move between <tt>[pts:]</tt> fields. ")
+ cm_help_text),
label=_("Feedback text (Ctrl+Shift+F)"))
Expand Down
4 changes: 1 addition & 3 deletions course/page/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ def __init__(self, read_only, interaction_mode, initial_code,
cm_widget, cm_help_text = get_codemirror_widget(
language_mode=language_mode,
interaction_mode=interaction_mode,
read_only=read_only,

# Automatically focus the text field once there has
# been some input.
Expand All @@ -167,10 +166,9 @@ def __init__(self, read_only, interaction_mode, initial_code,
initial=initial_code,
help_text=cm_help_text,
widget=cm_widget,
disabled=read_only,
label=_("Answer"))

self.style_codemirror_widget()

def clean(self):
# FIXME Should try compilation
pass
Expand Down
16 changes: 5 additions & 11 deletions course/page/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class TextAnswerForm(StyledForm):
use_required_attribute = False

@staticmethod
def get_text_widget(widget_type, read_only=False, check_only=False,
def get_text_widget(widget_type, check_only=False,
interaction_mode=None, initial_text=None):
"""Returns None if no widget found."""

Expand All @@ -59,18 +59,14 @@ def get_text_widget(widget_type, read_only=False, check_only=False,

widget = forms.TextInput()
widget.attrs["autofocus"] = None
if read_only:
widget.attrs["readonly"] = None
return widget, None

elif widget_type == "textarea":
if check_only:
return True

widget = forms.Textarea()
# widget.attrs["autofocus"] = None
if read_only:
widget.attrs["readonly"] = None
widget.attrs["autofocus"] = None
return widget, None

elif widget_type.startswith("editor:"):
Expand All @@ -80,8 +76,7 @@ def get_text_widget(widget_type, read_only=False, check_only=False,
from course.utils import get_codemirror_widget
cm_widget, cm_help_text = get_codemirror_widget(
language_mode=widget_type[widget_type.find(":")+1:],
interaction_mode=interaction_mode,
read_only=read_only)
interaction_mode=interaction_mode)

return cm_widget, cm_help_text

Expand All @@ -94,18 +89,17 @@ def __init__(self, read_only, interaction_mode, validators, *args, **kwargs):

super().__init__(*args, **kwargs)
widget, help_text = self.get_text_widget(
widget_type, read_only,
widget_type,
interaction_mode=interaction_mode)
self.validators = validators
self.fields["answer"] = forms.CharField(
required=True,
initial=initial_text,
disabled=read_only,
widget=widget,
help_text=help_text,
label=_("Answer"))

self.style_codemirror_widget()

def clean(self):
cleaned_data = super().clean()

Expand Down
3 changes: 2 additions & 1 deletion course/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ def __init__(self, initial_text: str,
from course.utils import get_codemirror_widget
cm_widget, cm_help_text = get_codemirror_widget(
language_mode=language_mode,
interaction_mode=interaction_mode)
interaction_mode=interaction_mode,
autofocus=True)

self.fields["content"] = forms.CharField(
required=False,
Expand Down
37 changes: 1 addition & 36 deletions course/templates/course/flow-page.html
Original file line number Diff line number Diff line change
Expand Up @@ -400,22 +400,6 @@
{
var input_changed = false;

// {{{ listen for codemirror changes

function on_cm_change(cm, change_obj)
{
input_changed = true;
}

$("div.CodeMirror").each(
function ()
{
var cm = this.CodeMirror;
cm.on("change", on_cm_change);
});

// }}}

// {{{ listen for other input changes

function on_input_change(evt)
Expand All @@ -434,7 +418,7 @@
$(window).on('beforeunload',
function()
{
if (input_changed)
if (input_changed || (rlCodemirror && rlCodemirror.anyEditorChanged()))
return "{% trans 'You have unsaved changes on this page.' %}";
});

Expand Down Expand Up @@ -559,25 +543,6 @@

{# }}} #}

{# {{{ codemirror resizing, save #}

<script type="text/javascript">
$("div.CodeMirror")
.each(
function ()
{
function submit_page_for_feedback()
{
$(".relate-submit-button").click();
}

var cm = this.CodeMirror;
cm.save = submit_page_for_feedback;
});
</script>

{# }}} #}

{% endblock %}

{% block page_bottom_javascript_extra %}
Expand Down
Loading

0 comments on commit ea36dc0

Please sign in to comment.