Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
93 lines (80 loc) · 3.31 KB
/
Copy pathscript.js
File metadata and controls
93 lines (80 loc) · 3.31 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
document.addEventListener("DOMContentLoaded", function () {
const searchBox = document.getElementById("searchBox");
const categoryFilter = document.getElementById("categoryFilter");
const commandsTable = document.getElementById("commandsTable");
const themeToggle = document.getElementById("themeToggle");
// Theme handling
function getPreferredTheme() {
const storedTheme = localStorage.getItem('theme');
if (storedTheme) {
return storedTheme;
}
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
}
function setTheme(theme) {
document.documentElement.setAttribute('data-theme', theme);
localStorage.setItem('theme', theme);
}
// Set initial theme
setTheme(getPreferredTheme());
// Theme toggle handler
themeToggle.addEventListener('click', () => {
const currentTheme = document.documentElement.getAttribute('data-theme');
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
setTheme(newTheme);
});
// Listen for system theme changes
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
if (!localStorage.getItem('theme')) {
setTheme(e.matches ? 'dark' : 'light');
}
});
// Fetch and populate commands
let commands = [];
fetch('commands.json')
.then(response => response.json())
.then(data => {
commands = data;
populateCategoryFilter(commands);
displayCommands(commands);
});
function populateCategoryFilter(data) {
const categories = new Set();
data.forEach(category => {
categories.add(category.category);
});
categories.forEach(category => {
const option = document.createElement('option');
option.value = category;
option.textContent = category;
categoryFilter.appendChild(option);
});
}
function displayCommands(data) {
const searchTerm = searchBox.value.toLowerCase();
const selectedCategory = categoryFilter.value;
commandsTable.innerHTML = '';
data.forEach(category => {
if (selectedCategory === 'all' || category.category === selectedCategory) {
category.commands.forEach(cmd => {
if (searchTerm === '' ||
cmd.command.toLowerCase().includes(searchTerm) ||
cmd.description.toLowerCase().includes(searchTerm) ||
cmd.keywords.some(keyword => keyword.toLowerCase().includes(searchTerm))) {
const row = document.createElement('tr');
row.innerHTML = `
<td>${cmd.command}</td>
<td>${cmd.description}</td>
<td>${cmd.keywords.join(', ')}</td>
<td>${category.category}</td>
`;
commandsTable.appendChild(row);
}
});
}
});
}
// Event listeners for search and filter
searchBox.addEventListener('input', () => displayCommands(commands));
categoryFilter.addEventListener('change', () => displayCommands(commands));
});