-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
158 lines (140 loc) · 4.88 KB
/
Copy pathscript.js
File metadata and controls
158 lines (140 loc) · 4.88 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
// Image zoom functionality
const images = document.querySelectorAll('.blog-content img');
const modal = document.getElementById('imageModal');
const modalImg = document.getElementById('modalImage');
const closeBtn = document.getElementsByClassName('close')[0];
let isModalZoomed = false;
let isDetailZoomed = false;
let currentScale = 1;
let initialX, initialY, currentX, currentY, translateX = 0, translateY = 0;
let isPanning = false;
let didPan = false; // To distinguish a click from a drag-release
let panStartClientX, panStartClientY; // To calculate if significant pan occurred
images.forEach(img => {
img.onclick = function() {
modal.style.display = "block";
modalImg.src = this.src;
resetZoomAndPan();
modalImg.style.cursor = 'zoom-in';
isModalZoomed = true;
}
});
function closeAndResetModal() {
modal.style.display = "none";
resetZoomAndPan();
}
function resetZoomAndPan() {
isModalZoomed = false;
isDetailZoomed = false;
isPanning = false;
didPan = false;
currentScale = 1;
translateX = 0;
translateY = 0;
modalImg.style.transform = 'scale(1) translate(0, 0)';
modalImg.style.cursor = 'zoom-in'; // Set initial cursor for modal image
}
closeBtn.onclick = function() {
closeAndResetModal();
}
modal.onclick = function(event) {
if (event.target === modal) {
closeAndResetModal();
}
}
modalImg.onclick = function() {
if (!isModalZoomed) return;
if (didPan) { // If a pan (drag) just occurred, don't toggle zoom.
didPan = false; // Reset for the next interaction
return;
}
if (!isDetailZoomed) { // Click to zoom IN
currentScale = 1.75;
translateX = 0;
translateY = 0;
modalImg.style.transform = `scale(${currentScale}) translate(${translateX}px, ${translateY}px)`;
modalImg.style.cursor = 'grab';
isDetailZoomed = true;
} else { // Click to zoom OUT
currentScale = 1;
translateX = 0;
translateY = 0;
modalImg.style.transform = `scale(${currentScale}) translate(${translateX}px, ${translateY}px)`;
modalImg.style.cursor = 'zoom-in';
isDetailZoomed = false;
}
}
modalImg.onmousedown = function(event) {
if (isDetailZoomed) {
event.preventDefault();
initialX = event.clientX - translateX;
initialY = event.clientY - translateY;
panStartClientX = event.clientX;
panStartClientY = event.clientY;
isPanning = true;
didPan = false; // Reset didPan at the start of a potential pan
modalImg.style.cursor = 'grabbing';
}
}
modalImg.onmousemove = function(event) {
if (isPanning && isDetailZoomed) {
event.preventDefault();
const deltaX = Math.abs(event.clientX - panStartClientX);
const deltaY = Math.abs(event.clientY - panStartClientY);
if (deltaX > 5 || deltaY > 5) { // Threshold to consider it a pan
didPan = true;
}
currentX = event.clientX - initialX;
currentY = event.clientY - initialY;
translateX = currentX;
translateY = currentY;
modalImg.style.transform = `scale(${currentScale}) translate(${translateX}px, ${translateY}px)`;
}
}
modalImg.onmouseup = function() {
if (isPanning) { // Only change cursor if a pan was in progress
if (isDetailZoomed) {
modalImg.style.cursor = 'grab';
}
isPanning = false;
}
// didPan is checked by the subsequent onclick event if it fires
}
modalImg.onmouseleave = function() {
if (isPanning) {
isPanning = false;
if (isDetailZoomed) {
modalImg.style.cursor = 'grab';
}
}
}
// Close on escape key
document.addEventListener('keydown', function(event) {
if (event.key === 'Escape' && modal.style.display === 'block') {
closeAndResetModal();
}
});
// Copy button functionality
document.addEventListener('DOMContentLoaded', () => {
const copyButtons = document.querySelectorAll('.copy-btn');
copyButtons.forEach(button => {
button.addEventListener('click', () => {
const codeBlockContainer = button.parentElement;
const preElement = codeBlockContainer.querySelector('pre');
if (preElement) {
const codeToCopy = preElement.innerText || preElement.textContent;
navigator.clipboard.writeText(codeToCopy).then(() => {
const originalButtonContent = button.innerHTML;
button.innerHTML = 'Copied!';
button.disabled = true;
setTimeout(() => {
button.innerHTML = originalButtonContent;
button.disabled = false;
}, 2000);
}).catch(err => {
console.error('Failed to copy text: ', err);
});
}
});
});
});