Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions app/components/maglev/section_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
# we need to move it along with the PageComponent and BlockComponent
# to a separate folder (content?)

# rubocop:disable Metrics/ClassLength
module Maglev
class SectionComponent < BaseComponent
include TagHelper

extend Forwardable

def_delegators :parent, :site, :page, :config

attr_reader :parent, :id, :type, :settings, :attributes, :definition, :templates_root_path,
Expand All @@ -35,8 +35,11 @@ def dom_id
@dom_id ||= "section-#{id}"
end

# the lock version of the store the section content comes from
# (stamped by the sections content fetcher, even for site scoped sections
# whose content lives in the global site scoped store)
def lock_version
@lock_version ||= lock_source&.lock_version || '0'
attributes[:lock_version] || '0'
end

def dom_data
Expand Down Expand Up @@ -68,10 +71,6 @@ def render

private

def lock_source
definition&.site_scoped? ? site : page
end

def build_block_list
build_blocks(attributes[:blocks])
end
Expand Down Expand Up @@ -140,4 +139,3 @@ def inspect_fields
end
end
end
# rubocop:enable Metrics/ClassLength
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ module LockVersionConcern

private

# NOTE: we can't always rely on the lock version of the store the section was fetched
# from: a site scoped section is listed in a layout store (its "slot") but its content
# lives in the global site scoped store, which is what the content services update.
# Fortunately, the sections content fetcher stamps each section with the lock version
# of the store its content actually comes from, so we use it whenever possible.
def source_lock_version
sections_store.lock_version || 0
@section ? @section.lock_version : (sections_store.lock_version || 0)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/maglev/editor/section_blocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def sections_store
end

