Add saved layouts, and make /stp an ordinary canvas page
A layout is a named copy of one canvas page's boxes and background (models.Layout, layout.graphql), saved from the canvas's new Layouts button and loaded onto any page from there or from the admin Pages tab. Loading onto a page with boxes asks whether to replace them or add on top; a replace first saves the page as a "<page> before <layout>" layout so it can be undone. Copies, not links: editing a page never changes a layout or another page. The copying is server-side in one transaction, and everything is @admin since a layout keeps admin-only boxes. /stp loses its static route and becomes a Page with slug "stp", created once at start-up (ensureStpPage), so it can be renamed, hidden or deleted like a shrine. Its boxes were already keyed "stp". Home3.vue is kept as legacy; its starter is now "Old /stp grid" in pageStarters.js. ConfirmDialog gains an optional second button. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
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
|
||||
}
|
||||
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 }
|
||||
Reference in New Issue
Block a user