From aefa7586fd792aa0ac971f762d3af19c81702b5d Mon Sep 17 00:00:00 2001 From: evazion Date: Fri, 9 Dec 2022 15:17:45 -0600 Subject: [PATCH] media assets: refactor media asset component. --- .../media_asset_component.html.erb | 4 +- .../media_asset_component.scss | 19 +++++---- .../src/javascripts/media_asset_component.js | 40 ++++++++----------- .../src/styles/common/utilities.scss | 1 + app/views/media_assets/show.html.erb | 2 +- 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/app/components/media_asset_component/media_asset_component.html.erb b/app/components/media_asset_component/media_asset_component.html.erb index d1f0c59e0..949efb4a2 100644 --- a/app/components/media_asset_component/media_asset_component.html.erb +++ b/app/components/media_asset_component/media_asset_component.html.erb @@ -1,7 +1,7 @@ -
+
<%= header %> -
+
diff --git a/app/components/media_asset_component/media_asset_component.scss b/app/components/media_asset_component/media_asset_component.scss index 4a72c7e65..8bf659836 100644 --- a/app/components/media_asset_component/media_asset_component.scss +++ b/app/components/media_asset_component/media_asset_component.scss @@ -1,4 +1,4 @@ -.media-asset-component:not(:hover) .media-asset-zoom-level { +.media-asset-container:not(:hover) .media-asset-zoom-level { opacity: 0; } @@ -7,8 +7,8 @@ background: var(--preview-icon-background); } -.media-asset-container { - &:not(.media-asset-container-fit-height) .paginator { +.media-asset-component { + &:not(.media-asset-component-fit-height) .paginator { position: sticky; } @@ -37,19 +37,24 @@ .media-asset-image { user-select: none; width: auto; - height: auto; - max-width: 100%; max-height: inherit; } - &.media-asset-container-fit-height { + &.media-asset-component-fit-height { max-height: calc(100vh - var(--header-visible-height)); justify-content: center; - .media-asset-component { + .media-asset-container { aspect-ratio: var(--media-asset-width) / var(--media-asset-height); height: max-content; height: intrinsic; // XXX Safari-only hack to make images correct height on upload page. I have no idea how this works. } } + + &.media-asset-component-fit-width { + .media-asset-image { + height: auto; + max-width: 100%; + } + } } diff --git a/app/javascript/src/javascripts/media_asset_component.js b/app/javascript/src/javascripts/media_asset_component.js index 97ff2a6f0..1fab9d4e9 100644 --- a/app/javascript/src/javascripts/media_asset_component.js +++ b/app/javascript/src/javascripts/media_asset_component.js @@ -1,19 +1,19 @@ export default class MediaAssetComponent { static initialize() { - $(".media-asset-container").toArray().forEach(element => { + $(".media-asset-component").toArray().forEach(element => { new MediaAssetComponent(element); }); } constructor(element) { - this.$container = $(element); - this.$component = this.$container.find(".media-asset-component"); + this.$component = $(element); + this.$container = this.$component.find(".media-asset-container"); + this.$image = this.$component.find(".media-asset-image"); + this.$zoomLevel = this.$component.find(".media-asset-zoom-level"); - if (this.$container.attr("data-dynamic-height") === "true") { + if (this.$component.attr("data-dynamic-height") === "true") { this.updateHeight(); - $(window).on("scroll.danbooru", element => { - this.updateHeight(); - }); + $(window).on("scroll.danbooru", e => this.updateHeight()); } if (this.$image.length) { @@ -26,8 +26,10 @@ export default class MediaAssetComponent { } toggleFit() { - if (this.canZoom) { - this.$container.toggleClass("media-asset-container-fit-height"); + if (this.canZoomOut) { + this.$component.addClass("media-asset-component-fit-height media-asset-component-fit-width"); + } else if (this.canZoomHeight) { + this.$component.removeClass("media-asset-component-fit-height"); } this.updateZoom(); @@ -46,19 +48,19 @@ export default class MediaAssetComponent { updateHeight() { // XXX 115 = header height (hardcoded to prevent height glitches as page loads) - this.$container.css("--header-visible-height", Math.min(115, Math.max(0, this.$container.offset().top - $(window).scrollTop())) + "px"); + this.$component.css("--header-visible-height", Math.min(115, Math.max(0, this.$component.offset().top - $(window).scrollTop())) + "px"); } get zoomLevel() { return this.$image.width() / Number(this.$image.attr("width")); } - get canZoom() { - return this.canZoomIn || this.canZoomOut; + get canZoomIn() { + return this.canZoomHeight; } - get canZoomIn() { - return !this.isZoomed && this.$image.height() < this.$image.get(0).naturalHeight && Math.round(this.$image.width()) < Math.round(this.$container.width()); + get canZoomHeight() { + return !this.isZoomed && this.$image.height() < this.$image.get(0).naturalHeight && Math.round(this.$image.width()) < Math.round(this.$component.width()); } get canZoomOut() { @@ -66,15 +68,7 @@ export default class MediaAssetComponent { } get isZoomed() { - return !this.$container.is(".media-asset-container-fit-height"); - } - - get $image() { - return this.$component.find(".media-asset-image"); - } - - get $zoomLevel() { - return this.$component.find(".media-asset-zoom-level"); + return !this.$component.is(".media-asset-component-fit-height.media-asset-component-fit-width"); } } diff --git a/app/javascript/src/styles/common/utilities.scss b/app/javascript/src/styles/common/utilities.scss index c396e2cb0..604a627ba 100644 --- a/app/javascript/src/styles/common/utilities.scss +++ b/app/javascript/src/styles/common/utilities.scss @@ -47,6 +47,7 @@ $spacer: 0.25rem; /* 4px */ .overflow-auto { overflow: auto; } .overflow-hidden { overflow: hidden; } +.overflow-x-hidden { overflow-x: hidden; } .break-all { word-break: break-all; overflow-wrap: anywhere; } .break-words { word-break: break-word; overflow-wrap: anywhere; } diff --git a/app/views/media_assets/show.html.erb b/app/views/media_assets/show.html.erb index 2c74a8ca8..c0d5508c7 100644 --- a/app/views/media_assets/show.html.erb +++ b/app/views/media_assets/show.html.erb @@ -1,7 +1,7 @@
-
+
<%= render MediaAssetComponent.new(media_asset: @media_asset, current_user: CurrentUser.user, outer_classes: "sticky h-full relative top-0", inner_classes: "mx-auto", dynamic_height: true) do |component| %> <% component.with_header do %>