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:
@@ -196,6 +196,12 @@ type Box struct {
|
||||
// AdminOnly boxes are never sent to anyone but an admin (the boxes
|
||||
// resolver filters them out), so they suit private notes.
|
||||
AdminOnly bool `gorm:"not null;default:false" json:"adminOnly"`
|
||||
// ComponentID links an html box to a Component, whose HTML it shows
|
||||
// instead of its own: the row's HTML is nil, and the resolvers fill it in
|
||||
// from the component before answering (graph/component_helpers.go). There
|
||||
// is deliberately no association field — with one, DB.Save(&box) would
|
||||
// write the component back too.
|
||||
ComponentID *uint `gorm:"index" json:"componentId"`
|
||||
}
|
||||
|
||||
// Page is an admin-created canvas page, served by the SPA's catch-all route
|
||||
@@ -254,3 +260,26 @@ type Layout struct {
|
||||
// Background is the page's background as JSON, or nil if it had none.
|
||||
Background *string `gorm:"type:text" json:"-"`
|
||||
}
|
||||
|
||||
// Component is a named HTML box that can be placed on any canvas page, either
|
||||
// as a copy (an ordinary box) or linked (a box with ComponentID set). Linked
|
||||
// boxes share only the HTML — editing it changes every one of them at once —
|
||||
// while title, geometry, frame and visibility stay each box's own, since
|
||||
// those are about where it sits on a page rather than what it is. Title, W,
|
||||
// H, Framed and AdminOnly are the defaults a newly placed box starts with.
|
||||
// Widgets are not components: a widget box is just a name, with nothing
|
||||
// worth sharing. The unique index is partial for the same reason as Page's.
|
||||
type Component struct {
|
||||
ID uint `gorm:"primarykey" json:"id"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"`
|
||||
Name string `gorm:"not null;index:idx_components_name,unique,where:deleted_at IS NULL" json:"name"`
|
||||
Title *string `json:"title"`
|
||||
HTML string `gorm:"type:text;not null" json:"html"`
|
||||
W int `gorm:"not null" json:"w"`
|
||||
H int `gorm:"not null" json:"h"`
|
||||
// No gorm default on Framed, for the same reason as Box.Framed.
|
||||
Framed bool `gorm:"not null" json:"framed"`
|
||||
AdminOnly bool `gorm:"not null;default:false" json:"adminOnly"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user