11import './lightbox.css'
22
33let images = [ ]
4+ /** @type {HTMLImageElement } */
45let currentImage = null
56let currentIndexIndex = 0
67
@@ -21,10 +22,13 @@ function findOrCreateLightboxContainer () {
2122 </div>
2223 `
2324
25+ addImageZoomListener ( lightBoxContainer )
26+
2427 const hideContainer = ( e ) => {
2528 e . stopPropagation ( )
2629 lightBoxContainer . classList . remove ( 'show' )
2730 document . body . classList . remove ( 'no-scroll' )
31+ currentImage = null
2832 }
2933
3034 lightBoxContainer . querySelector ( '.lightbox-control-previous' ) . addEventListener ( 'click' , ( e ) => {
@@ -36,7 +40,7 @@ function findOrCreateLightboxContainer () {
3640 switchImage ( 1 )
3741 } )
3842 lightBoxContainer . querySelector ( '.lightbox-control-close' ) . addEventListener ( 'click' , hideContainer )
39- lightBoxContainer . addEventListener ( 'click' , hideContainer )
43+ // lightBoxContainer.addEventListener('click', hideContainer)
4044
4145 document . body . appendChild ( lightBoxContainer )
4246 }
@@ -63,7 +67,8 @@ function setImageInner (img, lightBoxContainer) {
6367 const src = img . getAttribute ( 'src' )
6468 const alt = img . getAttribute ( 'alt' )
6569
66- lightBoxContainer . querySelector ( '.lightbox-inner' ) . innerHTML = `<img src="${ src } " alt="${ alt } ">`
70+ lightBoxContainer . querySelector ( '.lightbox-inner' ) . innerHTML = `<img src="${ src } " alt="${ alt } " draggable="false">`
71+ addImageDragListener ( lightBoxContainer . querySelector ( '.lightbox-inner img' ) )
6772}
6873
6974function onClickImage ( img ) {
@@ -86,15 +91,80 @@ function updateLightboxImages () {
8691 }
8792}
8893
94+ function addImageZoomListener ( container ) {
95+ container . addEventListener ( 'wheel' , function ( e ) {
96+ // normalize scroll position as percentage
97+ e . preventDefault ( )
98+
99+ /** @type {HTMLImageElement } */
100+ const image = container . querySelector ( 'img' )
101+
102+ if ( ! image ) {
103+ return
104+ }
105+
106+ let scale = image . getBoundingClientRect ( ) . width / image . offsetWidth
107+ scale += e . deltaY * - 0.01
108+
109+ // Restrict scale
110+ scale = Math . min ( Math . max ( 0.125 , scale ) , 4 )
111+
112+ var transformValue = `scale(${ scale } )`
113+
114+ image . style . WebkitTransform = transformValue
115+ image . style . MozTransform = transformValue
116+ image . style . OTransform = transformValue
117+ image . style . transform = transformValue
118+ } )
119+ }
120+
121+ /**
122+ * @param {HTMLImageElement } image
123+ */
124+ function addImageDragListener ( image ) {
125+ let moved = false
126+ let pos = [ ]
127+ image . addEventListener ( 'mousedown' , ( evt ) => {
128+ moved = true
129+
130+ const { left, top } = image . getBoundingClientRect ( )
131+
132+ pos = [
133+ evt . pageX - left ,
134+ evt . pageY - top
135+ ]
136+ } , true )
137+
138+ image . addEventListener ( 'mousemove' , ( evt ) => {
139+ if ( ! moved ) {
140+ return
141+ }
142+
143+ image . style . left = `${ evt . pageX - pos [ 0 ] } px`
144+ image . style . top = `${ evt . pageY - pos [ 1 ] } px`
145+ image . style . position = 'absolute'
146+ } , true )
147+
148+ image . addEventListener ( 'mouseup' , ( ) => {
149+ moved = false
150+ pos = [ ]
151+ } , true )
152+
153+ image . addEventListener ( 'mouseout' , ( ) => {
154+ moved = false
155+ } )
156+ }
157+
89158const init = ( ) => {
90159 const markdownBody = document . querySelector ( '.markdown-body' )
91160 if ( ! markdownBody ) {
92161 return
93162 }
94163
95164 markdownBody . addEventListener ( 'click' , function ( e ) {
96- if ( e . target . nodeName === 'IMG' && e . target . classList . contains ( 'md-image' ) ) {
97- onClickImage ( e . target )
165+ const img = e . target
166+ if ( img . nodeName === 'IMG' && img . classList . contains ( 'md-image' ) ) {
167+ onClickImage ( img )
98168 e . stopPropagation ( )
99169 }
100170 } )
0 commit comments