From ddbc2c23e9c9cf9e400a386a20d60adc88c22658 Mon Sep 17 00:00:00 2001 From: Dipesh Rawat Date: Thu, 2 May 2024 23:48:32 +0100 Subject: [PATCH] Add full-screen image display feature --- assets/scss/_custom.scss | 37 ++++++++++++++++++ layouts/partials/head.html | 5 +++ layouts/shortcodes/figure.html | 30 +++++++++++++++ static/js/zoom.js | 69 ++++++++++++++++++++++++++++++++++ 4 files changed, 141 insertions(+) create mode 100644 layouts/shortcodes/figure.html create mode 100644 static/js/zoom.js diff --git a/assets/scss/_custom.scss b/assets/scss/_custom.scss index f58fa37831b05..b72e1b08f9581 100644 --- a/assets/scss/_custom.scss +++ b/assets/scss/_custom.scss @@ -1352,3 +1352,40 @@ div.alert > em.javascript-required { outline: none; padding: .5em 0 .5em 0; } + +/* CSS for 'figure' full-screen display */ + +/* Define styles for full-screen overlay */ +.figure-fullscreen-overlay { + position: fixed; + inset: 0; + z-index: 9999; + background-color: rgba(255, 255, 255, 0.95); /* White background with some transparency */ + display: flex; + justify-content: center; + align-items: center; + padding: calc(5% + 20px); + box-sizing: border-box; +} + +/* CSS class to scale the image when zoomed */ +.figure-zoomed { + transform: scale(1.2); +} + +/* Define styles for full-screen image */ +.figure-fullscreen-img { + max-width: 100%; + max-height: 100%; + object-fit: contain; /* Maintain aspect ratio and fit within the container */ +} + +/* Define styles for close button */ +.figure-close-button { + position: absolute; + top: 1%; + right: 2%; + cursor: pointer; + font-size: calc(5vw + 10px); + color: #333; +} \ No newline at end of file diff --git a/layouts/partials/head.html b/layouts/partials/head.html index 236ffac759f11..5bea1abf07d3a 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -99,6 +99,11 @@ {{- end -}} + +{{- if .HasShortcode "figure" -}} + +{{- end -}} + {{- if eq (lower .Params.cid) "community" -}} {{- if eq .Params.community_styles_migrated true -}} diff --git a/layouts/shortcodes/figure.html b/layouts/shortcodes/figure.html new file mode 100644 index 0000000000000..722eef6217307 --- /dev/null +++ b/layouts/shortcodes/figure.html @@ -0,0 +1,30 @@ +{{ $src := (.Page.Resources.GetMatch (printf "**%s*" (.Get "src"))) }} +{{ $clickToZoom := .Get "clicktozoom" }} + + {{- if .Get "link" -}} + + {{- end }} + {{ with .Get + {{- if .Get "link" }}{{ end -}} + {{- if or (or (.Get "title") (.Get "caption")) (.Get "attr") -}} +
+ {{ with (.Get "title") -}} +

{{ . }}

+ {{- end -}} + {{- if or (.Get "caption") (.Get "attr") -}}

+ {{- .Get "caption" | markdownify -}} + {{- with .Get "attrlink" }} + + {{- end -}} + {{- .Get "attr" | markdownify -}} + {{- if .Get "attrlink" }}{{ end }}

+ {{- end }} +
+ {{- end }} + \ No newline at end of file diff --git a/static/js/zoom.js b/static/js/zoom.js new file mode 100644 index 0000000000000..9db0e26513bb0 --- /dev/null +++ b/static/js/zoom.js @@ -0,0 +1,69 @@ +// The page and script is loaded successfully +$(document).ready(function() { + // Function to handle hover over
elements + function handleFigureHover() { + // Only change cursor to zoom-in if figure has 'clickable-zoom' class + if ($(this).hasClass('clickable-zoom') && !$(this).hasClass('figure-fullscreen-content')) { + $(this).css('cursor', 'zoom-in'); + } + } + + // Attach hover event to
elements with 'clickable-zoom' class + $('figure.clickable-zoom').hover(handleFigureHover, function() { + // Mouse out - revert cursor back to default + $(this).css('cursor', 'default'); + }); + + // Attach click event to
elements with 'clickable-zoom' class + $('figure.clickable-zoom').click(function() { + var $figure = $(this); + + // Check if the figure has 'clickable-zoom' class + if ($figure.hasClass('clickable-zoom')) { + var $img = $figure.find('img'); // Get the element within the clicked
+ + // Toggle 'figure-zoomed' class to scale the image + $img.toggleClass('figure-zoomed'); + + // Create a full-screen overlay + var $fullscreenOverlay = $('
'); + + // Clone the element to display in full-screen + var $fullscreenImg = $img.clone(); + $fullscreenImg.addClass('figure-fullscreen-img'); + + // Append the full-screen image to the overlay + $fullscreenOverlay.append($fullscreenImg); + + // Create a close button for the full-screen overlay + var $closeButton = $('×'); + $closeButton.click(function() { + // Remove the full-screen overlay when close button is clicked + $fullscreenOverlay.remove(); + $('body').css('overflow', 'auto'); // Restore scrolling to the underlying page + + // Remove 'figure-zoomed' class to reset image scale + $img.removeClass('figure-zoomed'); + }); + $fullscreenOverlay.append($closeButton); + + // Append the overlay to the body + $('body').append($fullscreenOverlay); + + // Disable scrolling on the underlying page + $('body').css('overflow', 'hidden'); + + // Close full-screen figure when clicking outside of it (on the overlay) + $fullscreenOverlay.click(function(event) { + if (event.target === this) { + // Clicked on the overlay area (outside the full-screen image) + $fullscreenOverlay.remove(); + $('body').css('overflow', 'auto'); // Restore scrolling to the underlying page + + // Remove 'figure-zoomed' class to reset image scale + $img.removeClass('figure-zoomed'); + } + }); + } + }); +});