Spaces:
Sleeping
Sleeping
File size: 6,367 Bytes
e82864c 755e340 e82864c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# src/app/viewers.py
import json
from pathlib import Path
from typing import Dict, List
from .config import get_user_dir
from .flashcards_tools import load_deck
def _build_flipbook_html(deck_name: str, cards: List[Dict]) -> str:
"""
Builds a simple HTML+JS flip-style viewer for a deck of cards.
Front = card['front'], Back = card['back'].
"""
js_cards = json.dumps(
[
{"front": c.get("front", ""), "back": c.get("back", "")}
for c in cards
],
ensure_ascii=False,
)
html = f"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Flashcards โ {deck_name}</title>
<style>
body {{
background: #111;
color: #eee;
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
padding: 1.5rem;
}}
.wrapper {{
max-width: 700px;
margin: 0 auto;
}}
h1 {{
text-align: center;
margin-bottom: 1rem;
}}
.card-container {{
perspective: 1000px;
margin-bottom: 1rem;
}}
.card {{
position: relative;
width: 100%;
height: 250px;
border-radius: 16px;
background: #222;
box-shadow: 0 8px 15px rgba(0,0,0,0.4);
transition: transform 0.6s;
transform-style: preserve-3d;
cursor: pointer;
}}
.card.flipped {{
transform: rotateY(180deg);
}}
.card-face {{
position: absolute;
inset: 0;
border-radius: 16px;
backface-visibility: hidden;
display: flex;
align-items: center;
justify-content: center;
padding: 1rem;
}}
.card-face.front {{
background: #333;
}}
.card-face.back {{
background: #1a73e8;
transform: rotateY(180deg);
}}
.card-text {{
font-size: 2.2rem;
text-align: center;
word-wrap: break-word;
}}
.controls {{
display: flex;
justify-content: center;
gap: 0.75rem;
margin-bottom: 0.5rem;
}}
button {{
background: #333;
color: #eee;
border-radius: 999px;
border: none;
padding: 0.5rem 1rem;
font-size: 0.95rem;
cursor: pointer;
transition: background 0.2s ease, transform 0.1s ease;
}}
button:hover {{
background: #444;
transform: translateY(-1px);
}}
.meta {{
text-align: center;
margin-top: 0.25rem;
font-size: 0.9rem;
color: #ccc;
}}
.badge {{
display: inline-block;
padding: 0.1rem 0.75rem;
border-radius: 999px;
font-size: 0.8rem;
background: #555;
margin-left: 0.5rem;
}}
</style>
</head>
<body>
<div class="wrapper">
<h1>{deck_name}</h1>
<div class="card-container">
<div class="card" id="card">
<div class="card-face front">
<div class="card-text" id="cardFront"></div>
</div>
<div class="card-face back">
<div class="card-text" id="cardBack"></div>
</div>
</div>
</div>
<div class="controls">
<button id="prevBtn">โฎ Prev</button>
<button id="flipBtn">๐ Flip</button>
<button id="nextBtn">Next โญ</button>
<button id="shuffleBtn">๐ Shuffle</button>
</div>
<div class="meta">
<span id="cardIndex">Card 0 / 0</span>
<span class="badge" id="sideLabel">Front</span>
</div>
</div>
<script>
const cards = {js_cards};
let currentIndex = 0;
let isFlipped = false;
const cardEl = document.getElementById('card');
const frontEl = document.getElementById('cardFront');
const backEl = document.getElementById('cardBack');
const indexEl = document.getElementById('cardIndex');
const sideLabelEl = document.getElementById('sideLabel');
function renderCard() {{
if (!cards.length) {{
frontEl.textContent = "(No cards)";
backEl.textContent = "";
indexEl.textContent = "Card 0 / 0";
sideLabelEl.textContent = "Front";
cardEl.classList.remove('flipped');
return;
}}
const card = cards[currentIndex];
frontEl.textContent = card.front || "";
backEl.textContent = card.back || "";
indexEl.textContent = "Card " + (currentIndex + 1) + " / " + cards.length;
sideLabelEl.textContent = isFlipped ? "Back" : "Front";
}}
function flipCard() {{
if (!cards.length) return;
isFlipped = !isFlipped;
if (isFlipped) {{
cardEl.classList.add('flipped');
}} else {{
cardEl.classList.remove('flipped');
}}
sideLabelEl.textContent = isFlipped ? "Back" : "Front";
}}
function nextCard() {{
if (!cards.length) return;
currentIndex = (currentIndex + 1) % cards.length;
isFlipped = false;
cardEl.classList.remove('flipped');
renderCard();
}}
function prevCard() {{
if (!cards.length) return;
currentIndex = (currentIndex - 1 + cards.length) % cards.length;
isFlipped = false;
cardEl.classList.remove('flipped');
renderCard();
}}
function shuffleCards() {{
for (let i = cards.length - 1; i > 0; i--) {{
const j = Math.floor(Math.random() * (i + 1));
[cards[i], cards[j]] = [cards[j], cards[i]];
}}
currentIndex = 0;
isFlipped = false;
cardEl.classList.remove('flipped');
renderCard();
}}
cardEl.addEventListener('click', flipCard);
document.getElementById('flipBtn').addEventListener('click', flipCard);
document.getElementById('nextBtn').addEventListener('click', nextCard);
document.getElementById('prevBtn').addEventListener('click', prevCard);
document.getElementById('shuffleBtn').addEventListener('click', shuffleCards);
document.addEventListener('keydown', (e) => {{
if (!cards.length) return;
if (e.code === "ArrowRight") nextCard();
else if (e.code === "ArrowLeft") prevCard();
else if (e.code === "Space") {{
e.preventDefault();
flipCard();
}}
}});
renderCard();
</script>
</body>
</html>
"""
return html
def generate_flashcard_viewer_for_user(username: str, deck_path: Path) -> Path:
"""
Generates an HTML flipbook viewer for the given deck in the user's
/viewers directory, and returns the path to the HTML file.
"""
deck = load_deck(deck_path)
deck_name = deck.get("name", deck_path.stem)
cards = deck.get("cards", [])
html_str = _build_flipbook_html(deck_name, cards)
user_dir = get_user_dir(username)
viewer_dir = user_dir / "viewers"
viewer_dir.mkdir(parents=True, exist_ok=True)
safe_name = deck_path.stem
out_path = viewer_dir / f"{safe_name}_viewer.html"
out_path.write_text(html_str, encoding="utf-8")
return out_path
|