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
Storefront: support nice color names / color visualization for variants
Damian Legawiec75463f65b855
diff --git a/storefront/app/models/spree/color_names.rb b/storefront/app/models/spree/color_names.rbnew file mode 100644index 0000000000..f4b11be95a--- /dev/null+++ b/storefront/app/models/spree/color_names.rb@@ -0,0 +1,35 @@+module Spree+ class ColorNames+ include Singleton++ class << self+ def colors+ @colors ||= Rails.cache.fetch('color_names', expires_in: 1.day) do+ file_path = File.join(Rails.root, 'lib', 'assets', 'colornames.json')++ if File.exist?(file_path)+ JSON.parse(File.read(file_path))+ else+ []+ end+ end+ end++ def colors_cache+ @colors_cache ||= colors.inject({}) do |hash, color|+ hash[color['name'].downcase] = color+ hash+ end+ end++ def find_by_name(name)+ colors_cache[name.downcase]+ end++ def split_by_color_name(name)+ multi_color_regex = /(\s+and\s+|\s*-\s*|\s*&\s*|\s*\+\s*|\s*\/\s*)/+ name.gsub(multi_color_regex, ',').split(',').map(&:strip)+ end+ end+ end+enddiff --git a/storefront/app/presenters/spree/colors_preview_styles_presenter.rb b/storefront/app/presenters/spree/colors_preview_styles_presenter.rbnew file mode 100644index 0000000000..939bb96249--- /dev/null+++ b/storefront/app/presenters/spree/colors_preview_styles_presenter.rb@@ -0,0 +1,84 @@+module Spree+ class ColorsPreviewStylesPresenter+ def initialize(colors)+ @colors = colors.compact_blank.map do |color|+ case color+ when String+ color_name = CGI.unescapeHTML(color.strip)++ { name: color_name, filter_name: color_name }+ when Hash+ color_name = CGI.unescapeHTML(color[:name].strip)+ filter_name = color[:filter_name].present? ? CGI.unescapeHTML(color[:filter_name].strip) : color_name++ { name: color_name, filter_name: filter_name }+ end+ end+ end++ def to_s+ @to_s ||= if colors.any?+ css = ['<style>']++ colors.each do |color|+ css_color = css_colors_hash[color[:filter_name]] || color[:filter_name].gsub(' ', '')+ color_name = color[:name]+ css << <<~CSS+ @supports(background: #{css_color}) {+ .color-input[value="#{color_name}"] ~ .label-container .color-preview,+ [data-color="#{color_name}"] .color-preview {+ background: #{css_color};+ display: inline-flex;+ }+ .color-input[value="#{color_name}"] ~ .label-container .color-label,+ [data-color="#{color_name}"] .color-label {+ display: none;+ }+ }+ CSS+ end++ css << '</style>'++ css.join("\n")+ end+ end++ private++ attr_reader :colors++ def css_colors_hash+ @css_colors_hash ||= begin+ colors_hash = {}++ colors.each do |color|+ color_name = color[:filter_name]+ multi_colors = Spree::ColorNames.split_by_color_name(color_name)+ hex_colors = multi_colors.map(&method(:find_color)).compact++ if hex_colors.any?+ colors_hash[color_name] = generate_css_color(hex_colors)+ elsif (subcolors = color_name.split.compact) && subcolors.length > 1+ subcolors = subcolors.map(&method(:find_color)).compact+ colors_hash[color_name] = generate_css_color(subcolors)+ end+ end++ colors_hash+ end+ end++ def generate_css_color(hex_colors)+ if hex_colors.length > 1+ "linear-gradient(to right, #{hex_colors.join(', ')})"+ elsif hex_colors.length == 1+ hex_colors.first+ end+ end++ def find_color(color)+ Spree::ColorNames.find_by_name(color)&.dig('hex')+ end+ end+enddiff --git a/storefront/lib/generators/spree/storefront/setup_color_names/setup_color_names_generator.rb b/storefront/lib/generators/spree/storefront/setup_color_names/setup_color_names_generator.rbnew file mode 100644index 0000000000..aa305ce7ab--- /dev/null+++ b/storefront/lib/generators/spree/storefront/setup_color_names/setup_color_names_generator.rb@@ -0,0 +1,23 @@+require 'rails/generators'+require 'open-uri'+require 'fileutils'++module Spree+ module Storefront+ module Generators+ class SetupColorNamesGenerator < Rails::Generators::Base+ desc 'Downloads color names list from unpkg.com'++ def download_color_names+ say_status :downloading, 'color names list'+ FileUtils.mkdir_p('lib/assets')+ URI.open('https://unpkg.com/color-name-list/dist/colornames.json') do |remote_file|+ File.open('lib/assets/colornames.json', 'wb') do |local_file|+ local_file.write(remote_file.read)+ end+ end+ end+ end+ end+ end+end