77 lines
2.9 KiB
TypeScript
77 lines
2.9 KiB
TypeScript
|
|
// Finding things across the whole project: parts by name, kind, component,
|
||
|
|
// signal or panel row, in every circuit; and components with where they are
|
||
|
|
// placed.
|
||
|
|
|
||
|
|
import { KIND_NAMES } from './progress'
|
||
|
|
import type { CircuitNode, ComponentDef, Project } from './types'
|
||
|
|
|
||
|
|
export interface PartHit {
|
||
|
|
/** The circuit it is in. */
|
||
|
|
def: string
|
||
|
|
defName: string
|
||
|
|
node: string
|
||
|
|
/** What it is, e.g. "Input switch A0" or "Adder 4 U1". */
|
||
|
|
text: string
|
||
|
|
/** What matched, when it isn't in the text (a panel row, a note's words). */
|
||
|
|
detail?: string
|
||
|
|
kind: CircuitNode['kind']
|
||
|
|
score: number
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface ComponentHit {
|
||
|
|
def: ComponentDef
|
||
|
|
/** Circuits that place it, with the parts placed. */
|
||
|
|
usedIn: { def: ComponentDef; nodes: string[] }[]
|
||
|
|
score: number
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 3 for the whole text, 2 for its start, 1 for a word start, 0.5 anywhere inside, 0 not at all. */
|
||
|
|
function rank(text: string, q: string): number {
|
||
|
|
const t = text.toLowerCase()
|
||
|
|
if (!t || !q) return 0
|
||
|
|
if (t === q) return 3
|
||
|
|
if (t.startsWith(q)) return 2
|
||
|
|
if (t.split(/[\s_\-/.:]+/).some(w => w.startsWith(q))) return 1
|
||
|
|
return t.includes(q) ? 0.5 : 0
|
||
|
|
}
|
||
|
|
|
||
|
|
export function searchProject(project: Project, query: string, limit = 60): { components: ComponentHit[]; parts: PartHit[] } {
|
||
|
|
const q = query.trim().toLowerCase()
|
||
|
|
if (!q) return { components: [], parts: [] }
|
||
|
|
const byId = new Map(project.components.map(d => [d.id, d]))
|
||
|
|
|
||
|
|
const components: ComponentHit[] = []
|
||
|
|
for (const d of project.components) {
|
||
|
|
const score = rank(d.name, q)
|
||
|
|
if (!score) continue
|
||
|
|
const usedIn = project.components
|
||
|
|
.map(u => ({ def: u, nodes: u.nodes.filter(n => n.kind === 'chip' && n.chip === d.id).map(n => n.id) }))
|
||
|
|
.filter(u => u.def.id !== d.id && u.nodes.length)
|
||
|
|
components.push({ def: d, usedIn, score: score + (usedIn.length ? 0.1 : 0) })
|
||
|
|
}
|
||
|
|
|
||
|
|
const parts: PartHit[] = []
|
||
|
|
for (const d of project.components) {
|
||
|
|
for (const n of d.nodes) {
|
||
|
|
const kind = n.kind === 'chip' ? ((n.chip && byId.get(n.chip)?.name) ?? 'Missing component') : (KIND_NAMES[n.kind] ?? n.kind)
|
||
|
|
const label = n.label?.trim() ?? ''
|
||
|
|
const text = n.kind === 'note' ? `Note` : label ? `${kind} ${label}` : kind
|
||
|
|
let score = Math.max(rank(label, q) * 2, rank(kind, q))
|
||
|
|
let detail: string | undefined
|
||
|
|
if (n.kind === 'note' && rank(label, q)) detail = label.split('\n')[0]
|
||
|
|
for (const r of n.rows ?? []) {
|
||
|
|
const row = r.label?.trim() || r.name?.trim() || ''
|
||
|
|
const s = rank(row, q) * 2
|
||
|
|
if (s > score) {
|
||
|
|
score = s
|
||
|
|
detail = `row ${row}`
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (score) parts.push({ def: d.id, defName: d.name, node: n.id, text, detail, kind: n.kind, score })
|
||
|
|
}
|
||
|
|
}
|
||
|
|
components.sort((a, b) => b.score - a.score || a.def.name.localeCompare(b.def.name))
|
||
|
|
parts.sort((a, b) => b.score - a.score || a.defName.localeCompare(b.defName) || a.text.localeCompare(b.text))
|
||
|
|
return { components: components.slice(0, 20), parts: parts.slice(0, limit) }
|
||
|
|
}
|