-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix CLICK-to-Upload Image Cross-Wiring Issues #8987
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,25 +33,27 @@ jQuery(function() { | |
}); | ||
$('.dropzone').on('drop',function(e) { | ||
// this 'drop' listener is also reused for pages with just one form, ie. /wiki/new | ||
$D.selected = $(e.target).closest('div.comment-form-wrapper').eq(0); | ||
const closestCommentFormWrapper = e.target.closest('div.comment-form-wrapper'); // this returns null if there is no match | ||
e.preventDefault(); | ||
let params = {}; | ||
// $D.selected will look different for multi vs. single form pages, because of what $.closest returns: | ||
// multiple comments: { 0: the_closest_element } | ||
// /wiki/new: .comment-form-wrapper doesn't exist! | ||
if ($D.selected.hasOwnProperty(0)) { // eg. jQuery finds a .comment-form-wrapper somewhere on the page. | ||
params['textarea'] = $D.selected[0].querySelector('textarea').id // assign the ID of the textarea within the closest comment-form-wrapper | ||
// there are no .comment-form-wrappers on /wiki/edit or /wiki/new | ||
// these pages just have a single text-input form. | ||
if (closestCommentFormWrapper) { | ||
$D.selected = $(closestCommentFormWrapper); | ||
// assign the ID of the textarea within the closest comment-form-wrapper | ||
params['textarea'] = closestCommentFormWrapper.querySelector('textarea').id; | ||
} else { | ||
// default to #text-input | ||
// ideally there should only be one per page | ||
params['textarea'] = 'text-input' | ||
params['textarea'] = 'text-input'; | ||
} | ||
$E.initialize(params); | ||
}); | ||
$('#side-dropzone').on('drop',function(e) { | ||
e.preventDefault(); | ||
}); | ||
|
||
// these functions are also used on /wiki/edit and /wiki/new pages | ||
$('.dropzone').each(function() { | ||
$(this).fileupload({ | ||
url: "/images", | ||
|
@@ -63,6 +65,13 @@ jQuery(function() { | |
'nid':$D.nid | ||
}, | ||
start: function(e) { | ||
// 'start' function runs: | ||
// 1. when user drag-and-drops image | ||
// 2. when user clicks on upload button. | ||
// for click-upload-button scenarios, it's important to set $D.selected here, because the 'drop' listener above doesn't run in those: | ||
$D.selected = $(e.target).closest('div.comment-form-wrapper'); | ||
// the above line is redundant in drag & drop, because it's assigned in 'drop' listener too. | ||
// on /wiki/new & /wiki/edit, $D.selected will = undefined from this assignment | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a major change: assign As a reminder, this function runs on both I left a comment in the code about this, but I'm thinking it would be good down the road to not have to assign |
||
$('#imagebar .inline_image_drop').show(); | ||
$('#create_progress').show(); | ||
$('#create_uploading').show(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
$E = { | ||
initialize: function(args) { | ||
args = args || {} | ||
// title | ||
$E.title = $('#title') | ||
// textarea | ||
args['textarea'] = args['textarea'] || 'text-input' | ||
$E.textarea = $('#'+args['textarea']) | ||
$E.title = $('#title') | ||
$E.textarea.bind('input propertychange',$E.save) | ||
// preview | ||
args['preview'] = args['preview'] || 'preview' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No additions here, just rearranged stuff for greater readability. Sometime soon, I'd like to rewrite this part to use JavaScript ES6's default parameters: |
||
$E.preview = $('#'+args['preview']) | ||
$E.textarea.bind('input propertychange',$E.save) | ||
|
||
|
||
marked.setOptions({ | ||
gfm: true, | ||
tables: true, | ||
|
@@ -23,24 +26,25 @@ $E = { | |
return code; | ||
} | ||
}); | ||
|
||
}, | ||
is_editing: function() { | ||
return ($E.textarea[0].selectionStart == 0 && $E.textarea[0].selectionEnd == 0) | ||
}, | ||
|
||
refresh: function() { | ||
if($D.selected) { | ||
$E.textarea = ($D.selected).find('textarea').eq(0); | ||
$E.preview = ($D.selected).find('#preview').eq(0); | ||
$E.textarea.bind('input propertychange',$E.save); | ||
} | ||
// textarea | ||
$E.textarea = ($D.selected).find('textarea').eq(0); | ||
$E.textarea.bind('input propertychange',$E.save); | ||
// preview | ||
$E.preview = ($D.selected).find('#preview').eq(0); | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't think these lines needed to be inside of a conditional checking for |
||
|
||
// wraps currently selected text in textarea with strings a and b | ||
wrap: function(a,b,args) { | ||
var isWiki = (window.location + '').includes('wiki'); | ||
if (!isWiki) this.refresh(); | ||
// this RegEx is: /wiki/ + any char not "/" + /edit | ||
const isWikiCommentPage = (/\/wiki\/[^\/]+\/edit/).test(window.location.pathname); | ||
// we don't need to refresh $E's values if we're on a page with a single comment form, ie. /wiki/new or /wiki/edit | ||
if (window.location.pathname !== "/wiki/new" && !isWikiCommentPage && $D.selected) { | ||
this.refresh(); | ||
} | ||
var len = $E.textarea.val().length; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a pretty big change too. The Unfortunately this had the side effect of running The last note is that I'm thinking that |
||
var start = $E.textarea[0].selectionStart; | ||
var end = $E.textarea[0].selectionEnd; | ||
|
@@ -68,7 +72,6 @@ $E = { | |
image: function(src) { | ||
$E.wrap('\n![',']('+src+')\n') | ||
}, | ||
|
||
h1: function() { | ||
$E.wrap('#','') | ||
}, | ||
|
@@ -123,7 +126,6 @@ $E = { | |
toggle_preview: function (comment_id = null) { | ||
let previewBtn; | ||
let dropzone; | ||
|
||
// if the element is part of a multi-comment page, | ||
// ensure to grab the current element and not the other comment element. | ||
if (comment_id) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I rewrote this part for greater readability, using JavaScript's
closest
(as opposed to jQuery's method with the same name).JavaScript's
closest
returnsnull
, whereas jQuery's returns an object. I thought this would be more intuitive.