Project

General

Profile

Feature #26071 » 0001-Generate-markup-for-uploaded-image-dropped-into-wiki.patch

Felix Gliesche, 2017-05-30 17:40

View differences:

lib/redmine/mime_type.rb
53 53
      map
54 54
    end
55 55

  
56
    # returns all full mime types for a given (top level) type
57
    def self.by_type(type)
58
      MIME_TYPES.keys.select{|m| m.start_with? "#{type}/"}
59
    end
60

  
56 61
    # returns mime type for name or nil if unknown
57 62
    def self.of(name)
58 63
      return nil unless name.present?
lib/redmine/wiki_formatting/markdown/helper.rb
35 35
              javascript_include_tag('jstoolbar/jstoolbar') +
36 36
              javascript_include_tag('jstoolbar/markdown') +
37 37
              javascript_include_tag("jstoolbar/lang/jstoolbar-#{current_language.to_s.downcase}") +
38
              javascript_tag("var wikiImageMimeTypes = #{Redmine::MimeType.by_type('image').to_json};") +
38 39
              stylesheet_link_tag('jstoolbar')
39 40
            end
40 41
            @heads_for_wiki_formatter_included = true
lib/redmine/wiki_formatting/textile/helper.rb
35 35
            content_for :header_tags do
36 36
              javascript_include_tag('jstoolbar/jstoolbar-textile.min') +
37 37
              javascript_include_tag("jstoolbar/lang/jstoolbar-#{current_language.to_s.downcase}") +
38
              javascript_tag("var wikiImageMimeTypes = #{Redmine::MimeType.by_type('image').to_json};") +
38 39
              stylesheet_link_tag('jstoolbar')
39 40
            end
40 41
            @heads_for_wiki_formatter_included = true
public/javascripts/attachments.js
58 58
        progressEventHandler: onProgress.bind(progressSpan)
59 59
      })
60 60
      .done(function(result) {
61
        addInlineAttachmentMarkup(file);
61 62
        progressSpan.progressbar( 'value', 100 ).remove();
62 63
        fileSpan.find('input.description, a').css('display', 'inline-block');
63 64
      })
......
175 176
  blockEventPropagation(e);
176 177

  
177 178
  if ($.inArray('Files', e.dataTransfer.types) > -1) {
179
    handleFileDropEvent.target = e.target;
178 180
    uploadAndAttachFiles(e.dataTransfer.files, $('input:file.filedrop').first());
179 181
  }
180 182
}
183
handleFileDropEvent.target = '';
181 184

  
182 185
function dragOverHandler(e) {
183 186
  $(this).addClass('fileover');
......
204 207
  }
205 208
}
206 209

  
210
function addInlineAttachmentMarkup(file) {
211
  // insert uploaded image inline if dropped area is currently focused textarea
212
  if($(handleFileDropEvent.target).hasClass('wiki-edit') && $.inArray(file.type, window.wikiImageMimeTypes) > -1) {
213
    var $textarea = $(handleFileDropEvent.target);
214
    var cursorPosition = $textarea.prop('selectionStart');
215
    var description = $textarea.val();
216
    var sanitizedFilename = file.name.replace(/[\/\?\%\*\:\|\"\'<>\n\r]+/, '_');
217
    var inlineFilename = encodeURIComponent(sanitizedFilename);
218
    var newLineBefore = true;
219
    var newLineAfter = true;
220
    if(cursorPosition === 0 || description.substr(cursorPosition-1,1).match(/\r|\n/)) {
221
      newLineBefore = false;
222
    }
223
    if(description.substr(cursorPosition,1).match(/\r|\n/)) {
224
      newLineAfter = false;
225
    }
226

  
227
    $textarea.val(
228
      description.substring(0, cursorPosition)
229
      + (newLineBefore ? '\n' : '')
230
      + inlineFilename
231
      + (newLineAfter ? '\n' : '')
232
      + description.substring(cursorPosition, description.length)
233
    );
234

  
235
    $textarea.prop({
236
      'selectionStart': cursorPosition + newLineBefore,
237
      'selectionEnd': cursorPosition + inlineFilename.length + newLineBefore
238
    });
239
    $textarea.closest('.jstEditor')
240
      .siblings('.jstElements')
241
      .find('.jstb_img').click();
242

  
243
    // move cursor into next line
244
    cursorPosition = $textarea.prop('selectionStart');
245
    $textarea.prop({
246
      'selectionStart': cursorPosition + 1,
247
      'selectionEnd': cursorPosition + 1
248
    });
249

  
250
  }
251
}
252

  
207 253
$(document).ready(setupFileDrop);
208 254
$(document).ready(function(){
209 255
  $("input.deleted_attachment").change(function(){
(2-2/2)