96 lines
4.1 KiB
Swift
96 lines
4.1 KiB
Swift
import Foundation
|
|
|
|
/// Which lines, cards and rows light up for the current hover, selection and search.
|
|
public struct SchemaHighlight: Sendable, Equatable {
|
|
/// The hovered table, else the selected one.
|
|
public var focus: String?
|
|
/// Ids (`table.column`) of the edges drawn in amber.
|
|
public var activeEdges: Set<String>
|
|
/// Both ends of every active edge, plus the focus.
|
|
public var relatedTables: Set<String>
|
|
/// Cards drawn at 30% opacity.
|
|
public var dimmedTables: Set<String>
|
|
/// Cards whose name holds the search query; empty without a query.
|
|
public var searchMatches: Set<String>
|
|
/// The foreign-key rows and the rows they reference, of every active edge.
|
|
public var highlightedColumns: Set<SchemaColumnRef>
|
|
/// True when inactive lines take the receded colour (a focus, a hovered key, or a query).
|
|
public var linesRecede: Bool
|
|
|
|
/// A hovered foreign key (`table.column`) narrows the highlight to its one edge and wins over the focus.
|
|
///
|
|
/// The search only dims cards while nothing is focused.
|
|
public init(model: SchemaModel, hovered: String?, selected: String?, hoveredColumn: String?, query: String) {
|
|
let focus = hovered ?? selected
|
|
let needle = query.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
|
|
let active = model.edges.filter { edge in
|
|
if let hoveredColumn { return edge.id == hoveredColumn }
|
|
guard let focus else { return false }
|
|
return edge.from == focus || edge.to == focus
|
|
}
|
|
var related = Set(active.flatMap { [$0.from, $0.to] })
|
|
if let focus { related.insert(focus) }
|
|
|
|
var columns = Set<SchemaColumnRef>()
|
|
for edge in active {
|
|
columns.insert(SchemaColumnRef(table: edge.from, column: edge.column))
|
|
if let target = model.table(named: edge.to), edge.targetColumnIndex < target.columns.count {
|
|
columns.insert(SchemaColumnRef(table: edge.to, column: target.columns[edge.targetColumnIndex].name))
|
|
}
|
|
}
|
|
|
|
let names = model.tables.map(\.name)
|
|
let matches = needle.isEmpty ? Set<String>() : Set(names.filter { $0.lowercased().contains(needle) })
|
|
let dimsAll = focus != nil || hoveredColumn != nil
|
|
let dimmed: Set<String> = if dimsAll {
|
|
Set(names.filter { !related.contains($0) })
|
|
} else if !needle.isEmpty {
|
|
Set(names.filter { !matches.contains($0) })
|
|
} else {
|
|
[]
|
|
}
|
|
|
|
self.focus = focus
|
|
activeEdges = Set(active.map(\.id))
|
|
relatedTables = related
|
|
dimmedTables = dimmed
|
|
searchMatches = matches
|
|
highlightedColumns = columns
|
|
linesRecede = dimsAll || !needle.isEmpty
|
|
}
|
|
|
|
public func isActive(_ edge: SchemaEdge) -> Bool {
|
|
activeEdges.contains(edge.id)
|
|
}
|
|
|
|
public func isHighlighted(table: String, column: String) -> Bool {
|
|
highlightedColumns.contains(SchemaColumnRef(table: table, column: column))
|
|
}
|
|
|
|
/// 1 or 0.3.
|
|
public func opacity(of table: String) -> Double {
|
|
dimmedTables.contains(table) ? 0.3 : 1
|
|
}
|
|
|
|
/// The edges in drawing order: inactive first, so active lines sit on top.
|
|
public func drawingOrder(_ edges: [SchemaEdge]) -> [SchemaEdge] {
|
|
edges.filter { !isActive($0) } + edges.filter(isActive)
|
|
}
|
|
}
|
|
|
|
public enum SchemaSearch {
|
|
/// The tables whose name holds `query` (case-insensitive), in name order; all of them for an empty query.
|
|
public static func filter(_ model: SchemaModel, query: String) -> [SchemaTable] {
|
|
let needle = query.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
|
|
guard !needle.isEmpty else { return model.tables }
|
|
return model.tables.filter { $0.name.lowercased().contains(needle) }
|
|
}
|
|
|
|
/// `[ TABLES ] 16`, or `[ TABLES ] 3 of 16` while filtering.
|
|
public static func listLabel(_ model: SchemaModel, query: String) -> String {
|
|
let total = model.tables.count
|
|
let isFiltering = !query.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
|
|
return isFiltering ? "[ TABLES ] \(filter(model, query: query).count) of \(total)" : "[ TABLES ] \(total)"
|
|
}
|
|
}
|