$(document).on('page:editor', function() { var container = $('.editor'); var preview = $('#editor-preview'); var editor = $('#editor-source'); if (container.hasClass('complete')) { // Change title field when editing the header $('#content').on('keyup', '#site-title', function() { $('.frontmatter #title').val($(this).val()); }); } if (!container.hasClass('frontmatter-only')) { // Setup ace editor var mode = $("#editor-source").data('mode'); var textarea = $('textarea[name="content"]').hide(); var aceEditor = ace.edit('editor-source'); aceEditor.getSession().setMode("ace/mode/" + mode); aceEditor.getSession().setValue(textarea.val()); aceEditor.getSession().on('change', function() { textarea.val(aceEditor.getSession().getValue()); }); aceEditor.setOptions({ wrap: true, maxLines: Infinity, theme: "ace/theme/github", showPrintMargin: false, fontSize: "1em", minLines: 20 }); $('#content').on('click', '#see-source', function(event) { event.preventDefault(); preview.hide(); editor.fadeIn(); $(this).addClass('active'); $("#see-preview").removeClass('active'); $("#see-preview").data("previewing", "false"); }) // Toggles between preview and editing mode $('#content').on('click', '#see-preview', function(event) { event.preventDefault(); // If it currently in the preview mode, hide the preview // and show the editor if ($(this).data("previewing") == "true") { preview.hide(); editor.fadeIn(); $(this).removeClass('active'); $("#see-source").addClass('active'); $(this).data("previewing", "false"); } else { // If it's in editing mode, convert the markdown to html // and show it var converter = new showdown.Converter(), text = aceEditor.getValue(), html = converter.makeHtml(text); // Hide the editor and show the preview editor.hide(); preview.html(html).fadeIn(); $(this).addClass('active'); $("#see-source").removeClass('active'); $(this).data("previewing", "true"); } return false; }); } $('#content').on('keypress', 'input', function(event) { if (event.keyCode == 13) { event.preventDefault(); $('input[value="Save"]').focus().click(); return false; } }); var submitActor = null; $('#content').on('click', 'form input[type=submit]', function(event) { submitActor = this; }); // Submites any form in the page in JSON format $('#content').on('submit', 'form', function(event) { event.preventDefault(); if (!container.hasClass('frontmatter-only')) { // Reset preview area and button to make sure it will // not serialize any form inside the preview preview.html('').fadeOut(); $("#see-preview").data("previewing", "false"); editor.fadeIn(); } var button = $(submitActor); var data = { content: $(this).serializeJSON(), contentType: button.data("type"), schedule: button.data("schedule"), regenerate: button.data("regenerate") } var request = new XMLHttpRequest(); request.open("POST", window.location); request.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); request.send(JSON.stringify(data)); request.onreadystatechange = function() { if (request.readyState == 4) { var response = JSON.parse(request.responseText), type = "success", timeout = 5000; if (request.status == 200) { response.message = button.data("message"); } if (request.status != 200) { type = "error"; timeout = false; } notification({ text: response.message, type: type, timeout: timeout }); } } return false; }); // Adds one more field to the current group $("#content").on('click', '.add', function(event) { event.preventDefault(); defaultID = "lorem-ipsum-sin-dolor-amet"; // Remove if there is an incomplete new item newItem = $("#" + defaultID); if (newItem.length) { newItem.remove(); } block = $(this).parent().parent(); blockType = block.data("type"); blockID = block.attr("id"); // If the Block Type is an array if (blockType == "array") { newID = blockID + "[]"; input = blockID; input = input.replace(/\[/, '\\['); input = input.replace(/\]/, '\\]'); block.append('