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]>
163 lines
4.7 KiB
Go
163 lines
4.7 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"
|
|
)
|
|
|
|
// CreateBox is the resolver for the createBox field.
|
|
func (r *mutationResolver) CreateBox(ctx context.Context, input model.CreateBoxInput) (*models.Box, error) {
|
|
// Framed defaults to true here rather than in the gorm tag — see the
|
|
// comment on models.Box.Framed. Sizes not supplied fall back to the
|
|
// same 300x200 the column defaults use.
|
|
box := models.Box{
|
|
Page: input.Page,
|
|
Kind: input.Kind,
|
|
Widget: input.Widget,
|
|
HTML: input.HTML,
|
|
Title: input.Title,
|
|
W: 300,
|
|
H: 200,
|
|
Framed: true,
|
|
}
|
|
setInt(&box.X, input.X)
|
|
setInt(&box.Y, input.Y)
|
|
setInt(&box.W, input.W)
|
|
setInt(&box.H, input.H)
|
|
setInt(&box.Z, input.Z)
|
|
if input.Framed != nil {
|
|
box.Framed = *input.Framed
|
|
}
|
|
if input.HideOnMobile != nil {
|
|
box.HideOnMobile = *input.HideOnMobile
|
|
}
|
|
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
|
|
}
|
|
|
|
// UpdateBox is the resolver for the updateBox field.
|
|
func (r *mutationResolver) UpdateBox(ctx context.Context, id int, input model.UpdateBoxInput) (*models.Box, error) {
|
|
// Partial update: nil fields are left alone. The canvas sends only the
|
|
// geometry after a drag and only the html after an edit, so this is
|
|
// what keeps the two from overwriting each other. Kind is not
|
|
// updatable — switching a widget box to html is a delete and create.
|
|
var box models.Box
|
|
if err := r.Store.DB.First(&box, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if input.Widget != nil {
|
|
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 {
|
|
box.Title = input.Title
|
|
}
|
|
setInt(&box.X, input.X)
|
|
setInt(&box.Y, input.Y)
|
|
setInt(&box.W, input.W)
|
|
setInt(&box.H, input.H)
|
|
setInt(&box.Z, input.Z)
|
|
if input.Framed != nil {
|
|
box.Framed = *input.Framed
|
|
}
|
|
if input.HideOnMobile != nil {
|
|
box.HideOnMobile = *input.HideOnMobile
|
|
}
|
|
if input.AdminOnly != nil {
|
|
box.AdminOnly = *input.AdminOnly
|
|
}
|
|
if err := validateBox(&box); err != nil {
|
|
return nil, err
|
|
}
|
|
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
|
|
}
|
|
|
|
// DeleteBox is the resolver for the deleteBox field.
|
|
func (r *mutationResolver) DeleteBox(ctx context.Context, id int) (*models.Box, error) {
|
|
var box models.Box
|
|
if err := r.Store.DB.First(&box, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
if err := r.Store.DB.Delete(&box).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &box, nil
|
|
}
|
|
|
|
// Boxes is the resolver for the boxes field.
|
|
func (r *queryResolver) Boxes(ctx context.Context, page string) ([]*models.Box, error) {
|
|
// Public: this is the page layout every visitor loads. Ordered by z so
|
|
// DOM order matches stacking order, which keeps tab order sensible.
|
|
//
|
|
// What is admin-only is filtered here rather than hidden in the browser,
|
|
// so a private note never reaches anyone else: an adminOnly page answers
|
|
// with no boxes at all, and adminOnly boxes are left out of the rest.
|
|
hidden, err := pageHiddenFrom(ctx, r.Store.DB, page)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if hidden {
|
|
return []*models.Box{}, nil
|
|
}
|
|
query := r.Store.DB.Where("page = ?", page)
|
|
if !IsAdminFromCtx(ctx) {
|
|
query = query.Where("admin_only = ?", false)
|
|
}
|
|
var boxes []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
|
|
}
|