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

Fix Rich-Text Cross-Wiring in Comment Forms #9003

Closed
wants to merge 4 commits into from

Conversation

noi5e
Copy link
Contributor

@noi5e noi5e commented Jan 13, 2021

Fixes #8478

The review comments explain most everything. I think this fix covers most bases, since $D.selected is now assigned any time a button is clicked.

Interestingly, even before I merged unique button IDs, I saw that e.target was pretty accurate. That makes me think that these cross-wiring fixes maybe have less to do with unique IDs for every single element, and more to do with how the jQuery eventListeners are attached? Still not sure.

The most important thing here is code architecture, which codeclimate caught. dragdrop.js and editor.js now have duplicated code... Not sure where to put their common code. Also, not sure if the click eventHandler truly belongs in editor.js. Pointers definitely welcome!!! 🙏


(This issue is part of the larger Comment Editor Overhaul Project with Outreachy. Refer to Planning Issue #9069 for more context)

@noi5e noi5e requested review from a team as code owners January 13, 2021 01:58
@gitpod-io
Copy link

gitpod-io bot commented Jan 13, 2021

@noi5e noi5e added bug the issue is regarding one of our programs which faces problems when a certain task is executed outreachy labels Jan 13, 2021
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) {
Copy link

Choose a reason for hiding this comment

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

Identical blocks of code found in 2 locations. Consider refactoring.

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 yeah, this is important too I guess. The code here is reduplicated from dragdrop.js. I guess, do I make a new asset/js that can be loaded from both scripts?

I'm a little unfamiliar with the conventions for how these scripts are organized and loaded, can you give me some pointers?

app/assets/javascripts/editor.js Show resolved Hide resolved
// this click eventHandler 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
// 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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Feels weird to have this click handler in editor.js, but I wasn't sure where else to put it.

$E.initialize(params);
const action = e.currentTarget.dataset.action // 'bold', 'italic', etc.
$E[action](); // call the appropriate editor function
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These lines replace the inline onclick handlers that used to be in the _toolbar.html.erb partial.

@codecov
Copy link

codecov bot commented Jan 13, 2021

Codecov Report

❗ No coverage uploaded for pull request base (main@0f8ffbf). Click here to learn what that means.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #9003   +/-   ##
=======================================
  Coverage        ?   74.54%           
=======================================
  Files           ?      100           
  Lines           ?     6058           
  Branches        ?        0           
=======================================
  Hits            ?     4516           
  Misses          ?     1542           
  Partials        ?        0           

link: function() {
uri = prompt('Enter a URL');
if (uri == null) { uri = ""; }
$E.wrap('[', '](' + uri + ')');
},
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 added a check for null values here. Before, if the user didn't fill out the prompt, ()[null] would appear in the comment form.

class="bold-button btn btn-outline-secondary btn-sm"
onClick="$E.bold()"
data-toggle="tooltip"
class="bold-button btn btn-outline-secondary btn-sm rich-text-button"
>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Again, note that I removed the onclick function. I also added a data-action attribute which is used in the button click handler.

<% end %>
</div>
</div>
<% end %>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unrelated to this PR, but I've been wanting to change this indentation for a while and thought I'd bundle it into this one.

text_input_value = main_comment_form.find('#text-input').value
assert_equal(text_input_value, '****')
end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Two new tests, these were the most likely scenarios for screw-ups when I did my manual testing.

@noi5e noi5e requested a review from a team as a code owner January 14, 2021 20:38
@noi5e noi5e force-pushed the bug/rich-text-crosswiring branch 2 times, most recently from 09461ac to fa81084 Compare January 14, 2021 20:56
@publiclab publiclab deleted a comment from codeclimate bot Jan 14, 2021
@noi5e
Copy link
Contributor Author

noi5e commented Jan 14, 2021

I did a git rebase main on my local branch, and I can't figure out how to push the new changes without including a ton of changes by everyone else! Going to close this PR and delete the branch, then make it again from a new branch.

@noi5e noi5e closed this Jan 14, 2021
@noi5e noi5e deleted the bug/rich-text-crosswiring branch January 14, 2021 21:34
@noi5e noi5e changed the title Bug: Fix Rich-Text Cross-Wiring in Comment Forms Fix Rich-Text Cross-Wiring in Comment Forms Jan 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug the issue is regarding one of our programs which faces problems when a certain task is executed outreachy
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrong text input gets selected while using toolbar on comments
1 participant