feat(builder): media package -- image crop/perf, video size+picker, gallery cols/lightbox, box-model+anim/vis rollout

- ImageBlock/ImageStylePanel: SizeControl width(absorbs old maxWidth
  presets)/height, AspectRatioControl + OBJECT_FIT grid + FocalPointGrid for
  CSS framing crop (aspect-ratio/object-fit/object-position on the <img>).
  Exported <img> always gets loading="lazy" decoding="async", plus width/
  height attrs when the style has a plain px length (pxAttr helper).
- VideoBlock/MediaStylePanel: Video URL text input replaced with
  AssetPicker(mediaType="video") writing videoUrl (paste-URL still handles
  YouTube/Vimeo, upload/browse handle files). Added SizeControl(width) +
  AspectRatioControl so the video frame honors real size/aspect instead of
  a hardcoded 16:9 (padding-bottom hack replaced with CSS aspect-ratio).
  New optional `poster` prop (image AssetPicker) + preload="metadata" on
  file-type <video>.
- Gallery: surfaced the existing-but-unexposed `columns` and `lightbox`
  props with panel controls.
- All 5 owned components (ImageBlock, VideoBlock, Gallery, ContentSlider,
  MapEmbed): added margin/padding (per-side)/border/box-shadow/opacity style
  defaults + AnimationControl/VisibilityControl-backed animation/
  animationDelay/hideOnDesktop/hideOnTablet/hideOnMobile props. New shared
  mediaBoxModel.tsx (package-local, not shared.tsx) DRYs the box-model +
  border/effects + animation/visibility panel sections across
  ImageStylePanel and MediaStylePanel.
- Tests: extended *.toHtml.test.ts for all 5 components (crop/perf attrs,
  video size/aspect/poster/preload, gallery columns/lightbox, box-model
  style emission, craft.props presence) + new MediaStylePanel.video.test.tsx
  verifying the AssetPicker wiring writes videoUrl/poster and the video-only
  size controls are gated on videoUrl. 694 tests green, tsc + vite build
  clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-14 06:30:50 -07:00
