From 064871b687068dbccff80eb80bd13e575a70b34b Mon Sep 17 00:00:00 2001 From: Roman Priesol Date: Fri, 27 Nov 2026 12:47:58 +0100 Subject: [PATCH] Google saved places: a name or description changed here survives a new export (the last imported text is kept per point, a field differing from it counts as a hand edit); the popup marks such a point as edited --- lib/maptool/library.rb | 19 ++++++++++++++++--- public/css/style.css | 6 ++++++ public/js/gpx.js | 9 ++++++++- test/test_google_saved.rb | 9 ++++++++- 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/lib/maptool/library.rb b/lib/maptool/library.rb index 6668eee..051434b 100644 --- a/lib/maptool/library.rb +++ b/lib/maptool/library.rb @@ -319,8 +319,12 @@ module Maptool props = (feature["properties"] || {}).except("id") write do |db, rev, now| # Moved by hand: an imported approximate position (GoogleSaved) is checked now. - old = db.get_first_value("SELECT geometry FROM items WHERE id = ?", [id]) + old, old_props = db.get_first_row("SELECT geometry, properties FROM items WHERE id = ?", [id]) props.delete("approximate") if old && round(JSON.parse(old)) != round(feature["geometry"]) + # What the last import said stays, whatever the popup sent back + # (upsert_by_source_id tells a hand edit from it). + imported = old_props && JSON.parse(old_props)["imported"] + props["imported"] = imported if imported db.execute("UPDATE items SET name = ?, geometry = ?, properties = ?, updated_at = ?, rev = ? WHERE id = ? AND deleted_at IS NULL", [props["name"], JSON.generate(feature["geometry"]), JSON.generate(props), now, rev, id]) raise NotFound if db.changes.zero? @@ -633,9 +637,13 @@ module Maptool # saved place's id) is kept once per folder: importing the list again # refreshes the point's name and Google's fields, keeping what was # changed here (color, icon, a moved position — only an approximate - # position gives way to an exact one). One deleted here stays deleted. + # position gives way to an exact one, a renamed or redescribed point + # keeps its own text). One deleted here stays deleted. # Returns the id, nil when skipped. + IMPORTED_TEXT = %w[name description].freeze + def upsert_by_source_id(db, folder, props, geometry, rev, now) + props = props.merge("imported" => props.slice(*IMPORTED_TEXT)) row = db.get_first_row("SELECT id, geometry, properties, deleted_at FROM items WHERE folder_id IS ? AND json_extract(properties, '$.source_id') = ?", [folder, props["source_id"]]) unless row @@ -649,13 +657,18 @@ module Maptool old = JSON.parse(old_props) merged = old.merge(props.except("id", "approximate")) + # A field differing from what the last import said was edited here. + # Points imported before "imported" was kept: any difference counts + # as an edit, the safe guess. + last = old["imported"] || {} + IMPORTED_TEXT.each { |f| merged[f] = old[f] if old.key?(f) && old[f] != last.fetch(f, props[f]) } exact = old["approximate"] && !props["approximate"] merged.delete("approximate") if exact new_geometry = exact ? JSON.generate(geometry) : old_geometry return id if merged == old && new_geometry == old_geometry db.execute("UPDATE items SET name = ?, geometry = ?, properties = ?, updated_at = ?, rev = ? WHERE id = ?", - [props["name"], new_geometry, JSON.generate(merged), now, rev, id]) + [merged["name"], new_geometry, JSON.generate(merged), now, rev, id]) id end diff --git a/public/css/style.css b/public/css/style.css index 310ce3a..a3acc04 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -1962,6 +1962,12 @@ body.side-pinned:has(.basemap-panel:not([hidden]), .gpx-panel:not([hidden])) .pr height: 100%; } +.gpx-popup__edited { + margin: 4px 0; + font-size: 12px; + color: #5a6b3a; +} + .gpx-popup__approximate { margin: 4px 0; font-size: 12px; diff --git a/public/js/gpx.js b/public/js/gpx.js index 3d388f3..30d0424 100644 --- a/public/js/gpx.js +++ b/public/js/gpx.js @@ -1536,7 +1536,7 @@ function nodeKeys(node) { // for — see _registerPointInteractivity) adds Edit/Delete buttons; the // click handling for them lives in _wireNoteActions, not here, since this // only builds markup, it has no map/store access of its own. -function popupHtml({ name, description, time, category, photos, icon, approximate }, { editable = false, movable = false } = {}) { +function popupHtml({ name, description, time, category, photos, icon, approximate, imported }, { editable = false, movable = false } = {}) { const parts = []; if (name) { const glyph = icon && POI_ICONS[icon] ? `${POI_ICONS[icon]}` : ""; @@ -1546,6 +1546,13 @@ function popupHtml({ name, description, time, category, photos, icon, approximat if (photos?.length > 0) parts.push(formatPhotos(photos)); // Google saved places Nominatim couldn't pin down (GoogleSaved). if (approximate) parts.push(`

≈ Approximate position, check it

`); + // Renamed or redescribed here: a new Google export leaves it as it is + // (Library#upsert_by_source_id). MapLibre hands nested properties back + // as JSON text. + const last = typeof imported === "string" ? JSON.parse(imported) : imported; + if (last && ((last.name ?? "") !== (name ?? "") || (last.description ?? "") !== (description ?? ""))) { + parts.push(`

✎ Edited here, Google updates keep your text

`); + } // A Note's description is self-authored by the (logged-in) viewer, not // uploaded/untrusted like every other description here — formatDescription // is deliberately left alone for that untrusted case (see diff --git a/test/test_google_saved.rb b/test/test_google_saved.rb index e217b7c..0fc1986 100644 --- a/test/test_google_saved.rb +++ b/test/test_google_saved.rb @@ -54,9 +54,13 @@ class GoogleSavedTest < Minitest::Test reine = points.call.find { |f| f["properties"]["name"] == "Reine" } reine["properties"]["color"] = "#ff0000" library.update_feature("points:x", reine["properties"]["id"], reine) + somewhere = points.call.find { |f| f["properties"]["name"] == "Somewhere" } + somewhere["properties"]["name"] = "Bratislava" + somewhere["properties"].delete("imported") # the popup needn't send it back + library.update_feature("points:x", somewhere["properties"]["id"], somewhere) sleep 1.1 # a newer export (mtime) with a changed note, now found by name - write_zip.call("takeout-2.zip", CSV_TEXT.sub("red huts", "rorbuer")) + write_zip.call("takeout-2.zip", CSV_TEXT.sub("red huts", "rorbuer").sub("Somewhere,,", "Somewhere,a note,")) Maptool::GoogleSaved.import_takeout(takeout, library: library, state_path: state, lookup: ->(*) { [13.0897, 67.9322] }) features = points.call @@ -66,5 +70,8 @@ class GoogleSavedTest < Minitest::Test assert_equal "#ff0000", reine["properties"]["color"], "own changes kept" assert_equal [13.0897, 67.9322], reine["geometry"]["coordinates"], "approximate spot replaced by the exact one" refute reine["properties"].key?("approximate") + renamed = features.find { |f| f["properties"]["source_id"].include?("48.1486") } + assert_equal "Bratislava", renamed["properties"]["name"], "a name changed here is kept" + assert_equal "a note", renamed["properties"]["description"].lines.first.strip, "an untouched description still updates" end end