Skip to content

Commit

Permalink
Add possibility to show an outline in preview if no tag is present
Browse files Browse the repository at this point in the history
Extend the element_view_for - method to show an outline to element, that has a disabled tag - option. It adds an unregistered custom component tag (which will have no visual representation except one display property), to make the element clickable and highlightable.
  • Loading branch information
kulturbande committed Feb 25, 2024
1 parent 68a79c7 commit 42a095f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
8 changes: 7 additions & 1 deletion app/helpers/alchemy/elements_block_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,13 @@ def element_view_for(element, options = {})
end

# wrap output in a useful DOM element
if (tag = options.delete(:tag))
tag = options.delete(:tag)

# add custom element with no representation if the user is in the preview mode to allow
# an interaction with the element
tag = "alchemy-preview" if tag.blank? && is_element_in_preview_mode?(element)

if tag.present?
# add preview attributes
options.merge!(element_preview_code_attributes(element))

Expand Down
7 changes: 6 additions & 1 deletion app/helpers/alchemy/elements_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,14 @@ def element_preview_code(element)
tag_builder.tag_options(element_preview_code_attributes(element))
end

# is element in preview mode and connect with current page
def is_element_in_preview_mode?(element)
element.present? && @preview_mode && element.page == @page
end

# Returns a hash containing the HTML tag attributes required for preview mode.
def element_preview_code_attributes(element)
return {} unless element.present? && @preview_mode && element.page == @page
return {} unless is_element_in_preview_mode?(element)

{"data-alchemy-element" => element.id}
end
Expand Down
5 changes: 5 additions & 0 deletions app/views/alchemy/_preview_mode_code.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<% if @preview_mode %>
<style>
alchemy-preview {
display: flow;
}
</style>
<script type="text/javascript">
Alchemy = { locale: "<%= session[:alchemy_locale] %>" };
</script>
Expand Down
5 changes: 5 additions & 0 deletions spec/helpers/alchemy/elements_block_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ module Alchemy

subject { helper.element_view_for(element) }
it { is_expected.to have_css "#{expected_wrapper_tag}[data-alchemy-element='#{element.id}']" }

context "without tag" do
subject { helper.element_view_for(element, tag: false) }
it { is_expected.to have_css "alchemy-preview[data-alchemy-element='#{element.id}']" }
end
end
end

Expand Down
14 changes: 14 additions & 0 deletions spec/helpers/alchemy/elements_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,20 @@ module Alchemy
end
end

describe "#is_element_in_preview_mode?" do
subject { helper.is_element_in_preview_mode?(element) }

context "in preview_mode" do
before { assign(:preview_mode, true) }

it { is_expected.to be_truthy }
end

context "not in preview_mode" do
it { is_expected.to be_falsy }
end
end

describe "#element_preview_code" do
subject { helper.element_preview_code(element) }

Expand Down

0 comments on commit 42a095f

Please sign in to comment.