$(document).on('page:editor', function() { var mode = $("#editor-source").data('mode'); var aceEditor = ace.edit("editor-source"); aceEditor.getSession().setMode("ace/mode/" + mode); aceEditor.setOptions({ wrap: true, maxLines: Infinity, theme: "ace/theme/github", showPrintMargin: false, fontSize: "1em" }); $('body').off('keypress', 'input').on('keypress', 'input', function(event) { if (event.keyCode == 13) { event.preventDefault(); $('input[value="Save"]').focus().click(); return false; } }); $('#site-title').keyup(function() { $('.frontmatter #title').val($(this).val()); }); // Toggles between preview and editing mode $("#see-preview").off('click').click(function(event) { var preview = $('#editor-preview'); var editor = $('pre.ace_editor'); 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).data("previewing", "false"); notification({ text: "Think, relax and do the better you can!", type: 'information', timeout: 2000 }); } 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).data("previewing", "true"); notification({ text: "This is how your post looks like.", type: 'information', timeout: 2000 }); } return false; }); //TODO: reform this // Submites any form in the page in JSON format $('form').submit(function(event) { event.preventDefault(); // Reset preview area and button to make sure it will // not serialize any form inside the preview $('#preview-area').html('').fadeOut(); $('#preview').data("previewing", "false"); $('.CodeMirror').fadeIn(); // Save editor values if (typeof editor !== 'undefined' && editor) { editor.save(); } var data = JSON.stringify($(this).serializeJSON()), button = $(this).find("input[type=submit]:focus"); $.ajax({ type: 'POST', url: window.location, data: data, headers: { 'X-Regenerate': button.data("regenerate"), 'X-Schedule': button.data("schedule"), 'X-Content-Type': button.data("type") }, dataType: 'json', encode: true, contentType: "application/json; charset=utf-8", }).done(function(data) { notification({ text: button.data("message"), type: 'success', timeout: 5000 }); }).fail(function(data) { notification({ text: 'Something went wrong.', type: 'error' }); console.log(data); }); return false; }); // Adds one more field to the current group $("body").on('click', '.add', function(event) { event.preventDefault(); defaultID = "lorem-ipsum-sin-dolor-amet"; if ($("#" + defaultID).length) { return false; } block = $(this).parent().parent(); blockType = block.data("type"); blockID = block.attr("id"); // Main add button, after all blocks if (block.is('div') && block.hasClass("frontmatter")) { block = $('.blocks'); block.append('
'); blockType = "object"; } // If the Block Type is an array if (blockType == "array") { newID = blockID + "[]"; input = blockID; input = input.replace(/\[/, '\\['); input = input.replace(/\]/, '\\]'); block.append('