Youngger9765 commited on
Commit
6dccf0d
·
1 Parent(s): 982f0fe

更新 index.html 模板,修正本地儲存載入過程中的物件值問題

Browse files

- 新增函式以修正從 localStorage 載入的損壞值,確保在載入過程中能正確處理特定欄位的預設值
- 改進 JSON 載入邏輯,確保在表單模式下能正確顯示陣列和物件欄位的內容

此次更新旨在提升用戶體驗,確保從本地儲存載入的變數設定正確無誤。

Files changed (1) hide show
  1. templates/index.html +98 -24
templates/index.html CHANGED
@@ -1919,7 +1919,10 @@
1919
  try {
1920
  const element = document.getElementById(`var-${field}`);
1921
  if (element && element.value) {
1922
- variables[field] = JSON.parse(element.value);
 
 
 
1923
  }
1924
  } catch (e) {
1925
  console.error(`Error parsing ${field}:`, e);
@@ -2291,34 +2294,105 @@
2291
  showToast('檔案下載中...', 'success');
2292
  }
2293
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2294
  // Load saved variables from localStorage on page load
2295
  window.addEventListener('DOMContentLoaded', function() {
2296
- try {
2297
- const savedVariables = localStorage.getItem('lsw-html-variables');
2298
- if (savedVariables) {
2299
- document.getElementById('variables').value = savedVariables;
2300
-
2301
- // If in form mode, update form from JSON
2302
- if (variableMode === 'form') {
2303
- try {
2304
- const jsonData = JSON.parse(savedVariables);
2305
- Object.keys(jsonData).forEach(key => {
2306
- const element = document.getElementById(`var-${key}`);
2307
- if (element) {
2308
- element.value = jsonData[key] || '';
2309
- }
2310
- });
2311
- } catch (e) {
2312
- console.error('Error parsing saved JSON:', e);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2313
  }
 
 
 
 
 
2314
  }
2315
-
2316
- console.log('已從 localStorage 載入變數');
2317
- showToast('已載入上次儲存的設定', 'info');
2318
  }
2319
- } catch (e) {
2320
- console.error('無法從 localStorage 載入:', e);
2321
- }
2322
  });
2323
 
2324
  // Don't auto-generate on page load
 
1919
  try {
1920
  const element = document.getElementById(`var-${field}`);
1921
  if (element && element.value) {
1922
+ // Check if the value is already an object (happens after localStorage load)
1923
+ if (typeof element.value === 'string' && element.value.trim()) {
1924
+ variables[field] = JSON.parse(element.value);
1925
+ }
1926
  }
1927
  } catch (e) {
1928
  console.error(`Error parsing ${field}:`, e);
 
2294
  showToast('檔案下載中...', 'success');
2295
  }
2296
 
