fix(studio): paste style, the page background, layouts and fill page keep the engine's rules

Paste style wrote the copied fields straight onto the element, although a
pasted look is meant to be the style operation the assistant sends. It now
runs the engine's own restyle (bookElementStyled): a colour or face pasted by
hand takes the element off the theme, so the next theme applied no longer
brings the old colour back; a plain fill replaces the paint of the shape it
lands on instead of staying hidden under it; and a line takes another line's
look without its points, which are its course, not its look.

A background picked by hand in the inspector kept the page's link to the
theme, so the next theme applied set the page back to the theme's colour. The
picker and the background operation now paint through one helper that takes
the link away (withSpreadPaintByHand). Giving one page's background to every
page carries that page's link along, or takes the others' away when it was
painted by hand.

A built-in layout, a book's own layout and laying a page out again from its
entry ignored locks in the Studio: a locked shape was dropped and a locked
photo poured into a slot unlocked, while the assistant's layout operation left
both where they were. All four now lay a page out around its locked elements
through one function (layoutAroundLocks), which the engine's layout operation
uses too.

Fill page and fill spread kept the photo in its group, so the background
picture was selected and dragged with its caption and listed under the group
at the front. It leaves its group as the engine's fill does, and a group left
with one member goes.
This commit is contained in:
Maurice
2026-10-24 04:06:27 +00:00
parent 090e754077
commit 84e18e4954
22 changed files with 397 additions and 47 deletions
+60
View File
@@ -0,0 +1,60 @@
import { layoutAroundLocks } from './book-layout-locks';
import {
MAX_SPREAD_ELEMENTS,
bookShapeElementSchema,
bookSpreadSchema,
type BookElement,
type BookSpread,
} from './book.schema';
import { describe, expect, it } from 'vitest';
/** A page laid out again around its locked elements, for the layout operation and the Studio alike. */
const shape = (id: string, over: Record<string, unknown> = {}) =>
bookShapeElementSchema.parse({ id, kind: 'shape', frame: { x: 0, y: 0, w: 10, h: 10 }, ...over }) as BookElement;
const page = (elements: BookElement[], over: Record<string, unknown> = {}): BookSpread =>
bookSpreadSchema.parse({ id: 'sp', role: 'inner', elements, ...over });
/** A layout that throws away what it is given and lays `n` new shapes. */
const fresh =
(n: number) =>
(free: BookSpread): BookSpread => ({
...free,
groups: undefined,
elements: Array.from({ length: n }, (_, i) => shape(`new-${i}`)),
});
describe('a layout around locked elements', () => {
it('BKLOCK-001: lays out only what is free, and puts each locked element back at its place in the paint order', () => {
const seen: string[][] = [];
const out = layoutAroundLocks(
page([shape('back', { locked: true }), shape('a'), shape('front', { locked: true }), shape('b')]),
(free) => {
seen.push(free.elements.map((e) => e.id));
return fresh(3)(free);
},
);
expect(seen).toEqual([['a', 'b']]);
expect(out?.elements.map((e) => e.id)).toEqual(['back', 'new-0', 'front', 'new-1', 'new-2']);
});
it('BKLOCK-002: keeps the name of a locked group the layout wrote away', () => {
const out = layoutAroundLocks(
page([shape('m1', { locked: true, group: 'g' }), shape('m2', { locked: true, group: 'g' }), shape('a')], {
groups: [{ id: 'g', name: 'Stamp' }],
}),
fresh(1),
);
expect(out?.groups).toEqual([{ id: 'g', name: 'Stamp' }]);
expect(out?.elements.map((e) => e.id)).toEqual(['m1', 'm2', 'new-0']);
});
it('BKLOCK-003: answers null when the layout lays nothing, or would overfill the page with the locked ones', () => {
const locked = page([shape('l', { locked: true }), shape('a')]);
expect(layoutAroundLocks(locked, () => null)).toBeNull();
expect(layoutAroundLocks(locked, fresh(MAX_SPREAD_ELEMENTS))).toBeNull();
expect(layoutAroundLocks(locked, fresh(MAX_SPREAD_ELEMENTS - 1))?.elements).toHaveLength(MAX_SPREAD_ELEMENTS);
});
});
+27
View File
@@ -0,0 +1,27 @@
import { MAX_SPREAD_ELEMENTS, type BookSpread } from './book.schema';
/**
* A page laid out again around its locked elements.
*
* A lock protects a finished part of a page while the rest is reworked, so a
* layout arranges only what is not locked: every locked element stays where
* it is, at its place in the paint order, and a locked group keeps its name
* through a layout that writes the page's groups anew. One rule for the
* assistant's `layout` operation and for the Studio's layouts, so a lock holds
* whichever of them lays the page out.
*
* `lay` gets the page without its locked elements and answers it laid out,
* or null when it lays nothing. The answer is null too when the layout and
* the locked elements together would not fit on one page.
*/
export function layoutAroundLocks(spread: BookSpread, lay: (free: BookSpread) => BookSpread | null): BookSpread | null {
const locked = spread.elements.flatMap((el, i) => (el.locked ? [{ el, i }] : []));
const next = lay({ ...spread, elements: spread.elements.filter((e) => !e.locked) });
if (!next) return null;
const elements = next.elements.slice();
for (const { el, i } of locked) elements.splice(Math.min(i, elements.length), 0, el);
if (elements.length > MAX_SPREAD_ELEMENTS) return null;
const have = new Set((next.groups ?? []).map((g) => g.id));
const lost = (spread.groups ?? []).filter((g) => !have.has(g.id) && locked.some(({ el }) => el.group === g.id));
return { ...next, elements, ...(lost.length ? { groups: [...(next.groups ?? []), ...lost] } : {}) };
}
+7
View File
@@ -179,6 +179,13 @@ function withProps(el: BookElement, props: Record<string, unknown>, theme?: Book
return withThemeLinks(made.kind === 'shape' ? withPaintProps(made, props) : made, props, theme);
}
/**
* An element restyled exactly as the `style` operation restyles it, for the
* Studio's paste style: one restyle, so a pasted look and the assistant's
* keep a page's theme links, a shape's paint and a line's course alike.
*/
export const bookElementStyled = withProps;
/**
* A shape's paint, kept in step with its fill: a paint given is written
* through withShapePaint, and a plain fill or fade given without one replaces
+10 -18
View File
@@ -2,6 +2,7 @@ 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 { layoutAroundLocks } from './book-layout-locks';
import { BOOK_TEXT_STYLES, applyBookTemplate, classifyBookTexts, findBookTemplate } from './book-layouts';
import {
bookBoundWords,
@@ -24,7 +25,7 @@ import {
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 { withSpreadPaintByHand } from './book-paint';
import { applySavedBookLayout, bookLayoutRole } from './book-saved-layouts';
import { fitSpreadTexts } from './book-text-fit';
import {
@@ -128,14 +129,12 @@ function applyOne(spread: BookSpread, op: BookPageOp, env: Env): BookSpread {
return opFrame(spread, op, env);
case 'remove':
return opRemove(spread, op, env);
case 'background': {
// A colour set by hand stops following the theme's paper.
const { backgroundToken: _unlinked, ...rest } = spread;
return withSpreadPaint(
rest,
case 'background':
// A colour set by hand stops following the theme's paper, as in the Studio.
return withSpreadPaintByHand(
spread,
op.paint !== undefined ? op.paint : op.color ? { type: 'solid', color: op.color } : null,
);
}
default:
return isGroupOp(op) ? applyGroupOp(spread, op, env) : applyElementOp(spread, op, env);
}
@@ -175,19 +174,12 @@ const spreadWidth = (spread: BookSpread, page: BookPageSetup) => page.pageWidth
function opLayout(spread: BookSpread, op: Op<'layout'>, env: Env): BookSpread {
// A locked element is left where it is, and keeps its place in the paint
// order; the layout arranges everything else.
const locked = spread.elements.flatMap((el, i) => (el.locked ? [{ el, i }] : []));
const free: BookSpread = { ...spread, elements: spread.elements.filter((e) => !e.locked) };
const next = layoutFree(free, op, env);
const elements = next.elements.slice();
for (const { el, i } of locked) elements.splice(Math.min(i, elements.length), 0, el);
if (elements.length > MAX_SPREAD_ELEMENTS) {
// order; the layout arranges everything else, as in the Studio.
const laid = layoutAroundLocks(spread, (free) => layoutFree(free, op, env));
if (!laid) {
throw failOp('too_many_elements', `the layout and the locked elements exceed ${MAX_SPREAD_ELEMENTS}`, env.opIndex);
}
// A locked group keeps its name, also through a layout that writes the page's groups anew.
const have = new Set((next.groups ?? []).map((g) => g.id));
const lost = (spread.groups ?? []).filter((g) => !have.has(g.id) && locked.some(({ el }) => el.group === g.id));
return { ...next, elements, ...(lost.length ? { groups: [...(next.groups ?? []), ...lost] } : {}) };
return laid;
}
function layoutFree(spread: BookSpread, op: Op<'layout'>, env: Env): BookSpread {
+14 -1
View File
@@ -12,6 +12,7 @@ import {
spreadPaint,
withShapePaint,
withSpreadPaint,
withSpreadPaintByHand,
} from './book-paint';
import { BOOK_PATTERNS, bookPaintInSchema, bookPaintSchema, type BookPaint } from './book-paint.schema';
import { applySavedBookLayout, bookLayoutFromSpread, bookLayoutPreview } from './book-saved-layouts';
@@ -28,7 +29,7 @@ import { describeSpread } from './describe-spread';
import { describe, expect, it } from 'vitest';
// BKPAINT-001 to BKPAINT-016
// BKPAINT-001 to BKPAINT-017
/**
* Paint: the document's gradients, alpha and patterns, how they stay readable
@@ -187,6 +188,18 @@ describe('a page and its paint', () => {
const replaced = applySavedBookLayout(spread({ backgroundPaint: linear }), plainLayout, page);
expect(['backgroundPaint' in replaced, replaced.background]).toEqual([false, '#111111']);
});
it('BKPAINT-017: a paint set by hand takes the page off its theme colour, Studio and assistant alike', () => {
const linked = spread({ background: '#f4efe6', backgroundToken: 'paper' });
expect(withSpreadPaint(linked, stripes).backgroundToken).toBe('paper');
const painted = withSpreadPaintByHand(linked, stripes);
expect(painted).toMatchObject({ backgroundPaint: stripes, background: '#ffeedd' });
expect('backgroundToken' in painted).toBe(false);
const result = applyBookOps(doc([linked]), { spreadId: 'sp1', ops: [{ op: 'background', color: '#202020' }] });
if (!result.ok) throw new Error(result.error.message);
expect(result.doc.spreads[0]).toMatchObject({ background: '#202020' });
expect(result.doc.spreads[0]?.backgroundToken).toBeUndefined();
});
});
describe('what a paint is in words', () => {
+11
View File
@@ -140,6 +140,17 @@ export function withSpreadPaint<T extends BookSpread>(spread: T, paint: BookPain
return { ...rest, backgroundPaint: paint, background: paintFallbackHex(paint) } as T;
}
/**
* A page painted by hand: this paint, and the page's link to a theme colour
* (`backgroundToken`) taken away, so the next theme applied leaves the colour
* somebody chose. The `background` operation and the Studio's background
* picker both paint a page through it.
*/
export function withSpreadPaintByHand(spread: BookSpread, paint: BookPaint | null): BookSpread {
const { backgroundToken: _unlinked, ...rest } = spread;
return withSpreadPaint(rest, paint);
}
/** Every colour a paint is made of, in the order it names them. */
export function paintColours(paint: BookPaint | null | undefined): string[] {
if (!paint) return [];
+19
View File
@@ -1,6 +1,7 @@
import { bookElementStyle, bookStyleKeys, pastableStyle } from './book-style-copy';
import {
bookIconElementSchema,
bookLineElementSchema,
bookListElementSchema,
bookShapeElementSchema,
bookTextElementSchema,
@@ -35,6 +36,24 @@ describe('copy style', () => {
expect(bookStyleKeys('stats')).not.toContain('metrics');
});
it("BKSTY-005: takes a line's look and never its points, which are where it runs", () => {
const route = bookLineElementSchema.parse({
id: 'r',
kind: 'line',
frame,
points: [
{ x: 0, y: 1 },
{ x: 0.5, y: 0 },
{ x: 1, y: 1 },
],
stroke: '#c2410c',
dash: 'dashed',
}) as BookElement;
expect(bookStyleKeys('line')).not.toContain('points');
expect(bookElementStyle(route).props).toMatchObject({ stroke: '#c2410c', dash: 'dashed' });
expect(bookElementStyle(route).props).not.toHaveProperty('points');
});
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' });
+11 -7
View File
@@ -5,11 +5,12 @@ 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.
* kind (book-element-ops.schema.ts), less the few props that are words or a
* course rather than looks (a list's column labels, the figures a stats block
* shows, a line's points). So a pasted style is one `style` operation per
* element, applied by the same restyle (`bookElementStyled`), 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 {
@@ -17,8 +18,11 @@ export interface BookCopiedStyle {
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']);
/**
* Props of `style` that say what an element shows or where it runs, not how it
* looks: a line's points are its course, so pasting a look never redraws one.
*/
const CONTENT_KEYS: ReadonlySet<string> = new Set(['metrics', 'proLabel', 'conLabel', 'points']);
const STYLE_KEYS = new Map<string, string[]>(
bookElementPropsSchema.options.map((o) => [
+2
View File
@@ -133,6 +133,8 @@ export * from './book/book-line.schema';
export * from './book/book-line';
export * from './book/book-line-ops.schema';
export { bookNewLine, bookLineStyled } from './book/book-ops-line';
export { bookElementStyled } from './book/book-ops-elements';
export { layoutAroundLocks } from './book/book-layout-locks';
export { bookSpreadPhotoIds } from './book/book-photo-refs';
export * from './book/book-normalize';
export * from './book/book-schema-rev';