Skip to content

Commit

Permalink
extract code into editorHelper.js 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 f7e9328 commit 2334234
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 25 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
16 changes: 4 additions & 12 deletions app/assets/javascripts/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,10 @@ $(function() {
// on pages with multiple comments, $D.selected needs to be accurate so that rich-text changes (bold, italic, etc.) go into the right comment form
// however, the editor is also used on pages with JUST ONE form, and no other comments, eg. /wiki/new & /wiki/edit, so this code needs to be reusable for that context
$('.rich-text-button').on('click', function(e) {
const closestCommentFormWrapper = e.target.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) {
$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';
const params = getEditorParams(e.target); // defined in editorHelper.js
// assign dSelected
if (params.hasOwnProperty('dSelected')) {
$D.selected = params['dSelected'];
}
$E.initialize(params);
const action = e.currentTarget.dataset.action // 'bold', 'italic', etc.
Expand Down

0 comments on commit 2334234

Please sign in to comment.