feat(demo): fase 3, componente ChatDemo como isla y guiones en JSON
- Colección demos con esquema zod: guion, chips, respuestas, pasos con PDF, reglas de clasificación (regex validadas) y fallback - ChatDemo: arranca al entrar en pantalla y se pausa al salir, respeta prefers-reduced-motion, escala al ancho disponible, Pausar/Reiniciar y entrada libre; visor de PDF dentro del teléfono; fuentes del sitio - Estilos globales anidados bajo .chatdemo para los nodos creados por JS - Las fichas con demo tipo chat incrustan el guion correspondiente - Script de capturas acepta ESPERA_MS para fotografiar el chat en marcha Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
eb9c2cebc1
commit
c72ddb73e7
7 files changed
+1071
-16
No files matched your search
@@ -0,0 +1,836 @@
|
||||
---
|
||||
/* Chat de demostración (isla). Recibe el guion como datos de la colección
|
||||
`demos` y lo reproduce dentro de un teléfono simulado:
|
||||
- arranca al entrar en pantalla y se pausa al salir (IntersectionObserver);
|
||||
- respeta prefers-reduced-motion (sin animaciones de burbujas ni chips);
|
||||
- escala el teléfono al ancho disponible (ResizeObserver) sin desbordar;
|
||||
- usa las fuentes del sitio; conserva Pausar / Reiniciar y la entrada libre.
|
||||
Los datos del guion viajan en un <script type="application/json">: no es
|
||||
código ejecutable, así que es compatible con la CSP. */
|
||||
import type { CollectionEntry } from 'astro:content';
|
||||
|
||||
interface Props {
|
||||
demo: CollectionEntry<'demos'>;
|
||||
}
|
||||
const { demo } = Astro.props;
|
||||
const d = demo.data;
|
||||
const id = `chatdemo-${demo.id}`;
|
||||
---
|
||||
|
||||
<div class="chatdemo" data-chatdemo id={id}>
|
||||
<script type="application/json" data-guion set:html={JSON.stringify(d)} />
|
||||
<div class="escenario" data-escenario>
|
||||
<div class="telefono" data-telefono aria-label={d.descripcion} role="group">
|
||||
<div class="pantalla">
|
||||
<div class="isla" aria-hidden="true"></div>
|
||||
<div class="barra" aria-hidden="true"><span data-reloj>9:41</span><span class="senal">●●● ▲ ▮</span></div>
|
||||
<div class="cabecera">
|
||||
<span class="atras" aria-hidden="true">‹</span>
|
||||
<span class="avatar" aria-hidden="true">{d.iniciales}</span>
|
||||
<div class="quien">
|
||||
<div class="nombre">{d.nombre}</div>
|
||||
<div class="estado" data-estado>en línea</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="chat" data-chat aria-label="Conversación de demostración" aria-live="off">
|
||||
<div class="dia">HOY</div>
|
||||
</div>
|
||||
<div class="chips" data-chips role="group" aria-label="Preguntas de ejemplo">
|
||||
{d.chips.map((c) => <button type="button" class="chip" data-clave={c.clave}>{c.etiqueta}</button>)}
|
||||
</div>
|
||||
<form class="entrada" data-form>
|
||||
<label class="sr-only" for={`${id}-entrada`}>Escribe un mensaje</label>
|
||||
<input id={`${id}-entrada`} type="text" placeholder="Escribe un mensaje" autocomplete="off" maxlength="200" data-entrada />
|
||||
<button type="submit" class="enviar" aria-label="Enviar">➤</button>
|
||||
</form>
|
||||
<div class="visor" data-visor hidden>
|
||||
<div class="visor-barra">
|
||||
<span class="visor-titulo" data-visor-titulo></span>
|
||||
<button type="button" class="visor-cerrar" data-visor-cerrar aria-label="Cerrar documento">✕</button>
|
||||
</div>
|
||||
<div class="visor-cuerpo" data-visor-cuerpo></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="controles">
|
||||
<button type="button" class="ctrl" data-pausar aria-pressed="false">Pausar</button>
|
||||
<button type="button" class="ctrl" data-reiniciar>Reiniciar</button>
|
||||
<span class="nota mono">Demostración con datos ficticios</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<style is:global>
|
||||
/* Estilos globales anidados bajo .chatdemo: las burbujas, tarjetas y filas se crean
|
||||
desde JS y no reciben el ámbito de Astro. */
|
||||
.chatdemo {
|
||||
& {
|
||||
--wa-verde: #075e54;
|
||||
--wa-fondo: #ece5dd;
|
||||
--wa-entrante: #ffffff;
|
||||
--wa-saliente: #d9fdd3;
|
||||
--wa-hora: #667781;
|
||||
--wa-tinta: #0b1411;
|
||||
--wa-pdf: #e0483d;
|
||||
--escala: 1;
|
||||
width: 100%;
|
||||
max-width: 390px;
|
||||
}
|
||||
.escenario {
|
||||
width: 100%;
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
}
|
||||
.telefono {
|
||||
width: 390px;
|
||||
height: 844px;
|
||||
transform: scale(var(--escala));
|
||||
transform-origin: top left;
|
||||
border-radius: 56px;
|
||||
background: linear-gradient(160deg, #1c1c1e, #0b0b0c);
|
||||
padding: 13px;
|
||||
box-shadow:
|
||||
0 2px 0 1px rgba(255, 255, 255, 0.06) inset,
|
||||
0 0 0 2px #2a2a2c;
|
||||
color: var(--wa-tinta);
|
||||
font-family: var(--font-body);
|
||||
font-size: 14.5px;
|
||||
line-height: 1.35;
|
||||
}
|
||||
.pantalla {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
border-radius: 44px;
|
||||
overflow: hidden;
|
||||
background: var(--wa-fondo);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
.isla {
|
||||
position: absolute;
|
||||
top: 14px;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
width: 118px;
|
||||
height: 34px;
|
||||
background: #000;
|
||||
border-radius: 20px;
|
||||
z-index: 3;
|
||||
}
|
||||
.barra {
|
||||
height: 46px;
|
||||
flex: none;
|
||||
background: var(--wa-verde);
|
||||
color: #fff;
|
||||
display: flex;
|
||||
align-items: flex-end;
|
||||
justify-content: space-between;
|
||||
padding: 0 26px 7px;
|
||||
font-size: 14px;
|
||||
font-weight: 600;
|
||||
}
|
||||
.senal {
|
||||
font-size: 10px;
|
||||
letter-spacing: 0.1em;
|
||||
}
|
||||
.cabecera {
|
||||
flex: none;
|
||||
background: var(--wa-verde);
|
||||
color: #fff;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
padding: 6px 12px 10px;
|
||||
}
|
||||
.atras {
|
||||
font-size: 26px;
|
||||
line-height: 1;
|
||||
opacity: 0.95;
|
||||
}
|
||||
.avatar {
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
border-radius: 50%;
|
||||
flex: none;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
background: #fbfaf7;
|
||||
color: var(--wa-verde);
|
||||
font-family: var(--font-display);
|
||||
font-weight: 700;
|
||||
font-size: 16px;
|
||||
box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.3);
|
||||
}
|
||||
.quien {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
.nombre {
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
line-height: 1.15;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
.estado {
|
||||
font-size: 12.5px;
|
||||
opacity: 0.85;
|
||||
height: 15px;
|
||||
}
|
||||
.estado.escribiendo {
|
||||
color: #bff3df;
|
||||
font-style: italic;
|
||||
}
|
||||
.chat {
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
overflow-y: auto;
|
||||
padding: 14px 12px 8px;
|
||||
scroll-behavior: smooth;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 6px;
|
||||
background-image: radial-gradient(rgba(0, 0, 0, 0.04) 1px, transparent 1px);
|
||||
background-size: 18px 18px;
|
||||
}
|
||||
.dia {
|
||||
align-self: center;
|
||||
background: #fff;
|
||||
color: var(--wa-hora);
|
||||
font-size: 11.5px;
|
||||
font-weight: 600;
|
||||
padding: 4px 10px;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 6px;
|
||||
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.08);
|
||||
}
|
||||
.fila {
|
||||
display: flex;
|
||||
}
|
||||
.fila.in {
|
||||
justify-content: flex-start;
|
||||
}
|
||||
.fila.out {
|
||||
justify-content: flex-end;
|
||||
}
|
||||
.burbuja {
|
||||
max-width: 86%;
|
||||
padding: 7px 9px 18px;
|
||||
border-radius: 10px;
|
||||
position: relative;
|
||||
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
|
||||
white-space: pre-wrap;
|
||||
overflow-wrap: anywhere;
|
||||
animation: aparecer 0.18s ease-out;
|
||||
}
|
||||
.fila.in .burbuja {
|
||||
background: var(--wa-entrante);
|
||||
border-top-left-radius: 2px;
|
||||
}
|
||||
.fila.out .burbuja {
|
||||
background: var(--wa-saliente);
|
||||
border-top-right-radius: 2px;
|
||||
}
|
||||
.burbuja .meta {
|
||||
position: absolute;
|
||||
right: 8px;
|
||||
bottom: 3px;
|
||||
font-size: 10.5px;
|
||||
color: var(--wa-hora);
|
||||
white-space: nowrap;
|
||||
}
|
||||
.burbuja .meta .tick {
|
||||
color: #53bdeb;
|
||||
margin-left: 3px;
|
||||
}
|
||||
.puntos {
|
||||
display: inline-flex;
|
||||
gap: 4px;
|
||||
padding: 4px 2px 0;
|
||||
}
|
||||
.puntos i {
|
||||
width: 7px;
|
||||
height: 7px;
|
||||
border-radius: 50%;
|
||||
background: #9aa5a0;
|
||||
animation: latir 1.2s infinite ease-in-out;
|
||||
}
|
||||
.puntos i:nth-child(2) {
|
||||
animation-delay: 0.15s;
|
||||
}
|
||||
.puntos i:nth-child(3) {
|
||||
animation-delay: 0.3s;
|
||||
}
|
||||
.tarjeta-pdf {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
align-items: center;
|
||||
background: #f5f1ea;
|
||||
border: 1px solid #e6dfd2;
|
||||
border-radius: 8px;
|
||||
padding: 8px;
|
||||
width: 100%;
|
||||
text-align: left;
|
||||
font: inherit;
|
||||
color: inherit;
|
||||
cursor: pointer;
|
||||
}
|
||||
.tarjeta-pdf + .tarjeta-pdf {
|
||||
margin-top: 6px;
|
||||
}
|
||||
.tarjeta-pdf .icono {
|
||||
width: 30px;
|
||||
height: 36px;
|
||||
border-radius: 4px;
|
||||
background: var(--wa-pdf);
|
||||
color: #fff;
|
||||
font-size: 9px;
|
||||
font-weight: 700;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
flex: none;
|
||||
}
|
||||
.tarjeta-pdf .archivo {
|
||||
font-weight: 600;
|
||||
font-size: 13px;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
.tarjeta-pdf .abrir {
|
||||
font-size: 11.5px;
|
||||
color: var(--wa-hora);
|
||||
}
|
||||
.chips {
|
||||
flex: none;
|
||||
display: flex;
|
||||
gap: 6px;
|
||||
overflow-x: auto;
|
||||
padding: 6px 10px;
|
||||
scrollbar-width: none;
|
||||
}
|
||||
.chip {
|
||||
flex: none;
|
||||
font: inherit;
|
||||
font-size: 12.5px;
|
||||
font-weight: 500;
|
||||
padding: 6px 11px;
|
||||
border-radius: 999px;
|
||||
border: 1px solid #c9d6d2;
|
||||
background: #fff;
|
||||
color: var(--wa-verde);
|
||||
cursor: pointer;
|
||||
transition: transform 0.15s;
|
||||
}
|
||||
.chip:hover {
|
||||
transform: translateY(-1px);
|
||||
}
|
||||
.entrada {
|
||||
flex: none;
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
padding: 6px 10px 22px;
|
||||
background: #f0f0f0;
|
||||
}
|
||||
.entrada input {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
font: inherit;
|
||||
font-size: 14px;
|
||||
padding: 9px 12px;
|
||||
border-radius: 20px;
|
||||
border: 1px solid #d5d5d5;
|
||||
background: #fff;
|
||||
color: var(--wa-tinta);
|
||||
}
|
||||
.enviar {
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
border-radius: 50%;
|
||||
border: 0;
|
||||
background: var(--wa-verde);
|
||||
color: #fff;
|
||||
font-size: 16px;
|
||||
cursor: pointer;
|
||||
}
|
||||
.visor {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
z-index: 4;
|
||||
background: #e8e4dc;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
.visor-barra {
|
||||
flex: none;
|
||||
background: var(--wa-verde);
|
||||
color: #fff;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 8px;
|
||||
padding: 52px 14px 10px;
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
}
|
||||
.visor-titulo {
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.visor-cerrar {
|
||||
background: none;
|
||||
border: 0;
|
||||
color: #fff;
|
||||
font-size: 18px;
|
||||
cursor: pointer;
|
||||
}
|
||||
.visor-cuerpo {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
padding: 14px;
|
||||
}
|
||||
.hoja {
|
||||
background: #fff;
|
||||
padding: 18px 16px;
|
||||
border-radius: 4px;
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
|
||||
font-size: 12.5px;
|
||||
}
|
||||
.hoja-titulo {
|
||||
font-family: var(--font-display);
|
||||
font-weight: 700;
|
||||
font-size: 18px;
|
||||
margin-top: 6px;
|
||||
}
|
||||
.hoja-sub {
|
||||
color: var(--wa-hora);
|
||||
font-size: 11.5px;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
.hoja-vehiculo {
|
||||
font-weight: 600;
|
||||
font-size: 14px;
|
||||
}
|
||||
.hoja-vsub {
|
||||
color: var(--wa-hora);
|
||||
font-size: 11.5px;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
.hoja-fila {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
gap: 8px;
|
||||
padding: 5px 0;
|
||||
border-bottom: 1px solid #eee;
|
||||
}
|
||||
.hoja-fila .v {
|
||||
font-family: var(--font-mono);
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
.hoja-renta {
|
||||
margin-top: 12px;
|
||||
padding: 10px;
|
||||
background: #f5f1ea;
|
||||
border-radius: 6px;
|
||||
text-align: center;
|
||||
}
|
||||
.hoja-renta .monto {
|
||||
font-family: var(--font-mono);
|
||||
font-size: 20px;
|
||||
font-weight: 600;
|
||||
}
|
||||
.hoja-renta .por {
|
||||
font-size: 11px;
|
||||
color: var(--wa-hora);
|
||||
}
|
||||
.hoja-legal {
|
||||
margin-top: 12px;
|
||||
font-size: 10px;
|
||||
color: var(--wa-hora);
|
||||
}
|
||||
.controles {
|
||||
display: flex;
|
||||
gap: 0.6rem;
|
||||
align-items: center;
|
||||
flex-wrap: wrap;
|
||||
margin-top: 0.8rem;
|
||||
}
|
||||
.ctrl {
|
||||
font: inherit;
|
||||
font-size: 0.85rem;
|
||||
font-weight: 500;
|
||||
padding: 0.4rem 0.8rem;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 4px;
|
||||
background: var(--bg);
|
||||
color: var(--fg);
|
||||
cursor: pointer;
|
||||
}
|
||||
.ctrl:hover {
|
||||
border-color: var(--accent);
|
||||
color: var(--accent);
|
||||
}
|
||||
.nota {
|
||||
font-size: 0.72rem;
|
||||
color: var(--fg-3);
|
||||
}
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.burbuja,
|
||||
.puntos i,
|
||||
.chip {
|
||||
animation: none;
|
||||
transition: none;
|
||||
}
|
||||
.chat {
|
||||
scroll-behavior: auto;
|
||||
}
|
||||
}
|
||||
}
|
||||
@keyframes aparecer {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: translateY(4px);
|
||||
}
|
||||
}
|
||||
@keyframes latir {
|
||||
0%,
|
||||
80%,
|
||||
100% {
|
||||
opacity: 0.35;
|
||||
}
|
||||
40% {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
<script>
|
||||
type Hoja = { vehiculo: string; subtitulo: string; plazo: string; renta: string; filas: [string, string][] };
|
||||
type Doc = { archivo: string; hoja: Hoja };
|
||||
type Paso = { texto: string } | { pdf: Doc[] };
|
||||
type Guion = {
|
||||
nombre: string;
|
||||
reloj: 'fijo' | 'actual';
|
||||
guion: string[];
|
||||
chips: { clave: string; etiqueta: string }[];
|
||||
clasificacion: { patron: string; clave: string }[];
|
||||
fallback: string;
|
||||
respuestas: Record<string, { usuario?: string; pasos: Paso[] }>;
|
||||
};
|
||||
|
||||
const reduceMovimiento = matchMedia('(prefers-reduced-motion: reduce)').matches;
|
||||
const dormir = (ms: number) => new Promise<void>((r) => setTimeout(r, ms));
|
||||
const MESES = ['Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun', 'Jul', 'Ago', 'Sep', 'Oct', 'Nov', 'Dic'];
|
||||
|
||||
function montar(raiz: HTMLElement) {
|
||||
const guion = JSON.parse(raiz.querySelector('[data-guion]')!.textContent || '{}') as Guion;
|
||||
const q = <T extends HTMLElement>(sel: string) => raiz.querySelector<T>(sel)!;
|
||||
const escenario = q('[data-escenario]');
|
||||
const telefono = q('[data-telefono]');
|
||||
const chat = q('[data-chat]');
|
||||
const estado = q('[data-estado]');
|
||||
const relojEl = q('[data-reloj]');
|
||||
const form = q<HTMLFormElement>('[data-form]');
|
||||
const entrada = q<HTMLInputElement>('[data-entrada]');
|
||||
const btnPausar = q<HTMLButtonElement>('[data-pausar]');
|
||||
const btnReiniciar = q<HTMLButtonElement>('[data-reiniciar]');
|
||||
const visor = q('[data-visor]');
|
||||
const visorTitulo = q('[data-visor-titulo]');
|
||||
const visorCuerpo = q('[data-visor-cuerpo]');
|
||||
const patrones = guion.clasificacion.map((c) => ({ re: new RegExp(c.patron, 'i'), clave: c.clave }));
|
||||
|
||||
/* Escala del teléfono al ancho disponible. */
|
||||
function ajustar() {
|
||||
const ancho = escenario.clientWidth;
|
||||
const s = Math.min(1, ancho / 390);
|
||||
raiz.style.setProperty('--escala', String(s));
|
||||
escenario.style.height = `${Math.round(844 * s)}px`;
|
||||
}
|
||||
new ResizeObserver(ajustar).observe(escenario);
|
||||
ajustar();
|
||||
|
||||
/* Reloj: fijo (9:41 y avanza por mensaje) o la hora actual. */
|
||||
let reloj = [9, 41];
|
||||
function horaActual() {
|
||||
const d = new Date();
|
||||
return `${d.getHours()}:${String(d.getMinutes()).padStart(2, '0')}`;
|
||||
}
|
||||
function sello(): string {
|
||||
if (guion.reloj === 'actual') return horaActual();
|
||||
const t = `${reloj[0]}:${String(reloj[1]).padStart(2, '0')}`;
|
||||
reloj[1]++;
|
||||
if (reloj[1] > 59) {
|
||||
reloj[1] = 0;
|
||||
reloj[0]++;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
function fechaActual() {
|
||||
const d = new Date();
|
||||
return `${d.getDate()} ${MESES[d.getMonth()]} ${d.getFullYear()}`;
|
||||
}
|
||||
if (guion.reloj === 'actual') {
|
||||
relojEl.textContent = horaActual();
|
||||
setInterval(() => (relojEl.textContent = horaActual()), 30000);
|
||||
}
|
||||
|
||||
/* Texto con *negritas* y _cursivas_ convertido a nodos (sin innerHTML). */
|
||||
function formatear(texto: string): DocumentFragment {
|
||||
const frag = document.createDocumentFragment();
|
||||
const t = texto.replace(/\{HORA\}/g, horaActual()).replace(/\{FECHA\}/g, fechaActual());
|
||||
const re = /\*([^*\n]+)\*|_([^_\n]+)_/g;
|
||||
let ultimo = 0;
|
||||
let m: RegExpExecArray | null;
|
||||
while ((m = re.exec(t))) {
|
||||
if (m.index > ultimo) frag.append(t.slice(ultimo, m.index));
|
||||
const el = document.createElement(m[1] !== undefined ? 'strong' : 'em');
|
||||
el.textContent = m[1] ?? m[2] ?? '';
|
||||
frag.append(el);
|
||||
ultimo = m.index + m[0].length;
|
||||
}
|
||||
if (ultimo < t.length) frag.append(t.slice(ultimo));
|
||||
return frag;
|
||||
}
|
||||
|
||||
function bajar() {
|
||||
chat.scrollTop = chat.scrollHeight;
|
||||
}
|
||||
function fila(lado: 'in' | 'out'): [HTMLElement, HTMLElement] {
|
||||
const f = document.createElement('div');
|
||||
f.className = `fila ${lado}`;
|
||||
const b = document.createElement('div');
|
||||
b.className = 'burbuja';
|
||||
f.append(b);
|
||||
chat.append(f);
|
||||
return [f, b];
|
||||
}
|
||||
function meta(b: HTMLElement, lado: 'in' | 'out') {
|
||||
const m = document.createElement('span');
|
||||
m.className = 'meta';
|
||||
m.textContent = sello();
|
||||
if (lado === 'out') {
|
||||
const tick = document.createElement('span');
|
||||
tick.className = 'tick';
|
||||
tick.textContent = '✓✓';
|
||||
m.append(tick);
|
||||
}
|
||||
b.append(m);
|
||||
}
|
||||
function agregarTexto(lado: 'in' | 'out', texto: string) {
|
||||
const [, b] = fila(lado);
|
||||
b.append(formatear(texto));
|
||||
meta(b, lado);
|
||||
bajar();
|
||||
}
|
||||
function agregarEscribiendo(): HTMLElement {
|
||||
const [f, b] = fila('in');
|
||||
const p = document.createElement('span');
|
||||
p.className = 'puntos';
|
||||
p.append(document.createElement('i'), document.createElement('i'), document.createElement('i'));
|
||||
b.append(p);
|
||||
bajar();
|
||||
return f;
|
||||
}
|
||||
function agregarPdf(docs: Doc[]) {
|
||||
const [, b] = fila('in');
|
||||
for (const d of docs) {
|
||||
const card = document.createElement('button');
|
||||
card.type = 'button';
|
||||
card.className = 'tarjeta-pdf';
|
||||
const icono = document.createElement('span');
|
||||
icono.className = 'icono';
|
||||
icono.textContent = 'PDF';
|
||||
const txt = document.createElement('span');
|
||||
const nombre = document.createElement('div');
|
||||
nombre.className = 'archivo';
|
||||
nombre.textContent = d.archivo;
|
||||
const abrir = document.createElement('div');
|
||||
abrir.className = 'abrir';
|
||||
abrir.textContent = 'PDF · 1 página · Abrir cotización';
|
||||
txt.append(nombre, abrir);
|
||||
card.append(icono, txt);
|
||||
card.addEventListener('click', () => abrirVisor(d));
|
||||
b.append(card);
|
||||
}
|
||||
meta(b, 'in');
|
||||
bajar();
|
||||
}
|
||||
|
||||
/* Visor de la "hoja" PDF dentro del teléfono. */
|
||||
function abrirVisor(d: Doc) {
|
||||
visorTitulo.textContent = d.archivo;
|
||||
visorCuerpo.replaceChildren();
|
||||
const h = d.hoja;
|
||||
const hoja = document.createElement('div');
|
||||
hoja.className = 'hoja';
|
||||
const add = (cls: string, txt: string) => {
|
||||
const e = document.createElement('div');
|
||||
e.className = cls;
|
||||
e.textContent = txt;
|
||||
hoja.append(e);
|
||||
};
|
||||
add('hoja-titulo', 'Cotización de arrendamiento');
|
||||
add('hoja-sub', 'Arrendamiento puro · vehicular');
|
||||
add('hoja-vehiculo', h.vehiculo);
|
||||
add('hoja-vsub', h.subtitulo);
|
||||
for (const [k, v] of h.filas) {
|
||||
const f = document.createElement('div');
|
||||
f.className = 'hoja-fila';
|
||||
const ke = document.createElement('span');
|
||||
ke.textContent = k;
|
||||
const ve = document.createElement('span');
|
||||
ve.className = 'v';
|
||||
ve.textContent = v;
|
||||
f.append(ke, ve);
|
||||
hoja.append(f);
|
||||
}
|
||||
const renta = document.createElement('div');
|
||||
renta.className = 'hoja-renta';
|
||||
const l = document.createElement('div');
|
||||
l.textContent = 'Renta mensual';
|
||||
const monto = document.createElement('div');
|
||||
monto.className = 'monto';
|
||||
monto.textContent = h.renta;
|
||||
const por = document.createElement('div');
|
||||
por.className = 'por';
|
||||
por.textContent = `IVA incluido · ${h.plazo} meses`;
|
||||
renta.append(l, monto, por);
|
||||
hoja.append(renta);
|
||||
add(
|
||||
'hoja-legal',
|
||||
'Cotización informativa sin valor contractual. Vigencia 15 días naturales. Sujeta a aprobación de crédito y disponibilidad de la unidad. Cifras en pesos mexicanos.',
|
||||
);
|
||||
visorCuerpo.append(hoja);
|
||||
visor.hidden = false;
|
||||
q<HTMLButtonElement>('[data-visor-cerrar]').focus();
|
||||
}
|
||||
q('[data-visor-cerrar]').addEventListener('click', () => (visor.hidden = true));
|
||||
|
||||
function escribiendo(on: boolean) {
|
||||
estado.textContent = on ? 'escribiendo…' : 'en línea';
|
||||
estado.classList.toggle('escribiendo', on);
|
||||
}
|
||||
function limpiar() {
|
||||
chat.replaceChildren();
|
||||
const dia = document.createElement('div');
|
||||
dia.className = 'dia';
|
||||
dia.textContent = 'HOY';
|
||||
chat.append(dia);
|
||||
reloj = [9, 41];
|
||||
visor.hidden = true;
|
||||
}
|
||||
|
||||
/* Reproducción. `corrida` invalida cualquier secuencia anterior. */
|
||||
let corrida = 0;
|
||||
let ocupado = false;
|
||||
let auto = false;
|
||||
let indice = 0;
|
||||
let visible = false;
|
||||
let arrancado = false;
|
||||
|
||||
async function intercambio(usuario: string | undefined, pasos: Paso[], id: number) {
|
||||
if (ocupado) return;
|
||||
ocupado = true;
|
||||
try {
|
||||
if (usuario) {
|
||||
agregarTexto('out', usuario);
|
||||
await dormir(reduceMovimiento ? 300 : 480);
|
||||
}
|
||||
for (let i = 0; i < pasos.length; i++) {
|
||||
if (id !== corrida) return;
|
||||
const paso = pasos[i]!;
|
||||
const typ = agregarEscribiendo();
|
||||
escribiendo(true);
|
||||
const dur = 'texto' in paso ? Math.min(2200, 650 + paso.texto.length * 6) : 1100;
|
||||
await dormir(reduceMovimiento ? Math.min(dur, 900) : dur);
|
||||
typ.remove();
|
||||
escribiendo(false);
|
||||
if (id !== corrida) return;
|
||||
if ('texto' in paso) agregarTexto('in', paso.texto);
|
||||
else agregarPdf(paso.pdf);
|
||||
if (i < pasos.length - 1) await dormir(950);
|
||||
}
|
||||
} finally {
|
||||
ocupado = false;
|
||||
}
|
||||
}
|
||||
|
||||
function botones() {
|
||||
btnPausar.textContent = auto ? 'Pausar' : 'Reproducir';
|
||||
btnPausar.setAttribute('aria-pressed', String(!auto));
|
||||
chat.setAttribute('aria-live', auto ? 'off' : 'polite');
|
||||
}
|
||||
async function reproducir(desde = 0) {
|
||||
const id = ++corrida;
|
||||
auto = true;
|
||||
botones();
|
||||
if (desde === 0) limpiar();
|
||||
await dormir(500);
|
||||
for (let i = desde; i < guion.guion.length; i++) {
|
||||
if (id !== corrida) return;
|
||||
indice = i;
|
||||
const r = guion.respuestas[guion.guion[i]!]!;
|
||||
await intercambio(r.usuario, r.pasos, id);
|
||||
if (id !== corrida) return;
|
||||
await dormir(1150);
|
||||
}
|
||||
if (id !== corrida) return;
|
||||
indice = 0;
|
||||
await dormir(2600);
|
||||
if (id === corrida && auto) reproducir(0);
|
||||
}
|
||||
function detener() {
|
||||
corrida++;
|
||||
auto = false;
|
||||
botones();
|
||||
}
|
||||
|
||||
btnPausar.addEventListener('click', () => (auto ? detener() : reproducir(indice)));
|
||||
btnReiniciar.addEventListener('click', () => reproducir(0));
|
||||
raiz.querySelectorAll<HTMLButtonElement>('[data-clave]').forEach((chip) =>
|
||||
chip.addEventListener('click', () => {
|
||||
if (ocupado) return;
|
||||
detener();
|
||||
const r = guion.respuestas[chip.dataset.clave!];
|
||||
if (r) intercambio(r.usuario, r.pasos, corrida);
|
||||
}),
|
||||
);
|
||||
form.addEventListener('submit', (e) => {
|
||||
e.preventDefault();
|
||||
if (ocupado) return;
|
||||
const texto = entrada.value.trim();
|
||||
if (!texto) return;
|
||||
entrada.value = '';
|
||||
detener();
|
||||
const clave = patrones.find((p) => p.re.test(texto))?.clave;
|
||||
const pasos = clave ? guion.respuestas[clave]!.pasos : [{ texto: guion.fallback }];
|
||||
intercambio(texto, pasos, corrida);
|
||||
});
|
||||
|
||||
/* Arranca al entrar en pantalla; se pausa al salir y retoma al volver. */
|
||||
let pausadoPorSalida = false;
|
||||
new IntersectionObserver(
|
||||
(entradas) => {
|
||||
for (const e of entradas) {
|
||||
visible = e.isIntersecting;
|
||||
if (visible) {
|
||||
if (!arrancado) {
|
||||
arrancado = true;
|
||||
reproducir(0);
|
||||
} else if (pausadoPorSalida) {
|
||||
pausadoPorSalida = false;
|
||||
reproducir(indice);
|
||||
}
|
||||
} else if (auto) {
|
||||
detener();
|
||||
pausadoPorSalida = true;
|
||||
}
|
||||
}
|
||||
},
|
||||
{ threshold: 0.35 },
|
||||
).observe(telefono);
|
||||
}
|
||||
|
||||
document.querySelectorAll<HTMLElement>('[data-chatdemo]').forEach(montar);
|
||||
</script>
|
||||
Reference in new issue
Block a user