package graph // Helpers live outside *.resolvers.go because gqlgen moves unknown code there. import ( "errors" "fmt" "strings" "adam-french.co.uk/backend/models" ) // minBoxSize stops a box being resized to nothing, after which it could not // be grabbed again on the canvas. const minBoxSize = 40 // maxShadowLen bounds a box's shadow, which is a single CSS value: even a // stack of a few comma-separated shadows is well under it. const maxShadowLen = 200 func setInt(dst *int, src *int) { if src != nil { *dst = *src } } // validateBox clamps geometry, so an overshooting drag lands at the edge. func validateBox(box *models.Box) error { if !validPageKey(box.Page) { return errors.New("a box needs a valid page") } switch box.Kind { case "widget": if box.Widget == nil || *box.Widget == "" { return errors.New("a widget box needs a widget name") } case "html": // A linked box's html lives on its component (fillLinked). if box.HTML == nil && box.ComponentID == nil { return errors.New("an html box needs html") } default: return errors.New(`box kind must be "widget" or "html"`) } if box.ComponentID != nil && box.Kind != "html" { return errors.New("only an html box can be linked to a component") } if err := validateShadow(&box.Shadow); err != nil { return err } box.X = max(box.X, 0) box.Y = max(box.Y, 0) box.W = max(box.W, minBoxSize) box.H = max(box.H, minBoxSize) return nil } // validateShadow doesn't parse: a style binding confines it to one property. func validateShadow(shadow *string) error { *shadow = strings.TrimSpace(*shadow) if len(*shadow) > maxShadowLen { return fmt.Errorf("a shadow is at most %d characters", maxShadowLen) } return nil }