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

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +249 -300
index.html CHANGED
@@ -5,217 +5,183 @@
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
  <title>即時互動文字雲 - 教師主控端</title>
7
 
8
- <!-- Tailwind CSS CDN -->
9
  <script src="https://cdn.tailwindcss.com"></script>
10
-
11
- <!-- 提前載入第三方庫,避免 ES Module 嚴格模式下的作用域問題 -->
12
  <script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
13
  <script src="https://cdnjs.cloudflare.com/ajax/libs/wordcloud2.js/1.0.2/wordcloud2.min.js"></script>
14
 
15
  <style>
16
- @import url('https://fonts.googleapis.com/css2?family=Noto+Sans+TC:wght@400;500;700;900&display=swap');
17
-
18
  body {
19
  font-family: 'Noto Sans TC', sans-serif;
20
- background-color: #F8FAFC;
21
- color: #1E293B;
22
  }
23
-
24
- /* 自訂開關切換器風格 */
25
- .switch {
26
- position: relative; display: inline-block;
27
- width: 46px; height: 24px; vertical-align: middle;
 
 
 
 
28
  }
29
- .switch input { opacity: 0; width: 0; height: 0; }
30
- .slider {
31
- position: absolute; cursor: pointer; inset: 0;
32
- background-color: #CBD5E1; border-radius: 24px; transition: .3s;
33
  }
34
- .slider:before {
35
- content: ""; position: absolute; height: 18px; width: 18px;
36
- left: 3px; bottom: 3px; background-color: #fff; border-radius: 50%; transition: .3s;
37
- box-shadow: 0 1px 3px rgba(0,0,0,0.2);
38
  }
39
- .switch input:checked + .slider { background-color: #F97316; }
40
- .switch input:checked + .slider:before { transform: translateX(22px); }
41
-
42
- /* 彈窗遮罩 */
43
- .overlay {
44
- display: none; position: fixed; inset: 0; z-index: 1000;
45
- background: rgba(15, 23, 42, 0.6); align-items: center; justify-content: center;
46
- backdrop-filter: blur(4px);
47
  }
48
-
49
- /* Toast 輕提示 */
50
- #toast {
51
- position: fixed; top: 20px; left: 50%; transform: translateX(-50%) translateY(-20px);
52
- background: #0f172a; color: #fff; padding: 10px 20px; border-radius: 99px;
53
- font-size: 14px; font-weight: 500; opacity: 0; pointer-events: none;
54
- transition: all 0.3s ease; z-index: 3000; box-shadow: 0 10px 25px rgba(0,0,0,0.2);
55
  }
