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

New Sliders! #109

Closed
wants to merge 6 commits into from
Closed

New Sliders! #109

wants to merge 6 commits into from

Conversation

SyxP
Copy link

@SyxP SyxP commented May 21, 2021

This reimplements Sliders solving two issues:

  1. It starts on the proposed rewrite in HypertextLiteral.jl as stated in [WIP] refactoring PlutoUI with Mustache #62 and Rewrite to use Hyperscript.jl #59 .
  2. This fixes a issue that with multiple of the same Slider with show_value = true, moving a Slider would only change the display of the various input ranges, but not the display of the value. This syncs all the values, solving @bind doesn't update showed value for multiple instances in different cells #96 .

Unfortunately, there is a slight caveat to consider. Traditionally, the Slider is seen as the "simplest" example of PlutoUI and a good way to introduce the ecosystem. However, this rewrite makes use of fairly nasty tricks to ensure (2) above.

@fonsp
Copy link
Member

fonsp commented May 22, 2021

Great! Some short comments based on reading the code:

  • Can I still place it inline? md"Hello $(@bind x Slider(1:10)) world"? If not try replacing <div> with <span>
  • We should use the struct because it defines Base.get, see the Interactivity sample notebook inside Pluto to learn what this means

src/Builtins.jl Outdated Show resolved Hide resolved
@SyxP
Copy link
Author

SyxP commented May 22, 2021

I have undid the changes so that it is now Slider <: DataType and re-added the get function. Moreover, you're right, I didn't notice that <div> will consume the whole-line and changed it to <span>. (I only started learning js because of Pluto 😄 )

Let me elaborate a bit more on the overwriting the div valuesetter. Observe the sample notebook on Javascript, if you create two cells saying

  1. t = @bind num_clicks ClickCounter()
  2. t

when you observe num_clicks, each click counter will carry its own internal state. Thus, simply adding

Object.defineProperty(div, "value",
  {configurable: false, enumerable: false,
  get: () => {return count},
  set: (newVal) => {
    count = newVal
  }})

would cause the individual click counters to update the local variable when the outer div's value property gets set.

src/Builtins.jl Outdated

slider.addEventListener("input", e => {
parentnode.value = +slider.value
$(JavaScript(show_value ? "parentnode.querySelector(\"output\").value = +slider.value" : ""))
Copy link
Member

Choose a reason for hiding this comment

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

Instead of generating JS like this, can you add line 34:

let show_value = $(show_value)

and then use normal JS in this line?

Copy link
Author

Choose a reason for hiding this comment

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

The reason for I feel that doing so is superior is two-fold:

  1. This minimizes the javascript on the page being rendered. When one attempts to study the Pluto cells, it is easier to understand if we provide less code in the first place, rather than having a bunch of objects in the page and hiding their visibility on the browser. Here, we minimized the code sent between the server and the client.
  2. The pedagogical advantage of showing an example where we interpolate javascript code using Julia is very useful in my opinion.

Copy link
Member

Choose a reason for hiding this comment

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

I don't want to linger on this point but 1. is not a necessary optimization, and I see 2. exactly opposite: I don't want to teach this code style to people, so we shouldn't use it ourselves.

src/Builtins.jl Outdated Show resolved Hide resolved
@fonsp
Copy link
Member

fonsp commented Nov 29, 2021

Implemented in #148 , thanks for thinking together!

@fonsp fonsp closed this Nov 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants