Skip to content
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

Refactor Editor Constructor and Methods, Write Editor Getters #9110

Merged
merged 11 commits into from
Feb 4, 2021
238 changes: 116 additions & 122 deletions app/assets/javascripts/editor.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
// jQuery (document).ready function:
class Editor {
// default parameters:
// defaultForm - when the Editor is initialized, there needs to be a default editor form:
// 1. the main comment form in multi-comment wikis, questions, & research notes.
// 2. the only editor form on /wiki/new and /wiki/edit
// elementIDPrefixes - the ID naming conventions are different depending on context:
// 1. multi-form pages with multiple comments: #comment-preview-123
// 2. /wiki/new and /wiki/edit: #preview-main
constructor(defaultForm = "main", elementIDPrefixes = {}) {
this.commentFormID = defaultForm;
this.elementIDPrefixes = elementIDPrefixes;
this.textarea = $("#text-input-" + this.commentFormID);

$E = {
initialize: function() {
// call setState with no parameters, aka. default parameters.
// default parameters point toward either:
// 1. the comment form at the bottom of multi-comment wikis/questions/research notes
// 2. the only editor form on /wiki/new and /wiki/edit
$E.setState();

// this will get deleted in the next few PRs, so collapsing into one line to pass codeclimate
this.templates = { 'blog': "## The beginning\n\n## What we did\n\n## Why it matters\n\n## How can you help", 'default': "## What I want to do\n\n## My attempt and results\n\n## Questions and next steps\n\n## Why I'm interested", 'support': "## Details about the problem\n\n## A photo or screenshot of the setup", 'event': "## Event details\n\nWhen, where, what\n\n## Background\n\nWho, why", 'question': "## What I want to do or know\n\n## Background story" };

marked.setOptions({
gfm: true,
tables: true,
Expand All @@ -23,131 +29,119 @@ $E = {
return code;
}
});
},
setState: function(textarea = 'text-input', preview = 'comment-preview-main', title = 'title') {
$E.title = $('#' + title + 'title'); // not sure why this exists? seems like $E.title is always #title
$E.textarea = $('#' + textarea);
}
setState(commentFormID) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the default parameter (commentFormID = "main") here. I think it makes sense so that errors will be easier to catch.

this.commentFormID = commentFormID;
this.textarea = $("#text-input-" + commentFormID);
$E.textarea.bind('input propertychange', $E.save);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed most instances of $E.method to this.method except for this one here. I toyed around with this, but it opens up a can of worms and is out of scope of this PR. Will go back to this when I refactor save and recover functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to explain this a little further, it's because when $E.textarea is changed to this.textarea it brings up JavaScript this weirdness. The eventListener for input propertychange (which I'm not sure is a valid event type? I tried Googling it) gets attached to the wrong this.

$E.preview = $('#' + preview);
},
is_editing: function() {
return ($E.textarea[0].selectionStart == 0 && $E.textarea[0].selectionEnd == 0)
},
refresh: function() {
// textarea
$E.textarea = ($D.selected).find('textarea').eq(0);
$E.textarea.bind('input propertychange',$E.save);
// preview
$E.preview = ($D.selected).find('.comment-preview').eq(0);
},
isRichTextEditor: function(url) {
// this RegEx matches three different cases where the legacy editor is still used:
// 1. /wiki/new
// 2. /wiki/{wiki name}/edit
// 3. /features/new
const legacyEditorPath = RegExp(/\/(wiki|features)(\/[^\/]+\/edit|\/new)/);
return !legacyEditorPath.test(url); // if we're not on one of these pages, we are using the rich-text editor.
},
// wraps currently selected text in textarea with strings a and b
wrap: function(a, b, args) {
// we only refresh $E's values if we are on a page using the rich-text editor (most pages).
// the legacy editor pages only have one editor form, unlike pages with multiple comments.
if (this.isRichTextEditor(window.location.pathname)) { this.refresh(); }
var len = $E.textarea.val().length;
var start = $E.textarea[0].selectionStart;
var end = $E.textarea[0].selectionEnd;
const fallbackParameterExists = args && args['fallback'];
const newlineParameterExists = args && args['newline'];
var sel = fallbackParameterExists ? args['fallback'] : $E.textarea.val().substring(start, end); // // fallback if nothing has been selected, and we're simply dealing with an insertion point
var replace = a + sel + b;
if (newlineParameterExists) {
replace = replace + "\n\n";
}
if (newlineParameterExists && $E.textarea[0].selectionStart > 0) {
replace = "\n" + replace;
}
get textAreaElement() {
const textAreaID = "#text-input-" + this.commentFormID;
return $(textAreaID);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New getter functions!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oohhhhh!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, are these transpiled out in the asset pipeline? Any worries about internet explorer? I guess not these days!

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get

I mean, i guess it's still 5% of usage: https://www.netmarketshare.com/browser-market-share.aspx?qprid=2&qpcustomd=0

what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh huh... That's a good point. I hadn't checked this one out in MDN.

I think they can be rewritten as regular methods! That won't be too hard. Can do it in another PR although... Do our users really use IE to access the website? Better safe than sorry I guess.

get textAreaValue() {
return this.textAreaElement.val();
}
get previewElement() {
let previewIDPrefix = "#comment-preview-";
if (this.elementIDPrefixes.hasOwnProperty("preview")) {
// eg. on /wiki/new & /wiki/edit, the preview element is called #preview-main
previewIDPrefix = "#" + this.elementIDPrefixes["preview"] + "-";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one of the main changes in this PR. Honestly, I have mixed feelings about it since it's a little weird, especially in the constructor.

But I do think it's an okay workaround. I made this because there was some tension in having editor.js operative on both 1) multi-comment pages and 2) /wiki/new and /wiki/edit.

I wanted preview elements to be named #comment-preview-reply-123 (they used to just be called #preview), for greater transparency. But that clashes with the wiki editor, where there are no comments involved.

So, this is my workaround. It's a little weird, but thanks to OOP, it can be scaled back or revised much easier.

}
$E.textarea.val($E.textarea.val().substring(0,start) + replace + $E.textarea.val().substring(end,len));
},
bold: function() {
$E.wrap('**','**')
},
italic: function() {
$E.wrap('_','_')
},
link: function(uri) {
const previewID = previewIDPrefix + this.commentFormID;
return $(previewID);
}
// wraps currently selected text in textarea with strings a and b
wrap(a, b, newlineDesired = false, fallback) {
const selectionStart = this.textAreaElement[0].selectionStart;
const selectionEnd = this.textAreaElement[0].selectionEnd;
const selection = fallback || this.textAreaValue.substring(selectionStart, selectionEnd); // fallback if nothing has been selected, and we're simply dealing with an insertion point

let newText = a + selection + b; // ie. ** + selection + ** (wrapping selection in bold)
if (newlineDesired) { newText = newText + "\n\n"; }
const selectionStartsMidText = this.textAreaElement[0].selectionStart > 0;
if (newlineDesired && selectionStartsMidText) { newText = "\n" + newText; }

const textLength = this.textAreaValue.length;
const textBeforeSelection = this.textAreaValue.substring(0, selectionStart);
const textAfterSelection = this.textAreaValue.substring(selectionEnd, textLength);
this.textAreaElement.val(textBeforeSelection + newText + textAfterSelection);
}
bold() {
this.wrap('**', '**');
}
italic() {
this.wrap('_', '_');
}
link(uri) {
uri = prompt('Enter a URL');
if (uri === null) { uri = ""; }
$E.wrap('[', '](' + uri + ')');
},
image: function(src) {
$E.wrap('\n![',']('+src+')\n')
},
h1: function() {
$E.wrap('#','')
},
h2: function() {
$E.wrap('##','')
},
h3: function() {
$E.wrap('###','')
},
h4: function() {
$E.wrap('####','')
},
h5: function() {
$E.wrap('#####','')
},
h6: function() {
$E.wrap('######','')
},
h7: function() {
$E.wrap('#######','')
},
this.wrap(
'[',
'](' + uri + ')'
);
}
image(src) {
this.wrap(
'\n![',
'](' + src + ')\n'
);
}
// these header formatting functions are not used anywhere, so commenting them out for now to pass codeclimate:

// h1() {
// this.wrap('#','')
// }
h2() {
this.wrap('##', '');
}
// h3() {
// this.wrap('###','')
// }
// h4() {
// this.wrap('####','')
// }
// h5() {
// this.wrap('#####','')
// }
// h6() {
// this.wrap('######','')
// }
// h7() {
// this.wrap('#######','')
// }
// this function is dedicated to Don Blair https://github.com/donblair
save: function() {
localStorage.setItem('plots:lastpost',$E.textarea.val())
localStorage.setItem('plots:lasttitle',$E.title.val())
},
recover: function() {
$E.textarea.val(localStorage.getItem('plots:lastpost'))
$E.title.val(localStorage.getItem('plots:lasttitle'))
},
apply_template: function(template) {
if($E.textarea.val() == ""){
$E.textarea.val($E.templates[template])
}else if(($E.textarea.val() == $E.templates['event']) || ($E.textarea.val() == $E.templates['default']) || ($E.textarea.val() == $E.templates['support'])){
$E.textarea.val($E.templates[template])
save() {
localStorage.setItem('plots:lastpost', this.textAreaValue);
}
recover() {
this.textAreaElement.val(localStorage.getItem('plots:lastpost'));
}
apply_template(template) {
if(this.textAreaValue == ""){
this.textAreaElement.val(this.templates[template])
}else if((this.textAreaValue == this.templates['event']) || (this.textAreaValue == this.templates['default']) || (this.textAreaValue == this.templates['support'])){
this.textAreaElement.val(this.templates[template])
}else{
$E.textarea.val($E.textarea.val()+'\n\n'+$E.templates[template])
this.textAreaElement.val(this.textAreaValue+'\n\n'+this.templates[template])
}
},
templates: {
'blog': "## The beginning\n\n## What we did\n\n## Why it matters\n\n## How can you help",
'default': "## What I want to do\n\n## My attempt and results\n\n## Questions and next steps\n\n## Why I'm interested",
'support': "## Details about the problem\n\n## A photo or screenshot of the setup",
'event': "## Event details\n\nWhen, where, what\n\n## Background\n\nWho, why",
'question': "## What I want to do or know\n\n## Background story"
},
previewing: false,
previewed: false,
generate_preview: function(id,text) {
$('#'+id)[0].innerHTML = marked(text)
},
toggle_preview: function() {
let previewBtn;
let dropzone;
}
generate_preview(id,text) {
$('#' + id)[0].innerHTML = marked(text)
}
toggle_preview() {
// if the element is part of a multi-comment page,
// ensure to grab the current element and not the other comment element.
previewBtn = $(this.textarea.context).find('.preview-btn');
dropzone = $(this.textarea.context).find('.dropzone');
const previewBtn = $("#toggle-preview-button-" + this.commentFormID);
const dropzone = $("#dropzone-large-" + this.commentFormID);

$E.preview[0].innerHTML = marked($E.textarea.val());
$E.preview.toggle();
this.previewElement[0].innerHTML = marked(this.textAreaValue);
this.previewElement.toggle();
dropzone.toggle();

this.toggleButtonPreviewMode(previewBtn);
},
toggleButtonPreviewMode: function (previewBtn) {
}
toggleButtonPreviewMode(previewBtn) {
let isPreviewing = previewBtn.attr('data-previewing');

// If data-previewing attribute is not present -> we are not in "preview" mode
Expand All @@ -166,4 +160,4 @@ $E = {
previewBtn.text('Preview');
}
}
}
}
69 changes: 17 additions & 52 deletions app/assets/javascripts/editorToolbar.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
// this script is used in a variety of different contexts including:
// this script is used wherever the legacy editor is used.
// pages (wikis, questions, research notes) with multiple comments & editors for each comment
// pages with JUST ONE form, and no other comments, eg. /wiki/new & /wiki/edit
// /app/views/features/_form.html.erb
// /app/views/map/edit.html.erb
// the legacy editor: /app/views/editor/_editor.html.erb (if it's still in use live?)

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;
params['preview'] = closestCommentFormWrapper.querySelector('.comment-preview').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';
// #preview-main should be unique as well
params['preview'] = 'comment-preview-main';
}
return params;
};
// and wherever /app/views/editor/editor.html.erb is still used in production

