From 0a62196d28e554db8434602b87f6c160622b9b42 Mon Sep 17 00:00:00 2001 From: Tim Fischbach Date: Thu, 22 Feb 2024 17:11:27 +0100 Subject: [PATCH] Allow registering additional editor packs with stylesheets Core editor styles are inserted as inline style tags via `rollup-plugin-postcss` which relies on `style-inject`. For additional editor packs in the host application, this mechanism does not work with Shakapacker's default setup. We thus add an option to also add links to additional stylesheet packs. REDMINE-20374 --- .../helpers/pageflow_scrolled/packs_helper.rb | 11 +++++++ .../editor/entries/_head.html.erb | 1 + .../lib/pageflow_scrolled/additional_packs.rb | 11 +++++-- .../pageflow_scrolled/packs_helper_spec.rb | 31 +++++++++++++++++++ 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/entry_types/scrolled/app/helpers/pageflow_scrolled/packs_helper.rb b/entry_types/scrolled/app/helpers/pageflow_scrolled/packs_helper.rb index ea764c741..7b4694b35 100644 --- a/entry_types/scrolled/app/helpers/pageflow_scrolled/packs_helper.rb +++ b/entry_types/scrolled/app/helpers/pageflow_scrolled/packs_helper.rb @@ -22,6 +22,13 @@ def scrolled_editor_javascript_packs_tag(entry) ) end + def scrolled_editor_stylesheet_packs_tag(entry) + stylesheet_pack_tag( + *scrolled_editor_stylesheet_packs(entry), + media: 'all' + ) + end + def scrolled_frontend_packs(entry, widget_scope:) ['pageflow-scrolled-frontend'] + scrolled_additional_frontend_packs(entry, widget_scope) + @@ -34,6 +41,10 @@ def scrolled_editor_packs(entry) scrolled_frontend_widget_type_packs(entry, :editor) end + def scrolled_editor_stylesheet_packs(entry) + Pageflow.config_for(entry).additional_editor_packs.stylesheet_paths + end + private def scrolled_additional_frontend_packs(entry, widget_scope) diff --git a/entry_types/scrolled/app/views/pageflow_scrolled/editor/entries/_head.html.erb b/entry_types/scrolled/app/views/pageflow_scrolled/editor/entries/_head.html.erb index 2eb090933..2c6456134 100644 --- a/entry_types/scrolled/app/views/pageflow_scrolled/editor/entries/_head.html.erb +++ b/entry_types/scrolled/app/views/pageflow_scrolled/editor/entries/_head.html.erb @@ -9,3 +9,4 @@ <%= scrolled_webpack_public_path_script_tag %> <%= scrolled_editor_javascript_packs_tag(entry) %> +<%= scrolled_editor_stylesheet_packs_tag(entry) %> diff --git a/entry_types/scrolled/lib/pageflow_scrolled/additional_packs.rb b/entry_types/scrolled/lib/pageflow_scrolled/additional_packs.rb index 7747f7005..21f43c986 100644 --- a/entry_types/scrolled/lib/pageflow_scrolled/additional_packs.rb +++ b/entry_types/scrolled/lib/pageflow_scrolled/additional_packs.rb @@ -8,8 +8,8 @@ def initialize # content_element_type_names option only takes effect for frontend # packs. - def register(path, content_element_type_names: []) - @packs << AdditionalPack.new(path, content_element_type_names) + def register(path, content_element_type_names: [], stylesheet: false) + @packs << AdditionalPack.new(path, content_element_type_names, stylesheet) end # @api private @@ -24,6 +24,11 @@ def paths @packs.map(&:path) end + # @api private + def stylesheet_paths + @packs.select(&:stylesheet).map(&:path) + end + # @api private def paths_for_content_element_types(type_names) @packs.reject { |pack| @@ -33,6 +38,6 @@ def paths_for_content_element_types(type_names) end # @api private - AdditionalPack = Struct.new(:path, :content_element_type_names) + AdditionalPack = Struct.new(:path, :content_element_type_names, :stylesheet) end end diff --git a/entry_types/scrolled/spec/helpers/pageflow_scrolled/packs_helper_spec.rb b/entry_types/scrolled/spec/helpers/pageflow_scrolled/packs_helper_spec.rb index 5616ccb4e..a6a8f4ce7 100644 --- a/entry_types/scrolled/spec/helpers/pageflow_scrolled/packs_helper_spec.rb +++ b/entry_types/scrolled/spec/helpers/pageflow_scrolled/packs_helper_spec.rb @@ -247,5 +247,36 @@ module PageflowScrolled expect(result).to include('pageflow-scrolled/widgets/customNavigation') end end + + describe 'scrolled_editor_stylesheet_packs' do + it 'empty by default' do + entry = create(:published_entry, type_name: 'scrolled') + + result = helper.scrolled_editor_stylesheet_packs(entry) + + expect(result).to eq([]) + end + + it 'includes additional editor packs in editor with stylesheet option' do + pageflow_configure do |config| + config.for_entry_type(PageflowScrolled.entry_type) do |entry_type_config| + entry_type_config.additional_editor_packs.register( + 'pageflow-scrolled/only-js' + ) + + entry_type_config.additional_editor_packs.register( + 'pageflow-scrolled/extra-editor', + stylesheet: true + ) + end + end + + entry = create(:published_entry, type_name: 'scrolled') + + result = helper.scrolled_editor_stylesheet_packs(entry) + + expect(result).to eq(['pageflow-scrolled/extra-editor']) + end + end end end