CoolFace
Apppublic

lerobot/robot-learning-tutorial

sourceHugging Faceupdated 1y agoView on Hugging Face
508likes
HtmlEmbed.astro172 linesDownload Raw Back to components
1---2interface Props { src: string; title?: string; desc?: string; frameless?: boolean; align?: 'left' | 'center' | 'right'; id?: string, data?: string | string[], config?: any }3const { src, title, desc, frameless = false, align = 'left', id, data, config } = Astro.props as Props;4 5// Load all .html embeds under src/content/embeds/** as strings (dev & build)6const embeds = (import.meta as any).glob('../content/embeds/**/*.html', { query: '?raw', import: 'default', eager: true }) as Record<string, string>;7 8function resolveFragment(requested: string): string | null {9  // Allow both "banner.html" and "embeds/banner.html"10  const needle = requested.replace(/^\/*/, '');11  for (const [key, html] of Object.entries(embeds)) {12    if (key.endsWith('/' + needle) || key.endsWith('/' + needle.replace(/^embeds\//, ''))) {13      return html;14    }15  }16  return null;17}18 19const html = resolveFragment(src);20const mountId = `frag-${Math.random().toString(36).slice(2)}`;21const dataAttr = Array.isArray(data) ? JSON.stringify(data) : (typeof data === 'string' ? data : undefined);22const configAttr = typeof config === 'string' ? config : (config != null ? JSON.stringify(config) : undefined);23---24{ html ? (25  <figure class="html-embed" id={id}>26    {title && <figcaption class="html-embed__title" style={`text-align:${align}`}>{title}</figcaption>}27    <div class={`html-embed__card${frameless ? ' is-frameless' : ''}`}>28      <div id={mountId} data-datafiles={dataAttr} data-config={configAttr} set:html={html} />29    </div>30    {desc && <figcaption class="html-embed__desc" style={`text-align:${align}`} set:html={desc}></figcaption>}31  </figure>32 ) : (33  <div><!-- Fragment not found: {src} --></div>34 ) }35 36 37 38<script>39  // Re-execute <script> tags inside the injected fragment (innerHTML doesn't run scripts)40  const scriptEl = document.currentScript;41  const mount = scriptEl ? scriptEl.previousElementSibling : null;42  const execute = () => {43    if (!mount) return;44    const scripts = mount.querySelectorAll('script');45    scripts.forEach(old => {46      // ignore non-executable types (e.g., application/json)47      if (old.type && old.type !== 'text/javascript' && old.type !== 'module' && old.type !== '') return;48      if (old.dataset.executed === 'true') return;49      old.dataset.executed = 'true';50      if (old.src) {51        const s = document.createElement('script');52        Array.from(old.attributes).forEach(attr => s.setAttribute(attr.name, attr.value));53        document.body.appendChild(s);54      } else {55        try {56          // run inline57          (0, eval)(old.text || '');58        } catch (e) {59          console.error('HtmlEmbed inline script error:', e);60        }61      }62    });63  };64  // Execute after DOM is parsed (ensures deferred module scripts are executed first)65  if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', execute, { once: true });66  else execute();67  </script>68 69<style is:global>70  .html-embed { margin: 0 0 var(--block-spacing-y);71    z-index: var(--z-elevated);72    position: relative;73  74  }75  .html-embed__title { 76    text-align: left; 77    font-weight: 600; 78    font-size: 0.95rem; 79    color: var(--text-color);80    margin: 0;81    padding: 0;82    padding-bottom: var(--spacing-1);83    position: relative;84    display: block;85    width: 100%;86  }87  .html-embed__card {88    background: var(--code-bg);89    border: 1px solid var(--border-color);90    border-radius: 10px;91    padding: 8px;92    z-index: calc(var(--z-elevated) + 1);93    position: relative;94  }95  .html-embed__card.is-frameless {96    background: transparent;97    border-color: transparent;98    padding: 0;99  }100  .html-embed__desc { 101    text-align: left; 102    font-size: 0.9rem; 103    color: var(--muted-color); 104    margin: 0; 105    padding: 0;106    padding-top: var(--spacing-1);107    position: relative;108    z-index: var(--z-elevated);109    display: block;110    width: 100%;111  }112  /* Plotly – fragments & controls */113  .html-embed__card svg text { fill: var(--text-color); }114  .html-embed__card label { color: var(--text-color); }115  .plotly-graph-div { width: 100%; min-height: 320px; }116  @media (max-width: 768px) { .plotly-graph-div { min-height: 260px; } }117  [id^="plot-"] { display: flex; flex-direction: column; align-items: center; gap: 15px; }118  .plotly_caption { font-style: italic; margin-top: 10px; }119  .plotly_controls { display: flex; flex-wrap: wrap; justify-content: center; gap: 30px; }120  .plotly_input_container { display: flex; align-items: center; flex-direction: column; gap: 10px; }121  .plotly_input_container > select { padding: 2px 4px; line-height: 1.5em; text-align: center; border-radius: 4px; font-size: 12px; background-color: var(--neutral-200); outline: none; border: 1px solid var(--neutral-300); }122  .plotly_slider { display: flex; align-items: center; gap: 10px; }123  .plotly_slider > input[type="range"] { -webkit-appearance: none; appearance: none; height: 2px; background: var(--neutral-400); border-radius: 5px; outline: none; }124  .plotly_slider > input[type="range"]::-webkit-slider-thumb { -webkit-appearance: none; width: 18px; height: 18px; border-radius: 50%; background: var(--primary-color); cursor: pointer; }125  .plotly_slider > input[type="range"]::-moz-range-thumb { width: 18px; height: 18px; border-radius: 50%; background: var(--primary-color); cursor: pointer; }126  .plotly_slider > span { font-size: 14px; line-height: 1.6em; min-width: 16px; }127  /* Dark mode overrides for Plotly readability */128  [data-theme="dark"] .html-embed__card:not(.is-frameless) { background: #12151b; border-color: rgba(255,255,255,.15); }129  [data-theme="dark"] .html-embed__card .xaxislayer-above text,130  [data-theme="dark"] .html-embed__card .yaxislayer-above text,131  [data-theme="dark"] .html-embed__card .infolayer text,132  [data-theme="dark"] .html-embed__card .legend text,133  [data-theme="dark"] .html-embed__card .annotation text,134  [data-theme="dark"] .html-embed__card .colorbar text,135  [data-theme="dark"] .html-embed__card .hoverlayer text { fill: #fff !important; }136  [data-theme="dark"] .html-embed__card .xaxislayer-above path,137  [data-theme="dark"] .html-embed__card .yaxislayer-above path,138  [data-theme="dark"] .html-embed__card .xlines-above,139  [data-theme="dark"] .html-embed__card .ylines-above { stroke: rgba(255,255,255,.35) !important; }140  [data-theme="dark"] .html-embed__card .gridlayer path { stroke: rgba(255,255,255,.15) !important; }141  [data-theme="dark"] .html-embed__card .legend rect.bg { fill: rgba(0,0,0,.25) !important; stroke: rgba(255,255,255,.2) !important; }142  [data-theme="dark"] .html-embed__card .hoverlayer .bg { fill: rgba(0,0,0,.8) !important; stroke: rgba(255,255,255,.2) !important; }143  [data-theme="dark"] .html-embed__card .colorbar .cbbg { fill: rgba(0,0,0,.25) !important; stroke: rgba(255,255,255,.2) !important; }144  @media print {145    .html-embed, .html-embed__card { max-width: 100% !important; width: 100% !important; margin-left: 0 !important; margin-right: 0 !important; }146    .html-embed__card { padding: 6px; }147    .html-embed__card.is-frameless { padding: 0; }148    .html-embed__card svg,149    .html-embed__card canvas,150    .html-embed__card img { max-width: 100% !important; height: auto !important; }151    .html-embed__card > div[id^="frag-"] { width: 100% !important; }152  }153  @media print {154    /* Avoid breaks inside embeds */155    .html-embed, .html-embed__card { break-inside: avoid; page-break-inside: avoid; }156    /* Constrain width and scale inner content */157    .html-embed, .html-embed__card { max-width: 100% !important; width: 100% !important; }158    .html-embed__card { padding: 6px; }159    .html-embed__card.is-frameless { padding: 0; }160    .html-embed__card svg,161    .html-embed__card canvas,162    .html-embed__card img,163    .html-embed__card video,164    .html-embed__card iframe { max-width: 100% !important; height: auto !important; }165    .html-embed__card > div[id^="frag-"] { width: 100% !important; max-width: 100% !important; }166    /* Center and constrain the banner (galaxy) when printing */167    .html-embed .d3-galaxy { width: 100% !important; max-width: 980px !important; margin-left: auto !important; margin-right: auto !important; }168  }169</style>170 171 172