Lashtw commited on
Commit
82326c2
·
verified ·
1 Parent(s): 522c6b5

Update guest.html

Browse files
Files changed (1) hide show
  1. guest.html +120 -470
guest.html CHANGED
@@ -3,514 +3,164 @@
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>即時互動文字雲 - 教師主控端</title>
7
-
8
- <script src="https://cdn.tailwindcss.com"></script>
9
- <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+TC:wght@400;500;700;800;900&display=swap" rel="stylesheet">
10
- <script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
11
- <script src="https://cdnjs.cloudflare.com/ajax/libs/wordcloud2.js/1.0.2/wordcloud2.min.js"></script>
12
-
13
  <style>
 
14
  body {
15
- font-family: 'Noto Sans TC', sans-serif;
 
 
 
16
  }
17
- .overlay {
18
- display: none;
19
- position: fixed;
20
- inset: 0;
21
- z-index: 1000;
22
- background: rgba(15, 23, 42, 0.6);
23
- backdrop-filter: blur(4px);
24
- align-items: center;
25
- justify-content: center;
26
  }
27
- .overlay.active {
28
- display: flex;
 
 
 
 
 
29
  }
30
- /* Custom Scrollbar for list panels */
31
- ::-webkit-scrollbar {
32
- width: 6px;
33
- height: 6px;
34
  }
35
- ::-webkit-scrollbar-track {
36
- background: #f1f5f9;
37
- border-radius: 8px;
 
 
 
 
38
  }
39
- ::-webkit-scrollbar-thumb {
40
- background: #cbd5e1;
41
- border-radius: 8px;
42
  }
43
- ::-webkit-scrollbar-thumb:hover {
44
- background: #94a3b8;
45
  }
 
46
  </style>
47
  </head>