parent 1d9460c173
commit 0419291259
14 changed files with 851 additions and 44 deletions
@@ -0,0 +1,149 @@
import { describe, test, expect, vi, beforeEach, afterEach } from 'vitest';
import React from 'react';
import { createRoot, Root } from 'react-dom/client';
import { act } from 'react-dom/test-utils';
/* Same DOM-harness pattern as MediaStylePanel.slides.test.tsx -- mock
@craftjs/core's useEditor so setProp calls can be observed without
mounting a real <Editor> tree, and mock utils/assets so AssetPicker
doesn't hit the network. */
const setPropSpy = vi.fn((_id: string, updater: (p: any) => void) => {
updater(lastProps);
});
let lastProps: any;
vi.mock('@craftjs/core', () => ({
useEditor: () => ({ actions: { setProp: setPropSpy } }),
}));
vi.mock('../../../utils/assets', () => ({
uploadAsset: vi.fn(),
listAssets: vi.fn(),
}));
import { MediaStylePanel } from './MediaStylePanel';
let container: HTMLDivElement;
let root: Root;
function render(ui: React.ReactElement) {
container = document.createElement('div');
document.body.appendChild(container);
act(() => {
root = createRoot(container);
root.render(ui);
});
}
function unmount() {
act(() => { root.unmount(); });
container.remove();
}
function q<T extends Element = Element>(testId: string): T | null {
return container.querySelector(`[data-testid="${testId}"]`);
}
function qAll<T extends Element = Element>(testId: string): T[] {
return Array.from(container.querySelectorAll(`[data-testid="${testId}"]`));
}
function setValue(input: HTMLInputElement, value: string) {
const setter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value')!.set!;
setter.call(input, value);
input.dispatchEvent(new Event('input', { bubbles: true }));
}
beforeEach(() => {
setPropSpy.mockClear();
});
afterEach(() => {
if (container) unmount();
});
describe('MediaStylePanel Video source uses AssetPicker (upload/browse/paste-URL), not a plain text input', () => {
test('renders a full-variant AssetPicker (not the old plain input) for videoUrl', () => {
lastProps = { videoUrl: '', poster: '', style: {} };
render(<MediaStylePanel selectedId="vid-1" nodeProps={lastProps} />);
expect(q('asset-picker-full')).not.toBeNull();
});
test('applying a pasted URL via the AssetPicker writes videoUrl (not some other key)', () => {
lastProps = { videoUrl: '', poster: '', style: {} };
render(<MediaStylePanel selectedId="vid-1" nodeProps={lastProps} />);
// The Video Source AssetPicker is the first url-input on the page (the
// Poster picker is a second, separate AssetPicker instance below it).
const urlInput = qAll<HTMLInputElement>('asset-picker-url-input')[0];
expect(urlInput).toBeTruthy();
setValue(urlInput, 'https://example.com/clip.mp4');
const applyBtn = qAll<HTMLButtonElement>('asset-picker-apply')[0];
act(() => {
applyBtn.dispatchEvent(new MouseEvent('click', { bubbles: true }));
});
expect(setPropSpy).toHaveBeenCalled();
expect(lastProps.videoUrl).toBe('https://example.com/clip.mp4');
});
test('a video-shaped selection also renders a Poster AssetPicker (image) that writes `poster`', () => {
lastProps = { videoUrl: 'https://example.com/clip.mp4', poster: '', style: {} };
render(<MediaStylePanel selectedId="vid-1" nodeProps={lastProps} />);
const pickers = qAll('asset-picker-full');
expect(pickers.length).toBe(2); // Video Source + Poster Image
const urlInputs = qAll<HTMLInputElement>('asset-picker-url-input');
const posterInput = urlInputs[1];
setValue(posterInput, 'https://example.com/poster.jpg');
const applyBtns = qAll<HTMLButtonElement>('asset-picker-apply');
act(() => {
applyBtns[1].dispatchEvent(new MouseEvent('click', { bubbles: true }));
});
expect(lastProps.poster).toBe('https://example.com/poster.jpg');
});
});
describe('MediaStylePanel Video size controls (Width + Aspect Ratio) are gated on videoUrl', () => {
test('a video-shaped selection renders SizeControl + AspectRatioControl', () => {
lastProps = { videoUrl: '', poster: '', style: {} };
render(<MediaStylePanel selectedId="vid-1" nodeProps={lastProps} />);
expect(q('size-control')).not.toBeNull();
expect(q('aspect-ratio-control')).not.toBeNull();
});
test('a Map-shaped selection (no videoUrl) does NOT render the video size controls', () => {
lastProps = { address: 'New York, NY', zoom: 14, height: '400px', style: {} };
render(<MediaStylePanel selectedId="map-1" nodeProps={lastProps} />);
expect(q('size-control')).toBeNull();
expect(q('aspect-ratio-control')).toBeNull();
});
});
function openCollapsibleByTitle(title: string) {
const btn = Array.from(container.querySelectorAll('button')).find((b) => b.textContent?.includes(title));
expect(btn).toBeTruthy();
act(() => {
btn!.dispatchEvent(new MouseEvent('click', { bubbles: true }));
});
}
describe('MediaStylePanel box-model + animation/visibility rollout renders for every media type', () => {
test('a Map-shaped selection still gets Spacing / Border & Effects / Animation & Visibility sections (collapsed by default, open on click)', () => {
lastProps = { address: 'New York, NY', zoom: 14, height: '400px', style: {}, animation: '', hideOnDesktop: false };
render(<MediaStylePanel selectedId="map-1" nodeProps={lastProps} />);
openCollapsibleByTitle('Spacing');
expect(q('spacing-control')).not.toBeNull();
openCollapsibleByTitle('Border & Effects');
expect(q('border-control')).not.toBeNull();
openCollapsibleByTitle('Animation & Visibility');
expect(q('animation-control')).not.toBeNull();
expect(q('visibility-control')).not.toBeNull();
});
});