def update_section_block
services.update_section_block.call(
@updated_store = services.update_section_block.call(
store: sections_store,
section_id: @section.id,
block_id: @section_block.id,
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/maglev/editor/sections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def create_section
end

def update_section
services.update_section.call(
@updated_store = services.update_section.call(
store: sections_store,
section_id: @section.id,
content: params[:section].to_unsafe_h,
Expand Down
8 changes: 7 additions & 1 deletion app/models/maglev/content/section_content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ def block_definitions
end

def root_blocks
blocks.select(&:root?)
# NOTE: a block whose type is root-eligible (root: true, the default) can still be
# nested under another block when its type is also accepted as a child
# (e.g. a navbar menu_item inside another menu_item). Selecting blocks by their
# definition's root? flag would return those nested blocks as well, rendering them
# twice in the editor tree. So we filter on the parent_id instead, like the
# front-end block tree builder does.
blocks.select { |block| block.parent_id.blank? }
end

# return the definitions of root blocks that can be added
Expand Down
22 changes: 20 additions & 2 deletions app/services/maglev/content/sort_section_blocks_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,29 @@ def sort_section_blocks!(source)
source.save!
end

# Only the siblings (the blocks sharing the same parent) are re-ordered, in place.
# The other blocks keep their position in the flat list: sorting the whole list
# with a non-stable sort (Array#sort_by!) used to shuffle the blocks outside of
# the sorted scope, like the children of the other blocks in a tree.
def sort_section_blocks(source)
find_blocks(source)&.sort_by! do |block|
block['parent_id'] == parent_id ? block_ids.index(block['id']) || Float::INFINITY : -1
blocks = find_blocks(source)
return if blocks.blank?

indices = blocks.each_index.select { |index| sibling?(blocks[index]) }
sorted_siblings = sort_siblings(blocks.values_at(*indices))

indices.zip(sorted_siblings) { |index, block| blocks[index] = block }
end

def sort_siblings(siblings)
siblings.sort_by.with_index do |block, position|
[block_ids.index(block['id']) || Float::INFINITY, position]
end
end

def sibling?(block)
block['parent_id'].presence == parent_id.presence
end
end
end
end
5 changes: 5 additions & 0 deletions app/services/maglev/content/update_section_block_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def call
private

def unsafe_call
# return the store which has been actually written into, so the caller
# can read its fresh lock version (a site scoped section is listed in
# a layout store but its content is written in the site scoped store)
if site_scoped?
update_section_block_content!(site_scoped_store)
else
Expand All @@ -41,6 +44,8 @@ def update_section_block_content!(source)
source.sections_translations_will_change!
update_section_block_content(source)
source.save!

source
end

def update_section_block_content(source)
Expand Down
5 changes: 5 additions & 0 deletions app/services/maglev/content/update_section_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def call
private

def unsafe_call
# return the store which has been actually written into, so the caller
# can read its fresh lock version (a site scoped section is listed in
# a layout store but its content is written in the site scoped store)
if site_scoped?
add_missing_site_scoped_section # make sure the section is also present in the site scoped store
update_section_content!(site_scoped_store)
Expand All @@ -40,6 +43,8 @@ def update_section_content!(source)
source.sections_translations_will_change!
update_section_content(source)
source.save!

source
end

def update_section_content(source)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
<%= turbo_stream.dispatch_event "section-block-#{@section_block.id}:persisted", payload: { lockVersion: source_lock_version }.to_json %>

<%= turbo_stream.dispatch_event "section-block-#{@section_block.id}:persisted", payload: { lockVersion: @updated_store.lock_version }.to_json %>
<%= turbo_stream.update 'page-layout-notification', method: 'morph' do %>
<%= render Maglev::Uikit::BadgeComponent.new(color: :green, icon_name: 'checkbox_circle', disappear_after: 3.seconds).with_content(flash.now[:notice]) %>
<% end %>

<%= turbo_stream.update dom_id(current_maglev_page, 'topbar-page-info'), method: 'morph' do %>
<%= render 'layouts/maglev/editor/topbar/page_info' %>
<% end %>

<%= turbo_stream.update dom_id(current_maglev_page, 'topbar-publish-button'), method: 'morph' do %>
<%= render 'layouts/maglev/editor/topbar/publish_button' %>
<% end %>
<% end %>
7 changes: 2 additions & 5 deletions app/views/maglev/editor/sections/update.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
<%= turbo_stream.dispatch_event "section-#{@section.id}:persisted", payload: { lockVersion: source_lock_version }.to_json %>

<%= turbo_stream.dispatch_event "section-#{@section.id}:persisted", payload: { lockVersion: @updated_store.lock_version }.to_json %>
<%= turbo_stream.update 'page-layout-notification', method: 'morph' do %>
<%= render Maglev::Uikit::BadgeComponent.new(color: :green, icon_name: 'checkbox_circle', disappear_after: 3.seconds).with_content(flash.now[:notice]) %>
<% end %>

<%= turbo_stream.update dom_id(current_maglev_page, 'topbar-page-info'), method: 'morph' do %>
<%= render 'layouts/maglev/editor/topbar/page_info' %>
<% end %>

<%= turbo_stream.update dom_id(current_maglev_page, 'topbar-publish-button'), method: 'morph' do %>
<%= render 'layouts/maglev/editor/topbar/publish_button' %>
<% end %>
<% end %>
25 changes: 25 additions & 0 deletions spec/components/maglev/section_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,31 @@
end
end

describe '#lock_version' do
subject { component.lock_version }

context 'when the attributes carry no lock version' do
it { is_expected.to eq '0' }
end

context 'when the attributes carry the lock version of the source store' do
before { attributes[:lock_version] = 42 }

it { is_expected.to eq 42 }
end

context 'when the section is site scoped' do
# the lock version stamped on the attributes is the one of the
# site scoped store (where the content lives), not the one of
# the layout store the section is listed in
let(:definition) { build(:section, category: 'headers', site_scoped: true) }

before { attributes[:lock_version] = 7 }

it { is_expected.to eq 7 }
end
end

describe '#render' do
subject { pretty_html(component.render) }

Expand Down
10 changes: 6 additions & 4 deletions spec/factories/maglev/themes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@
site_scoped: true,
singleton: true,
insert_at: 'top',
blocks_presentation: 'tree',
settings: [{ label: 'Logo', id: 'logo', type: 'image' }],
blocks: [
{
name: 'Menu item',
type: 'menu_item',
accept: %w[menu_item],
settings: [
{ label: 'Label', id: 'label', type: 'text', default: 'Menu item' },
{ label: 'Link', id: 'link', type: 'link', default: '/' }
Expand Down Expand Up @@ -153,8 +155,8 @@
{
type: 'jumbotron',
settings: {
"title": "Let's create the product<br/>your clients<br/>will love.",
"body": '<p>NoCoffee, passionated developers,<br/>creators of web applications, mobiles apps and<br/>fancy R&D projects.</p>'
title: "Let's create the product<br/>your clients<br/>will love.",
body: '<p>NoCoffee, passionated developers,<br/>creators of web applications, mobiles apps and<br/>fancy R&D projects.</p>'
},
blocks: []
},
Expand Down Expand Up @@ -190,8 +192,8 @@
{
type: 'jumbotron',
settings: {
"title": 'About our awesome team',
"body": '<p>NoCoffee, passionated developers,<br/>creators of web applications, mobiles apps and<br/>fancy R&D projects.</p>'
title: 'About our awesome team',
body: '<p>NoCoffee, passionated developers,<br/>creators of web applications, mobiles apps and<br/>fancy R&D projects.</p>'
},
blocks: []
}
Expand Down
23 changes: 23 additions & 0 deletions spec/models/maglev/content/section_content_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,27 @@
end
end
end

context '#root_blocks' do
let(:store) { build(:sections_content_store, :with_navbar, page: page) }

it 'returns only the blocks without a parent' do
# the menu_item type is both root-eligible and accepted as a child of itself:
# nested menu items must not be returned here, otherwise the editor tree
# would render them twice (at the root and under their parent)
expect(section_content.root_blocks.map(&:id)).to eq %w[menu-item-0 menu-item-1]
end
end

context '#child_blocks_of' do
let(:store) { build(:sections_content_store, :with_navbar, page: page) }

it 'returns the direct children of the given block' do
expect(section_content.child_blocks_of('menu-item-1').map(&:id)).to eq %w[menu-item-1-1 menu-item-1-2]
end

it 'returns an empty list for a block without children' do
expect(section_content.child_blocks_of('menu-item-0')).to eq []
end
end
end
Loading
Loading