No component selected
From 183d5bc64cee69886389e5ac9b199690c92f45a0 Mon Sep 17 00:00:00 2001
From: Jordan Parker <6415727+byjpr@users.noreply.github.com>
Date: Sat, 23 Apr 2022 14:13:21 +0000
Subject: [PATCH 03/10] Call each function
---
lib/surface/catalogue/extendable_sort.ex | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git a/lib/surface/catalogue/extendable_sort.ex b/lib/surface/catalogue/extendable_sort.ex
index ca02450..ef4345b 100644
--- a/lib/surface/catalogue/extendable_sort.ex
+++ b/lib/surface/catalogue/extendable_sort.ex
@@ -12,4 +12,26 @@ defmodule Surface.Catalogue.ExtendableSort do
Surface.Catalogue.ExtendableSort.Sort.ByTags
])
end
+
+ def run_extendable(ast, module) do
+ case apply(module, :apply, [ast]) do
+ {:ok, returned_ast} ->
+ {:ok, returned_ast}
+
+ {:error, _message} ->
+ {:error, ast}
+
+ _ ->
+ {:error, ast}
+ end
+ end
+
+ def apply_sort_list(init_ast, [head | tail] = _sort_modules) do
+ {_resp_type, ast} = run_extendable(init_ast, head)
+ apply_sort_list(ast, tail)
+ end
+
+ def apply_sort_list(ast, [] = _sort_modules) do
+ ast
+ end
end
From 988ecebe87da5ddd8c4153339c09a5071e2512ba Mon Sep 17 00:00:00 2001
From: Jordan Parker <6415727+byjpr@users.noreply.github.com>
Date: Sat, 23 Apr 2022 14:13:34 +0000
Subject: [PATCH 04/10] Rename to adapters
---
lib/surface/catalogue/extendable_sort.ex | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/lib/surface/catalogue/extendable_sort.ex b/lib/surface/catalogue/extendable_sort.ex
index ef4345b..b4d9261 100644
--- a/lib/surface/catalogue/extendable_sort.ex
+++ b/lib/surface/catalogue/extendable_sort.ex
@@ -1,15 +1,17 @@
defmodule Surface.Catalogue.ExtendableSort do
- def handle(surface_ast) do
+ def handle(surface_ast, config \\ []) do
surface_ast
|> Surface.Catalogue.ExtendableSort.MapHandler.to_extendable_sort()
|> Surface.Catalogue.ExtendableSort.Builder.from_map()
+ |> apply_sort_list(get_sort_config())
end
def get_sort_config do
Application.get_env(:surface_catalogue, :sort, [
- Surface.Catalogue.ExtendableSort.Sort.ByCodeDirectory,
- Surface.Catalogue.ExtendableSort.Sort.ByModule,
- Surface.Catalogue.ExtendableSort.Sort.ByTags
+ Surface.Catalogue.ExtendableSort.Adapters.CategoryFirst,
+ Surface.Catalogue.ExtendableSort.Adapters.ByCodeDirectory,
+ Surface.Catalogue.ExtendableSort.Adapters.ByModuleABC,
+ Surface.Catalogue.ExtendableSort.Adapters.ByTags
])
end
From 991fb48cf9a28f77808d27b9eb91f71c3e0fc3c7 Mon Sep 17 00:00:00 2001
From: Jordan Parker <6415727+byjpr@users.noreply.github.com>
Date: Sat, 23 Apr 2022 14:13:53 +0000
Subject: [PATCH 05/10] Simplify rendering code
---
lib/surface/catalogue/components/extendable_sort/item.ex | 8 ++------
1 file changed, 2 insertions(+), 6 deletions(-)
diff --git a/lib/surface/catalogue/components/extendable_sort/item.ex b/lib/surface/catalogue/components/extendable_sort/item.ex
index 9fdbe0f..545b484 100644
--- a/lib/surface/catalogue/components/extendable_sort/item.ex
+++ b/lib/surface/catalogue/components/extendable_sort/item.ex
@@ -13,13 +13,9 @@ defmodule Surface.Catalogue.Components.ExtendableSort.Item do
def render(assigns, node) when is_list(node) do
~F"""
{#for item <- node}
- {#if item.__struct__ == Surface.Catalogue.ExtendableSort.Category}
-
- {render(assigns, item)}
-
- {#else}
+
{render(assigns, item)}
- {/if}
+
{#else}
{/for}
From 4d0fc57480d997e3c06b24c19782aa15a60170fc Mon Sep 17 00:00:00 2001
From: Jordan Parker <6415727+byjpr@users.noreply.github.com>
Date: Sat, 23 Apr 2022 14:14:29 +0000
Subject: [PATCH 06/10] Create adapter callbacks
---
.../catalogue/extendable_sort/adapters.ex | 24 +++++++++++++++++++
1 file changed, 24 insertions(+)
create mode 100644 lib/surface/catalogue/extendable_sort/adapters.ex
diff --git a/lib/surface/catalogue/extendable_sort/adapters.ex b/lib/surface/catalogue/extendable_sort/adapters.ex
new file mode 100644
index 0000000..48c7880
--- /dev/null
+++ b/lib/surface/catalogue/extendable_sort/adapters.ex
@@ -0,0 +1,24 @@
+defmodule Surface.Catalogue.ExtendableSort.Adapter do
+ @type error :: Exception.t() | String.t()
+
+ @doc """
+ Selective filter `children` in a nested data structure and
+ apply `sort/1` to them.
+ """
+ @callback apply(ast :: any) :: {:ok, any} | {:error, error}
+
+ @doc """
+ Sorting rules
+
+ ```elixir
+ Enum.sort_by(points, fn(p) -> p.points end)
+ ```
+
+ ```elixir
+ Enum.sort_by(points, fn(p) -> {p.points, p.coordinate} end)
+ ```
+
+ https://stackoverflow.com/questions/48310861/sort-list-of-maps-based-on-a-value-within-the-map
+ """
+ @callback sort(ast :: any) :: {:ok, any} | {:error, error}
+end
From 3cc8ef1440e38aadba34caf39c6aa55e16953eef Mon Sep 17 00:00:00 2001
From: Jordan Parker <6415727+byjpr@users.noreply.github.com>
Date: Sat, 23 Apr 2022 14:15:03 +0000
Subject: [PATCH 07/10] Remove sort files
---
.../extendable_sort/sort/by_code_directory.ex | 25 -------------------
.../extendable_sort/sort/by_module_name.ex | 10 --------
.../catalogue/extendable_sort/sort/by_tags.ex | 16 ------------
3 files changed, 51 deletions(-)
delete mode 100644 lib/surface/catalogue/extendable_sort/sort/by_code_directory.ex
delete mode 100644 lib/surface/catalogue/extendable_sort/sort/by_module_name.ex
delete mode 100644 lib/surface/catalogue/extendable_sort/sort/by_tags.ex
diff --git a/lib/surface/catalogue/extendable_sort/sort/by_code_directory.ex b/lib/surface/catalogue/extendable_sort/sort/by_code_directory.ex
deleted file mode 100644
index f894b75..0000000
--- a/lib/surface/catalogue/extendable_sort/sort/by_code_directory.ex
+++ /dev/null
@@ -1,25 +0,0 @@
-defmodule Surface.Catalogue.ExtendableSort.Sort.ByCodeDirectory do
- @moduledoc """
- Sort Module Maps by Code Directory.
-
- for example if you have a code structure inside `lib`
- you would see the same structure reflected in the Component Library.
-
- ```
- surface
- button
- buttongroup
- button-small
- button-large
- pills
- text
- paragraph
- header
- codeblock
- pre
- ```
- """
-
- def sort(components) do
- end
-end
diff --git a/lib/surface/catalogue/extendable_sort/sort/by_module_name.ex b/lib/surface/catalogue/extendable_sort/sort/by_module_name.ex
deleted file mode 100644
index 23e480a..0000000
--- a/lib/surface/catalogue/extendable_sort/sort/by_module_name.ex
+++ /dev/null
@@ -1,10 +0,0 @@
-defmodule Surface.Catalogue.ExtendableSort.Sort.ByModule do
- @moduledoc """
- Sort Module Maps by Module Name.
-
- Default Surface sorting.
- """
-
- def sort(components) do
- end
-end
diff --git a/lib/surface/catalogue/extendable_sort/sort/by_tags.ex b/lib/surface/catalogue/extendable_sort/sort/by_tags.ex
deleted file mode 100644
index 7dc8913..0000000
--- a/lib/surface/catalogue/extendable_sort/sort/by_tags.ex
+++ /dev/null
@@ -1,16 +0,0 @@
-defmodule Surface.Catalogue.ExtendableSort.Sort.ByTags do
- @moduledoc """
- Sort Module Maps by Tags.
-
- Module will be sorted into the catalouge that is titled in `@module_parent`
-
- ```
- defmodule SurfaceUI.Example do
- @module_parent "paragraph"
- end
- ```
- """
-
- def sort(components) do
- end
-end
From 4900a12be3ef2177e09d17ec3c43f2388eb2077e Mon Sep 17 00:00:00 2001
From: Jordan Parker <6415727+byjpr@users.noreply.github.com>
Date: Sat, 23 Apr 2022 14:15:25 +0000
Subject: [PATCH 08/10] Create `category_first` strategy with recurrsive sorts
---
.../adapters/category_first.ex | 32 +++++++++++++++++++
1 file changed, 32 insertions(+)
create mode 100644 lib/surface/catalogue/extendable_sort/adapters/category_first.ex
diff --git a/lib/surface/catalogue/extendable_sort/adapters/category_first.ex b/lib/surface/catalogue/extendable_sort/adapters/category_first.ex
new file mode 100644
index 0000000..ec87dbc
--- /dev/null
+++ b/lib/surface/catalogue/extendable_sort/adapters/category_first.ex
@@ -0,0 +1,32 @@
+defmodule Surface.Catalogue.ExtendableSort.Adapters.CategoryFirst do
+ @moduledoc """
+ Sort Module Maps by Module Name in alphabetical order.
+ """
+ @behaviour Surface.Catalogue.ExtendableSort.Adapter
+
+ @impl true
+ def apply(ast) do
+ {:ok, ast |> sort_children |> sort}
+ end
+
+ def sort_children([head | tail] = _ast) do
+ [sort_children(head) | sort_children(tail)]
+ end
+
+ def sort_children(%Surface.Catalogue.ExtendableSort.Category{} = ast) do
+ Map.put(
+ ast,
+ :children,
+ ast.children |> sort_children |> sort
+ )
+ end
+
+ def sort_children(ast) do
+ ast
+ end
+
+ @impl true
+ def sort(ast) do
+ Enum.sort_by(ast, fn p -> p.__struct__ end)
+ end
+end
From a5401b8708166926ed0cd9cb39635876bdd17aae Mon Sep 17 00:00:00 2001
From: Jordan Parker <6415727+byjpr@users.noreply.github.com>
Date: Sat, 23 Apr 2022 17:49:32 +0000
Subject: [PATCH 09/10] Rename for more descriptive module name
---
.../extendable_sort/adapters/by_catalogue_abc.ex | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
create mode 100644 lib/surface/catalogue/extendable_sort/adapters/by_catalogue_abc.ex
diff --git a/lib/surface/catalogue/extendable_sort/adapters/by_catalogue_abc.ex b/lib/surface/catalogue/extendable_sort/adapters/by_catalogue_abc.ex
new file mode 100644
index 0000000..be6e708
--- /dev/null
+++ b/lib/surface/catalogue/extendable_sort/adapters/by_catalogue_abc.ex
@@ -0,0 +1,16 @@
+defmodule Surface.Catalogue.ExtendableSort.Adapters.ByCatalogueABC do
+ @moduledoc """
+ Sort Catalogues in alphabetical order.
+ """
+ @behaviour Surface.Catalogue.ExtendableSort.Adapter
+
+ @impl true
+ def apply(ast) do
+ {:ok, sort(ast)}
+ end
+
+ @impl true
+ def sort(ast) do
+ Enum.sort_by(ast, fn p -> p.name end)
+ end
+end
From 46a12e511e6099101fd963d2f59e5c2523c45bed Mon Sep 17 00:00:00 2001
From: Jordan Parker <6415727+byjpr@users.noreply.github.com>
Date: Sat, 23 Apr 2022 17:49:57 +0000
Subject: [PATCH 10/10] Remove ByCodeDirectory and ByTags
---
lib/surface/catalogue/extendable_sort.ex | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/lib/surface/catalogue/extendable_sort.ex b/lib/surface/catalogue/extendable_sort.ex
index b4d9261..7cc58db 100644
--- a/lib/surface/catalogue/extendable_sort.ex
+++ b/lib/surface/catalogue/extendable_sort.ex
@@ -9,9 +9,7 @@ defmodule Surface.Catalogue.ExtendableSort do
def get_sort_config do
Application.get_env(:surface_catalogue, :sort, [
Surface.Catalogue.ExtendableSort.Adapters.CategoryFirst,
- Surface.Catalogue.ExtendableSort.Adapters.ByCodeDirectory,
- Surface.Catalogue.ExtendableSort.Adapters.ByModuleABC,
- Surface.Catalogue.ExtendableSort.Adapters.ByTags
+ Surface.Catalogue.ExtendableSort.Adapters.ByCatalogueABC
])
end