| @@ -12,9 +12,14 @@ |
| 12 | 12 | // One body row per segment: track, label, start, length, then any columns |
| 13 | 13 | // the tooltip should show (engine/table.ts). The element reads the table, |
| 14 | 14 | // draws the lanes after it and hides it visually; screen readers and |
| 15 | | −// no-script readers keep the table. When the page rewrites the rows, the |
| 16 | | −// element re-reads them. It carries no words: labels, headers and the axis |
| 17 | | −// title come from the page, numbers are formatted in the page's language. |
| 15 | +// no-script readers keep the table. With [show-table], CSS shows it under |
| 16 | +// the plot as a data table; the element marks each segment row with what |
| 17 | +// CSS cannot work out alone (data-fill: its resolved palette slot, |
| 18 | +// data-track-start: the first row of a run of one track). When the page |
| 19 | +// rewrites the rows, the element re-reads them; lanes and segments are keyed |
| 20 | +// by track and label, so a segment that changes glides to its new place |
| 21 | +// rather than being redrawn. It carries no words: labels, headers and the |
| 22 | +// axis title come from the page, numbers are formatted in the page's language. |
| 18 | 23 | // |
| 19 | 24 | // Streams own time, signals own derivation: the table's mutations, the keys |
| 20 | 25 | // and the pointer are streams; one `scan` folds the chart actions |
| @@ -34,6 +39,7 @@ import { |
| 34 | 39 | map, |
| 35 | 40 | mapTo, |
| 36 | 41 | merge, |
| 42 | + on, |
| 37 | 43 | onCleanup, |
| 38 | 44 | pipe, |
| 39 | 45 | scan, |
| @@ -70,15 +76,51 @@ const KEYMAP = { |
| 70 | 76 | Enter: "select", |
| 71 | 77 | } as const; |
| 72 | 78 | |
| 73 | | −/** The table's rows and text changing under the element, as a stream. */ |
| 79 | +/** |
| 80 | + * The table's rows and text changing under the element, as a stream. Only the |
| 81 | + * attributes readTable reads count, so the element's own row marks never |
| 82 | + * trigger a re-read. |
| 83 | + */ |
| 74 | 84 | function tableChanges(table: HTMLTableElement): Source<void> { |
| 75 | 85 | return sink => { |
| 76 | 86 | const observer = new MutationObserver(() => sink.next()); |
| 77 | | − observer.observe(table, { childList: true, subtree: true, characterData: true, attributes: true }); |
| 87 | + observer.observe(table, { |
| 88 | + childList: true, |
| 89 | + subtree: true, |
| 90 | + characterData: true, |
| 91 | + attributeFilter: ["data-series", "data-texture", "data-value"], |
| 92 | + }); |
| 78 | 93 | return () => observer.disconnect(); |
| 79 | 94 | }; |
| 80 | 95 | } |
| 81 | 96 | |
| 97 | +/** Set an attribute only when it changes; null removes it. */ |
| 98 | +function mark(row: Element, name: string, value: string | null): void { |
| 99 | + if (row.getAttribute(name) === value) return; |
| 100 | + if (value === null) row.removeAttribute(name); |
| 101 | + else row.setAttribute(name, value); |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Mark the segment rows for the table view: each row's palette slot (a row |
| 106 | + * without data-series takes one by label, which CSS cannot know) and the |
| 107 | + * first row of each run of one track. Rows that stopped being segments lose |
| 108 | + * their marks. |
| 109 | + */ |
| 110 | +function markRows(table: HTMLTableElement, segments: readonly Segment[]): void { |
| 111 | + const rows = new Set<Element>(segments.map(s => s.row)); |
| 112 | + for (const row of table.querySelectorAll("tr[data-fill]")) if (!rows.has(row)) unmarkRow(row); |
| 113 | + segments.forEach((segment, i) => { |
| 114 | + mark(segment.row, "data-fill", segment.series); |
| 115 | + mark(segment.row, "data-track-start", i === 0 || segments[i - 1]!.track !== segment.track ? "" : null); |
| 116 | + }); |
| 117 | +} |
| 118 | + |
| 119 | +function unmarkRow(row: Element): void { |
| 120 | + mark(row, "data-fill", null); |
| 121 | + mark(row, "data-track-start", null); |
| 122 | +} |
| 123 | + |
| 82 | 124 | /** The element's size changing; nothing where ResizeObserver is missing. */ |
| 83 | 125 | function resizes(target: Element): Source<void> { |
| 84 | 126 | return sink => { |
| @@ -122,6 +164,9 @@ define( |
| 122 | 164 | console.warn(`<pebble-timeline>: ${t.skipped} body row(s) skipped: a segment's start (3rd cell) and length (4th) must be numbers`); |
| 123 | 165 | }); |
| 124 | 166 | |
| 167 | + subscribe(fromSignal(timeline))(t => markRows(table, t.segments)); |
| 168 | + onCleanup(() => table.querySelectorAll("tr[data-fill]").forEach(unmarkRow)); |
| 169 | + |
| 125 | 170 | const ax = computed(() => |
| 126 | 171 | axis(timeline(), { min: props.min(), max: props.max(), step: props.step(), baseline: props.baseline() }), |
| 127 | 172 | ); |
| @@ -163,7 +208,15 @@ define( |
| 163 | 208 | } else acts.next(name === "clear" ? { type: "activate", index: null } : { type: "step", by: name as Step, count: timeline().segments.length }); |
| 164 | 209 | }); |
| 165 | 210 | |
| 166 | | − subscribe(pipe(merge(resizes(host), pipe(fromSignal(timeline), mapTo(undefined))), throttleFrame()))(() => fitLabels(host)); |
| 211 | + // Labels are fitted again when the size, the rows, or a segment's width (once its transition ends) change. |
| 212 | + const widthSettled = pipe( |
| 213 | + on(host, "transitionend"), |
| 214 | + filter(event => event.propertyName === "width"), |
| 215 | + mapTo(undefined), |
| 216 | + ); |
| 217 | + subscribe(pipe(merge(resizes(host), pipe(fromSignal(timeline), mapTo(undefined)), widthSettled), throttleFrame()))(() => |
| 218 | + fitLabels(host), |
| 219 | + ); |
| 167 | 220 | |
| 168 | 221 | host.setAttribute("data-enhanced", ""); |
| 169 | 222 | onCleanup(() => host.removeAttribute("data-enhanced")); |
| @@ -206,31 +259,33 @@ define( |
| 206 | 259 | )} |
| 207 | 260 | </For> |
| 208 | 261 | </div> |
| 209 | | − <For each={timeline().tracks}> |
| 262 | + {/* Keyed by track, then by label within it: a re-read keeps each segment's element, so |
| 263 | + a changed row moves it (and CSS can glide it) instead of drawing a new one. */} |
| 264 | + <For each={timeline().tracks} key={track => track.name}> |
| 210 | 265 | {(track, i) => ( |
| 211 | 266 | <> |
| 212 | 267 | <span class="track-label" part="track-label" style={{ "--row": String(i() + 1), "--label-row": String(2 * i() + 1) }}> |
| 213 | | − {track.name} |
| 268 | + {track().name} |
| 214 | 269 | </span> |
| 215 | 270 | <div class="lane" part="lane" aria-hidden="true" style={{ "--row": String(i() + 1), "--lane-row": String(2 * i() + 2) }}> |
| 216 | | − <For each={track.segments}> |
| 271 | + <For each={track().segments} key={segment => segment.label}> |
| 217 | 272 | {(segment, j) => ( |
| 218 | 273 | <span |
| 219 | 274 | class="segment" |
| 220 | 275 | part="segment" |
| 221 | | − data-series={segment.series} |
| 222 | | − data-texture={segment.texture ?? undefined} |
| 276 | + data-series={segment().series} |
| 277 | + data-texture={segment().texture ?? undefined} |
| 223 | 278 | data-first={j() === 0 ? "" : undefined} |
| 224 | | − data-last={j() === track.segments.length - 1 ? "" : undefined} |
| 225 | | − data-active={state().active === segment.index ? "" : undefined} |
| 279 | + data-last={j() === track().segments.length - 1 ? "" : undefined} |
| 280 | + data-active={state().active === segment().index ? "" : undefined} |
| 226 | 281 | style={{ |
| 227 | | − "--start": String(place(segment, ax()).start), |
| 228 | | − "--size": String(place(segment, ax()).size), |
| 282 | + "--start": String(place(segment(), ax()).start), |
| 283 | + "--size": String(place(segment(), ax()).size), |
| 229 | 284 | }} |
| 230 | | − onMouseEnter={() => acts.next({ type: "activate", index: segment.index })} |
| 231 | | − onClick={() => choose(segment)} |
| 285 | + onMouseEnter={() => acts.next({ type: "activate", index: segment().index })} |
| 286 | + onClick={() => choose(segment())} |
| 232 | 287 | > |
| 233 | | − <span class="segment-label">{segment.label}</span> |
| 288 | + <span class="segment-label">{segment().label}</span> |
| 234 | 289 | </span> |
| 235 | 290 | )} |
| 236 | 291 | </For> |