Skip to content
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
66 changes: 59 additions & 7 deletions lib/devils_dictionary_web/components/source_badge.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,35 @@ defmodule DevilsDictionaryWeb.SourceBadge do
attr :sources, :list, required: true
attr :class, :any, default: nil

attr :cap, :integer,
default: 12,
doc: "how many badges the row shows before the rest fold behind a +N"

def stack(assigns) do
assigns = assign(assigns, :count, length(assigns.sources))
{shown, rest} = Enum.split(assigns.sources, assigns.cap)

assigns =
assigns
|> assign(:count, length(assigns.sources))
|> assign(:shown, shown)
|> assign(:rest, rest)
|> assign(:shown_count, length(shown))

~H"""
<section :if={@sources != []} id={@id} class={@class} aria-labelledby={"#{@id}-heading"}>
<h2 id={"#{@id}-heading"} class="text-base/8 font-medium text-mist-950 dark:text-white">
Sources
</h2>
<%!-- A ring in the page's own colour is what makes the overlap read as
a stack rather than a smear; `-space-x-1.5` is the overlap. The
row wraps rather than capping: the widest page measured holds
fifteen, and sixteen fit the rail at 22.5 rem. --%>
a stack rather than a smear; `-space-x-1.5` is the overlap. Twelve
on the row and the rest behind a +N (#162): `oyster` holds
seventeen, which wraps in the 22.5 rem rail and would wrap at
fifteen on a phone. The +N is a `<details>` summary, so it opens
without JavaScript and in the dead render, and its title lists
who is folded for a pointer that only hovers. Tier then slug is
the order already, so the fold takes the tail of the 📱 sources. --%>
<ul role="list" class="mt-2 flex flex-wrap items-center gap-y-2 pl-1 -space-x-1.5">
<li :for={{source, index} <- Enum.with_index(@sources)} class="group/badge relative">
<li :for={{source, index} <- Enum.with_index(@shown)} class="group/badge relative">
<a
id={"#{@id}-#{source.slug}"}
href={source.anchor}
Expand All @@ -137,15 +152,52 @@ defmodule DevilsDictionaryWeb.SourceBadge do
role="tooltip"
class={[
"pointer-events-none absolute bottom-full z-20 mb-2 rounded-md bg-mist-950 px-2 py-1 text-xs whitespace-nowrap text-white opacity-0 group-focus-within/badge:opacity-100 group-hover/badge:opacity-100 dark:bg-white dark:text-mist-950",
index < div(@count + 1, 2) && "left-0",
index >= div(@count + 1, 2) && "right-0"
index < div(@shown_count + 1, 2) && "left-0",
index >= div(@shown_count + 1, 2) && "right-0"
]}
>
<span :if={source.tier} aria-hidden="true" class="mr-1">{tier_glyph(source.tier)}</span>{short_name(
source.name
)}
</span>
</li>
<li :if={@rest != []} class="relative">
<details id={"#{@id}-more"} class="group/more">
<%!-- Anchored to its right edge and opening upward. The +N is the
last badge on the row, so a panel that grows leftward stays
inside the rail on a phone where one that grew rightward
would leave the viewport; and it opens upward because the
stack is the last thing in the rail and the kit's main is
overflow-clip — a panel that opened downward was cut off at
the footer on a phone. --%>
<summary
id={"#{@id}-more-summary"}
title={Enum.map_join(@rest, ", ", &short_name(&1.name))}
class="block cursor-pointer list-none rounded-full ring-2 ring-mist-50 transition-transform hover:z-10 hover:scale-110 focus-visible:z-10 focus-visible:outline-2 focus-visible:outline-offset-2 dark:ring-mist-950 [&::-webkit-details-marker]:hidden"
>
<span class="inline-flex size-7 items-center justify-center rounded-full bg-mist-950/5 text-[0.6875rem]/7 font-medium tabular-nums text-mist-700 outline-1 -outline-offset-1 outline-mist-950/10 select-none group-open/more:bg-mist-950/10 dark:bg-white/10 dark:text-mist-200 dark:outline-white/10">
+{length(@rest)}
</span>
<span class="sr-only">{length(@rest)} more sources</span>
</summary>
<ul
role="list"
class="absolute right-0 bottom-full z-20 mb-2 w-56 rounded-lg bg-mist-50 p-1.5 shadow-lg outline-1 -outline-offset-1 outline-mist-950/10 dark:bg-mist-900 dark:shadow-none dark:outline-white/10"
>
<li :for={source <- @rest}>
<a
id={"#{@id}-#{source.slug}"}
href={source.anchor}
title={source.name}
class="flex items-center gap-2 rounded-md px-2 py-1 text-base/7 text-mist-950 hover:bg-mist-950/5 sm:text-sm/7 dark:text-white dark:hover:bg-white/10"
>
<.badge source={source} decorative />
<span class="truncate">{short_name(source.name)}</span>
</a>
</li>
</ul>
</details>
</li>
</ul>
<p id={"#{@id}-count"} class="mt-2 text-base/7 tabular-nums text-mist-500 sm:text-sm/7">
{@count} {if @count == 1, do: "source", else: "sources"} on this page
Expand Down
24 changes: 16 additions & 8 deletions lib/devils_dictionary_web/components/word.ex
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,23 @@ defmodule DevilsDictionaryWeb.Word do
id="sources"
class="text-base/7 font-medium text-mist-700 sm:text-sm/7 dark:text-mist-400"
>
<span :if={length(@sources) > 1}>Defined here by {count(@sources, "source")}</span>
<span :if={length(@sources) > 1}>Defined here by {dictionaries(@sources)}</span>
<span :if={match?([_one], @sources)} id="one-source">
One source so far · {Enum.join(@sources, " · ")}
One dictionary so far · {Enum.join(@sources, " · ")}
</span>
</p>
"""
end

