wrap images in a button so they can be individually toggled

This commit is contained in:
Sarah Schneider 2021-04-16 14:55:16 -04:00
Родитель 6752ab3d9e
Коммит 6a708b9a49
2 изменённых файлов: 65 добавлений и 21 удалений

Просмотреть файл

@ -15,50 +15,86 @@ export default function () {
return return
} }
// Get the aria-labels from the span elements containing the hide/show tooltips for single images.
// (We do it this way instead of hardcoding text here for localization-friendliness.)
const tooltipHideSingle = document.getElementById('js-hide-single-image').getAttribute('aria-label')
const tooltipShowSingle = document.getElementById('js-show-single-image').getAttribute('aria-label')
for (const img of images) {
// First, wrap each image in a button and add some attributes.
const parentDiv = img.parentNode
const parentButton = document.createElement('button')
parentDiv.replaceChild(parentButton, img)
parentButton.appendChild(img)
parentButton.classList.add('tooltipped', 'tooltipped-nw', 'btn-toggle-image')
parentButton.setAttribute('aria-label', tooltipHideSingle)
// On any individual image button click...
parentButton.addEventListener('click', (e) => {
// Determine current state.
const hideOnNextClick = parentButton.getAttribute('aria-label') === tooltipShowSingle
// Hide or show!
if (hideOnNextClick) {
toggleImage(img, 'show', tooltipHideSingle)
} else {
toggleImage(img, 'hide', tooltipShowSingle)
}
})
}
// Get the span elements containing the hide and show icons. // Get the span elements containing the hide and show icons.
const hideIcon = document.getElementById('js-hide-icon') const hideIcon = document.getElementById('js-hide-icon')
const showIcon = document.getElementById('js-show-icon') const showIcon = document.getElementById('js-show-icon')
// Get the aria-labels from the span elements for the tooltips. // Get the aria-labels from the span elements for the tooltips.
const tooltipHide = hideIcon.getAttribute('aria-label') const tooltipHideAll = hideIcon.getAttribute('aria-label')
const tooltipShow = showIcon.getAttribute('aria-label') const tooltipShowAll = showIcon.getAttribute('aria-label')
// The icon should be "Hide" to start, so we suppress the "Show" icon here. // The icon should be "Hide" to start, so we suppress the "Show" icon here.
showIcon.style.display = 'none' showIcon.style.display = 'none'
toggleImagesBtn.setAttribute('aria-label', tooltipHide) toggleImagesBtn.setAttribute('aria-label', tooltipHideAll)
let hideImages = true let showOnNextClick = true
toggleImagesBtn.addEventListener('click', (e) => { toggleImagesBtn.addEventListener('click', (e) => {
if (hideImages) { if (showOnNextClick) {
// Button should say "Show" on first click // Button should say "Show" on first click
showIcon.style.display = 'inline' showIcon.style.display = 'inline'
hideIcon.style.display = 'none' hideIcon.style.display = 'none'
toggleImagesBtn.setAttribute('aria-label', tooltipShow) toggleImagesBtn.setAttribute('aria-label', tooltipShowAll)
toggleImages(images, 'hide') toggleImages(images, 'hide', tooltipShowSingle)
} else { } else {
// Button should say "Hide" on another click // Button should say "Hide" on another click
showIcon.style.display = 'none' showIcon.style.display = 'none'
hideIcon.style.display = 'inline' hideIcon.style.display = 'inline'
toggleImagesBtn.setAttribute('aria-label', tooltipHide) toggleImagesBtn.setAttribute('aria-label', tooltipHideAll)
toggleImages(images, 'show') toggleImages(images, 'show', tooltipHideSingle)
} }
// Toggle the action on every click. // Toggle the action on every click.
hideImages = !hideImages showOnNextClick = !showOnNextClick
// Track image toggle events // Track image toggle events
// sendEvent({ type: 'imageToggle' }) // sendEvent({ type: 'imageToggle' })
}) })
} }
function toggleImages (images, action) { function toggleImages (images, action, tooltipText) {
for (const img of images) { for (const img of images) {
if (action === 'show') { toggleImage(img, action, tooltipText)
img.src = img.getAttribute('originalSrc') }
} else { }
if (!img.getAttribute('originalSrc')) img.setAttribute('originalSrc', img.src)
img.src = '/assets/images/octicons/image.svg' function toggleImage (img, action, tooltipText) {
} const parentButton = img.parentNode
if (action === 'show') {
img.src = img.getAttribute('originalSrc')
parentButton.setAttribute('aria-label', tooltipText)
} else {
if (!img.getAttribute('originalSrc')) img.setAttribute('originalSrc', img.src)
img.src = '/assets/images/octicons/image.svg'
parentButton.setAttribute('aria-label', tooltipText)
} }
} }

Просмотреть файл

@ -155,10 +155,18 @@ pre .bluebox {
} }
} }
.markdown-body li img {
max-width: calc(100% - 32px);
}
.markdown-body img { .markdown-body img {
max-height: 600px; max-height: 600px;
padding: 0;
}
.markdown-body button.btn-toggle-image {
border: none;
margin-top: 2px;
max-width: calc(100% - 32px);
background-color: transparent;
}
.markdown-body button.btn-toggle-image img {
padding: 0;
} }