Skip to content

Commit

Permalink
document: support for adding rows before/after
Browse files Browse the repository at this point in the history
This makes editing document templates easier as you can insert
rows mid-table.
  • Loading branch information
tbaliance committed Jul 27, 2018
1 parent 3e25a72 commit acf3430
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions document/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,41 @@ func (t Table) AddRow() Row {
return Row{t.d, tr}
}

// InsertRowAfter inserts a row after another row
func (t Table) InsertRowAfter(r Row) Row {
for i, rc := range t.x.EG_ContentRowContent {
if len(rc.Tr) > 0 && r.X() == rc.Tr[0] {
c := wml.NewEG_ContentRowContent()
if len(t.x.EG_ContentRowContent) <= i+2 {
return t.AddRow()
}
t.x.EG_ContentRowContent = append(t.x.EG_ContentRowContent, nil)
copy(t.x.EG_ContentRowContent[i+2:], t.x.EG_ContentRowContent[i+1:])
t.x.EG_ContentRowContent[i+1] = c
tr := wml.NewCT_Row()
c.Tr = append(c.Tr, tr)
return Row{t.d, tr}
}
}
return t.AddRow()
}

// InsertRowBefore inserts a row before another row
func (t Table) InsertRowBefore(r Row) Row {
for i, rc := range t.x.EG_ContentRowContent {
if len(rc.Tr) > 0 && r.X() == rc.Tr[0] {
c := wml.NewEG_ContentRowContent()
t.x.EG_ContentRowContent = append(t.x.EG_ContentRowContent, nil)
copy(t.x.EG_ContentRowContent[i+1:], t.x.EG_ContentRowContent[i:])
t.x.EG_ContentRowContent[i] = c
tr := wml.NewCT_Row()
c.Tr = append(c.Tr, tr)
return Row{t.d, tr}
}
}
return t.AddRow()
}

// Rows returns the rows defined in the table.
func (t Table) Rows() []Row {
ret := []Row{}
Expand Down

0 comments on commit acf3430

Please sign in to comment.