Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
One-time setup that gathers your project's design context and saves it to CLAUDE.md for future sessions.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
scripts/live/insert-ui.mjs
1/**2* Pure helpers for live-mode insert UI (browser + tests).3* Kept separate from live-browser.js so insert logic is unit-testable.4*/56export const PLACEHOLDER_DEFAULT_HEIGHT = 80;7export const PLACEHOLDER_MIN_HEIGHT = 48;8export const PLACEHOLDER_MIN_WIDTH = 120;910/** @typedef {'before' | 'after'} InsertPosition */11/** @typedef {'row' | 'column'} InsertAxis */1213/**14* Infer sibling flow axis from a container's computed layout styles.15* @param {{ display?: string, flexDirection?: string, gridTemplateColumns?: string, gridAutoFlow?: string }} style16* @returns {InsertAxis}17*/18export function detectInsertAxisFromStyle(style) {19const display = style?.display || 'block';20if (display.includes('flex')) {21const dir = style.flexDirection || 'row';22return dir.startsWith('row') ? 'row' : 'column';23}24if (display === 'grid' || display === 'inline-grid') {25const flow = style.gridAutoFlow || 'row';26if (flow.includes('column')) return 'column';27const cols = (style.gridTemplateColumns || '').trim();28if (cols && cols !== 'none') {29const colCount = cols.split(/\s+/).filter(Boolean).length;30if (colCount > 1) return 'row';31}32return 'row';33}34return 'column';35}3637/**38* Pick insertion side from pointer position against an anchor element box.39* @param {number} clientX40* @param {number} clientY41* @param {{ top: number, left: number, width: number, height: number, bottom?: number, right?: number }} rect42* @param {InsertAxis} [axis]43* @returns {InsertPosition}44*/45export function computeInsertPosition(clientX, clientY, rect, axis = 'column') {46if (!rect) return 'after';47if (axis === 'row') {48if (!Number.isFinite(rect.left) || !Number.isFinite(rect.width) || rect.width <= 0) return 'after';49const mid = rect.left + rect.width / 2;50return clientX < mid ? 'before' : 'after';51}52if (!Number.isFinite(rect.top) || !Number.isFinite(rect.height) || rect.height <= 0) return 'after';53const mid = rect.top + rect.height / 2;54return clientY < mid ? 'before' : 'after';55}5657/**58* Whether Create is allowed for an insert session.59* Requires a non-empty prompt OR at least one annotation.60*/61export function canCreateInsert({ prompt, comments, strokes }) {62const hasPrompt = typeof prompt === 'string' && prompt.trim().length > 0;63const hasComments = Array.isArray(comments) && comments.length > 0;64const hasStrokes = Array.isArray(strokes) && strokes.some(65(s) => Array.isArray(s?.points) && s.points.length >= 2,66);67return hasPrompt || hasComments || hasStrokes;68}6970/** Tooltip/title when Create is disabled. */71export function insertCreateDisabledReason({ prompt, comments, strokes }) {72if (canCreateInsert({ prompt, comments, strokes })) return null;73return 'Add a prompt or annotate the placeholder to create';74}7576/**77* Fixed-position insert line coordinates (viewport px).78* @param {{ top: number, left: number, width: number, height: number, bottom?: number, right?: number }} rect79* @param {InsertPosition} position80* @param {InsertAxis} [axis]81*/82export function insertLineCoords(rect, position, axis = 'column') {83if (axis === 'row') {84const right = rect.right ?? rect.left + rect.width;85const x = position === 'before' ? rect.left - 2 : right + 2;86return { axis: 'row', top: rect.top, left: x, width: 0, height: rect.height };87}88const bottom = rect.bottom ?? rect.top + rect.height;89const y = position === 'before' ? rect.top - 2 : bottom + 2;90return { axis: 'column', top: y, left: rect.left, width: rect.width, height: 0 };91}9293/** Cursor while hovering an insert boundary. */94export function cursorForInsertAxis(axis) {95return axis === 'row' ? 'ew-resize' : 'ns-resize';96}9798function groupSiblingRows(siblings, rowThreshold = 8) {99const sorted = [...siblings].sort((a, b) => a.rect.top - b.rect.top || a.rect.left - b.rect.left);100const rows = [];101for (const entry of sorted) {102let placed = false;103for (const row of rows) {104if (Math.abs(entry.rect.top - row[0].rect.top) <= rowThreshold) {105row.push(entry);106placed = true;107break;108}109}110if (!placed) rows.push([entry]);111}112return rows;113}114115function horizontalOverlap(a, b) {116const left = Math.max(a.left, b.left);117const right = Math.min(a.right ?? a.left + a.width, b.right ?? b.left + b.width);118return Math.max(0, right - left);119}120121/**122* Hit-test the gap between adjacent siblings (flex rows, grid columns, stacked blocks).123* @param {number} clientX124* @param {number} clientY125* @param {Array<{ el: unknown, rect: { top: number, left: number, width: number, height: number, bottom?: number, right?: number } }>} siblings126* @param {{ slop?: number, minOverlap?: number }} [opts]127*/128export function hitSiblingInsertGap(clientX, clientY, siblings, opts = {}) {129if (!Array.isArray(siblings) || siblings.length < 2) return null;130const slop = opts.slop ?? 12;131const minOverlap = opts.minOverlap ?? 0.25;132133for (const row of groupSiblingRows(siblings)) {134if (row.length < 2) continue;135const sorted = [...row].sort((a, b) => a.rect.left - b.rect.left);136for (let i = 0; i < sorted.length - 1; i++) {137const a = sorted[i];138const b = sorted[i + 1];139const aRight = a.rect.right ?? a.rect.left + a.rect.width;140const bLeft = b.rect.left;141if (bLeft <= aRight) continue;142const top = Math.max(a.rect.top, b.rect.top);143const aBottom = a.rect.bottom ?? a.rect.top + a.rect.height;144const bBottom = b.rect.bottom ?? b.rect.top + b.rect.height;145const bottom = Math.min(aBottom, bBottom);146const span = bottom - top;147const minH = Math.min(a.rect.height, b.rect.height);148if (span < minH * minOverlap) continue;149150const inX = clientX >= aRight - slop && clientX <= bLeft + slop;151const inY = clientY >= top - slop && clientY <= bottom + slop;152if (!inX || !inY) continue;153154const midX = (aRight + bLeft) / 2;155return {156anchor: b.el,157position: 'before',158axis: 'row',159line: { axis: 'row', left: midX, top, width: 0, height: span },160};161}162}163164const sortedCol = [...siblings].sort((a, b) => a.rect.top - b.rect.top || a.rect.left - b.rect.left);165for (let i = 0; i < sortedCol.length - 1; i++) {166const a = sortedCol[i];167const b = sortedCol[i + 1];168const overlap = horizontalOverlap(a.rect, b.rect);169const minW = Math.min(a.rect.width, b.rect.width);170if (overlap < minW * minOverlap) continue;171172const aBottom = a.rect.bottom ?? a.rect.top + a.rect.height;173const gapTop = aBottom;174const gapBottom = b.rect.top;175if (gapBottom <= gapTop) continue;176177const overlapLeft = Math.max(a.rect.left, b.rect.left);178const overlapRight = Math.min(179a.rect.right ?? a.rect.left + a.rect.width,180b.rect.right ?? b.rect.left + b.rect.width,181);182const inY = clientY >= gapTop - slop && clientY <= gapBottom + slop;183const inX = clientX >= overlapLeft - slop && clientX <= overlapRight + slop;184if (!inY || !inX) continue;185186const midY = (gapTop + gapBottom) / 2;187return {188anchor: b.el,189position: 'before',190axis: 'column',191line: { axis: 'column', top: midY, left: overlapLeft, width: overlap, height: 0 },192};193}194195return null;196}197198/**199* Resolve insert hover target, side, axis, and indicator line for the pointer.200*/201export function resolveInsertHover({ clientX, clientY, target, rect, axis, siblings }) {202const gap = hitSiblingInsertGap(clientX, clientY, siblings);203if (gap) return gap;204205const position = computeInsertPosition(clientX, clientY, rect, axis);206const line = insertLineCoords(rect, position, axis);207return { anchor: target, position, axis, line };208}209210/**211* How the in-flow placeholder should participate in layout.212* Prefer implicit sizing (flex / %) so row inserts don't inherit the full parent width in px.213* @returns {{ kind: 'flex', flex: string, minWidth: number } | { kind: 'percent' } | { kind: 'auto' } | { kind: 'explicit', width: number }}214*/215export function placeholderSizing({ axis, parentDisplay, parentWidth, anchorFlex }) {216const display = parentDisplay || 'block';217const w = Number.isFinite(parentWidth) ? parentWidth : 0;218219if (axis === 'row') {220if (display.includes('flex')) {221const flex = anchorFlex && anchorFlex !== 'none' && anchorFlex !== '0 1 auto'222? anchorFlex223: '1 1 0';224return { kind: 'flex', flex, minWidth: 0 };225}226if (display === 'grid' || display === 'inline-grid') {227return { kind: 'auto' };228}229}230231if (w >= PLACEHOLDER_MIN_WIDTH) {232return { kind: 'percent' };233}234235return {236kind: 'explicit',237width: Math.max(PLACEHOLDER_MIN_WIDTH, w || PLACEHOLDER_MIN_WIDTH),238};239}240241/** Width kinds that need materializing to px before edge-resize. */242export function placeholderWidthIsImplicit(kind) {243return kind === 'flex' || kind === 'percent' || kind === 'auto';244}245246/**247* Clamp user-resized placeholder dimensions.248*/249export function clampPlaceholderSize(width, height, parentWidth, opts = {}) {250const minW = opts.minWidth ?? PLACEHOLDER_MIN_WIDTH;251const minH = opts.minHeight ?? PLACEHOLDER_MIN_HEIGHT;252const maxW = opts.maxWidth ?? Math.max(minW, parentWidth || minW);253return {254width: Math.min(maxW, Math.max(minW, Math.round(width))),255height: Math.max(minH, Math.round(height)),256};257}258259/** CSS cursor for a placeholder edge resize handle. */260export function cursorForPlaceholderEdge(edge) {261if (edge === 'n' || edge === 's') return 'ns-resize';262if (edge === 'e' || edge === 'w') return 'ew-resize';263return 'default';264}265266/**267* Compute placeholder box after dragging one edge (in-flow margins shift for n/w).268* @param {{ width: number, height: number, marginLeft?: number, marginTop?: number }} start269* @param {'n'|'e'|'s'|'w'} edge270* @param {number} dx pointer delta X since drag start271* @param {number} dy pointer delta Y since drag start272* @param {number} parentWidth273*/274export function resizePlaceholderFromEdge(start, edge, dx, dy, parentWidth, opts = {}) {275const base = {276width: start.width,277height: start.height,278marginLeft: start.marginLeft ?? 0,279marginTop: start.marginTop ?? 0,280};281if (edge === 'e') base.width = start.width + dx;282else if (edge === 'w') {283base.width = start.width - dx;284base.marginLeft = start.marginLeft + dx;285} else if (edge === 's') base.height = start.height + dy;286else if (edge === 'n') {287base.height = start.height - dy;288base.marginTop = start.marginTop + dy;289}290291const clamped = clampPlaceholderSize(base.width, base.height, parentWidth, opts);292if (edge === 'w') {293base.marginLeft = start.marginLeft + start.width - clamped.width;294} else if (edge === 'n') {295base.marginTop = start.marginTop + start.height - clamped.height;296}297298return {299width: clamped.width,300height: clamped.height,301marginLeft: Math.round(base.marginLeft),302marginTop: Math.round(base.marginTop),303};304}305306/** Pick and insert toggles are independent but turning one ON turns the other OFF. */307export function applyPickToggle(pickActive, insertActive) {308const nextPick = !pickActive;309return {310pickActive: nextPick,311insertActive: nextPick ? false : insertActive,312};313}314315export function applyInsertToggle(pickActive, insertActive) {316const nextInsert = !insertActive;317return {318pickActive: nextInsert ? false : pickActive,319insertActive: nextInsert,320};321}322323/**324* Build the browser generate payload for insert mode.325*/326export function buildInsertGeneratePayload({327id,328count,329pageUrl,330anchorContext,331position,332placeholder,333freeformPrompt,334comments,335strokes,336screenshotPath,337}) {338const payload = {339type: 'generate',340mode: 'insert',341id,342count,343pageUrl,344insert: {345position,346anchor: anchorContext,347},348placeholder,349freeformPrompt: freeformPrompt?.trim() || undefined,350};351if (comments?.length) payload.comments = comments;352if (strokes?.length) payload.strokes = strokes;353if (screenshotPath) payload.screenshotPath = screenshotPath;354return payload;355}356357/**358* Whether a variant wrapper is currently shown (handles `hidden` and display:none).359* @param {{ hidden?: boolean, style?: { display?: string } } | null | undefined} el360*/361export function isVariantShown(el) {362if (!el) return false;363if (el.hidden) return false;364if (el.style?.display === 'none') return false;365return true;366}367368/**369* Show or hide a variant wrapper for cycling.370* @param {{ hidden?: boolean, style?: { display?: string }, removeAttribute?: (name: string) => void, setAttribute?: (name: string, value?: string) => void } | null | undefined} el371* @param {boolean} shown372*/373export function setVariantShown(el, shown) {374if (!el) return;375if (shown) {376el.removeAttribute?.('hidden');377if (el.style) el.style.display = '';378} else {379el.setAttribute?.('hidden', '');380if (el.style) el.style.display = 'none';381}382}383384/**385* Pick the best live anchor during an insert session (placeholder until variants land).386* @param {{387* wrapper?: unknown,388* variantCount?: number,389* visibleVariant?: number,390* placeholder?: unknown,391* insertAnchor?: unknown,392* pickVariantContent?: (wrapper: unknown, index: number) => unknown,393* }} opts394*/395export function resolveInsertSessionAnchor(opts) {396const {397wrapper,398variantCount = 0,399visibleVariant = 0,400placeholder,401insertAnchor,402pickVariantContent,403} = opts || {};404if (wrapper && variantCount > 0 && visibleVariant > 0 && pickVariantContent) {405const vis = pickVariantContent(wrapper, visibleVariant);406if (vis) return vis;407}408return placeholder || insertAnchor || null;409}410411/**412* Snapshot placeholder geometry + anchor fingerprint so HMR can recreate the box.413* @param {{414* tagName?: string,415* className?: string,416* textContent?: string,417* }} anchor418* @param {{419* offsetWidth?: number,420* offsetHeight?: number,421* style?: { marginLeft?: string, marginTop?: string },422* }} placeholder423* @param {{ position: 'before' | 'after', layoutAxis?: 'row' | 'column' }} meta424*/425export function buildInsertPlaceholderSnapshot(anchor, placeholder, { position, layoutAxis }) {426return {427width: Math.round(placeholder.offsetWidth || 0),428height: Math.round(placeholder.offsetHeight || PLACEHOLDER_DEFAULT_HEIGHT),429marginLeft: parseFloat(placeholder.style?.marginLeft || '') || 0,430marginTop: parseFloat(placeholder.style?.marginTop || '') || 0,431position,432layoutAxis: layoutAxis || 'column',433anchorTag: anchor.tagName || 'DIV',434anchorClasses: anchor.className || '',435anchorText: (anchor.textContent || '').trim().slice(0, 120),436};437}438439/**440* Re-find an insert anchor after framework HMR replaced the live DOM node.441* @param {Pick<Document, 'body' | 'querySelectorAll'>} doc442* @param {ReturnType<typeof buildInsertPlaceholderSnapshot> | null | undefined} snapshot443* @param {Element | null | undefined} liveAnchor444*/445export function findInsertAnchorInDom(doc, snapshot, liveAnchor = null) {446if (liveAnchor && doc.body.contains(liveAnchor)) return liveAnchor;447if (!snapshot) return null;448const tag = (snapshot.anchorTag || 'div').toLowerCase();449const cls = (snapshot.anchorClasses || '').split(/\s+/).filter(Boolean)[0];450const needle = snapshot.anchorText || '';451const sel = cls ? `${tag}.${cls}` : tag;452const candidates = doc.querySelectorAll(sel);453for (const candidate of candidates) {454if (needle && !(candidate.textContent || '').includes(needle.slice(0, 40))) continue;455return candidate;456}457return null;458}459