-$(function() {
- var BLOCKS = [
+(function(window, document, undefined) {
+ var Phylly = {
+ BLOCKS: [
{name: 'code', tag: 'PRE', klass: 'screen'},
{name: 'figure', tag: 'DIV', subtag: true, klass: 'figure'},
{name: 'note', tag: 'DIV', subtag: true, klass: 'note'},
- ];
- function get_contenteditable_subnode(node) {
- if (node === null) return null;
- if (node.parentNode.contentEditable === 'true') return node;
- return get_contenteditable_subnode(node.parentNode);
- }
- function get_active_block(node) {
- var main_node = get_contenteditable_subnode(node);
- if (main_node === null) return null;
- for (const block of BLOCKS) {
- if (main_node.tagName === block.tag && main_node.classList.contains(block.klass))
- return block;
- }
- return null;
- }
-
- $('div[contenteditable]').on('input', function(event) {
- if (event.originalEvent.inputType == "insertParagraph") {
+ ],
+ input_event: function(event) {
+ if (event.originalEvent.inputType != "insertParagraph") return true;
var sel = document.getSelection();
var anchorNode = sel.anchorNode;
var prev_p = sel.anchorNode.previousSibling;
title.textContent = title.textContent.slice(4);
prev_p.replaceWith(title);
}
- }
- return true;
- });
- $('div[contenteditable]').on('keyup click', update_block_style_popup);
+ 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;
- });
+ init: function() {
+ $(document).on('selectionchange', function(event) {
+ if ($('input[name=link-target].shown').length) {
+ return;
+ }
+ var sel = window.getSelection();
+ if ($(sel.anchorNode).parents('div[contenteditable]').length && sel.toString()) {
+ show_style_popup(sel);
+ } else if (style_popup) {
+ $(style_popup).hide();
+ }
+ });
+ },
+
+ bind_events: function(elem) {
+ $(elem).on('input', Phylly.input_event);
+ $(elem).on('keyup click', update_block_style_popup);
+ },
+
+ }
+ window.Phylly = Phylly;
+
+ function get_contenteditable_subnode(node) {
+ if (node === null) return null;
+ if (node.parentNode.contentEditable === 'true') return node;
+ return get_contenteditable_subnode(node.parentNode);
+ }
+ function get_active_block(node) {
+ var main_node = get_contenteditable_subnode(node);
+ if (main_node === null) return null;
+ for (const block of Phylly.BLOCKS) {
+ if (main_node.tagName === block.tag && main_node.classList.contains(block.klass))
+ return block;
+ }
+ return null;
+ }
var block_style_popup = null;
function block_style() {
}
if (block_style_popup === null) {
block_style_popup = $('<div class="block-style-popup"></div>');
- for (const block of BLOCKS) {
+ for (const block of Phylly.BLOCKS) {
var button = document.createElement('button');
button.action_block = block;
button.dataset.action = block.name;
style_popup.css('left', pos.left + window.scrollX);
style_popup.show();
};
- $(document).on('selectionchange', function(event) {
- if ($('input[name=link-target].shown').length) {
- return;
- }
- var sel = window.getSelection();
- if ($(sel.anchorNode).parents('div[contenteditable]').length && sel.toString()) {
- show_style_popup(sel);
- } else if (style_popup) {
- $(style_popup).hide();
- }
+}(window, document));
+
+$(function() {
+ Phylly.init(),
+ $('div[contenteditable]').each(function(i, elem) {Phylly.bind_events(elem)});
+ $('#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;
});
});