]> source.dussan.org Git - redmine.git/commitdiff
Generate markup for uploaded image dropped into wiki-edit textarea (#26071).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Wed, 7 Jun 2017 21:01:45 +0000 (21:01 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Wed, 7 Jun 2017 21:01:45 +0000 (21:01 +0000)
Patch by Felix Gliesche.

git-svn-id: http://svn.redmine.org/redmine/trunk@16643 e93f8b46-1217-0410-a6f0-8f06a7374b81

lib/redmine/mime_type.rb
lib/redmine/wiki_formatting/markdown/helper.rb
lib/redmine/wiki_formatting/textile/helper.rb
public/javascripts/attachments.js

index 2a3db8080974bb077254ae32180807ebcd55d5b7..63fb0015268dd1f3b79dfe8477381666a34d3930 100644 (file)
@@ -53,6 +53,11 @@ module Redmine
       map
     end
 
+    # returns all full mime types for a given (top level) type
+    def self.by_type(type)
+      MIME_TYPES.keys.select{|m| m.start_with? "#{type}/"}
+    end
+
     # returns mime type for name or nil if unknown
     def self.of(name)
       return nil unless name.present?
index f864988eaa6941cc8006c7634986c2503f8d8ff2..6524142fcbcf8b2d6ed573374626a29a38db9470 100644 (file)
@@ -35,6 +35,7 @@ module Redmine
               javascript_include_tag('jstoolbar/jstoolbar') +
               javascript_include_tag('jstoolbar/markdown') +
               javascript_include_tag("jstoolbar/lang/jstoolbar-#{current_language.to_s.downcase}") +
+              javascript_tag("var wikiImageMimeTypes = #{Redmine::MimeType.by_type('image').to_json};") +
               stylesheet_link_tag('jstoolbar')
             end
             @heads_for_wiki_formatter_included = true
index eedbe6de8a9caa9dc90daf5d6038211f8b78a7ee..53d0cfaa6889eec4ccf8d42c88836e26125e8479 100644 (file)
@@ -35,6 +35,7 @@ module Redmine
             content_for :header_tags do
               javascript_include_tag('jstoolbar/jstoolbar-textile.min') +
               javascript_include_tag("jstoolbar/lang/jstoolbar-#{current_language.to_s.downcase}") +
+              javascript_tag("var wikiImageMimeTypes = #{Redmine::MimeType.by_type('image').to_json};") +
               stylesheet_link_tag('jstoolbar')
             end
             @heads_for_wiki_formatter_included = true
index 8700aafeec04b1f2ff8735e0e9e14b3e6c5f402b..9afa73b17aad57b5ae6f6f38bd9bda52a3ce9875 100644 (file)
@@ -58,6 +58,7 @@ function ajaxUpload(file, attachmentId, fileSpan, inputEl) {
         progressEventHandler: onProgress.bind(progressSpan)
       })
       .done(function(result) {
+        addInlineAttachmentMarkup(file);
         progressSpan.progressbar( 'value', 100 ).remove();
         fileSpan.find('input.description, a').css('display', 'inline-block');
       })
@@ -175,9 +176,11 @@ function handleFileDropEvent(e) {
   blockEventPropagation(e);
 
   if ($.inArray('Files', e.dataTransfer.types) > -1) {
+    handleFileDropEvent.target = e.target;
     uploadAndAttachFiles(e.dataTransfer.files, $('input:file.filedrop').first());
   }
 }
+handleFileDropEvent.target = '';
 
 function dragOverHandler(e) {
   $(this).addClass('fileover');
@@ -204,6 +207,49 @@ function setupFileDrop() {
   }
 }
 
+function addInlineAttachmentMarkup(file) {
+  // insert uploaded image inline if dropped area is currently focused textarea
+  if($(handleFileDropEvent.target).hasClass('wiki-edit') && $.inArray(file.type, window.wikiImageMimeTypes) > -1) {
+    var $textarea = $(handleFileDropEvent.target);
+    var cursorPosition = $textarea.prop('selectionStart');
+    var description = $textarea.val();
+    var sanitizedFilename = file.name.replace(/[\/\?\%\*\:\|\"\'<>\n\r]+/, '_');
+    var inlineFilename = encodeURIComponent(sanitizedFilename);
+    var newLineBefore = true;
+    var newLineAfter = true;
+    if(cursorPosition === 0 || description.substr(cursorPosition-1,1).match(/\r|\n/)) {
+      newLineBefore = false;
+    }
+    if(description.substr(cursorPosition,1).match(/\r|\n/)) {
+      newLineAfter = false;
+    }
+
+    $textarea.val(
+      description.substring(0, cursorPosition)
+      + (newLineBefore ? '\n' : '')
+      + inlineFilename
+      + (newLineAfter ? '\n' : '')
+      + description.substring(cursorPosition, description.length)
+    );
+
+    $textarea.prop({
+      'selectionStart': cursorPosition + newLineBefore,
+      'selectionEnd': cursorPosition + inlineFilename.length + newLineBefore
+    });
+    $textarea.closest('.jstEditor')
+      .siblings('.jstElements')
+      .find('.jstb_img').click();
+
+    // move cursor into next line
+    cursorPosition = $textarea.prop('selectionStart');
+    $textarea.prop({
+      'selectionStart': cursorPosition + 1,
+      'selectionEnd': cursorPosition + 1
+    });
+
+  }
+}
+
 $(document).ready(setupFileDrop);
 $(document).ready(function(){
   $("input.deleted_attachment").change(function(){