56
- #toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
57
-
58
- /* 滾動條樣式 */
59
- ::-webkit-scrollbar { width: 6px; }
60
- ::-webkit-scrollbar-track { background: transparent; }
61
- ::-webkit-scrollbar-thumb { background: #CBD5E1; border-radius: 10px; }
62
- ::-webkit-scrollbar-thumb:hover { background: #94A3B8; }
63
  </style>
64
  </head>
65
- <body class="h-screen w-screen overflow-hidden flex flex-col md:flex-row">
66
 
67
- <!-- 載入中遮罩 -->
68
- <div id="loading" class="fixed inset-0 bg-slate-100 flex flex-col items-center justify-center z-[2000] transition-opacity duration-500">
69
- <div class="w-12 h-12 border-4 border-slate-300 border-t-orange-500 rounded-full animate-spin mb-4"></div>
70
- <div class="text-slate-600 font-semibold text-lg">雲端系統載入中,請稍候…</div>
71
  </div>
72
 
73
- <!-- 複製提示 Toast -->
74
- <div id="toast">已複製學生端連結!</div>
75
-
76
- <!-- ==================== 左側:文字雲主展示區 ==================== -->
77
- <main class="flex-1 flex flex-col h-full relative overflow-hidden bg-slate-50/50">
78
-
79
- <!-- 頂部資訊條 -->
80
- <header class="bg-white/90 backdrop-blur-md px-6 py-4 border-b border-slate-200 flex flex-wrap items-center justify-between gap-4 shadow-sm z-10">
81
- <div class="flex items-center gap-3">
82
- <span class="inline-flex items-center justify-center w-3 h-3 rounded-full bg-emerald-500 animate-pulse"></span>
83
- <h2 id="questionDisplay" class="text-lg md:text-xl font-bold text-slate-800 tracking-wide">
84
- 📌 尚未設定題目,請於右側工具欄輸入
85
- </h2>
86
- </div>
87
-
88
- <div class="flex items-center gap-4 text-sm font-medium text-slate-600">
89
- <div class="bg-orange-50 border border-orange-200 px-3 py-1.5 rounded-lg text-orange-700">
90
- 總詞彙數量:<b id="wordCount" class="text-xl font-extrabold text-orange-600 ml-1">0</b>
91
  </div>
92
- </div>
93
- </header>
94
-
95
- <!-- Canvas 文字雲容器 -->
96
- <div id="cloud-container" class="flex-1 relative m-4 md:m-6 bg-white rounded-2xl shadow-sm border border-slate-200/80 overflow-hidden">
97
- <canvas id="word-canvas" class="w-full h-full block cursor-pointer"></canvas>
98
- </div>
99
-
100
- <!-- 右下角作者資訊標籤 -->
101
- <div id="credit-bar" class="absolute right-6 bottom-6 z-20 bg-white/90 backdrop-blur border border-slate-200 px-3 py-1.5 rounded-lg text-xs text-slate-500 shadow-sm">
102
- 程式設計者:精華國中 藍星宇 ·
103
- <a href="https://www.facebook.com/groups/1554372228718393" target="_blank" rel="noopener" class="text-indigo-600 font-semibold hover:underline">臉書社團:萬物皆數</a>
104
- </div>
105
- </main>
106
 
107
- <!-- ==================== 右側:操作控制工具欄 ==================== -->
108
- <aside class="w-full md:w-80 lg:w-96 bg-white border-l border-slate-200 flex flex-col h-full shadow-xl z-20 overflow-y-auto">
109
-
110
- <!-- 標題與房號 header -->
111
- <div class="p-5 border-b border-slate-100 flex items-center justify-between bg-slate-50/50">
112
- <div>
113
- <h1 class="text-xl font-extrabold text-slate-900 flex items-center gap-2">
114
- <span class="text-2xl">☁️</span> 文字雲主控端
115
- </h1>
116
- <p class="text-xs text-slate-400 mt-0.5">即時互動與詞彙視覺化工具</p>
117
- </div>
118
- <div class="bg-gradient-to-r from-orange-500 to-amber-500 text-white font-black text-lg px-3.5 py-1 rounded-xl shadow-sm">
119
- <span class="text-xs opacity-80 font-normal block text-center -mb-1">房號</span>
120
- <span id="roomCodeDisplay">----</span>
121
  </div>
122
- </div>
123
 
124
- <div class="p-5 flex-1 space-y-6">
125
 
126
- <!-- QR Code 邀請卡片 -->
127
- <div class="bg-slate-50 rounded-2xl p-4 border border-slate-200/60 flex flex-col items-center text-center">
128
- <span class="text-xs font-bold text-slate-400 uppercase tracking-wider mb-2">學生掃描加入</span>
129
-
130
- <!-- QR Code 容器 -->
131
- <div class="bg-white p-3 rounded-xl shadow-sm border border-slate-100 mb-3 flex items-center justify-center">
132
- <div id="qrcode"></div>
 
 
 
 
 
 
 
 
 
 
133
  </div>
134
 
135
- <!-- 操作按鈕列 -->
136
- <div class="grid grid-cols-2 gap-2 w-full">
137
- <button id="btnCopyLink" class="w-full py-2 px-3 bg-white hover:bg-slate-100 border border-slate-200 text-slate-700 text-xs font-semibold rounded-lg transition-colors flex items-center justify-center gap-1.5 shadow-sm">
138
- 📋 複製連結
139
- </button>
140
- <button id="btnOpenGuest" class="w-full py-2 px-3 bg-white hover:bg-slate-100 border border-slate-200 text-slate-700 text-xs font-semibold rounded-lg transition-colors flex items-center justify-center gap-1.5 shadow-sm">
141
- 🔗 開啟學員端
142
- </button>
 
 
 
 
 
 
 
143
  </div>
144
- </div>
145
 
146
- <!-- 討論題目設定區 -->
147
- <div class="space-y-2">
148
- <label for="questionInput" class="text-xs font-bold text-slate-500 uppercase tracking-wider block">
149
- 設定討論題目
150
- </label>
151
- <div class="flex gap-2">
152
- <input id="questionInput" type="text" placeholder="輸入題目並按送出…"
153
- class="flex-1 px-3.5 py-2.5 text-sm bg-slate-50 border border-slate-200 rounded-xl focus:outline-none focus:ring-2 focus:ring-orange-500 focus:bg-white transition-all">
154
- <button id="btnQuestion" class="px-4 py-2.5 bg-orange-500 hover:bg-orange-600 text-white font-semibold text-sm rounded-xl transition-all shadow-sm active:scale-95">
155
- 送出
156
- </button>
157
  </div>
158
- </div>
159
 
160
- <!-- 房間狀態開關切換區 -->
161
- <div class="bg-slate-50 rounded-2xl p-4 border border-slate-200/60 space-y-3.5">
162
- <div class="flex items-center justify-between">
163
- <div class="flex items-center gap-2">
164
- <span class="text-base">🔒</span>
165
  <div>
166
- <div class="text-sm font-bold text-slate-700">鎖定房間</div>
167
- <div class="text-xs text-slate-400">開啟後將暫停接收學生新回應</div>
168
  </div>
 
 
 
 
169
  </div>
170
- <label class="switch">
171
- <input type="checkbox" id="lockToggle">
172
- <span class="slider"></span>
173
- </label>
174
- </div>
175
-
176
- <div class="h-px bg-slate-200/60"></div>
177
 
178
- <div class="flex items-center justify-between">
179
- <div class="flex items-center gap-2">
180
- <span class="text-base">🔄</span>
181
  <div>
182
- <div class="text-sm font-bold text-slate-700">允許多次送出</div>
183
- <div class="text-xs text-slate-400">學生可重複提交多個詞彙</div>
184
  </div>
 
 
 
 
185
  </div>
186
- <label class="switch">
187
- <input type="checkbox" id="toggleSwitch">
188
- <span class="slider"></span>
189
- </label>
190
  </div>
191
  </div>
192
 
193
- <!-- 功能按鈕群組 -->
194
- <div class="space-y-2.5 pt-2">
195
- <span class="text-xs font-bold text-slate-400 uppercase tracking-wider block">工具選單</span>
196
-
197
- <div class="grid grid-cols-2 gap-2.5">
198
- <button id="btnFullScreen" class="py-2.5 px-3 bg-slate-100 hover:bg-slate-200/80 text-slate-700 text-xs font-bold rounded-xl transition-colors flex items-center justify-center gap-2">
199
- ⛶ 切換全螢幕
200
  </button>
201
- <button id="btnScreenshot" class="py-2.5 px-3 bg-slate-100 hover:bg-slate-200/80 text-slate-700 text-xs font-bold rounded-xl transition-colors flex items-center justify-center gap-2">
202
- 📸 畫面截圖
 
 
 
203
  </button>
204
  </div>
205
-
206
- <button id="btnCSV" class="w-full py-2.5 px-3 bg-slate-100 hover:bg-slate-200/80 text-slate-700 text-xs font-bold rounded-xl transition-colors flex items-center justify-center gap-2">
207
- 📄 匯出分析 CSV 檔案
208
- </button>
209
-
210
- <button id="btnReset" class="w-full py-2.5 px-3 bg-red-50 hover:bg-red-100 text-red-600 text-xs font-bold rounded-xl transition-colors flex items-center justify-center gap-2 border border-red-100">
211
- 🗑 清除所有文字重來
212
  </button>
213
  </div>
214
 
215
- </div>
216
- </aside>
217
 
218
- <!-- ==================== 彈窗對話框群 ==================== -->
219
 
220
  <!-- 詞彙選擇列表彈窗 -->
221
  <div class="overlay" id="pickOverlay">
@@ -230,15 +196,15 @@
230
  </div>
231
  </div>
232
 
233
- <!-- 詞彙放大顯示彈窗 -->
234
  <div class="overlay" id="zoomOverlay">
235
- <div class="bg-white p-8 rounded-2xl max-w-md w-[90%] text-center shadow-2xl">
236
- <div id="zoomText" class="text-4xl md:text-5xl font-extrabold text-slate-800 break-words my-6"></div>
237
- <div class="flex gap-3 justify-center border-t pt-5">
238
- <button id="zoomDelete" class="px-5 py-2.5 bg-red-500 hover:bg-red-600 text-white text-sm font-semibold rounded-xl transition-colors shadow-sm">
239
  🗑 刪除此詞彙
240
  </button>
241
- <button id="zoomClose" class="px-5 py-2.5 bg-slate-100 hover:bg-slate-200 text-slate-700 text-sm font-semibold rounded-xl transition-colors">
242
  關閉
243
  </button>
244
  </div>
@@ -248,11 +214,11 @@
248
  <!-- 清除數據確認彈窗 -->
249
  <div class="overlay" id="resetConfirmOverlay">
250
  <div class="bg-white p-6 rounded-2xl max-w-sm w-[90%] text-center shadow-2xl">
251
- <div class="w-12 h-12 bg-red-100 text-red-500 rounded-full flex items-center justify-center text-2xl mx-auto mb-3">⚠️</div>
252
- <h3 class="text-lg font-bold text-slate-800 mb-2">重置所有詞彙?</h3>
253
- <p class="text-sm text-slate-500 leading-relaxed mb-6">確定要清除畫面上所有的文字數據嗎?此操作無法恢復。</p>
254
  <div class="flex gap-3 justify-center">
255
- <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 shadow-sm">
256
  確定清除
257
  </button>
258
  <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">
@@ -262,11 +228,16 @@
262
  </div>
263
  </div>
264
 
 
 
 
 
 
265
  <script type="module">
266
  import { initializeApp } from 'https://www.gstatic.com/firebasejs/10.12.2/firebase-app.js';
267
  import { getFirestore, doc, setDoc, onSnapshot, updateDoc, deleteField } from 'https://www.gstatic.com/firebasejs/10.12.2/firebase-firestore.js';
268
 
269
- // Firebase 設定
270
  const firebaseConfig = {
271
  apiKey: "AIzaSyAcIQYst9gFRyE2nCqCwNgCL3LG2VkBXbM",
272
  authDomain: "factor-529c9.firebaseapp.com",
@@ -279,7 +250,7 @@ const firebaseConfig = {
279
  const app = initializeApp(firebaseConfig);
280
  const db = getFirestore(app);
281
 
282
- // 全域狀態變數
283
  const roomCode = Math.floor(1000 + Math.random() * 9000).toString();
284
  let allowMultiple = false;
285
  let isLocked = false;
@@ -287,13 +258,11 @@ let wordData = {};
287
  let isDrawing = false;
288
  let drawQueue = false;
289
  let selectedWord = null;
290
- let resizeTimer = null;
291
 
 
292
  const canvas = document.getElementById('word-canvas');
293
  const container = document.getElementById('cloud-container');
294
  const roomRef = doc(db, 'rooms', roomCode);
295
-
296
- // 學員端網址
297
  const clientUrl = `${location.origin}${location.pathname.replace(/[^/]*$/, '')}guest.html?room=${roomCode}`;
298
 
299
  async function initRoom() {
@@ -301,6 +270,7 @@ async function initRoom() {
301
  await setDoc(roomRef, { roomCode, locked: false, allowMultiple: false, words: {}, question: '' }, { merge: true });
302
  document.getElementById('roomCodeDisplay').textContent = roomCode;
303
 
 
304
  onSnapshot(roomRef, (docSnap) => {
305
  if (!docSnap.exists()) return;
306
  const data = docSnap.data();
@@ -314,17 +284,7 @@ async function initRoom() {
314
  document.getElementById('lockToggle').checked = isLocked;
315
  }
316
 
317
- const qDisplay = document.getElementById('questionDisplay');
318
- if (data.question && data.question.trim() !== '') {
319
- qDisplay.textContent = '📌 ' + data.question;
320
- qDisplay.classList.remove('text-slate-400');
321
- qDisplay.classList.add('text-slate-800');
322
- } else {
323
- qDisplay.textContent = '📌 尚未設定討論題目';
324
- qDisplay.classList.remove('text-slate-800');
325
- qDisplay.classList.add('text-slate-400');
326
- }
327
-
328
  document.getElementById('wordCount').textContent = Object.keys(data.words || {}).length;
329
 
330
  const newWords = data.words || {};
@@ -334,7 +294,7 @@ async function initRoom() {
334
  }
335
  });
336
  } catch (err) {
337
- console.error("初始化房間失敗:", err);
338
  }
339
  }
340
 
@@ -348,57 +308,52 @@ function drawCloud() {
348
  canvas.height = container.clientHeight;
349
  const list = Object.entries(wordData).map(([w, c]) => [w, c]);
350
 
 
 
 
351
  if (list.length === 0) {
352
- const ctx = canvas.getContext('2d');
353
- ctx.clearRect(0, 0, canvas.width, canvas.height);
354
- ctx.font = '500 18px "Noto Sans TC"';
355
  ctx.fillStyle = '#94A3B8';
356
  ctx.textAlign = 'center';
357
- ctx.fillText('☁️ 等待學生送出詞彙中…', canvas.width / 2, canvas.height / 2);
358
  return;
359
  }
360
 
361
  isDrawing = true;
362
 
363
- // 動態計算字型大小:當詞彙數量少���大幅放大,數量多時自適應漸進縮小
 
364
  const minDim = Math.min(canvas.width, canvas.height);
365
- const maxFreq = Math.max(...list.map(([_, count]) => count), 1);
366
- const itemCount = list.length;
367
-
368
- let targetMaxPx;
369
- if (itemCount <= 3) {
370
- targetMaxPx = minDim * 0.32; // 詞彙極少時放大顯眼
371
- } else if (itemCount <= 8) {
372
- targetMaxPx = minDim * 0.22;
373
- } else if (itemCount <= 20) {
374
- targetMaxPx = minDim * 0.16;
375
- } else if (itemCount <= 50) {
376
- targetMaxPx = minDim * 0.11;
377
- } else {
378
- targetMaxPx = minDim * 0.08;
379
- }
380
-
381
- if (typeof WordCloud !== 'undefined') {
382
- WordCloud(canvas, {
383
- list,
384
- gridSize: Math.max(4, Math.floor(minDim / 120)),
385
- weightFactor: (w) => {
386
- const ratio = Math.pow(w / maxFreq, 0.7);
387
- const computedSize = targetMaxPx * ratio;
388
- return Math.max(18, computedSize); // 確保最小字級清晰可讀 (>=18px)
389
- },
390
- fontFamily: 'Noto Sans TC, sans-serif',
391
- color: () => ['#F97316','#3B82F6','#10B981','#8B5CF6','#EF4444','#F59E0B','#06B6D4','#EC4899'][Math.floor(Math.random()*8)],
392
- rotateRatio: itemCount > 15 ? 0.2 : 0,
393
- minSize: 12,
394
- weightMode: 'size',
395
- shape: 'circle',
396
- ellipticity: 0.75,
397
- backgroundColor: '#ffffff'
398
- });
399
- }
400
  }
401
 
 
402
  canvas.addEventListener('wordcloudstop', function onStop() {
403
  isDrawing = false;
404
  if (drawQueue) {
@@ -407,38 +362,32 @@ canvas.addEventListener('wordcloudstop', function onStop() {
407
  }
408
  });
409
 
410
- // 視窗大小改變自動重新調整畫布
411
- window.addEventListener('resize', () => {
412
- clearTimeout(resizeTimer);
413
- resizeTimer = setTimeout(() => { queueDraw(); }, 250);
414
- });
415
-
416
- // 點擊 Canvas 顯示詞彙點選選單
417
  canvas.addEventListener('click', () => {
418
  if (Object.keys(wordData).length === 0) return;
419
  const listEl = document.getElementById('pickList');
420
  listEl.innerHTML = '';
421
-
422
- Object.keys(wordData).sort((a,b) => wordData[b] - wordData[a]).forEach(word => {
423
- const chip = document.createElement('button');
424
- chip.className = 'inline-flex items-center gap-1.5 px-3.5 py-1.5 m-1 bg-amber-50 hover:bg-amber-100 border border-amber-200 text-amber-900 text-sm font-medium rounded-full transition-all';
425
- chip.innerHTML = `<span>${word}</span><span class="bg-amber-200 text-amber-900 text-xs px-2 py-0.5 rounded-full font-bold">${wordData[word]}</span>`;
426
  chip.addEventListener('click', () => { hidePick(); showZoom(word); });
427
  listEl.appendChild(chip);
428
  });
429
- document.getElementById('pickOverlay').style.display = 'flex';
430
  });
431
 
432
- function hidePick(){ document.getElementById('pickOverlay').style.display = 'none'; }
433
  document.getElementById('pickClose').addEventListener('click', hidePick);
434
 
435
  function showZoom(word) {
436
  selectedWord = word;
437
  document.getElementById('zoomText').textContent = word;
438
- document.getElementById('zoomOverlay').style.display = 'flex';
 
 
 
 
439
  }
440
-
441
- function hideZoom(){ document.getElementById('zoomOverlay').style.display = 'none'; selectedWord = null; }
442
  document.getElementById('zoomClose').addEventListener('click', hideZoom);
443
  document.getElementById('zoomOverlay').addEventListener('click', e => {
444
  if (e.target === document.getElementById('zoomOverlay')) hideZoom();
@@ -450,7 +399,6 @@ document.getElementById('zoomDelete').addEventListener('click', async () => {
450
  hideZoom();
451
  });
452
 
453
- // 控制開關事件
454
  document.getElementById('lockToggle').addEventListener('change', async () => {
455
  isLocked = document.getElementById('lockToggle').checked;
456
  await updateDoc(roomRef, { locked: isLocked });
@@ -462,10 +410,9 @@ document.getElementById('toggleSwitch').addEventListener('change', async () => {
462
  });
463
 
464
  document.getElementById('btnQuestion').addEventListener('click', async () => {
465
- const input = document.getElementById('questionInput');
466
- const text = input.value.trim();
467
  await updateDoc(roomRef, { question: text });
468
- showToast('題目已更新');
469
  });
470
 
471
  document.getElementById('btnFullScreen').addEventListener('click', () => {
@@ -475,52 +422,53 @@ document.getElementById('btnFullScreen').addEventListener('click', () => {
475
 
476
  document.getElementById('btnScreenshot').addEventListener('click', () => {
477
  const tempCanvas = document.createElement('canvas');
478
- tempCanvas.width = canvas.width; tempCanvas.height = canvas.height;
 
479
  const tCtx = tempCanvas.getContext('2d');
480
- tCtx.fillStyle = '#FFFFFF'; tCtx.fillRect(0, 0, tempCanvas.width, tempCanvas.height);
 
481
  tCtx.drawImage(canvas, 0, 0);
 
482
  const dataUrl = tempCanvas.toDataURL("image/png");
483
  const a = document.createElement('a');
484
- a.href = dataUrl; a.download = `文字雲牆_${roomCode}.png`; a.click();
 
 
485
  });
486
 
487
  document.getElementById('btnCSV').addEventListener('click', () => {
488
- const rows = [['詞彙','次數'], ...Object.entries(wordData)];
489
- const csv = rows.map(r => r.map(v => `"${String(v).replace(/"/g,'""')}"`).join(',')).join('\n');
490
- const blob = new Blob(['\uFEFF' + csv], { type:'text/csv;charset=utf-8;' });
491
  const u = URL.createObjectURL(blob);
492
- const a = document.createElement('a'); a.href = u; a.download = `文字雲統計_${roomCode}.csv`; a.click();
 
 
 
493
  URL.revokeObjectURL(u);
494
  });
495
 
496
- // 重置彈窗邏輯
497
  document.getElementById('btnReset').addEventListener('click', () => {
498
- document.getElementById('resetConfirmOverlay').style.display = 'flex';
499
  });
500
-
501
  document.getElementById('btnCancelReset').addEventListener('click', () => {
502
- document.getElementById('resetConfirmOverlay').style.display = 'none';
503
  });
504
-
505
  document.getElementById('btnConfirmReset').addEventListener('click', async () => {
506
- try {
507
- await updateDoc(roomRef, { words: {} });
508
- showToast('文字雲已清空');
509
- } catch (e) {
510
- console.error("清除文字失敗:", e);
511
- }
512
- document.getElementById('resetConfirmOverlay').style.display = 'none';
513
  });
514
 
515
- // Toast 提示框
516
  function showToast(msg) {
517
  const toast = document.getElementById('toast');
518
  toast.textContent = msg;
519
- toast.classList.add('show');
520
- setTimeout(() => toast.classList.remove('show'), 2000);
521
  }
522
 
523
- // 複製連結按鈕
524
  document.getElementById('btnCopyLink').addEventListener('click', () => {
525
  const tempInput = document.createElement('input');
526
  tempInput.value = clientUrl;
@@ -531,37 +479,38 @@ document.getElementById('btnCopyLink').addEventListener('click', () => {
531
  showToast('已複製學員端連結!');
532
  });
533
 
534
- // 開啟學員頁面按鈕
535
  document.getElementById('btnOpenGuest').addEventListener('click', () => {
536
- window.open(clientUrl, '_blank');
537
  });
538
 
539
- function setupUI() {
 
 
 
540
  try {
541
- const qrContainer = document.getElementById('qrcode');
542
- qrContainer.innerHTML = '';
543
- if (typeof QRCode !== 'undefined') {
544
- new QRCode(qrContainer, {
545
- text: clientUrl,
546
- width: 130, height: 130,
547
- colorDark: "#0F172A", colorLight: "#FFFFFF",
548
- correctLevel: QRCode.CorrectLevel.H
549
- });
550
- }
551
  } catch (err) {
552
- console.error("QRCode 產生失敗:", err);
553
- } finally {
554
- const loading = document.getElementById('loading');
555
- if (loading) {
556
- loading.style.opacity = '0';
557
- setTimeout(() => loading.style.display = 'none', 500);
558
- }
559
  }
560
- }
561
 
562
- // 啟動系統
563
- initRoom();
564
- setupUI();
 
 
 
 
 
 
 
 
565
  </script>
566
 
567
  <script>
 
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">
 
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>
 
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">
 
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",
 
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;
 
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() {
 
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();
 
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 || {};
 
294
  }
295
  });
296
  } catch (err) {
297
+ console.error("Firebase init error:", err);
298
  }
299
  }
300
 
 
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) {
 
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();
 
399
  hideZoom();
400
  });
401
 
 
402
  document.getElementById('lockToggle').addEventListener('change', async () => {
403
  isLocked = document.getElementById('lockToggle').checked;
404
  await updateDoc(roomRef, { locked: isLocked });
 
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', () => {
 
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;
 
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>