moving sections up and down

This commit is contained in:
2022-12-01 20:14:34 +02:00
parent a4af2d6872
commit 4f6538e135
8 changed files with 51 additions and 40 deletions

View File

@ -38,11 +38,30 @@ module Merged
@content[0]["template"]
end
def new_section
section = Hash.new
section['id'] = SecureRandom.hex(10)
@content << section
Section.new(self , 0 , section)
def move_section_up(section)
return if sections.length == 1
return if section.index == 0
swap( section , sections[section.index - 1])
end
def move_section_down(section)
return if sections.length == 1
return if section.index == sections.last.index
swap( section , sections[section.index + 1])
end
def swap( this_section , that_section)
# swap in the actual objects, index is cached in the objects
this_old_index = this_section.index
this_section.set_index( that_section.index )
that_section.set_index( this_old_index )
# swap in the sections cache
sections[ this_section.index ] = this_section
sections[ that_section.index ] = that_section
# swap in the yaml
content[this_section.index] = this_section.content
content[that_section.index] = that_section.content
end
def save
@ -55,7 +74,6 @@ module Merged
page = @@all[name]
raise "Page not found #{name}" unless page
return page
end
end

View File

@ -39,7 +39,7 @@ module Merged
@page.move_section_up(self)
end
def move_down
@section.move_section_down(self)
@page.move_section_down(self)
end
def move_card_up(card)
@ -83,6 +83,10 @@ module Merged
page.save
end
def set_index(index)
@index = index
end
def self.find(section_id)
raise "nil given" if section_id.blank?
section = @@all[section_id]