Skip to content

Commit

Permalink
feat(server): add padding properties to widget align system area (#381)
Browse files Browse the repository at this point in the history
  • Loading branch information
mimoham24 committed Feb 2, 2023
1 parent 8c1d82e commit d5dbcf0
Show file tree
Hide file tree
Showing 12 changed files with 993 additions and 50 deletions.
656 changes: 653 additions & 3 deletions server/internal/adapter/gql/generated.go

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions server/internal/adapter/gql/gqlmodel/convert_scene_align.go
Expand Up @@ -73,6 +73,19 @@ func FromSceneWidgetLocation(l *WidgetLocationInput) *scene.WidgetLocation {
}
}

func FromSceneWidgetAreaPadding(p *WidgetAreaPaddingInput) *scene.WidgetAreaPadding {
if p == nil {
return nil
}
pp := scene.NewWidgetAreaPadding(
p.Left,
p.Right,
p.Top,
p.Bottom,
)
return pp
}

func FromSceneWidgetZoneType(t WidgetZoneType) scene.WidgetZoneType {
switch t {
case WidgetZoneTypeInner:
Expand Down
21 changes: 21 additions & 0 deletions server/internal/adapter/gql/gqlmodel/convert_scene_align_test.go
@@ -0,0 +1,21 @@
package gqlmodel

import (
"testing"

"github.com/reearth/reearth/server/pkg/scene"
"github.com/stretchr/testify/assert"
)

func TestFromSceneWidgetAreaPadding(t *testing.T) {
got := FromSceneWidgetAreaPadding(&WidgetAreaPaddingInput{
Top: 2,
Bottom: 2,
Left: 2,
Right: 2,
})
want := scene.NewWidgetAreaPadding(2, 2, 2, 2)
assert.Equal(t, want, got)
var want2 *scene.WidgetAreaPadding
assert.Equal(t, want2, FromSceneWidgetAreaPadding(nil))
}
32 changes: 27 additions & 5 deletions server/internal/adapter/gql/gqlmodel/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions server/internal/adapter/gql/resolver_mutation_scene.go
Expand Up @@ -107,9 +107,13 @@ func (r *mutationResolver) UpdateWidgetAlignSystem(ctx context.Context, input gq
}

scene, err := usecases(ctx).Scene.UpdateWidgetAlignSystem(ctx, interfaces.UpdateWidgetAlignSystemParam{
SceneID: sid,
Location: *gqlmodel.FromSceneWidgetLocation(input.Location),
Align: gqlmodel.FromWidgetAlignType(input.Align),
SceneID: sid,
Location: *gqlmodel.FromSceneWidgetLocation(input.Location),
Align: gqlmodel.FromWidgetAlignType(input.Align),
Padding: gqlmodel.FromSceneWidgetAreaPadding(input.Padding),
Gap: input.Gap,
Centered: input.Centered,
Background: input.Background,
}, getOperator(ctx))
if err != nil {
return nil, err
Expand Down
29 changes: 28 additions & 1 deletion server/internal/infrastructure/mongo/mongodoc/scene_align.go
Expand Up @@ -24,6 +24,12 @@ type WidgetSectionDocument struct {
type WidgetAreaDocument struct {
WidgetIDs []string
Align string
Padding struct {
Top, Bottom, Left, Right int
}
Gap int
Centered bool
Background *string
}

func NewWidgetAlignSystem(was *scene.WidgetAlignSystem) *WidgetAlignSystemDocument {
Expand Down Expand Up @@ -84,6 +90,17 @@ func NewWidgetArea(a *scene.WidgetArea) *WidgetAreaDocument {
return &WidgetAreaDocument{
WidgetIDs: a.WidgetIDs().Strings(),
Align: string(a.Alignment()),
Padding: struct {
Top, Bottom, Left, Right int
}{
Top: a.Padding().Top(),
Bottom: a.Padding().Bottom(),
Left: a.Padding().Left(),
Right: a.Padding().Right(),
},
Gap: a.Gap(),
Centered: a.Centered(),
Background: a.Background(),
}
}

Expand Down Expand Up @@ -127,7 +144,17 @@ func (a *WidgetAreaDocument) Model() *scene.WidgetArea {
return nil
}

return scene.NewWidgetArea(stringsToWidgetIDs(a.WidgetIDs), scene.WidgetAlignType(a.Align))
return scene.NewWidgetArea(stringsToWidgetIDs(a.WidgetIDs), scene.WidgetAlignType(a.Align),
scene.NewWidgetAreaPadding(
a.Padding.Left,
a.Padding.Right,
a.Padding.Top,
a.Padding.Bottom,
),
a.Gap,
a.Centered,
a.Background,
)
}

func stringsToWidgetIDs(wids []string) []id.WidgetID {
Expand Down
21 changes: 16 additions & 5 deletions server/internal/usecase/interactor/scene.go
Expand Up @@ -331,15 +331,15 @@ func (i *Scene) UpdateWidgetAlignSystem(ctx context.Context, param interfaces.Up
}
}()

scene, err2 := i.sceneRepo.FindByID(ctx, param.SceneID)
s, err2 := i.sceneRepo.FindByID(ctx, param.SceneID)
if err2 != nil {
return nil, err2
}
if err := i.CanWriteWorkspace(scene.Workspace(), operator); err != nil {
if err := i.CanWriteWorkspace(s.Workspace(), operator); err != nil {
return nil, err
}

area := scene.Widgets().Alignment().Area(param.Location)
area := s.Widgets().Alignment().Area(param.Location)

if area == nil {
return nil, errors.New("invalid location")
Expand All @@ -349,12 +349,23 @@ func (i *Scene) UpdateWidgetAlignSystem(ctx context.Context, param interfaces.Up
area.SetAlignment(*param.Align)
}

if err = i.sceneRepo.Save(ctx, scene); err != nil {
if param.Padding != nil {
area.SetPadding(param.Padding)
}
if param.Gap != nil {
area.SetGap(*param.Gap)
}
if param.Centered != nil {
area.SetCentered(*param.Centered)
}
area.SetBackground(param.Background)

if err = i.sceneRepo.Save(ctx, s); err != nil {
return nil, err
}

tx.Commit()
return scene, nil
return s, nil
}

func (i *Scene) RemoveWidget(ctx context.Context, id id.SceneID, wid id.WidgetID, operator *usecase.Operator) (_ *scene.Scene, err error) {
Expand Down
10 changes: 7 additions & 3 deletions server/internal/usecase/interfaces/scene.go
Expand Up @@ -42,9 +42,13 @@ type UpdateWidgetParam struct {
}

type UpdateWidgetAlignSystemParam struct {
SceneID id.SceneID
Location scene.WidgetLocation
Align *scene.WidgetAlignType
SceneID id.SceneID
Location scene.WidgetLocation
Align *scene.WidgetAlignType
Padding *scene.WidgetAreaPadding
Gap *int
Centered *bool
Background *string
}

type UpdateClusterParam struct {
Expand Down
97 changes: 91 additions & 6 deletions server/pkg/scene/widget_area.go
@@ -1,13 +1,15 @@
package scene

import (
"github.com/samber/lo"
)
import "github.com/samber/lo"

// WidgetArea has the widgets and alignment information found in each part area of a section.
type WidgetArea struct {
widgetIds WidgetIDList
align WidgetAlignType
widgetIds WidgetIDList
align WidgetAlignType
padding *WidgetAreaPadding
gap int
centered bool
background *string
}

type WidgetAlignType string
Expand All @@ -18,10 +20,15 @@ const (
WidgetAlignEnd WidgetAlignType = "end"
)

func NewWidgetArea(widgetIds []WidgetID, align WidgetAlignType) *WidgetArea {
func NewWidgetArea(widgetIds []WidgetID, align WidgetAlignType, padding *WidgetAreaPadding, gap int, centered bool, background *string) *WidgetArea {
wa := &WidgetArea{}
wa.AddAll(widgetIds)
wa.SetAlignment(align)
wa.SetPadding(padding)
wa.SetGap(gap)
wa.SetCentered(centered)
wa.SetBackground(background)

return wa
}

Expand All @@ -43,6 +50,38 @@ func (a *WidgetArea) Alignment() WidgetAlignType {
return a.align
}

func (a *WidgetArea) Padding() *WidgetAreaPadding {
if a == nil {
return nil
}

return a.padding
}

func (a *WidgetArea) Gap() int {
if a == nil {
return 0
}

return a.gap
}

func (a *WidgetArea) Centered() bool {
if a == nil {
return false
}

return a.centered
}

func (a *WidgetArea) Background() *string {
if a == nil {
return nil
}

return a.background
}

func (a *WidgetArea) Find(wid WidgetID) int {
if a == nil {
return -1
Expand Down Expand Up @@ -84,6 +123,26 @@ func (a *WidgetArea) SetAlignment(at WidgetAlignType) {
}
}

func (a *WidgetArea) SetPadding(ap *WidgetAreaPadding) {
if a == nil || ap == nil {
return
}

a.padding = ap
}

func (a *WidgetArea) SetGap(g int) {
a.gap = g
}

func (a *WidgetArea) SetCentered(c bool) {
a.centered = c
}

func (a *WidgetArea) SetBackground(bg *string) {
a.background = bg
}

func (a *WidgetArea) Remove(wid WidgetID) {
if a == nil {
return
Expand All @@ -105,3 +164,29 @@ func (a *WidgetArea) Move(from, to int) {
wid := a.widgetIds[from]
a.widgetIds = a.widgetIds.DeleteAt(from).Insert(to, wid)
}

type WidgetAreaPadding struct {
top, bottom, left, right int
}

func NewWidgetAreaPadding(l, r, t, b int) *WidgetAreaPadding {
return &WidgetAreaPadding{
top: t,
bottom: b,
left: l,
right: r,
}
}

func (p WidgetAreaPadding) Top() int {
return p.top
}
func (p WidgetAreaPadding) Bottom() int {
return p.bottom
}
func (p WidgetAreaPadding) Left() int {
return p.left
}
func (p WidgetAreaPadding) Right() int {
return p.right
}

0 comments on commit d5dbcf0

Please sign in to comment.