Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
848 lines (768 loc) · 37.3 KB
/
Copy pathscript.js
File metadata and controls
848 lines (768 loc) · 37.3 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
// 自動轉換URL為超連結
function convertUrlsToLinks(text) {
// URL正則表達式 - 支援 http、https、www 開頭的網址
const urlRegex = /(https?:\/\/[^\s]+|www\.[^\s]+)/gi;
return text.replace(urlRegex, function(url) {
let href = url;
// 如果是 www 開頭,自動加上 https://
if (url.startsWith('www.')) {
href = 'https://' + url;
}
return `<a href="${href}" target="_blank" rel="noopener noreferrer" class="text-blue-600 hover:text-blue-800 underline">${url}</a>`;
});
}// 組別對應表
const departments = {
'shsd': '住宿服務組',
'isrc': '原民中心',
'dsa': '學務長室',
'saad': '就學服務組',
'clss': '生輔教中心',
'hd': '衛生保健組',
'esd': '課外活動組',
'scd': '諮商輔導組',
'hesp': '高教深耕'
};
// 全域變數
let faqData = [];
let expandedCategories = {};
let expandedQuestions = {};
let editingCategory = null;
let editingQuestion = null;
let showPreview = false;
let currentDepartment = 'shsd'; // 預設為住宿服務組
// 拖拽相關變數
let draggedElement = null;
let draggedIndex = -1;
let draggedType = null; // 'category' 或 'question'
let draggedCategoryId = null;
// DOM 元素
let editMode, previewMode, toggleModeBtn, addCategoryBtn, exportBtn, importBtn;
let importInput, emptyState, categoriesContainer, previewContainer, departmentSelect;
// 初始化DOM元素
function initializeElements() {
editMode = document.getElementById('editMode');
previewMode = document.getElementById('previewMode');
toggleModeBtn = document.getElementById('toggleModeBtn');
addCategoryBtn = document.getElementById('addCategoryBtn');
exportBtn = document.getElementById('exportBtn');
importBtn = document.getElementById('importBtn');
importInput = document.getElementById('importInput');
emptyState = document.getElementById('emptyState');
categoriesContainer = document.getElementById('categoriesContainer');
previewContainer = document.getElementById('previewContainer');
departmentSelect = document.getElementById('departmentSelect');
}
// 輔助函數:從ID提取類別編號
function extractCategoryNumber(id) {
const match = id.match(/([a-z]+)_c(\d+)_/);
return match ? parseInt(match[2]) : 0;
}
// 輔助函數:從ID提取問題編號
function extractQuestionNumber(id) {
const match = id.match(/([a-z]+)_c(\d+)_(\d+)/);
// match[2] 是類別編號,從 match[3]中切掉類別編號的長度,剩下的就是問題編號
return match ? parseInt(match[3].slice(match[2].length)) : 0;
}
// 輔助函數:從ID提取部門代碼
function extractDepartmentCode(id) {
const match = id.match(/^([a-z]+)_/);
return match ? match[1] : currentDepartment;
}
// 生成新的類別ID
function generateCategoryId() {
const existingIds = faqData.map(cat => cat.id);
const departmentIds = existingIds.filter(id => id.startsWith(currentDepartment));
const maxClassNumber = Math.max(...departmentIds.map(id => extractCategoryNumber(id)), 0);
const newClassNumber = maxClassNumber + 1;
return `${currentDepartment}_c${newClassNumber}_${newClassNumber}`;
}
// 生成新的問題ID
function generateQuestionId(categoryId) {
const departmentCode = extractDepartmentCode(categoryId);
const classNumber = extractCategoryNumber(categoryId);
const category = faqData.find(cat => cat.id === categoryId);
const maxQuestionNumber = Math.max(...category.questions.map(q => extractQuestionNumber(q.id)), 0);
const newQuestionNumber = maxQuestionNumber + 1;
return `${departmentCode}_c${classNumber}_${classNumber}${newQuestionNumber}`;
}
// 拖拽功能實現
function enableDragAndDrop() {
// 類別拖拽
function handleCategoryDragStart(e, categoryIndex) {
draggedElement = e.currentTarget;
draggedIndex = categoryIndex;
draggedType = 'category';
draggedCategoryId = null;
e.currentTarget.classList.add('dragging');
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text/html', e.currentTarget.outerHTML);
}
function handleCategoryDragEnd(e) {
e.currentTarget.classList.remove('dragging');
clearDragStyles();
}
function handleCategoryDragOver(e) {
e.preventDefault();
e.dataTransfer.dropEffect = 'move';
const categoryCard = e.currentTarget;
const rect = categoryCard.getBoundingClientRect();
const midY = rect.top + rect.height / 2;
if (e.clientY < midY) {
showSortIndicator(categoryCard, 'top');
} else {
showSortIndicator(categoryCard, 'bottom');
}
}
function handleCategoryDrop(e, targetIndex) {
e.preventDefault();
if (draggedType === 'category' && draggedIndex !== targetIndex) {
const draggedCategory = faqData[draggedIndex];
faqData.splice(draggedIndex, 1);
const newIndex = draggedIndex < targetIndex ? targetIndex - 1 : targetIndex;
faqData.splice(newIndex, 0, draggedCategory);
renderEditMode();
showNotification('類別順序已更新', 'success');
}
clearDragStyles();
}
// 問題拖拽
function handleQuestionDragStart(e, categoryId, questionIndex) {
draggedElement = e.currentTarget;
draggedIndex = questionIndex;
draggedType = 'question';
draggedCategoryId = categoryId;
e.currentTarget.classList.add('dragging');
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text/html', e.currentTarget.outerHTML);
}
function handleQuestionDragEnd(e) {
e.currentTarget.classList.remove('dragging');
clearDragStyles();
}
function handleQuestionDragOver(e) {
e.preventDefault();
e.dataTransfer.dropEffect = 'move';
const questionItem = e.currentTarget;
const rect = questionItem.getBoundingClientRect();
const midY = rect.top + rect.height / 2;
if (e.clientY < midY) {
showSortIndicator(questionItem, 'top');
} else {
showSortIndicator(questionItem, 'bottom');
}
}
function handleQuestionDrop(e, targetCategoryId, targetQuestionIndex) {
e.preventDefault();
if (draggedType === 'question') {
const sourceCategoryIndex = faqData.findIndex(cat => cat.id === draggedCategoryId);
const targetCategoryIndex = faqData.findIndex(cat => cat.id === targetCategoryId);
if (sourceCategoryIndex !== -1 && targetCategoryIndex !== -1) {
const draggedQuestion = faqData[sourceCategoryIndex].questions[draggedIndex];
// 從原位置移除
faqData[sourceCategoryIndex].questions.splice(draggedIndex, 1);
// 計算新位置
let newIndex = targetQuestionIndex;
if (draggedCategoryId === targetCategoryId && draggedIndex < targetQuestionIndex) {
newIndex = targetQuestionIndex - 1;
}
// 插入到新位置
faqData[targetCategoryIndex].questions.splice(newIndex, 0, draggedQuestion);
renderEditMode();
showNotification('問題順序已更新', 'success');
}
}
clearDragStyles();
}
// 輔助函數
function showSortIndicator(element, position) {
clearDragStyles();
const indicator = document.createElement('div');
indicator.className = `sort-indicator show ${position}`;
element.style.position = 'relative';
element.appendChild(indicator);
}
function clearDragStyles() {
document.querySelectorAll('.sort-indicator').forEach(el => el.remove());
document.querySelectorAll('.drag-over').forEach(el => el.classList.remove('drag-over'));
}
// 暴露拖拽函數到全域
window.handleCategoryDragStart = handleCategoryDragStart;
window.handleCategoryDragEnd = handleCategoryDragEnd;
window.handleCategoryDragOver = handleCategoryDragOver;
window.handleCategoryDrop = handleCategoryDrop;
window.handleQuestionDragStart = handleQuestionDragStart;
window.handleQuestionDragEnd = handleQuestionDragEnd;
window.handleQuestionDragOver = handleQuestionDragOver;
window.handleQuestionDrop = handleQuestionDrop;
}
// 更新部門資訊顯示
function updateDepartmentInfo() {
const departmentInfo = document.getElementById('departmentInfo');
const currentDeptDisplay = document.getElementById('currentDeptDisplay');
const idFormatExample = document.getElementById('idFormatExample');
const departmentName = departments[currentDepartment];
if (departmentInfo) {
departmentInfo.innerHTML = `
<div class="flex items-center space-x-2 text-sm text-gray-600">
<span class="px-2 py-1 bg-blue-100 text-blue-700 rounded font-mono">${currentDepartment}</span>
<span>${departmentName}</span>
</div>
`;
}
if (currentDeptDisplay) {
currentDeptDisplay.innerHTML = `
<span class="font-mono text-blue-700">${currentDepartment}</span>
<span class="text-blue-600">${departmentName}</span>
`;
}
if (idFormatExample) {
idFormatExample.textContent = `${currentDepartment}_c1_1`;
}
}
// 新增類別
function addCategory() {
const newId = generateCategoryId();
const newCategory = {
id: newId,
categoryName: "",
questions: []
};
faqData.push(newCategory);
expandedCategories[newId] = true;
editingCategory = newId;
renderEditMode();
}
// 新增問題
function addQuestion(categoryId) {
const newId = generateQuestionId(categoryId);
const newQuestion = {
id: newId,
question: "",
answer: ""
};
const categoryIndex = faqData.findIndex(cat => cat.id === categoryId);
faqData[categoryIndex].questions.push(newQuestion);
expandedQuestions[newId] = true;
editingQuestion = newId;
renderEditMode();
}
// 刪除類別
function deleteCategory(categoryId) {
if (confirm('確定要刪除此類別及其所有問題嗎?')) {
faqData = faqData.filter(cat => cat.id !== categoryId);
editingCategory = null;
renderEditMode();
renderPreview();
}
}
// 刪除問題
function deleteQuestion(categoryId, questionId) {
if (confirm('確定要刪除此問題嗎?')) {
const categoryIndex = faqData.findIndex(cat => cat.id === categoryId);
faqData[categoryIndex].questions = faqData[categoryIndex].questions.filter(q => q.id !== questionId);
editingQuestion = null;
renderEditMode();
renderPreview();
}
}
// 儲存類別編輯
function saveCategoryEdit(categoryId, newName) {
const categoryIndex = faqData.findIndex(cat => cat.id === categoryId);
faqData[categoryIndex].categoryName = newName;
editingCategory = null;
renderEditMode();
renderPreview();
}
// 儲存問題編輯
function saveQuestionEdit(categoryId, questionId, newQuestion, newAnswer) {
const categoryIndex = faqData.findIndex(cat => cat.id === categoryId);
const questionIndex = faqData[categoryIndex].questions.findIndex(q => q.id === questionId);
// 自動轉換答案中的URL為超連結
const processedAnswer = convertUrlsToLinks(newAnswer);
faqData[categoryIndex].questions[questionIndex].question = newQuestion;
faqData[categoryIndex].questions[questionIndex].answer = processedAnswer;
editingQuestion = null;
renderEditMode();
renderPreview();
}
// 切換類別展開狀態
function toggleCategory(categoryId) {
expandedCategories[categoryId] = !expandedCategories[categoryId];
if (showPreview) {
renderPreview();
} else {
renderEditMode();
}
}
// 切換問題展開狀態
function toggleQuestion(questionId) {
expandedQuestions[questionId] = !expandedQuestions[questionId];
if (showPreview) {
renderPreview();
} else {
renderEditMode();
}
}
// 渲染編輯模式
function renderEditMode() {
if (faqData.length === 0) {
emptyState.style.display = 'block';
categoriesContainer.innerHTML = '';
return;
}
emptyState.style.display = 'none';
categoriesContainer.innerHTML = faqData.map((category, categoryIndex) => `
<div class="card-modern p-6 sortable-item draggable"
draggable="true"
ondragstart="handleCategoryDragStart(event, ${categoryIndex})"
ondragend="handleCategoryDragEnd(event)"
ondragover="handleCategoryDragOver(event)"
ondrop="handleCategoryDrop(event, ${categoryIndex})">
<div class="category-header p-6 mb-4">
<div class="flex items-center justify-between">
<div class="flex items-center space-x-4">
<!-- 拖拽手柄 -->
<div class="drag-handle p-1" title="拖拽移動類別">
<svg class="icon" viewBox="0 0 24 24">
<line x1="3" y1="6" x2="21" y2="6"/>
<line x1="3" y1="12" x2="21" y2="12"/>
<line x1="3" y1="18" x2="21" y2="18"/>
</svg>
</div>
<button onclick="toggleCategory('${category.id}')" class="btn-modern p-3">
<svg class="icon transition-transform" viewBox="0 0 24 24">
${expandedCategories[category.id] ?
'<polyline points="6,9 12,15 18,9"></polyline>' :
'<polyline points="9,18 15,12 9,6"></polyline>'
}
</svg>
</button>
${editingCategory === category.id ?
`<input type="text" value="${category.categoryName}"
class="input-modern text-xl font-bold flex-1"
placeholder="輸入類別名稱"
id="edit-cat-${category.id}">` :
`<h3 class="text-xl font-bold text-gray-800">${category.categoryName || '未命名類別'}</h3>`
}
<span class="px-3 py-1 bg-blue-50 text-blue-700 text-sm rounded-lg font-medium">
${category.id}
</span>
<span class="px-2 py-1 bg-gray-100 text-gray-600 text-xs rounded">
${category.questions.length} 個問題
</span>
</div>
<div class="flex items-center space-x-2">
${editingCategory === category.id ? `
<button onclick="saveCategoryFromInput('${category.id}')" class="btn-modern btn-success p-3" title="儲存">
<svg class="icon" viewBox="0 0 24 24">
<path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z"/>
<polyline points="17,21 17,13 7,13 7,21"/>
<polyline points="7,3 7,8 15,8"/>
</svg>
</button>
<button onclick="cancelEdit()" class="btn-modern p-3 hover:bg-gray-50" title="取消">
<svg class="icon" viewBox="0 0 24 24">
<line x1="18" y1="6" x2="6" y2="18"/>
<line x1="6" y1="6" x2="18" y2="18"/>
</svg>
</button>
` : `
<button onclick="editCategory('${category.id}')" class="btn-modern p-3 hover:bg-blue-50 text-blue-600" title="編輯類別">
<svg class="icon" viewBox="0 0 24 24">
<path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/>
<path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/>
</svg>
</button>
<button onclick="addQuestion('${category.id}')" class="btn-modern btn-success p-3" title="新增問題">
<svg class="icon" viewBox="0 0 24 24">
<line x1="12" y1="5" x2="12" y2="19"/>
<line x1="5" y1="12" x2="19" y2="12"/>
</svg>
</button>
<button onclick="deleteCategory('${category.id}')" class="btn-modern btn-danger p-3" title="刪除類別">
<svg class="icon" viewBox="0 0 24 24">
<polyline points="3,6 5,6 21,6"/>
<path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/>
</svg>
</button>
`}
</div>
</div>
</div>
${expandedCategories[category.id] ? `
<div class="space-y-4 ml-4 sortable-container">
${category.questions.length === 0 ? `
<div class="empty-state p-8 text-center">
<svg class="w-12 h-12 mx-auto text-gray-400 mb-4" viewBox="0 0 24 24" fill="none" stroke="currentColor">
<circle cx="12" cy="12" r="10"/>
<path d="M9.09 9a3 3 0 0 1 5.83 1c0 2-3 3-3 3"/>
<line x1="12" y1="17" x2="12.01" y2="17"/>
</svg>
<p class="text-gray-500 mb-4">此類別尚無問題</p>
<button onclick="addQuestion('${category.id}')" class="btn-modern btn-success px-6 py-3">
新增問題
</button>
</div>
` : category.questions.map((question, questionIndex) => `
<div class="question-item p-4 sortable-item draggable"
draggable="true"
ondragstart="handleQuestionDragStart(event, '${category.id}', ${questionIndex})"
ondragend="handleQuestionDragEnd(event)"
ondragover="handleQuestionDragOver(event)"
ondrop="handleQuestionDrop(event, '${category.id}', ${questionIndex})">
<div class="flex items-center justify-between mb-3">
<div class="flex items-center space-x-3 flex-1">
<!-- 問題拖拽手柄 -->
<div class="drag-handle p-1" title="拖拽移動問題">
<svg class="w-4 h-4" viewBox="0 0 24 24" fill="none" stroke="currentColor">
<line x1="3" y1="6" x2="21" y2="6"/>
<line x1="3" y1="12" x2="21" y2="12"/>
<line x1="3" y1="18" x2="21" y2="18"/>
</svg>
</div>
<button onclick="toggleQuestion('${question.id}')" class="btn-modern p-2">
<svg class="icon transition-transform" viewBox="0 0 24 24">
${expandedQuestions[question.id] ?
'<polyline points="6,9 12,15 18,9"></polyline>' :
'<polyline points="9,18 15,12 9,6"></polyline>'
}
</svg>
</button>
${editingQuestion === question.id ?
`<input type="text" value="${question.question}"
class="input-modern font-semibold flex-1"
placeholder="輸入問題標題"
id="edit-q-${question.id}">` :
`<h4 class="font-semibold text-gray-800 flex-1">${question.question || '未命名問題'}</h4>`
}
<span class="px-2 py-1 bg-gray-100 text-gray-600 text-xs rounded-lg font-mono">
${question.id}
</span>
</div>
<div class="flex items-center space-x-1 ml-3">
${editingQuestion === question.id ? `
<button onclick="saveQuestionFromInput('${category.id}', '${question.id}')" class="btn-modern btn-success p-2" title="儲存">
<svg class="icon" viewBox="0 0 24 24">
<path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z"/>
<polyline points="17,21 17,13 7,13 7,21"/>
<polyline points="7,3 7,8 15,8"/>
</svg>
</button>
<button onclick="cancelEdit()" class="btn-modern p-2 hover:bg-gray-50" title="取消">
<svg class="icon" viewBox="0 0 24 24">
<line x1="18" y1="6" x2="6" y2="18"/>
<line x1="6" y1="6" x2="18" y2="18"/>
</svg>
</button>
` : `
<button onclick="editQuestion('${question.id}')" class="btn-modern p-2 hover:bg-blue-50 text-blue-600" title="編輯問題">
<svg class="icon" viewBox="0 0 24 24">
<path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/>
<path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/>
</svg>
</button>
<button onclick="deleteQuestion('${category.id}', '${question.id}')" class="btn-modern btn-danger p-2" title="刪除問題">
<svg class="icon" viewBox="0 0 24 24">
<polyline points="3,6 5,6 21,6"/>
<path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/>
</svg>
</button>
`}
</div>
</div>
${expandedQuestions[question.id] ? `
<div class="ml-10 mt-4">
<div class="bg-gray-50 p-4 rounded-lg border border-gray-200">
${editingQuestion === question.id ?
`<div class="space-y-3">
<textarea id="answer-${question.id}" class="input-modern w-full min-h-[120px] resize-none" placeholder="輸入答案內容(網址會自動轉換為超連結)">${question.answer.replace(/<a[^>]*>(.*?)<\/a>/gi, '$1')}</textarea>
<div class="flex space-x-2">
<button onclick="saveQuestionFromInput('${category.id}', '${question.id}')" class="btn-modern btn-success px-4 py-2 text-sm">
儲存
</button>
<button onclick="cancelEdit()" class="btn-modern px-4 py-2 text-sm">
取消
</button>
</div>
<div class="text-xs text-gray-500">
💡 提示:直接輸入網址(如 https://example.com 或 www.example.com),儲存時會自動轉換為可點擊的超連結
</div>
</div>` :
`<div class="text-gray-700 leading-relaxed">${question.answer}</div>`
}
</div>
</div>
` : ''}
</div>
`).join('')}
</div>
` : ''}
</div>
`).join('');
// 自動聚焦到編輯中的元素
setTimeout(() => {
if (editingCategory) {
const input = document.getElementById(`edit-cat-${editingCategory}`);
if (input) {
input.focus();
input.select();
}
}
if (editingQuestion) {
const input = document.getElementById(`edit-q-${editingQuestion}`);
if (input) {
input.focus();
input.select();
}
}
}, 100);
}
// 渲染預覽模式
function renderPreview() {
if (faqData.length === 0) {
previewContainer.innerHTML = `
<div class="empty-state text-center py-16">
<div class="w-16 h-16 mx-auto bg-gray-100 rounded-full flex items-center justify-center mb-4">
<svg class="w-8 h-8 text-gray-400" viewBox="0 0 24 24" fill="none" stroke="currentColor">
<circle cx="12" cy="12" r="10"/>
<path d="M9.09 9a3 3 0 0 1 5.83 1c0 2-3 3-3 3"/>
<line x1="12" y1="17" x2="12.01" y2="17"/>
</svg>
</div>
<h3 class="text-xl font-semibold text-gray-600 mb-2">尚無內容</h3>
<p class="text-gray-500">請先在編輯模式中新增類別和問題</p>
</div>
`;
return;
}
previewContainer.innerHTML = faqData.map((category, index) => `
<div class="card-modern mb-6 overflow-hidden">
<button onclick="toggleCategory('preview_${category.id}')"
class="w-full p-6 text-left bg-gray-50 hover:bg-gray-100 transition-all duration-200 flex items-center justify-between">
<div class="flex items-center space-x-4">
<div class="w-10 h-10 bg-blue-500 rounded-lg flex items-center justify-center">
<svg class="w-5 h-5 text-white" viewBox="0 0 24 24" fill="none" stroke="currentColor">
<path d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
</svg>
</div>
<div>
<h3 class="text-xl font-bold text-gray-800">
${category.categoryName}
</h3>
<p class="text-sm text-gray-500">${category.questions.length} 個問題</p>
</div>
</div>
<svg class="icon-lg text-gray-400 transition-all" viewBox="0 0 24 24">
${expandedCategories[`preview_${category.id}`] ?
'<polyline points="6,9 12,15 18,9"></polyline>' :
'<polyline points="9,18 15,12 9,6"></polyline>'
}
</svg>
</button>
${expandedCategories[`preview_${category.id}`] ? `
<div class="p-6 space-y-4 bg-gray-50">
${category.questions.map((question, qIndex) => `
<div class="question-item">
<button onclick="toggleQuestion('preview_${question.id}')"
class="w-full p-4 text-left hover:bg-white transition-all duration-200 rounded-lg flex items-center justify-between">
<div class="flex items-center space-x-3">
<div class="w-6 h-6 bg-green-500 rounded-lg flex items-center justify-center">
<svg class="w-3 h-3 text-white" viewBox="0 0 24 24" fill="none" stroke="currentColor">
<circle cx="12" cy="12" r="10"/>
<path d="M9.09 9a3 3 0 0 1 5.83 1c0 2-3 3-3 3"/>
<line x1="12" y1="17" x2="12.01" y2="17"/>
</svg>
</div>
<span class="font-medium text-gray-800">
${question.question}
</span>
</div>
<svg class="icon text-gray-400 transition-all" viewBox="0 0 24 24">
${expandedQuestions[`preview_${question.id}`] ?
'<polyline points="6,9 12,15 18,9"></polyline>' :
'<polyline points="9,18 15,12 9,6"></polyline>'
}
</svg>
</button>
${expandedQuestions[`preview_${question.id}`] ? `
<div class="ml-9 mr-6 mb-4 p-4 bg-white rounded-lg border border-gray-200">
<div class="text-gray-700 leading-relaxed">
${question.answer}
</div>
</div>
` : ''}
</div>
`).join('')}
</div>
` : ''}
</div>
`).join('');
}
// 編輯類別
function editCategory(categoryId) {
editingCategory = categoryId;
renderEditMode();
}
// 編輯問題
function editQuestion(questionId) {
editingQuestion = questionId;
renderEditMode();
}
// 從輸入框儲存類別編輯
function saveCategoryFromInput(categoryId) {
const categoryInput = document.getElementById(`edit-cat-${categoryId}`);
const newName = categoryInput ? categoryInput.value : '';
saveCategoryEdit(categoryId, newName);
}
// 從輸入框儲存問題編輯
function saveQuestionFromInput(categoryId, questionId) {
const questionInput = document.getElementById(`edit-q-${questionId}`);
const answerInput = document.getElementById(`answer-${questionId}`);
const newQuestion = questionInput ? questionInput.value : '';
const newAnswer = answerInput ? answerInput.value : '';
saveQuestionEdit(categoryId, questionId, newQuestion, newAnswer);
}
// 取消編輯
function cancelEdit() {
editingCategory = null;
editingQuestion = null;
renderEditMode();
}
// 切換模式
function toggleMode() {
showPreview = !showPreview;
if (showPreview) {
editMode.style.display = 'none';
previewMode.style.display = 'block';
toggleModeBtn.innerHTML = `
<svg class="icon" viewBox="0 0 24 24">
<path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/>
<path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/>
</svg>
<span>編輯模式</span>
`;
renderPreview();
} else {
editMode.style.display = 'block';
previewMode.style.display = 'none';
toggleModeBtn.innerHTML = `
<svg class="icon" viewBox="0 0 24 24">
<path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/>
<circle cx="12" cy="12" r="3"/>
</svg>
<span>預覽模式</span>
`;
renderEditMode();
}
}
// 匯出JSON
function exportJSON() {
const dataStr = JSON.stringify(faqData, null, 2);
const dataUri = 'data:application/json;charset=utf-8,'+ encodeURIComponent(dataStr);
const exportFileDefaultName = `faq_${currentDepartment}_${new Date().toISOString().split('T')[0]}.json`;
const linkElement = document.createElement('a');
linkElement.setAttribute('href', dataUri);
linkElement.setAttribute('download', exportFileDefaultName);
linkElement.click();
// 顯示成功提示
showNotification('匯出成功!', 'success');
}
// 匯入JSON
function importJSON(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
try {
const importedData = JSON.parse(e.target.result);
faqData = importedData;
// 自動偵測部門代碼
if (faqData.length > 0) {
const firstId = faqData[0].id;
const detectedDepartment = extractDepartmentCode(firstId);
if (departments[detectedDepartment]) {
currentDepartment = detectedDepartment;
departmentSelect.value = currentDepartment;
updateDepartmentInfo();
}
}
expandedCategories = {};
expandedQuestions = {};
editingCategory = null;
editingQuestion = null;
renderEditMode();
renderPreview();
showNotification('匯入成功!', 'success');
} catch (error) {
showNotification('匯入失敗:JSON格式錯誤', 'error');
}
};
reader.readAsText(file);
}
}
// 顯示通知
function showNotification(message, type = 'info') {
const notification = document.createElement('div');
notification.className = `notification ${type}`;
notification.innerHTML = `
<div class="flex items-center space-x-3">
<svg class="w-5 h-5" viewBox="0 0 24 24" fill="none" stroke="currentColor">
${type === 'success' ? '<path d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>' :
type === 'error' ? '<circle cx="12" cy="12" r="10"/><line x1="15" y1="9" x2="9" y2="15"/><line x1="9" y1="9" x2="15" y2="15"/>' :
'<circle cx="12" cy="12" r="10"/><path d="M12 16v-4"/><path d="M12 8h.01"/>'}
</svg>
<span class="font-medium">${message}</span>
</div>
`;
document.body.appendChild(notification);
setTimeout(() => {
notification.classList.add('show');
}, 100);
setTimeout(() => {
notification.classList.remove('show');
setTimeout(() => {
if (notification.parentNode) {
document.body.removeChild(notification);
}
}, 300);
}, 3000);
}
// 綁定事件監聽器
function bindEventListeners() {
if (toggleModeBtn) toggleModeBtn.addEventListener('click', toggleMode);
if (addCategoryBtn) addCategoryBtn.addEventListener('click', addCategory);
if (exportBtn) exportBtn.addEventListener('click', exportJSON);
if (importBtn) importBtn.addEventListener('click', () => importInput.click());
if (importInput) importInput.addEventListener('change', importJSON);
// 綁定部門選擇事件
if (departmentSelect) {
departmentSelect.addEventListener('change', function(event) {
currentDepartment = event.target.value;
updateDepartmentInfo();
});
}
// 綁定快速開始按鈕
const quickStartBtn = document.getElementById('quickStartBtn');
if (quickStartBtn) {
quickStartBtn.addEventListener('click', addCategory);
}
}
// DOM加載完成後初始化
document.addEventListener('DOMContentLoaded', function() {
initializeElements();
bindEventListeners();
enableDragAndDrop(); // 啟用拖拽功能
updateDepartmentInfo();
renderEditMode();
});
// 全域函數暴露
window.toggleCategory = toggleCategory;
window.toggleQuestion = toggleQuestion;
window.addQuestion = addQuestion;
window.editCategory = editCategory;
window.editQuestion = editQuestion;
window.deleteCategory = deleteCategory;
window.deleteQuestion = deleteQuestion;
window.saveCategoryFromInput = saveCategoryFromInput;
window.saveQuestionFromInput = saveQuestionFromInput;
window.cancelEdit = cancelEdit;