A component (models.Component, component.graphql) is a named HTML box. "+ New box" now lists components and places a copy, as it did with the hard-coded starters; the canvas's new Components button (ComponentPanel) can also place one linked. A linked box has Box.componentId, its row's html is nil, and the resolvers fill in the component's html before answering (fillLinked), so saving the html in the box editor saves the component and every linked box on every page changes. Only the html is shared: title, geometry, frame and visibility stay each box's own. The box editor gains "Save as component" (makes one from an html box and links it) and, on a linked box, a banner with Detach (gives the box its own copy). updateBox refuses html on a linked box. Deleting a component detaches its boxes first, so no page changes. Layouts store the link and the html; loading relinks only if the component still exists. The built-in starters in templates.js are no longer listed directly: "Import built-ins" in the panel creates them as components (skipping names already taken), so they can be edited and deleted like the rest. The html helpers there are unchanged. Everything is @admin. Co-Authored-By: Claude Opus 5.5 <[email protected]>
48 lines
1.8 KiB
Go
48 lines
1.8 KiB
Go
package graph
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"adam-french.co.uk/backend/models"
|
|
)
|
|
|
|
func TestValidateBox(t *testing.T) {
|
|
str := func(s string) *string { return &s }
|
|
one := uint(1)
|
|
|
|
for _, c := range []struct {
|
|
name string
|
|
box models.Box
|
|
wantErr bool
|
|
}{
|
|
{"widget with a name", models.Box{Page: "stp", Kind: "widget", Widget: str("Chat")}, false},
|
|
{"widget without a name", models.Box{Page: "stp", Kind: "widget"}, true},
|
|
{"widget with an empty name", models.Box{Page: "stp", Kind: "widget", Widget: str("")}, true},
|
|
{"html with html", models.Box{Page: "stp", Kind: "html", HTML: str("<p>hi</p>")}, false},
|
|
{"html with empty html", models.Box{Page: "stp", Kind: "html", HTML: str("")}, false},
|
|
{"html without html", models.Box{Page: "stp", Kind: "html"}, true},
|
|
{"linked html without html", models.Box{Page: "stp", Kind: "html", ComponentID: &one}, false},
|
|
{"linked widget", models.Box{Page: "stp", Kind: "widget", Widget: str("Chat"), ComponentID: &one}, true},
|
|
{"no page", models.Box{Kind: "widget", Widget: str("Chat")}, true},
|
|
{"bad page", models.Box{Page: "Shrines/GTO", Kind: "widget", Widget: str("Chat")}, true},
|
|
{"unknown kind", models.Box{Page: "stp", Kind: "iframe", HTML: str("x")}, true},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
err := validateBox(&c.box)
|
|
if (err != nil) != c.wantErr {
|
|
t.Fatalf("err = %v, wantErr %v", err, c.wantErr)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Geometry is clamped, not rejected: a drag past the canvas edge lands
|
|
// at the edge, and a box can never shrink below a grabbable size.
|
|
box := models.Box{Page: "stp", Kind: "widget", Widget: str("Chat"), X: -30, Y: -1, W: 5, H: 0}
|
|
if err := validateBox(&box); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if box.X != 0 || box.Y != 0 || box.W != minBoxSize || box.H != minBoxSize {
|
|
t.Fatalf("clamped to %d,%d %dx%d; want 0,0 %dx%d", box.X, box.Y, box.W, box.H, minBoxSize, minBoxSize)
|
|
}
|
|
}
|