48
- <body class="bg-slate-100 text-slate-800 h-screen overflow-hidden flex flex-col">
49
-
50
- <div id="loading" class="fixed inset-0 bg-slate-900/80 text-white flex flex-col items-center justify-center z-[2000] transition-opacity duration-300">
51
- <div class="w-12 h-12 border-4 border-orange-500 border-t-transparent rounded-full animate-spin mb-4"></div>
52
- <p class="text-lg font-medium tracking-wider">正在載入文字雲系統...</p>
53
- </div>
54
-
55
- <div class="flex-1 flex flex-col lg:flex-row h-full overflow-hidden">
56
-
57
- <!-- ================= 左側:大畫布文字雲區域 ================= -->
58
- <main class="flex-1 flex flex-col p-4 lg:p-6 overflow-hidden relative min-h-0">
59
-
60
- <!-- 頂部資訊條:題目與總數統計 -->
61
- <header class="flex items-center justify-between bg-white px-6 py-3.5 rounded-2xl shadow-sm border border-slate-200/80 mb-4 shrink-0">
62
- <div class="flex items-center gap-3 overflow-hidden mr-4">
63
- <span class="text-xl">📌</span>
64
- <h2 id="questionDisplay" class="text-base sm:text-lg font-bold text-slate-700 truncate">
65
- 等待教師發布題目...
66
- </h2>
67
- </div>
68
- <div class="flex items-center gap-2 bg-orange-50 px-4 py-1.5 rounded-xl border border-orange-200 shrink-0">
69
- <span class="text-xs sm:text-sm font-semibold text-orange-600">總詞彙數量</span>
70
- <span id="wordCount" class="text-lg sm:text-xl font-extrabold text-orange-600">0</span>
71
- </div>
72
- </header>
73
-
74
- <!-- Canvas 畫布容器 -->
75
- <div id="cloud-container" class="flex-1 bg-white rounded-2xl shadow-sm border border-slate-200/80 relative overflow-hidden flex items-center justify-center">
76
- <canvas id="word-canvas" class="w-full h-full cursor-pointer block"></canvas>
77
-
78
- <!-- 右下角開發者資訊標籤 -->
79
- <div class="absolute bottom-4 right-4 bg-white/90 backdrop-blur border border-slate-200/80 px-3.5 py-1.5 rounded-xl text-xs text-slate-500 shadow-sm z-10 flex items-center gap-2 select-none">
80
- <span>程���設計者:精華國中 藍星宇</span>
81
- <span class="text-slate-300">|</span>
82
- <a href="https://www.facebook.com/groups/1554372228718393" target="_blank" rel="noopener" class="text-blue-600 hover:underline font-medium">
83
- 臉書社團:萬物皆數
84
- </a>
85
- </div>
86
- </div>
87
-
88
- </main>
89
-
90
- <!-- ================= 右側:控制中心工具欄 ================= -->
91
- <aside class="w-full lg:w-80 xl:w-96 bg-white border-t lg:border-t-0 lg:border-l border-slate-200 p-5 flex flex-col justify-between gap-5 overflow-y-auto shrink-0 z-20">
92
-
93
- <!-- 上半部:標題、QR Code 與發布區域 -->
94
- <div class="space-y-5">
95
- <!-- 標頭列 -->
96
- <div class="flex items-center justify-between border-b border-slate-100 pb-3">
97
- <div class="flex items-center gap-2">
98
- <span class="text-2xl">☁️</span>
99
- <div>
100
- <h1 class="text-lg font-bold text-slate-800 leading-tight">文字雲主控端</h1>
101
- <p class="text-xs text-slate-400">即時互動與詞彙視覺化工具</p>
102
- </div>
103
- </div>
104
- <div class="bg-orange-500 text-white font-black text-sm px-3 py-1 rounded-xl shadow-sm">
105
- 房號:<span id="roomCodeDisplay">----</span>
106
- </div>
107
- </div>
108
-
109
- <!-- QR Code 掃碼加入區 -->
110
- <div class="bg-slate-50 border border-slate-200/80 rounded-2xl p-4 flex flex-col items-center">
111
- <p class="text-xs font-semibold text-slate-500 mb-2">學生掃描加入</p>
112
- <div class="p-2.5 bg-white rounded-xl border border-slate-200 shadow-sm mb-3">
113
- <div id="qrcode" class="flex justify-center items-center"></div>
114
- </div>
115
-
116
- <div class="grid grid-cols-2 gap-2 w-full">
117
- <button id="btnCopyLink" class="px-3 py-2 bg-white hover:bg-slate-100 border border-slate-200 text-slate-700 text-xs font-semibold rounded-xl transition-all shadow-sm flex items-center justify-center gap-1">
118
- 📋 複製連結
119
- </button>
120
- <button id="btnOpenGuest" class="px-3 py-2 bg-white hover:bg-slate-100 border border-slate-200 text-slate-700 text-xs font-semibold rounded-xl transition-all shadow-sm flex items-center justify-center gap-1">
121
- 🔗 開啟學員端
122
- </button>
123
- </div>
124
- </div>
125
-
126
- <!-- 發布討論題目 -->
127
- <div class="space-y-2">
128
- <label for="questionInput" class="text-xs font-bold text-slate-600 block">設定討論題目</label>
129
- <div class="flex gap-2">
130
- <input id="questionInput" type="text" placeholder="輸入題目,學生端同步顯示..." class="flex-1 px-3 py-2 text-sm bg-slate-50 border border-slate-200 rounded-xl focus:outline-none focus:ring-2 focus:ring-orange-400 focus:bg-white transition-all">
131
- <button id="btnQuestion" class="px-4 py-2 bg-orange-500 hover:bg-orange-600 active:scale-95 text-white font-semibold text-sm rounded-xl transition-all shadow-sm shrink-0">
132
- 送出
133
- </button>
134
- </div>
135
- </div>
136
-
137
- <!-- 房間設定開關 -->
138
- <div class="space-y-3 border-t border-slate-100 pt-3">
139
- <div class="flex items-center justify-between p-2.5 rounded-xl hover:bg-slate-50 transition-colors">
140
- <div>
141
- <div class="text-xs font-bold text-slate-700">🔒 鎖定房間</div>
142
- <div class="text-[11px] text-slate-400">開啟後將暫停接收學生新回應</div>
143
- </div>
144
- <label class="relative inline-flex items-center cursor-pointer">
145
- <input type="checkbox" id="lockToggle" class="sr-only peer">
146
- <div class="w-11 h-6 bg-slate-200 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-slate-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-orange-500"></div>
147
- </label>
148
- </div>
149
-
150
- <div class="flex items-center justify-between p-2.5 rounded-xl hover:bg-slate-50 transition-colors">
151
- <div>
152
- <div class="text-xs font-bold text-slate-700">🔄 允許多次送出</div>
153
- <div class="text-[11px] text-slate-400">學生可重複提交多個詞彙</div>
154
- </div>
155
- <label class="relative inline-flex items-center cursor-pointer">
156
- <input type="checkbox" id="toggleSwitch" class="sr-only peer">
157
- <div class="w-11 h-6 bg-slate-200 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-slate-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-orange-500"></div>
158
- </label>
159
- </div>
160
- </div>
161
- </div>
162
-
163
- <!-- 下半部:工具選單按鈕區 -->
164
- <div class="space-y-2 border-t border-slate-100 pt-3">
165
- <div class="text-xs font-bold text-slate-400 mb-2">工具選單</div>
166
- <div class="grid grid-cols-3 gap-2">
167
- <button id="btnFullScreen" class="px-3 py-2.5 bg-slate-100 hover:bg-slate-200 text-slate-700 text-xs font-semibold rounded-xl transition-all flex items-center justify-center gap-1">
168
- ⛶ 全螢幕
169
- </button>
170
- <button id="btnScreenshot" class="px-3 py-2.5 bg-slate-100 hover:bg-slate-200 text-slate-700 text-xs font-semibold rounded-xl transition-all flex items-center justify-center gap-1">
171
- 📸 截圖
172
- </button>
173
- <button id="btnCSV" class="px-3 py-2.5 bg-slate-100 hover:bg-slate-200 text-slate-700 text-xs font-semibold rounded-xl transition-all flex items-center justify-center gap-1">
174
- 📄 匯出 CSV
175
- </button>
176
- </div>
177
- <button id="btnReset" class="w-full py-2.5 bg-red-50 hover:bg-red-100 text-red-600 border border-red-200 font-semibold text-xs rounded-xl transition-all shadow-sm mt-2 flex items-center justify-center gap-1">
178
- 🗑 清除重來
179
- </button>
180
- </div>
181
 
