feat(studio): groups, a layers panel, marquee, nudge, multi-resize, align and paste style

Elements can be grouped on a page and picked, moved, resized, aligned and
restacked as one. A layers panel lists the page front to back with lock,
visibility, rename and drag to restack. The sheet gains marquee selection,
arrow-key nudge, one box that resizes a multi-selection with Shift holding
the proportions, and the design-tool shortcuts. The inspector lines a
selection up and spaces it out, and copies and pastes a style.

The assistant gets the same through the group, ungroup, name and hide
operations, and describe_book_page lists the groups. The format gains the
optional group, name and hidden fields and a spread's group names, so
BOOK_SCHEMA_REV goes to 2.
This commit is contained in:
Maurice
2026-10-23 19:31:14 +02:00
parent 8c0707f34c
commit d8213bfa42
102 changed files with 5374 additions and 259 deletions
+146
View File
@@ -0,0 +1,146 @@
import {
alignElements,
arrangeElements,
arrangeWithinGroup,
boundsOfFrames,
distributableCount,
distributeElements,
} from './book-arrange';
import {
bookPageSetupSchema,
bookShapeElementSchema,
bookSpreadSchema,
type BookElement,
type BookSpread,
} from './book.schema';
import { describe, expect, it } from 'vitest';
/**
* Paint order, alignment and spacing, with groups moving as one. The page is
* the default square book: 210 mm pages, the fold at 210, 5 mm safe margin.
*/
const pageSetup = bookPageSetupSchema.parse({});
const box = (id: string, x: number, y: number, w = 20, h = 20, over: Partial<BookElement> = {}) =>
bookShapeElementSchema.parse({ id, kind: 'shape', frame: { x, y, w, h }, shape: 'rect', ...over }) as BookElement;
const spread = (elements: BookElement[]): BookSpread => bookSpreadSchema.parse({ id: 'sp', elements });
const ids = (sp: BookSpread) => sp.elements.map((e) => e.id);
const at = (sp: BookSpread, id: string) => sp.elements.find((e) => e.id === id)?.frame;
const pick = (sp: BookSpread, list: string[]) => sp.elements.filter((e) => list.includes(e.id));
describe('paint order', () => {
const sp = spread([
box('a', 0, 0),
box('g1', 0, 0, 20, 20, { group: 'g' }),
box('g2', 0, 0, 20, 20, { group: 'g' }),
box('b', 0, 0),
]);
it('BKARR-001: to front and to back keep the moved ones in their order, and a group together', () => {
expect(ids(arrangeElements(sp, ['a'], 'front'))).toEqual(['g1', 'g2', 'b', 'a']);
expect(ids(arrangeElements(sp, ['b'], 'back'))).toEqual(['b', 'a', 'g1', 'g2']);
// One member moved to the front takes its group along.
expect(ids(arrangeElements(sp, ['g1'], 'front'))).toEqual(['a', 'b', 'g1', 'g2']);
});
it('BKARR-002: forward and backward step one place', () => {
// A step goes past a whole group, never into it.
expect(ids(arrangeElements(sp, ['a'], 'forward'))).toEqual(['g1', 'g2', 'a', 'b']);
expect(ids(arrangeElements(sp, ['g2'], 'backward'))).toEqual(['g1', 'g2', 'a', 'b']);
expect(arrangeElements(sp, ['b'], 'front')).toBe(sp);
expect(ids(arrangeElements(spread([box('a', 0, 0), box('b', 0, 0), box('c', 0, 0)]), ['a'], 'forward'))).toEqual([
'b',
'a',
'c',
]);
expect(ids(arrangeElements(spread([box('a', 0, 0), box('b', 0, 0), box('c', 0, 0)]), ['c'], 'backward'))).toEqual([
'a',
'c',
'b',
]);
});
it('BKARR-003: inside a group, a member moves among the members only', () => {
const grouped = spread([
box('a', 0, 0),
box('m1', 0, 0, 20, 20, { group: 'g' }),
box('m2', 0, 0, 20, 20, { group: 'g' }),
box('m3', 0, 0, 20, 20, { group: 'g' }),
box('b', 0, 0),
]);
expect(ids(arrangeWithinGroup(grouped, 'g', ['m1'], 'front'))).toEqual(['a', 'm2', 'm3', 'm1', 'b']);
expect(ids(arrangeWithinGroup(grouped, 'g', ['m3'], 'back'))).toEqual(['a', 'm3', 'm1', 'm2', 'b']);
expect(arrangeWithinGroup(grouped, 'g', ['m3'], 'front')).toBe(grouped);
expect(arrangeWithinGroup(grouped, 'g', ['m3'], 'front').elements[3]?.group).toBe('g');
});
});
describe('align and distribute', () => {
it('BKARR-004: lines single elements up on the edge of their box', () => {
const sp = spread([box('a', 10, 10), box('b', 40, 30)]);
const left = alignElements(sp, pageSetup, sp.elements, 'left', 'selection');
expect([at(left, 'a')?.x, at(left, 'b')?.x]).toEqual([10, 10]);
const bottom = alignElements(sp, pageSetup, sp.elements, 'bottom', 'selection');
expect([at(bottom, 'a')?.y, at(bottom, 'b')?.y]).toEqual([30, 30]);
// The other axis is left exactly as it was.
expect(at(left, 'b')?.y).toBe(30);
});
it('BKARR-005: moves a group as one box, and lines a group up inside itself when it is all that is picked', () => {
const sp = spread([
box('a', 100, 10),
box('g1', 10, 50, 20, 20, { group: 'g' }),
box('g2', 40, 60, 20, 20, { group: 'g' }),
]);
const right = alignElements(sp, pageSetup, sp.elements, 'right', 'selection');
// The group's box (10..60) goes to the right edge 120 as one: 60 to the right.
expect([at(right, 'g1')?.x, at(right, 'g2')?.x, at(right, 'a')?.x]).toEqual([70, 100, 100]);
const inside = alignElements(sp, pageSetup, pick(sp, ['g1', 'g2']), 'top', 'selection');
expect([at(inside, 'g1')?.y, at(inside, 'g2')?.y]).toEqual([50, 50]);
});
it('BKARR-006: aligns against the page under an element, and its safe area', () => {
const sp = spread([box('r', pageSetup.pageWidth + 90, 10)]);
const centred = alignElements(sp, pageSetup, sp.elements, 'center', 'page');
expect(at(centred, 'r')?.x).toBe(pageSetup.pageWidth + (pageSetup.pageWidth - 20) / 2);
const safe = alignElements(sp, pageSetup, sp.elements, 'top', 'safe');
expect(at(safe, 'r')?.y).toBe(pageSetup.safe);
const middle = alignElements(sp, pageSetup, sp.elements, 'middle', 'page');
expect(at(middle, 'r')?.y).toBe((pageSetup.pageHeight - 20) / 2);
});
it('BKARR-007: spaces units evenly, a group counted once, or by a fixed gap', () => {
const sp = spread([
box('a', 0, 0),
box('g1', 30, 0, 10, 10, { group: 'g' }),
box('g2', 45, 0, 10, 10, { group: 'g' }),
box('c', 100, 0),
]);
expect(distributableCount(sp, sp.elements)).toBe(3);
const even = distributeElements(sp, sp.elements, 'horizontal');
// Span 0..120, sizes 20 + 25 + 20, so two gaps of 27.5; the group moves as one.
expect([at(even, 'g1')?.x, at(even, 'g2')?.x, at(even, 'c')?.x]).toEqual([47.5, 62.5, 100]);
const fixed = distributeElements(sp, sp.elements, 'horizontal', 5);
expect([at(fixed, 'g1')?.x, at(fixed, 'c')?.x]).toEqual([25, 55]);
const down = distributeElements(
spread([box('a', 0, 0), box('b', 0, 50), box('c', 0, 30)]),
spread([box('a', 0, 0), box('b', 0, 50), box('c', 0, 30)]).elements,
'vertical',
);
expect(at(down, 'c')?.y).toBe(25);
});
it('BKARR-008: leaves a page with fewer than three units as it is, and measures a box', () => {
const sp = spread([box('a', 0, 0), box('b', 50, 0)]);
expect(distributeElements(sp, sp.elements, 'horizontal')).toBe(sp);
expect(
boundsOfFrames([
{ x: 1, y: 2, w: 3, h: 4 },
{ x: 10, y: 0, w: 1, h: 1 },
]),
).toEqual({ x: 1, y: 0, w: 10, h: 6 });
});
});
+218
View File
@@ -0,0 +1,218 @@
import { commonGroup, gatherGroups } from './book-groups';
import type { BookElement, BookFrame, BookPageSetup, BookSpread } from './book.schema';
/**
* Paint order, alignment and spacing of the elements of a page.
*
* The `arrange`, `align` and `distribute` operations of the book engine run
* these (book-ops-elements.ts), and the Studio's layer buttons, align bar and
* layers panel run the same engine, so a press and an assistant's operation
* move a page the same way.
*
* Groups move as one: aligning two groups lines up their boxes, not every
* member on the same edge, and distributing spaces the boxes. When every named
* element is in the one group, the members line up among themselves, which is
* how the inside of a lockup is set.
*/
export type BookArrangeTo = 'front' | 'forward' | 'backward' | 'back';
export type BookAlignEdge = 'left' | 'center' | 'right' | 'top' | 'middle' | 'bottom';
export type BookAlignTo = 'selection' | 'page' | 'safe';
export type BookDistributeAxis = 'horizontal' | 'vertical';
const round2 = (v: number) => Math.round(v * 100) / 100;
/**
* The page in paint order as the things that move as one: a group's members
* together (they are next to each other, see `gatherGroups`), every other
* element alone.
*/
function paintUnits(spread: BookSpread): BookElement[][] {
const units: BookElement[][] = [];
for (const el of gatherGroups(spread).elements) {
const last = units[units.length - 1];
if (el.group && last?.[0]?.group === el.group) last.push(el);
else units.push([el]);
}
return units;
}
/**
* The named elements moved in the paint order, keeping their order among
* themselves. A group moves whole, and a step forward or backward goes past a
* whole group at once, never into it.
*/
export function arrangeElements(spread: BookSpread, ids: readonly string[], to: BookArrangeTo): BookSpread {
const chosen = new Set(ids);
const units = paintUnits(spread);
const isChosen = (u: BookElement[]) => u.some((e) => chosen.has(e.id));
let next: BookElement[][];
if (to === 'front') next = [...units.filter((u) => !isChosen(u)), ...units.filter(isChosen)];
else if (to === 'back') next = [...units.filter(isChosen), ...units.filter((u) => !isChosen(u))];
else {
next = units.slice();
const step = to === 'forward' ? 1 : -1;
const order = next.map((_, i) => i);
if (step === 1) order.reverse();
for (const i of order) {
const here = next[i];
const there = next[i + step];
if (!here || !there || !isChosen(here) || isChosen(there)) continue;
next[i] = there;
next[i + step] = here;
}
}
const elements = next.flat();
return elements.every((e, i) => e === spread.elements[i]) ? spread : { ...spread, elements };
}
/**
* Members of one group moved in the paint order among the group's own members
* only, the group staying where it is on the page: what "to front" does to an
* element picked inside a group.
*/
export function arrangeWithinGroup(
spread: BookSpread,
groupId: string,
ids: readonly string[],
to: BookArrangeTo,
): BookSpread {
const slots: number[] = [];
spread.elements.forEach((e, i) => {
if (e.group === groupId) slots.push(i);
});
const members = slots.map((i) => spread.elements[i] as BookElement);
const reordered = arrangeElements({ ...spread, elements: members.map(withoutGroupId) }, ids, to).elements;
const byId = new Map(members.map((m) => [m.id, m]));
const elements = spread.elements.slice();
slots.forEach((slot, k) => {
const moved = reordered[k];
if (moved) elements[slot] = byId.get(moved.id) as BookElement;
});
return elements.every((e, i) => e === spread.elements[i]) ? spread : { ...spread, elements };
}
/** A member taken out of its group for reordering among the members alone, so gathering leaves them be. */
function withoutGroupId(el: BookElement): BookElement {
const next = { ...el };
delete next.group;
return next;
}
/** The box around frames. */
export function boundsOfFrames(frames: readonly BookFrame[]): BookFrame {
const x = Math.min(...frames.map((f) => f.x));
const y = Math.min(...frames.map((f) => f.y));
const right = Math.max(...frames.map((f) => f.x + f.w));
const bottom = Math.max(...frames.map((f) => f.y + f.h));
return { x, y, w: right - x, h: bottom - y };
}
/** The page a box's centre is on, or its safe area. */
function pageBox(spread: BookSpread, page: BookPageSetup, f: BookFrame, safe: boolean): BookFrame {
const onRight = spread.role === 'inner' && f.x + f.w / 2 >= page.pageWidth;
const x = onRight ? page.pageWidth : 0;
const inset = safe ? page.safe : 0;
return { x: x + inset, y: inset, w: page.pageWidth - inset * 2, h: page.pageHeight - inset * 2 };
}
/** Where a box goes to line up on an edge of another. */
function alignedAt(f: BookFrame, box: BookFrame, edge: BookAlignEdge): { x: number; y: number } {
switch (edge) {
case 'left':
return { x: box.x, y: f.y };
case 'center':
return { x: box.x + (box.w - f.w) / 2, y: f.y };
case 'right':
return { x: box.x + box.w - f.w, y: f.y };
case 'top':
return { x: f.x, y: box.y };
case 'middle':
return { x: f.x, y: box.y + (box.h - f.h) / 2 };
case 'bottom':
return { x: f.x, y: box.y + box.h - f.h };
}
}
/** What moves as one: a group's named members together, every other element alone. */
interface Unit {
members: BookElement[];
box: BookFrame;
}
function unitsOf(spread: BookSpread, els: readonly BookElement[]): Unit[] {
const ids = els.map((e) => e.id);
// All in one group: the members are the units, so the inside of a group can be set.
const inside = commonGroup(spread, ids) !== null;
const byKey = new Map<string, BookElement[]>();
for (const el of els) {
const key = !inside && el.group ? `g:${el.group}` : `e:${el.id}`;
byKey.set(key, [...(byKey.get(key) ?? []), el]);
}
return [...byKey.values()].map((members) => ({ members, box: boundsOfFrames(members.map((m) => m.frame)) }));
}
/** Every member of a unit moved by the same offset, rounded as the document stores it. */
function shifted(unit: Unit, dx: number, dy: number): BookElement[] {
// An axis that does not move is left exactly as it was, unrounded or not.
return unit.members.map((e) => {
const x = dx === 0 ? e.frame.x : round2(e.frame.x + dx);
const y = dy === 0 ? e.frame.y : round2(e.frame.y + dy);
return { ...e, frame: { ...e.frame, x, y } } as BookElement;
});
}
function replaceAll(spread: BookSpread, next: readonly BookElement[]): BookSpread {
const byId = new Map(next.map((e) => [e.id, e]));
return { ...spread, elements: spread.elements.map((e) => byId.get(e.id) ?? e) };
}
/** The named elements lined up on an edge or a centre line of their box, their page, or its safe area. */
export function alignElements(
spread: BookSpread,
page: BookPageSetup,
els: readonly BookElement[],
edge: BookAlignEdge,
to: BookAlignTo,
): BookSpread {
const units = unitsOf(spread, els);
const selection = boundsOfFrames(units.map((u) => u.box));
const next = units.flatMap((u) => {
const box = to === 'selection' ? selection : pageBox(spread, page, u.box, to === 'safe');
const at = alignedAt(u.box, box, edge);
return shifted(u, at.x - u.box.x, at.y - u.box.y);
});
return replaceAll(spread, next);
}
/** How many units a distribute has: it needs three. */
export function distributableCount(spread: BookSpread, els: readonly BookElement[]): number {
return unitsOf(spread, els).length;
}
/** Three or more units spaced evenly between the outer two, or by a fixed gap in millimetres. */
export function distributeElements(
spread: BookSpread,
els: readonly BookElement[],
axis: BookDistributeAxis,
gap?: number,
): BookSpread {
const units = unitsOf(spread, els);
if (units.length < 3) return spread;
const across = axis === 'horizontal';
const start = (f: BookFrame) => (across ? f.x : f.y);
const size = (f: BookFrame) => (across ? f.w : f.h);
const sorted = units.slice().sort((a, b) => start(a.box) - start(b.box));
const first = sorted[0] as Unit;
const last = sorted[sorted.length - 1] as Unit;
const total = sorted.reduce((sum, u) => sum + size(u.box), 0);
const span = start(last.box) + size(last.box) - start(first.box);
const space = gap ?? (span - total) / (sorted.length - 1);
let cursor = start(first.box);
const next = sorted.flatMap((u) => {
const delta = round2(cursor) - start(u.box);
cursor += size(u.box) + space;
return across ? shifted(u, delta, 0) : shifted(u, 0, delta);
});
return replaceAll(spread, next);
}
+2 -1
View File
@@ -1,6 +1,7 @@
import { compareCodeUnits } from '../common/compare';
import { contrastOfLuminance, deltaE, greyLuminance, luminance, parseHex, requiredContrast } from './book-colour';
import { bookFontCountsInMix, bookFontsUsed } from './book-fonts';
import { visibleSpread } from './book-groups';
import type { BookPhotoInfo } from './book-ops-core';
import { BOOK_OP_LIMITS, type BookFinding } from './book-ops.schema';
import type { BookDocument, BookElement, BookFrame, BookPageSetup, BookPhotoElement, BookSpread } from './book.schema';
@@ -300,7 +301,7 @@ export function designFindings(
page: BookPageSetup,
photos?: readonly BookPhotoInfo[],
): BookFinding[] {
const ctx: Ctx = { spread, page, photos, out: [] };
const ctx: Ctx = { spread: visibleSpread(spread), page, photos, out: [] };
contrastAndBusy(ctx);
smallText(ctx);
crowded(ctx);
+5 -1
View File
@@ -1,6 +1,7 @@
import { bookVisiblePart, sortBookFindings } from './book-findings-design';
import { bookFontFace } from './book-fonts';
import { stickerFindings } from './book-findings-sticker';
import { visibleSpread } from './book-groups';
import type { BookPhotoInfo } from './book-ops-core';
import type { BookFinding } from './book-ops.schema';
import type { BookElement, BookPageSetup, BookPhotoElement, BookSpread, BookTextElement } from './book.schema';
@@ -242,6 +243,8 @@ function overlaps(ctx: Ctx): void {
const a = content[i] as BookElement;
const b = content[j] as BookElement;
if (a.kind !== b.kind) continue;
// Set over each other inside one group on purpose: a lockup, a framed caption.
if (a.group !== undefined && a.group === b.group) continue;
const ba = elementBox(a);
const bb = elementBox(b);
const shared = overlapArea(ba, bb);
@@ -487,7 +490,8 @@ export function spreadFindings(
photos?: readonly BookPhotoInfo[],
): BookFinding[] {
const ctx: Ctx = {
spread,
// A hidden element is not on the page, so nothing about it is a fault of the page.
spread: visibleSpread(spread),
page,
W: page.pageWidth * (spread.role === 'inner' ? 2 : 1),
H: page.pageHeight,
+49
View File
@@ -0,0 +1,49 @@
import { bookElementIdSchema } from './book-op-ids';
import { BOOK_LAYER_NAME_MAX } from './book.schema';
import { z } from 'zod';
/**
* The layer operations of a book page: group, ungroup, name and hide. They
* join the page operations of book-ops.schema.ts beside the element ones, run
* in the one engine (book-ops-groups.ts) the Studio's layers panel and its
* Ctrl+G use too.
*/
/** A group's id as the page has it (`g-...`), never invented. */
export const bookGroupIdSchema = z.string().regex(/^[\w-]{1,40}$/);
const layerName = z.string().max(BOOK_LAYER_NAME_MAX);
export const BOOK_GROUP_OP_SCHEMAS = [
/**
* Make two or more elements one group, picked, moved, aligned and arranged
* as one. Elements already in a group leave it for the new one.
*/
z.strictObject({
op: z.literal('group'),
elementIds: z.array(bookElementIdSchema).min(2).max(30),
name: layerName
.describe('What the group is called in the layers panel, such as "Title lockup". Optional.')
.optional(),
}),
/** Put a group's members back on their own, where they are. */
z.strictObject({ op: z.literal('ungroup'), groupId: bookGroupIdSchema }),
/** Name an element or a group in the layers panel; an empty name clears it. */
z
.strictObject({
op: z.literal('name'),
elementId: bookElementIdSchema.optional(),
groupId: bookGroupIdSchema.optional(),
name: layerName,
})
.refine((v) => (v.elementId === undefined) !== (v.groupId === undefined), {
message: 'name an elementId or a groupId, not both',
}),
/** Hide elements (kept on the page's layer list, left out of the page and its print) or show them again. */
z.strictObject({
op: z.literal('hide'),
elementIds: z.array(bookElementIdSchema).min(1).max(30),
hidden: z.boolean(),
}),
] as const;
+201
View File
@@ -0,0 +1,201 @@
import {
bookGroupName,
commonGroup,
expandToGroups,
gatherGroups,
groupElements,
groupMembers,
isWholeGroup,
regroupCopies,
renameElement,
renameGroup,
setElementsHidden,
spreadGroupIds,
tidyGroups,
ungroupElements,
visibleSpread,
withCopies,
withoutGroup,
} from './book-groups';
import { bookShapeElementSchema, bookSpreadSchema, type BookElement, type BookSpread } from './book.schema';
import { describe, expect, it } from 'vitest';
/**
* Groups on a page: membership on the elements, names on the spread, at least
* two members each, kept next to each other in the paint order.
*/
const shape = (id: string, over: Partial<BookElement> = {}) =>
bookShapeElementSchema.parse({
id,
kind: 'shape',
frame: { x: 10, y: 10, w: 20, h: 20 },
shape: 'rect',
...over,
}) as BookElement;
const page = (elements: BookElement[], over: Partial<BookSpread> = {}): BookSpread =>
bookSpreadSchema.parse({ id: 'sp', elements, ...over });
const order = (sp: BookSpread) => sp.elements.map((e) => `${e.id}${e.group ? `:${e.group}` : ''}`);
describe('reading groups', () => {
const sp = page(
[
shape('a', { group: 'g1' }),
shape('b'),
shape('c', { group: 'g1' }),
shape('d', { group: 'g2' }),
shape('e', { group: 'g2' }),
],
{ groups: [{ id: 'g1', name: 'Lockup' }] },
);
it('BKGRP-001: lists members, names and the groups on the page in paint order', () => {
expect(groupMembers(sp, 'g1').map((e) => e.id)).toEqual(['a', 'c']);
expect(bookGroupName(sp, 'g1')).toBe('Lockup');
expect(bookGroupName(sp, 'g2')).toBeUndefined();
expect(spreadGroupIds(sp)).toEqual(['g1', 'g2']);
});
it('BKGRP-002: a click on one member picks the whole group, and an ungrouped element alone', () => {
expect(expandToGroups(sp, ['c'])).toEqual(['a', 'c']);
expect(expandToGroups(sp, ['b'])).toEqual(['b']);
expect(expandToGroups(sp, ['b', 'e'])).toEqual(['b', 'd', 'e']);
});
it('BKGRP-003: knows the one group a selection is in, and whether it is the whole of it', () => {
expect(commonGroup(sp, ['a', 'c'])).toBe('g1');
expect(commonGroup(sp, ['a', 'd'])).toBeNull();
expect(commonGroup(sp, ['b'])).toBeNull();
expect(commonGroup(sp, [])).toBeNull();
expect(isWholeGroup(sp, ['a', 'c'])).toBe(true);
expect(isWholeGroup(sp, ['a'])).toBe(false);
});
});
describe('making and undoing groups', () => {
it('BKGRP-004: gathers a group at the place of its topmost member, keeping their order', () => {
const sp = page([shape('a', { group: 'g' }), shape('b'), shape('c', { group: 'g' }), shape('d')]);
expect(order(gatherGroups(sp))).toEqual(['b', 'a:g', 'c:g', 'd']);
const tidy = page([shape('a', { group: 'g' }), shape('c', { group: 'g' }), shape('d')]);
expect(gatherGroups(tidy)).toBe(tidy);
const none = page([shape('a')]);
expect(gatherGroups(none)).toBe(none);
});
it('BKGRP-005: groups elements under a new id and a trimmed name, gathered together', () => {
const sp = groupElements(page([shape('a'), shape('b'), shape('c')]), ['a', 'c'], 'g-new', ' Title ');
expect(order(sp)).toEqual(['b', 'a:g-new', 'c:g-new']);
expect(sp.groups).toEqual([{ id: 'g-new', name: 'Title' }]);
const unnamed = groupElements(page([shape('a'), shape('b')]), ['a', 'b'], 'g2');
expect(unnamed.groups).toBeUndefined();
});
it('BKGRP-006: an element taken into a new group leaves its old one, which dissolves when one is left', () => {
const sp = page([shape('a', { group: 'old' }), shape('b', { group: 'old' }), shape('c')], {
groups: [{ id: 'old', name: 'Old' }],
});
const next = groupElements(sp, ['b', 'c'], 'new');
expect(order(next)).toEqual(['a', 'b:new', 'c:new']);
expect(next.groups).toBeUndefined();
});
it('BKGRP-007: ungroups, leaving the members where they are and the name gone', () => {
const sp = page([shape('a', { group: 'g' }), shape('b', { group: 'g' }), shape('c')], {
groups: [{ id: 'g', name: 'X' }],
});
const next = ungroupElements(sp, 'g');
expect(order(next)).toEqual(['a', 'b', 'c']);
expect(next.groups).toBeUndefined();
expect('group' in (next.elements[0] as object)).toBe(false);
});
it('BKGRP-008: tidies a lone member and a name without a group, and leaves an orderly page as it is', () => {
const sp = page([shape('a', { group: 'g' }), shape('b')], {
groups: [
{ id: 'g', name: 'Lone' },
{ id: 'gone', name: 'Gone' },
],
});
const next = tidyGroups(sp);
expect(order(next)).toEqual(['a', 'b']);
expect(next.groups).toBeUndefined();
const fine = page([shape('a', { group: 'g' }), shape('b', { group: 'g' })], { groups: [{ id: 'g', name: 'Y' }] });
expect(tidyGroups(fine)).toBe(fine);
const keeps = tidyGroups(
page([shape('a', { group: 'g' }), shape('b', { group: 'g' })], {
groups: [
{ id: 'g', name: 'Y' },
{ id: 'x', name: 'Z' },
],
}),
);
expect(keeps.groups).toEqual([{ id: 'g', name: 'Y' }]);
});
});
describe('layer names and visibility', () => {
it('BKGRP-009: names an element or a group, and an empty name clears it', () => {
const sp = page([shape('a', { group: 'g' }), shape('b', { group: 'g' })]);
const named = renameElement(sp, 'a', ' Sun ');
expect(named.elements[0]?.name).toBe('Sun');
expect('name' in (renameElement(named, 'a', ' ').elements[0] as object)).toBe(false);
const group = renameGroup(sp, 'g', 'Sky');
expect(group.groups).toEqual([{ id: 'g', name: 'Sky' }]);
expect(renameGroup(renameGroup(group, 'g', 'Sea'), 'g', '').groups).toBeUndefined();
expect(renameGroup(group, 'g', 'Sea').groups).toEqual([{ id: 'g', name: 'Sea' }]);
});
it('BKGRP-010: hides and shows, a shown element storing no flag, and the print leaves the hidden out', () => {
const sp = page([shape('a'), shape('b')]);
const hidden = setElementsHidden(sp, ['a'], true);
expect(hidden.elements[0]?.hidden).toBe(true);
expect(visibleSpread(hidden).elements.map((e) => e.id)).toEqual(['b']);
expect(visibleSpread(sp)).toBe(sp);
const shown = setElementsHidden(hidden, ['a'], false);
expect('hidden' in (shown.elements[0] as object)).toBe(false);
});
it('BKGRP-011: a document from before groups reads with none of the new fields set', () => {
const sp = page([shape('a')]);
expect(sp.groups).toBeUndefined();
expect(Object.keys(sp.elements[0] as object)).not.toEqual(expect.arrayContaining(['group']));
// A broken name list is dropped, never the page.
const odd = bookSpreadSchema.parse({ id: 'sp', elements: [], groups: 'nonsense' });
expect(odd.groups).toBeUndefined();
});
});
describe('copies', () => {
let n = 0;
const newId = (p: string) => `${p}-c${++n}`;
it('BKGRP-012: copies of a group become a group of their own, its name carried along', () => {
const source = page([shape('a', { group: 'g' }), shape('b', { group: 'g' }), shape('c', { group: 'h' })], {
groups: [{ id: 'g', name: 'Lockup' }],
});
const copies = source.elements.map((e) => ({ ...e, id: `${e.id}2` }));
const out = regroupCopies(copies, source, newId);
const [a, b, c] = out.elements;
expect(a?.group).toBe(b?.group);
expect(a?.group).not.toBe('g');
// c was the only copy of its group: it goes alone.
expect(c && 'group' in c).toBe(false);
expect(out.groups).toEqual([{ id: a?.group, name: 'Lockup' }]);
const target = withCopies(page([shape('z')]), out);
expect(target.elements.map((e) => e.id)).toEqual(['z', 'a2', 'b2', 'c2']);
expect(target.groups).toEqual(out.groups);
});
it('BKGRP-013: copies without groups, or without a source to name them from, stay plain', () => {
const plain = regroupCopies([shape('a')], undefined, newId);
expect(plain.groups).toEqual([]);
const unnamed = regroupCopies([shape('a', { group: 'g' }), shape('b', { group: 'g' })], undefined, newId);
expect(unnamed.groups).toEqual([]);
expect(unnamed.elements[0]?.group).toBe(unnamed.elements[1]?.group);
expect(withCopies(page([]), plain).groups).toBeUndefined();
expect(withoutGroup(shape('x', { group: 'g' })).group).toBeUndefined();
});
});
+231
View File
@@ -0,0 +1,231 @@
import type { BookElement, BookGroup, BookSpread } from './book.schema';
/**
* Groups on a page, and the layer fields that come with them.
*
* ── What a group is ──────────────────────────────────────────────────────
*
* A flat set of elements of one spread that are picked, moved, aligned and
* arranged as one. Membership lives on the elements (`group`, an id that is
* only meaningful on its own spread); the spread's `groups` list holds what a
* group is called. There is no nesting: grouping elements that are already in
* groups makes one new group of all of them, which is what a page needs and
* what a model can describe without a tree.
*
* Two rules keep every reader simple:
* - a group has at least two members on the page (`tidyGroups` dissolves the
* rest, so a lone element never carries a group nobody can see);
* - its members are next to each other in the paint order (`gatherGroups`),
* so the layers panel can show a group as one row without lying about what
* is in front of what.
*
* The Studio and the book engine (book-ops-groups.ts) both call these, so a
* group made by hand and one made by the assistant are the same thing.
*/
/** Every member of a group, in paint order. */
export function groupMembers(spread: BookSpread, groupId: string): BookElement[] {
return spread.elements.filter((e) => e.group === groupId);
}
/** What a group is called, or undefined when it has no name. */
export function bookGroupName(spread: BookSpread, groupId: string): string | undefined {
return spread.groups?.find((g) => g.id === groupId)?.name;
}
/** The groups of a spread that have members on the page, in the paint order of their lowest member. */
export function spreadGroupIds(spread: BookSpread): string[] {
const seen = new Set<string>();
for (const el of spread.elements) if (el.group) seen.add(el.group);
return [...seen];
}
/**
* The ids named plus every other member of their groups, in paint order.
*
* What a click on a grouped element picks, and what a marquee that touches one
* member of a group picks: the group is the thing on the page.
*/
export function expandToGroups(spread: BookSpread, ids: readonly string[]): string[] {
const named = new Set(ids);
const groups = new Set<string>();
for (const el of spread.elements) if (named.has(el.id) && el.group) groups.add(el.group);
return spread.elements
.filter((e) => named.has(e.id) || (e.group !== undefined && groups.has(e.group)))
.map((e) => e.id);
}
/** The one group every named element is in, or null when they are not all in the same one. */
export function commonGroup(spread: BookSpread, ids: readonly string[]): string | null {
const els = spread.elements.filter((e) => ids.includes(e.id));
const first = els[0]?.group;
if (!first || !els.every((e) => e.group === first)) return null;
return first;
}
/** Whether the named ids are exactly the members of one group, no more and no fewer. */
export function isWholeGroup(spread: BookSpread, ids: readonly string[]): boolean {
const group = commonGroup(spread, ids);
return group !== null && groupMembers(spread, group).length === new Set(ids).size;
}
/**
* The elements moved so every group's members are next to each other, at the
* place of the group's topmost member, keeping their order among themselves.
* The same spread object when nothing had to move.
*/
export function gatherGroups(spread: BookSpread): BookSpread {
const lastIndex = new Map<string, number>();
spread.elements.forEach((e, i) => {
if (e.group) lastIndex.set(e.group, i);
});
if (!lastIndex.size) return spread;
const out: BookElement[] = [];
spread.elements.forEach((e, i) => {
if (!e.group) {
out.push(e);
return;
}
if (lastIndex.get(e.group) !== i) return;
out.push(...groupMembers(spread, e.group));
});
return out.every((e, i) => e === spread.elements[i]) ? spread : { ...spread, elements: out };
}
/**
* Groups with fewer than two members on the page dissolved, and names of
* groups that are gone dropped. The same spread object when all was in order.
*/
export function tidyGroups(spread: BookSpread): BookSpread {
const count = new Map<string, number>();
for (const e of spread.elements) if (e.group) count.set(e.group, (count.get(e.group) ?? 0) + 1);
const lonely = (e: BookElement) => e.group !== undefined && (count.get(e.group) ?? 0) < 2;
const elements = spread.elements.some(lonely)
? spread.elements.map((e) => (lonely(e) ? withoutGroup(e) : e))
: spread.elements;
const live = new Set([...count].filter(([, n]) => n >= 2).map(([id]) => id));
const groups = spread.groups?.filter((g) => live.has(g.id));
const groupsChanged = spread.groups !== undefined && groups?.length !== spread.groups.length;
if (elements === spread.elements && !groupsChanged) return spread;
const next: BookSpread = { ...spread, elements };
if (groupsChanged) {
if (groups?.length) next.groups = groups;
else delete next.groups;
}
return next;
}
/** An element without its group. */
export function withoutGroup<E extends BookElement>(el: E): E {
const next = { ...el };
delete next.group;
return next;
}
function withName(groups: readonly BookGroup[] | undefined, id: string, name: string | undefined): BookGroup[] {
const rest = (groups ?? []).filter((g) => g.id !== id);
return name ? [...rest, { id, name }] : rest;
}
function setGroups(spread: BookSpread, groups: BookGroup[]): BookSpread {
const next: BookSpread = { ...spread, groups };
if (!groups.length) delete next.groups;
return next;
}
/**
* The named elements made one new group, gathered at the place of the topmost
* of them. Elements that were in other groups leave them (and a group left with
* one member dissolves). `name` is the group's name, none for an unnamed one.
*/
export function groupElements(spread: BookSpread, ids: readonly string[], groupId: string, name?: string): BookSpread {
const chosen = new Set(ids);
const elements = spread.elements.map((e) => (chosen.has(e.id) ? { ...e, group: groupId } : e));
const trimmed = name?.trim();
return tidyGroups(gatherGroups(setGroups({ ...spread, elements }, withName(spread.groups, groupId, trimmed))));
}
/** A group's members back on their own, where they are in the paint order. */
export function ungroupElements(spread: BookSpread, groupId: string): BookSpread {
const elements = spread.elements.map((e) => (e.group === groupId ? withoutGroup(e) : e));
return tidyGroups({ ...spread, elements });
}
/** A group named, or its name cleared with an empty one. */
export function renameGroup(spread: BookSpread, groupId: string, name: string): BookSpread {
return setGroups(spread, withName(spread.groups, groupId, name.trim() || undefined));
}
/** An element named, or its name cleared with an empty one. */
export function renameElement(spread: BookSpread, id: string, name: string): BookSpread {
const trimmed = name.trim();
const elements = spread.elements.map((e) => {
if (e.id !== id) return e;
const next = { ...e };
if (trimmed) next.name = trimmed;
else delete next.name;
return next;
});
return { ...spread, elements };
}
/** Elements shown or hidden. `hidden: false` drops the field, so a shown element stores what it stored before. */
export function setElementsHidden(spread: BookSpread, ids: readonly string[], hidden: boolean): BookSpread {
const chosen = new Set(ids);
const elements = spread.elements.map((e) => {
if (!chosen.has(e.id)) return e;
const next = { ...e };
if (hidden) next.hidden = true;
else delete next.hidden;
return next;
});
return { ...spread, elements };
}
/** The spread as it prints: without its hidden elements. The same object when none is hidden. */
export function visibleSpread(spread: BookSpread): BookSpread {
if (!spread.elements.some((e) => e.hidden)) return spread;
return { ...spread, elements: spread.elements.filter((e) => !e.hidden) };
}
/**
* Copies of elements given groups of their own.
*
* A copy that kept its source's group id would join the original group on a
* duplicate, or another page's group of the same id on a paste. Each group
* among the copies gets a fresh id; its name comes along from `source` (the
* spread the copies came from), so a copied lockup is still called what it was.
* A copy that is the only one of its group among the copies goes alone.
*/
export function regroupCopies(
copies: readonly BookElement[],
source: BookSpread | undefined,
newId: (prefix: string) => string,
): { elements: BookElement[]; groups: BookGroup[] } {
const count = new Map<string, number>();
for (const e of copies) if (e.group) count.set(e.group, (count.get(e.group) ?? 0) + 1);
const fresh = new Map<string, string>();
const groups: BookGroup[] = [];
for (const [old, n] of count) {
if (n < 2) continue;
const id = newId('g');
fresh.set(old, id);
const name = source ? bookGroupName(source, old) : undefined;
groups.push(name ? { id, name } : { id });
}
const elements = copies.map((e) => {
if (!e.group) return e;
const id = fresh.get(e.group);
return id ? { ...e, group: id } : withoutGroup(e);
});
return { elements, groups: groups.filter((g) => g.name !== undefined) };
}
/** Copies added on top of a spread with the names of their new groups. */
export function withCopies(
spread: BookSpread,
copied: { elements: readonly BookElement[]; groups: readonly BookGroup[] },
): BookSpread {
const next = { ...spread, elements: [...spread.elements, ...copied.elements] };
return copied.groups.length ? { ...next, groups: [...(spread.groups ?? []), ...copied.groups] } : next;
}
+1
View File
@@ -43,6 +43,7 @@ export const BOOK_OP_ERROR_CODES = [
'sticker_unknown',
'not_enough_elements',
'layouts_full',
'group_not_found',
] as const;
export type BookOpErrorCode = (typeof BOOK_OP_ERROR_CODES)[number];
+16 -81
View File
@@ -1,8 +1,10 @@
import { relayoutSpread } from './auto/auto-book';
import type { AutoEntry, AutoInput } from './auto/auto-types';
import { alignElements, arrangeElements, distributableCount, distributeElements } from './book-arrange';
import { bookElementPropsSchema, type BOOK_ELEMENT_OP_SCHEMAS, type BookNewElement } from './book-element-ops.schema';
import { withRealWeight } from './book-fonts';
import { bookGridElements, defaultBookGridBox, findBookGrid } from './book-grids';
import { regroupCopies, tidyGroups, withCopies, withoutGroup } from './book-groups';
import {
bookBoundWords,
failOp,
@@ -427,94 +429,26 @@ function opLock(spread: BookSpread, op: Op<'lock'>, env: ElementOpEnv): BookSpre
);
}
// ── arrange ────────────────────────────────────────────────────────────────
function opArrange(spread: BookSpread, op: Op<'arrange'>, env: ElementOpEnv): BookSpread {
const chosen = new Set(namedElements(spread, op.elementIds, env).map((e) => e.id));
const pick = (want: boolean) => spread.elements.filter((e) => chosen.has(e.id) === want);
if (op.to === 'front') return { ...spread, elements: [...pick(false), ...pick(true)] };
if (op.to === 'back') return { ...spread, elements: [...pick(true), ...pick(false)] };
const els = spread.elements.slice();
const step = op.to === 'forward' ? 1 : -1;
const order = els.map((_, i) => i);
if (step === 1) order.reverse();
for (const i of order) {
const here = els[i];
const there = els[i + step];
if (!here || !there || !chosen.has(here.id) || chosen.has(there.id)) continue;
els[i] = there;
els[i + step] = here;
}
return { ...spread, elements: els };
}
// ── align, distribute ──────────────────────────────────────────────────────
// ── arrange, align, distribute (book-arrange.ts) ───────────────────────────
/** A frame in millimetres, as the book types it, so a zod-inferred `el.frame` passes in every compile mode. */
type Box = BookFrame;
function boundsOf(frames: readonly Box[]): Box {
const x = Math.min(...frames.map((f) => f.x));
const y = Math.min(...frames.map((f) => f.y));
const right = Math.max(...frames.map((f) => f.x + f.w));
const bottom = Math.max(...frames.map((f) => f.y + f.h));
return { x, y, w: right - x, h: bottom - y };
}
/** The page an element's centre is on, or its safe area. */
function pageBox(spread: BookSpread, page: BookPageSetup, f: Box, safe: boolean): Box {
const onRight = isInner(spread) && f.x + f.w / 2 >= page.pageWidth;
const x = onRight ? page.pageWidth : 0;
const inset = safe ? page.safe : 0;
return { x: x + inset, y: inset, w: page.pageWidth - inset * 2, h: page.pageHeight - inset * 2 };
}
function alignedFrame(f: Box, box: Box, edge: Op<'align'>['edge']): BookFrame {
switch (edge) {
case 'left':
return { ...f, x: round2(box.x) };
case 'center':
return { ...f, x: round2(box.x + (box.w - f.w) / 2) };
case 'right':
return { ...f, x: round2(box.x + box.w - f.w) };
case 'top':
return { ...f, y: round2(box.y) };
case 'middle':
return { ...f, y: round2(box.y + (box.h - f.h) / 2) };
case 'bottom':
return { ...f, y: round2(box.y + box.h - f.h) };
}
function opArrange(spread: BookSpread, op: Op<'arrange'>, env: ElementOpEnv): BookSpread {
const chosen = namedElements(spread, op.elementIds, env).map((e) => e.id);
return arrangeElements(spread, chosen, op.to);
}
function opAlign(spread: BookSpread, op: Op<'align'>, env: ElementOpEnv): BookSpread {
const els = namedElements(spread, op.elementIds, env);
const selection = boundsOf(els.map((e) => e.frame));
const next = els.map((e) => {
const box = op.to === 'selection' ? selection : pageBox(spread, env.page, e.frame, op.to === 'safe');
return { ...e, frame: alignedFrame(e.frame, box, op.edge) } as BookElement;
});
return replaceAll(spread, next);
return alignElements(spread, env.page, namedElements(spread, op.elementIds, env), op.edge, op.to);
}
function opDistribute(spread: BookSpread, op: Op<'distribute'>, env: ElementOpEnv): BookSpread {
const els = namedElements(spread, op.elementIds, env);
if (els.length < 3) throw failOp('not_enough_elements', 'distribute needs three different elements', env.opIndex);
const across = op.axis === 'horizontal';
const start = (f: Box) => (across ? f.x : f.y);
const size = (f: Box) => (across ? f.w : f.h);
const sorted = els.slice().sort((a, b) => start(a.frame) - start(b.frame));
const first = sorted[0] as BookElement;
const last = sorted[sorted.length - 1] as BookElement;
const total = sorted.reduce((sum, e) => sum + size(e.frame), 0);
const span = start(last.frame) + size(last.frame) - start(first.frame);
const gap = op.gap ?? (span - total) / (sorted.length - 1);
let cursor = start(first.frame);
const next = sorted.map((e) => {
const at = round2(cursor);
cursor += size(e.frame) + gap;
return { ...e, frame: across ? { ...e.frame, x: at } : { ...e.frame, y: at } } as BookElement;
});
return replaceAll(spread, next);
if (distributableCount(spread, els) < 3) {
throw failOp('not_enough_elements', 'distribute needs three different elements or groups', env.opIndex);
}
return distributeElements(spread, els, op.axis, op.gap);
}
// ── duplicate, fill, copy_from ─────────────────────────────────────────────
@@ -530,7 +464,7 @@ function opDuplicate(spread: BookSpread, op: Op<'duplicate'>, env: ElementOpEnv)
const frame = { ...e.frame, x: round2(e.frame.x + dx), y: round2(e.frame.y + dy) };
return { ...e, id: env.newId(prefixOf(e)), frame } as BookElement;
});
return { ...spread, elements: [...spread.elements, ...copies] };
return withCopies(spread, regroupCopies(copies, spread, env.newId));
}
/** The inspector's fill: the photo to the bleed of the page its centre is on, or of the spread, at the back. */
@@ -548,8 +482,9 @@ function opFill(spread: BookSpread, op: Op<'fill'>, env: ElementOpEnv): BookSpre
w: (spanning ? 2 : 1) * pageWidth + 2 * bleed,
h: pageHeight + 2 * bleed,
};
const filled: BookElement = { ...el, frame, rotation: 0, fit: 'cover' };
return { ...spread, elements: [filled, ...spread.elements.filter((e) => e.id !== el.id)] };
// A picture that is the page is nobody's lockup: it leaves its group.
const filled: BookElement = { ...withoutGroup(el), frame, rotation: 0, fit: 'cover' };
return tidyGroups({ ...spread, elements: [filled, ...spread.elements.filter((e) => e.id !== el.id)] });
}
/** Which page of its spread an element is on: the left (or only) page is 0, the right one starts at the fold. */
@@ -568,7 +503,7 @@ function opCopyFrom(spread: BookSpread, op: Op<'copy_from'>, env: ElementOpEnv):
const x = sameKind ? e.frame.x : e.frame.x - pageOrigin(source, env.page, e.frame) + target;
return { ...e, id: env.newId(prefixOf(e)), frame: { ...e.frame, x: round2(x) } } as BookElement;
});
return { ...spread, elements: [...spread.elements, ...copies] };
return withCopies(spread, regroupCopies(copies, source, env.newId));
}
// ── refresh_travel, add_grid ───────────────────────────────────────────────
+239
View File
@@ -0,0 +1,239 @@
import { spreadFindings } from './book-findings';
import { designFindings } from './book-findings-design';
import { applyBookOps } from './book-ops';
import { isGroupOp } from './book-ops-groups';
import { bookPageOpSchema, type BookPageOpInput } from './book-ops.schema';
import { blankBook } from './book-pages';
import {
bookPhotoElementSchema,
bookShapeElementSchema,
bookSpreadSchema,
bookTextElementSchema,
type BookDocument,
type BookElement,
type BookSpread,
} from './book.schema';
import { describeSpread } from './describe-spread';
import { describe, expect, it } from 'vitest';
/**
* The layer operations the assistant sends, `group`, `ungroup`, `name` and
* `hide`, run by the one engine, and what grouping changes about the other
* operations, the findings and the words describe_book_page reads out.
*/
let seq = 0;
const newId = (p: string) => `${p}-n${++seq}`;
type Frame = { x: number; y: number; w: number; h: number };
const text = (id: string, words: string, frame: Frame = { x: 230, y: 120, w: 160, h: 40 }, over = {}) =>
bookTextElementSchema.parse({ id, kind: 'text', frame, text: words, size: 10, ...over }) as BookElement;
const shape = (id: string, x: number, y: number, over = {}) =>
bookShapeElementSchema.parse({
id,
kind: 'shape',
frame: { x, y, w: 20, h: 20 },
shape: 'rect',
...over,
}) as BookElement;
const photo = (id: string, frame: Frame, over = {}) =>
bookPhotoElementSchema.parse({ id, kind: 'photo', frame, photoId: 1, ...over }) as BookElement;
function book(elements: BookElement[], over: Partial<BookSpread> = {}): BookDocument {
const doc = blankBook({ title: 'Iceland', newId: (p) => `${p}-x` });
const ids = ['sp-cover', 'sp-first', 'sp-in', 'sp-last', 'sp-back'];
const spreads = doc.spreads.map((s, i) => ({ ...s, id: ids[i] as string }));
spreads[2] = { ...(spreads[2] as BookSpread), elements, ...over };
return { ...doc, spreads };
}
const inner = (doc: BookDocument) => doc.spreads[2] as BookSpread;
const run = (doc: BookDocument, ops: BookPageOpInput[]) => applyBookOps(doc, { spreadId: 'sp-in', ops }, { newId });
function ok(result: ReturnType<typeof applyBookOps>): BookDocument {
if (!result.ok) throw new Error(`refused: ${result.error.code} ${result.error.message}`);
expect(bookSpreadSchema.safeParse(inner(result.doc)).success).toBe(true);
return result.doc;
}
const refusal = (result: ReturnType<typeof applyBookOps>) => (result.ok ? null : result.error.code);
const order = (doc: BookDocument) => inner(doc).elements.map((e) => `${e.id}${e.group ? ':g' : ''}`);
describe('the layer operations', () => {
const page = () => book([shape('a', 230, 20), text('t', 'Title'), shape('b', 300, 20), shape('c', 330, 20)]);
it('BKGOPS-001: groups elements under a new id and name, gathered together at the topmost', () => {
const doc = ok(run(page(), [{ op: 'group', elementIds: ['a', 'b'], name: 'Sun and sea' }]));
expect(order(doc)).toEqual(['t', 'a:g', 'b:g', 'c']);
const groupId = inner(doc).elements[1]?.group;
expect(groupId).toMatch(/^g-n\d+$/);
expect(inner(doc).groups).toEqual([{ id: groupId, name: 'Sun and sea' }]);
});
it('BKGOPS-002: refuses a locked element, a missing one, and one element twice', () => {
const locked = book([shape('a', 230, 20, { locked: true }), shape('b', 300, 20)]);
expect(refusal(run(locked, [{ op: 'group', elementIds: ['a', 'b'] }]))).toBe('locked');
expect(refusal(run(page(), [{ op: 'group', elementIds: ['a', 'zz'] }]))).toBe('element_not_found');
expect(refusal(run(page(), [{ op: 'group', elementIds: ['a', 'a'] }]))).toBe('not_enough_elements');
});
it('BKGOPS-003: ungroups, names and hides, locked elements too, and says which group is missing', () => {
const grouped = ok(run(page(), [{ op: 'group', elementIds: ['a', 'b'] }]));
const groupId = inner(grouped).elements.find((e) => e.id === 'a')?.group as string;
const named = ok(
run(grouped, [
{ op: 'name', groupId, name: 'Pair' },
{ op: 'name', elementId: 't', name: 'Heading' },
]),
);
expect(inner(named).groups).toEqual([{ id: groupId, name: 'Pair' }]);
expect(inner(named).elements.find((e) => e.id === 't')?.name).toBe('Heading');
const apart = ok(run(named, [{ op: 'ungroup', groupId }]));
expect(order(apart)).toEqual(['t', 'a', 'b', 'c']);
expect(inner(apart).groups).toBeUndefined();
expect(refusal(run(apart, [{ op: 'ungroup', groupId }]))).toBe('group_not_found');
expect(refusal(run(apart, [{ op: 'name', groupId: 'g-none', name: 'x' }]))).toBe('group_not_found');
expect(refusal(run(apart, [{ op: 'name', elementId: 'zz', name: 'x' }]))).toBe('element_not_found');
const pinned = book([shape('a', 230, 20, { locked: true })]);
const hidden = ok(run(pinned, [{ op: 'hide', elementIds: ['a'], hidden: true }]));
expect(inner(hidden).elements[0]?.hidden).toBe(true);
const shown = ok(run(hidden, [{ op: 'hide', elementIds: ['a'], hidden: false }]));
expect('hidden' in (inner(shown).elements[0] as object)).toBe(false);
expect(refusal(run(pinned, [{ op: 'hide', elementIds: ['zz'], hidden: true }]))).toBe('element_not_found');
});
it('BKGOPS-004: the contract wants an element or a group to name, and two elements to group', () => {
expect(bookPageOpSchema.safeParse({ op: 'name', name: 'x' }).success).toBe(false);
expect(bookPageOpSchema.safeParse({ op: 'name', name: 'x', elementId: 'a', groupId: 'g' }).success).toBe(false);
expect(bookPageOpSchema.safeParse({ op: 'group', elementIds: ['a'] }).success).toBe(false);
expect(bookPageOpSchema.safeParse({ op: 'name', name: 'x'.repeat(61), elementId: 'a' }).success).toBe(false);
expect(isGroupOp({ op: 'group' })).toBe(true);
expect(isGroupOp({ op: 'align' })).toBe(false);
});
});
describe('the other operations, with groups', () => {
const grouped = () =>
book([shape('a', 230, 20, { group: 'g' }), shape('b', 260, 40, { group: 'g' }), shape('c', 330, 20)], {
groups: [{ id: 'g', name: 'Lockup' }],
});
it('BKGOPS-005: a duplicate of a group is a new group of the same name, never part of the old one', () => {
const doc = ok(run(grouped(), [{ op: 'duplicate', elementIds: ['a', 'b'] }]));
const els = inner(doc).elements;
const copies = els.slice(3);
expect(copies[0]?.group).toBe(copies[1]?.group);
expect(copies[0]?.group).not.toBe('g');
expect(inner(doc).groups).toEqual([
{ id: 'g', name: 'Lockup' },
{ id: copies[0]?.group, name: 'Lockup' },
]);
});
it('BKGOPS-006: copy_from brings a group as a group of its own', () => {
const doc = grouped();
const withSource = {
...doc,
spreads: doc.spreads.map((s, i) => (i === 3 ? { ...inner(doc), id: 'sp-last', role: 'last' as const } : s)),
};
const empty = {
...withSource,
spreads: withSource.spreads.map((s, i) => (i === 2 ? { ...s, elements: [], groups: undefined } : s)),
};
const done = ok(
applyBookOps(
empty,
{ spreadId: 'sp-in', ops: [{ op: 'copy_from', spreadId: 'sp-last', elementIds: ['a', 'b'] }] },
{ newId },
),
);
const [a, b] = inner(done).elements;
expect(a?.group).toBe(b?.group);
expect(a?.group).not.toBe('g');
expect(inner(done).groups?.[0]?.name).toBe('Lockup');
});
it('BKGOPS-007: removing a member leaves no group of one; aligning moves the group as one box', () => {
const removed = ok(run(grouped(), [{ op: 'remove', elementIds: ['a'], park: false }]));
expect(order(removed)).toEqual(['b', 'c']);
expect(inner(removed).groups).toBeUndefined();
const aligned = ok(run(grouped(), [{ op: 'align', elementIds: ['a', 'b', 'c'], edge: 'top' }]));
const ys = inner(aligned).elements.map((e) => e.frame.y);
// The group's box starts at 20 already, so it stays; c is at 20 too.
expect(ys).toEqual([20, 40, 20]);
const arranged = ok(run(grouped(), [{ op: 'arrange', elementIds: ['c'], to: 'back' }]));
expect(order(arranged)).toEqual(['c', 'a:g', 'b:g']);
});
it('BKGOPS-008: distribute counts a group once', () => {
const doc = book([
shape('a', 0, 0),
shape('g1', 50, 0, { group: 'g' }),
shape('g2', 60, 0, { group: 'g' }),
shape('c', 200, 0),
]);
const spaced = ok(run(doc, [{ op: 'distribute', elementIds: ['a', 'g1', 'g2', 'c'], axis: 'horizontal' }]));
const x = (id: string) => inner(spaced).elements.find((e) => e.id === id)?.frame.x;
// Span 0..220, sizes 20 + 30 + 20, so gaps of 75; the group keeps its shape.
expect([x('g1'), x('g2'), x('c')]).toEqual([95, 105, 200]);
const two = book([shape('g1', 50, 0, { group: 'g' }), shape('g2', 60, 0, { group: 'g' }), shape('c', 200, 0)]);
expect(refusal(run(two, [{ op: 'distribute', elementIds: ['g1', 'g2', 'c'], axis: 'horizontal' }]))).toBe(
'not_enough_elements',
);
});
it('BKGOPS-009: a picture filled to the page leaves its group', () => {
const doc = book([
photo('p', { x: 230, y: 20, w: 50, h: 40 }, { group: 'g' }),
shape('s', 300, 20, { group: 'g' }),
]);
const filled = ok(run(doc, [{ op: 'fill', elementId: 'p', scope: 'page' }]));
expect(inner(filled).elements.every((e) => e.group === undefined)).toBe(true);
});
});
describe('what a model reads of groups', () => {
it('BKGOPS-010: describe_book_page names the groups, their members, the hidden and the named', () => {
const doc = book(
[
text('t1', 'Iceland', undefined, { group: 'g-1', name: 'Title' }),
text('t2', 'June 2026', { x: 230, y: 170, w: 160, h: 20 }, { group: 'g-1' }),
shape('s', 300, 20, { hidden: true }),
],
{ groups: [{ id: 'g-1', name: 'Title lockup' }] },
);
const page = describeSpread(doc, 'sp-in');
expect(page?.text).toContain(
'Groups, each picked, moved, aligned and arranged as one: g-1 "Title lockup" (t1, t2)',
);
expect(page?.text).toContain('in group g-1');
expect(page?.text).toContain('named "Title"');
expect(page?.text).toContain('hidden (not on the page or in print)');
expect(page?.elements[0]).toMatchObject({ group: 'g-1', name: 'Title' });
expect(page?.elements[2]).toMatchObject({ hidden: true });
expect(describeSpread(book([shape('s', 300, 20)]), 'sp-in')?.text).not.toContain('Groups,');
});
it('BKGOPS-011: two texts set over each other inside one group are a lockup, not an accident', () => {
const over = [
text('t1', 'Iceland', { x: 230, y: 120, w: 160, h: 40 }),
text('t2', 'Island', { x: 235, y: 125, w: 160, h: 40 }),
];
const loose = spreadFindings(inner(book(over)), book(over).page);
expect(loose.some((f) => f.code === 'overlap')).toBe(true);
const lockup = over.map((e) => ({ ...e, group: 'g' }) as BookElement);
expect(spreadFindings(inner(book(lockup)), book(lockup).page).some((f) => f.code === 'overlap')).toBe(false);
});
it('BKGOPS-012: a hidden element is not on the page, so the checks leave it out', () => {
const els = [
photo('p1', { x: 230, y: 20, w: 80, h: 60 }),
photo('p2', { x: 240, y: 30, w: 80, h: 60 }, { hidden: true }),
];
const doc = book(els);
expect(spreadFindings(inner(doc), doc.page).some((f) => f.elementIds?.includes('p2'))).toBe(false);
expect(designFindings(inner(doc), doc.page).some((f) => f.elementIds?.includes('p2'))).toBe(false);
});
});
+68
View File
@@ -0,0 +1,68 @@
import type { BOOK_GROUP_OP_SCHEMAS } from './book-group-ops.schema';
import {
groupElements,
groupMembers,
renameElement,
renameGroup,
setElementsHidden,
ungroupElements,
} from './book-groups';
import { failOp, pageElement, unlockedElement } from './book-ops-core';
import type { BookSpread } from './book.schema';
import type { z } from 'zod';
/**
* What `group`, `ungroup`, `name` and `hide` do to a page, for `applyBookOps`.
*
* Grouping moves the members next to each other in the paint order, so a
* locked element refuses it like any other change. Ungrouping, naming and
* hiding move nothing on the page and take locked elements too, as the
* Studio's layers panel does: a locked background can still be hidden to get
* at what is behind it.
*/
type GroupOp = z.infer<(typeof BOOK_GROUP_OP_SCHEMAS)[number]>;
interface GroupOpEnv {
newId: (prefix: string) => string;
opIndex: number;
}
const BOOK_GROUP_OP_NAMES: ReadonlySet<string> = new Set(['group', 'ungroup', 'name', 'hide']);
export function isGroupOp(op: { op: string }): op is GroupOp {
return BOOK_GROUP_OP_NAMES.has(op.op);
}
function existingGroup(spread: BookSpread, groupId: string, opIndex: number): void {
if (!groupMembers(spread, groupId).length) {
throw failOp('group_not_found', `no group ${groupId} on this page`, opIndex);
}
}
export function applyGroupOp(spread: BookSpread, op: GroupOp, env: GroupOpEnv): BookSpread {
switch (op.op) {
case 'group': {
const ids = [...new Set(op.elementIds)];
for (const id of ids) unlockedElement(pageElement(spread, id, env.opIndex), env.opIndex);
if (ids.length < 2) throw failOp('not_enough_elements', 'a group needs two different elements', env.opIndex);
return groupElements(spread, ids, env.newId('g'), op.name);
}
case 'ungroup':
existingGroup(spread, op.groupId, env.opIndex);
return ungroupElements(spread, op.groupId);
case 'name':
if (op.groupId !== undefined) {
existingGroup(spread, op.groupId, env.opIndex);
return renameGroup(spread, op.groupId, op.name);
}
pageElement(spread, op.elementId ?? '', env.opIndex);
return renameElement(spread, op.elementId ?? '', op.name);
case 'hide': {
const ids = [...new Set(op.elementIds)];
for (const id of ids) pageElement(spread, id, env.opIndex);
return setElementsHidden(spread, ids, op.hidden);
}
}
}
+6
View File
@@ -1,5 +1,6 @@
import { idSchema } from '../common/primitives.schema';
import { BOOK_ELEMENT_OP_SCHEMAS, TEXT_STYLE_FIELDS, bindingNamesItsSource } from './book-element-ops.schema';
import { BOOK_GROUP_OP_SCHEMAS } from './book-group-ops.schema';
import { BOOK_COVER_TEMPLATES, BOOK_TEMPLATES } from './book-layouts';
import {
BOOK_BINDING_HINT,
@@ -198,6 +199,7 @@ export const bookPageOpSchema = z.discriminatedUnion('op', [
})
.refine((v) => (v.color === undefined) !== (v.paint === undefined), { message: 'give the color or the paint' }),
...BOOK_ELEMENT_OP_SCHEMAS,
...BOOK_GROUP_OP_SCHEMAS,
]);
export type BookPageOp = z.infer<typeof bookPageOpSchema>;
export type BookPageOpInput = z.input<typeof bookPageOpSchema>;
@@ -459,6 +461,10 @@ export const bookElementSummarySchema = z.object({
photoId: z.number().int().nullable().optional(),
text: z.string().max(80).optional(),
binding: z.string().optional(),
/** The group it is in, its layer name, and whether it is hidden; absent when not. */
group: z.string().optional(),
name: z.string().optional(),
hidden: z.boolean().optional(),
});
export type BookElementSummary = z.infer<typeof bookElementSummarySchema>;
+5 -1
View File
@@ -1,5 +1,6 @@
import { withRealWeight } from './book-fonts';
import { bookGridElements, defaultBookGridBox, findBookGrid, type BookGridDef } from './book-grids';
import { tidyGroups } from './book-groups';
import { bookElementId } from './book-ids';
import { BOOK_TEXT_STYLES, applyBookTemplate, classifyBookTexts, findBookTemplate } from './book-layouts';
import {
@@ -21,6 +22,7 @@ import {
type BookPhotoInfo,
} from './book-ops-core';
import { applyElementOp, autoRelayout } from './book-ops-elements';
import { applyGroupOp, isGroupOp } from './book-ops-groups';
import { BOOK_OP_LIMITS, bookPageOpSchema, type BookPageOp } from './book-ops.schema';
import { withSpreadPaint } from './book-paint';
import { applySavedBookLayout, bookLayoutRole } from './book-saved-layouts';
@@ -88,6 +90,8 @@ export function applyBookOps(
env.opIndex = i;
spread = applyOne(spread, op, env);
});
// A removal or a layout can leave a group with one member: it goes, with its name.
spread = tidyGroups(spread);
checkedSpread(spread, null);
const spreads = doc.spreads.slice();
spreads[index] = spread;
@@ -125,7 +129,7 @@ function applyOne(spread: BookSpread, op: BookPageOp, env: Env): BookSpread {
op.paint !== undefined ? op.paint : op.color ? { type: 'solid', color: op.color } : null,
);
default:
return applyElementOp(spread, op, env);
return isGroupOp(op) ? applyGroupOp(spread, op, env) : applyElementOp(spread, op, env);
}
}
-6
View File
@@ -20,12 +20,6 @@ import { describe, expect, it } from 'vitest';
*/
const frame = { x: 12, y: 18, w: 60, h: 40 };
/** A revision newer than this build reads. */
const NEWER = BOOK_SCHEMA_REV + 1;
/** A revision one past what this build reads. */
const NEWER = BOOK_SCHEMA_REV + 1;
/** A revision above this build's, and an element kind no revision of it knows. */
const NEWER = BOOK_SCHEMA_REV + 1;
const fromLater = { id: 'x1', kind: 'hologram', frame, glow: 'lemon' };
+2 -1
View File
@@ -34,8 +34,9 @@ import type { BookDocument } from './book.schema';
* - 2: the wider type, new families and weights (book-fonts.data.ts).
* - 3: paint on shapes and pages (`paint`, `backgroundPaint`, book-paint.schema.ts).
* - 4: the sticker kind (book-sticker.ts, stickers/).
* - 5: groups, names and hidden layers (`group`, `name`, `hidden`, a spread's `groups`).
*/
export const BOOK_SCHEMA_REV = 4;
export const BOOK_SCHEMA_REV = 5;
/** The `code` a refused save carries in its body when the stored book is newer than the writer. */
export const BOOK_SCHEMA_NEWER_CODE = 'book_schema_newer';
+52
View File
@@ -0,0 +1,52 @@
import { bookElementStyle, bookStyleKeys, pastableStyle } from './book-style-copy';
import {
bookIconElementSchema,
bookListElementSchema,
bookShapeElementSchema,
bookTextElementSchema,
type BookElement,
} from './book.schema';
import { describe, expect, it } from 'vitest';
/** Copy and paste style: the look `style` may set, carried onto any kind that takes it. */
const frame = { x: 0, y: 0, w: 40, h: 20 };
const text = (over = {}) =>
bookTextElementSchema.parse({ id: 't', kind: 'text', frame, text: 'Hi', ...over }) as BookElement;
const icon = (over = {}) =>
bookIconElementSchema.parse({ id: 'i', kind: 'icon', frame, name: 'Sun', ...over }) as BookElement;
const shape = (over = {}) =>
bookShapeElementSchema.parse({ id: 's', kind: 'shape', frame, shape: 'rect', ...over }) as BookElement;
describe('copy style', () => {
it('BKSTY-001: takes the look of an element and none of what it says or where it is', () => {
const style = bookElementStyle(text({ size: 24, color: '#ff0000', weight: 700, opacity: 0.5 }));
expect(style.kind).toBe('text');
expect(style.props).toMatchObject({ size: 24, color: '#ff0000', weight: 700, opacity: 0.5 });
expect(style.props).not.toHaveProperty('text');
expect(style.props).not.toHaveProperty('frame');
});
it('BKSTY-002: leaves out the props of `style` that are words rather than looks', () => {
const list = bookListElementSchema.parse({ id: 'l', kind: 'list', frame, proLabel: 'Good' }) as BookElement;
expect(bookStyleKeys('list')).not.toContain('proLabel');
expect(bookElementStyle(list).props).not.toHaveProperty('proLabel');
expect(bookStyleKeys('stats')).not.toContain('metrics');
});
it('BKSTY-003: pastes all of it onto the same kind, and what both have onto another', () => {
const style = bookElementStyle(text({ size: 24, color: '#ff0000', opacity: 0.5 }));
expect(pastableStyle(style, 'text')).toMatchObject({ kind: 'text', size: 24, color: '#ff0000' });
// An icon has a colour and an opacity, no type size.
const onIcon = pastableStyle(style, 'icon');
expect(onIcon).toEqual({ kind: 'icon', color: '#ff0000', opacity: 0.5 });
});
it('BKSTY-004: answers null when the two kinds share nothing that was set', () => {
const style = { kind: 'icon' as const, props: { lineWidth: 2 } };
expect(pastableStyle(style, 'shape')).toBeNull();
expect(pastableStyle(bookElementStyle(shape({ fill: '#00ff00' })), 'shape')).toMatchObject({ fill: '#00ff00' });
expect(pastableStyle(bookElementStyle(icon({ color: '#123456' })), 'icon')).toMatchObject({ color: '#123456' });
});
});
+58
View File
@@ -0,0 +1,58 @@
import { bookElementPropsSchema, type BookElementProps } from './book-element-ops.schema';
import type { BookElement } from './book.schema';
/**
* Copy and paste style: the look of one element taken onto others.
*
* The look is exactly what the `style` operation may set for the element's
* kind (book-element-ops.schema.ts), less the few props that are words rather
* than looks (a list's column labels, the figures a stats block shows). So a
* pasted style is one `style` operation per element, the same thing the
* assistant sends, and onto an element of another kind it carries what both
* kinds have: a text's colour onto an icon, the opacity onto anything.
*/
export interface BookCopiedStyle {
kind: BookElement['kind'];
props: Record<string, unknown>;
}
/** Props of `style` that say what an element shows, not how it looks. */
const CONTENT_KEYS: ReadonlySet<string> = new Set(['metrics', 'proLabel', 'conLabel']);
const STYLE_KEYS = new Map<string, string[]>(
bookElementPropsSchema.options.map((o) => [
o.shape.kind.value,
Object.keys(o.shape).filter((k) => k !== 'kind' && !CONTENT_KEYS.has(k)),
]),
);
/** The look fields an element of a kind takes from a pasted style. */
export function bookStyleKeys(kind: BookElement['kind']): readonly string[] {
return STYLE_KEYS.get(kind) ?? [];
}
/** The look of an element, as the clipboard keeps it. */
export function bookElementStyle(el: BookElement): BookCopiedStyle {
const source = el as unknown as Record<string, unknown>;
const props: Record<string, unknown> = {};
for (const key of bookStyleKeys(el.kind)) if (source[key] !== undefined) props[key] = source[key];
return { kind: el.kind, props };
}
/**
* The `style` operation's `set` for pasting a copied look onto an element of
* `kind`: what both kinds have, or null when they share nothing.
*/
export function pastableStyle(style: BookCopiedStyle, kind: BookElement['kind']): BookElementProps | null {
const set: Record<string, unknown> = { kind };
let any = false;
for (const key of bookStyleKeys(kind)) {
if (style.props[key] === undefined) continue;
set[key] = style.props[key];
any = true;
}
if (!any) return null;
const parsed = bookElementPropsSchema.safeParse(set);
return parsed.success ? parsed.data : null;
}
+27
View File
@@ -171,6 +171,10 @@ export type BookShapeId = (typeof BOOK_SHAPES)[number];
export { BOOK_FONTS_IDS, type BookFontFamily };
const bookWeightSchema = z.literal(BOOK_WEIGHTS);
/** How long a group id and a layer's name may be. */
export const BOOK_GROUP_ID_MAX = 40;
export const BOOK_LAYER_NAME_MAX = 60;
export const bookFrameSchema = z.object({
/** Millimetres from the left edge of the spread. Negative means bleed. */
x: mm,
@@ -188,6 +192,16 @@ const elementBase = {
rotation: z.number().finite().default(0),
opacity: z.number().min(0).max(1).default(1),
locked: z.boolean().default(false),
/**
* The group the element belongs to on its spread, by the group's id (see
* book-groups.ts). Absent for an element on its own. Optional rather than
* defaulted, so a book without groups stores what it stored before.
*/
group: z.string().min(1).max(BOOK_GROUP_ID_MAX).optional(),
/** What the layers panel calls it. Absent means the name of its kind. */
name: z.string().max(BOOK_LAYER_NAME_MAX).optional(),
/** Kept in the layers panel but left off the page, on screen and in print. */
hidden: z.boolean().optional(),
};
export const bookPhotoElementSchema = z.object({
@@ -840,6 +854,13 @@ export type BookBadgeElement = z.infer<typeof bookBadgeElementSchema>;
export type BookListElement = z.infer<typeof bookListElementSchema>;
export type BookUnits = z.infer<typeof bookUnitsSchema>;
/** A named group of a spread. */
export const bookGroupSchema = z.object({
id: z.string().min(1).max(BOOK_GROUP_ID_MAX),
name: z.string().max(BOOK_LAYER_NAME_MAX).optional(),
});
export type BookGroup = z.infer<typeof bookGroupSchema>;
export const bookSpreadSchema = z.object({
id: z.string().min(1),
/**
@@ -870,6 +891,12 @@ export const bookSpreadSchema = z.object({
parked: z.array(bookElementSchema).max(MAX_SPREAD_ELEMENTS).default([]),
/** The journey entry this spread was generated from, if any. */
entryId: z.number().int().nullable().default(null),
/**
* The names of the spread's groups. Membership is on the elements (`group`);
* a group without a row here is simply unnamed. A list that does not read is
* dropped rather than the page.
*/
groups: z.array(bookGroupSchema).max(MAX_SPREAD_ELEMENTS).optional().catch(undefined),
});
export type BookSpread = z.infer<typeof bookSpreadSchema>;
+21
View File
@@ -2,6 +2,7 @@ import { photoDpi, spreadFindings } from './book-findings';
import { designFindings, sortBookFindings } from './book-findings-design';
import { bookFontRole, bookWeightWord, nearestBookWeight } from './book-fonts';
import { BOOK_GRIDS } from './book-grids';
import { bookGroupName, groupMembers, spreadGroupIds } from './book-groups';
import { BOOK_COVER_TEMPLATES, BOOK_TEMPLATES, type BookTemplate } from './book-layouts';
import type { BookPhotoInfo } from './book-ops-core';
import type {
@@ -150,11 +151,26 @@ function elementLine(el: BookElement, i: number, photos: readonly BookPhotoInfo[
el.rotation ? `turned ${n(el.rotation)} deg` : '',
el.opacity < 1 ? `opacity ${n(el.opacity)}` : '',
el.locked ? 'locked' : '',
el.hidden ? 'hidden (not on the page or in print)' : '',
el.group ? `in group ${el.group}` : '',
el.name ? `named "${head(el.name, 40)}"` : '',
].filter(Boolean);
const tail = extra.length ? `; ${extra.join(', ')}` : '';
return `${i + 1}. ${el.kind} ${el.id} at ${n(x)}, ${n(y)}, ${n(w)} x ${n(h)}: ${elementWords(el, photos)}${tail}`;
}
/** The page's groups by id, with their names and members, so a model can move or align one as a whole. */
function groupsLine(spread: BookSpread): string | null {
const ids = spreadGroupIds(spread);
if (!ids.length) return null;
const rows = ids.map((id) => {
const name = bookGroupName(spread, id);
const members = groupMembers(spread, id).map((e) => e.id);
return `${id}${name ? ` "${head(name, 40)}"` : ''} (${members.join(', ')})`;
});
return `Groups, each picked, moved, aligned and arranged as one: ${rows.join('; ')}`;
}
function parkedLine(spread: BookSpread): string | null {
if (!spread.parked.length) return null;
const items = spread.parked
@@ -200,6 +216,9 @@ function elementSummary(el: BookElement): BookElementSummary {
...(el.kind === 'photo' ? { photoId: el.photoId } : {}),
...(el.kind === 'text' ? { text: head(el.text, 80) } : {}),
...(el.kind === 'text' && el.binding ? { binding: el.binding.source } : {}),
...(el.group ? { group: el.group } : {}),
...(el.name ? { name: el.name } : {}),
...(el.hidden ? { hidden: true } : {}),
};
}
@@ -238,6 +257,8 @@ export function describeSpread(
} else {
lines.push('The page is empty.');
}
const groups = groupsLine(spread);
if (groups) lines.push(groups);
const parked = parkedLine(spread);
if (parked) lines.push(parked);
lines.push(findings.length ? 'Findings:' : 'Findings: none.');