diff --git a/Project.toml b/Project.toml index 23edd5e..bd572c2 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "PlutoTeachingTools" uuid = "661c6b06-c737-4d37-b85c-46df65de6f69" authors = ["Eric Ford and contributors"] -version = "0.3.0" +version = "0.3.1" [deps] Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6" diff --git a/example.jl b/example.jl index 13748d5..33b8360 100644 --- a/example.jl +++ b/example.jl @@ -295,13 +295,18 @@ md""" """ # ╔═╡ fc5789dd-4b86-4007-9771-a6246235fd73 -TwoColumn(md"Left col", md"Right col") +Columns(md"Left col", md"Right col") # ╔═╡ 2881e5de-f6b7-47c0-a794-3e0fa57b712b -ThreeColumn(md"Left col", md"Middle col", md"Right col") +Columns(md"Left col", md"Middle col", md"Right col") + +# ╔═╡ c22f7f6a-171d-4379-86c9-1781875cf0a4 +md""" +You can customize the widths of the columns. +""" # ╔═╡ 0e1e62a6-3b78-4415-89fe-fa17279fddbf -TwoColumnWideLeft(md"Left col", md"Right col") +Columns(md"Left col", md"Right col"; widths=[33, 66]) # ╔═╡ f43beea9-7a7e-4ee6-8ae6-350640c426aa TwoColumnWideRight(md"Left col", md"Right col") @@ -957,6 +962,7 @@ version = "17.4.0+2" # ╟─d7593309-3462-4dba-8275-c2eb76b4c3fe # ╠═fc5789dd-4b86-4007-9771-a6246235fd73 # ╠═2881e5de-f6b7-47c0-a794-3e0fa57b712b +# ╟─c22f7f6a-171d-4379-86c9-1781875cf0a4 # ╠═0e1e62a6-3b78-4415-89fe-fa17279fddbf # ╠═f43beea9-7a7e-4ee6-8ae6-350640c426aa # ╠═44d651d3-ce42-4061-b193-da7c31efed8e diff --git a/src/PlutoTeachingTools.jl b/src/PlutoTeachingTools.jl index 45cb1b1..acbeae9 100644 --- a/src/PlutoTeachingTools.jl +++ b/src/PlutoTeachingTools.jl @@ -1,5 +1,6 @@ module PlutoTeachingTools +using PlutoUI.ExperimentalLayout: Div using Markdown: @md_str, MD, Admonition, LaTeX using HypertextLiteral: @htl, @htl_str using Downloads: download # used in robustlocalresouce.jl @@ -31,7 +32,7 @@ export display_msg_if_fail include("present.jl") export present_button export Foldable -export TwoColumn, ThreeColumn +export Columns, TwoColumn, ThreeColumn export TwoColumnWideLeft, TwoColumnWideRight export ChooseDisplayMode # combines present_button and WidthOverDocs diff --git a/src/present.jl b/src/present.jl index 4f23377..a72b884 100644 --- a/src/present.jl +++ b/src/present.jl @@ -8,71 +8,63 @@ end Foldable(title, content) = details(title, content) +""" -struct TwoColumn{L,R} - left::L - right::R -end + Columns(cols...; widths, gap) -function Base.show(io, mime::MIME"text/html", tc::TwoColumn) - write(io, """
""") - show(io, mime, tc.left) - write(io, """
""") - write(io, """
""") - show(io, mime, tc.right) - write(io, """
""") - return nothing -end +Displays (any number of) columns nicely in Pluto. -struct TwoColumnWideLeft{L,R} - left::L - right::R -end +* `widths` should sum to 100 +* `gap` in percent -function Base.show(io, mime::MIME"text/html", tc::TwoColumnWideLeft) - write(io, """
""") - show(io, mime, tc.left) - write(io, """
""") - write(io, """
""") - show(io, mime, tc.right) - write(io, """
""") - return nothing -end +### Examples +```julia +# three columns +Columns( + almost(md"here"), + almost(md"there"), + md"bla bla bla" +) -struct TwoColumnWideRight{L,R} - left::L - right::R -end +# two columns with customization +Columns( + almost(md"here"), almost(md"there"), + widths = [40, 60], gap = 2 +) +``` +""" +function Columns(cols...; widths=nothing, gap=2) + ncols = length(cols) + ngaps = ncols - 1 + if isnothing(widths) + widths = fill(100 / ncols, ncols) + end + if gap > 0 # adjust widths if gaps are desired + widths = widths / sum(widths) * (sum(widths) - gap * ngaps) + end -function Base.show(io, mime::MIME"text/html", tc::TwoColumnWideRight) - write(io, """
""") - show(io, mime, tc.left) - write(io, """
""") - write(io, """
""") - show(io, mime, tc.right) - write(io, """
""") - return nothing -end + columns = [ + Div([cols[i]], style=Dict("flex" => "0 1 $(widths[i])%")) for + i in 1:ncols + ] + the_gap = Div([], style=Dict("flex" => "0 0 $gap%")) -struct ThreeColumn{L,C,R} - left::L - center::C - right::R -end + # insert gaps between columns + # i.e. [a, b, c] ==> [a, gap, b, gap, c] + children = vec([reshape(columns, 1, :); fill(the_gap, 1, ncols)])[1:(end - 1)] -function Base.show(io, mime::MIME"text/html", tc::ThreeColumn) - write(io, """
""") - show(io, mime, tc.left) - write(io, """
""") - write(io, """
""") - show(io, mime, tc.center) - write(io, """
""") - write(io, """
""") - show(io, mime, tc.right) - write(io, """
""") - return nothing + return Div(children, style=Dict("display" => "flex", "flex-direction" => "row")) end +# for backwards compatibility +TwoColumn(a, b; kwargs...) = Columns(a, b; kwargs...) + +ThreeColumn(a, b, c; kwargs...) = Columns(a, b, c; kwargs...) + +TwoColumnWideLeft(a, b; kwargs...) = Columns(a, b; widths=[66, 34], kwargs...) + +TwoColumnWideRight(a, b; kwargs...) = Columns(a, b; widths=[34, 66], kwargs...) + """ Provides checkbox to toggle full width and present mode. """ function ChooseDisplayMode(; wide::Bool=false, present::Bool=false, lang::AbstractLanguage=default_language[]