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

Various fixes #84

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 37 additions & 15 deletions lib/live_view_native/platforms.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,7 @@ defmodule LiveViewNative.Platforms do

@default_platforms [LiveViewNative.Platforms.HTML]

@env_platforms :live_view_native
|> Application.compile_env(:plugins, [])
|> Enum.flat_map(fn plugin_mod -> apply(plugin_mod, :platforms, []) end)
|> Enum.concat(@default_platforms)
|> Enum.uniq()
|> Enum.map(fn platform_mod ->
platform_config = Application.compile_env(:live_view_native, platform_mod, [])
platform_params = Enum.into(platform_config, %{})

{platform_mod, platform_params}
end)
|> Enum.into(%{})
@platforms_table :live_view_native_platforms

@doc """
Provides configuration constants about all platforms supported by an
Expand All @@ -24,9 +13,25 @@ defmodule LiveViewNative.Platforms do
session originates from.
"""
def env_platforms do
@env_platforms
|> Enum.map(&expand_env_platform/1)
|> Enum.into(%{})
case fetch_platforms() do
:none ->
:live_view_native
|> Application.get_env(:plugins, [])
|> Enum.flat_map(fn plugin_mod -> apply(plugin_mod, :platforms, []) end)
|> Enum.concat(@default_platforms)
|> Enum.uniq()
|> Enum.map(fn platform_mod ->
platform_config = Application.get_env(:live_view_native, platform_mod, [])
platform_params = Enum.into(platform_config, %{})

{platform_mod, platform_params}
end)
|> Enum.into(%{})
|> Enum.map(&expand_env_platform/1)
|> Enum.into(%{})
|> store_platforms()
platforms -> platforms
end
end

def env_platform(platform_id) do
Expand All @@ -36,6 +41,23 @@ defmodule LiveViewNative.Platforms do

###

defp fetch_platforms do
case :ets.info(@platforms_table) do
:undefined -> :ets.new(@platforms_table, [:named_table, :public])
_ -> nil
end

case :ets.lookup(@platforms_table, :all) do
[all: platforms] -> platforms
_ -> :none
end
end

defp store_platforms(platforms) do
:ets.insert(:live_view_native_platforms, {:all, platforms})
platforms
end

defp expand_env_platform({platform_mod, %{} = platform_params}) do
platform_config = struct!(platform_mod, platform_params)

Expand Down
53 changes: 41 additions & 12 deletions lib/live_view_native/templates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,51 @@ defmodule LiveViewNative.Templates do
end

defp extract_all_class_names(doc) do
Enum.flat_map(doc, &extract_class_names/1)
doc
|> Floki.traverse_and_update(%{}, &extract_class_names/2)
|> elem(1)
|> Map.keys()
|> IO.inspect()
end

defp extract_class_names({_key, _, node}) do
case node do
%{attributes: [_ | _] = attributes} ->
attributes
|> Enum.into(%{})
|> Map.get("class", "")
|> String.split(" ")
|> Enum.filter(&(&1 != ""))
defp extract_class_names(node, acc) do
new_acc =
node
|> Floki.attribute("class")
|> Enum.reduce(acc, fn(class_name, acc) ->
Map.put(acc, class_name, true)
end)

_ ->
[]
end
{nil, new_acc}
end
# defp extract_all_class_names(nodes, acc \\ %{})

# defp extract_all_class_names([], acc), do: acc
# # defp extract_all_class_names(nodes, acc) when is_list(nodes) do

# # Enum.flat_map(doc, &extract_class_names/1)
# # end

# # defp extract_all_class_names(node, acc) when is_tuple(node) do
# # extract_class_names(node, acc)
# # end

# defp extract_class_names(doc) when is_binary(doc), do: []
# defp extract_class_names({_key, _, node} = other) do
# require IEx
# IEx.pry()
# case node do
# %{attributes: [_ | _] = attributes} ->
# attributes
# |> Enum.into(%{})
# |> Map.get("class", "")
# |> String.split(" ")
# |> Enum.filter(&(&1 != ""))

# _ ->
# []
# end
# end

defp module_has_stylesheet?(module) do
:functions
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ defmodule LiveViewNative.MixProject do
{:phoenix_live_view, ">= 0.18.0"},
{:jason, "~> 1.2"},
{:plug_cowboy, "~> 2.5"},
{:floki, ">= 0.30.0", only: :test},
{:floki, ">= 0.30.0"},
{:ex_doc, "~> 0.24", only: :dev, runtime: false},
{:makeup_eex, ">= 0.1.1", only: :dev, runtime: false},
{:owl, "~> 0.8", runtime: false},
Expand Down
Loading