]> git.0d.be Git - chloro.git/blobdiff - chloro/phyll/static/js/chloro.js
misc: add image/file upload for live edit (sync with panikdb)
[chloro.git] / chloro / phyll / static / js / chloro.js
index 6b831ac2588c7f92e10609c2a5b643512a0659a7..b70fa291ea3c5410bfac518fc640c0fe7476db57 100644 (file)
         sel.addRange(range);
         return;
       }
+      if (event.originalEvent.inputType == "insertText") {
+        var main_node = get_contenteditable_subnode(sel.anchorNode);
+        if (main_node.tagName != 'PRE') {
+          var anchorNode = sel.anchorNode;
+          var offset = sel.anchorOffset;
+          var orig_text = sel.anchorNode.data;
+          var text = orig_text;
+          // typography
+          if (event.originalEvent.data === "'") {
+            text = text.slice(0, offset-1) + '’' + text.slice(offset);
+          }
+          if (text != orig_text) {
+            var new_text = document.createTextNode(text);
+            anchorNode.replaceWith(new_text);
+            sel.collapse(new_text, offset);
+          }
+        }
+        return;
+      }
       if (event.originalEvent.inputType != "insertParagraph") return true;
       if (sel.anchorNode.tagName == "DIV" && sel.anchorNode.innerHTML == "<br>") {
         // new empty div got inserted, replace it with a <p>
       $image_upload.on('change', upload_image);
       $image_upload.appendTo(document.body);
 
+      var $document_upload = $('<input type="file" nam="document" id="document-upload">');
+      $document_upload.on('change', upload_document);
+      $document_upload.appendTo(document.body);
+
+      document.execCommand('defaultParagraphSeparator', false, 'p');
       $(document).on('click', 'div.figure span.empty', function() {
         window.active_figure = this.parentNode;
         $('#image-upload').trigger('click');
         return true;
       });
+      $(document).on('click', 'div.document span.empty', function() {
+        window.active_document = this.parentNode;
+        $('#document-upload').trigger('click');
+        return true;
+      });
     },
 
     off: function() {
       $('#image-upload').remove();
+      $('#document-upload').remove();
       if (block_style_toolbar) { block_style_toolbar.hide(); }
       if (inline_style_toolbar) { inline_style_toolbar.hide(); }
       $(document).off('selectionchange');
     },
 
+    window_keypress: function(ev) {
+      if (inline_style_toolbar && inline_style_toolbar.is(':visible')) {
+        if (event.ctrlKey || event.metaKey) {
+          var key = String.fromCharCode(event.which).toLowerCase();
+          var button = inline_style_toolbar.find('[data-accel="' + key + '"]').first();
+          if (button.length) {
+            button.trigger('click');
+            ev.preventDefault();
+          }
+        }
+      }
+    },
+
     bind_events: function(elem) {
       $(elem).on('input', Phylly.input_event);
       $(elem).on('keyup click', update_block_style_toolbar);
+      $(window).on('keydown', this.window_keypress);
     },
 
     unbind_events: function(elem) {
       $(elem).off('input');
       $(elem).off('keyup click');
+      $(window).off('keydown', this.window_keypress);
     },
 
   }
     if ($(this).prop('files').length > 0) {
       var file = $(this).prop('files')[0];
       var params = new FormData();
-      params.append('image', file);
-      $.post({url: '/wiki/ajax/image/', processData: false, data: params, contentType: false}).success(function(data) {
+      params.append('upload', file);
+      $.post({url: '/ajax/upload/', processData: false, data: params, contentType: false}).success(function(data) {
         var img = document.createElement('IMG');
         img.src = data.url;
         if (data.orig_url) {
     }
   }
 
+  function upload_document() {
+    if ($(this).prop('files').length > 0) {
+      var file = $(this).prop('files')[0];
+      var params = new FormData();
+      params.append('upload', file);
+      $.post({url: '/ajax/upload/', processData: false, data: params, contentType: false}).success(function(data) {
+        var doc_link = document.createElement('A');
+        doc_link.className = 'button';
+        doc_link.textContent = 'Télécharger ' + data.filename;
+        doc_link.href = data.url;
+        $(window.active_document).empty().append(doc_link);
+      });
+    }
+  }
+
   function get_contenteditable_subnode(node) {
     if (node === null) return null;
     if (node.contentEditable === 'true') return node;  // but we shouldn't arrive at root
       sel.removeAllRanges();
       sel.addRange(range);
       update_block_style_toolbar();
-
+      return;
+    }
+    if (this.action_block.special == 'doc') {
+      action = 'insertHTML';
+      param = '<div class="document"><span class="empty"></span></div><p id="new-p"></p>';
+      document.execCommand(action, false, param);
+      current_anchor = $('#new-p')[0];
+      $(current_anchor).attr('id', null);
+      var range = document.createRange();
+      range.setStart(current_anchor, 0);
+      sel.removeAllRanges();
+      sel.addRange(range);
+      update_block_style_toolbar();
       return;
     }
     if (this.action_block.special == 'list') {
   function show_inline_style_toolbar(sel) {
     if (inline_style_toolbar === null) {
       inline_style_toolbar = $('<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">&lt;&gt;</button>' +
-                      '<button data-action="removeFormat">×</button>' +
+                      '<button data-action="italic" data-accel="i"><i>i</i></button>' +
+                      '<button data-action="bold" data-accel="b"><b>b</b></button>' +
+                      '<button data-action="code" data-accel="<">&lt;&gt;</button>' +
+                      '<button data-action="removeFormat" data-accel="m">×</button>' +
                       '<button data-action="createLink">a</button>' +
                       '<input name="link-target"/>' +
                       '</div>');