diff --git a/Project.toml b/Project.toml index 3e704acd..1097f2cc 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Stipple" uuid = "4acbeb90-81a0-11ea-1966-bdaff8155998" authors = ["Adrian "] -version = "0.27.14" +version = "0.27.16" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" @@ -27,7 +27,7 @@ Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" DataFrames = "1" Dates = "1.6" FilePathsBase = "0.9" -Genie = "5.20" +Genie = "5.21.1" GenieSession = "1" GenieSessionFileSession = "1" JSON = "0.20, 0.21" diff --git a/src/Layout.jl b/src/Layout.jl index af9ea60a..e52a02cf 100644 --- a/src/Layout.jl +++ b/src/Layout.jl @@ -7,7 +7,7 @@ module Layout using Genie, Stipple -export layout +export layout, add_css, remove_css export page, app, row, column, cell, container, flexgrid_kwargs export theme @@ -300,5 +300,47 @@ function theme(; core_theme::Bool = true) :: Vector{String} output end +""" + add_css(css::Function; update = true) + +Add a css function to the `THEMES`. + +### Params +* `css::Function` - a function that results in a vector of style elements +* `update` - determines, whether existing style sheets with the same name shall be removed + +### Example +```julia +# css to remove the stipple-core color format of q-table rows +# (this will enable font color setting by the attribute `table-class`) + +function mycss() + [ + style(\"\"\" + .stipple-core .q-table tbody tr { color: inherit; } + \"\"\") + ] +end + +add_css(mycss) +``` +` +""" +function add_css(css::Function; update = true) + # removal can only be done by name, as the old function has already been overwritten + update && remove_css(css::Function, byname = true) + push!(Stipple.Layout.THEMES, css) +end + +""" + remove_css(css::Function, byname::Bool = false) + +Remove a stylesheet function from the stack (`Stipple.Layout.THEMES`) +If called with `byname = true`, the function will be identified by name rather than by the function itself. +""" +function remove_css(css::Function; byname::Bool = false) + inds = byname ? nameof.(Stipple.Layout.THEMES) .== nameof(css) : Stipple.Layout.THEMES .== css + deleteat!(Stipple.Layout.THEMES, inds) +end end diff --git a/src/Stipple.jl b/src/Stipple.jl index d34440f1..a9fa4040 100644 --- a/src/Stipple.jl +++ b/src/Stipple.jl @@ -74,7 +74,7 @@ using Logging, Mixers, Random, Reexport, Dates, Tables @reexport using Observables @reexport @using_except Genie: download import Genie.Router.download -@reexport @using_except Genie.Renderer.Html: mark, div, time, view, render, Headers +@reexport @using_except Genie.Renderers.Html: mark, div, time, view, render, Headers, menu const htmldiv = Html.div export render, htmldiv, js_attr @reexport using JSON3 diff --git a/test/runtests.jl b/test/runtests.jl index 808ebf93..a2c932b5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -394,4 +394,25 @@ end @test df[end, 1] == 3 @test df[end, end] == 14 @test df[:, end] == 12:14 +end + +@testset "adding and removing stylesheets" begin + function my_css() + [style(""" + .stipple-core .q-table tbody tr { color: inherit; } + """)] + end + + add_css(my_css) + @test Stipple.Layout.THEMES[end] == my_css + + n = length(Stipple.Layout.THEMES) + remove_css(my_css) + @test length(Stipple.Layout.THEMES) == n - 1 + @test findfirst(==(my_css), Stipple.Layout.THEMES) === nothing + + add_css(my_css) + @test Stipple.Layout.THEMES[end] == my_css + remove_css(my_css, byname = true) + @test findfirst(==(my_css), Stipple.Layout.THEMES) === nothing end \ No newline at end of file