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

Optionally provide innerHtml and textContent on StimulusReflex::Element #517

Merged
merged 15 commits into from
Jul 18, 2021

Conversation

julianrubisch
Copy link
Contributor

@julianrubisch julianrubisch commented Jun 3, 2021

Enhancement

Description

This PR allows to optionally transmit the reflexElement's innerHTML and/or textContent when calling a reflex.

This can be done:

  1. by passing includeInnerHTML: true or includeTextContent: true as an option to this.stimulate
    OR
  2. by declaring data-reflex-include-html or data-reflex-include-text on the reflex HTML element

This PR also includes a refactoring of client-side reflexData handling into its own class, analog to the Ruby side, along with tests.

Why should this be added

This PR is inspired by working with contenteditable divs (i.e., a "Notion"-like interface).

Prior to this, you would have to put the innerHTML into a data attribute manually like this:

    currentTarget.dataset.content = currentTarget.innerHTML;

    this.stimulate("ContentElementsReflex#save", currentTarget);

and I figured this should be taken care of at the framework level.

Checklist

  • My code follows the style guidelines of this project
  • Checks (StandardRB & Prettier-Standard) are passing
  • This is not a documentation update

Please note that the best way to suggest changes or updates to the documentation is to join Discord and leave a note in the #docs channel. Any documentation updates posted as PRs cannot be accepted at this time. ❤️

@julianrubisch julianrubisch added javascript Pull requests that update Javascript code proposal enhancement New feature or request labels Jun 3, 2021
@julianrubisch julianrubisch force-pushed the reflex-data-innerhtml branch from 20c84a5 to d916662 Compare June 3, 2021 15:27
@leastbad
Copy link
Contributor

leastbad commented Jun 3, 2021

This is really exciting! 🦖

@julianrubisch julianrubisch force-pushed the reflex-data-innerhtml branch from a93880e to 91055bc Compare June 4, 2021 06:52
@julianrubisch julianrubisch changed the title Extract JS ReflexData class analog to Ruby Optionally provide innerHtml and textContent on StimulusReflex::Element Jun 4, 2021
@julianrubisch julianrubisch marked this pull request as ready for review June 4, 2021 10:31
@julianrubisch julianrubisch requested review from hopsoft and leastbad June 4, 2021 10:32
@assuntaw
Copy link
Contributor

assuntaw commented Jun 4, 2021

This is so useful @julianrubisch !

The note-taking feature I'm working on has contenteditable divs all over the place, and there's a lot going on in the corresponding Stimulus controllers. Not having to assign the innerHTML to the dataset before saving would be super helpful. Thanks for taking this on 🎉

app/channels/stimulus_reflex/channel.rb Show resolved Hide resolved
javascript/stimulus_reflex.js Show resolved Hide resolved
lib/stimulus_reflex/element.rb Show resolved Hide resolved
lib/stimulus_reflex/element.rb Show resolved Hide resolved
@julianrubisch julianrubisch force-pushed the reflex-data-innerhtml branch from 8539540 to 8fe3cbb Compare June 21, 2021 08:32
@julianrubisch julianrubisch mentioned this pull request Jun 29, 2021
3 tasks
@leastbad leastbad added this to the 3.5 milestone Jun 30, 2021
@julianrubisch
Copy link
Contributor Author

We'll have to hold this back a bit, I might have found a bug around checkbox list values not being pulled correctly from attrs

@julianrubisch julianrubisch force-pushed the reflex-data-innerhtml branch from c595fe5 to 09b9c98 Compare July 6, 2021 18:40
Copy link
Contributor

@leastbad leastbad left a comment

Choose a reason for hiding this comment

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

What if you call a succession of selector/nothing Reflexes, using the same instance of an SR controller, and the dataset/selectors change between invocations?

@julianrubisch
Copy link
Contributor Author

hmmm so all I can say is this mimicks behavior as it was before. I was seeing errors because I was pulling attrs of reflexElement which changed its checked status midway (in the setTimeout in stimulate), and suddenly the wrong set of values was sent back to the server

In other words: the state of ReflexData was mutable, whereas prior to that PR it was just stored as a simple object (const data = { .... }) that did not mutate.

Now we're back to "as before"

@leastbad
Copy link
Contributor

leastbad commented Jul 7, 2021

That's a great answer, thank-you.

With that insight in mind, I amend my review to LGTM.

Each stimulate invocation calls new ReflexData so there is no risk of stale values.

@julianrubisch
Copy link
Contributor Author

Exactly!

@hopsoft
Copy link
Contributor

hopsoft commented Jul 17, 2021

This looks great. I propose that we make the data attributes more explicit to match the stimulate args. Even though it's annoyingly verbose.

  • data-reflex-include-inner-html
  • data-reflex-include-text-content

leastbad and others added 3 commits July 18, 2021 07:15
* GitBook: [master] 18 pages modified

* GitBook: [master] 18 pages modified

* Update Changelog

* GitBook: [master] one page modified

* GitBook: [master] one page modified

* refactor stream_name (stimulusreflex#519)

* Add localization example to ApplicationReflex template (stimulusreflex#521)

* connection_identifier needs to return a non-empty value

* Update Changelog

* Fix two minor typos (stimulusreflex#526)

* chore: add sideEffects false to make Webpack happy (stimulusreflex#523)

* Update Changelog

* Schema object with getters (stimulusreflex#505)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Erlingur Þorsteinsson <erlingur@erlingur.is>
Co-authored-by: Julian Rubisch <julian@julianrubisch.at>
Co-authored-by: Konnor Rogers <konnor5456@gmail.com>
@leastbad leastbad merged commit 93e69e9 into stimulusreflex:master Jul 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request javascript Pull requests that update Javascript code proposal
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants