$(function() {
$('div[contenteditable]').on('input', function(event) {
if (event.originalEvent.inputType == "insertParagraph") {
var sel = document.getSelection();
var anchorNode = sel.anchorNode;
var prev_p = sel.anchorNode.previousSibling;
if (prev_p.tagName != 'P') {
prev_p = $(prev_p).parents('p')[0];
}
var title_match = prev_p.innerText.match(/^(h[1-6]). /);
if (title_match) {
var title = document.createElement(title_match[1]);
title.innerHTML = prev_p.innerHTML;
title.textContent = title.textContent.slice(4);
prev_p.replaceWith(title);
}
}
return true;
});
$('#save').on('click', function() {
var text = $('div[contenteditable]')[0].innerHTML;
var csrf = $('[name=csrfmiddlewaretoken]').val();
$.post('api-save/',
{ text: text, csrfmiddlewaretoken: csrf}
).fail(function() {
$('#save').css('background', 'red');
});
return false;
});
var style_popup = null;
function update_style() {
var action = $(this).data('action');
var param = null;
if (action == 'code') {
action = 'insertHTML';
param = $('', {text: window.getSelection().toString()})[0].outerHTML;
}
document.execCommand(action, false, param);
}
function show_style_popup(sel) {
if (style_popup === null) {
style_popup = $('