182
- </aside>
 
 
183
 
184
- </div>
185
-
186
- <!-- 詞彙選擇列表彈窗 -->
187
- <div class="overlay" id="pickOverlay">
188
- <div class="bg-white p-6 rounded-2xl max-w-lg w-[90%] max-h-[75vh] flex flex-col shadow-2xl">
189
- <h3 class="text-lg font-bold text-slate-800 mb-3">選擇要檢視或放大的詞彙:</h3>
190
- <div id="pickList" class="flex-1 overflow-y-auto space-x-1 space-y-2 py-2"></div>
191
- <div class="mt-4 text-right border-t pt-3">
192
- <button id="pickClose" class="px-5 py-2 bg-slate-100 hover:bg-slate-200 text-slate-700 text-sm font-semibold rounded-xl transition-colors">
193
- 取消
194
- </button>
195
- </div>
196
- </div>
197
- </div>
198
-
199
- <!-- 詞彙放大顯示彈窗 (加寬加高優化) -->
200
- <div class="overlay" id="zoomOverlay">
201
- <div class="bg-white p-8 md:p-10 rounded-3xl max-w-2xl md:max-w-3xl lg:max-w-4xl w-[92%] text-center shadow-2xl transition-all">
202
- <div id="zoomText" class="text-3xl sm:text-4xl md:text-5xl lg:text-6xl font-extrabold text-slate-800 break-words leading-snug my-6 md:my-8 px-2 max-h-[60vh] overflow-y-auto"></div>
203
- <div class="flex gap-3 justify-center border-t border-slate-100 pt-6">
204
- <button id="zoomDelete" class="px-6 py-2.5 bg-red-500 hover:bg-red-600 text-white text-sm font-semibold rounded-xl transition-colors shadow-sm">
205
- 🗑 刪除此詞彙
206
- </button>
207
- <button id="zoomClose" class="px-6 py-2.5 bg-slate-100 hover:bg-slate-200 text-slate-700 text-sm font-semibold rounded-xl transition-colors">
208
- 關閉
209
- </button>
210
- </div>
211
  </div>
212
- </div>
213
 
