From 00018959c60731c86c80c48137abf68d166118e4 Mon Sep 17 00:00:00 2001 From: Maurice Date: Sat, 24 Oct 2026 04:06:52 +0000 Subject: [PATCH] fix(studio): a full page takes no new element, the opacity slider commits a move only, a theme draft survives saves Every add in the Studio (a sticker, a shape, an icon, a text, a frame, a line, a travel element, a photo from the content panel or a drop), a text style and a grid of frames appended without looking at the page's cap of 90 elements, which paste and duplicate already kept. The save then failed with a 400 and every later save of the book failed the same way. Each of them now leaves a full page as it is and selects nothing, as a paste does. The opacity slider committed on every key let go on it, so the Tab that moved the focus there wrote the value it was last moved to, an empty undo step at best and another colour's opacity at worst. It commits once after it moved, on the key or the pointer let go or when the focus leaves. The Theme panel threw away colours, faces and motifs being tried whenever the book came back from a save by another editor or the assistant, because the same theme read anew is a new object. The draft now follows the book's theme only when the theme itself changed. --- client/src/components/Studio/StudioCanvas.tsx | 1 - .../Studio/StudioElementsPanel.test.tsx | 40 ++++++++++++- .../components/Studio/StudioElementsPanel.tsx | 8 ++- .../components/Studio/StudioSwatches.test.tsx | 59 +++++++++++++++++++ .../src/components/Studio/StudioSwatches.tsx | 10 +++- .../Studio/StudioTextStyles.test.tsx | 19 +++++- .../components/Studio/StudioTextStyles.tsx | 5 +- .../components/Studio/theme/useStudioTheme.ts | 21 +++++-- client/src/store/studioStore.ts | 7 ++- .../tests/unit/studio/paintPalette.test.tsx | 3 +- .../studio/studioClipboardLayouts.test.ts | 15 ++++- client/tests/unit/studio/studioTheme.test.tsx | 20 ++++++- 12 files changed, 190 insertions(+), 18 deletions(-) create mode 100644 client/src/components/Studio/StudioSwatches.test.tsx diff --git a/client/src/components/Studio/StudioCanvas.tsx b/client/src/components/Studio/StudioCanvas.tsx index 6ea61b9bc..1f90f531e 100644 --- a/client/src/components/Studio/StudioCanvas.tsx +++ b/client/src/components/Studio/StudioCanvas.tsx @@ -235,7 +235,6 @@ export function StudioCanvas({ photoId, fit: 'cover', focalX: 0.5, focalY: 0.5, radius: 0, filter: 'none', mask: null, frameStyle: 'none', } as BookElement) - select([id]) }} > {/* diff --git a/client/src/components/Studio/StudioElementsPanel.test.tsx b/client/src/components/Studio/StudioElementsPanel.test.tsx index 7d7466dc0..73a98e6d5 100644 --- a/client/src/components/Studio/StudioElementsPanel.test.tsx +++ b/client/src/components/Studio/StudioElementsPanel.test.tsx @@ -1,5 +1,5 @@ import type { BookDocument } from '@trek/shared'; -import { BOOK_GRIDS, bookPageSetupSchema, normalizeBookDocument } from '@trek/shared'; +import { BOOK_GRIDS, MAX_SPREAD_ELEMENTS, bookPageSetupSchema, normalizeBookDocument } from '@trek/shared'; import { beforeEach, describe, expect, it } from 'vitest'; import { fireEvent, render, screen } from '../../../tests/helpers/render'; @@ -10,9 +10,9 @@ import { DIVIDER_PRESETS, LINE_PRESETS } from './linePresets'; import { matchStickers } from './stickerLibrary'; /** - * FE-STUDIO-ELEMENTS-001 to FE-STUDIO-ELEMENTS-006: the Elements panel's six + * FE-STUDIO-ELEMENTS-001 to FE-STUDIO-ELEMENTS-007: the Elements panel's six * tabs, each opening its own shelves, and each placing what it shows on the - * open page. + * open page, as long as the page has room for it. */ const t = (k: string) => k; @@ -78,6 +78,40 @@ describe('the Elements panel', () => { expect(useStudioStore.getState().selection).toHaveLength(grid.cells.length); }); + it('FE-STUDIO-ELEMENTS-007: places nothing on a page that holds all it can, a frame or a whole grid', () => { + const grid = BOOK_GRIDS.find((g) => g.cells.length > 1); + if (!grid) throw new Error('no grid of several frames'); + const crowded = (n: number) => + normalizeBookDocument({ + ...book(), + spreads: [ + { + id: 'sp1', + role: 'inner', + entryId: null, + background: null, + parked: [], + elements: Array.from({ length: n }, (_, i) => ({ + id: `s${i}`, + kind: 'shape', + frame: { x: 0, y: 0, w: 5, h: 5 }, + })), + }, + ], + }); + useStudioStore.getState().load(crowded(MAX_SPREAD_ELEMENTS)); + render(); + open('journey.studio.frames'); + fireEvent.click(screen.getByRole('button', { name: 'journey.studio.polaroidFrame' })); + expect(elements()).toHaveLength(MAX_SPREAD_ELEMENTS); + expect(useStudioStore.getState().selection).toEqual([]); + + useStudioStore.getState().load(crowded(MAX_SPREAD_ELEMENTS - 1)); + fireEvent.click(screen.getByRole('button', { name: grid.id })); + expect(elements()).toHaveLength(MAX_SPREAD_ELEMENTS - 1); + expect(useStudioStore.getState().canUndo()).toBe(false); + }); + it('FE-STUDIO-ELEMENTS-005: searches the icons and the stickers above their grids', () => { render(); open('journey.studio.icons'); diff --git a/client/src/components/Studio/StudioElementsPanel.tsx b/client/src/components/Studio/StudioElementsPanel.tsx index e1ce5a895..110b043eb 100644 --- a/client/src/components/Studio/StudioElementsPanel.tsx +++ b/client/src/components/Studio/StudioElementsPanel.tsx @@ -3,7 +3,10 @@ import { Frame, Minus, Quote, Shapes, Spline, Sparkles, Square, Sticker, Type, t import type { BookElement, BookPageSetup, BookShapeId } from '@trek/shared' import { useStudioStore } from '../../store/studioStore' import { FRAME_SHAPES, HOLED_SHAPES, SHAPE_GROUPS } from './shapes' -import { BOOK_GRIDS as GRIDS, bookElementId, bookShapeSize, bookGridElements as gridElements, defaultBookGridBox as defaultGridBox } from '@trek/shared' +import { + BOOK_GRIDS as GRIDS, MAX_SPREAD_ELEMENTS, bookElementId, bookShapeSize, bookGridElements as gridElements, + defaultBookGridBox as defaultGridBox, +} from '@trek/shared' import { shapeTile } from './shapeOutline' import { StudioLineShelf } from './StudioLineShelf' import { PanelHead, Shelf } from './StudioPanelHead' @@ -190,6 +193,9 @@ function useElementAdders(page: BookPageSetup) { /** A block of empty frames, selected as a group so it moves and resizes as one thing straight away. */ const addGrid = (grid: (typeof GRIDS)[number]) => { const els = gridElements(grid, defaultGridBox(page, single)) + // The page's own cap, as a duplicate keeps it: frames that would make the book unsaveable are not added. + const on = useStudioStore.getState().doc?.spreads[active] + if (!on || on.elements.length + els.length > MAX_SPREAD_ELEMENTS) return commit(d => ({ ...d, spreads: d.spreads.map((sp, i) => (i !== active ? sp : { ...sp, elements: [...sp.elements, ...els] })), diff --git a/client/src/components/Studio/StudioSwatches.test.tsx b/client/src/components/Studio/StudioSwatches.test.tsx new file mode 100644 index 000000000..8899e0946 --- /dev/null +++ b/client/src/components/Studio/StudioSwatches.test.tsx @@ -0,0 +1,59 @@ +import { describe, expect, it, vi } from 'vitest'; + +import { fireEvent, render } from '../../../tests/helpers/render'; +import { AlphaSlider } from './StudioSwatches'; + +/** + * The opacity slider: a move is shown while it is made and committed once + * when it is let go, and a key let go on it that moved nothing (the Tab that + * brought the focus there) commits nothing, whatever value it last moved to. + */ + +function slider(value: number) { + const onChange = vi.fn(); + const view = render(); + const input = view.getByLabelText('Opacity') as HTMLInputElement; + return { ...view, onChange, input }; +} + +describe('the opacity slider', () => { + it('commits nothing when the focus arrives on it with Tab', () => { + const { input, onChange } = slider(1); + fireEvent.keyUp(input, { key: 'Tab' }); + fireEvent.blur(input); + expect(onChange).not.toHaveBeenCalled(); + }); + + it('commits an arrow key once, at the value it moved to', () => { + const { input, onChange } = slider(1); + fireEvent.keyDown(input, { key: 'ArrowLeft' }); + fireEvent.change(input, { target: { value: '99' } }); + fireEvent.keyUp(input, { key: 'ArrowLeft' }); + fireEvent.keyUp(input, { key: 'Tab' }); + expect(onChange.mock.calls).toEqual([ + [0.99, false], + [0.99, true], + ]); + }); + + it('does not take an old move to a value given since', () => { + const { input, onChange, rerender } = slider(0.4); + fireEvent.pointerDown(input); + fireEvent.change(input, { target: { value: '30' } }); + fireEvent.pointerUp(input); + expect(onChange).toHaveBeenLastCalledWith(0.3, true); + onChange.mockClear(); + // The same slider, now showing another colour's opacity: tabbing onto it changes nothing. + rerender(); + fireEvent.keyUp(input, { key: 'Tab' }); + expect(onChange).not.toHaveBeenCalled(); + }); + + it('commits a move the focus leaves before any key or pointer is let go', () => { + const { input, onChange } = slider(1); + fireEvent.keyDown(input, { key: 'ArrowLeft' }); + fireEvent.change(input, { target: { value: '80' } }); + fireEvent.blur(input); + expect(onChange).toHaveBeenLastCalledWith(0.8, true); + }); +}); diff --git a/client/src/components/Studio/StudioSwatches.tsx b/client/src/components/Studio/StudioSwatches.tsx index 0847f9534..edc82acbe 100644 --- a/client/src/components/Studio/StudioSwatches.tsx +++ b/client/src/components/Studio/StudioSwatches.tsx @@ -116,11 +116,18 @@ export function AlphaSlider({ color, value, onChange, label }: { const pct = Math.round(value * 100) // What the slider was last moved to: a controlled input may still show the old value when it is let go. const moved = useRef(value) + // Whether it moved since the last commit: a key let go on it (the Tab that brought the focus) is no change. + const dirty = useRef(false) const move = (raw: string) => { moved.current = Math.max(0, Math.min(100, Number(raw))) / 100 + dirty.current = true onChange(moved.current, false) } - const finish = () => onChange(moved.current, true) + const finish = () => { + if (!dirty.current) return + dirty.current = false + onChange(moved.current, true) + } return (
@@ -136,6 +143,7 @@ export function AlphaSlider({ color, value, onChange, label }: { onChange={e => move(e.target.value)} onPointerUp={finish} onKeyUp={finish} + onBlur={finish} />
{pct}% diff --git a/client/src/components/Studio/StudioTextStyles.test.tsx b/client/src/components/Studio/StudioTextStyles.test.tsx index 3f76ff4c8..2d1695f0c 100644 --- a/client/src/components/Studio/StudioTextStyles.test.tsx +++ b/client/src/components/Studio/StudioTextStyles.test.tsx @@ -1,7 +1,7 @@ import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest'; import type { BookDocument, BookTextElement } from '@trek/shared'; -import { BOOK_LOCKUPS, bookPageSetupSchema, normalizeBookDocument } from '@trek/shared'; +import { BOOK_LOCKUPS, MAX_SPREAD_ELEMENTS, bookPageSetupSchema, normalizeBookDocument } from '@trek/shared'; import { fireEvent, render, screen } from '../../../tests/helpers/render'; import { useStudioStore } from '../../store/studioStore'; @@ -73,6 +73,23 @@ describe('the Text styles shelf', () => { expect(elements()).toEqual([]); }); + it('places nothing on a page without room for the whole set, so the book stays saveable', () => { + const shapes = Array.from({ length: MAX_SPREAD_ELEMENTS - 2 }, (_, i) => ({ + id: `s${i}`, + kind: 'shape', + frame: { x: 0, y: 0, w: 5, h: 5 }, + })); + const crowded = book(); + useStudioStore + .getState() + .load(normalizeBookDocument({ ...crowded, spreads: crowded.spreads.map((sp) => ({ ...sp, elements: shapes })) })); + render(); + fireEvent.click(screen.getByRole('button', { name: 'journey.studio.lockup.circle-badge' })); + expect(elements()).toHaveLength(MAX_SPREAD_ELEMENTS - 2); + expect(useStudioStore.getState().canUndo()).toBe(false); + expect(useStudioStore.getState().selection).toEqual([]); + }); + it('opens the Elements panel on the type tab, the plain styles above the lockups', () => { render(); expect(screen.getByRole('tab', { name: 'journey.studio.text' }).getAttribute('aria-selected')).toBe('true'); diff --git a/client/src/components/Studio/StudioTextStyles.tsx b/client/src/components/Studio/StudioTextStyles.tsx index 8ead78685..87884cc0f 100644 --- a/client/src/components/Studio/StudioTextStyles.tsx +++ b/client/src/components/Studio/StudioTextStyles.tsx @@ -1,7 +1,7 @@ import { useMemo } from 'react'; import type { BookLockup, BookLockupWords, BookPageSetup } from '@trek/shared'; -import { addBookLockup, bookElementId } from '@trek/shared'; +import { MAX_SPREAD_ELEMENTS, addBookLockup, bookElementId } from '@trek/shared'; import { useElementSize } from '../../hooks/useElementSize'; import { useStudioStore } from '../../store/studioStore'; @@ -98,7 +98,8 @@ export function StudioTextStyles({ const place = (lockup: BookLockup) => { const els = placeLockup(lockup, { page, spread, words, gallery: source.photos, newId: bookElementId }); - if (!els.length) return; + // The page's own cap, as a duplicate keeps it: a set that would make the book unsaveable is not placed. + if (!els.length || !spread || spread.elements.length + els.length > MAX_SPREAD_ELEMENTS) return; commit((d) => ({ ...d, spreads: d.spreads.map((sp, i) => (i === active ? addBookLockup(sp, els, bookElementId('g')) : sp)), diff --git a/client/src/components/Studio/theme/useStudioTheme.ts b/client/src/components/Studio/theme/useStudioTheme.ts index cce8619bb..7bfa9b636 100644 --- a/client/src/components/Studio/theme/useStudioTheme.ts +++ b/client/src/components/Studio/theme/useStudioTheme.ts @@ -2,6 +2,7 @@ import { BOOK_THEME_IDS, bookPaletteFromPhotos, bookThemeOf, + compareCodeUnits, setBookTheme, type BookFontSlot, type BookPaletteKey, @@ -42,6 +43,15 @@ export function themeRequest(draft: BookTheme, spreadIds: string[] | undefined, }; } +/** A theme as text, its keys in one order at every depth, so the same theme read twice compares equal. */ +function themeKey(theme: BookTheme | undefined): string { + if (!theme) return ''; + return JSON.stringify(theme, (_key, value: unknown) => { + if (!value || typeof value !== 'object' || Array.isArray(value)) return value; + return Object.fromEntries(Object.entries(value).sort(([a], [b]) => compareCodeUnits(a, b))); + }); +} + /** Whether a draft differs from the built-in theme it is named after. */ export function isEdited(draft: BookTheme): boolean { if (!draft.id) return false; @@ -60,10 +70,13 @@ export function useStudioTheme() { const [reading, setReading] = useState(false); // The book's theme changed under the panel (applied, undone, another editor): the draft follows it, - // set while rendering rather than in an effect, so the panel never paints the old draft first. - const [seen, setSeen] = useState(current); - if (seen !== current) { - setSeen(current); + // set while rendering rather than in an effect, so the panel never paints the old draft first. Compared + // by what it says, not by identity: a save from elsewhere reads the same theme in anew, and must not + // throw away colours being tried here. + const currentKey = useMemo(() => themeKey(current), [current]); + const [seen, setSeen] = useState(currentKey); + if (seen !== currentKey) { + setSeen(currentKey); if (current) setDraft(current); } diff --git a/client/src/store/studioStore.ts b/client/src/store/studioStore.ts index 35278af8d..b0dc56254 100644 --- a/client/src/store/studioStore.ts +++ b/client/src/store/studioStore.ts @@ -87,7 +87,8 @@ interface StudioState extends StudioLayerActions { * step, and the gesture that drives this calls it on every pointer move. */ setRotation: (spreadIndex: number, id: string, rotation: number) => void - addElement: (spreadIndex: number, el: BookElement) => void + /** One element onto a page, selected; false, and nothing changed, when the page holds all it can. */ + addElement: (spreadIndex: number, el: BookElement) => boolean removeElements: (spreadIndex: number, ids: string[]) => void /** Copy elements of a spread to the clipboard (#2316). */ copy: (spreadIndex: number, ids: string[]) => void @@ -225,6 +226,9 @@ export const useStudioStore = create((set, get) => ({ }))), addElement: (spreadIndex, el) => { + // The page's own cap, as paste and duplicate keep it: what would make the book unsaveable is not added. + const target = get().doc?.spreads[spreadIndex] + if (!target || target.elements.length >= MAX_SPREAD_ELEMENTS) return false get().commit(doc => replaceSpread(doc, spreadIndex, sp => ({ ...sp, elements: [...sp.elements, el] }))) /* @@ -236,6 +240,7 @@ export const useStudioStore = create((set, get) => ({ * asked for. Duplicating already worked this way; placing did not. */ set({ selection: [el.id] }) + return true }, removeElements: (spreadIndex, ids) => { diff --git a/client/tests/unit/studio/paintPalette.test.tsx b/client/tests/unit/studio/paintPalette.test.tsx index 2935b2fcf..11ecd1675 100644 --- a/client/tests/unit/studio/paintPalette.test.tsx +++ b/client/tests/unit/studio/paintPalette.test.tsx @@ -221,8 +221,9 @@ describe('the opacity slider', () => { expect(onChange).toHaveBeenLastCalledWith(0.3, false); fireEvent.pointerUp(range); expect(onChange).toHaveBeenLastCalledWith(0.3, true); + // A key let go without a move since that commit is no change, so nothing more is reported. fireEvent.keyUp(range, { key: 'ArrowLeft' }); - expect(onChange).toHaveBeenCalledTimes(3); + expect(onChange).toHaveBeenCalledTimes(2); }); it('sits under the swatches only when the caller takes an alpha', () => { diff --git a/client/tests/unit/studio/studioClipboardLayouts.test.ts b/client/tests/unit/studio/studioClipboardLayouts.test.ts index a776d4445..49e222a47 100644 --- a/client/tests/unit/studio/studioClipboardLayouts.test.ts +++ b/client/tests/unit/studio/studioClipboardLayouts.test.ts @@ -1,8 +1,8 @@ import { describe, it, expect, beforeEach } from 'vitest' import type { BookDocument, BookElement, BookPageSetup, BookSpread } from '@trek/shared' import { - MAX_BOOK_LAYOUTS, bookBadgeElementSchema, bookLayoutSchema, bookPageSetupSchema, bookPhotoElementSchema, - bookShapeElementSchema, bookTextElementSchema, + MAX_BOOK_LAYOUTS, MAX_SPREAD_ELEMENTS, bookBadgeElementSchema, bookLayoutSchema, bookPageSetupSchema, + bookPhotoElementSchema, bookShapeElementSchema, bookTextElementSchema, applySavedBookLayout as applySavedLayout, bookLayoutFromSpread as layoutFromSpread, bookLayoutPreview as layoutPreview, bookLayoutRole as layoutRole, nextBookLayoutName as nextLayoutName, } from '@trek/shared' @@ -166,6 +166,17 @@ describe('studio store: clipboard and layouts', () => { expect(applied.filter(e => e.kind === 'photo' && !e.locked).map(e => e.kind === 'photo' && e.photoId)).toEqual([9]) }) + it('FE-STUDIO-STORE-ADD-001: a full page takes no new element, and nothing is selected for it', () => { + const full = Array.from({ length: MAX_SPREAD_ELEMENTS }, (_, i) => ({ ...shape('s'), id: `s${i}` }) as BookElement) + load({ spreads: [spread('a', full), spread('b', [])] }) + const before = useStudioStore.getState().doc + expect(useStudioStore.getState().addElement(0, text('extra', 'One more'))).toBe(false) + expect(useStudioStore.getState().doc).toBe(before) + expect(useStudioStore.getState().selection).toEqual([]) + expect(useStudioStore.getState().addElement(1, text('first', 'Fits'))).toBe(true) + expect(useStudioStore.getState().selection).toEqual(['first']) + }) + it('FE-STUDIO-STORE-LAYOUT-002: an empty page is not a layout, and a full book says no', () => { const full = Array.from({ length: MAX_BOOK_LAYOUTS }, (_, i) => layoutFromSpread(spread('x', []), page, `L${i}`)) load({ spreads: [spread('a', []), spread('b', [photo('p', 1)])], layouts: full }) diff --git a/client/tests/unit/studio/studioTheme.test.tsx b/client/tests/unit/studio/studioTheme.test.tsx index 8b120c9d3..5676c6d26 100644 --- a/client/tests/unit/studio/studioTheme.test.tsx +++ b/client/tests/unit/studio/studioTheme.test.tsx @@ -1,5 +1,5 @@ import { act, renderHook } from '@testing-library/react'; -import type { BookDocument, BookElement } from '@trek/shared'; +import type { BookDocument, BookElement, BookTheme } from '@trek/shared'; import { bookPageSetupSchema, bookTextElementSchema, bookThemeOf } from '@trek/shared'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import { StudioThemeLink } from '../../../src/components/Studio/theme/StudioThemeLink'; @@ -102,6 +102,24 @@ describe('the theme draft', () => { }); }); +describe('the draft and saves from elsewhere', () => { + it('FE-STUDIO-THEME-012: keeps colours being tried when the same theme is read anew, follows another', () => { + load([text()], { theme: bookThemeOf('nordic') }); + const { result } = renderHook(() => useStudioTheme()); + act(() => result.current.setColour('accent', '#ff0000')); + + // Another editor saved a page: the book comes back parsed again, its theme the same in new objects and key order. + const before = doc().theme as BookTheme; + const reread = Object.fromEntries(Object.entries(JSON.parse(JSON.stringify(before)) as BookTheme).reverse()); + act(() => useStudioStore.setState({ doc: { ...doc(), theme: reread as BookTheme } })); + expect(result.current.draft.palette.accent).toBe('#ff0000'); + + act(() => useStudioStore.setState({ doc: { ...doc(), theme: bookThemeOf('alpine') } })); + expect(result.current.draft.id).toBe('alpine'); + expect(result.current.draft.palette).toEqual(bookThemeOf('alpine').palette); + }); +}); + describe('applying a theme', () => { it('FE-STUDIO-THEME-004: to the book adopts and recolours every page in one undo step', () => { const { result } = renderHook(() => useStudioTheme());