|
8 | 8 |
|
9 | 9 | Foldable(title, content) = details(title, content)
|
10 | 10 |
|
| 11 | +""" |
11 | 12 |
|
12 |
| -struct TwoColumn{L,R} |
13 |
| - left::L |
14 |
| - right::R |
15 |
| -end |
| 13 | + Columns(cols...; widths, gap) |
16 | 14 |
|
17 |
| -function Base.show(io, mime::MIME"text/html", tc::TwoColumn) |
18 |
| - write(io, """<div style="display: flex;"><div style="flex: 49%;">""") |
19 |
| - show(io, mime, tc.left) |
20 |
| - write(io, """</div><div style="flex: 2%;">""") |
21 |
| - write(io, """</div><div style="flex: 49%;">""") |
22 |
| - show(io, mime, tc.right) |
23 |
| - write(io, """</div></div>""") |
24 |
| - return nothing |
25 |
| -end |
| 15 | +Displays (any number of) columns nicely in Pluto. |
26 | 16 |
|
27 |
| -struct TwoColumnWideLeft{L,R} |
28 |
| - left::L |
29 |
| - right::R |
30 |
| -end |
| 17 | +* `widths` should sum to 100 |
| 18 | +* `gap` in percent |
31 | 19 |
|
32 |
| -function Base.show(io, mime::MIME"text/html", tc::TwoColumnWideLeft) |
33 |
| - write(io, """<div style="display: flex;"><div style="flex: 65%;">""") |
34 |
| - show(io, mime, tc.left) |
35 |
| - write(io, """</div><div style="flex: 2%;">""") |
36 |
| - write(io, """</div><div style="flex: 33%;">""") |
37 |
| - show(io, mime, tc.right) |
38 |
| - write(io, """</div></div>""") |
39 |
| - return nothing |
40 |
| -end |
| 20 | +### Examples |
| 21 | +```julia |
| 22 | +# three columns |
| 23 | +Columns( |
| 24 | + almost(md"here"), |
| 25 | + almost(md"there"), |
| 26 | + md"bla bla bla" |
| 27 | +) |
41 | 28 |
|
42 |
| -struct TwoColumnWideRight{L,R} |
43 |
| - left::L |
44 |
| - right::R |
45 |
| -end |
| 29 | +# two columns with customization |
| 30 | +Columns( |
| 31 | + almost(md"here"), almost(md"there"), |
| 32 | + widths = [40, 60], gap = 2 |
| 33 | +) |
| 34 | +``` |
| 35 | +""" |
| 36 | +function Columns(cols...; widths=nothing, gap=2) |
| 37 | + ncols = length(cols) |
| 38 | + ngaps = ncols - 1 |
| 39 | + if isnothing(widths) |
| 40 | + widths = fill(100 / ncols, ncols) |
| 41 | + end |
| 42 | + if gap > 0 # adjust widths if gaps are desired |
| 43 | + widths = widths / sum(widths) * (sum(widths) - gap * ngaps) |
| 44 | + end |
46 | 45 |
|
47 |
| -function Base.show(io, mime::MIME"text/html", tc::TwoColumnWideRight) |
48 |
| - write(io, """<div style="display: flex;"><div style="flex: 33%;">""") |
49 |
| - show(io, mime, tc.left) |
50 |
| - write(io, """</div><div style="flex: 2%;">""") |
51 |
| - write(io, """</div><div style="flex: 65%;">""") |
52 |
| - show(io, mime, tc.right) |
53 |
| - write(io, """</div></div>""") |
54 |
| - return nothing |
55 |
| -end |
| 46 | + columns = [ |
| 47 | + Div([cols[i]], style=Dict("flex" => "0 1 $(widths[i])%")) for |
| 48 | + i in 1:ncols |
| 49 | + ] |
| 50 | + the_gap = Div([], style=Dict("flex" => "0 0 $gap%")) |
56 | 51 |
|
57 |
| -struct ThreeColumn{L,C,R} |
58 |
| - left::L |
59 |
| - center::C |
60 |
| - right::R |
61 |
| -end |
| 52 | + # insert gaps between columns |
| 53 | + # i.e. [a, b, c] ==> [a, gap, b, gap, c] |
| 54 | + children = vec([reshape(columns, 1, :); fill(the_gap, 1, ncols)])[1:(end - 1)] |
62 | 55 |
|
63 |
| -function Base.show(io, mime::MIME"text/html", tc::ThreeColumn) |
64 |
| - write(io, """<div style="display: flex;"><div style="flex: 32%;">""") |
65 |
| - show(io, mime, tc.left) |
66 |
| - write(io, """</div><div style="flex: 2%;">""") |
67 |
| - write(io, """</div><div style="flex: 32%;">""") |
68 |
| - show(io, mime, tc.center) |
69 |
| - write(io, """</div><div style="flex: 2%;">""") |
70 |
| - write(io, """</div><div style="flex: 32%;">""") |
71 |
| - show(io, mime, tc.right) |
72 |
| - write(io, """</div></div>""") |
73 |
| - return nothing |
| 56 | + return Div(children, style=Dict("display" => "flex", "flex-direction" => "row")) |
74 | 57 | end
|
75 | 58 |
|
| 59 | +# for backwards compatibility |
| 60 | +TwoColumn(a, b; kwargs...) = Columns(a, b; kwargs...) |
| 61 | + |
| 62 | +ThreeColumn(a, b, c; kwargs...) = Columns(a, b, c; kwargs...) |
| 63 | + |
| 64 | +TwoColumnWideLeft(a, b; kwargs...) = Columns(a, b; widths=[66, 34], kwargs...) |
| 65 | + |
| 66 | +TwoColumnWideRight(a, b; kwargs...) = Columns(a, b; widths=[34, 66], kwargs...) |
| 67 | + |
76 | 68 | """ Provides checkbox to toggle full width and present mode. """
|
77 | 69 | function ChooseDisplayMode(;
|
78 | 70 | wide::Bool=false, present::Bool=false, lang::AbstractLanguage=default_language[]
|
|
0 commit comments