214
- <!-- 清除數據確認彈窗 -->
215
- <div class="overlay" id="resetConfirmOverlay">
216
- <div class="bg-white p-6 rounded-2xl max-w-sm w-[90%] text-center shadow-2xl">
217
- <div class="text-3xl mb-2">⚠️</div>
218
- <h3 class="text-lg font-bold text-slate-800 mb-2">重置所有資料?</h3>
219
- <p class="text-sm text-slate-500 mb-6">這將會清除當前畫面上所有的文字雲詞彙,此動作無法復原。</p>
220
- <div class="flex gap-3 justify-center">
221
- <button id="btnConfirmReset" class="flex-1 py-2.5 bg-red-500 hover:bg-red-600 text-white text-sm font-semibold rounded-xl transition-colors">
222
- 確定清除
223
- </button>
224
- <button id="btnCancelReset" class="flex-1 py-2.5 bg-slate-100 hover:bg-slate-200 text-slate-700 text-sm font-semibold rounded-xl transition-colors">
225
- 取消
226
- </button>
227
- </div>
228
  </div>
229
  </div>
230
 
231
- <!-- 複製成功 Toast 提示 -->
232
- <div id="toast" class="fixed bottom-6 left-1/2 -translate-x-1/2 bg-slate-900/90 text-white px-5 py-2.5 rounded-2xl shadow-xl text-sm font-medium transition-all opacity-0 pointer-events-none z-[3000]">
233
- 已複製學員端連結!
234
- </div>
235
-
236
  <script type="module">
237
  import { initializeApp } from 'https://www.gstatic.com/firebasejs/10.12.2/firebase-app.js';
238
- import { getFirestore, doc, setDoc, onSnapshot, updateDoc, deleteField } from 'https://www.gstatic.com/firebasejs/10.12.2/firebase-firestore.js';
239
 
240
- // Firebase Configuration
241
  const firebaseConfig = {
242
- apiKey: "AIzaSyAcIQYst9gFRyE2nCqCwNgCL3LG2VkBXbM",
243
- authDomain: "factor-529c9.firebaseapp.com",
244
- projectId: "factor-529c9",
245
- storageBucket: "factor-529c9.firebasestorage.app",
246
- messagingSenderId: "575829635896",
247
- appId: "1:575829635896:web:a65b6edd9d163375b087cd"
248
  };
249
 
250
  const app = initializeApp(firebaseConfig);
251
- const db = getFirestore(app);
252
-
253
- // Application State Variables
254
- const roomCode = Math.floor(1000 + Math.random() * 9000).toString();
255
- let allowMultiple = false;
256
- let isLocked = false;
257
- let wordData = {};
258
- let isDrawing = false;
259
- let drawQueue = false;
260
- let selectedWord = null;
261
-
262
- // UI Elements Reference
263
- const canvas = document.getElementById('word-canvas');
264
- const container = document.getElementById('cloud-container');
265
- const roomRef = doc(db, 'rooms', roomCode);
266
- const clientUrl = `${location.origin}${location.pathname.replace(/[^/]*$/, '')}guest.html?room=${roomCode}`;
267
-
268
- async function initRoom() {
269
- try {
270
- await setDoc(roomRef, { roomCode, locked: false, allowMultiple: false, words: {}, question: '' }, { merge: true });
271
- document.getElementById('roomCodeDisplay').textContent = roomCode;
272
-
273
- // Listen for real-time Firestore updates
274
- onSnapshot(roomRef, (docSnap) => {
275
- if (!docSnap.exists()) return;
276
- const data = docSnap.data();
277
-
278
- if (data.allowMultiple !== allowMultiple) {
279
- allowMultiple = data.allowMultiple;
280
- document.getElementById('toggleSwitch').checked = !!allowMultiple;
281
- }
282
- if (data.locked !== isLocked) {
283
- isLocked = !!data.locked;
284
- document.getElementById('lockToggle').checked = isLocked;
285
- }
286
-
287
- document.getElementById('questionDisplay').textContent = data.question ? data.question : '等待教師發布題目...';
288
- document.getElementById('wordCount').textContent = Object.keys(data.words || {}).length;
289
-
290
- const newWords = data.words || {};
291
- if (JSON.stringify(newWords) !== JSON.stringify(wordData)) {
292
- wordData = newWords;
293
- queueDraw();
294
- }
295
- });
296
- } catch (err) {
297
- console.error("Firebase init error:", err);
298
- }
299
- }
300
-
301
- function queueDraw() {
302
- if (isDrawing) { drawQueue = true; return; }
303
- drawCloud();
304
  }
305
 
