aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuben Beltran del Rio <git@r.bdr.sh>2025-07-08 09:42:56 +0200
committerRuben Beltran del Rio <git@r.bdr.sh>2025-07-08 09:42:56 +0200
commit96185dcaa7e2a0f475e6519a94f53a90920af93f (patch)
tree6d5a4254a1cb397effca8da85cb8dcbe4c515fa8
parent68c53f41cf165e42f40e12fe724043b014bc9b68 (diff)
Use line maps for quicker vertex drag updates
-rw-r--r--Map/Presentation/MapEditor.swift47
1 files changed, 46 insertions, 1 deletions
diff --git a/Map/Presentation/MapEditor.swift b/Map/Presentation/MapEditor.swift
index 56ac898..73302df 100644
--- a/Map/Presentation/MapEditor.swift
+++ b/Map/Presentation/MapEditor.swift
@@ -34,6 +34,7 @@ struct MapEditor: View {
@State var selectedTerm = 0
@State var results: [Range<String.Index>] = []
+ @State private var vertexLineMap: [String: (lineIndex: Int, shapeText: String)] = [:]
private func updateRanges() {
if !isSearching || searchTerm.isEmpty {
@@ -115,7 +116,14 @@ struct MapEditor: View {
document: $document, evolution: $selectedEvolution, onDragVertex: onDragVertex
).scaleEffect(zoom, anchor: .center).frame(
width: (Dimensions.Map.size.width + 2 * Dimensions.Map.padding) * zoom,
- height: (Dimensions.Map.size.height + 2 * Dimensions.Map.padding) * zoom)
+ height: (Dimensions.Map.size.height + 2 * Dimensions.Map.padding) * zoom
+ )
+ .onAppear {
+ buildVertexLineMap()
+ }
+ .onChange(of: document.text) { _, _ in
+ buildVertexLineMap()
+ }
}.background(Color.Theme.UI.background)
.gesture(
MagnificationGesture()
@@ -181,7 +189,44 @@ struct MapEditor: View {
return (formatter.string(from: NSNumber(value: number)) ?? "") + "x"
}
+ private func buildVertexLineMap() {
+ let lines = document.text.components(separatedBy: .newlines)
+ let vertexPattern =
+ "^([^\\(\\[\\]]*?)[\\s]*\\([\\s]*([0-9]+.?[0-9]*)[\\s]*,[\\s]*([0-9]+.?[0-9]*)[\\s]*\\)[\\s]*(?:\\[(.*?)\\])?[\\s]*$"
+
+ vertexLineMap.removeAll()
+
+ for (index, line) in lines.enumerated() {
+ if let regex = try? NSRegularExpression(pattern: vertexPattern, options: [.caseInsensitive]),
+ let match = regex.firstMatch(in: line, range: NSRange(line.startIndex..., in: line))
+ {
+
+ let nameRange = Range(match.range(at: 1), in: line)!
+ let name = String(line[nameRange]).trimmingCharacters(in: .whitespaces)
+
+ let shapeRange = match.range(at: 4)
+ let shapeText =
+ shapeRange.location != NSNotFound
+ ? " [\(String(line[Range(shapeRange, in: line)!]))]" : ""
+
+ vertexLineMap[name] = (lineIndex: index, shapeText: shapeText)
+ }
+ }
+ }
+
private func onDragVertex(vertex: Vertex, x: CGFloat, y: CGFloat) {
+
+ guard let lineInfo = vertexLineMap[vertex.label] else { return }
+
+ let lines = document.text.components(separatedBy: .newlines)
+ var updatedLines = lines
+
+ // Directly update the line without regex
+ updatedLines[lineInfo.lineIndex] =
+ "\(vertex.label) (\(String(format: "%.2f", x)), \(String(format: "%.2f", y)))\(lineInfo.shapeText)"
+
+ // Update document text
+ document.text = updatedLines.joined(separator: "\n")
}
}