Skip to content

Commit

Permalink
basic integration of codemirror
Browse files Browse the repository at this point in the history
  • Loading branch information
mgaitan committed Sep 19, 2014
1 parent e2ab8ee commit 71139bc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 69 deletions.
12 changes: 11 additions & 1 deletion waliki/forms.py
Expand Up @@ -6,6 +6,16 @@ class PageForm(forms.ModelForm):
raw = forms.CharField(label="", widget=forms.Textarea)
message = forms.CharField(max_length=200, required=False)

class Media:
js = ('codemirror/lib/codemirror.js',
'codemirror/mode/markdown/markdown.js',
'codemirror/mode/rst/rst.js',
'js/waliki.js'
)
css = {
'all': ('codemirror/lib/codemirror.css',)
}

def __init__(self, *args, **kwargs):
is_hidden = kwargs.pop('is_hidden', None)
super(PageForm, self).__init__(*args, **kwargs)
Expand All @@ -24,5 +34,5 @@ def save(self, commit=True):

class Meta:
model = Page
fields = ['title', 'raw', 'message']
fields = ['title', 'markup', 'raw', 'message']

4 changes: 2 additions & 2 deletions waliki/static/css/waliki.css
Expand Up @@ -13,7 +13,7 @@ textarea#body {
font-size: 13px;
}

//diff
// diff

span.ins, span.del {
padding: 3px;
Expand Down Expand Up @@ -82,6 +82,6 @@ div.important, div.note, div.hint {
}


div#editor {
div#editor, div#preview {
padding: 1.2em;
}
1 change: 1 addition & 0 deletions waliki/static/js/waliki.js
@@ -0,0 +1 @@
CodeMirror.fromTextArea(document.getElementById("id_raw"));
2 changes: 0 additions & 2 deletions waliki/templates/base.html
Expand Up @@ -13,9 +13,7 @@

{% block style_base %}
<link rel="stylesheet" type="text/css" href="{% static 'css/bootstrap.min.css' %}">
<link rel="stylesheet" type="text/css" href="{% static 'css/bootstrap-theme.min.css'%}">
<link rel="stylesheet" type="text/css" href="{% static 'css/pygments.css'%}">
<link rel="stylesheet" type="text/css" href="{% static 'codemirror/lib/codemirror.css'%}">
<link rel="stylesheet" type="text/css" href="{% static 'css/waliki.css'%}">
{% block extra_style %}{% endblock %}
{% endblock %}
Expand Down
85 changes: 21 additions & 64 deletions waliki/templates/waliki/edit.html
Expand Up @@ -2,6 +2,12 @@
{% load bootstrap_tags %}
{% block title %}<h1>Editing: {{ page.title }}</h1>{% endblock title %}

{% block extra_head %}
{{ form.media.css }}
{% endblock %}



{% block content %}

<div class="row">
Expand All @@ -10,6 +16,20 @@
<ul class="nav nav-tabs">
<li class="active"><a href="#editor" data-toggle="tab">Editor</a></li>
<li><a href="#preview" data-toggle="tab" id="previewlink">Preview</a></li>
<li class="pull-right">
<div class="btn-group btn-group-sm">
<a href="#" class="btn btn-default">reStructuredText</a>
<button class="btn dropdown-toggle btn-default" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><a id="#" >Markdown</a></li>
</ul>
</div>

</li>


</ul>
<div class="tab-content">
<div class="tab-pane active" id="editor">
Expand Down Expand Up @@ -74,70 +94,7 @@
$('#previewlink').click();
});

{% comment %}
$('#btn-attachments').click(function(e) {
e.preventDefault();
var url = $(this).attr('href');
$.get(url, function(data){
$('#attachments-body').html(data);
$('#attachments').modal('show');
});
});


// Configurable Codemirror integration

function isFullScreen(cm) {
return /\bCodeMirror-fullscreen\b/.test(cm.getWrapperElement().className);
}
function winHeight() {
return window.innerHeight || (document.documentElement || document.body).clientHeight;
}
function setFullScreen(cm, full) {
var wrap = cm.getWrapperElement();
if (full) {
wrap.className += " CodeMirror-fullscreen";
wrap.style.height = winHeight() + "px";
document.documentElement.style.overflow = "hidden";
} else {
wrap.className = wrap.className.replace(" CodeMirror-fullscreen", "");
wrap.style.height = "";
document.documentElement.style.overflow = "";
}
cm.refresh();
}

CodeMirror.on(window, "resize", function() {
var showing = document.body.getElementsByClassName("CodeMirror-fullscreen")[0];
if (!showing) return;
showing.CodeMirror.getWrapperElement().style.height = winHeight() + "px";
});


var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
lineNumbers: true,
tabMode: "indent",
extraKeys: {
"F11": function(cm) {
setFullScreen(cm, !isFullScreen(cm));
},
"Esc": function(cm) {
if (isFullScreen(cm)) setFullScreen(cm, false);
}
}
});

var theme = '{{ config.THEME }}';
editor.setOption("theme", theme);

var markLang = '{{ page.markup }}';
if (markLang == 'reStructuredText'){
editor.setOption("mode", 'rst');
}
else{
editor.setOption("mode", 'markdown');
}
{% endcomment %}
</script>
{{ form.media.js }}

{% endblock extra_script %}

0 comments on commit 71139bc

Please sign in to comment.