]> git.0d.be Git - chloro.git/blobdiff - chloro/phyll/static/js/chloro.js
move inline style popup after <body>
[chloro.git] / chloro / phyll / static / js / chloro.js
index c47bb0a16ec5e159a34a9d48c271799a7af65b09..b5ae3134f5042660ba6f2f37282fedacc330491d 100644 (file)
@@ -1,6 +1,12 @@
-$(function() {
-  $('div[contenteditable]').on('input', function(event) {
-    if (event.originalEvent.inputType == "insertParagraph") {
+(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'},
+    ],
+    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;
@@ -15,86 +21,114 @@ $(function() {
         title.textContent = title.textContent.slice(4);
         prev_p.replaceWith(title);
       }
-    }
-    return true;
-  });
-  $('div[contenteditable]').on('keyup', function(event) {
-    var sel = document.getSelection();
-    if ((sel.anchorNode instanceof Element && (
-            (sel.anchorOffset == 0 && sel.isCollapsed) || // first line
-            (sel.anchorNode.tagName === 'PRE'))) ||
-        (sel.anchorNode.parentNode.tagName === 'PRE')) {
-      // start of line
-      show_block_style_popup();
-    } else {
-      hide_block_style_popup();
-    }
-    return true;
-  });
+      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() {
-    var action = $(this).data('action');
-    var class_name = null;
-    if (action == 'code') {
-      action = 'pre';
-      class_name = 'screen';
-      if ($(this).hasClass('on')) {  // toggle off
-        action = 'p';
-        class_name = null;
-      }
-    }
-    document.execCommand('formatBlock', false, action);
     var sel = window.getSelection();
-    if (class_name) {
-      $(sel.anchorNode).addClass(class_name);
+    var current_anchor = sel.anchorNode;
+    if (this.classList.contains('on')) { // toggle off
+      if (this.action_block.subtag) {
+        // unwrap
+        var main_node = get_contenteditable_subnode(current_anchor);
+        $(current_anchor).detach().insertAfter(main_node);
+      } else {
+        document.execCommand('formatBlock', false, 'p');
+        current_anchor = sel.anchorNode;
+      }
+    } else {
+      action = this.action_block.subtag || this.action_block.tag;
+      if (this.action_block.subtag) {
+        // enclose current tag into a new parent;
+        var new_parent = document.createElement(this.action_block.tag);
+        new_parent.className = this.action_block.klass;
+        $(current_anchor).wrap(new_parent);
+      } else {
+        document.execCommand('formatBlock', false, this.action_block.tag);
+        sel.anchorNode.className = this.action_block.klass;
+        current_anchor = sel.anchorNode;
+      }
     }
     var range = document.createRange();
-    range.setStart(sel.anchorNode, 0);
+    range.setStart(current_anchor, 0);
     sel.removeAllRanges();
     sel.addRange(range);
+    update_block_style_popup();
   }
-  function show_block_style_popup() {
+  function update_block_style_popup() {
+    var sel = window.getSelection();
+    if (! ((sel.anchorNode instanceof Element && (sel.anchorOffset == 0 && sel.isCollapsed)) || get_active_block(sel.anchorNode))) {
+      if (block_style_popup) {
+        $(block_style_popup).hide();
+      }
+      return true;
+    }
     if (block_style_popup === null) {
-      block_style_popup = $(
-                      '<div class="style-popup">' +
-                      '<button data-action="code">code</button>' +
-                      '</div>');
+      block_style_popup = $('<div class="block-style-popup"></div>');
+      for (const block of Phylly.BLOCKS) {
+        var button = document.createElement('button');
+        button.action_block = block;
+        button.dataset.action = block.name;
+        button.textContent = block.name;
+        block_style_popup.append(button);
+      }
       block_style_popup.hide();
-      block_style_popup.insertAfter($('.actions'));
+      block_style_popup.insertAfter(document.body);
       block_style_popup.find('button').on('click', block_style);
     }
     block_style_popup.css('position', 'absolute');
-    var sel = window.getSelection();
-    var anchor = sel.anchorNode;
-    if (anchor instanceof Text) {
-      anchor = anchor.parentNode;
-    }
-    if (anchor.tagName === "PRE") {
-      block_style_popup.find('[data-action=code]').addClass('on');
+    var block = get_active_block(sel.anchorNode);
+    block_style_popup.find('button').removeClass('on');
+    if (block) {
+      block_style_popup.find('[data-action=' + block.name + ']').addClass('on');
+      block_style_popup.addClass('selected');
     } else {
-      block_style_popup.find('[data-action=code]').removeClass('on');
+      block_style_popup.removeClass('selected');
     }
+    var anchor = get_contenteditable_subnode(sel.anchorNode);
     var pos = $(anchor).offset();
     block_style_popup.css('top', pos.top - 33);
     block_style_popup.css('left', pos.left);
     block_style_popup.show();
-  }
-  function hide_block_style_popup() {
-    if (block_style_popup) {
-      $(block_style_popup).hide();
-    }
+    return true;
   }
 
   var style_popup = null;
@@ -149,16 +183,16 @@ $(function() {
 
   function show_style_popup(sel) {
     if (style_popup === null) {
-      style_popup = $('<div class="style-popup short">' +
+      style_popup = $('<div class="inline-style-popup">' +
                       '<button data-action="italic"><i>i</i></button>' +
                       '<button data-action="bold"><b>b</b></button>' +
-                      '<button data-action="code">#</button>' +
+                      '<button data-action="code">&lt;&gt;</button>' +
                       '<button data-action="removeFormat">×</button>' +
                       '<button data-action="createLink">a</button>' +
                       '<input name="link-target"/>' +
                       '</div>');
       style_popup.hide();
-      style_popup.insertAfter($('.actions'));
+      style_popup.insertAfter(document.body);
       style_popup.find('button').on('click', update_style);
       style_popup.find('[name=link-target]').on('keypress', validate_link).on('focusout', focusout_link);
     }
@@ -168,15 +202,19 @@ $(function() {
     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;
   });
 });