def __init__(self, read_only, initial_code, *args, **kwargs): super(PythonCodeForm, self).__init__(*args, **kwargs) from codemirror import CodeMirrorTextarea, CodeMirrorJavascript theme = "default" if read_only: theme += " relate-readonly" self.fields["answer"] = forms.CharField( required=True, initial=initial_code, help_text="Hit F9 to toggle full screen mode.", widget=CodeMirrorTextarea( mode="python", theme=theme, addon_css=( "dialog/dialog", "display/fullscreen", ), addon_js=( "search/searchcursor", "dialog/dialog", "search/search", "edit/matchbrackets", "comment/comment", "display/fullscreen", "selection/active-line", ), config={ "fixedGutter": True, "indentUnit": 4, "matchBrackets": True, "styleActiveLine": True, "readOnly": read_only, # "autofocus": not read_only, "extraKeys": CodeMirrorJavascript(""" { "Ctrl-/": "toggleComment", "Tab": function(cm) { var spaces = \ Array(cm.getOption("indentUnit") + 1).join(" "); cm.replaceSelection(spaces); }, "F9": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); } } """) }), )
def get_codemirror_widget(): # type: (...) -> CodeMirrorTextarea theme = "default" addon_css = ( "dialog/dialog", "display/fullscreen", ) addon_js = ( "search/searchcursor", "dialog/dialog", "search/search", "comment/comment", "edit/matchbrackets", "display/fullscreen", "selection/active-line", "edit/trailingspace", ) indent_unit = 2 config = { "autofocus": True, "fixedGutter": True, "matchBrackets": True, "styleActiveLine": True, "showTrailingSpace": True, "indentUnit": indent_unit, "readOnly": False, "extraKeys": CodeMirrorJavascript(""" { "Ctrl-/": "toggleComment", "Tab": function(cm) { // from https://github.com/codemirror/CodeMirror/issues/988 if (cm.doc.somethingSelected()) { return CodeMirror.Pass; } var spacesPerTab = cm.getOption("indentUnit"); var spacesToInsert = ( spacesPerTab - (cm.doc.getCursor("start").ch % spacesPerTab)); var spaces = Array(spacesToInsert + 1).join(" "); cm.replaceSelection(spaces, "end", "+input"); }, "Shift-Tab": "indentLess", "F9": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); } } """) } return CodeMirrorTextarea(mode="stex", theme=theme, addon_css=addon_css, addon_js=addon_js, config=config)
def get_codemirror_widget( language_mode, # type: Text interaction_mode, # type: Text config=None, # type: Optional[Dict] addon_css=(), # type: Tuple addon_js=(), # type: Tuple dependencies=(), # type: Tuple read_only=False, # type: bool ): # type: (...) -> CodeMirrorTextarea theme = "default" if read_only: theme += " relate-readonly" from django.urls import reverse help_text = (_("Press F9 to toggle full-screen mode. ") + _("Set editor mode in <a href='%s'>user profile</a>.") % reverse("relate-user_profile")) actual_addon_css = ( "dialog/dialog", "display/fullscreen", ) + addon_css actual_addon_js = ( "search/searchcursor", "dialog/dialog", "search/search", "comment/comment", "edit/matchbrackets", "display/fullscreen", "selection/active-line", "edit/trailingspace", ) + addon_js if language_mode == "python": indent_unit = 4 else: indent_unit = 2 actual_config = { "fixedGutter": True, #"autofocus": True, "matchBrackets": True, "styleActiveLine": True, "showTrailingSpace": True, "indentUnit": indent_unit, "readOnly": read_only, "extraKeys": CodeMirrorJavascript(""" { "Ctrl-/": "toggleComment", "Tab": function(cm) { // from https://github.com/codemirror/CodeMirror/issues/988 if (cm.doc.somethingSelected()) { return CodeMirror.Pass; } var spacesPerTab = cm.getOption("indentUnit"); var spacesToInsert = ( spacesPerTab - (cm.doc.getCursor("start").ch % spacesPerTab)); var spaces = Array(spacesToInsert + 1).join(" "); cm.replaceSelection(spaces, "end", "+input"); }, "Shift-Tab": "indentLess", "F9": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); } } """) } if interaction_mode == "vim": actual_config["vimMode"] = True actual_addon_js += ('../keymap/vim',) elif interaction_mode == "emacs": actual_config["keyMap"] = "emacs" actual_addon_js += ('../keymap/emacs',) elif interaction_mode == "sublime": actual_config["keyMap"] = "sublime" actual_addon_js += ('../keymap/sublime',) # every other interaction mode goes to default if config is not None: actual_config.update(config) return CodeMirrorTextarea( mode=language_mode, dependencies=dependencies, theme=theme, addon_css=actual_addon_css, addon_js=actual_addon_js, config=actual_config), help_text
def get_text_widget(widget_type, read_only=False, check_only=False): """Returns None if no widget found.""" if widget_type in [None, "text_input"]: if check_only: return True widget = forms.TextInput() widget.attrs["autofocus"] = None if read_only: widget.attrs["readonly"] = None return widget elif widget_type == "textarea": if check_only: return True widget = forms.Textarea() # widget.attrs["autofocus"] = None if read_only: widget.attrs["readonly"] = None return widget elif widget_type in ["editor:markdown", "editor:yaml"]: if check_only: return True editor_mode = widget_type[widget_type.find(":") + 1:] theme = "default" if read_only: theme += " relate-readonly" from codemirror import CodeMirrorTextarea, CodeMirrorJavascript return CodeMirrorTextarea( mode=editor_mode, theme=theme, addon_css=( "dialog/dialog", "display/fullscreen", ), addon_js=( "search/searchcursor", "dialog/dialog", "search/search", "edit/matchbrackets", "display/fullscreen", "selection/active-line", ), config={ "fixedGutter": True, # "autofocus": True, "matchBrackets": True, "styleActiveLine": True, "indentUnit": 2, "readOnly": read_only, "extraKeys": CodeMirrorJavascript(""" { "Tab": function(cm) { var spaces = \ Array(cm.getOption("indentUnit") + 1).join(" "); cm.replaceSelection(spaces); }, "F9": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, } """) }) else: return None
def __init__(self, initial_text, editor_mode, vim_mode, help_text, *args, **kwargs): super(SandboxForm, self).__init__(*args, **kwargs) from crispy_forms.helper import FormHelper self.helper = FormHelper() self.helper.form_class = "form-horizontal" from codemirror import CodeMirrorTextarea, CodeMirrorJavascript self.fields["content"] = forms.CharField( required=False, initial=initial_text, widget=CodeMirrorTextarea( mode=editor_mode, theme="default", addon_css=( "dialog/dialog", "display/fullscreen", ), addon_js=( "search/searchcursor", "dialog/dialog", "search/search", "edit/matchbrackets", "display/fullscreen", "selection/active-line", "edit/trailingspace", ), config={ "fixedGutter": True, "autofocus": True, "matchBrackets": True, "styleActiveLine": True, "showTrailingSpace": True, "indentUnit": 2, "vimMode": vim_mode, "extraKeys": CodeMirrorJavascript(""" { "Tab": function(cm) { var spaces = \ Array(cm.getOption("indentUnit") + 1).join(" "); cm.replaceSelection(spaces); }, "F9": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); } } """) }), help_text=mark_safe( help_text + " Press Alt/Cmd+(Shift+)P to preview. " "Press F9 to toggle full screen mode.")) self.fields["vim_mode"] = forms.BooleanField( required=False, initial=vim_mode) self.helper.add_input( Submit( "preview", "Preview", accesskey="p"))
def get_codemirror_widget(language_mode, interaction_mode, config=None, addon_css=(), addon_js=(), dependencies=(), read_only=False): theme = "default" if read_only: theme += " relate-readonly" from codemirror import CodeMirrorTextarea, CodeMirrorJavascript from django.core.urlresolvers import reverse help_text = (_("Press F9 to toggle full-screen mode. ") + _("Set editor mode in <a href='%s'>user profile</a>.") % reverse("relate-user_profile")) actual_addon_css = ( "dialog/dialog", "display/fullscreen", ) + addon_css actual_addon_js = ( "search/searchcursor", "dialog/dialog", "search/search", "comment/comment", "edit/matchbrackets", "display/fullscreen", "selection/active-line", "edit/trailingspace", ) + addon_js if language_mode == "python": indent_unit = 4 else: indent_unit = 2 actual_config = { "fixedGutter": True, #"autofocus": True, "matchBrackets": True, "styleActiveLine": True, "showTrailingSpace": True, "indentUnit": indent_unit, "readOnly": read_only, "extraKeys": CodeMirrorJavascript(""" { "Ctrl-/": "toggleComment", "Tab": function(cm) { var spaces = \ Array(cm.getOption("indentUnit") + 1).join(" "); cm.replaceSelection(spaces); }, "F9": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); } } """) } if interaction_mode == "vim": actual_config["vimMode"] = True actual_addon_js += ('../keymap/vim', ) elif interaction_mode == "emacs": actual_config["keyMap"] = "emacs" actual_addon_js += ('../keymap/emacs', ) elif interaction_mode == "sublime": actual_config["keyMap"] = "sublime" actual_addon_js += ('../keymap/sublime', ) # every other interaction mode goes to default if config is not None: actual_config.update(config) return CodeMirrorTextarea(mode=language_mode, dependencies=dependencies, theme=theme, addon_css=actual_addon_css, addon_js=actual_addon_js, config=actual_config), help_text