{% extends "templates/base.html" %}
{% block extra-headers %}
  {% ifequal js-from ":cdnjs" %}
    <script src="https://cdnjs.cloudflare.com/ajax/libs/simplemde/1.11.2/simplemde.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/simplemde/1.11.2/simplemde.min.css" rel="stylesheet" type="text/css" />
  {% else %}
    {% style "/vendor/simplemde/dist/simplemde.min.css" %}
    {% script "/vendor/simplemde/dist/simplemde.min.js" %}
  {% endifequal %}
{% endblock %}

{% block content %}
        <div id="content" class="edit">
            <form id="edit-form" action="{{servlet-context}}/edit" method="POST">
                {% csrf-field %}
                <input type="hidden" name="page" value="{{page}}"/>
                <textarea name="src" id="src" rows="25" cols="80">{{content}}</textarea>
                <p class="widget">
                    <label for="summary">{% i18n what-changed-prompt %}</label>
                    <input name="summary" id="summary" type="text"
                        value="{%if exists%}{%else%}New file {{page}}{%endif%}" required/>
                </p>
                <p class="widget">
                    <label for="submit-button">{% i18n save-prompt %}</label>
                    <input name="submit-button" id="submit-button" type="submit"
                           class="action" value="{% i18n save-label %}"/>
                </p>
            </form>
        </div>
        <script>
          /* intercept control-S (or on Mac meta-S) and submit the form. Saves much annoyance. */
          document.addEventListener("keydown", function(e) {
            if (e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
              e.preventDefault();
              document.getElementById('edit-form').submit();
            }
          }, false);
          // initialise the editor
          var simplemde = new SimpleMDE({
            autosave: {
              /* there's an unlovely bug that if you have two Smeagol instances on the same host,
               * and you edit their files in the same browser, the autosave gets confused as to
               * which site the autosave buffer belongs to. Better to disable in that case? */
              enabled: true,
              uniqueId: "Smeagol-{{page}}",
              delay: 1000,
            },
            indentWithTabs: true,
            insertTexts: {
              horizontalRule: ["", "\n\n-----\n\n"],
              image: ["![](http://", ")"],
              link: ["[", "](http://)"],
              table: ["", "\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Text     | Text      | Text     |\n\n"],
            },
            showIcons: ["code"], //, "table"], - sadly, markdown-clj does not support tables
	          spellChecker: true,
	          status: ["autosave", "lines", "words", "cursor"]
          });
        </script>
{% endblock %}
