Copy FEN to clipboard from the endgame view (#33)

* display fen string under engame data and add hook to copy to clipboard

* split seed data across files
This commit is contained in:
2026-11-03 12:00:35 +01:00
committed by GitHub
parent a5bb1cb670
commit dd060531d3
6 changed files with 104 additions and 26 deletions
+2
View File
@@ -25,10 +25,12 @@ import { LiveSocket } from "phoenix_live_view";
import { hooks as colocatedHooks } from "phoenix-colocated/chesstrainer"; import { hooks as colocatedHooks } from "phoenix-colocated/chesstrainer";
import topbar from "../vendor/topbar"; import topbar from "../vendor/topbar";
import { ChessBoard } from "./hooks/chessBoard"; import { ChessBoard } from "./hooks/chessBoard";
import { CopyFen } from "./hooks/copyFen"
let Hooks = { let Hooks = {
...colocatedHooks, ...colocatedHooks,
ChessBoard, ChessBoard,
CopyFen
}; };
const csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content"); const csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content");
+19
View File
@@ -0,0 +1,19 @@
export const CopyFen = {
mounted() {
this.el.addEventListener("click", e => {
e.preventDefault()
const fen = this.el.dataset.fen
navigator.clipboard.writeText(fen).then(() => {
const originalHTML = this.el.innerHTML
this.el.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4 text-emerald-600 inline" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" /></svg> Copied!`
setTimeout(() => {
this.el.innerHTML = originalHTML
}, 2000)
}).catch(err => {
console.log('Failed to copy FEN: ', err)
})
})
}
}
@@ -13,6 +13,18 @@ defmodule ChesstrainerWeb.EndgameLive.Play do
<:subtitle> <:subtitle>
{@endgame.color |> Atom.to_string() |> String.capitalize()} to move · Rating: {@endgame.rating} · Attempts: {@endgame.times_attempted || {@endgame.color |> Atom.to_string() |> String.capitalize()} to move · Rating: {@endgame.rating} · Attempts: {@endgame.times_attempted ||
0} · Solved: {@endgame.times_solved || 0} 0} · Solved: {@endgame.times_solved || 0}
<p class="text-xs text-gray-500 flex items-center mt-1">
<span>FEN: {@endgame.fen}</span>
<button
id="copy-fen-btn"
phx-hook="CopyFen"
data-fen={@endgame.fen}
class="inline-flex items-center cursor-pointer"
title="Copy FEN to clipboard"
>
<.icon name="hero-document-duplicate" class="w-3.5 h-3.5" />
</button>
</p>
</:subtitle> </:subtitle>
<:actions> <:actions>
<.button navigate={~p"/endgames"}> <.button navigate={~p"/endgames"}>
+10 -26
View File
@@ -10,37 +10,29 @@
# We recommend using the bang functions (`insert!`, `update!` # We recommend using the bang functions (`insert!`, `update!`
# and so on) as they will fail if something goes wrong. # and so on) as they will fail if something goes wrong.
Code.require_file("seeds/endgames.ex", __DIR__)
Code.require_file("seeds/themes.ex", __DIR__)
alias Chesstrainer.Repo alias Chesstrainer.Repo
alias Chesstrainer.Endgames.Endgame alias Chesstrainer.Endgames.Endgame
alias Chesstrainer.Themes.Theme alias Chesstrainer.Themes.Theme
alias Chesstrainer.Repo.Seeds.Endgames
alias Chesstrainer.Repo.Seeds.Themes
now = DateTime.utc_now() |> DateTime.truncate(:second) now = DateTime.utc_now() |> DateTime.truncate(:second)
IO.puts("🌱 Seeding basic endgames into DB...") IO.puts("🌱 Seeding basic endgames into DB...")
endgames = [ endgames = Endgames.all()
%{
fen: "8/8/3k4/8/8/3K1R2/8/8 w - - 0 1",
color: :white,
message: "Rook vs king endgame",
notes: "Basic rook endgame",
result: :win
},
%{
fen: "6k1/5p2/6p1/8/7p/8/6PP/6K1 b - - 0 1",
color: :black,
message: "3 pawns vs 2 pawns",
notes: "https://www.chessgames.com/perl/chessgame?gid=1017147",
result: :win
}
]
Repo.insert_all( Repo.insert_all(
Endgame, Endgame,
Enum.map(endgames, fn endgame -> Enum.map(endgames, fn endgame ->
{:ok, key} = Chesstrainer.Key.from_fen(endgame.fen) fen = Chesstrainer.FEN.normalize(endgame.fen)
{:ok, key} = Chesstrainer.Key.from_fen(fen)
endgame endgame
|> Map.put(:fen, fen)
|> Map.put(:id, Ecto.UUID.generate()) |> Map.put(:id, Ecto.UUID.generate())
|> Map.put(:key, key) |> Map.put(:key, key)
|> Map.merge(%{inserted_at: now, updated_at: now}) |> Map.merge(%{inserted_at: now, updated_at: now})
@@ -52,15 +44,7 @@ IO.puts("✅ Done seeding endgames.")
IO.puts("🌱 Seeding endgame themes into DB...") IO.puts("🌱 Seeding endgame themes into DB...")
theme_names = [ theme_names = Themes.all()
"mate in 2",
"mate in 3",
"Lucena",
"Philidor",
"opposition",
"promotion",
"stalemate"
]
Repo.insert_all( Repo.insert_all(
Theme, Theme,
+48
View File
@@ -0,0 +1,48 @@
defmodule Chesstrainer.Repo.Seeds.Endgames do
def all do
[
%{
fen: "8/8/3k4/8/8/3K1R2/8/8 w - - 0 1",
color: :white,
message: "Rook vs king endgame",
notes: "Basic rook endgame",
result: :win
},
%{
fen: "3k4/2n2B2/1KP5/2B2p2/5b1p/7P/8/8 b - - 0 0",
color: :black,
result: :win
},
%{
fen: "r7/4R2P/3p4/3k1K2/2p5/8/8/8 b - - 0 0",
color: :black,
result: :draw
},
%{
fen: "8/8/5p2/1P1K1k2/8/2r5/8/7R w - - 0 0",
color: :white,
result: :draw
},
%{
fen: "5n2/R7/4pk2/8/5PK1/8/8/8 b - - 0 0",
color: :black,
result: :draw
},
%{
fen: "5k2/R7/3K4/4p3/5P2/8/8/5r2 w - - 0 1",
color: :white,
result: :draw
},
%{
fen: "5k2/8/p7/4K1P1/P4R2/6r1/8/8 b - - 0 1",
color: :black,
result: :draw
},
%{
fen: "8/8/8/1P4p1/5k2/5p2/P6K/8 b - - 0 0",
color: :black,
result: :win
}
]
end
end
+13
View File
@@ -0,0 +1,13 @@
defmodule Chesstrainer.Repo.Seeds.Themes do
def all do
[
"mate in 2",
"mate in 3",
"Lucena",
"Philidor",
"opposition",
"promotion",
"stalemate"
]
end
end