Skip to content

Commit

Permalink
extract code to editorHelper for DRY publiclab#8478
Browse files Browse the repository at this point in the history
  • Loading branch information
noi5e committed Jan 14, 2021
1 parent 5ea73ec commit f637e46
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
16 changes: 3 additions & 13 deletions app/assets/javascripts/dragdrop.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,10 @@ jQuery(function() {
$('#side-dropzone').removeClass('hover');
});
$('.dropzone').on('drop',function(e) {
// this 'drop' listener is also reused for pages with just one form, ie. /wiki/new
const closestCommentFormWrapper = e.target.closest('div.comment-form-wrapper'); // this returns null if there is no match
const params = getEditorParams(e.target); // defined in editorHelper.js
e.preventDefault();
let params = {};
// 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';
if (params.hasOwnProperty('dSelected')) {
$D.selected = params['dSelected'];
}
$E.initialize(params);
});
Expand Down
17 changes: 17 additions & 0 deletions app/assets/javascripts/editorHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// used in editor.js & dragdrop.js
const getEditorParams = (targetDiv) => {
const closestCommentFormWrapper = targetDiv.closest('div.comment-form-wrapper'); // this returns null if there is no match
let params = {};
// there are no .comment-form-wrappers on /wiki/edit or /wiki/new
// these pages just have a single text-input form.
if (closestCommentFormWrapper) {
params['dSelected'] = $(closestCommentFormWrapper);
// assign the ID of the textarea within the closest comment-form-wrapper
params['textarea'] = closestCommentFormWrapper.querySelector('textarea').id;
} else {
// default to #text-input
// #text-input ID should be unique, and the only comment form on /wiki/new & /wiki/edit
params['textarea'] = 'text-input';
}
return params;
};

0 comments on commit f637e46

Please sign in to comment.