2020-03-27 14:27:57 +00:00
|
|
|
import './lightbox.css'
|
|
|
|
|
|
|
|
let images = []
|
2020-04-19 15:31:27 +00:00
|
|
|
/** @type {HTMLImageElement} */
|
2020-03-27 14:27:57 +00:00
|
|
|
let currentImage = null
|
|
|
|
let currentIndexIndex = 0
|
|
|
|
|
2020-04-20 16:08:56 +00:00
|
|
|
let hideContainer
|
|
|
|
|
2020-03-27 14:27:57 +00:00
|
|
|
function findOrCreateLightboxContainer () {
|
|
|
|
const lightboxContainerSelector = '.lightbox-container'
|
|
|
|
|
|
|
|
let lightBoxContainer = document.querySelector(lightboxContainerSelector)
|
|
|
|
if (!lightBoxContainer) {
|
|
|
|
lightBoxContainer = document.createElement('div')
|
|
|
|
lightBoxContainer.className = 'lightbox-container'
|
|
|
|
|
|
|
|
lightBoxContainer.innerHTML = `
|
|
|
|
<i class="fa fa-chevron-left lightbox-control-previous" aria-hidden="true"></i>
|
|
|
|
<i class="fa fa-chevron-right lightbox-control-next" aria-hidden="true"></i>
|
|
|
|
<i class="fa fa-close lightbox-control-close" aria-hidden="true"></i>
|
|
|
|
|
|
|
|
<div class="lightbox-inner">
|
|
|
|
</div>
|
|
|
|
`
|
|
|
|
|
2020-04-19 15:31:27 +00:00
|
|
|
addImageZoomListener(lightBoxContainer)
|
|
|
|
|
2020-04-20 16:08:56 +00:00
|
|
|
hideContainer = () => {
|
2020-03-27 14:27:57 +00:00
|
|
|
lightBoxContainer.classList.remove('show')
|
2020-04-19 12:59:49 +00:00
|
|
|
document.body.classList.remove('no-scroll')
|
2020-04-19 15:31:27 +00:00
|
|
|
currentImage = null
|
2020-03-27 14:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lightBoxContainer.querySelector('.lightbox-control-previous').addEventListener('click', (e) => {
|
|
|
|
e.stopPropagation()
|
|
|
|
switchImage(-1)
|
|
|
|
})
|
|
|
|
lightBoxContainer.querySelector('.lightbox-control-next').addEventListener('click', (e) => {
|
|
|
|
e.stopPropagation()
|
|
|
|
switchImage(1)
|
|
|
|
})
|
2020-04-20 16:08:56 +00:00
|
|
|
lightBoxContainer.querySelector('.lightbox-control-close').addEventListener('click', (e) => {
|
|
|
|
e.stopPropagation()
|
|
|
|
hideContainer()
|
|
|
|
})
|
|
|
|
lightBoxContainer.addEventListener('click', (e) => {
|
|
|
|
e.stopPropagation()
|
|
|
|
hideContainer()
|
|
|
|
})
|
2020-03-27 14:27:57 +00:00
|
|
|
|
|
|
|
document.body.appendChild(lightBoxContainer)
|
|
|
|
}
|
|
|
|
|
|
|
|
return lightBoxContainer
|
|
|
|
}
|
|
|
|
|
|
|
|
function switchImage (dir) {
|
|
|
|
const lightBoxContainer = findOrCreateLightboxContainer()
|
|
|
|
|
|
|
|
currentIndexIndex += dir
|
|
|
|
if (currentIndexIndex >= images.length) {
|
|
|
|
currentIndexIndex = 0
|
|
|
|
} else if (currentIndexIndex < 0) {
|
|
|
|
currentIndexIndex = images.length - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
const img = images[currentIndexIndex]
|
|
|
|
|
|
|
|
setImageInner(img, lightBoxContainer)
|
|
|
|
}
|
|
|
|
|
|
|
|
function setImageInner (img, lightBoxContainer) {
|
|
|
|
const src = img.getAttribute('src')
|
|
|
|
const alt = img.getAttribute('alt')
|
|
|
|
|
2020-04-19 15:31:27 +00:00
|
|
|
lightBoxContainer.querySelector('.lightbox-inner').innerHTML = `<img src="${src}" alt="${alt}" draggable="false">`
|
|
|
|
addImageDragListener(lightBoxContainer.querySelector('.lightbox-inner img'))
|
2020-03-27 14:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function onClickImage (img) {
|
|
|
|
const lightBoxContainer = findOrCreateLightboxContainer()
|
|
|
|
|
|
|
|
setImageInner(img, lightBoxContainer)
|
|
|
|
|
|
|
|
lightBoxContainer.classList.add('show')
|
2020-04-19 12:59:49 +00:00
|
|
|
document.body.classList.add('no-scroll')
|
2020-03-27 14:27:57 +00:00
|
|
|
|
|
|
|
currentImage = img
|
|
|
|
updateLightboxImages()
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateLightboxImages () {
|
2020-04-19 12:59:35 +00:00
|
|
|
images = [...document.querySelectorAll('.markdown-body img.md-image')]
|
2020-03-27 14:27:57 +00:00
|
|
|
|
|
|
|
if (currentImage) {
|
|
|
|
currentIndexIndex = images.findIndex(image => image === currentImage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 15:31:27 +00:00
|
|
|
function addImageZoomListener (container) {
|
|
|
|
container.addEventListener('wheel', function (e) {
|
|
|
|
// normalize scroll position as percentage
|
|
|
|
e.preventDefault()
|
|
|
|
|
|
|
|
/** @type {HTMLImageElement} */
|
|
|
|
const image = container.querySelector('img')
|
|
|
|
|
|
|
|
if (!image) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let scale = image.getBoundingClientRect().width / image.offsetWidth
|
|
|
|
scale += e.deltaY * -0.01
|
|
|
|
|
|
|
|
// Restrict scale
|
|
|
|
scale = Math.min(Math.max(0.125, scale), 4)
|
|
|
|
|
|
|
|
var transformValue = `scale(${scale})`
|
|
|
|
|
|
|
|
image.style.WebkitTransform = transformValue
|
|
|
|
image.style.MozTransform = transformValue
|
|
|
|
image.style.OTransform = transformValue
|
|
|
|
image.style.transform = transformValue
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {HTMLImageElement} image
|
|
|
|
*/
|
|
|
|
function addImageDragListener (image) {
|
|
|
|
let moved = false
|
|
|
|
let pos = []
|
|
|
|
image.addEventListener('mousedown', (evt) => {
|
|
|
|
moved = true
|
|
|
|
|
|
|
|
const { left, top } = image.getBoundingClientRect()
|
|
|
|
|
|
|
|
pos = [
|
|
|
|
evt.pageX - left,
|
|
|
|
evt.pageY - top
|
|
|
|
]
|
|
|
|
}, true)
|
|
|
|
|
|
|
|
image.addEventListener('mousemove', (evt) => {
|
|
|
|
if (!moved) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
image.style.left = `${evt.pageX - pos[0]}px`
|
|
|
|
image.style.top = `${evt.pageY - pos[1]}px`
|
|
|
|
image.style.position = 'absolute'
|
|
|
|
}, true)
|
|
|
|
|
|
|
|
image.addEventListener('mouseup', () => {
|
|
|
|
moved = false
|
|
|
|
pos = []
|
|
|
|
}, true)
|
|
|
|
|
|
|
|
image.addEventListener('mouseout', () => {
|
|
|
|
moved = false
|
|
|
|
})
|
2020-04-19 15:38:31 +00:00
|
|
|
|
|
|
|
image.addEventListener('click', (e) => {
|
|
|
|
e.stopPropagation()
|
|
|
|
})
|
2020-04-19 15:31:27 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 14:27:57 +00:00
|
|
|
const init = () => {
|
|
|
|
const markdownBody = document.querySelector('.markdown-body')
|
|
|
|
if (!markdownBody) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
markdownBody.addEventListener('click', function (e) {
|
2020-04-19 15:31:27 +00:00
|
|
|
const img = e.target
|
|
|
|
if (img.nodeName === 'IMG' && img.classList.contains('md-image')) {
|
|
|
|
onClickImage(img)
|
2020-03-27 14:27:57 +00:00
|
|
|
e.stopPropagation()
|
|
|
|
}
|
|
|
|
})
|
2020-04-20 16:08:56 +00:00
|
|
|
|
|
|
|
window.addEventListener('keydown', function (e) {
|
|
|
|
if (!currentImage) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.key === 'ArrowRight') {
|
|
|
|
switchImage(1)
|
|
|
|
e.stopPropagation()
|
|
|
|
} else if (e.key === 'ArrowLeft') {
|
|
|
|
switchImage(-1)
|
|
|
|
e.stopPropagation()
|
|
|
|
} else if (e.key === 'Escape') {
|
|
|
|
if (hideContainer) {
|
|
|
|
hideContainer()
|
|
|
|
e.stopPropagation()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2020-03-27 14:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
init()
|