-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathscript.js
More file actions
449 lines (373 loc) · 14.2 KB
/
script.js
File metadata and controls
449 lines (373 loc) · 14.2 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
// 複製 Agent 設置指令
async function copyAgentPrompt() {
const promptElement = document.getElementById('agentPrompt');
const button = event.target;
const originalText = button.textContent;
try {
await navigator.clipboard.writeText(promptElement.textContent);
button.textContent = '已複製';
button.style.background = '#48bb78';
setTimeout(() => {
button.textContent = originalText;
button.style.background = '#007aff';
}, 2000);
} catch (err) {
// 降級方案
const textArea = document.createElement('textarea');
textArea.value = promptElement.textContent;
textArea.style.position = 'fixed';
textArea.style.left = '-999999px';
document.body.appendChild(textArea);
textArea.select();
try {
document.execCommand('copy');
button.textContent = '已複製';
button.style.background = '#48bb78';
setTimeout(() => {
button.textContent = originalText;
button.style.background = '#007aff';
}, 2000);
} catch (err) {
button.textContent = '複製失敗';
setTimeout(() => {
button.textContent = originalText;
}, 2000);
}
document.body.removeChild(textArea);
}
}
// 監聽 details 元素的開啟/關閉
document.addEventListener('DOMContentLoaded', function() {
const detailsElement = document.querySelector('details');
const copyButton = detailsElement.querySelector('button[onclick="copyAgentPrompt()"]');
// 監聽 toggle 事件
detailsElement.addEventListener('toggle', function() {
if (detailsElement.open) {
copyButton.style.display = 'block';
} else {
copyButton.style.display = 'none';
}
});
});
// 儲存和取得捲動位置
function saveScrollPosition(path, position) {
localStorage.setItem(`scroll_${path}`, position);
}
function getScrollPosition(path) {
return parseInt(localStorage.getItem(`scroll_${path}`) || '0');
}
// 儲存當前文件路徑
let currentMarkdownPath = '';
// 載入並顯示 Markdown
async function loadMarkdown(path, title) {
const modal = document.getElementById('markdownModal');
const body = document.getElementById('markdownBody');
const container = body.parentElement;
// 儲存當前路徑
currentMarkdownPath = path;
// 顯示 Modal
modal.style.display = 'block';
body.innerHTML = '<div class="markdown-loading">載入中...</div>';
try {
// Fetch Markdown 檔案,加入防快取參數
const timestamp = new Date().getTime();
const response = await fetch(`${path}?t=${timestamp}`, {
cache: 'no-cache'
});
if (!response.ok) {
throw new Error('無法載入文件');
}
const markdown = await response.text();
// 使用 marked.js 轉換 Markdown 為 HTML
const html = marked.parse(markdown);
// 顯示內容
body.innerHTML = html;
// 為所有程式碼區塊添加複製按鈕
addCopyButtons();
// 恢復上次的捲動位置
const savedPosition = getScrollPosition(path);
if (savedPosition > 0) {
// 使用 setTimeout 確保內容已經渲染
setTimeout(() => {
container.scrollTop = savedPosition;
}, 100);
} else {
// 如果沒有儲存的位置,滾動到頂部
container.scrollTop = 0;
}
// 監聽捲動事件,儲存位置
container.onscroll = debounce(() => {
if (currentMarkdownPath) {
saveScrollPosition(currentMarkdownPath, container.scrollTop);
}
}, 300);
} catch (error) {
body.innerHTML = `
<div style="text-align: center; padding: 50px; color: #f00;">
<h2>載入失敗</h2>
<p>無法載入教學內容</p>
<p style="color: #666; font-size: 0.9rem;">${error.message}</p>
</div>
`;
}
}
// 防抖函數
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
// 關閉 Markdown Modal
function closeMarkdown() {
const modal = document.getElementById('markdownModal');
const container = document.getElementById('markdownBody').parentElement;
// 儲存最後的捲動位置
if (currentMarkdownPath) {
saveScrollPosition(currentMarkdownPath, container.scrollTop);
}
// 清理事件監聽器
container.onscroll = null;
// 關閉 Modal
modal.style.display = 'none';
}
// 點擊 Modal 外部關閉
window.onclick = function(event) {
const modal = document.getElementById('markdownModal');
if (event.target == modal) {
closeMarkdown();
}
}
// ESC 鍵關閉
document.addEventListener('keydown', function(event) {
if (event.key === 'Escape') {
closeMarkdown();
}
});
// 添加複製按鈕到程式碼區塊
function addCopyButtons() {
const codeBlocks = document.querySelectorAll('.markdown-content pre');
codeBlocks.forEach((block, index) => {
// 確保 pre 元素是 relative 定位
block.style.position = 'relative';
// 獲取程式碼語言類型
const codeElement = block.querySelector('code');
const classList = codeElement.className.split(' ');
let language = '';
classList.forEach(cls => {
if (cls.startsWith('language-')) {
language = cls.replace('language-', '').toUpperCase();
}
});
// 創建按鈕容器
const buttonContainer = document.createElement('div');
buttonContainer.style.cssText = 'position: absolute; top: 10px; right: 10px; display: flex; gap: 5px; align-items: center; z-index: 10;';
// 如果有語言標示,添加語言標籤
if (language) {
const langLabel = document.createElement('span');
langLabel.className = 'lang-label';
langLabel.style.cssText = 'font-size: 12px; color: #666; background: #f0f0f0; padding: 2px 8px; border-radius: 3px;';
langLabel.textContent = language;
buttonContainer.appendChild(langLabel);
}
// 創建複製按鈕
const button = document.createElement('button');
button.className = 'copy-button';
button.textContent = '複製';
button.setAttribute('data-code-index', index);
// 複製功能
button.addEventListener('click', async function() {
const code = block.querySelector('code');
let textToCopy = code.textContent || code.innerText;
// 移除多餘的空白
textToCopy = textToCopy.trim();
try {
await navigator.clipboard.writeText(textToCopy);
// 顯示複製成功
button.textContent = '已複製';
button.classList.add('copied');
// 2秒後恢復
setTimeout(() => {
button.textContent = '複製';
button.classList.remove('copied');
}, 2000);
} catch (err) {
// 降級方案:使用傳統方法
const textArea = document.createElement('textarea');
textArea.value = textToCopy;
textArea.style.position = 'fixed';
textArea.style.left = '-999999px';
document.body.appendChild(textArea);
textArea.select();
try {
document.execCommand('copy');
button.textContent = '已複製';
button.classList.add('copied');
setTimeout(() => {
button.textContent = '複製';
button.classList.remove('copied');
}, 2000);
} catch (err) {
button.textContent = '複製失敗';
setTimeout(() => {
button.textContent = '複製';
}, 2000);
}
document.body.removeChild(textArea);
}
});
// 添加按鈕到容器
buttonContainer.appendChild(button);
// 添加容器到程式碼區塊
block.appendChild(buttonContainer);
});
}
// 生成 QR Code
window.onload = function() {
const currentUrl = window.location.href;
const qrcodeContainer = document.getElementById('qrcode');
// 生成 QR Code
new QRCode(qrcodeContainer, {
text: currentUrl,
width: 200,
height: 200,
colorDark: "#000000",
colorLight: "#ffffff",
correctLevel: QRCode.CorrectLevel.H
});
// 初始化場景滾動
initScenarioScroll();
}
// 場景導航功能
function scrollScenarios(direction) {
const scenarioList = document.querySelector('.scenario-list');
const scenarioItems = document.querySelectorAll('.scenario-item');
if (!scenarioList || !scenarioItems.length) return;
// 計算每個項目的寬度和間距
const itemWidth = scenarioItems[0].offsetWidth + 20; // 包含 gap
const currentScroll = scenarioList.scrollLeft;
const maxScroll = scenarioList.scrollWidth - scenarioList.clientWidth;
let targetScroll;
if (direction === 'prev') {
targetScroll = Math.max(0, currentScroll - itemWidth);
} else {
targetScroll = Math.min(maxScroll, currentScroll + itemWidth);
}
scenarioList.scrollTo({
left: targetScroll,
behavior: 'smooth'
});
// 更新按鈕狀態
setTimeout(updateNavButtons, 300);
}
// 更新導航按鈕狀態
function updateNavButtons() {
const scenarioList = document.querySelector('.scenario-list');
const prevBtn = document.querySelector('.scenario-nav-btn.prev');
const nextBtn = document.querySelector('.scenario-nav-btn.next');
if (!scenarioList || !prevBtn || !nextBtn) return;
const scrollLeft = scenarioList.scrollLeft;
const maxScroll = scenarioList.scrollWidth - scenarioList.clientWidth;
// 更新上一個按鈕
if (scrollLeft <= 10) {
prevBtn.classList.add('disabled');
} else {
prevBtn.classList.remove('disabled');
}
// 更新下一個按鈕
if (scrollLeft >= maxScroll - 10) {
nextBtn.classList.add('disabled');
} else {
nextBtn.classList.remove('disabled');
}
}
// 場景滾動功能
function initScenarioScroll() {
const scenarioList = document.querySelector('.scenario-list');
const scrollDots = document.querySelectorAll('.scroll-dot');
const scenarioItems = document.querySelectorAll('.scenario-item');
const scenarioWrapper = document.querySelector('.scenario-wrapper');
const scenariosSection = document.querySelector('.scenarios');
if (!scenarioList || !scrollDots.length || !scenarioItems.length) return;
// 計算每個項目的寬度和間距
const itemWidth = scenarioItems[0].offsetWidth + 20; // 包含 gap
// 更新滾動指示器
function updateScrollIndicator() {
const scrollLeft = scenarioList.scrollLeft;
const scrollWidth = scenarioList.scrollWidth;
const clientWidth = scenarioList.clientWidth;
// 計算當前顯示的場景索引
const currentIndex = Math.round(scrollLeft / itemWidth);
// 更新指示器狀態
scrollDots.forEach((dot, index) => {
if (index === currentIndex) {
dot.classList.add('active');
} else {
dot.classList.remove('active');
}
});
// 檢查滾動位置,控制左右漸變
if (scrollLeft > 10) {
scenarioWrapper.classList.add('scrolled');
} else {
scenarioWrapper.classList.remove('scrolled');
}
if (scrollLeft + clientWidth >= scrollWidth - 10) {
scenarioWrapper.classList.add('scrolled-end');
} else {
scenarioWrapper.classList.remove('scrolled-end');
}
}
// 監聽滾動事件
let scrollTimeout;
scenarioList.addEventListener('scroll', () => {
clearTimeout(scrollTimeout);
scrollTimeout = setTimeout(() => {
updateScrollIndicator();
updateNavButtons();
}, 50);
});
// 點擊指示器滾動到對應場景
scrollDots.forEach((dot, index) => {
dot.addEventListener('click', () => {
const targetScroll = index * itemWidth;
scenarioList.scrollTo({
left: targetScroll,
behavior: 'smooth'
});
});
});
// 初始化指示器狀態
updateScrollIndicator();
updateNavButtons();
// 添加觸摸滑動支持
let startX = 0;
let scrollLeftStart = 0;
let isDragging = false;
scenarioList.addEventListener('mousedown', (e) => {
isDragging = true;
startX = e.pageX - scenarioList.offsetLeft;
scrollLeftStart = scenarioList.scrollLeft;
scenarioList.style.cursor = 'grabbing';
});
scenarioList.addEventListener('mouseleave', () => {
isDragging = false;
scenarioList.style.cursor = 'grab';
});
scenarioList.addEventListener('mouseup', () => {
isDragging = false;
scenarioList.style.cursor = 'grab';
});
scenarioList.addEventListener('mousemove', (e) => {
if (!isDragging) return;
e.preventDefault();
const x = e.pageX - scenarioList.offsetLeft;
const walk = (x - startX) * 1.5;
scenarioList.scrollLeft = scrollLeftStart - walk;
});
}