-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_part1.js
More file actions
executable file
·418 lines (355 loc) · 14.7 KB
/
Copy pathscript_part1.js
File metadata and controls
executable file
·418 lines (355 loc) · 14.7 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
// ===== GIT WORKTREE GUIDE - JAVASCRIPT FUNCTIONALITY =====
// ===== ОСНОВНЫЕ ПЕРЕМЕННЫЕ И КОНСТАНТЫ =====
const THEME_KEY = 'git-worktree-theme';
const SCROLL_THRESHOLD = 100;
const ANIMATION_DELAY = 100;
// DOM элементы
let themeToggle;
let mobileMenuToggle;
let mobileMenu;
let navLinks;
let scrollToTopBtn;
// ===== ИНИЦИАЛИЗАЦИЯ =====
document.addEventListener('DOMContentLoaded', function() {
initializeTheme();
initializeNavigation();
initializeScrollEffects();
initializeCodeBlocks();
initializeMobileMenu();
initializeInteractiveElements();
initializeScrollToTop();
injectAdditionalStyles();
console.log('🚀 Git Worktree Guide - JavaScript initialized');
});
// ===== УПРАВЛЕНИЕ ТЕМОЙ =====
function initializeTheme() {
// Создаем кнопку переключения темы если её нет
createThemeToggle();
// Загружаем сохраненную тему или используем системную
const savedTheme = localStorage.getItem(THEME_KEY);
const systemTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
const currentTheme = savedTheme || systemTheme;
// Применяем тему
setTheme(currentTheme);
// Слушаем изменения системной темы
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
if (!localStorage.getItem(THEME_KEY)) {
setTheme(e.matches ? 'dark' : 'light');
}
});
}
function createThemeToggle() {
// Проверяем, есть ли уже кнопка
themeToggle = document.querySelector('.theme-toggle');
if (!themeToggle) {
themeToggle = document.createElement('button');
themeToggle.className = 'theme-toggle';
themeToggle.setAttribute('aria-label', 'Переключить тему');
themeToggle.innerHTML = `
<svg class="sun-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<circle cx="12" cy="12" r="5"></circle>
<line x1="12" y1="1" x2="12" y2="3"></line>
<line x1="12" y1="21" x2="12" y2="23"></line>
<line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line>
<line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line>
<line x1="1" y1="12" x2="3" y2="12"></line>
<line x1="21" y1="12" x2="23" y2="12"></line>
<line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line>
<line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line>
</svg>
<svg class="moon-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path>
</svg>
`;
document.body.appendChild(themeToggle);
}
// Добавляем обработчик события
themeToggle.addEventListener('click', toggleTheme);
}
function setTheme(theme) {
document.documentElement.setAttribute('data-theme', theme);
// Обновляем иконку в кнопке
if (themeToggle) {
const sunIcon = themeToggle.querySelector('.sun-icon');
const moonIcon = themeToggle.querySelector('.moon-icon');
if (theme === 'dark') {
sunIcon.style.display = 'block';
moonIcon.style.display = 'none';
themeToggle.setAttribute('aria-label', 'Переключить на светлую тему');
} else {
sunIcon.style.display = 'none';
moonIcon.style.display = 'block';
themeToggle.setAttribute('aria-label', 'Переключить на темную тему');
}
}
// Анимация переключения
document.body.style.transition = 'background-color 0.3s ease, color 0.3s ease';
setTimeout(() => {
document.body.style.transition = '';
}, 300);
}
function toggleTheme() {
const currentTheme = document.documentElement.getAttribute('data-theme');
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
setTheme(newTheme);
localStorage.setItem(THEME_KEY, newTheme);
// Анимация кнопки
themeToggle.style.transform = 'scale(0.9)';
setTimeout(() => {
themeToggle.style.transform = 'scale(1)';
}, 150);
}
// ===== НАВИГАЦИЯ И ПЛАВНАЯ ПРОКРУТКА =====
function initializeNavigation() {
// Плавная прокрутка для всех якорных ссылок
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function(e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
const headerOffset = 80; // Отступ для фиксированной навигации
const elementPosition = target.getBoundingClientRect().top;
const offsetPosition = elementPosition + window.pageYOffset - headerOffset;
window.scrollTo({
top: offsetPosition,
behavior: 'smooth'
});
// Закрываем мобильное меню если открыто
closeMobileMenu();
}
});
});
// Подсветка активного пункта навигации
window.addEventListener('scroll', throttle(updateActiveNavLink, 100));
}
function updateActiveNavLink() {
const sections = document.querySelectorAll('section[id], div[id]');
const navLinks = document.querySelectorAll('.nav-link');
let currentSection = '';
sections.forEach(section => {
const sectionTop = section.getBoundingClientRect().top;
const sectionHeight = section.offsetHeight;
if (sectionTop <= 100 && sectionTop + sectionHeight > 100) {
currentSection = section.getAttribute('id');
}
});
navLinks.forEach(link => {
link.classList.remove('active');
if (link.getAttribute('href') === `#${currentSection}`) {
link.classList.add('active');
}
});
}
// ===== ЭФФЕКТЫ ПРОКРУТКИ И АНИМАЦИИ =====
function initializeScrollEffects() {
// Intersection Observer для анимаций появления
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('animate-in');
// Добавляем задержку для последовательной анимации
const delay = Array.from(entry.target.parentNode.children).indexOf(entry.target) * ANIMATION_DELAY;
entry.target.style.animationDelay = `${delay}ms`;
}
});
}, observerOptions);
// Наблюдаем за элементами для анимации
document.querySelectorAll('.card, .code-block, .btn, h1, h2, h3, p, ul, ol').forEach(el => {
el.classList.add('fade-in-element');
observer.observe(el);
});
// Параллакс эффект для hero секции
const heroSection = document.querySelector('.hero');
if (heroSection) {
window.addEventListener('scroll', throttle(() => {
const scrolled = window.pageYOffset;
const rate = scrolled * -0.5;
heroSection.style.transform = `translateY(${rate}px)`;
}, 16));
}
}
// ===== БЛОКИ КОДА =====
function initializeCodeBlocks() {
document.querySelectorAll('.code-block, pre').forEach(codeBlock => {
// Добавляем кнопку копирования
const copyButton = document.createElement('button');
copyButton.className = 'copy-btn';
copyButton.innerHTML = `
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect>
<path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path>
</svg>
<span>Копировать</span>
`;
copyButton.setAttribute('aria-label', 'Копировать код');
// Позиционируем кнопку
codeBlock.style.position = 'relative';
codeBlock.appendChild(copyButton);
// Обработчик копирования
copyButton.addEventListener('click', async () => {
const code = codeBlock.querySelector('code');
const text = code ? code.textContent : codeBlock.textContent;
try {
await navigator.clipboard.writeText(text);
showCopySuccess(copyButton);
} catch (err) {
// Fallback для старых браузеров
fallbackCopyTextToClipboard(text, copyButton);
}
});
// Подсветка синтаксиса (базовая)
highlightSyntax(codeBlock);
});
}
function showCopySuccess(button) {
const originalContent = button.innerHTML;
button.innerHTML = `
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="20,6 9,17 4,12"></polyline>
</svg>
<span>Скопировано!</span>
`;
button.classList.add('copied');
setTimeout(() => {
button.innerHTML = originalContent;
button.classList.remove('copied');
}, 2000);
}
function fallbackCopyTextToClipboard(text, button) {
const textArea = document.createElement('textarea');
textArea.value = text;
textArea.style.position = 'fixed';
textArea.style.left = '-999999px';
textArea.style.top = '-999999px';
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
document.execCommand('copy');
showCopySuccess(button);
} catch (err) {
console.error('Не удалось скопировать текст: ', err);
}
document.body.removeChild(textArea);
}
function highlightSyntax(codeBlock) {
const code = codeBlock.querySelector('code');
if (!code) return;
let text = code.innerHTML;
// Базовая подсветка для Git команд
text = text.replace(/\b(git|worktree|add|commit|push|pull|merge|branch|checkout|status|log|clone|fetch|rebase|stash|tag|remote|config|init|diff|show|reset|clean|mv|rm)\b/g, '<span class="keyword">$1</span>');
text = text.replace(/--[a-zA-Z-]+/g, '<span class="option">$&</span>');
text = text.replace(/"[^"]*"/g, '<span class="string">$&</span>');
text = text.replace(/'[^']*'/g, '<span class="string">$&</span>');
text = text.replace(/#[^\n]*/g, '<span class="comment">$&</span>');
text = text.replace(/\b\d+\b/g, '<span class="number">$&</span>');
code.innerHTML = text;
}
// ===== МОБИЛЬНОЕ МЕНЮ =====
function initializeMobileMenu() {
// Создаем кнопку мобильного меню если её нет
createMobileMenuToggle();
// Находим мобильное меню
mobileMenu = document.querySelector('.mobile-menu') || createMobileMenu();
// Обработчики событий
if (mobileMenuToggle) {
mobileMenuToggle.addEventListener('click', toggleMobileMenu);
}
// Закрытие меню при клике вне его
document.addEventListener('click', (e) => {
if (mobileMenu && mobileMenu.classList.contains('active') &&
!mobileMenu.contains(e.target) &&
!mobileMenuToggle.contains(e.target)) {
closeMobileMenu();
}
});
// Закрытие меню при изменении размера экрана
window.addEventListener('resize', debounce(() => {
if (window.innerWidth > 768) {
closeMobileMenu();
}
}, 250));
}
function createMobileMenuToggle() {
mobileMenuToggle = document.querySelector('.mobile-menu-toggle');
if (!mobileMenuToggle) {
const nav = document.querySelector('.nav, nav, header');
if (nav) {
mobileMenuToggle = document.createElement('button');
mobileMenuToggle.className = 'mobile-menu-toggle';
mobileMenuToggle.innerHTML = `
<span></span>
<span></span>
<span></span>
`;
mobileMenuToggle.setAttribute('aria-label', 'Открыть меню');
nav.appendChild(mobileMenuToggle);
}
}
}
function createMobileMenu() {
const menu = document.createElement('div');
menu.className = 'mobile-menu';
// Копируем ссылки из основной навигации
const navLinks = document.querySelectorAll('.nav-link, nav a, header a');
const menuList = document.createElement('ul');
navLinks.forEach(link => {
if (link.getAttribute('href') && link.getAttribute('href').startsWith('#')) {
const listItem = document.createElement('li');
const menuLink = link.cloneNode(true);
menuLink.addEventListener('click', closeMobileMenu);
listItem.appendChild(menuLink);
menuList.appendChild(listItem);
}
});
menu.appendChild(menuList);
document.body.appendChild(menu);
return menu;
}
function toggleMobileMenu() {
if (mobileMenu.classList.contains('active')) {
closeMobileMenu();
} else {
openMobileMenu();
}
}
function openMobileMenu() {
mobileMenu.classList.add('active');
mobileMenuToggle.classList.add('active');
mobileMenuToggle.setAttribute('aria-label', 'Закрыть меню');
document.body.style.overflow = 'hidden';
}
function closeMobileMenu() {
mobileMenu.classList.remove('active');
mobileMenuToggle.classList.remove('active');
mobileMenuToggle.setAttribute('aria-label', 'Открыть меню');
document.body.style.overflow = '';
}
// ===== УТИЛИТЫ =====
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
function throttle(func, limit) {
let inThrottle;
return function() {
const args = arguments;
const context = this;
if (!inThrottle) {
func.apply(context, args);
inThrottle = true;
setTimeout(() => inThrottle = false, limit);
}
}
}