diff --git a/notebooks/basic_tests.jl b/notebooks/basic_tests.jl index e238cce..a417535 100644 --- a/notebooks/basic_tests.jl +++ b/notebooks/basic_tests.jl @@ -1,5 +1,5 @@ ### A Pluto.jl notebook ### -# v0.19.27 +# v0.19.29 #> custom_attrs = ["hide-enabled"] @@ -136,7 +136,7 @@ pp = Plot(scatter3d(x = rand(N), y = rand(N), z = rand(N), mode="markers"), Layo # ╔═╡ ccf62e33-8fcf-45d9-83ed-c7de80800b76 let p = PlutoPlot(pp) - add_plotly_listener!(p, "plotly_relayout", htl_js(""" + add_plotly_listener!(p, "plotly_relayout", """ (e) => { console.log(e) @@ -154,8 +154,7 @@ let console.log('plot_obj: ',plot_obj.layout.scene?.camera?.eye) } - """)) - PlutoPlotly._show(p) + """) end # ╔═╡ 1460ece1-7828-4e93-ac37-e979b874b492 @@ -228,40 +227,39 @@ md""" # ╔═╡ c3b1a198-ef19-4a54-9c32-d9ea32a63812 let p = PlutoPlot(Plot(rand(10), Layout(uirevision = 1))) - add_plotly_listener!(p, "plotly_relayout", htl_js(""" + add_plotly_listener!(p, "plotly_relayout", """ function(e) { console.log('listener 1') } - """)) - add_plotly_listener!(p, "plotly_relayout", htl_js(""" + """) + add_plotly_listener!(p, "plotly_relayout", """ function(e) { console.log('listener 2') } - """)) - @htl "$p" + """) end # ╔═╡ e9fc2030-c2f0-48e9-a807-424039e796b2 let p = PlutoPlot(Plot(rand(10), Layout(uirevision = 1))) - add_plotly_listener!(p, "plotly_relayout", htl_js(""" + add_plotly_listener!(p, "plotly_relayout", """ function(e) { console.log('listener 1') } - """)) - add_plotly_listener!(p, "plotly_relayout", htl_js(""" + """) + add_plotly_listener!(p, "plotly_relayout", """ function(e) { console.log('listener 2') } - """)) + """) p.plotly_listeners end @@ -277,13 +275,13 @@ lololol = 1 let lololol p = PlutoPlot(Plot(rand(10), Layout(uirevision = 1))) - add_js_listener!(p, "mousedown", htl_js(""" + add_js_listener!(p, "mousedown", """ function(e) { console.log('MOUSEDOWN!') } - """)) + """) end # ╔═╡ 6128ff76-3f1f-4144-bb3d-f44678210013 @@ -518,8 +516,8 @@ PlutoUI = "7f904dfe-b85e-4ff6-b463-dae2292396a8" [compat] Colors = "~0.12.10" -PlutoDevMacros = "~0.5.8" -PlutoExtras = "~0.7.9" +PlutoDevMacros = "0.6" +PlutoExtras = "0.7" PlutoUI = "~0.7.52" """ @@ -529,7 +527,7 @@ PLUTO_MANIFEST_TOML_CONTENTS = """ julia_version = "1.10.0-beta2" manifest_format = "2.0" -project_hash = "becb366eb1628f34a109423203d027d4a184385e" +project_hash = "6b1e8a02b00ef43bc0119f5085df2925b6e55084" [[deps.AbstractPlutoDingetjes]] deps = ["Pkg"] @@ -568,6 +566,12 @@ version = "1.0.5+1" deps = ["Printf"] uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" +[[deps.DocStringExtensions]] +deps = ["LibGit2"] +git-tree-sha1 = "2fb1e02f2b635d0845df5d7c167fec4dd739b00d" +uuid = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" +version = "0.9.3" + [[deps.Downloads]] deps = ["ArgTools", "FileWatching", "LibCURL", "NetworkOptions"] uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6" @@ -687,16 +691,16 @@ uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" version = "1.10.0" [[deps.PlutoDevMacros]] -deps = ["HypertextLiteral", "InteractiveUtils", "MacroTools", "Markdown", "Pkg", "Random", "TOML"] -git-tree-sha1 = "6ce1d9f7c078b493812161349c48735dee275466" +deps = ["AbstractPlutoDingetjes", "DocStringExtensions", "HypertextLiteral", "InteractiveUtils", "MacroTools", "Markdown", "Pkg", "Random", "TOML"] +git-tree-sha1 = "06fa4aa7a8f2239eec99cf54eeddd34f3d4359be" uuid = "a0499f29-c39b-4c5c-807c-88074221b949" -version = "0.5.8" +version = "0.6.0" [[deps.PlutoExtras]] -deps = ["AbstractPlutoDingetjes", "HypertextLiteral", "InteractiveUtils", "Markdown", "PlutoDevMacros", "PlutoUI", "REPL", "Reexport"] -git-tree-sha1 = "aad38509250eaa0840d2aadd73ef23c2a89bdb4a" +deps = ["AbstractPlutoDingetjes", "HypertextLiteral", "InteractiveUtils", "Markdown", "PlutoDevMacros", "PlutoUI", "REPL"] +git-tree-sha1 = "382b530c2ebe31f4a44cb055642bbd71197fbd20" uuid = "ed5d0301-4775-4676-b788-cf71e66ff8ed" -version = "0.7.9" +version = "0.7.11" [[deps.PlutoUI]] deps = ["AbstractPlutoDingetjes", "Base64", "ColorTypes", "Dates", "FixedPointNumbers", "Hyperscript", "HypertextLiteral", "IOCapture", "InteractiveUtils", "JSON", "Logging", "MIMEs", "Markdown", "Random", "Reexport", "URIs", "UUIDs"] @@ -712,9 +716,9 @@ version = "1.2.0" [[deps.Preferences]] deps = ["TOML"] -git-tree-sha1 = "7eb1686b4f04b82f96ed7a4ea5890a4f0c7a09f1" +git-tree-sha1 = "00805cd429dcb4870060ff49ef443486c262e38e" uuid = "21216c6a-2e73-6563-6e65-726566657250" -version = "1.4.0" +version = "1.4.1" [[deps.Printf]] deps = ["Unicode"] @@ -833,7 +837,7 @@ version = "17.4.0+2" # ╠═de0cb780-ff4e-4236-89c4-4c3163337cfc # ╠═dd23fe10-a8d5-461a-85a8-e03468cdcd97 # ╠═ccf62e33-8fcf-45d9-83ed-c7de80800b76 -# ╠═1460ece1-7828-4e93-ac37-e979b874b492 +# ╟─1460ece1-7828-4e93-ac37-e979b874b492 # ╠═18c80ea2-0df4-40ea-bd87-f8fee463161e # ╠═ce29fa1f-0c52-4d38-acbd-0a96cb3b9ce6 # ╟─c3e29c94-941d-4a52-a358-c4ffbfc8cab8 diff --git a/notebooks/make_subplots_tests.jl b/notebooks/make_subplots_tests.jl index d71707f..90157c4 100644 --- a/notebooks/make_subplots_tests.jl +++ b/notebooks/make_subplots_tests.jl @@ -170,8 +170,8 @@ PlutoExtras = "ed5d0301-4775-4676-b788-cf71e66ff8ed" PlutoUI = "7f904dfe-b85e-4ff6-b463-dae2292396a8" [compat] -PlutoDevMacros = "~0.5.8" -PlutoExtras = "~0.7.10" +PlutoDevMacros = "~0.6.0" +PlutoExtras = "~0.7.11" PlutoUI = "~0.7.52" """ @@ -181,7 +181,7 @@ PLUTO_MANIFEST_TOML_CONTENTS = """ julia_version = "1.10.0-beta2" manifest_format = "2.0" -project_hash = "f50c3104111b5d594359e3dd1628a076c5fe269d" +project_hash = "46f2e02ace855d54b6bc9494b62148b187ae7606" [[deps.AbstractPlutoDingetjes]] deps = ["Pkg"] @@ -214,6 +214,12 @@ version = "1.0.5+1" deps = ["Printf"] uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" +[[deps.DocStringExtensions]] +deps = ["LibGit2"] +git-tree-sha1 = "2fb1e02f2b635d0845df5d7c167fec4dd739b00d" +uuid = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" +version = "0.9.3" + [[deps.Downloads]] deps = ["ArgTools", "FileWatching", "LibCURL", "NetworkOptions"] uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6" @@ -333,16 +339,16 @@ uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" version = "1.10.0" [[deps.PlutoDevMacros]] -deps = ["HypertextLiteral", "InteractiveUtils", "MacroTools", "Markdown", "Pkg", "Random", "TOML"] -git-tree-sha1 = "6ce1d9f7c078b493812161349c48735dee275466" +deps = ["AbstractPlutoDingetjes", "DocStringExtensions", "HypertextLiteral", "InteractiveUtils", "MacroTools", "Markdown", "Pkg", "Random", "TOML"] +git-tree-sha1 = "06fa4aa7a8f2239eec99cf54eeddd34f3d4359be" uuid = "a0499f29-c39b-4c5c-807c-88074221b949" -version = "0.5.8" +version = "0.6.0" [[deps.PlutoExtras]] -deps = ["AbstractPlutoDingetjes", "HypertextLiteral", "InteractiveUtils", "Markdown", "PlutoDevMacros", "PlutoUI", "REPL", "Reexport"] -git-tree-sha1 = "beedecb30d8ed0874773d5641f5ce5ee2bfeeded" +deps = ["AbstractPlutoDingetjes", "HypertextLiteral", "InteractiveUtils", "Markdown", "PlutoDevMacros", "PlutoUI", "REPL"] +git-tree-sha1 = "382b530c2ebe31f4a44cb055642bbd71197fbd20" uuid = "ed5d0301-4775-4676-b788-cf71e66ff8ed" -version = "0.7.10" +version = "0.7.11" [[deps.PlutoUI]] deps = ["AbstractPlutoDingetjes", "Base64", "ColorTypes", "Dates", "FixedPointNumbers", "Hyperscript", "HypertextLiteral", "IOCapture", "InteractiveUtils", "JSON", "Logging", "MIMEs", "Markdown", "Random", "Reexport", "URIs", "UUIDs"] diff --git a/src/js_helpers.jl b/src/js_helpers.jl index 1e4c32d..5b6af1a 100644 --- a/src/js_helpers.jl +++ b/src/js_helpers.jl @@ -24,13 +24,14 @@ console.log(PLOT) // logs the plot div inside the developer console when pressin \"\"\" ``` """ -function add_js_listener!(p::PlutoPlot, event_name::String, listener::String) +function add_js_listener!(p::PlutoPlot, event_name::String, listener::JS) ldict = p.js_listeners - listeners_array = get!(ldict, event_name, String[]) + listeners_array = get!(ldict, event_name, JS[]) push!(listeners_array, listener) return p end -add_js_listener!(p::PlutoPlot, event_name, listener::String) = add_js_listener!(p, event_name, htl_js(listener)) +add_js_listener!(p::PlutoPlot, event_name, listener::String) = +add_js_listener!(p, event_name, JS(listener)) ## add class ## """ @@ -68,34 +69,52 @@ end ## Push Script ## """ - push_script!(p::PlutoPlot, items...) -Add script contents contained in collection `items` at the end of the plot show method script. -The `item` must either be a collection of `String` or `HypertextLiteral.JavaScript` elements + push_script!(p::PlutoPlot, item) + push_script!(p::PlutoPlot, name::String, item) +Add `item` to the end of the `script_contents` field of the `PlutoPlot` object +`p`. The function accepts a custom name to identify this script as the +`script_contents` is a Dictionary with String Indices. + +If the `name` is not provided, a name of the form `custom_script_N` will be +generated automatically. """ -function push_script!(p::PlutoPlot, items::Vararg{Union{Script, ScriptContent, Function},N}) where N +function push_script!(p::PlutoPlot, name::String, item) @nospecialize - push!(p.script_contents.vec, items...) + insert!(p.script_contents, name, PrintToScript(item)) return p end +function push_script!(p::PlutoPlot, item) + sc = p.script_contents + N = count(startswith("custom_script_"), sc.indices) + 1 + name = "custom_script_$N" + push_script!(p, name, item) +end + ## plotly listener ## """ - add_plotly_listener!(p::PlutoPlot, event_name::String, listener::HypertextLiteral.JavaScript) add_plotly_listener!(p::PlutoPlot, event_name::String, listener::String) -Add a custom *javascript* `listener` (to be provided as `String` or directly as `HypertextLiteral.JavaScript`) to the `PlutoPlot` object `p`, and associated to the [plotly event](https://plotly.com/javascript/plotlyjs-events/) specified by `event_name`. +Add a custom *javascript* `listener` (to be provided as `String` or directly as +`HypertextLiteral.JavaScript`) to the `PlutoPlot` object `p`, and associated to +the [plotly event](https://plotly.com/javascript/plotlyjs-events/) specified by +`event_name`. -The listeners are added to the HTML plot div after rendering. The div where the plot is inserted can be accessed using the variable named `PLOT` inside the listener code. +The listeners are added to the HTML plot div after rendering. The div where the +plot is inserted can be accessed using the variable named `PLOT` inside the +listener code. # Differences with `add_js_listener!` -This function adds a listener using the plotly internal events via the `on` function. These events differ from the standard javascript ones and provide data specific to the plot. +This function adds a listener using the plotly internal events via the `on` +function. These events differ from the standard javascript ones and provide data +specific to the plot. See also: [`add_js_listener!`](@ref), [`htl_js`](@ref) # Examples: ```julia p = PlutoPlot(Plot(rand(10), Layout(uirevision = 1))) -add_plotly_listener!(p, "plotly_relayout", htl_js(\"\"\" +add_plotly_listener!(p, "plotly_relayout", \"\"\" function(e) { console.log(PLOT) // logs the plot div inside the developer console @@ -104,10 +123,11 @@ console.log(PLOT) // logs the plot div inside the developer console \"\"\" ``` """ -function add_plotly_listener!(p::PlutoPlot, event_name::String, listener::String) +function add_plotly_listener!(p::PlutoPlot, event_name::String, listener::JS) ldict = p.plotly_listeners - listeners_array = get!(ldict, event_name, String[]) + listeners_array = get!(ldict, event_name, JS[]) push!(listeners_array, listener) return p end -add_plotly_listener!(p::PlutoPlot, event_name, listener::String) = add_plotly_listener!(p, event_name, htl_js(listener)) \ No newline at end of file +add_plotly_listener!(p::PlutoPlot, event_name::String, listener::String) = +add_plotly_listener!(p, event_name, JS(listener)) \ No newline at end of file diff --git a/src/main_struct.jl b/src/main_struct.jl index 62ff0dd..3837fa0 100644 --- a/src/main_struct.jl +++ b/src/main_struct.jl @@ -1,6 +1,7 @@ const PLOTLY_VERSION = Ref("2.26.2") -const SCType = Dictionary{String, Union{Function, Script}} +const SCType = Dictionary{String, Union{Function, Script, PrintToScript}} +const JS = HypertextLiteral.JavaScript # This will contain the default contents of the script generating the plot const _default_script_contents = SCType() @@ -53,8 +54,8 @@ See also: [`ScriptContents`](@ref), [`add_js_listener!`](@ref), [`add_plotly_lis """ Base.@kwdef struct PlutoPlot Plot::PlotlyBase.Plot - plotly_listeners::Dict{String, Vector{String}} = Dict{String, Vector{String}}() - js_listeners::Dict{String, Vector{String}} = Dict{String, Vector{String}}() + plotly_listeners::Dict{String, Vector{JS}} = Dict{String, Vector{JS}}() + js_listeners::Dict{String, Vector{JS}} = Dict{String, Vector{JS}}() classList::Vector{String} = String[] script_contents::SCType = deepcopy(_default_script_contents) end diff --git a/src/show.jl b/src/show.jl index e8af7c3..3d030c4 100644 --- a/src/show.jl +++ b/src/show.jl @@ -30,6 +30,7 @@ function PlutoCombineHTL.print_javascript(io::IO, pts::PrintToScript{<:DisplayLo # Extract the PlutoPlot pp = pts.el _publish = x -> print_javascript(io, x; pluto) + _publish_listeners = x -> HypertextLiteral.print_script(io, x) # We publish the plot obj print(io, " // Publish the plot object to JS @@ -40,12 +41,12 @@ function PlutoCombineHTL.print_javascript(io::IO, pts::PrintToScript{<:DisplayLo print(io, " // Publish the plotly listeners const plotly_listeners = ") - _publish(pp.plotly_listeners) + _publish_listeners(pp.plotly_listeners) ## JS print(io, " // Publish the JS listeners const js_listeners = ") - _publish(pp.js_listeners) + _publish_listeners(pp.js_listeners) # Custom classes print(io, " // Deal with eventual custom classes