Skip to content

Text to vertical video in 100+ styles: AI codes each scene (HTML/SVG/WebGL), renders a 9:16 MP4 with music and English/Vietnamese voice. MCP, CLI and REST for AI agents.

6.9 KB
Raw Blame
// Tiện ích chung của Studio + màn tạo mới. Chữ từ server chỉ vào DOM qua textContent (luật P2): el(tag, cls, text).
export const $ = (s, r = document) => r.querySelector(s);
export const $$ = (s, r = document) => [...r.querySelectorAll(s)];
export const I = window.I18N;
export const t = (k, v) => I.t(k, v);

/** Tạo phần tử; text đi qua textContent. attrs: {data-*, aria-*, type…} (giá trị là chuỗi, không phải HTML). */
export function el(tag, cls, text, attrs) {
  const e = document.createElement(tag);
  if (cls) e.className = cls;
  if (text != null) e.textContent = text;
  if (attrs) for (const [k, v] of Object.entries(attrs)) if (v != null && v !== false) e.setAttribute(k, v === true ? '' : String(v));
  return e;
}
/** Phần tử có chữ dịch được: data-i18n (I18N.apply đổi lại khi đổi VI/EN). */
export function tx(tag, cls, key, attrs) { const e = el(tag, cls, t(key), attrs); e.dataset.i18n = key; return e; }

// bộ icon nét (Lucide-like), hằng số của app — không có dữ liệu người dùng
const P = {
  back: 'M12 19l-7-7 7-7M19 12H5',
  cap: 'M5 5h14a3 3 0 0 1 3 3v8a3 3 0 0 1-3 3H5a3 3 0 0 1-3-3V8a3 3 0 0 1 3-3zM7 14.5h4M14 14.5h3M7 10.5h2M12 10.5h5',
  music: 'M9 18V5l12-2v13M9 18a3 3 0 1 1-6 0 3 3 0 0 1 6 0zM21 16a3 3 0 1 1-6 0 3 3 0 0 1 6 0z',
  zoom: 'M15 3h6v6M9 21H3v-6M21 3l-7 7M3 21l7-7',
  gfx: 'M6 3h12a3 3 0 0 1 3 3v12a3 3 0 0 1-3 3H6a3 3 0 0 1-3-3V6a3 3 0 0 1 3-3zM3 9h18M9 21V9',
  ai: 'M11 3.5l1.8 5.2 5.2 1.8-5.2 1.8L11 17.5l-1.8-5.2L4 10.5l5.2-1.8zM19 15v5M16.5 17.5h5',
  dl: 'M12 15V3M7 10l5 5 5-5M5 21h14',
  warn: 'M21.73 18l-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3zM12 9v4M12 17h.01',
  pause: 'M8 5v14M16 5v14',
  prev: 'M19 20L9 12l10-8zM5 19V5',
  next: 'M5 4l10 8-10 8zM19 5v14',
  undo: 'M9 14L4 9l5-5M4 9h11a5 5 0 0 1 0 10h-4',
  redo: 'M15 14l5-5-5-5M20 9H9a5 5 0 0 0 0 10h4',
  cut: 'M9 6a3 3 0 1 1-6 0 3 3 0 0 1 6 0zM9 18a3 3 0 1 1-6 0 3 3 0 0 1 6 0zM20 4L8.12 15.88M14.47 14.48L20 20M8.12 8.12L12 12',
  trash: 'M3 6h18M8 6V4h8v2M19 6l-1 14H6L5 6',
  merge: 'M7 4v4a4 4 0 0 0 4 4h2a4 4 0 0 1 4 4v4M17 4v4a4 4 0 0 1-4 4',
  split: 'M12 3v18M7 8l-4 4 4 4M17 8l4 4-4 4',
  plus: 'M12 5v14M5 12h14',
  check: 'M20 6L9 17l-5-5',
  x: 'M18 6L6 18M6 6l12 12',
  upload: 'M12 15V3M17 8l-5-5-5 5M5 21h14',
  film: 'M5 3h14a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2zM7 3v18M17 3v18M3 7.5h4M3 12h18M3 16.5h4M17 7.5h4M17 16.5h4',
  mag: 'M18 11a7 7 0 1 1-14 0 7 7 0 0 1 14 0zM21 21l-4.3-4.3',
  clock: 'M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0zM12 7v5l3 2',
  minus: 'M5 12h14',
  eye: 'M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7S2 12 2 12zM15 12a3 3 0 1 1-6 0 3 3 0 0 1 6 0z',
  pen: 'M17 3a2.85 2.83 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5z',
  rotate: 'M3 12a9 9 0 1 0 3-6.7L3 8M3 3v5h5',
  hi: 'M4 20h16M7 16l5-12 5 12M9 11h6',
  right: 'M9 6l6 6-6 6',
  left: 'M15 6l-6 6 6 6',
  more: 'M5 12h.01M12 12h.01M19 12h.01',
  send: 'M22 2L11 13M22 2l-7 20-4-9-9-4z',
  fit: 'M3 8V5a2 2 0 0 1 2-2h3M16 3h3a2 2 0 0 1 2 2v3M21 16v3a2 2 0 0 1-2 2h-3M8 21H5a2 2 0 0 1-2-2v-3M8 12h8M8 12l2-2M8 12l2 2M16 12l-2-2M16 12l-2 2',
};
const PLAY = 'M7 4.5v15l12.5-7.5z';
const NS = 'http://www.w3.org/2000/svg';
export function icon(name, cls) {
  const s = document.createElementNS(NS, 'svg');
  s.setAttribute('viewBox', '0 0 24 24'); s.setAttribute('aria-hidden', 'true'); s.setAttribute('focusable', 'false');
  s.setAttribute('class', 'ic' + (cls ? ' ' + cls : ''));
  const p = document.createElementNS(NS, 'path');
  if (name === 'play') { p.setAttribute('d', PLAY); p.setAttribute('class', 'fill'); } else p.setAttribute('d', P[name] || '');
  s.append(p);
  return s;
}
/** Nút chỉ có icon: luôn có aria-label (+ title) dịch được. */
export function iconBtn(name, labelKey, cls, extra) {
  const b = el('button', 'ib' + (cls ? ' ' + cls : ''), null, { type: 'button', ...(extra || {}) });
  b.append(icon(name));
  b.setAttribute('aria-label', t(labelKey)); b.title = t(labelKey);
  b.dataset.i18nAria = labelKey; b.dataset.i18nTitle = labelKey;
  return b;
}