2297
+ // Fix corrupted [object Object] values
2298
+ function fixCorruptedValues() {
2299
+ const jsonArrayFields = ['requirements', 'scoringCriteria', 'notes', 'awards'];
2300
+ jsonArrayFields.forEach(field => {
2301
+ const element = document.getElementById(`var-${field}`);
2302
+ if (element && element.value.includes('[object Object]')) {
2303
+ // Reset to default values
2304
+ switch(field) {
2305
+ case 'requirements':
2306
+ element.value = `[
2307
+ {"title": "字數與格式要求", "content": "徵文字數至少五百字。電腦打字需使用全形符號;於樂寫公益學習網平台個人帳號上傳。"},
2308
+ {"title": "原創性要求", "content": "作品必須未曾在任何形式的平面和網路媒體(樂寫公益平台除外)發表,嚴禁抄襲及代筆(包括AI),違者取消資格。"},
2309
+ {"title": "投稿注意事項", "content": "稿件送出前,請確認內容正確性(包含錯字、斷行、特殊字、空格等)。為公平起見,投稿後不可再修改文章。"}
2310
+ ]`;
2311
+ break;
2312
+ case 'scoringCriteria':
2313
+ element.value = `[
2314
+ {"item": "內容與結構", "percentage": "25%", "description": "切合題旨,思想積極健康,論點合理,文字生動;結構嚴謹,行文流暢,兼具廣度與深度。"},
2315
+ {"item": "邏輯與修辭", "percentage": "25%", "description": "邏輯分明,條理清晰,敘事明白;用字遣詞合宜,修辭靈活優美。"},
2316
+ {"item": "創意與觀察", "percentage": "20%", "description": "富含想像力,觀察微小細節,洞悉人性幽微。"},
2317
+ {"item": "平台練習積分", "percentage": "20%", "description": "2025.5.1-2025.8.30期間,練習篇數與教練評分會影響最終積分。"},
2318
+ {"item": "標點符號與字詞正確", "percentage": "10%", "description": "標點符號運用得宜,詞能達義,無錯別字。"}
2319
+ ]`;
2320
+ break;
2321
+ case 'notes':
2322
+ element.value = `[
2323
+ {"title": "創作注意事項", "content": "作文以白話文寫作,不得使用文言文、詩歌、韻文寫作;使用標準字體,並詳加全形標點符號;內文不得書寫姓名、校名等個人資訊"},
2324
+ {"title": "評審辦法", "content": "本比賽採初審、複審、決審三輪制,聘請樂寫平台資深志工教練群、知名作家、專家學者、與聯發科技長官共同參與評比,確保評選過程公平公正。"}
2325
+ ]`;
2326
+ break;
2327
+ case 'awards':
2328
+ element.value = `[
2329
+ {
2330
+ "title": "勤筆獎勵—企業參訪體驗",
2331
+ "description": "從報名活動到8/31前,於樂寫公益學習平台積分最高的前三十名得主,可獲得聯發科技企業參訪活動體驗資格。"
2332
+ },
2333
+ {
2334
+ "title": "徵文比賽大獎",
2335
+ "description": "本屆徵文比賽前三十名表現優異者獲「大使獎」殊榮",
2336
+ "items": [
2337
+ "世界觀察大使獎:本屆文學獎總表現第一名",
2338
+ "觀察善思大使獎:善於觀察,描寫人事物具體詳細,能在平凡中見不凡",
2339
+ "永續發展大使獎:關懷社會議題,對於弱勢富有同理心,對於土地願意思考如何更好",
2340
+ "勤筆精修大使獎:在樂寫平台願意多次修改文章,志工教練認可勤寫精修的作者",
2341
+ "真誠感動大使獎:表達真實情感,運用富有感情的文字打動人",
2342
+ "創意巧思大使獎:展現創造力,勇於發表個人獨特見解"
2343
+ ]
2344
+ }
2345
+ ]`;
2346
+ break;
2347
+ }
2348
+ }
2349
+ });
2350
+ }
2351
+
2352
  // Load saved variables from localStorage on page load
2353
  window.addEventListener('DOMContentLoaded', function() {
2354
+ setTimeout(() => {
2355
+ // First fix any corrupted values
2356
+ fixCorruptedValues();
2357
+
2358
+ try {
2359
+ const savedVariables = localStorage.getItem('lsw-html-variables');
2360
+ if (savedVariables) {
2361
+ console.log('找到儲存的變數:', savedVariables.substring(0, 100) + '...');
2362
+ document.getElementById('variables').value = savedVariables;
2363
+
2364
+ // Check current mode (default is 'form')
2365
+ const currentMode = document.getElementById('variables-form').classList.contains('hidden') ? 'json' : 'form';
2366
+
2367
+ // If in form mode, update form from JSON
2368
+ if (currentMode === 'form') {
2369
+ try {
2370
+ const jsonData = JSON.parse(savedVariables);
2371
+ Object.keys(jsonData).forEach(key => {
2372
+ const element = document.getElementById(`var-${key}`);
2373
+ if (element) {
2374
+ // Handle array/object fields differently
2375
+ if (['requirements', 'scoringCriteria', 'notes', 'awards'].includes(key)) {
2376
+ element.value = JSON.stringify(jsonData[key], null, 4);
2377
+ } else {
2378
+ element.value = jsonData[key] || '';
2379
+ }
2380
+ }
2381
+ });
2382
+ } catch (e) {
2383
+ console.error('Error parsing saved JSON:', e);
2384
+ }
2385
  }
2386
+
2387
+ console.log('已從 localStorage 載入變數');
2388
+ showToast('已載入上次儲存的設定', 'info');
2389
+ } else {
2390
+ console.log('localStorage 中沒有找到儲存的變數');
2391
  }
2392
+ } catch (e) {
2393
+ console.error('無法從 localStorage 載入:', e);
 
2394
  }
2395
+ }, 100);
 
 
2396
  });
2397
 
2398
  // Don't auto-generate on page load