A box can now cast a shadow, independent of its frame: ticking "Shadow" in the box editor gives a hard offset shadow, and Hard/Soft/Glow presets or any typed CSS box-shadow value customise it. The value is stored on Box and Component and carried through duplicates, components and layouts. Co-Authored-By: Claude Opus 5.5 <[email protected]>
169 lines
4.8 KiB
Go
169 lines
4.8 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.Shadow != nil {
|
|
box.Shadow = *input.Shadow
|
|
}
|
|
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.Shadow != nil {
|
|
box.Shadow = *input.Shadow
|
|
}
|
|
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
|
|
}
|