Add saved components: reusable HTML boxes, placed as copies or linked
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]>
This commit is contained in:
@@ -7,6 +7,7 @@ package graph
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"adam-french.co.uk/backend/graph/model"
|
||||
"adam-french.co.uk/backend/models"
|
||||
@@ -41,12 +42,31 @@ func (r *mutationResolver) CreateBox(ctx context.Context, input model.CreateBoxI
|
||||
if input.AdminOnly != nil {
|
||||
box.AdminOnly = *input.AdminOnly
|
||||
}
|
||||
// A linked box takes its html from the component, so its own is
|
||||
// dropped rather than stored as a copy that would quietly go stale.
|
||||
if input.ComponentID != nil {
|
||||
id := uint(*input.ComponentID)
|
||||
exists, err := componentExists(r.Store.DB, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exists {
|
||||
return nil, errors.New("there is no such component")
|
||||
}
|
||||
box.Kind = "html"
|
||||
box.Widget = nil
|
||||
box.HTML = nil
|
||||
box.ComponentID = &id
|
||||
}
|
||||
if err := validateBox(&box); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := r.Store.DB.Create(&box).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := fillLinkedOne(r.Store.DB, &box); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &box, nil
|
||||
}
|
||||
|
||||
@@ -64,6 +84,11 @@ func (r *mutationResolver) UpdateBox(ctx context.Context, id int, input model.Up
|
||||
box.Widget = input.Widget
|
||||
}
|
||||
if input.HTML != nil {
|
||||
// A linked box's html is the component's; changing it here would
|
||||
// only fork a copy. The editor sends it to updateComponent instead.
|
||||
if box.ComponentID != nil {
|
||||
return nil, errors.New("that box is linked to a component: edit the component, or detach the box first")
|
||||
}
|
||||
box.HTML = input.HTML
|
||||
}
|
||||
if input.Title != nil {
|
||||
@@ -89,6 +114,9 @@ func (r *mutationResolver) UpdateBox(ctx context.Context, id int, input model.Up
|
||||
if err := r.Store.DB.Save(&box).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := fillLinkedOne(r.Store.DB, &box); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &box, nil
|
||||
}
|
||||
|
||||
@@ -127,5 +155,8 @@ func (r *queryResolver) Boxes(ctx context.Context, page string) ([]*models.Box,
|
||||
if err := query.Order("z ASC, id ASC").Find(&boxes).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := fillLinked(r.Store.DB, boxes); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ptrs(boxes), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user