| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import { html, css, LitElement } from "https://esm.sh/lit@3"; |
|
|
| const escapeHtml = (s) => |
| String(s ?? "").replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">"); |
|
|
| export class Trace extends LitElement { |
| static properties = { |
| steps: { type: Array, state: true }, |
| meta: { type: String, reflect: true }, |
| stepLabels: { type: Object }, |
| }; |
|
|
| createRenderRoot() { return this; } |
|
|
| constructor() { |
| super(); |
| this.steps = []; |
| this.meta = ""; |
| this.stepLabels = {}; |
| } |
|
|
| pushStep(step) { |
| this.steps = [...this.steps, step]; |
| } |
|
|
| clear() { |
| this.steps = []; |
| this.meta = ""; |
| } |
|
|
| _renderStep(step) { |
| const [label, hint] = this.stepLabels[step.step] || [step.step, ""]; |
| const ok = step.ok === true; |
| const fail = step.ok === false; |
| const cls = ok ? "ok" : fail ? "err" : "running"; |
| const mark = ok ? "β" : fail ? "β" : "β"; |
| const time = step.elapsed_s != null |
| ? `<span class="time">${step.elapsed_s}s</span>` : ""; |
| const result = step.result |
| ? `<div class="result">${escapeHtml(JSON.stringify(step.result))}</div>` : ""; |
| const err = step.err |
| ? `<div class="result" style="color:var(--nyc-scarlet)">${escapeHtml(step.err)}</div>` : ""; |
| |
| |
| |
| const li = document.createElement("li"); |
| li.className = cls; |
| li.innerHTML = ` |
| <span class="icon">${mark}</span> |
| <div> |
| <div class="label">${escapeHtml(label)}</div> |
| <div class="meta">${escapeHtml(hint)}</div> |
| </div> |
| ${time} |
| ${result} |
| ${err} |
| `; |
| return li; |
| } |
|
|
| render() { |
| |
| |
| queueMicrotask(() => { |
| const ol = this.querySelector("ol#steps-list"); |
| if (!ol) return; |
| ol.innerHTML = ""; |
| for (const s of this.steps) ol.appendChild(this._renderStep(s)); |
| }); |
| |
| |
| return html`<ol id="steps-list" style="list-style:none; margin:0; padding:4px 0; font-size:12.5px;"></ol>`; |
| } |
| } |
|
|
| customElements.define("r-trace", Trace); |
|
|