306
- function drawCloud() {
307
- canvas.width = container.clientWidth;
308
- canvas.height = container.clientHeight;
309
- const list = Object.entries(wordData).map(([w, c]) => [w, c]);
310
-
311
- const ctx = canvas.getContext('2d');
312
- ctx.clearRect(0, 0, canvas.width, canvas.height);
313
-
314
- if (list.length === 0) {
315
- ctx.font = '16px "Noto Sans TC"';
316
- ctx.fillStyle = '#94A3B8';
317
- ctx.textAlign = 'center';
318
- ctx.fillText('等待學生送出詞彙…', canvas.width / 2, canvas.height / 2);
319
- return;
320
- }
321
-
322
- isDrawing = true;
323
-
324
- // 智慧調適文字大小:詞彙少時大幅放大標題字級,隨詞彙增加自動縮小
325
- const wordCount = list.length;
326
- const minDim = Math.min(canvas.width, canvas.height);
327
-
328
- // 計算最高頻率詞彙
329
- const maxFreq = Math.max(...list.map(item => item[1]));
330
-
331
- WordCloud(canvas, {
332
- list,
333
- gridSize: Math.max(4, Math.floor(minDim / 100)),
334
- weightFactor: (w) => {
335
- // 依總詞數動態選擇基礎縮放比例
336
- let baseScale;
337
- if (wordCount <= 3) baseScale = minDim * 0.32;
338
- else if (wordCount <= 8) baseScale = minDim * 0.22;
339
- else if (wordCount <= 20) baseScale = minDim * 0.15;
340
- else baseScale = minDim * 0.09;
341
-
342
- const relativeWeight = Math.pow(w / maxFreq, 0.7);
343
- return Math.max(16, relativeWeight * baseScale);
344
- },
345
- fontFamily: 'Noto Sans TC, sans-serif',
346
- color: () => ['#F97316', '#3B82F6', '#10B981', '#8B5CF6', '#EF4444', '#F59E0B', '#06B6D4'][Math.floor(Math.random() * 7)],
347
- rotateRatio: 0.15,
348
- minSize: 12,
349
- weightMode: 'size',
350
- shape: 'circle',
351
- ellipticity: 0.75,
352
- backgroundColor: 'transparent'
353
  });
354
  }
355
 
356
- // 監聽 WordCloud 畫布繪製完成事件
357
- canvas.addEventListener('wordcloudstop', function onStop() {
358
- isDrawing = false;
359
- if (drawQueue) {
360
- drawQueue = false;
361
- setTimeout(drawCloud, 300);
362
- }
363
- });
364
-
365
- canvas.addEventListener('click', () => {
366
- if (Object.keys(wordData).length === 0) return;
367
- const listEl = document.getElementById('pickList');
368
- listEl.innerHTML = '';
369
- Object.keys(wordData).sort((a, b) => wordData[b] - wordData[a]).forEach(word => {
370
- const chip = document.createElement('span');
371
- chip.className = 'inline-block m-1.5 px-4 py-2 text-sm font-medium bg-amber-50 hover:bg-amber-100 border border-amber-200 text-amber-900 rounded-2xl cursor-pointer transition-colors shadow-sm';
372
- chip.textContent = `${word} (${wordData[word]})`;
373
- chip.addEventListener('click', () => { hidePick(); showZoom(word); });
374
- listEl.appendChild(chip);
375
  });
376
- document.getElementById('pickOverlay').classList.add('active');
377
- });
378
-
379
- function hidePick() { document.getElementById('pickOverlay').classList.remove('active'); }
380
- document.getElementById('pickClose').addEventListener('click', hidePick);
381
-
382
- function showZoom(word) {
383
- selectedWord = word;
384
- document.getElementById('zoomText').textContent = word;
385
- document.getElementById('zoomOverlay').classList.add('active');
386
  }
