Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] Allow long captions #2

Open
wants to merge 35 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
d8d0410
Rename framework.createEl to framework.createElement for clarity.
PeterSweetAndSour Apr 16, 2020
3d968ea
Rename function argument of a DOM node from 'captionEl' to 'captionEl…
PeterSweetAndSour Apr 16, 2020
dae5c97
Capture the calculated size of the image and set an option for the ca…
PeterSweetAndSour Apr 16, 2020
09d9b20
Move new option to the UI file.
PeterSweetAndSour Apr 16, 2020
a2e8e60
First pass with something that works. Need to fix z-index on captionE…
PeterSweetAndSour Apr 23, 2020
52becb6
Rename button to be consistent pattern with others. Fix z-index issue…
PeterSweetAndSour Apr 23, 2020
2a4a41f
Fix display/hiding of expand control when switching photos.
PeterSweetAndSour Apr 23, 2020
0f1e189
Set data attributes on .pswp__item to carry initial image height and …
PeterSweetAndSour Apr 24, 2020
c4511c4
Rename function to indicate that it is private.
PeterSweetAndSour Apr 25, 2020
9591a4c
Save so far now that positioning is correct. Need to fix hiding and …
PeterSweetAndSour Apr 29, 2020
2983364
Somewhat closer. Accounting for scale but only on first load. :-( Cap…
PeterSweetAndSour May 1, 2020
11eef5e
Seems to work.
PeterSweetAndSour May 3, 2020
a71e6db
Stop accidental scroll on close. Add a box at the end of the caption …
PeterSweetAndSour May 5, 2020
b5e4f45
Polyfill for closest() for IE11
PeterSweetAndSour May 7, 2020
219be4c
Various fixes
PeterSweetAndSour May 7, 2020
298e77e
Sorted out weirdness with tap behavior. Now preventing closing when t…
PeterSweetAndSour May 7, 2020
534b671
Renaming option name and removing redundant code.
PeterSweetAndSour May 8, 2020
43453c3
Drag up on collapsed caption will expand it.
PeterSweetAndSour May 8, 2020
493af2b
Swap out two lines of SVG for two dozen lines of CSS to make the expa…
PeterSweetAndSour May 9, 2020
aa2a947
Back to SVG background images but now Base64 encoded and background s…
PeterSweetAndSour May 10, 2020
f20655e
Ensure HTML exists for caption control and change title on button whe…
PeterSweetAndSour May 10, 2020
fd24fe1
Allow left/right buttons to show above expanded caption overlay when …
PeterSweetAndSour May 12, 2020
ab38d3e
Now with keyboard control.
PeterSweetAndSour May 13, 2020
599f1b0
Moving the setting of tabindex and focus on the innerCaptionElement t…
PeterSweetAndSour May 13, 2020
d16f1e9
Animate expansion and collapse of caption.
PeterSweetAndSour May 13, 2020
d8e13fd
Get around cross-site security issue by creating a new stylesheet rat…
PeterSweetAndSour May 13, 2020
d8fe302
A bit of rearrangement as I discovered that dragging to a new photo o…
PeterSweetAndSour May 14, 2020
99a15f9
Increase the width of the caption if long captions are selected.
PeterSweetAndSour May 14, 2020
0878043
Added section for caption expand/collapse by vertical swipe/drag.
PeterSweetAndSour May 17, 2020
661065d
Add "FYI" to console notices explaining that two options are overridd…
PeterSweetAndSour May 17, 2020
17bf594
Remove some console output and updated generated files.
PeterSweetAndSour May 17, 2020
507a2f5
Expand and collapse caption with just the mouse wheel
PeterSweetAndSour May 9, 2020
d9cd2ec
Add long caption to index
andi34 Sep 5, 2020
83885ed
seems to work
andi34 Sep 6, 2020
2e70369
Increase font size
PeterSweetAndSour Jun 18, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
</button>

<div class="pswp__caption">
<button class="pswp__button pswp__button--caption--ctrl" id="pswp__button--caption--ctrl" title="Expand caption"></button>
<div class="pswp__caption__center"></div>
</div>

Expand All @@ -100,7 +101,8 @@
{
src: 'https://farm7.staticflickr.com/6175/6176698785_7dee72237e_b.jpg',
w: 1024,
h: 683
h: 683,
title: 'First caption. Image height and width defined. Images are displayed at their highest possible size and are not limited by the width of the column or wrapper. Each image is isolated from the other content and fits the viewport vertically, so that the user can focus on it. If an image is larger than the viewport, it can be zoomed (which most galleries are unable to do)'
},
// Auto sized images
{
Expand All @@ -111,7 +113,7 @@
{
src: 'https://farm6.staticflickr.com/5591/15008867125_b61960af01_h.jpg',
autoSize: true,
title: 'Another example caption. Image height and width defined also via "autoSize: true"'
title: 'Another example caption. Image height and width defined also via "autoSize: true". Images are displayed at their highest possible size and are not limited by the width of the column or wrapper. Each image is isolated from the other content and fits the viewport vertically, so that the user can focus on it. If an image is larger than the viewport, it can be zoomed (which most galleries are unable to do). Images are displayed at their highest possible size and are not limited by the width of the column or wrapper. Each image is isolated from the other content and fits the viewport vertically, so that the user can focus on it. If an image is larger than the viewport, it can be zoomed (which most galleries are unable to do). Images are displayed at their highest possible size and are not limited by the width of the column or wrapper. Each image is isolated from the other content and fits the viewport vertically, so that the user can focus on it. If an image is larger than the viewport, it can be zoomed (which most galleries are unable to do). Images are displayed at their highest possible size and are not limited by the width of the column or wrapper. Each image is isolated from the other content and fits the viewport vertically, so that the user can focus on it. If an image is larger than the viewport, it can be zoomed (which most galleries are unable to do)'
}
];

Expand All @@ -132,6 +134,7 @@

closeEl: true,
captionEl: true,
allowLongCaptions: true,
fullscreenEl: true,
zoomEl: true,
shareEl: true,
Expand Down
78 changes: 75 additions & 3 deletions src/css/default-skin/default-skin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@
width: 70px;
height: 100px;
position: absolute;
z-index: 1;
}
/* Buttons should appear above expanded caption overlay if viewport wider than 540px as the
the maximum width of the caption is 480px plus 30px each side. */
@media only screen and (min-width: 540px) {
.pswp__button--arrow--left,
.pswp__button--arrow--right {
z-index: 3;
}
}