defp count([_one], noun), do: "1 #{noun}"
defp count(list, noun), do: "#{length(list)} #{noun}s"
@doc """
*4 dictionaries*, or *1 dictionary*. Not *sources*: since #152 the rail's
stack counts every source on the page — the thing's encyclopedia rows, the
Crowd card, each shelf's contributors — and *17 sources* beside *4 sources*
was two numbers wearing one word (#162). What this counts is what #133 R3
left in the slab when the article moved out: the dictionaries.
"""
def dictionaries([_one]), do: "1 dictionary"
def dictionaries(list), do: "#{length(list)} dictionaries"

@doc """
A word the index knows and no source has been asked about yet (#71 §2.7).
Expand Down Expand Up @@ -479,9 +486,10 @@ defmodule DevilsDictionaryWeb.Word do
do: headword.forms != [] or headword.etymologies != []

@doc """
The three things this page can count before it renders: how many sources have
spoken, how many senses they filed between them, and how much of the world
came back. The kit's `stat/1` shape, at the kit's own scale.
The things this page can count before it renders: how many dictionaries have
spoken and how many senses they filed between them. *Dictionaries*, not
*sources* — the stack under the related words counts sources, and it counts
every one on the page (#162). The kit's `stat/1` shape, at the kit's own scale.
"""
attr :page, :map, required: true
attr :sources, :list, default: []
Expand All @@ -501,7 +509,7 @@ defmodule DevilsDictionaryWeb.Word do
{@count}
</div>
<p class="mt-1 text-base/6 text-mist-700 sm:text-sm/6 dark:text-mist-400">
{if @count == 1, do: "source", else: "sources"}
{if @count == 1, do: "dictionary", else: "dictionaries"}
</p>
</div>
<div class="rounded-xl bg-mist-950/2.5 p-4 dark:bg-white/5">
Expand Down
4 changes: 3 additions & 1 deletion lib/devils_dictionary_web/live/word_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -594,10 +594,12 @@ defmodule DevilsDictionaryWeb.WordLive do

# "5 sources · 9 entries": a source that filed a noun and a verb is one
# source, and the entries are counted as what they are.
# *4 dictionaries · 8 entries*. The word is `Word.dictionaries/1`'s, for the
# reason given there: *sources* is the stack's word and the stack's number.
defp count_label(sources, cards) do
s = length(sources)
e = length(cards)
base = "#{s} #{if s == 1, do: "source", else: "sources"}"
base = Word.dictionaries(sources)
if e > s, do: "#{base} · #{e} entries", else: base
end

Expand Down
26 changes: 26 additions & 0 deletions test/devils_dictionary_web/components/source_badge_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,32 @@ defmodule DevilsDictionaryWeb.SourceBadgeTest do
assert html =~ ~r/role="tooltip"[^>]*>\s*<span[^>]*>👑<\/span>\s*Samuel Johnson\s*</
end

test "folds everything past the cap behind a +N that opens in place (#162)" do
sources =
for n <- 1..15 do
%{slug: "s#{n}", name: "Source #{n}", tier: :plebs, logo: nil, anchor: "#card-s#{n}"}
end

html = render_component(&SourceBadge.stack/1, id: "page-sources", sources: sources)

# Twelve on the row, the rest behind the fold; every one still a link.
for n <- 1..15, do: assert(html =~ ~s(id="page-sources-s#{n}"))
assert html =~ ~s(id="page-sources-more")
assert html =~ ~r/>\s*\+3\s*</
# Hover says who is folded, and the count is the whole page's.
assert html =~ ~s(title="Source 13, Source 14, Source 15")
assert html =~ "15 sources on this page"

# Twelve or fewer: no fold at all.
twelve =
render_component(&SourceBadge.stack/1,
id: "page-sources",
sources: Enum.take(sources, 12)
)

refute twelve =~ "page-sources-more"
end

test "draws nothing for a page with no sources" do
assert render_component(&SourceBadge.stack/1, id: "page-sources", sources: []) == ""
end
Expand Down
5 changes: 2 additions & 3 deletions test/devils_dictionary_web/live/animals_absent_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,11 @@ defmodule DevilsDictionaryWeb.AnimalsAbsentTest do
end

test "an animal word keeps its page, its sources and its provenance", ctx do
{:ok, _live, html} = live(ctx.conn, ~p"/define/cat")
{:ok, live, html} = live(ctx.conn, ~p"/define/cat")

assert html =~ "A soft automaton."
assert html =~ "a feline"
assert html =~ ~s(id="sources")
assert html =~ "Defined here by 2 sources"
assert has_element?(live, "#sources", "Defined here by 2 dictionaries")
end

test "and by its canonical address too", ctx do
Expand Down
15 changes: 6 additions & 9 deletions test/devils_dictionary_web/live/word_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,10 @@ defmodule DevilsDictionaryWeb.WordLiveTest do
quark = word!(ctx, "quark", ~w(wordnet), scope: nil)
sense!(ctx, quark, "wordnet", group_key: "oewn-quark-n", gloss: "an elementary particle")

{:ok, _live, html} = live(ctx.conn, ~p"/define/quark")
{:ok, live, html} = live(ctx.conn, ~p"/define/quark")

assert html =~ ~s(id="sources")
assert html =~ ~s(id="one-source")
assert html =~ "One source so far"
assert html =~ "Open English WordNet"
assert has_element?(live, "#one-source", "One dictionary so far")
assert has_element?(live, "#one-source", "Open English WordNet")
refute html =~ "Animals"
end

Expand All @@ -260,11 +258,10 @@ defmodule DevilsDictionaryWeb.WordLiveTest do
sense!(ctx, cat, "wordnet", group_key: "oewn-cat-n", gloss: "a feline")
entry!(ctx, cat, "bierce", headword: "CAT", pos: "n", body: "A soft automaton.", year: 1911)

{:ok, _live, html} = live(ctx.conn, ~p"/define/cat")
{:ok, live, _html} = live(ctx.conn, ~p"/define/cat")

assert html =~ ~s(id="sources")
assert html =~ "Defined here by 2 sources"
refute html =~ ~s(id="one-source")
assert has_element?(live, "#sources", "Defined here by 2 dictionaries")
refute has_element?(live, "#one-source")
end

test "a bare row has no source line to print and does not invent one", ctx do
Expand Down