387
- function hideZoom() {
388
- document.getElementById('zoomOverlay').classList.remove('active');
389
- selectedWord = null;
390
- }
391
- document.getElementById('zoomClose').addEventListener('click', hideZoom);
392
- document.getElementById('zoomOverlay').addEventListener('click', e => {
393
- if (e.target === document.getElementById('zoomOverlay')) hideZoom();
394
- });
395
-
396
- document.getElementById('zoomDelete').addEventListener('click', async () => {
397
- if (!selectedWord) return;
398
- await updateDoc(roomRef, { words: { [selectedWord]: deleteField() } });
399
- hideZoom();
400
- });
401
-
402
- document.getElementById('lockToggle').addEventListener('change', async () => {
403
- isLocked = document.getElementById('lockToggle').checked;
404
- await updateDoc(roomRef, { locked: isLocked });
405
- });
406
-
407
- document.getElementById('toggleSwitch').addEventListener('change', async () => {
408
- allowMultiple = document.getElementById('toggleSwitch').checked;
409
- await updateDoc(roomRef, { allowMultiple });
410
- });
411
-
412
- document.getElementById('btnQuestion').addEventListener('click', async () => {
413
- const text = document.getElementById('questionInput').value.trim();
414
- await updateDoc(roomRef, { question: text });
415
- document.getElementById('questionInput').value = '';
416
- });
417
-
418
- document.getElementById('btnFullScreen').addEventListener('click', () => {
419
- if (!document.fullscreenElement) document.documentElement.requestFullscreen();
420
- else document.exitFullscreen();
421
- });
422
-
423
- document.getElementById('btnScreenshot').addEventListener('click', () => {
424
- const tempCanvas = document.createElement('canvas');
425
- tempCanvas.width = canvas.width;
426
- tempCanvas.height = canvas.height;
427
- const tCtx = tempCanvas.getContext('2d');
428
- tCtx.fillStyle = '#FFFFFF';
429
- tCtx.fillRect(0, 0, tempCanvas.width, tempCanvas.height);
430
- tCtx.drawImage(canvas, 0, 0);
431
-
432
- const dataUrl = tempCanvas.toDataURL("image/png");
433
- const a = document.createElement('a');
434
- a.href = dataUrl;
435
- a.download = `即時互動文字雲_${roomCode}.png`;
436
- a.click();
437
- });
438
 
439
- document.getElementById('btnCSV').addEventListener('click', () => {
440
- const rows = [['詞彙', '次數'], ...Object.entries(wordData)];
441
- const csv = rows.map(r => r.map(v => `"${String(v).replace(/"/g, '""')}"`).join(',')).join('\n');
442
- const blob = new Blob(['\uFEFF' + csv], { type: 'text/csv;charset=utf-8;' });
443
- const u = URL.createObjectURL(blob);
444
- const a = document.createElement('a');
445
- a.href = u;
446
- a.download = `文字雲統計_${roomCode}.csv`;
447
- a.click();
448
- URL.revokeObjectURL(u);
449
- });
450
 
451
- // 清除重來:彈出自訂 Modal
452
- document.getElementById('btnReset').addEventListener('click', () => {
453
- document.getElementById('resetConfirmOverlay').classList.add('active');
454
- });
455
- document.getElementById('btnCancelReset').addEventListener('click', () => {
456
- document.getElementById('resetConfirmOverlay').classList.remove('active');
457
- });
458
- document.getElementById('btnConfirmReset').addEventListener('click', async () => {
459
- document.getElementById('resetConfirmOverlay').classList.remove('active');
460
- try { await updateDoc(roomRef, { words: {} }); }
461
- catch (e) { console.error("Reset failed:", e); }
462
- });
463
 
464
- // 複製連結與 Toast
465
- function showToast(msg) {
466
- const toast = document.getElementById('toast');
467
- toast.textContent = msg;
468
- toast.style.opacity = '1';
469
- setTimeout(() => { toast.style.opacity = '0'; }, 2200);
470
- }
471
-
472
- document.getElementById('btnCopyLink').addEventListener('click', () => {
473
- const tempInput = document.createElement('input');
474
- tempInput.value = clientUrl;
475
- document.body.appendChild(tempInput);
476
- tempInput.select();
477
- document.execCommand('copy');
478
- document.body.removeChild(tempInput);
479
- showToast('已複製學員端連結!');
480
- });
481
-
482
- document.getElementById('btnOpenGuest').addEventListener('click', () => {
483
- window.open(clientUrl, '_blank', 'noopener');
484
- });
485
-
486
- window.addEventListener('load', () => {
487
- initRoom();
488
-
489
- // 產生乾淨專屬 QR Code
490
- try {
491
- new QRCode(document.getElementById('qrcode'), {
492
- text: clientUrl,
493
- width: 140,
494
- height: 140,
495
- colorDark: "#1E293B",
496
- colorLight: "#FFFFFF",
497
- correctLevel: QRCode.CorrectLevel.M
498
- });
499
- } catch (err) {
500
- console.error("QRCode Error:", err);
501
  }
502
 
503
- // 視窗尺寸改變時重繪畫布
504
- const resizeObserver = new ResizeObserver(() => {
505
- queueDraw();
506
  });
507
- resizeObserver.observe(container);
508
 
509
- // 淡出並隱藏載入遮罩
510
- const loading = document.getElementById('loading');
511
- loading.style.opacity = '0';
512
- setTimeout(() => loading.style.display = 'none', 300);
513
- });
514
  </script>
