Skip to content

Commit 48b5c75

Browse files
[imageGalleryNavigation] Add basic zoom/pan to Image detail page. (#634)
1 parent 906a850 commit 48b5c75

File tree

3 files changed

+102
-2
lines changed

3 files changed

+102
-2
lines changed

plugins/imageGalleryNavigation/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ https://discourse.stashapp.cc/t/imagegallerynavigation/1857
44

55
This plugin adds features for navigating between images within a Gallery from the Image details page. This is intended to make it easier to edit metadata on each Image in a Gallery one at a time without constantly having to go back and forth between the Gallery and Image page.
66

7-
This plugin currently adds two things to the Image details page:
7+
This plugin currently adds the following to the Image details page:
88
- A line above the tabs in the left panel that indicates the current page and total number of pages in the current Gallery. The current image number can be changed to jump to a specific image within the Gallery.
99
- Buttons along the left/right side of the main Image display panel that allow moving to the previous/next image in the Gallery.
10+
- Basic zoom (mouse wheel) and pan (click and drag) functionality (can be enabled/disabled in plugin settings). Zooming out to original scale will also reset the image position.
1011

1112
In the case of Images that are in multiple Galleries, the Gallery being navigated is set by accessing an Image from the Gallery you want to navigate. When navigating to an Image from a Gallery, the sorting/filtering currently on the Gallery view will also be applied to the Gallery navigation on the Image page.
1213

plugins/imageGalleryNavigation/imageGalleryNavigation.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
sortdir: "asc",
88
});
99

10+
let pluginSettings = {};
11+
const defaultPluginSettings = {
12+
enableTransform: true
13+
};
14+
1015
// In order to handle scenarios where an image is in multiple galleries, capture ID of gallery the user is navigating from.
1116
// If user navigates directly to an image URL and image is in multiple galleries, we will just use the first gallery in list.
1217
// 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,6 +31,12 @@
2631
// On image page, get data about gallery (image's position within gallery, next/prev image IDs),
2732
// add arrow buttons to page, and register arrow keypress handlers,
2833
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+
2940
var imageID = window.location.pathname.split("/")[2];
3041
var imageGalleries = await findImage(imageID);
3142

@@ -66,6 +77,11 @@
6677
insertGalleryToolbar(currentImageIndex, totalImageCount, galleryImages);
6778
insertArrowButtons(nextImageID, prevImageID);
6879
insertArrowKeyHandlers(nextImageID, prevImageID);
80+
81+
// Add translate/scale controls.
82+
if (pluginSettings.enableTransform) {
83+
setupMouseEventHandlers();
84+
}
6985
}
7086
}
7187

@@ -131,6 +147,84 @@
131147
// });
132148
}
133149

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+
134228
// *** Utility Functions ***
135229

136230
function redirectToImage(imageID) {

plugins/imageGalleryNavigation/imageGalleryNavigation.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
name: imageGalleryNavigation
22
# requires: CommunityScriptsUILibrary
33
description: This plugin adds features for navigating between images within a Gallery from the Image details page.
4-
version: 0.2
4+
version: 0.3
5+
settings:
6+
enableTransform:
7+
displayName: Enable Zoom/Pan
8+
description: Enable zoom (mouse wheel) and pan (click and drag) on Image detail page.
9+
type: BOOLEAN
510
ui:
611
requires:
712
- CommunityScriptsUILibrary

0 commit comments

Comments
 (0)