.pswp__button--arrow--left {
Expand Down Expand Up @@ -329,6 +338,7 @@ a.pswp__share--download {

.pswp__caption {
position: absolute;
z-index: 2;
left: 0;
bottom: 0;
width: 100%;
Expand All @@ -344,10 +354,71 @@ a.pswp__share--download {
text-align: center;
max-width: 420px;
margin: 0 auto;
font-size: 13px;
padding: 10px;
font-size: 15px;
padding: 18px 10px 10px 10px;
line-height: 20px;
color: #ccc;
color: #fff;
background-color: #000;
transition: height 250ms ease-out;

a {
color: #ccf;
}
a:hover {
color: #fff;
}

p {
margin: 0 0 1em 10px;
}

p:last-child::after {
display: inline-block;
height: 10px;
width: 10px;
background-color: white;
margin-left: 6px;
margin-bottom: -1px;
content: ' ';
}
}

.pswp__button--caption--ctrl {
display: none;
width: 36px;
height: 36px;
background-image: none;
border-radius: 18px;
border: none;
overflow: hidden;
position: absolute;
top: -18px;
left: 50%;
transform: translateX(-50%);
text-decoration: none;
opacity: 1;
}

.pswp__button--caption--ctrl.pswp__button--caption--ctrl--expand {
background-image: url('data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgZmlsbC1ydWxlPSJldmVub2RkIiBjbGlwLXJ1bGU9ImV2ZW5vZGQiPjxwYXRoIGQ9Ik0xMiAwYzYuNjIzIDAgMTIgNS4zNzcgMTIgMTJzLTUuMzc3IDEyLTEyIDEyLTEyLTUuMzc3LTEyLTEyIDUuMzc3LTEyIDEyLTEyem0wIDFjNi4wNzEgMCAxMSA0LjkyOSAxMSAxMXMtNC45MjkgMTEtMTEgMTEtMTEtNC45MjktMTEtMTEgNC45MjktMTEgMTEtMTF6bTUuMjQ3IDE1bC01LjI0Ny02LjQ0LTUuMjYzIDYuNDQtLjczNy0uNjc4IDYtNy4zMjIgNiA3LjMzNS0uNzUzLjY2NXoiLz48L3N2Zz4=');
}

.pswp__button--caption--ctrl.pswp__button--caption--ctrl--collapse {
background-image: url('data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgZmlsbC1ydWxlPSJldmVub2RkIiBjbGlwLXJ1bGU9ImV2ZW5vZGQiPjxwYXRoIGQ9Ik0xMiAwYzYuNjIzIDAgMTIgNS4zNzcgMTIgMTJzLTUuMzc3IDEyLTEyIDEyLTEyLTUuMzc3LTEyLTEyIDUuMzc3LTEyIDEyLTEyem0wIDFjNi4wNzEgMCAxMSA0LjkyOSAxMSAxMXMtNC45MjkgMTEtMTEgMTEtMTEtNC45MjktMTEtMTEgNC45MjktMTEgMTEtMTF6bTUuMjQ3IDhsLTUuMjQ3IDYuNDQtNS4yNjMtNi40NC0uNzM3LjY3OCA2IDcuMzIyIDYtNy4zMzUtLjc1My0uNjY1eiIvPjwvc3ZnPg==');
}

/* This must come after setting the background above */
.pswp__button--caption--ctrl.pswp__button--caption--ctrl--expand,
.pswp__button--caption--ctrl.pswp__button--caption--ctrl--collapse {
display: block;
width: 36px;
height: 36px;
background-color: #ccc;
background-size: 36px 36px;
}
.pswp__button--caption--ctrl.pswp__button--caption--ctrl--expand:hover,
.pswp__button--caption--ctrl.pswp__button--caption--ctrl--collapse:hover {
background-color: #fff;
}

.pswp__caption--empty {
Expand Down Expand Up @@ -494,6 +565,7 @@ a.pswp__share--download {
/* top black bar with buttons and "1 of X" indicator */
.pswp__top-bar {
position: absolute;
z-index: 1;
left: 0;
top: 0;
height: 44px;
Expand Down
30 changes: 27 additions & 3 deletions src/js/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ var _options = {
mouseUsed: false,
loop: true,
pinchToClose: true,
closeOnScroll: true,
closeOnVerticalDrag: true,
closeOnScroll: true, // Will be overridden if allowLongCaptions is true
closeOnVerticalDrag: true, // Will be overridden if allowLongCaptions is true
verticalDragRange: 0.75,
hideAnimationDuration: 333,
showAnimationDuration: 333,
Expand Down Expand Up @@ -319,7 +319,12 @@ var _isOpen,
s.height = h + 'px';
s.left = item.initialPosition.x + 'px';
s.top = item.initialPosition.y + 'px';

item.zoom = zoomRatio;
item.apparentImageHeight = h;
item.imageFromTop = item.initialPosition;
};

_applyCurrentZoomPan = function () {
if (_currZoomElementStyle) {
var s = _currZoomElementStyle,
Expand All @@ -345,6 +350,21 @@ var _isOpen,
keydownAction = 'prev';
} else if (e.keyCode === 39) {
keydownAction = 'next';
} else if (e.keyCode === 13 || e.keyCode === 32) {
/* Enter or spacebar */
var btnCaptionCtrl = document.getElementById('pswp__button--caption--ctrl');
if (btnCaptionCtrl) {
if (
btnCaptionCtrl.classList.contains('pswp__button--caption--ctrl--expand') ||
btnCaptionCtrl.classList.contains('pswp__button--caption--ctrl--collapse')
) {
// Add tabindex to the caption div so that it can take focus and be controlled by up/down arrows
var innerCaptionElement = btnCaptionCtrl.parentNode.querySelector('.pswp__caption__center');
innerCaptionElement.setAttribute('tabindex', '-1');
innerCaptionElement.focus();
keydownAction = 'toggleCaption';
}
}
}
}

Expand Down Expand Up @@ -672,8 +692,8 @@ var publicMethods = {
framework.unbind(window, 'scroll', self);

_stopDragUpdateLoop();

_stopAllAnimations();
self.ui.resetCaption();

_listeners = {};
},
Expand Down Expand Up @@ -796,6 +816,10 @@ var publicMethods = {
self.updateSize(true);
},

toggleCaption: function (el) {
self.ui.toggleCaption(el);
},

// update current zoom/pan objects
updateCurrZoomItem: function (emulateSetContent) {
if (emulateSetContent) {
Expand Down
22 changes: 22 additions & 0 deletions src/js/desktop-zoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,28 @@ _registerModule('DesktopZoom', {
},

handleMouseWheel: function (e) {
// If scrolling down on a collapsed long caption, expand the caption
var _target = e.target || e.srcElement;
var targetCaption = _target.closest('.pswp__caption');
if (targetCaption) {
var toggleCaptionBtn = targetCaption.querySelector('.pswp__button--caption--ctrl');
if (toggleCaptionBtn) {
if (toggleCaptionBtn.classList.contains('pswp__button--caption--ctrl--expand') && e.wheelDeltaY < -50) {
self.ui.toggleCaption(toggleCaptionBtn);
} else if (toggleCaptionBtn.classList.contains('pswp__button--caption--ctrl--collapse')) {
// Collapse the caption if scrolled to the top and user scrolls further
var innerCaptionElement = targetCaption.querySelector('.pswp__caption__center');
if (innerCaptionElement.scrollTop == 0 && e.wheelDeltaY > 50) {
self.ui.toggleCaption(toggleCaptionBtn);
}
} else {
e.preventDefault();
}

return;
}
}

if (_currZoomLevel <= self.currItem.fitRatio) {
if (_options.modal) {
if (!_options.closeOnScroll || _numAnimations || _isDragging) {
Expand Down
22 changes: 20 additions & 2 deletions src/js/framework-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var framework = {
isArray: function (obj) {
return obj instanceof Array;
},
createEl: function (classes, tag) {
createElement: function (classes, tag) {
var el = document.createElement(tag || 'div');
if (classes) {
el.className = classes;
Expand Down Expand Up @@ -133,7 +133,7 @@ var framework = {
if (framework.features) {
return framework.features;
}
var helperEl = framework.createEl(),
var helperEl = framework.createElement(),
helperStyle = helperEl.style,
vendor = '',
features = {};
Expand Down Expand Up @@ -277,3 +277,21 @@ if (framework.features.oldIE) {
}
};
}

// Polyfill for closest() for IE11 from https://developer.mozilla.org/en-US/docs/Web/API/Element/closest.
// Not bothering with versions older than 11 and it is debatable whether we should worry even about 11 now.
if (!Element.prototype.matches) {
Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector;
}

if (!Element.prototype.closest) {
Element.prototype.closest = function (s) {
var el = this;

do {
if (Element.prototype.matches.call(el, s)) return el;
el = el.parentElement || el.parentNode;
} while (el !== null && el.nodeType === 1);
return null;
};
}
28 changes: 28 additions & 0 deletions src/js/gestures.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var _gestureStartTime,
_opacityChanged,
_bgOpacity,
_wasOverInitialZoom,
_target,
_isEqualPoints = function (p1, p2) {
return p1.x === p2.x && p1.y === p2.y;
},
Expand Down Expand Up @@ -288,6 +289,8 @@ var _gestureStartTime,

_shout('pointerDown');

_target = e.target || e.srcElement;

if (_pointerEventEnabled) {
var pointerIndex = framework.arraySearch(_currPointers, e.pointerId, 'id');
if (pointerIndex < 0) {
Expand All @@ -307,6 +310,7 @@ var _gestureStartTime,
if (!_isDragging || numPoints === 1) {
_isDragging = _isFirstMove = true;
framework.bind(window, _upMoveEvents, self);
// framework.bind(window, _downEvents, self); // I thought this might be needed to collapse caption but it made no difference.

_isZoomingIn = _wasOverInitialZoom = _opacityChanged = _verticalDragInitiated = _mainScrollShifted = _moved = _isMultitouch = _zoomStarted = false;

Expand Down Expand Up @@ -521,6 +525,29 @@ var _gestureStartTime,
return;
}

/* **************************************************************
Commenting this section out because it does not work reliably
especially when swiping down in an attempt to close the caption.
I'd be grateful if anyone can figure out why and fix it.
*****************************************************************
// If dragging up on a collapsed long caption, expand the caption;
// If dragging down on expanded long caption when at the top, collapse the caption.
if(_direction === 'v' && _options.allowLongCaptions) {
var targetCaption = _target.closest(".pswp__caption");
if(targetCaption) {
var toggleCaptionBtn = targetCaption.querySelector(".pswp__button--caption--ctrl");
var isExpanded = toggleCaptionBtn.classList.contains("pswp__button--caption--ctrl--collapse");
var isCollapsed = toggleCaptionBtn.classList.contains("pswp__button--caption--ctrl--expand");
var innerCaptionElement = targetCaption.querySelector(".pswp__caption__center");
if((delta.y < -DIRECTION_CHECK_OFFSET && isCollapsed) ||
(delta.y > DIRECTION_CHECK_OFFSET && isExpanded && innerCaptionElement.scrollTop === 0)) {
self.ui.toggleCaption(toggleCaptionBtn);
return;
}
}
}
*/

if (_direction === 'v' && _options.closeOnVerticalDrag) {
if (!_canPan()) {
_currPanDist.y += delta.y;
Expand Down Expand Up @@ -640,6 +667,7 @@ var _gestureStartTime,
if (numPoints === 0) {
_isDragging = false;
framework.unbind(window, _upMoveEvents, self);
// framework.unbind(window, _downEvents, self); // I thought this might be needed to collapse caption but it made no difference.

_stopDragUpdateLoop();

Expand Down
Loading