add page builder polish: utility blocks, templates, duplicate
All checks were successful
deploy / deploy (push) Successful in 1m24s
All checks were successful
deploy / deploy (push) Successful in 1m24s
New block types: spacer, divider, button/CTA, video embed (YouTube, Vimeo with privacy-enhanced embeds, fallback for unknown URLs). Page templates (blank, content, landing) shown when creating custom pages. Duplicate page action on admin index with slug deduplication. Fix block picker on shop edit sidebar being cut off on mobile by accounting for bottom nav and making the grid scrollable. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -203,6 +203,30 @@ defmodule Berrypod.Pages do
|
||||
|
||||
def delete_custom_page(%Page{type: "system"}), do: {:error, :system_page}
|
||||
|
||||
@doc """
|
||||
Duplicates a custom page, creating a draft copy with a "-copy" slug suffix.
|
||||
"""
|
||||
def duplicate_custom_page(%Page{type: "custom"} = page) do
|
||||
new_slug = find_available_slug(page.slug <> "-copy")
|
||||
|
||||
create_custom_page(%{
|
||||
"slug" => new_slug,
|
||||
"title" => page.title <> " (copy)",
|
||||
"blocks" => page.blocks,
|
||||
"published" => false,
|
||||
"meta_description" => page.meta_description,
|
||||
"show_in_nav" => false
|
||||
})
|
||||
end
|
||||
|
||||
defp find_available_slug(slug) do
|
||||
if Repo.one(from p in Page, where: p.slug == ^slug, select: p.id) do
|
||||
find_available_slug(slug <> "-2")
|
||||
else
|
||||
slug
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Resets a page to its default block list. Deletes the DB row (if any)
|
||||
and invalidates the cache.
|
||||
|
||||
@@ -147,6 +147,73 @@ defmodule Berrypod.Pages.BlockTypes do
|
||||
settings_schema: [],
|
||||
data_loader: :load_reviews
|
||||
},
|
||||
"spacer" => %{
|
||||
name: "Spacer",
|
||||
icon: "hero-arrows-up-down",
|
||||
allowed_on: :all,
|
||||
settings_schema: [
|
||||
%SettingsField{
|
||||
key: "size",
|
||||
label: "Size",
|
||||
type: :select,
|
||||
options: ~w(small medium large xlarge),
|
||||
default: "medium"
|
||||
}
|
||||
]
|
||||
},
|
||||
"divider" => %{
|
||||
name: "Divider",
|
||||
icon: "hero-minus",
|
||||
allowed_on: :all,
|
||||
settings_schema: [
|
||||
%SettingsField{
|
||||
key: "style",
|
||||
label: "Style",
|
||||
type: :select,
|
||||
options: ~w(line dots fade),
|
||||
default: "line"
|
||||
}
|
||||
]
|
||||
},
|
||||
"button" => %{
|
||||
name: "Button",
|
||||
icon: "hero-cursor-arrow-rays",
|
||||
allowed_on: :all,
|
||||
settings_schema: [
|
||||
%SettingsField{key: "text", label: "Button text", type: :text, default: "Learn more"},
|
||||
%SettingsField{key: "href", label: "Link URL", type: :text, default: "/"},
|
||||
%SettingsField{
|
||||
key: "style",
|
||||
label: "Style",
|
||||
type: :select,
|
||||
options: ~w(primary outline),
|
||||
default: "primary"
|
||||
},
|
||||
%SettingsField{
|
||||
key: "alignment",
|
||||
label: "Alignment",
|
||||
type: :select,
|
||||
options: ~w(left centre right),
|
||||
default: "centre"
|
||||
}
|
||||
]
|
||||
},
|
||||
"video_embed" => %{
|
||||
name: "Video embed",
|
||||
icon: "hero-play",
|
||||
allowed_on: :all,
|
||||
settings_schema: [
|
||||
%SettingsField{key: "url", label: "Video URL", type: :text, default: ""},
|
||||
%SettingsField{key: "caption", label: "Caption", type: :text, default: ""},
|
||||
%SettingsField{
|
||||
key: "aspect_ratio",
|
||||
label: "Aspect ratio",
|
||||
type: :select,
|
||||
options: ~w(16:9 4:3 1:1),
|
||||
default: "16:9"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
# ── PDP blocks ──────────────────────────────────────────────────
|
||||
|
||||
|
||||
@@ -206,6 +206,62 @@ defmodule Berrypod.Pages.Defaults do
|
||||
|
||||
defp blocks(_slug), do: []
|
||||
|
||||
# ── Page templates ─────────────────────────────────────────────
|
||||
|
||||
@doc "Returns available templates for new custom pages."
|
||||
def templates do
|
||||
[
|
||||
%{key: "blank", label: "Blank", description: "Start from scratch"},
|
||||
%{key: "content", label: "Content page", description: "Hero banner + text content"},
|
||||
%{key: "landing", label: "Landing page", description: "Hero, image + text, products, CTA"}
|
||||
]
|
||||
end
|
||||
|
||||
@doc "Returns the starter blocks for a template key."
|
||||
def template_blocks("content") do
|
||||
[
|
||||
block("hero", %{
|
||||
"title" => "Page title",
|
||||
"description" => "A short summary of this page",
|
||||
"variant" => "page"
|
||||
}),
|
||||
block("content_body", %{
|
||||
"content" => "Start writing your content here."
|
||||
})
|
||||
]
|
||||
end
|
||||
|
||||
def template_blocks("landing") do
|
||||
[
|
||||
block("hero", %{
|
||||
"title" => "Your headline here",
|
||||
"description" => "A compelling description that makes visitors want to keep reading.",
|
||||
"cta_text" => "Shop now",
|
||||
"cta_href" => "/collections/all"
|
||||
}),
|
||||
block("image_text", %{
|
||||
"title" => "Tell your story",
|
||||
"description" =>
|
||||
"Use this section to share what makes your products special, your process, or your inspiration."
|
||||
}),
|
||||
block("featured_products", %{
|
||||
"title" => "Featured products",
|
||||
"product_count" => 4,
|
||||
"layout" => "grid",
|
||||
"card_variant" => "default"
|
||||
}),
|
||||
block("button", %{
|
||||
"text" => "View all products",
|
||||
"href" => "/collections/all",
|
||||
"style" => "outline",
|
||||
"alignment" => "centre"
|
||||
}),
|
||||
block("newsletter_card")
|
||||
]
|
||||
end
|
||||
|
||||
def template_blocks(_), do: []
|
||||
|
||||
# ── Helpers ─────────────────────────────────────────────────────
|
||||
|
||||
defp block(type, settings \\ %{}) do
|
||||
|
||||
@@ -2,7 +2,7 @@ defmodule BerrypodWeb.Admin.Pages.CustomForm do
|
||||
use BerrypodWeb, :live_view
|
||||
|
||||
alias Berrypod.Pages
|
||||
alias Berrypod.Pages.Page
|
||||
alias Berrypod.Pages.{Defaults, Page}
|
||||
|
||||
@impl true
|
||||
def mount(params, _session, socket) do
|
||||
@@ -16,6 +16,7 @@ defmodule BerrypodWeb.Admin.Pages.CustomForm do
|
||||
|> assign(:page_title, "New page")
|
||||
|> assign(:page_struct, %Page{})
|
||||
|> assign(:slug_touched, false)
|
||||
|> assign(:selected_template, "blank")
|
||||
|> assign(:form, to_form(changeset))
|
||||
end
|
||||
|
||||
@@ -53,12 +54,20 @@ defmodule BerrypodWeb.Admin.Pages.CustomForm do
|
||||
{:noreply, assign(socket, form: form, slug_touched: slug_touched)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("select_template", %{"key" => key}, socket) do
|
||||
{:noreply, assign(socket, :selected_template, key)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("save", %{"page" => params}, socket) do
|
||||
save_page(socket, socket.assigns.live_action, params)
|
||||
end
|
||||
|
||||
defp save_page(socket, :new, params) do
|
||||
template_blocks = Defaults.template_blocks(socket.assigns.selected_template)
|
||||
params = Map.put(params, "blocks", template_blocks)
|
||||
|
||||
case Pages.create_custom_page(params) do
|
||||
{:ok, page} ->
|
||||
{:noreply,
|
||||
@@ -120,6 +129,25 @@ defmodule BerrypodWeb.Admin.Pages.CustomForm do
|
||||
{if @live_action == :new, do: "New page", else: "Page settings"}
|
||||
</.header>
|
||||
|
||||
<div :if={@live_action == :new} class="template-picker">
|
||||
<p class="template-picker-label">Start from a template</p>
|
||||
<div class="template-picker-cards">
|
||||
<button
|
||||
:for={tmpl <- Defaults.templates()}
|
||||
type="button"
|
||||
phx-click="select_template"
|
||||
phx-value-key={tmpl.key}
|
||||
class={[
|
||||
"template-card",
|
||||
assigns[:selected_template] == tmpl.key && "template-card-selected"
|
||||
]}
|
||||
>
|
||||
<span class="template-card-name">{tmpl.label}</span>
|
||||
<span class="template-card-desc">{tmpl.description}</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<.form
|
||||
for={@form}
|
||||
id="custom-page-form"
|
||||
|
||||
@@ -40,6 +40,26 @@ defmodule BerrypodWeb.Admin.Pages.Index do
|
||||
end
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("duplicate_custom_page", %{"slug" => slug}, socket) do
|
||||
case Pages.get_page_struct(slug) do
|
||||
%{type: "custom"} = page ->
|
||||
case Pages.duplicate_custom_page(page) do
|
||||
{:ok, copy} ->
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(:custom_pages, Pages.list_custom_pages())
|
||||
|> put_flash(:info, "\"#{copy.title}\" created as draft")}
|
||||
|
||||
{:error, _} ->
|
||||
{:noreply, put_flash(socket, :error, "Failed to duplicate page")}
|
||||
end
|
||||
|
||||
_ ->
|
||||
{:noreply, put_flash(socket, :error, "Page not found")}
|
||||
end
|
||||
end
|
||||
|
||||
@impl true
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
@@ -101,6 +121,14 @@ defmodule BerrypodWeb.Admin.Pages.Index do
|
||||
</span>
|
||||
<.icon name="hero-chevron-right-mini" class="size-4 page-card-arrow" />
|
||||
</.link>
|
||||
<button
|
||||
phx-click="duplicate_custom_page"
|
||||
phx-value-slug={page.slug}
|
||||
class="page-card-action"
|
||||
aria-label={"Duplicate #{page.title}"}
|
||||
>
|
||||
<.icon name="hero-document-duplicate" class="size-4" />
|
||||
</button>
|
||||
<button
|
||||
phx-click="delete_custom_page"
|
||||
phx-value-slug={page.slug}
|
||||
|
||||
@@ -965,6 +965,87 @@ defmodule BerrypodWeb.PageRenderer do
|
||||
"""
|
||||
end
|
||||
|
||||
# ── Utility blocks ──────────────────────────────────────────────
|
||||
|
||||
defp render_block(%{block: %{"type" => "spacer"}} = assigns) do
|
||||
size = get_in(assigns.block, ["settings", "size"]) || "medium"
|
||||
assigns = assign(assigns, :size, size)
|
||||
|
||||
~H"""
|
||||
<div class="block-spacer" data-size={@size} aria-hidden="true"></div>
|
||||
"""
|
||||
end
|
||||
|
||||
defp render_block(%{block: %{"type" => "divider"}} = assigns) do
|
||||
style = get_in(assigns.block, ["settings", "style"]) || "line"
|
||||
assigns = assign(assigns, :style, style)
|
||||
|
||||
~H"""
|
||||
<hr class="block-divider" data-style={@style} />
|
||||
"""
|
||||
end
|
||||
|
||||
defp render_block(%{block: %{"type" => "button"}} = assigns) do
|
||||
settings = assigns.block["settings"] || %{}
|
||||
|
||||
assigns =
|
||||
assigns
|
||||
|> assign(:text, settings["text"] || "Learn more")
|
||||
|> assign(:href, settings["href"] || "/")
|
||||
|> assign(:btn_style, settings["style"] || "primary")
|
||||
|> assign(:alignment, settings["alignment"] || "centre")
|
||||
|
||||
~H"""
|
||||
<div class="block-button" data-align={@alignment}>
|
||||
<.link
|
||||
navigate={@href}
|
||||
class={if @btn_style == "outline", do: "themed-button-outline", else: "themed-button"}
|
||||
>
|
||||
{@text}
|
||||
</.link>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
defp render_block(%{block: %{"type" => "video_embed"}} = assigns) do
|
||||
settings = assigns.block["settings"] || %{}
|
||||
url = settings["url"] || ""
|
||||
caption = settings["caption"] || ""
|
||||
aspect_ratio = settings["aspect_ratio"] || "16:9"
|
||||
|
||||
{provider, embed_url} = parse_video_url(url)
|
||||
|
||||
assigns =
|
||||
assigns
|
||||
|> assign(:embed_url, embed_url)
|
||||
|> assign(:provider, provider)
|
||||
|> assign(:caption, caption)
|
||||
|> assign(:aspect_ratio, aspect_ratio)
|
||||
|> assign(:raw_url, url)
|
||||
|
||||
~H"""
|
||||
<div class="page-container">
|
||||
<div :if={@provider != :unknown} class="video-embed" data-ratio={@aspect_ratio}>
|
||||
<iframe
|
||||
src={@embed_url}
|
||||
frameborder="0"
|
||||
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
|
||||
allowfullscreen
|
||||
loading="lazy"
|
||||
title={if @caption != "", do: @caption, else: "Embedded video"}
|
||||
>
|
||||
</iframe>
|
||||
</div>
|
||||
<p :if={@provider == :unknown && @raw_url != ""} class="video-embed-fallback">
|
||||
<a href={@raw_url} target="_blank" rel="noopener noreferrer">
|
||||
{if @caption != "", do: @caption, else: "Watch video"}
|
||||
</a>
|
||||
</p>
|
||||
<p :if={@caption != "" && @provider != :unknown} class="video-embed-caption">{@caption}</p>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
# ── Fallback ────────────────────────────────────────────────────
|
||||
|
||||
defp render_block(assigns) do
|
||||
@@ -1073,6 +1154,24 @@ defmodule BerrypodWeb.PageRenderer do
|
||||
end
|
||||
end
|
||||
|
||||
@youtube_re ~r/(?:youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/embed\/)([a-zA-Z0-9_-]{11})/
|
||||
@vimeo_re ~r/vimeo\.com\/(?:video\/)?(\d+)/
|
||||
|
||||
defp parse_video_url(url) when is_binary(url) do
|
||||
cond do
|
||||
match = Regex.run(@youtube_re, url) ->
|
||||
{:youtube, "https://www.youtube-nocookie.com/embed/#{Enum.at(match, 1)}"}
|
||||
|
||||
match = Regex.run(@vimeo_re, url) ->
|
||||
{:vimeo, "https://player.vimeo.com/video/#{Enum.at(match, 1)}?dnt=1"}
|
||||
|
||||
true ->
|
||||
{:unknown, nil}
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_video_url(_), do: {:unknown, nil}
|
||||
|
||||
def format_order_status("unfulfilled"), do: "Being prepared"
|
||||
def format_order_status("submitted"), do: "Sent to printer"
|
||||
def format_order_status("processing"), do: "In production"
|
||||
|
||||
Reference in New Issue
Block a user