Repository navigation
Expand file tree
/
Copy pathprogram.js
More file actions
390 lines (368 loc) · 18.5 KB
/
Copy pathprogram.js
File metadata and controls
390 lines (368 loc) · 18.5 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
* PyWebLib program page (/game/?id=<uuid>): a shareable page for a single
* shared program. Shows a preview you can Play inline (no Playground needed),
* the description and code, an upvote and comments, plus "Open in the
* Playground" (which binds the editor to this program) and "Copy link".
*/
(function () {
"use strict";
const PWL = window.PWL || {};
const sb = PWL.supabase;
const notice = document.getElementById("pg-notice");
const view = document.getElementById("pg-view");
if (!view) return;
const id = new URLSearchParams(location.search).get("id");
function esc(s) {
return String(s == null ? "" : s).replace(/[&<>"']/g, function (c) {
return { "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[c];
});
}
// Escape a comment body, then turn bare http(s) links into safe anchors.
// Only http/https are linked; href is escaped so it can't break out or run js.
function linkify(s) {
const str = String(s == null ? "" : s);
const re = /(https?:\/\/[^\s<>"']+)/g;
let out = "", last = 0, m;
while ((m = re.exec(str))) {
out += esc(str.slice(last, m.index));
let url = m[1], trail = "";
const t = url.match(/[.,!?;:)\]]+$/);
if (t) { trail = t[0]; url = url.slice(0, -trail.length); }
const safe = esc(url);
out += '<a href="' + safe + '" target="_blank" rel="noopener noreferrer nofollow">' + safe + "</a>" + esc(trail);
last = m.index + m[1].length;
}
out += esc(str.slice(last));
return out;
}
function timeAgo(iso) {
const then = new Date(iso).getTime();
if (isNaN(then)) return "";
const s = Math.max(1, Math.floor((Date.now() - then) / 1000));
if (s < 60) return "just now";
const m = Math.floor(s / 60); if (m < 60) return m + "m ago";
const h = Math.floor(m / 60); if (h < 24) return h + "h ago";
const d = Math.floor(h / 24); if (d < 30) return d + "d ago";
return new Date(iso).toLocaleDateString(undefined, { day: "numeric", month: "short" });
}
function whenLabel(p) {
const made = new Date(p.created_at).getTime();
const edited = new Date(p.updated_at || p.created_at).getTime();
if (edited && made && edited - made > 60000) return "updated " + timeAgo(p.updated_at);
return timeAgo(p.created_at);
}
function avatarOf(profile) {
const url = profile && profile.avatar_url;
const cross = window.crossOriginIsolated ? ' crossorigin="anonymous"' : "";
const photo = url ? '<img class="pwl-avatar-photo" src="' + esc(url) + '" alt="" referrerpolicy="no-referrer"' + cross + ' onerror="this.remove()" />' : "";
return '<span class="pwl-avatar sm"><svg class="pwl-avatar-person" viewBox="0 0 24 24" aria-hidden="true"><circle cx="12" cy="9" r="4.2"/><path d="M12 14.4c-4.3 0-7.8 2.7-7.8 6.1V24h15.6v-3.5c0-3.4-3.5-6.1-7.8-6.1z"/></svg>' + photo + "</span>";
}
function nameOf(profile) { return esc((profile && profile.display_name) || "Someone"); }
const EYE_SVG = '<svg viewBox="0 0 24 24" width="13" height="13" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M1.5 12S5 5 12 5s10.5 7 10.5 7-3.5 7-10.5 7S1.5 12 1.5 12Z"/><circle cx="12" cy="12" r="3"/></svg>';
function viewsHtml(n) { return '<span class="pg-views" title="Views">' + EYE_SVG + " " + (Number(n) || 0) + "</span>"; }
// Opening the program page is a view: count it once per browser session (so a
// refresh doesn't inflate it) and optimistically bump the number on the page.
function countView(p) {
try {
const k = "pwl-viewed:" + p.id;
if (sessionStorage.getItem(k)) return;
sessionStorage.setItem(k, "1");
} catch (e) {}
p.view_count = (Number(p.view_count) || 0) + 1;
const el = view.querySelector(".pg-views");
if (el) el.innerHTML = EYE_SVG + " " + p.view_count;
// supabase-js queries are lazy: they only send once awaited or .then()'d.
try { sb.rpc("increment_view", { pid: p.id }).then(null, function () {}); } catch (e) {}
}
function toast(msg) {
let t = document.getElementById("pwl-toast");
if (!t) { t = document.createElement("div"); t.id = "pwl-toast"; t.className = "toast"; document.body.appendChild(t); }
t.textContent = msg;
requestAnimationFrame(function () { t.classList.add("show"); });
clearTimeout(t._timer); t._timer = setTimeout(function () { t.classList.remove("show"); }, 3000);
}
function showNotice(msg) {
if (notice) { notice.textContent = msg; notice.hidden = false; }
view.hidden = true;
}
if (!PWL.configured || !sb) {
showNotice("The community isn't switched on yet. It needs a Supabase project with Google sign-in.");
return;
}
if (!id) {
showNotice("No program was named. Browse the community to find one.");
return;
}
async function load() {
function cols(withViews, withFeat) {
return "id,title,description,code,kind,scene,vote_count," + (withViews ? "view_count," : "") +
(withFeat ? "featured," : "") +
"created_at,updated_at,author_id,profiles!author_id(display_name,avatar_url),comments(count)";
}
// Ask for view_count and featured, but tolerate a database that hasn't
// added either yet: the page still has to open.
let res = await sb.from("projects").select(cols(true, true)).eq("id", id).single();
if (res.error && /featured/i.test(res.error.message || "")) {
res = await sb.from("projects").select(cols(true, false)).eq("id", id).single();
}
if (res.error && /view_count/i.test(res.error.message || "")) {
res = await sb.from("projects").select(cols(false, false)).eq("id", id).single();
}
const { data, error } = res;
if (error || !data) {
showNotice("That program couldn't be found. It may have been deleted.");
return;
}
render(data);
}
function render(p) {
document.title = (p.title || "Program") + " | PyWebLib";
const kind = p.kind || "python";
const playLabel = kind === "game" ? "Play" : "Run";
// A game that calls game.submit_score() gets a per-game leaderboard: one
// best score per signed-in player, submitted automatically as they play.
const hasLeaderboard = kind === "game" && /game\s*\.\s*submit_score/.test(p.code || "");
let runBest = null;
async function loadLeaderboard() {
const box = view.querySelector(".pg-lb-body");
if (!box) return;
const res = await sb.from("game_scores")
.select("score,user_id,profiles(display_name,avatar_url)")
.eq("project_id", p.id).order("score", { ascending: false }).limit(10);
if (res.error) { box.textContent = "Could not load scores."; return; }
renderLeaderboard(res.data || []);
}
function renderLeaderboard(rows) {
const box = view.querySelector(".pg-lb-body");
if (!box) return;
const user = PWL.auth && PWL.auth.user();
if (!rows.length) {
box.innerHTML = '<p class="pg-lb-empty">No scores yet. Play to be the first!</p>';
} else {
box.innerHTML = rows.map(function (r, i) {
const me = user && r.user_id === user.id ? " me" : "";
return '<div class="pg-lb-row' + me + '"><span class="pg-lb-rank">' + (i + 1) + "</span>" +
avatarOf(r.profiles) + '<span class="pg-lb-name">' + nameOf(r.profiles) +
'</span><span class="pg-lb-score">' + Number(r.score) + "</span></div>";
}).join("");
}
if (!user) box.innerHTML += '<p class="pg-lb-signin">Sign in and play to get on the board.</p>';
}
async function submitScore(points) {
points = Number(points);
const user = PWL.auth && PWL.auth.user();
if (!user || !isFinite(points)) return;
points = Math.floor(points);
if (runBest != null && points <= runBest) return; // not better than this run
runBest = points;
try {
const cur = await sb.from("game_scores").select("score")
.eq("project_id", p.id).eq("user_id", user.id).maybeSingle();
if (!(cur.data && Number(cur.data.score) >= points)) { // keep only your best
await sb.from("game_scores").upsert(
{ project_id: p.id, user_id: user.id, score: points, updated_at: new Date().toISOString() },
{ onConflict: "project_id,user_id" });
}
} catch (e) {}
loadLeaderboard();
}
view.hidden = false;
view.innerHTML =
'<div class="cc-head">' +
'<span class="cc-kind cc-kind-' + esc(kind) + '">' + esc(kind) + "</span>" +
(p.featured === true ? '<span class="cc-kind cc-featured" title="Pinned to the top of the community gallery">★ Featured</span>' : "") +
'<h1 class="pg-title"></h1>' +
"</div>" +
'<div class="cc-author">' + avatarOf(p.profiles) + "<span>" + nameOf(p.profiles) +
" · " + esc(whenLabel(p)) + "</span>" + viewsHtml(p.view_count) + "</div>" +
'<div class="pg-stage">' +
'<div class="pg-play" id="pg-play"></div>' +
(hasLeaderboard ? '<aside class="pg-leaderboard" id="pg-leaderboard"><h2 class="pg-lb-title">Leaderboard</h2><div class="pg-lb-body">Loading…</div></aside>' : "") +
"</div>" +
'<p class="pg-desc"></p>' +
'<div class="pg-actions">' +
'<button type="button" class="btn btn-primary" id="pg-open">Open in the Playground</button>' +
'<button type="button" class="cc-vote" id="pg-vote" title="Upvote">' +
'<span class="cc-arrow"><svg viewBox="0 0 16 16" width="11" height="11" aria-hidden="true"><path d="M8 4l5 6.5H3z" fill="currentColor"/></svg></span> ' +
'<span class="cc-votes">' + p.vote_count + "</span></button>" +
'<button type="button" class="btn btn-ghost" id="pg-copy">Copy link</button>' +
"</div>" +
'<details class="pg-code-wrap">' +
'<summary class="pg-code-summary">Code</summary>' +
'<pre class="pwl-modal-code pg-code"></pre>' +
"</details>" +
'<h2 class="pg-h">Comments</h2>' +
'<div class="pwl-comments" id="pg-comments">Loading…</div>' +
'<form class="pwl-comment-form" id="pg-comment-form" hidden>' +
'<input type="text" maxlength="1000" placeholder="Add a comment…" aria-label="Add a comment" />' +
'<button type="submit" class="btn btn-primary">Post</button>' +
"</form>" +
'<p class="pwl-comment-signin" id="pg-comment-signin" hidden>Sign in to join the conversation.</p>';
view.querySelector(".pg-title").textContent = p.title || "Untitled";
const desc = view.querySelector(".pg-desc");
if (p.description) desc.innerHTML = linkify(p.description); else desc.remove();
view.querySelector(".pg-code").textContent = p.code;
// Poster with a Play overlay: click it to run inline, no Playground needed.
const playHost = view.querySelector("#pg-play");
playHost.innerHTML =
'<div class="pg-poster">' +
'<canvas class="pg-poster-canvas" width="480" height="270"></canvas>' +
'<button type="button" class="pg-play-btn"><svg viewBox="0 0 16 16" aria-hidden="true"><path d="M4 3l9 5-9 5z" fill="currentColor"/></svg> ' + playLabel + "</button>" +
"</div>";
const posterCanvas = playHost.querySelector(".pg-poster-canvas");
if (window.PWL.preview) { try { window.PWL.preview.renderInto(posterCanvas, p.code, p.scene); } catch (e) {} }
playHost.querySelector(".pg-play-btn").addEventListener("click", function () {
if (!window.PWL.player) { toast("The player isn't ready, try refreshing."); return; }
runBest = null; // a fresh play resets the best-of-this-run guard
window.PWL.player.mount(playHost, { code: p.code, kind: kind, title: p.title },
hasLeaderboard ? { onScore: submitScore } : null);
});
if (hasLeaderboard) loadLeaderboard();
// Open in the Playground, bound to this program so a re-share updates it.
view.querySelector("#pg-open").addEventListener("click", function () {
try {
localStorage.setItem("pyweblib-load", p.code);
localStorage.setItem("pyweblib-bind", JSON.stringify({ id: p.id, title: p.title, author_id: p.author_id, published: p.published }));
} catch (e) {}
window.location.href = "../";
});
view.querySelector("#pg-copy").addEventListener("click", function () {
const url = location.href;
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(url).then(function () { toast("Link copied!"); }, function () { toast(url); });
} else { toast(url); }
});
wireAdmin(p, view);
wireVote(p);
loadComments(p);
countView(p);
}
// Admin moderation on the program page: rename / delete any post. The buttons
// are only CREATED once we've confirmed the signed-in user is an admin, so
// there's nothing for signed-out or ordinary users to see.
async function wireAdmin(p, view) {
const user = PWL.auth && PWL.auth.user();
if (!user) return;
let admin = false;
try { const r = await sb.rpc("is_admin"); admin = !!(r && r.data === true); } catch (e) { return; }
if (!admin) return;
const actions = view.querySelector(".pg-actions");
if (!actions) return;
const renameBtn = document.createElement("button");
renameBtn.type = "button"; renameBtn.className = "btn btn-ghost cc-mod"; renameBtn.textContent = "Rename";
renameBtn.addEventListener("click", async function () {
const next = window.prompt("Rename this post:", p.title || "");
if (next == null) return;
const title = next.trim().slice(0, 120);
if (!title || title === p.title) return;
const res = await sb.from("projects")
.update({ title: title, updated_at: new Date().toISOString() })
.eq("id", p.id).select("id").single();
if (res.error) { toast("Rename failed: " + res.error.message); return; }
p.title = title;
const el = view.querySelector(".pg-title"); if (el) el.textContent = title;
toast("Renamed.");
});
actions.appendChild(renameBtn);
const delBtn = document.createElement("button");
delBtn.type = "button"; delBtn.className = "btn btn-ghost cc-mod cc-mod-del"; delBtn.textContent = "Delete";
let armed = false, armTimer;
delBtn.addEventListener("click", async function () {
if (!armed) {
armed = true; delBtn.textContent = "Click again to delete"; delBtn.classList.add("armed");
clearTimeout(armTimer);
armTimer = setTimeout(function () { armed = false; delBtn.textContent = "Delete"; delBtn.classList.remove("armed"); }, 3500);
return;
}
clearTimeout(armTimer);
const res = await sb.from("projects").delete().eq("id", p.id);
if (res.error) { toast("Delete failed: " + res.error.message); return; }
toast("Post deleted.");
setTimeout(function () { window.location.href = "../community/"; }, 700);
});
actions.appendChild(delBtn);
}
async function wireVote(p) {
const btn = document.getElementById("pg-vote");
let voted = false;
const user = PWL.auth && PWL.auth.user();
if (user) {
const r = await sb.from("votes").select("project_id").eq("user_id", user.id).eq("project_id", p.id);
voted = !!(r.data && r.data.length);
btn.classList.toggle("voted", voted);
}
btn.addEventListener("click", async function () {
if (!PWL.auth || !PWL.auth.requireSignIn()) return;
const uid = PWL.auth.user().id;
voted = !voted;
p.vote_count = Math.max(0, p.vote_count + (voted ? 1 : -1));
btn.classList.toggle("voted", voted);
btn.querySelector(".cc-votes").textContent = p.vote_count;
try {
if (voted) await sb.from("votes").insert({ project_id: p.id, user_id: uid });
else await sb.from("votes").delete().eq("project_id", p.id).eq("user_id", uid);
} catch (e) {
voted = !voted;
p.vote_count = Math.max(0, p.vote_count + (voted ? 1 : -1));
btn.classList.toggle("voted", voted);
btn.querySelector(".cc-votes").textContent = p.vote_count;
toast("Vote didn't save, try again.");
}
});
}
async function loadComments(p) {
const box = document.getElementById("pg-comments");
const form = document.getElementById("pg-comment-form");
const signin = document.getElementById("pg-comment-signin");
const user = PWL.auth && PWL.auth.user();
if (user) form.hidden = false; else signin.hidden = false;
const { data, error } = await sb.from("comments")
.select("id,body,created_at,user_id,profiles(display_name,avatar_url)")
.eq("project_id", p.id).order("created_at", { ascending: true });
if (error) { box.textContent = "Could not load comments."; return; }
let comments = data || [];
function render(list) {
if (!list.length) { box.innerHTML = '<p class="pwl-comments-empty">No comments yet. Say something nice!</p>'; return; }
box.innerHTML = "";
list.forEach(function (c) {
const row = document.createElement("div");
row.className = "pwl-comment";
row.innerHTML =
avatarOf(c.profiles) +
'<div class="pwl-comment-body"><span class="pwl-comment-name">' + nameOf(c.profiles) +
' <span class="pwl-comment-when">' + esc(timeAgo(c.created_at)) + "</span></span>" +
'<span class="pwl-comment-text"></span></div>' +
(user && c.user_id === user.id ? '<button type="button" class="pwl-comment-del" title="Delete">×</button>' : "");
row.querySelector(".pwl-comment-text").innerHTML = linkify(c.body);
const del = row.querySelector(".pwl-comment-del");
if (del) del.addEventListener("click", async function () {
await sb.from("comments").delete().eq("id", c.id);
comments = comments.filter(function (x) { return x.id !== c.id; });
render(comments);
});
box.appendChild(row);
});
}
render(comments);
if (user) {
form.addEventListener("submit", async function (e) {
e.preventDefault();
const input = form.querySelector("input");
const body = (input.value || "").trim();
if (!body) return;
input.value = "";
const ins = await sb.from("comments")
.insert({ project_id: p.id, user_id: user.id, body: body })
.select("id,body,created_at,user_id,profiles(display_name,avatar_url)").single();
if (ins.error) { toast("Comment didn't post."); return; }
comments.push(ins.data);
render(comments);
});
}
}
// Re-run once auth resolves (so votes/comments reflect the signed-in user).
let loaded = false;
function start() { if (loaded) return; loaded = true; load(); }
document.addEventListener("pwl:auth", function () { /* auth affects vote/comment UI on next load */ });
start();
})();