The canvas is centred, so on wide screens there is margin to its left that boxes could not be dragged into: x was clamped to 0 in the drag geometry, the arrow-key nudge and validateBox. Only y stays clamped. Co-Authored-By: Claude Opus 5.5 <[email protected]>
65 lines
1.7 KiB
Go
65 lines
1.7 KiB
Go
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.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
|
|
}
|