Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Improve image handling for sizing, make each image size available through the API.
Matthew Kennedyc63bd6d88e98
diff --git a/api/app/serializers/spree/v2/storefront/cms_section_serializer.rb b/api/app/serializers/spree/v2/storefront/cms_section_serializer.rbindex 5eb672e676..98396f681f 100644--- a/api/app/serializers/spree/v2/storefront/cms_section_serializer.rb+++ b/api/app/serializers/spree/v2/storefront/cms_section_serializer.rb@@ -6,45 +6,14 @@ module Spreeattributes :name, :content, :settings, :link, :fit, :type, :position- attribute :image_one_path do |section|- if section.image_one.attached?- url_helpers = Rails.application.routes.url_helpers- url_helpers.rails_blob_path(section.image_one, only_path: true)- end- end-- attribute :image_two_path do |section|- if section.image_two.attached?- url_helpers = Rails.application.routes.url_helpers- url_helpers.rails_blob_path(section.image_two, only_path: true)- end- end-- attribute :image_three_path do |section|- if section.image_three.attached?- url_helpers = Rails.application.routes.url_helpers- url_helpers.rails_blob_path(section.image_three, only_path: true)- end- end-- attribute :image_four_path do |section|- if section.image_four.attached?- url_helpers = Rails.application.routes.url_helpers- url_helpers.rails_blob_path(section.image_four, only_path: true)- end- end-- attribute :image_five_path do |section|- if section.image_five.attached?- url_helpers = Rails.application.routes.url_helpers- url_helpers.rails_blob_path(section.image_five, only_path: true)- end- end-- attribute :image_six_path do |section|- if section.image_six.attached?- url_helpers = Rails.application.routes.url_helpers- url_helpers.rails_blob_path(section.image_six, only_path: true)+ Spree::CmsSection::IMAGE_COUNT.each do |count|+ Spree::CmsSection::IMAGE_SIZE.each do |size|+ attribute "img_#{count}_#{size}".to_sym do |section|+ if section.send("image_#{count}").attached? && section.send("img_#{count}_#{size}").present?+ url_helpers = Rails.application.routes.url_helpers+ url_helpers.rails_representation_path(section.send("img_#{count}_#{size}"), only_path: true)+ end+ endendenddiff --git a/backend/app/assets/stylesheets/spree/backend/views/_cms_pages.scss b/backend/app/assets/stylesheets/spree/backend/views/_cms_pages.scssindex 1c1baa1f9c..06693c46cd 100644--- a/backend/app/assets/stylesheets/spree/backend/views/_cms_pages.scss+++ b/backend/app/assets/stylesheets/spree/backend/views/_cms_pages.scss@@ -58,6 +58,10 @@ button#cmsSectionEditorFullScreen {}}+#draggableSectionsHolder {+ max-height: 750px;+}+#cmsPagesectionsArea {overflow-x: hidden;overflow-y: scroll;diff --git a/core/app/models/spree/cms/sections/hero_image.rb b/core/app/models/spree/cms/sections/hero_image.rbindex a0a1522830..f02a210007 100644--- a/core/app/models/spree/cms/sections/hero_image.rb+++ b/core/app/models/spree/cms/sections/hero_image.rb@@ -12,10 +12,28 @@ module Spree::Cms::Sectionsgutters == 'Gutters'end+ def img_one_sm(dimensions = '600x250>')+ super+ end++ def img_one_md(dimensions = '1200x500>')+ super+ end++ def img_one_lg(dimensions = '2400x1000>')+ super+ end++ def img_one_xl(dimensions = '4800x2000>')+ super+ end+privatedef reset_link_attributesif linked_resource_type_changed?+ return if linked_resource_id_was.nil?+self.linked_resource_id = nilendenddiff --git a/core/app/models/spree/cms/sections/image_gallery.rb b/core/app/models/spree/cms/sections/image_gallery.rbindex 8c47305e63..0fc77c0eda 100644--- a/core/app/models/spree/cms/sections/image_gallery.rb+++ b/core/app/models/spree/cms/sections/image_gallery.rb@@ -13,6 +13,48 @@ module Spree::Cms::Sectionsstore :settings, accessors: [:layout_style, :display_labels], coder: JSON+ #+ # img_one sizing+ def img_one_md(dimensions = '270x195>')+ super+ end++ def img_one_lg(dimensions = '540x390>')+ super+ end++ def img_one_xl(dimensions = '1080x780>')+ super+ end++ #+ # img_two sizing+ def img_two_md(dimensions = '270x400>')+ super+ end++ def img_two_lg(dimensions = '540x800>')+ super+ end++ def img_two_xl(dimensions = '1080x1600>')+ super+ end++ #+ # img_three sizing+ def img_three_md(dimensions = '270x195>')+ super+ end++ def img_three_lg(dimensions = '540x390>')+ super+ end++ def img_three_xl(dimensions = '1080x780>')+ super+ end+def default_layout?layout_style == 'Default'end@@ -27,14 +69,20 @@ module Spree::Cms::Sectionsreturn if Rails::VERSION::STRING < '6.0'if link_type_one_changed?+ return if link_type_one_was.nil?+self.link_one = nilendif link_type_two_changed?+ return if link_type_two_was.nil?+self.link_two = nilendif link_type_three_changed?+ return if link_type_three_was.nil?+self.link_three = nilendenddiff --git a/core/app/models/spree/cms/sections/side_by_side_images.rb b/core/app/models/spree/cms/sections/side_by_side_images.rbindex bcc5e88d34..4f0168b94d 100644--- a/core/app/models/spree/cms/sections/side_by_side_images.rb+++ b/core/app/models/spree/cms/sections/side_by_side_images.rb@@ -14,16 +14,48 @@ module Spree::Cms::Sectionsgutters == 'Gutters'end+ #+ # img_one sizing+ def img_one_md(dimensions = '387x250>')+ super+ end++ def img_one_lg(dimensions = '540x390>')+ super+ end++ def img_one_xl(dimensions = '1468x952>')+ super+ end++ #+ # img_two sizing+ def img_two_md(dimensions = '387x250>')+ super+ end++ def img_two_lg(dimensions = '734x476>')+ super+ end++ def img_two_xl(dimensions = '1468x952>')+ super+ end+privatedef reset_link_attributesreturn if Rails::VERSION::STRING < '6.0'if link_type_one_changed?+ return if link_type_one_was.nil?+self.link_one = nilendif link_type_two_changed?+ return if link_type_two_was.nil?+self.link_two = nilendenddiff --git a/core/app/models/spree/cms_section.rb b/core/app/models/spree/cms_section.rbindex 29fe45bcfc..3e5d2272c4 100644--- a/core/app/models/spree/cms_section.rb+++ b/core/app/models/spree/cms_section.rb@@ -2,21 +2,24 @@ module Spreeclass CmsSection < Spree::Baseinclude Spree::DisplayLink- IMAGE_TYPES = ['image/png', 'image/jpg', 'image/jpeg', 'image/gif'].freeze-acts_as_list scope: :cms_pagebelongs_to :cms_page, touch: true- # Six images in one section should cover a large number of potential use cases.- # The standardization of putting them here means we can offer them up through- # the API serializer vs hoping they get added on a per-section basis and the naming- # matches those used in the API serializer attributes.- has_one_attached :image_one- has_one_attached :image_two- has_one_attached :image_three- has_one_attached :image_four- has_one_attached :image_five- has_one_attached :image_six+ IMAGE_COUNT = ['one', 'two', 'three']+ IMAGE_TYPES = ['image/png', 'image/jpg', 'image/jpeg', 'image/gif'].freeze+ IMAGE_SIZE = ['sm', 'md', 'lg', 'xl']++ IMAGE_COUNT.each do |count|+ self.send(:has_one_attached, "image_#{count}".to_sym)++ IMAGE_SIZE.each do |size|+ define_method("img_#{count}_#{size}") do |dimensions = nil|+ return if !send("image_#{count}").attached? || dimensions.nil?++ send("image_#{count}").variant(resize: dimensions)+ end+ end+ endbelongs_to :linked_resource, polymorphic: true@@ -24,8 +27,7 @@ module Spreevalidates :name, :cms_page, presence: true- validates :image_one, :image_two, :image_three, :image_four,- :image_five, :image_six, content_type: IMAGE_TYPES+ validates :image_one, :image_two, :image_three, content_type: IMAGE_TYPESLINKED_RESOURCE_TYPE = []diff --git a/frontend/app/views/spree/shared/cms/sections/_hero_image.html.erb b/frontend/app/views/spree/shared/cms/sections/_hero_image.html.erbindex 6cd12d7860..e89f05ec78 100644--- a/frontend/app/views/spree/shared/cms/sections/_hero_image.html.erb+++ b/frontend/app/views/spree/shared/cms/sections/_hero_image.html.erb@@ -7,21 +7,33 @@<% else %><% section_padding = 'px-2' %><% end %>-<div class="col-12 homepage-hero-image position-relative p-0 <%= section_padding %>">+<div class="bg-light" style="--aspect-ratio:12/5;"><% if section.image_one.present? %>- <!-- XS to LG -->- <img class="d-lg-none lazyload"- data-src="<%= main_app.url_for(section.image_one.attachment.variant(resize: '1200x500>')) %>"- data-srcset="<%= main_app.url_for(section.image_one.attachment.variant(resize: '1200x500>')) %>"+ <!-- SM to MD -->+ <img class="d-block d-md-none lazyload"+ data-src="<%= main_app.url_for(section.img_one_sm) %>"+ data-srcset="<%= main_app.url_for(section.img_one_sm) %>"+ alt="<%= section&.title %>">++ <!-- MD to LG -->+ <img class="d-none d-md-block d-lg-none lazyload"+ data-src="<%= main_app.url_for(section.img_one_md) %>"+ data-srcset="<%= main_app.url_for(section.img_one_md) %>"+ alt="<%= section&.title %>">++ <!-- LG to XL -->+ <img class="d-none d-lg-block d-xl-none lazyload"+ data-src="<%= main_app.url_for(section.img_one_lg) %>"+ data-srcset="<%= main_app.url_for(section.img_one_lg) %>"alt="<%= section&.title %>">- <!-- LG UP -->- <img class="d-none d-lg-block lazyload"- data-src="<%= main_app.url_for(section.image_one.attachment.variant(resize: '2400x1000>')) %>"- data-srcset="<%= main_app.url_for(section.image_one.attachment.variant(resize: '2400x1000>')) %>"+ <!-- XL UP -->+ <img class="d-none d-xl-block lazyload"+ data-src="<%= main_app.url_for(section.img_one_xl) %>"+ data-srcset="<%= main_app.url_for(section.img_one_xl) %>"alt="<%= section&.title %>"><% else %>diff --git a/frontend/app/views/spree/shared/cms/sections/_image_gallery.html.erb b/frontend/app/views/spree/shared/cms/sections/_image_gallery.html.erbindex d26a12fa7b..eec658681d 100644--- a/frontend/app/views/spree/shared/cms/sections/_image_gallery.html.erb+++ b/frontend/app/views/spree/shared/cms/sections/_image_gallery.html.erb@@ -68,20 +68,20 @@<!-- XS to MD --><img class="d-md-none lazyload"- data-src="<%= main_app.url_for(section.image_one.attachment.variant(resize: '270x195>')) %>"- data-srcset="<%= main_app.url_for(section.image_one.attachment.variant(resize: '270x195>')) %>"+ data-src="<%= main_app.url_for(section.img_one_md) %>"+ data-srcset="<%= main_app.url_for(section.img_one_md) %>"alt="<%= section&.title_one %>"><!-- MD to XL --><img class="d-none d-md-block d-xl-none lazyload"- data-src="<%= main_app.url_for(section.image_one.attachment.variant(resize: '540x390>')) %>"- data-srcset="<%= main_app.url_for(section.image_one.attachment.variant(resize: '540x390>')) %>"+ data-src="<%= main_app.url_for(section.img_one_lg) %>"+ data-srcset="<%= main_app.url_for(section.img_one_lg) %>"alt="<%= section&.title_one %>"><!-- XL UP --><img class="d-none d-xl-block lazyload"- data-src="<%= main_app.url_for(section.image_one.attachment.variant(resize: '1080x780>')) %>"- data-srcset="<%= main_app.url_for(section.image_one.attachment.variant(resize: '1080x780>')) %>"+ data-src="<%= main_app.url_for(section.img_one_xl) %>"+ data-srcset="<%= main_app.url_for(section.img_one_xl) %>"alt="<%= section&.title_one %>"><% else %><%= icon( name: 'card-image',@@ -106,20 +106,20 @@<!-- XS to MD --><img class="d-md-none lazyload"- data-src="<%= main_app.url_for(section.image_three.attachment.variant(resize: '270x195>')) %>"- data-srcset="<%= main_app.url_for(section.image_three.attachment.variant(resize: '270x195>')) %>"+ data-src="<%= main_app.url_for(section.img_three_md) %>"+ data-srcset="<%= main_app.url_for(section.img_three_md) %>"alt="<%= section&.title_three %>"><!-- MD to XL --><img class="d-none d-md-block d-xl-none lazyload"- data-src="<%= main_app.url_for(section.image_three.attachment.variant(resize: '540x390>')) %>"- data-srcset="<%= main_app.url_for(section.image_three.attachment.variant(resize: '540x390>')) %>"+ data-src="<%= main_app.url_for(section.img_three_lg) %>"+ data-srcset="<%= main_app.url_for(section.img_three_lg) %>"alt="<%= section&.title_three %>"><!-- XL UP --><img class="d-none d-xl-block lazyload"- data-src="<%= main_app.url_for(section.image_three.attachment.variant(resize: '1080x780>')) %>"- data-srcset="<%= main_app.url_for(section.image_three.attachment.variant(resize: '1080x780>')) %>"+ data-src="<%= main_app.url_for(section.img_three_xl) %>"+ data-srcset="<%= main_app.url_for(section.img_three_xl) %>"alt="<%= section&.title_three %>"><% else %><%= icon( name: 'card-image',@@ -143,20 +143,20 @@<!-- XS to MD --><img class="d-md-none lazyload"- data-src="<%= main_app.url_for(section.image_two.variant(resize: '270x400>')) %>"- data-srcset="<%= main_app.url_for(section.image_two.variant(resize: '270x400>')) %>"+ data-src="<%= main_app.url_for(section.img_two_md) %>"+ data-srcset="<%= main_app.url_for(section.img_two_md) %>"alt="<%= section&.title_two %>"><!-- MD to XL --><img class="d-none d-md-block d-xl-none lazyload"- data-src="<%= main_app.url_for(section.image_two.variant(resize: '540x800>')) %>"- data-srcset="<%= main_app.url_for(section.image_two.variant(resize: '540x800>')) %>"+ data-src="<%= main_app.url_for(section.img_two_lg) %>"+ data-srcset="<%= main_app.url_for(section.img_two_lg) %>"alt="<%= section&.title_two %>"><!-- XL UP --><img class="d-none d-xl-block lazyload"- data-src="<%= main_app.url_for(section.image_two.variant(resize: '1080x1600>')) %>"- data-srcset="<%= main_app.url_for(section.image_two.variant(resize: '1080x1600>')) %>"+ data-src="<%= main_app.url_for(section.img_two_xl) %>"+ data-srcset="<%= main_app.url_for(section.img_two_xl) %>"alt="<%= section&.title_two %>"><% else %><%= icon( name: 'file-image',diff --git a/frontend/app/views/spree/shared/cms/sections/_side_by_side_images.html.erb b/frontend/app/views/spree/shared/cms/sections/_side_by_side_images.html.erbindex 589a9aeb5f..0ffaec6933 100644--- a/frontend/app/views/spree/shared/cms/sections/_side_by_side_images.html.erb+++ b/frontend/app/views/spree/shared/cms/sections/_side_by_side_images.html.erb@@ -39,20 +39,20 @@<% if section.image_one.attached? %><!-- XS to MD --><img class="d-md-none lazyload"- data-src="<%= main_app.url_for(section.image_one.attachment.variant(resize: '387x250>')) %>"- data-srcset="<%= main_app.url_for(section.image_one.attachment.variant(resize: '387x250>')) %>"+ data-src="<%= main_app.url_for(section.img_one_md) %>"+ data-srcset="<%= main_app.url_for(section.img_one_md) %>"alt="<%= section&.title_one %>"><!-- MD to XL --><img class="d-none d-md-block d-xl-none lazyload"- data-src="<%= main_app.url_for(section.image_one.attachment.variant(resize: '734x476>')) %>"- data-srcset="<%= main_app.url_for(section.image_one.attachment.variant(resize: '734x476>')) %>"+ data-src="<%= main_app.url_for(section.img_one_lg) %>"+ data-srcset="<%= main_app.url_for(section.img_one_lg) %>"alt="<%= section&.title_one %>"><!-- XL UP --><img class="d-none d-xl-block lazyload"- data-src="<%= main_app.url_for(section.image_one.attachment.variant(resize: '1468x952>')) %>"- data-srcset="<%= main_app.url_for(section.image_one.attachment.variant(resize: '1468x952>')) %>"+ data-src="<%= main_app.url_for(section.img_one_xl) %>"+ data-srcset="<%= main_app.url_for(section.img_one_xl) %>"alt="<%= section&.title_one %>"><% else %><%= icon( name: 'card-image',@@ -82,20 +82,20 @@<% if section.image_two.present? %><!-- XS to MD --><img class="d-md-none lazyload"- data-src="<%= main_app.url_for(section.image_two.attachment.variant(resize: '387x250>')) %>"- data-srcset="<%= main_app.url_for(section.image_two.attachment.variant(resize: '387x250>')) %>"+ data-src="<%= main_app.url_for(section.img_two_md) %>"+ data-srcset="<%= main_app.url_for(section.img_two_md) %>"alt="<%= section&.title_two %>"><!-- MD to XL --><img class="d-none d-md-block d-xl-none lazyload"- data-src="<%= main_app.url_for(section.image_two.attachment.variant(resize: '734x476>')) %>"- data-srcset="<%= main_app.url_for(section.image_two.attachment.variant(resize: '734x476>')) %>"+ data-src="<%= main_app.url_for(section.img_two_lg) %>"+ data-srcset="<%= main_app.url_for(section.img_two_lg) %>"alt="<%= section&.title_two %>"><!-- XL UP --><img class="d-none d-xl-block lazyload"- data-src="<%= main_app.url_for(section.image_two.attachment.variant(resize: '1468x952>')) %>"- data-srcset="<%= main_app.url_for(section.image_two.attachment.variant(resize: '1468x952>')) %>"+ data-src="<%= main_app.url_for(section.img_two_xl) %>"+ data-srcset="<%= main_app.url_for(section.img_two_xl) %>"alt="<%= section&.title_two %>"><% else %><%= icon( name: 'card-image',