Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7011 from erbunao/drag-and-drop_markdown
Implements drag and drop upload of image in every markdown-area
- Loading branch information
Showing
30 changed files
with
363 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
formatLink = (str) -> | ||
"![" + str.alt + "](" + str.url + ")" | ||
|
||
$(document).ready -> | ||
alertClass = "alert alert-danger alert-dismissable div-dropzone-alert" | ||
alertAttr = "class=\"close\" data-dismiss=\"alert\"" + "aria-hidden=\"true\"" | ||
divHover = "<div class=\"div-dropzone-hover\"></div>" | ||
divSpinner = "<div class=\"div-dropzone-spinner\"></div>" | ||
divAlert = "<div class=\"" + alertClass + "\"></div>" | ||
iconPicture = "<i class=\"icon-picture div-dropzone-icon\"></i>" | ||
iconSpinner = "<i class=\"icon-spinner icon-spin div-dropzone-icon\"></i>" | ||
btnAlert = "<button type=\"button\"" + alertAttr + ">×</button>" | ||
project_image_path_upload = window.project_image_path_upload or null | ||
|
||
$("textarea.markdown-area").wrap "<div class=\"div-dropzone\"></div>" | ||
|
||
$(".div-dropzone").parent().addClass "div-dropzone-wrapper" | ||
|
||
$(".div-dropzone").append divHover | ||
$(".div-dropzone-hover").append iconPicture | ||
$(".div-dropzone").append divSpinner | ||
$(".div-dropzone-spinner").append iconSpinner | ||
|
||
|
||
dropzone = $(".div-dropzone").dropzone( | ||
url: project_image_path_upload | ||
dictDefaultMessage: "" | ||
clickable: true | ||
paramName: "markdown_img" | ||
maxFilesize: 10 | ||
uploadMultiple: false | ||
acceptedFiles: "image/jpg,image/jpeg,image/gif,image/png" | ||
headers: | ||
"X-CSRF-Token": $("meta[name=\"csrf-token\"]").attr("content") | ||
|
||
previewContainer: false | ||
|
||
processing: -> | ||
$(".div-dropzone-alert").alert "close" | ||
|
||
dragover: -> | ||
$(".div-dropzone > textarea").addClass "div-dropzone-focus" | ||
$(".div-dropzone-hover").css "opacity", 0.7 | ||
return | ||
|
||
dragleave: -> | ||
$(".div-dropzone > textarea").removeClass "div-dropzone-focus" | ||
$(".div-dropzone-hover").css "opacity", 0 | ||
return | ||
|
||
drop: -> | ||
$(".div-dropzone > textarea").removeClass "div-dropzone-focus" | ||
$(".div-dropzone-hover").css "opacity", 0 | ||
$(".div-dropzone > textarea").focus() | ||
return | ||
|
||
success: (header, response) -> | ||
child = $(dropzone[0]).children("textarea") | ||
$(child).val $(child).val() + formatLink(response.link) + "\n" | ||
return | ||
|
||
error: (temp, errorMessage) -> | ||
checkIfMsgExists = $(".error-alert").children().length | ||
if checkIfMsgExists is 0 | ||
$(".error-alert").append divAlert | ||
$(".div-dropzone-alert").append btnAlert + errorMessage | ||
return | ||
|
||
sending: -> | ||
$(".div-dropzone-spinner").css "opacity", 0.7 | ||
return | ||
|
||
complete: -> | ||
$(".dz-preview").remove() | ||
$(".markdown-area").trigger "input" | ||
$(".div-dropzone-spinner").css "opacity", 0 | ||
return | ||
) | ||
|
||
$(".markdown-selector").click (e) -> | ||
e.preventDefault() | ||
$(".div-dropzone").click() | ||
return | ||
|
||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
.div-dropzone-wrapper { | ||
.div-dropzone { | ||
position: relative; | ||
padding: 0; | ||
border: 0; | ||
margin-bottom: 5px; | ||
|
||
.div-dropzone-focus { | ||
border-color: #66afe9 !important; | ||
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(102, 175, 233, 0.6) !important; | ||
outline: 0 !important; | ||
} | ||
|
||
.div-dropzone-hover { | ||
position: absolute; | ||
top: 50%; | ||
left: 50%; | ||
margin-top: -0.5em; | ||
margin-left: -0.6em; | ||
opacity: 0; | ||
font-size: 50px; | ||
transition: opacity 200ms ease-in-out; | ||
} | ||
|
||
.div-dropzone-spinner { | ||
position: absolute; | ||
top: 100%; | ||
left: 100%; | ||
margin-top: -1.1em; | ||
margin-left: -1.1em; | ||
opacity: 0; | ||
font-size: 30px; | ||
transition: opacity 200ms ease-in-out; | ||
} | ||
|
||
.div-dropzone-icon { | ||
display: block; | ||
text-align: center; | ||
font-size: inherit; | ||
} | ||
|
||
.dz-preview { | ||
display: none; | ||
} | ||
} | ||
|
||
.hint { | ||
float: left; | ||
padding: 0; | ||
margin: 0; | ||
} | ||
} | ||
|
||
.div-dropzone-alert { | ||
margin-top: 5px; | ||
margin-bottom: 0; | ||
transition: opacity 200ms ease-in-out; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,3 @@ def download | |
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# encoding: utf-8 | ||
class FileUploader < CarrierWave::Uploader::Base | ||
storage :file | ||
|
||
def initialize(base_dir, path = '', allowed_extensions = nil) | ||
@base_dir = base_dir | ||
@path = path | ||
@allowed_extensions = allowed_extensions | ||
end | ||
|
||
def base_dir | ||
@base_dir | ||
end | ||
|
||
def store_dir | ||
File.join(@base_dir, @path) | ||
end | ||
|
||
def cache_dir | ||
File.join(@base_dir, 'tmp', @path) | ||
end | ||
|
||
def extension_white_list | ||
@allowed_extensions | ||
end | ||
|
||
def store!(file) | ||
file.original_filename = self.class.generate_filename(file) | ||
super | ||
end | ||
|
||
def self.generate_filename(file) | ||
original_filename = File.basename(file.original_filename, '.*') | ||
extension = File.extname(file.original_filename) | ||
new_filename = Digest::MD5.hexdigest(original_filename) + extension | ||
end | ||
|
||
def self.generate_dir | ||
SecureRandom.hex(5) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ | |
|
||
.container | ||
.content= yield | ||
= yield :embedded_scripts |
Oops, something went wrong.