diff --git a/CITATION.cff b/CITATION.cff index e72d03c..d8008c6 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -2,7 +2,7 @@ cff-version: 1.2.0 message: "Si usas este software, por favor cítalo así. / If you use this software, please cite it as below." title: "Vector editor: in-browser cloning simulator with REBASE enzymes" type: software -version: 0.4.1 +version: 0.4.2 authors: - family-names: "Quintero López" given-names: "Oscar Alexis" diff --git a/package.json b/package.json index bcf584f..a826b63 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@oquinterol/vector-editor", - "version": "0.4.1", + "version": "0.4.2", "description": "In-browser cloning simulator: choose commercial or custom restriction enzymes, digest and ligate, and get a live FASTA and plasmid map.", "license": "MIT", "author": "Oscar Alexis Quintero López (https://oquinterol.com)", diff --git a/src/ui/VectorEditor.tsx b/src/ui/VectorEditor.tsx index 6d204d6..f49abde 100644 --- a/src/ui/VectorEditor.tsx +++ b/src/ui/VectorEditor.tsx @@ -13,7 +13,7 @@ import { type Fragment } from '../core' import { EnzymeCombobox } from './EnzymeCombobox' -import { FastaView } from './FastaView' +import { FastaView, type SeqRange } from './FastaView' import { SequenceEditor } from './SequenceEditor' import { labelsEn, type Labels } from './labels' import { fastaOf, useCloning, type Filter } from './useCloning' @@ -64,6 +64,13 @@ function saveFile(name: string, text: string, extension: string, type: string) { type Tab = 'map' | 'sequence' | 'build' +type LegendItem = { + key: string + kind: 'vector' | 'insert' | 'site' + name: string + ranges: SeqRange[] +} + const isTyping = (target: EventTarget | null) => target instanceof HTMLElement && (target.isContentEditable || ['INPUT', 'TEXTAREA', 'SELECT'].includes(target.tagName)) @@ -294,20 +301,128 @@ export function VectorEditor({ : 1, color: region.kind === 'insert' ? colors.insert : colors.vector })) - const legend = [ + // Legend: one chip per region and one per enzyme (its sites grouped, cycled on tap). + const seqLength = c.resultSeq.length + const siteRange = (start: number, length: number): SeqRange => ({ + start, + end: (start + length) % seqLength || seqLength + }) + const siteGroups = new Map() + for (const site of c.sites) { + const ranges = siteGroups.get(site.enzyme) ?? [] + ranges.push(siteRange(site.start, site.length)) + siteGroups.set(site.enzyme, ranges) + } + const legend: LegendItem[] = [ ...c.regions.map((region) => ({ key: region.kind, kind: region.kind, - label: region.kind === 'insert' ? c.insert.name : c.vector.name, - range: { start: region.start, end: region.end } + name: region.kind === 'insert' ? c.insert.name : c.vector.name, + ranges: [{ start: region.start, end: region.end }] })), - ...c.sites.map((site, i) => ({ - key: `${site.enzyme}-${site.start}-${i}`, + ...[...siteGroups].map(([enzyme, ranges]) => ({ + key: `site-${enzyme}`, kind: 'site' as const, - label: `${site.enzyme} ${site.start + 1}`, - range: { start: site.start, end: (site.start + site.length) % c.resultSeq.length || c.resultSeq.length } + name: enzyme, + ranges: ranges.sort((x, y) => x.start - y.start) })) ] + const sameRange = (x: SeqRange, y: SeqRange | null) => !!y && x.start === y.start && x.end === y.end + const active = (() => { + for (const item of legend) { + const index = item.ranges.findIndex((r) => sameRange(r, c.range)) + if (index >= 0) return { item, index } + } + return null + })() + const chipLabel = (item: LegendItem) => + item.kind === 'site' && item.ranges.length > 1 + ? `${item.name} ×${item.ranges.length}` + : item.kind === 'site' + ? `${item.name} ${item.ranges[0]!.start + 1}` + : item.name + const onChip = (item: LegendItem) => { + const current = active?.item.key === item.key ? active.index : -1 + c.select(item.ranges[(current + 1) % item.ranges.length]!, 'legend') + } + // What a tap on the map points at, so the matching chip lights up. + const resolveMapSelection = (selection: { type?: string; name?: string; start: number; end: number }) => { + const distance = (a: number, b: number) => { + const d = Math.abs(a - b) + return Math.min(d, seqLength - d) + } + if (selection.type === 'ENZYME' && selection.name) { + const ranges = siteGroups.get(selection.name) + if (ranges?.length) { + return [...ranges].sort((x, y) => distance(x.start, selection.start) - distance(y.start, selection.start))[0]! + } + } + if (selection.type === 'ANNOTATION') { + const region = legend.find((item) => item.kind !== 'site' && item.name === selection.name) + if (region) return region.ranges[0]! + } + return { start: selection.start, end: selection.end } + } + const rangeLength = (r: SeqRange) => (r.start <= r.end ? r.end - r.start : seqLength - r.start + r.end) + const pill = !c.range + ? null + : active?.item.kind === 'site' + ? `${active.item.name} · ${(c.range.start + 1).toLocaleString()}${ + active.item.ranges.length > 1 ? ` · ${active.index + 1}/${active.item.ranges.length}` : '' + }` + : `${active ? `${active.item.name} · ` : ''}${(c.range.start + 1).toLocaleString()}–${( + c.range.end || seqLength + ).toLocaleString()} · ${rangeLength(c.range).toLocaleString()} ${labels.bp}` + + // Phones: a tap anywhere near the ring picks the closest cut site (within ~8°) or the + // region under it. SeqViz's own marks are a few pixels wide, too small for a finger. + const tapStart = useRef<{ x: number; y: number } | null>(null) + const pickFromTap = (x: number, y: number) => { + const ring = [...(root.current?.querySelectorAll('.ve-map .la-vz-index-line') ?? [])] + if (!ring.length || !seqLength) return + const boxes = ring.map((path) => path.getBoundingClientRect()) + const left = Math.min(...boxes.map((b) => b.left)) + const right = Math.max(...boxes.map((b) => b.right)) + const top = Math.min(...boxes.map((b) => b.top)) + const bottom = Math.max(...boxes.map((b) => b.bottom)) + const radius = (right - left) / 2 + const dx = x - (left + right) / 2 + const dy = y - (top + bottom) / 2 + const distance = Math.hypot(dx, dy) + if (distance < radius * 0.45 || distance > radius * 1.4) return + // 0 at 12 o'clock, growing clockwise, as SeqViz draws it. + const angle = (Math.atan2(dx, -dy) + 2 * Math.PI) % (2 * Math.PI) + const index = Math.floor((angle / (2 * Math.PI)) * seqLength) + const gap = (a: number, b: number) => { + const d = Math.abs(a - b) + return Math.min(d, seqLength - d) + } + const tolerance = seqLength * (8 / 360) + let best: SeqRange | null = null + let bestGap = Infinity + for (const ranges of siteGroups.values()) { + for (const r of ranges) { + const g = gap(r.start, index) + if (g < bestGap) { + best = r + bestGap = g + } + } + } + if (best && bestGap <= tolerance) return c.select(best, 'map') + const region = c.regions.find((r) => index >= r.start && index < r.end) + if (region) c.select({ start: region.start, end: region.end }, 'map') + } + + // Keep the active chip in view in the swipeable row (phones). + const chipRefs = useRef(new Map()) + useEffect(() => { + if (!compact || !active) return + const reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches + chipRefs.current + .get(active.item.key) + ?.scrollIntoView({ inline: 'nearest', block: 'nearest', behavior: reduce ? 'auto' : 'smooth' }) + }, [compact, active?.item.key, active?.index]) return (
-
+
{ + tapStart.current = { x: event.clientX, y: event.clientY } + }} + onPointerUp={(event) => { + const start = tapStart.current + tapStart.current = null + // A tap, not a drag or pinch. + if (!compact || !start || Math.hypot(event.clientX - start.x, event.clientY - start.y) > 10) return + pickFromTap(event.clientX, event.clientY) + }} + > + {pill && ( +

+ {pill} +

+ )} {c.resultSeq && ( { + // Phones use the tap handler on the map container instead. + if (compact) return const { start, end } = selection if (start === undefined || end === undefined || start === end) return - c.select(selection.clockwise === false ? { start: end, end: start } : { start, end }, 'map') + const raw = selection.clockwise === false ? { start: end, end: start } : { start, end } + c.select(resolveMapSelection({ type: selection.type, name: selection.name, ...raw }), 'map') }} style={{ height: '100%', width: '100%' }} /> @@ -661,11 +799,16 @@ export function VectorEditor({ ))} diff --git a/src/ui/styles.css b/src/ui/styles.css index 70147fb..c3ad70e 100644 --- a/src/ui/styles.css +++ b/src/ui/styles.css @@ -976,6 +976,10 @@ padding-right: 0.75rem; white-space: nowrap; } +.ve[data-tab='map'] .ve-selection > span { + /* On the map the pill already names the selection; keep only its actions here. */ + display: none; +} .ve[data-compact] .ve-selection > span { overflow: hidden; text-overflow: ellipsis; @@ -987,6 +991,36 @@ display: none; } +/* Phones: the map only draws; names live in the chips and the pill, so no "+1" clusters. */ +.ve[data-compact] .ve-map .la-vz-circular-label, +.ve[data-compact] .ve-map .la-vz-label-line { + display: none; +} +.ve-map-pill { + display: none; +} +.ve[data-compact] .ve-map-pill { + position: absolute; + top: 0.75rem; + left: 50%; + z-index: 2; + display: block; + max-width: calc(100% - 1.5rem); + margin: 0; + padding: 0.3rem 0.75rem; + overflow: hidden; + border: 1px solid var(--ve-accent); + border-radius: 999px; + background: color-mix(in srgb, var(--ve-bg) 88%, transparent); + color: var(--ve-ink); + font-family: var(--ve-mono); + font-size: 0.78rem; + text-overflow: ellipsis; + white-space: nowrap; + transform: translateX(-50%); + pointer-events: none; +} + /* Editing vector or insert on a phone: a full-screen sheet with a large field. */ .ve-sheet-done { display: none;