/** * Element controller factory. Creates the fluent API returned by `engine.element(id)` and * `engine.element(slideIndex, path)`. Use the public API; this module is for implementation. * * @module elementController * @see Lumina.element * @see ElementController * @see AnimateOptions */ import gsap from 'gsap'; import type { LuminaStore } from './store'; import { resolveTransitionConfig } from './animationConfig'; import { resolveAnimationFromInput } from '../animation'; import type { AnimateOptions, ElementController as IElementController, ElementState, TimelineKeyframes, TimelineKeyframeState } from './types'; /** * Minimal engine interface for resolving a DOM node by element id. Implemented by {@link Lumina}. */ export interface LuminaEngineLike { getElementById(id: string): HTMLElement | null; } /** * Creates an {@link ElementController} for the given element id. Used internally by * `engine.element(id)` and `engine.element(slideIndex, path)`. Prefer the public API. * * @param store - Lumina store (setElementState, state.elementState). * @param engine - Engine with getElementById (for animate). * @param id - Resolved element id (from resolveId or user). * @returns Fluent controller for show/hide/opacity/transform/css/class/animate. * @internal */ export function createElementController( store: LuminaStore, engine: LuminaEngineLike, id: string ): IElementController { function set(patch: Partial) { store.setElementState(id, patch); } function getClass(): string { return store.state.elementState[id]?.class ?? ''; } const ctrl: IElementController = { show() { set({ visible: true, opacity: 1 }); const dom = engine.getElementById(id); const reveal = dom?.closest('.reveal-zoom, .reveal-up, .reveal-card, .reveal-left, .reveal-right'); if (reveal) gsap.set(reveal, { opacity: 1 }); return ctrl; }, hide() { set({ visible: false }); return ctrl; }, toggle(force?: boolean) { const cur = store.state.elementState[id]?.visible !== false; set({ visible: force !== undefined ? force : !cur }); return ctrl; }, opacity(value: number) { set({ opacity: value }); return ctrl; }, transform(value: string) { set({ transform: value }); return ctrl; }, css(style: Record) { set({ style }); return ctrl; }, addClass(className: string) { const cur = getClass(); const next = (cur + ' ' + className).trim(); set({ class: next }); return ctrl; }, removeClass(className: string) { const cur = getClass(); const next = cur .split(/\s+/) .filter((c) => c && c !== className) .join(' '); set({ class: next }); return ctrl; }, animate(options: AnimateOptions) { runAnimate(options); return ctrl; }, animateAsync(options: AnimateOptions): Promise { return new Promise((resolve) => { runAnimate(options, () => { options.onComplete?.(); resolve(); }); }); }, animateToProgress(progress: number, keyframes: TimelineKeyframes) { const keys = Object.keys(keyframes) .map((k) => parseFloat(k)) .filter((n) => !Number.isNaN(n)) .sort((a, b) => a - b); if (keys.length === 0) return ctrl; const k0 = keys[0]; const k1 = keys[keys.length - 1]; let state: TimelineKeyframeState; if (progress <= k0) { state = keyframes[String(k0)] ?? {}; } else if (progress >= k1) { state = keyframes[String(k1)] ?? {}; } else { let i = 0; while (i + 1 < keys.length && keys[i + 1] <= progress) i++; const a = keys[i]; const b = keys[i + 1]; const va = keyframes[String(a)] ?? {}; const vb = keyframes[String(b)] ?? {}; const t = (progress - a) / (b - a); state = {}; const numericKeys = ['opacity', 'x', 'y', 'scale', 'rotate'] as const; for (const prop of numericKeys) { const pa = va[prop]; const pb = vb[prop]; if (typeof pa === 'number' && typeof pb === 'number') { state[prop] = pa + (pb - pa) * t; } else if (typeof pb === 'number') { state[prop] = pb; } else if (typeof pa === 'number') { state[prop] = pa; } } if (typeof vb.visible === 'boolean') state.visible = vb.visible; else if (typeof va.visible === 'boolean') state.visible = va.visible; } const opacity = state.opacity !== undefined ? state.opacity : 1; const parts: string[] = []; if (state.x !== undefined || state.y !== undefined) { parts.push(`translate(${state.x ?? 0}px, ${state.y ?? 0}px)`); } if (state.scale !== undefined) { parts.push(`scale(${state.scale})`); } if (state.rotate !== undefined) { parts.push(`rotate(${state.rotate}deg)`); } const transform = parts.length ? parts.join(' ') : ''; const visible = state.visible !== undefined ? state.visible : opacity > 0; set({ opacity, transform, visible }); return ctrl; } }; function runAnimate(opts: AnimateOptions, onCompleteOverride?: () => void) { const el = engine.getElementById(id); if (!el) return; const cfg = resolveTransitionConfig(store, undefined); const motions = store.state.deck?.meta?.motions; const { from, to, ease, duration } = resolveAnimationFromInput(opts, { duration: cfg.elementDuration, ease: cfg.elementEase, }, motions); const cb = onCompleteOverride ?? opts.onComplete; const toVars: gsap.TweenVars = { ...to, duration, ease, overwrite: true, onComplete: cb }; if (from && Object.keys(from).length) { gsap.fromTo(el, from, toVars); } else { gsap.to(el, toVars); } } return ctrl; } /** No-op controller when engine has no store (e.g. studioEmbed mode). */ export function createNoopElementController(_engine: LuminaEngineLike, _id: string): IElementController { const ctrl: IElementController = { show: () => ctrl, hide: () => ctrl, toggle: () => ctrl, opacity: () => ctrl, transform: () => ctrl, css: () => ctrl, addClass: () => ctrl, removeClass: () => ctrl, animate: () => ctrl, animateAsync: () => Promise.resolve(), animateToProgress: () => ctrl }; return ctrl; }