export const clamp = (x, a, b) => Math.min(b, Math.max(a, x));
export const r2 = (x) => Math.round(x * 100) / 100;
/** 0:01.6 */
export const fmtT = (s) => { const x = Math.max(0, Math.round(s * 10) / 10); const m = Math.floor(x / 60); return `${m}:${(x - m * 60).toFixed(1).padStart(4, '0')}`; };
/** 0:36 */
export const fmtDur = (s) => { const x = Math.max(0, Math.round(s)); return `${Math.floor(x / 60)}:${String(x % 60).padStart(2, '0')}`; };
export const fmtDb = (x) => `${x > 0 ? '+' : x < 0 ? '−' : ''}${Math.abs(x)} dB`;
export const fmtMB = (b) => (b >= 1e9 ? (b / 1e9).toFixed(2) + ' GB' : Math.max(1, Math.round(b / 1e6)) + ' MB');
/** chỉ nhận đường dẫn cùng gốc ("/edits/…"), không nhận javascript:, //host… */
export const safeUrl = (u) => (typeof u === 'string' && /^\/(?!\/)/.test(u) ? u : null);
export const ID_RE = /^[\w-]{1,80}$/;

export async function getJSON(url) {
  const r = await fetch(url, { cache: 'no-store' });
  const j = await r.json().catch(() => ({}));
  if (!r.ok) throw Object.assign(new Error(j.error || 'HTTP ' + r.status), { status: r.status });
  return j;
}
export async function postJSON(url, body) {
  const r = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) });
  const j = await r.json().catch(() => ({}));
  if (!r.ok) throw Object.assign(new Error(j.error || 'HTTP ' + r.status), { status: r.status });
  return j;
}
export const store = {
  get(k, d = null) { try { const v = localStorage.getItem(k); return v == null ? d : JSON.parse(v); } catch { return d; } },
  set(k, v) { try { localStorage.setItem(k, JSON.stringify(v)); } catch { /* đầy / riêng tư: bỏ qua */ } },
  del(k) { try { localStorage.removeItem(k); } catch { /* bỏ qua */ } },
};
/** "*Giá* tăng" → [{w, hi}] (như edit/manual.mjs lineTokens) */
export function tokens(text) {
  const out = []; const re = /\*([^*]+)\*|([^*]+)/g; let m;
  while ((m = re.exec(String(text)))) { const hi = !!m[1]; for (const w of (m[1] || m[2]).split(/\s+/)) if (w.trim()) out.push({ w: w.trim(), hi }); }
  return out;
}
export const untokens = (ws) => ws.map((w) => (w.hi ? `*${w.w}*` : w.w)).join(' ');
export const plain = (text) => String(text).replace(/\*/g, '');
/** Gắn chữ (có chữ nhấn) vào phần tử: chữ nhấn bọc <em>, tất cả qua textContent. */
export function paintWords(node, text, { hiClass = null } = {}) {
  const ws = tokens(text);
  node.replaceChildren();
  ws.forEach((w, i) => {
    if (i) node.append(' ');
    if (w.hi) { const e = el('em', hiClass, w.w); node.append(e); } else node.append(w.w);
  });
}
export const reduceMotion = () => window.matchMedia('(prefers-reduced-motion: reduce)').matches;
export const narrow = () => window.matchMedia('(max-width: 767px)').matches;