-
Notifications
You must be signed in to change notification settings - Fork 1
/
album.ts
183 lines (166 loc) · 6.35 KB
/
album.ts
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
import { MediumLightboxCore } from "../core";
import { Classes, YamzPlugin } from "../types";
import "./album.css";
export interface Albumed<Yamz extends MediumLightboxCore> {
moveToAlbumEntry: (entry: AlbumEntry<Yamz>, direction: "next" | "prev") => void;
}
export interface AlbumOptions<Yamz extends MediumLightboxCore> {
album?: AlbumEntry<Yamz>[];
wrapAlbum?: boolean;
}
export interface AlbumEntry<Yamz extends MediumLightboxCore> {
img: HTMLElement;
opts?: ReturnType<Yamz["optsFromElm"]> & AlbumOptions<Yamz>;
}
/** Augments the YAMZ instance to support albums */
export default function withAlbum<YamzType extends MediumLightboxCore>(_yamz: YamzType) {
const { defaultLightboxGenerator, optsFromElm, onKeyDown } = _yamz;
const yamz = _yamz as YamzPlugin<
YamzType,
Albumed<YamzType>,
AlbumOptions<YamzType>,
AlbumOptions<YamzType>
>;
yamz.options = {
wrapAlbum: false,
...yamz.options,
};
function augmentLightbox(
yamz: Albumed<YamzType>,
$lightbox: HTMLElement,
opts: AlbumOptions<YamzType>,
index: number
) {
if (!opts.album) {
return $lightbox;
}
const prevIndex = opts.wrapAlbum
? (opts.album.length + index - 1) % opts.album.length
: index - 1;
const nextIndex = opts.wrapAlbum
? (opts.album.length + index + 1) % opts.album.length
: index + 1;
if (prevIndex >= 0) {
const $prev = document.createElement("button");
$prev.classList.add(Classes.ALBUM_PREV);
$prev.addEventListener("click", e => {
if (!opts.album) {
return;
}
e.stopPropagation();
yamz.moveToAlbumEntry(opts.album[prevIndex], "prev");
});
$lightbox.appendChild($prev);
}
if (nextIndex < opts.album.length) {
const $next = document.createElement("button");
$next.classList.add(Classes.ALBUM_NEXT);
$next.addEventListener("click", e => {
if (!opts.album) {
return;
}
e.stopPropagation();
yamz.moveToAlbumEntry(opts.album[nextIndex], "next");
});
$lightbox.appendChild($next);
}
}
// insert album stuff into the lightbox if we're given one
yamz.defaultLightboxGenerator = function($copiedImg, opts, $original) {
const $lightbox = defaultLightboxGenerator($copiedImg, opts, $original);
if (opts.album) {
const index = opts.album.findIndex(entry => entry.img === $original);
augmentLightbox(this, $lightbox, opts, index);
}
return $lightbox;
};
// also allow specifying the album in HTML
yamz.optsFromElm = function($elm: HTMLElement) {
type outpType = ReturnType<YamzType["optsFromElm"]> & AlbumOptions<YamzType>;
const outp: outpType = optsFromElm($elm) as outpType;
if ($elm.dataset.album) {
const $siblings = Array.from(
document.querySelectorAll(`[data-album="${$elm.dataset.album}"]`)
) as HTMLElement[];
outp.album = $siblings.map($sibling => {
return {
img: $sibling,
opts: optsFromElm($sibling),
};
}) as AlbumEntry<YamzType>[];
// make sure each entry knows about which album it's in
outp.album.forEach(entry => {
entry.opts = {
...(entry.opts || {}),
album: outp.album,
} as outpType;
});
}
return outp;
};
// add new method for moving to an album entry
yamz.moveToAlbumEntry = function(entry: AlbumEntry<YamzType>, direction: "next" | "prev") {
if (!this.active) {
return;
}
const $target = this.active.$lightbox.querySelector(
`.${Classes.IMG_WRAPPER}`
) as HTMLElement;
if (!$target) {
throw new ReferenceError("Could not find image wrapper in lightbox");
}
const directions = {
out: direction === "next" ? "left" : "right",
in: direction === "next" ? "right" : "left",
};
let replaced = false;
const _onAnimEnd = () => {
if (replaced || !this.active) {
return;
}
replaced = true;
this.replace(entry.img, entry.opts);
const $newTarget = this.active.$lightbox.querySelector(
`.${Classes.IMG_WRAPPER}`
) as HTMLElement;
if (!$newTarget) {
return;
}
$newTarget.classList.add(`${Classes.IMG_WRAPPER}--in-${directions.in}`);
};
setTimeout(_onAnimEnd, 1000); // fail safe if for whatever reason animation doesn't play
$target.addEventListener("animationend", _onAnimEnd);
$target.addEventListener("animationcancel", _onAnimEnd);
$target.classList.add(`${Classes.IMG_WRAPPER}--out-${directions.out}`);
};
// finally extend the keyboard interactivity
yamz.onKeyDown = function(e: KeyboardEvent) {
onKeyDown.call(this, e);
if (!this.active) {
return;
}
const opts = this.active.options as AlbumOptions<YamzType>;
if (!opts.album) {
return;
}
// move back/forward in album when pressing arrow keys
if (e.key === "ArrowLeft" || e.key === "ArrowRight") {
const $curImg = this.active.$img;
const index = opts.album.findIndex(entry => entry.img === $curImg);
const prevIndex = opts.wrapAlbum
? (opts.album.length + index - 1) % opts.album.length
: index - 1;
const nextIndex = opts.wrapAlbum
? (opts.album.length + index + 1) % opts.album.length
: index + 1;
const targetIndex = e.key === "ArrowLeft" ? prevIndex : nextIndex;
if (targetIndex >= 0 && targetIndex < opts.album.length) {
(this as Albumed<YamzType>).moveToAlbumEntry(
opts.album[targetIndex],
e.key === "ArrowLeft" ? "prev" : "next"
);
}
}
};
return yamz;
}