Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1827x 2519x 2519x 1827x 2x 2x 2x 2x 238780x 207735x 207735x 207735x 238780x 2x 41x 41x 2x 2x 7x 7x 2x 2x 2x 2x 2x 17x 17x 17x 17x 17x 27x 17x 10x 17x 2x 2x 1x 1x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 2x 30x 30x 2x 7x 7x 3x 3x 7x 7x 2x 2x 7x 2x 5x 5x 2x 2x 408x 408x 23x 23x 23x 408x 2x 987x 987x 987x 987x 987x 987x 987x 2x 14637x 14637x 14637x 14637x 14637x 14637x 14637x 14637x 14637x 14637x 14637x 14637x 14637x | /**
* The small amount of DOM plumbing the views share.
*
* There is no framework here on purpose: the whole app is a handful of views
* over data that arrives once, so a virtual DOM would buy nothing that a
* `textContent` assignment does not. Number formatting lives in `src/units.ts`,
* where a worker can reach it, and is re-exported here so the views keep one
* import.
*/
export type Child = Node | string | null | undefined | false
export interface ElementOptions {
class?: string
text?: string
title?: string
id?: string
type?: string
value?: string
placeholder?: string
href?: string
disabled?: boolean
attrs?: Record<string, string>
style?: Partial<CSSStyleDeclaration>
on?: Partial<Record<keyof HTMLElementEventMap, (event: never) => void>>
}
/** Create an element, set the usual properties, append children. */
export function el<K extends keyof HTMLElementTagNameMap>(
tag: K,
options: ElementOptions = {},
...children: Child[]
): HTMLElementTagNameMap[K] {
const node = document.createElement(tag)
if (options.class !== undefined) node.className = options.class
if (options.text !== undefined) node.textContent = options.text
if (options.title !== undefined) node.title = options.title
if (options.id !== undefined) node.id = options.id
if (options.href !== undefined) node.setAttribute('href', options.href)
if (options.type !== undefined) node.setAttribute('type', options.type)
if (options.placeholder !== undefined) node.setAttribute('placeholder', options.placeholder)
if (options.value !== undefined) (node as HTMLInputElement).value = options.value
if (options.disabled === true) node.setAttribute('disabled', '')
if (options.attrs) for (const [k, v] of Object.entries(options.attrs)) node.setAttribute(k, v)
if (options.style) Object.assign(node.style, options.style)
if (options.on) {
for (const [name, handler] of Object.entries(options.on)) {
node.addEventListener(name, handler as EventListener)
}
}
append(node, children)
return node
}
/** Append children, skipping the falsy ones so `cond && el(...)` reads well. */
export function append(parent: Node, children: Child[]): void {
for (const child of children) {
if (child === null || child === undefined || child === false) continue
parent.appendChild(typeof child === 'string' ? document.createTextNode(child) : child)
}
}
/**
* Whether a drag is carrying files, rather than text or a row from a card.
*
* Asked before any drop target lights up, because the Tracks card's own rows
* are draggable — that is how a stack is reordered — and one dragged across the
* card must not offer to load itself. `types` is the only thing a browser
* exposes during a drag; the files themselves are readable at `drop` and not
* before, which is exactly the moment it is too late to have highlighted.
*/
export function dragHasFiles(event: DragEvent): boolean {
return Array.from(event.dataTransfer?.types ?? []).includes('Files')
}
/**
* The type a dragged **track row** carries, so a target can tell it from a file
* while the drag is still in the air.
*
* A custom MIME type and not `text/plain`, which the same drag also sets: a
* plain string is what a text selection dragged out of any input carries too,
* and the map's edge rectangles must not offer to move a track because someone
* dragged a word across them. Lowercase, because a browser lowercases every
* type it stores and a comparison against a capital would never match.
*/
export const TRACK_DRAG_TYPE = 'application/x-chromosomix-track'
/** Whether a drag is carrying one of the Tracks card's rows. */
export function dragHasTrack(event: DragEvent): boolean {
return Array.from(event.dataTransfer?.types ?? []).includes(TRACK_DRAG_TYPE)
}
/**
* Written **beside** {@link TRACK_DRAG_TYPE} when the row dragged is an overlay.
*
* An overlay is drawn on the map's face and not along one of its edges, so a
* map shows it the one target it has — *on this map* — and not four edges that
* would each mean the same thing. Which row is in the air cannot be read until
* it lands; its types can, so the kind travels as one.
*/
export const OVERLAY_DRAG_TYPE = 'application/x-chromosomix-overlay'
/** Whether a drag is carrying a row of the Tracks card that is an overlay. */
export function dragHasOverlay(event: DragEvent): boolean {
return Array.from(event.dataTransfer?.types ?? []).includes(OVERLAY_DRAG_TYPE)
}
/**
* The drop effect a target should answer a drag with: `wanted`, where the
* drag's source allows it, and otherwise the first effect it does allow.
*
* A target is not free to choose. A drop effect the source's `effectAllowed`
* does not include makes the browser **refuse the drop without a word** — no
* `drop` event, the ghost sliding back — however hard the target's `dragover`
* said yes. That is how an empty cell of the deck, answering `copy`, refused
* every row of the Tracks card, which allowed `move` alone; the tests that
* dispatch a `drop` directly never saw it, because only a browser checks.
*/
export function dropEffectFor(
transfer: DataTransfer,
wanted: 'copy' | 'move' | 'link',
): 'copy' | 'move' | 'link' | 'none' {
const allowed = (transfer.effectAllowed ?? 'uninitialized').toLowerCase()
const allows = (effect: string): boolean =>
allowed === 'all' || allowed === 'uninitialized' || allowed.includes(effect)
if (allows(wanted)) return wanted
return (['copy', 'move', 'link'] as const).find(allows) ?? 'none'
}
/**
* A row of the **Structures** card, dragged onto a cell of the deck (§25.3).
*
* Its own type beside {@link TRACK_DRAG_TYPE} rather than a reuse of it,
* because the two mean different things where they land: a track dropped on the
* map's edge is drawn along that edge, and a structure dropped on an empty cell
* turns that cell into the model. A single type would leave each drop target
* guessing which it had caught from an id alone, and only `types` is readable
* while the drag is still in the air.
*/
export const STRUCTURE_DRAG_TYPE = 'application/x-chromosomix-structure'
/** Whether a drag is carrying one of the Structures card's rows. */
export function dragHasStructure(event: DragEvent): boolean {
return Array.from(event.dataTransfer?.types ?? []).includes(STRUCTURE_DRAG_TYPE)
}
/**
* Give a one-line element its whole text as a tooltip, whenever the line is cut.
*
* For a line that ends in an ellipsis rather than wrapping — a view's summary
* under its title, which changes on every pan and zoom and moved the map by a
* line each time it wrapped (`ui/styles.css`). What the ellipsis hides has to
* be somewhere, and the tooltip is where a reader pointing at the cut looks.
* Only when it *is* cut: a tooltip repeating the line it sits on is noise.
*
* Watched rather than set by the views, because five of them write this line
* from a dozen places, and one of those places is a child element another
* object rewrites on every frame of a pan (`MatrixPanel.viewNote`).
*/
export function titleWhenCut(node: HTMLElement): void {
const sync = (): void => {
const text = (node.textContent ?? '').replace(/\s+/g, ' ').trim()
const cut = node.scrollWidth > node.clientWidth + 1
const title = cut ? text : ''
if (node.title !== title) node.title = title
}
if (typeof MutationObserver !== 'undefined') {
new MutationObserver(sync).observe(node, { childList: true, characterData: true, subtree: true })
}
if (typeof ResizeObserver !== 'undefined') new ResizeObserver(sync).observe(node)
sync()
}
/**
* A **map** dragged by its grip on the Contact maps card, towards a cell of the
* deck (§16.9). Its own type, beside the track's and the structure's, for their
* reason: a cell has to tell what it is being offered while the drag is still
* in the air, and only `types` can be read then.
*
* The payload says which map: the session's own (`lead`), the one in its lower
* triangle (`compare`), a companion already in a cell, by the deck's id, or one
* the reader's eye took out of the deck (`hidden`), by the session's id for it.
*/
export const MAP_ROW_DRAG_TYPE = 'application/x-chromosomix-map'
export type MapRow =
| { kind: 'lead' }
| { kind: 'compare' }
| { kind: 'companion'; id: string }
| { kind: 'hidden'; id: string }
/** Whether a drag is carrying a row of the Contact maps card. */
export function dragHasMapRow(event: DragEvent): boolean {
return Array.from(event.dataTransfer?.types ?? []).includes(MAP_ROW_DRAG_TYPE)
}
/** The map a drop is carrying, or `null` where it carries none this build reads. */
export function readMapRow(event: DragEvent): MapRow | null {
const raw = event.dataTransfer?.getData(MAP_ROW_DRAG_TYPE) ?? ''
if (raw === '') return null
try {
const row = JSON.parse(raw) as Record<string, unknown>
if (row['kind'] === 'lead' || row['kind'] === 'compare') return { kind: row['kind'] }
if ((row['kind'] === 'companion' || row['kind'] === 'hidden') && typeof row['id'] === 'string' && row['id'] !== '') {
return { kind: row['kind'], id: row['id'] }
}
} catch {
// A payload this build did not write is a drop of nothing.
}
return null
}
/** Remove every child of a node. */
export function clear(node: Node): void {
while (node.firstChild) node.removeChild(node.firstChild)
}
export { fmt, trimNumber } from '../units'
/** Trailing-edge debounce, for the table's search and filter boxes. */
export function debounce<A extends unknown[]>(fn: (...args: A) => void, ms: number): (...args: A) => void {
let timer: ReturnType<typeof setTimeout> | undefined
return (...args: A) => {
if (timer !== undefined) clearTimeout(timer)
timer = setTimeout(() => fn(...args), ms)
}
}
/** A labelled control, the layout every toolbar in the app uses. */
export function field(label: string, control: HTMLElement, hint?: string): HTMLElement {
return el(
'label',
{ class: 'field', title: hint ?? '' },
el('span', { class: 'field-label', text: label }),
control,
)
}
/** A `<button>` with a click handler. */
export function button(
label: string,
onClick: () => void,
options: { class?: string; title?: string } = {},
): HTMLButtonElement {
const node = el('button', {
class: options.class ?? 'btn',
text: label,
title: options.title ?? '',
type: 'button',
})
node.addEventListener('click', onClick)
return node
}
|