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]>
148 lines
4.4 KiB
Go
148 lines
4.4 KiB
Go
package graph
|
|
|
|
// This file will be automatically regenerated based on the schema, any resolver
|
|
// implementations
|
|
// will be copied through when generating and any unknown code will be moved to the end.
|
|
// Code generated by github.com/99designs/gqlgen version v0.17.95
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"adam-french.co.uk/backend/graph/model"
|
|
"adam-french.co.uk/backend/models"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// BoxCount is the resolver for the boxCount field.
|
|
func (r *layoutResolver) BoxCount(ctx context.Context, obj *models.Layout) (int, error) {
|
|
// Decoded rather than stored, so it can never disagree with the boxes.
|
|
boxes, err := decodeLayoutBoxes(obj)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return len(boxes), nil
|
|
}
|
|
|
|
// HasBackground is the resolver for the hasBackground field.
|
|
func (r *layoutResolver) HasBackground(ctx context.Context, obj *models.Layout) (bool, error) {
|
|
return obj.Background != nil, nil
|
|
}
|
|
|
|
// CreateLayout is the resolver for the createLayout field.
|
|
func (r *mutationResolver) CreateLayout(ctx context.Context, input model.CreateLayoutInput) (*models.Layout, error) {
|
|
// A snapshot of the page as it is now, adminOnly boxes included —
|
|
// which is why every layout field is @admin.
|
|
name, err := cleanLayoutName(input.Name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !validPageKey(input.Page) {
|
|
return nil, errors.New("a layout needs a valid page")
|
|
}
|
|
taken, err := layoutNameTaken(r.Store.DB, name, 0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if taken {
|
|
return nil, errors.New("there is already a layout with that name")
|
|
}
|
|
layout, empty, err := snapshotPage(r.Store.DB, input.Page, name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if empty {
|
|
return nil, errors.New("that page has nothing on it to save")
|
|
}
|
|
if err := r.Store.DB.Create(&layout).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &layout, nil
|
|
}
|
|
|
|
// UpdateLayout is the resolver for the updateLayout field.
|
|
func (r *mutationResolver) UpdateLayout(ctx context.Context, id int, input model.UpdateLayoutInput) (*models.Layout, error) {
|
|
// Only the name changes: a layout is a snapshot, and saving the page
|
|
// again under a new name is how its content is refreshed.
|
|
var layout models.Layout
|
|
if err := r.Store.DB.First(&layout, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if input.Name != nil {
|
|
name, err := cleanLayoutName(*input.Name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
taken, err := layoutNameTaken(r.Store.DB, name, layout.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if taken {
|
|
return nil, errors.New("there is already a layout with that name")
|
|
}
|
|
layout.Name = name
|
|
}
|
|
if err := r.Store.DB.Save(&layout).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &layout, nil
|
|
}
|
|
|
|
// DeleteLayout is the resolver for the deleteLayout field.
|
|
func (r *mutationResolver) DeleteLayout(ctx context.Context, id int) (*models.Layout, error) {
|
|
// Pages it was loaded onto keep their boxes: they were copies.
|
|
var layout models.Layout
|
|
if err := r.Store.DB.First(&layout, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if err := r.Store.DB.Delete(&layout).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &layout, nil
|
|
}
|
|
|
|
// ApplyLayout is the resolver for the applyLayout field.
|
|
func (r *mutationResolver) ApplyLayout(ctx context.Context, id int, page string, replace bool) ([]*models.Box, error) {
|
|
// One transaction, so a replace that fails partway leaves the page as
|
|
// it was rather than half emptied.
|
|
var layout models.Layout
|
|
if err := r.Store.DB.First(&layout, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
exists, err := canvasExists(r.Store.DB, page)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !exists {
|
|
return nil, errors.New("there is no page at that address")
|
|
}
|
|
err = r.Store.DB.Transaction(func(tx *gorm.DB) error {
|
|
return applyLayoutTo(tx, &layout, page, replace)
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
boxes, err := pageBoxes(r.Store.DB, page)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := fillLinked(r.Store.DB, boxes); err != nil {
|
|
return nil, err
|
|
}
|
|
return ptrs(boxes), nil
|
|
}
|
|
|
|
// Layouts is the resolver for the layouts field.
|
|
func (r *queryResolver) Layouts(ctx context.Context) ([]*models.Layout, error) {
|
|
var layouts []models.Layout
|
|
if err := r.Store.DB.Order("created_at DESC").Find(&layouts).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return ptrs(layouts), nil
|
|
}
|
|
|
|
// Layout returns LayoutResolver implementation.
|
|
func (r *Resolver) Layout() LayoutResolver { return &layoutResolver{r} }
|
|
|
|
type layoutResolver struct{ *Resolver }
|