Commits

Takafumi Arakaki  committed 9064690

Changed edit behavior: dynamically load edit form

Why? Two reasons:

- hiding textarea does not work well with "Edit with Emacs"
- page text may have changed after the page was rendered

  • Participants
  • Parent commits a4a24be

Comments (0)

Files changed (6)

File neorg/static/neorg.coffee

       unBindKeydown arrowKeyArray[i]
 
 neorgEdit = ->
-  $("#edit-form").show()
-  $("#edit-form-textarea").focus()
+  $.ajax
+    url: "_edit_form"
+    success: (data) ->
+      $("#edit-form-wrapper").html data
+      $("#edit-form-textarea").focus()
+      return
   return false
 
+
 neorgInit = ->
   $(".neorg-gene-image-link").colorbox neorgCBSetting
 
-  eform = $("#edit-form")
-  if eform
-    $("a.page-action-edit").click neorgEdit
-
-    if eform.is(":visible")
-      $("#edit-form-textarea").focus()
+  $("a.page-action-edit").click neorgEdit
+  $("#edit-form-textarea").focus()
 
 
 # export functions as global function

File neorg/templates/edit.html

 {% extends "page.html" %}
 {%- block title_prefix %}Edit - {% endblock -%}
 {%- block title_root -%}{{ "<root page>" }}{% endblock -%}
+{% block body %}
+  <div id="edit-form-wrapper">
+    {% include "edit_form.html" %}
+  </div>
+  <div>
+    {{ page_html|safe }}
+  </div>
+{% endblock %}

File neorg/templates/edit_form.html

+<form action="{{ url_for('save', page_path=page_path) }}"
+      method="post" class="add-entry" id="edit-form">
+  <textarea name="page_text" rows="20" cols="80" id="edit-form-textarea">
+    {{- page_text -}}
+  </textarea>
+  <input type="submit" name="save" value="Save" />
+  <input type="submit" name="preview" value="Preview" />
+  <input type="submit" name="cancel" value="Cancel" />
+  <input type="submit" name="delete" value="Delete" />
+</form>

File neorg/templates/layout.html

     </div>
   <script type='text/javascript'>
     neorgInit();
-    {% if hide_edit_form is defined and hide_edit_form -%}
-    $("#edit-form").hide();
-    {%- endif %}
   </script>
   </body>
 </html>

File neorg/templates/page.html

     Organize your experiments and find out more!
 {%- endblock %}
 {% block body %}
-  {% if page_text is defined -%}
-  <form action="{{ url_for('save', page_path=page_path) }}"
-        method="post" class="add-entry" id="edit-form">
-    <textarea name="page_text" rows="20" cols="80" id="edit-form-textarea">
-      {{- page_text -}}
-    </textarea>
-    <input type="submit" name="save" value="Save" />
-    <input type="submit" name="preview" value="Preview" />
-    <input type="submit" name="cancel" value="Cancel" />
-    <input type="submit" name="delete" value="Delete" />
-  </form>
-  {%- endif %}
+  <div id="edit-form-wrapper"></div>
   <div>
     {{ page_html|safe }}
   </div>

File neorg/web.py

 
 @app.route('/_edit', defaults={'page_path': ''})
 @app.route('/<path:page_path>/_edit')
-def edit(page_path):
+def edit(page_path, template="edit.html"):
     (page_text, page_html) = get_page_text_and_html(page_path)
-    return render_template("edit.html",
+    return render_template(template,
                            title=path_as_title(page_path),
                            page_path=page_path,
                            page_html=page_html,
                            page_text=page_text if page_text else '')
 
 
+@app.route('/_edit_form', defaults={'page_path': ''})
+@app.route('/<path:page_path>/_edit_form')
+def edit_form(page_path):
+    return edit(page_path, template="edit_form.html")
+
+
 @app.route('/', defaults={'page_path': ''})
 @app.route('/<path:page_path>/')
 def page(page_path):
         return render_template("page.html",
                                title=path_as_title(page_path),
                                page_path=page_path,
-                               page_html=page_html,
-                               page_text=page_text if page_text else '',
-                               hide_edit_form=True)
+                               page_html=page_html)
     else:
         generated = gene_from_template(page_path)
         if generated: