|
7 | 7 | sortdir: "asc", |
8 | 8 | }); |
9 | 9 |
|
| 10 | + let pluginSettings = {}; |
| 11 | + const defaultPluginSettings = { |
| 12 | + enableTransform: true |
| 13 | + }; |
| 14 | + |
10 | 15 | // In order to handle scenarios where an image is in multiple galleries, capture ID of gallery the user is navigating from. |
11 | 16 | // If user navigates directly to an image URL and image is in multiple galleries, we will just use the first gallery in list. |
12 | 17 | // This may break if user jumps around in browser history, in which case we will fall back to basic scenario of assuming first gallery in list. |
|
26 | 31 | // On image page, get data about gallery (image's position within gallery, next/prev image IDs), |
27 | 32 | // add arrow buttons to page, and register arrow keypress handlers, |
28 | 33 | async function setupImageContainer() { |
| 34 | + const configSettings = await csLib.getConfiguration("imageGalleryNavigation", {}); // getConfiguration is from cs-ui-lib.js |
| 35 | + pluginSettings = { |
| 36 | + ...defaultPluginSettings, |
| 37 | + ...configSettings, |
| 38 | + }; |
| 39 | + |
29 | 40 | var imageID = window.location.pathname.split("/")[2]; |
30 | 41 | var imageGalleries = await findImage(imageID); |
31 | 42 |
|
|
66 | 77 | insertGalleryToolbar(currentImageIndex, totalImageCount, galleryImages); |
67 | 78 | insertArrowButtons(nextImageID, prevImageID); |
68 | 79 | insertArrowKeyHandlers(nextImageID, prevImageID); |
| 80 | + |
| 81 | + // Add translate/scale controls. |
| 82 | + if (pluginSettings.enableTransform) { |
| 83 | + setupMouseEventHandlers(); |
| 84 | + } |
69 | 85 | } |
70 | 86 | } |
71 | 87 |
|
|
131 | 147 | // }); |
132 | 148 | } |
133 | 149 |
|
| 150 | + function setupMouseEventHandlers() { |
| 151 | + var img = document.querySelector("div.image-container img"); |
| 152 | + |
| 153 | + // Init transform values. |
| 154 | + imgScale = 1.0; |
| 155 | + imgTranslateX = 0.0; |
| 156 | + imgTranslateY = 0.0; |
| 157 | + |
| 158 | + // Prevent listeners from being attached twice. |
| 159 | + img.removeEventListener('mousedown', onStartDrag); |
| 160 | + img.removeEventListener('mousemove', onDrag); |
| 161 | + document.removeEventListener('mouseup', onStopDrag); |
| 162 | + img.removeEventListener('wheel', onMouseWheel); |
| 163 | + |
| 164 | + // Add event listeners. |
| 165 | + img.addEventListener('mousedown', onStartDrag); |
| 166 | + img.addEventListener('mousemove', onDrag); |
| 167 | + document.addEventListener('mouseup', onStopDrag); |
| 168 | + img.addEventListener('wheel', onMouseWheel); |
| 169 | + } |
| 170 | + |
| 171 | + // *** Mouse Event Handlers *** // |
| 172 | + var imgScale = 1.0; |
| 173 | + var imgTranslateX = 0.0; |
| 174 | + var imgTranslateY = 0.0; |
| 175 | + var dragStartX = 0.0; |
| 176 | + var dragStartY = 0.0; |
| 177 | + var dragStartTranslateX = 0.0; |
| 178 | + var dragStartTranslateY = 0.0; |
| 179 | + var dragActive = false; |
| 180 | + |
| 181 | + function onStartDrag(event) { |
| 182 | + if (event.button === 0) { |
| 183 | + event.preventDefault(); // Needed to prevent drag from being blocked. |
| 184 | + dragActive = true; |
| 185 | + dragStartX = event.clientX; |
| 186 | + dragStartY = event.clientY; |
| 187 | + dragStartTranslateX = imgTranslateX; |
| 188 | + dragStartTranslateY = imgTranslateY; |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + function onStopDrag(event) { |
| 193 | + if (event.button === 0) { |
| 194 | + dragActive = false; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + function onDrag(event) { |
| 199 | + if (dragActive) { |
| 200 | + imgTranslateX = dragStartTranslateX + (event.clientX - dragStartX); |
| 201 | + imgTranslateY = dragStartTranslateY + (event.clientY - dragStartY); |
| 202 | + applyImageTransform(); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + function onMouseWheel(event) { |
| 207 | + event.preventDefault(); |
| 208 | + |
| 209 | + // TODO: Zoom to cursor. |
| 210 | + // var img = document.querySelector("div.image-container img"); |
| 211 | + // img.style.transformOrigin = `${event.offsetX}px ${event.offsetY}px` |
| 212 | + imgScale += (event.deltaY * -0.001); |
| 213 | + imgScale = Math.max(imgScale, 1.0); |
| 214 | + |
| 215 | + if (imgScale == 1.0) { |
| 216 | + imgTranslateX = 0.0; |
| 217 | + imgTranslateY = 0.0; |
| 218 | + } |
| 219 | + |
| 220 | + applyImageTransform(); |
| 221 | + } |
| 222 | + |
| 223 | + function applyImageTransform() { |
| 224 | + var img = document.querySelector("div.image-container img"); |
| 225 | + img.style.transform = `translate(${imgTranslateX}px,${imgTranslateY}px) scale(${imgScale})` |
| 226 | + } |
| 227 | + |
134 | 228 | // *** Utility Functions *** |
135 | 229 |
|
136 | 230 | function redirectToImage(imageID) { |
|
0 commit comments