const progressAll = (elem, data) => {
var progress = parseInt(data.loaded / data.total * 100, 10);
Expand All @@ -36,13 +16,8 @@ const progressAll = (elem, data) => {
// attach eventListeners on document.load for toolbar rich-text buttons & image upload .dropzones
$(function() {
// for rich-text buttons (bold, italic, header, and link):
// click eventHandler that assigns $D.selected to the appropriate comment form
// 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
$('.rich-text-button').on('click', function(e) {
const { textArea, preview, dSelected } = getEditorParams(e.target);
// assign dSelected
if (dSelected) { $D.selected = dSelected; }
$E.setState(textArea, preview);
$E.setState(e.currentTarget.dataset.formId); // string that is: "main", "reply-123", "edit-123" etc.
const action = e.currentTarget.dataset.action // 'bold', 'italic', etc.
$E[action](); // call the appropriate editor function
});
Expand All @@ -65,10 +40,8 @@ $(function() {

// runs on drag & drop
$(this).on('drop',function(e) {
const { textArea, preview, dSelected } = getEditorParams(e.target);
e.preventDefault();
if (dSelected) { $D.selected = dSelected; }
$E.setState(textArea, preview);
$E.setState(e.currentTarget.dataset.formId); // string that is: "main", "reply-123", "edit-123" etc.
});

$(this).fileupload({
Expand All @@ -84,22 +57,18 @@ $(function() {
// 1. when user drag-and-drops image
// 2. when user clicks on upload button.
start: function(e) {
$E.setState(e.target.dataset.formId); // string that is: "main", "reply-123", "edit-123" etc.
$(e.target).removeClass('hover');
// 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
elem = $($D.selected).closest('div.comment-form-wrapper').eq(0);
elem.find('.progress-bar-container').eq(0).show();
elem.find('.uploading-text').eq(0).show();
elem.find('.choose-one-prompt-text').eq(0).hide();
console.log($("#image-upload-progress-container-" + $E.commentFormID));
$("#image-upload-progress-container-" + $E.commentFormID).show();
$("#image-upload-text-" + $E.commentFormID).show();
$("#dropzone-choose-one-" + $E.commentFormID).hide();
},
done: function (e, data) {
elem = $($D.selected).closest('div.comment-form-wrapper').eq(0);
elem.find('.progress-bar-container').hide();
elem.find('.progress-bar').css('width', 0);
elem.find('.uploading-text').hide();
elem.find('.choose-one-prompt-text').show();
$("#image-upload-progress-container-" + $E.commentFormID).hide();
$("#image-upload-text-" + $E.commentFormID).hide();
$("#dropzone-choose-one-" + $E.commentFormID).show();
$("#image-upload-progress-bar-" + $E.commentFormID).css('width', 0);
var extension = data.result['filename'].split('.')[data.result['filename'].split('.').length - 1]; var file_url = data.result.url.split('?')[0]; var file_type;
if (['gif', 'GIF', 'jpeg', 'JPEG', 'jpg', 'JPG', 'png', 'PNG'].includes(extension))
file_type = 'image'
Expand All @@ -108,27 +77,23 @@ $(function() {
switch (file_type) {
case 'image':
orig_image_url = file_url + '?s=o' // size = original
$E.wrap('[![', '](' + file_url + ')](' + orig_image_url + ')', {'newline': true, 'fallback': data.result['filename']}) // on its own line; see /app/assets/js/editor.js
$E.wrap('[![', '](' + file_url + ')](' + orig_image_url + ')', true, data.result['filename']);
break;
case 'csv':
$E.wrap('[graph:' + file_url + ']', '', {'newline': true})
$E.wrap('[graph:' + file_url + ']', '', true);
break;
default:
$E.wrap('<a href="'+data.result.url.split('?')[0]+'"><i class="fa fa-file"></i> ','</a>', {'newline': true, 'fallback': data.result['filename'].replace(/[()]/g , "-")}) // on its own line; see /app/assets/js/editor.js
$E.wrap('<a href="'+data.result.url.split('?')[0]+'"><i class="fa fa-file"></i> ', '</a>', true, data.result['filename'].replace(/[()]/g , "-")); // on its own line; see /app/assets/js/editor.js
}
// here append the image id to the wiki edit form:
if ($('#node_images').val() && $('#node_images').val().split(',').length > 1) $('#node_images').val([$('#node_images').val(),data.result.id].join(','))
else $('#node_images').val(data.result.id)
// eventual handling of multiple files; must add "multiple" to file input and handle on server side:
//$.each(data.result.files, function (index, file) {
// $('<p/>').text(file.name).appendTo(document.body);
//});
},
fileuploadfail: function(e, data) {
console.log(e);
},
progressall: function (e, data) {
const closestProgressBar = $($D.selected).closest('div.comment-form-wrapper').find('.progress-bar').eq(0);
const closestProgressBar = $("#image-upload-progress-bar-" + $E.commentFormID);
return progressAll(closestProgressBar, data);
}
});
Expand Down
Loading