515
 
516
  <script>
@@ -522,4 +172,4 @@ window.MathJax = {
522
  <script async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>
523
 
524
  </body>
525
- </html>
 
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>即時互動文字雲 - 參與者</title>
 
 
 
 
 
 
7
  <style>
8
+ * { box-sizing: border-box; margin: 0; padding: 0; }
9
  body {
10
+ font-family: 'Noto Sans TC', 'Microsoft JhengHei', sans-serif;
11
+ background: linear-gradient(160deg, #FEF3C7, #FFEDD5);
12
+ min-height: 100vh; display: flex; align-items: center; justify-content: center;
13
+ padding: 20px;
14
  }
15
+ .card {
16
+ background: #fff; padding: 36px 40px; border-radius: 24px;
17
+ width: 100%; max-width: 460px;
18
+ box-shadow: 0 20px 50px rgba(0,0,0,.12); text-align: center;
 
 
 
 
 
19
  }
20
+ h1 { color: #F97316; font-size: 24px; margin-bottom: 6px; }
21
+ .sub { color: #64748B; font-size: 14px; margin-bottom: 26px; }
22
+
23
+ label { display: block; text-align: left; font-size: 14px; color: #475569; margin-bottom: 6px; }
24
+ input[type=text] {
25
+ width: 100%; padding: 11px 13px; font-size: 16px;
26
+ border: 1px solid #FDBA74; border-radius: 10px; outline: none; margin-bottom: 20px;
27
  }
28
+ input:focus { border-color: #F97316; }
29
+ .btn {
30
+ width: 100%; padding: 12px; font-size: 17px; font-weight: 700;
31
+ border: none; border-radius: 12px; background: #F97316; color: #fff; cursor: pointer;
32
  }
33
+ .btn:disabled { background: #CBD5E1; cursor: not-allowed; }
34
+
35
+ /* 加入後的輸入狀態 */
36
+ #input-section { display: none; }
37
+ #question-display {
38
+ display: none; background: #FFF7ED; border: 1px solid #FED7AA; border-radius: 12px;
39
+ padding: 12px 15px; margin-bottom: 22px; font-size: 16px; color: #9A3412;
40
  }
41
+ #lock-notice {
42
+ display: none; background: #FEE2E2; border: 1px solid #FECACA; color: #991B1B;
43
+ border-radius: 12px; padding: 12px; margin-bottom: 22px; font-size: 15px;
44
  }
45
+ #you-sent {
46
+ display: block; margin-top: 16px; font-size: 13px; color: #16A34A; min-height: 18px;
47
  }
48
+ #id-suffix { margin-top: 14px; font-size: 12px; color: #94A3B8; }
49
  </style>
50
  </head>
51
+ <body>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
+ <div class="card" id="join-card">
54
+ <h1>☁️ 互動文字雲</h1>
55
+ <div class="sub">加入並送出你的想法</div>
56
 
57
+ <!-- 加入階段 -->
58
+ <div id="join-section">
59
+ <label>輸入房間代碼</label>
60
+ <input type="text" id="room-input" maxlength="4" placeholder="例:1234" inputmode="numeric">
61
+ <button class="btn" id="btn-join">加入</button>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  </div>
 
63
 
64
+ <!-- 已加入,送詞彙 -->
65
+ <div id="input-section">
66
+ <div id="question-display"></div>
67
+ <div id="lock-notice">🔒 老師已暫停接收詞彙,請稍候。</div>
68
+ <label>輸入你的詞彙 / 想法</label>
69
+ <input type="text" id="word-input" placeholder="輸入詞彙或 $數學式$…" autocomplete="off">
70
+ <button class="btn" id="btn-send">送出</button>
71
+ <span id="you-sent"></span>
72
+ <div id="id-suffix"></div>
 
 
 
 
 
73
  </div>
74
  </div>
75
 
 
 
 
 
 
76
  <script type="module">
77
  import { initializeApp } from 'https://www.gstatic.com/firebasejs/10.12.2/firebase-app.js';
78
+ import { getFirestore, doc, getDoc, onSnapshot, updateDoc, increment } from 'https://www.gstatic.com/firebasejs/10.12.2/firebase-firestore.js';
79
 
 
80
  const firebaseConfig = {
81
+ ApiKey: "AIzaSyAcIQYst9gFRyE2nCqCwNgCL3LG2VkBXbM",
82
+ authDomain: "factor-529c9.firebaseapp.com",
83
+ projectId: "factor-529c9",
84
+ storageBucket: "factor-529c9.firebasestorage.app",
85
+ messagingSenderId: "575829635896",
86
+ appId: "1:575829635896:web:a65b6edd9d163375b087cd"
87
  };
88
 
89
  const app = initializeApp(firebaseConfig);
90
+ const db = getFirestore(app);
91
+
92
+ let roomCode = null;
93
+ let roomRef = null;
94
+ let participantId = Date.now().toString(36);
95
+
96
+ const qs = new URLSearchParams(location.search);
97
+ const qRoom = qs.get('room');
98
+ if (qRoom) { document.getElementById('room-input').value = qRoom; join(); }
99
+
100
+ // ---- 加入 ----
101
+ document.getElementById('btn-join').addEventListener('click', join);
102
+ async function join() {
103
+ const value = document.getElementById('room-input').value.trim();
104
+ if (!/^\d{4}$/.test(value)) { alert('請輸入 4 位數房間代碼'); return; }
105
+ roomCode = value;
106
+ roomRef = doc(db, 'rooms', roomCode);
107
+ const snap = await getDoc(roomRef);
108
+ if (!snap.exists()) { alert('找不到此房間,請確認代碼'); return; }
109
+
110
+ document.getElementById('join-section').style.display = 'none';
111
+ document.getElementById('input-section').style.display = 'block';
112
+ document.getElementById('id-suffix').textContent = '參與者代號:' + participantId;
113
+ initListeners();
114
+ initInput();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  }
116
 
117
+ // ---- 監聽房間狀態 ----
118
+ function initListeners() {
119
+ onSnapshot(roomRef, (snap) => {
120
+ const data = snap.data();
121
+ if (!data) return;
122
+ const qEl = document.getElementById('question-display');
123
+ if (data.question) { qEl.textContent = '📌 ' + data.question; qEl.style.display = 'block'; }
124
+ else { qEl.style.display = 'none'; }
125
+
126
+ const locked = data.locked || false;
127
+ document.getElementById('lock-notice').style.display = locked ? 'block' : 'none';
128
+ document.getElementById('word-input').disabled = locked;
129
+ document.getElementById('btn-send').disabled = locked;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  });
131
  }
132
 
133
+ // ---- 送出詞彙 ----
134
+ function initInput() {
135
+ document.getElementById('btn-send').addEventListener('click', submit);
136
+ document.getElementById('word-input').addEventListener('keydown', e => {
137
+ if (e.key === 'Enter') submit();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  });
 
 
 
 
 
 
 
 
 
 
139
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
 
141
+ async function submit() {
142
+ const word = document.getElementById('word-input').value.trim();
143
+ if (!word) return;
 
 
 
 
 
 
 
 
144
 
145
+ const snap = await getDoc(roomRef);
146
+ const data = snap.data();
147
+ if (data.locked) { alert('老師已鎖定,暫停接收'); return; }
 
 
 
 
 
 
 
 
 
148
 
149
+ // 多重送出關閉時,避免重複送出同一詞(可依需求調整)
150
+ const alreadySent = data.sentBy && data.sentBy[participantId];
151
+ if (!data.allowMultiple && alreadySent) {
152
+ alert('您已送出過詞彙,老師未開啟多次送出。');
153
+ return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
  }
155
 
156
+ await updateDoc(roomRef, {
157
+ [`words.${word}`]: increment(1),
158
+ [`sentBy.${participantId}`]: true // 記錄已送出
159
  });
 
160
 
161
+ document.getElementById('you-sent').textContent = '已送出:「' + word + '」✓';
162
+ document.getElementById('word-input').value = '';
163
+ }
 
 
164
  </script>
165
 
166
  <script>
 
172
  <script async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>
173
 
174
  </body>
175
+ </html>