$(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" }); $('#content').on('click', '#see-source', function(event) { event.preventDefault(); preview.hide(); editor.fadeIn(); $("#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).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).data("previewing", "true"); } return false; }); } $('#content').on('keypress', 'input', function(event) { if (event.keyCode == 13) { event.preventDefault(); $('input[value="Save"]').focus().click(); return false; } }); // 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 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 $("#content").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('
'); } if (blockType == "object" && !block.hasClass("frontmatter")) { block.append('
'); blockType = "object"; } // If the Block is an object if (blockType == "object") { newItem = $("#" + defaultID); newItem.html(''); field = $("#name-" + defaultID); // Show a notification with some information for newbies if (!document.cookie.replace(/(?:(?:^|.*;\s*)placeholdertip\s*\=\s*([^;]*).*$)|^.*$/, "$1")) { var date = new Date(); date.setDate(date.getDate() + 365); document.cookie = 'placeholdertip=true; expires=' + date.toUTCString + '; path=/'; notification({ text: 'Write the field name and then press enter. If you want to create an array or an object, end the name with ":array" or ":object".', type: 'information' }); } $(field).keypress(function(event) { console.log('got it'); // When you press enter within the new name field: if (event.which == 13) { event.preventDefault(); // This var should have a value of the type "name[:array, :object]" value = field.val(); if (value == "") { newItem.remove(); return false; } elements = value.split(":") if (elements.length > 2) { notification({ text: "Invalid syntax. It must be 'name[:type]'.", type: 'error' }); return false; } if (elements.length == 2 && elements[1] != "array" && elements[1] != "object") { notification({ text: "Only arrays and objects are allowed.", type: 'error' }); return false; } field.remove(); if (typeof blockID === "undefined") { blockID = elements[0]; } else { blockID = blockID + '[' + elements[0] + ']'; } if (elements.length == 1) { newItem.attr('id', 'block-' + blockID); newItem.append('
'); newItem.prepend(' '); } else { type = ""; if (elements[1] == "array") { type = "array"; } else { type = "object" } template = "

${elements[0]}

" template = template.replace("${blockID}", blockID); template = template.replace("${elements[0]}", elements[0]); template = template.replace("${type}", type); newItem.after(template); newItem.remove(); console.log('"' + blockID + '" block of type "' + type + '" added.'); } return false; } }); } return false; }); $("#content").on('click', '.delete', function(event) { event.preventDefault(); button = $(this); name = button.parent().parent().attr("for") || button.parent().parent().attr("id") || button.parent().parent().parent().attr("id"); name = name.replace(/\[/, '\\['); name = name.replace(/\]/, '\\]'); console.log(name) $('label[for="' + name + '"]').fadeOut().remove(); $('#' + name).fadeOut().remove(); return false; }); });