Installable PWA with a share target; design for an item-based library
CI / check (push) Successful in 18s
CI / deploy (push) Successful in 13s

Android's share sheet (OsmAnd, Locus, mapy.com, ...) can now send
GPX/FIT/GeoJSON/ZIP files to the installed app: the service worker parks
them and the page offers to import them into a Tracks & Points folder,
waiting through a login if needed. Manifest, app icon, a server-side
fallback for /share-target.

docs/library-design.adoc proposes replacing the file-based GpxStore with
folders and items (stable ids, timestamps, tombstones, a change feed) as
the base for syncing with OsmAnd and Locus.
This commit is contained in:
randy committed 2026-11-12 13:42:04 +01:00
1 parent e2461f794c
commit 25b87b94fe
14 files changed
+637 -5

No files matched your search

+120
View File
@@ -0,0 +1,120 @@
import { isLoggedIn } from "/js/auth.js";
import { escapeHtml } from "/js/gpx.js";
// Files shared to the installed app from Android's share sheet (OsmAnd,
// Locus, mapy.com, a file manager, ...): public/sw.js parks them in this
// cache and opens the app; this offers to import them into a folder of
// Tracks & Points. Checked on every page load, not just right after a
// share, so a share made while logged out is still waiting after login.
const SHARED_CACHE = "shared-files";
const KNOWN_EXTENSION = /\.(gpx|fit|geojson|zip)$/i;
// Some apps share a bare name ("Track 12") — the upload only accepts
// these extensions, so one is added from the file's type.
const EXTENSION_BY_TYPE = {
"application/gpx+xml": ".gpx",
"application/xml": ".gpx",
"text/xml": ".gpx",
"application/geo+json": ".geojson",
"application/json": ".geojson",
"application/zip": ".zip",
"application/vnd.ant.fit": ".fit",
};
const DEFAULT_FOLDER = "Shared";
export async function offerSharedFiles(gpxPanel) {
if (new URLSearchParams(window.location.search).has("shared")) window.history.replaceState(null, "", "/");
if (!("caches" in window)) return;
let cache;
let requests;
try {
cache = await caches.open(SHARED_CACHE);
requests = await cache.keys();
} catch {
return; // storage blocked — nothing to offer
}
if (requests.length === 0) return;
const files = await Promise.all(
requests.map(async (request) => {
const response = await cache.match(request);
const blob = await response.blob();
return new File([blob], withExtension(decodeURIComponent(response.headers.get("x-filename") || "shared"), blob.type), {
type: blob.type,
});
}),
);
const discard = () => Promise.all(requests.map((request) => cache.delete(request)));
const card = document.createElement("div");
card.className = "share-import";
card.innerHTML = `
<strong>Shared to MapTool</strong>
<ul class="share-import__files">${files.map((file) => `<li>${escapeHtml(file.name)}</li>`).join("")}</ul>
${
isLoggedIn()
? `<label class="share-import__folder">Folder
<input type="text" value="${DEFAULT_FOLDER}" list="share-import-folders">
<datalist id="share-import-folders"></datalist>
</label>
<div class="share-import__actions">
<button type="button" data-action="import">Import</button>
<button type="button" data-action="discard">Discard</button>
</div>`
: `<p>Log in to import — they'll wait here until then.</p>
<div class="share-import__actions">
<a href="/login">Log in</a>
<button type="button" data-action="discard">Discard</button>
</div>`
}
<p class="share-import__status" hidden></p>
`;
document.body.appendChild(card);
card.querySelector('[data-action="discard"]').addEventListener("click", async () => {
await discard();
card.remove();
});
const importButton = card.querySelector('[data-action="import"]');
if (!importButton) return;
fillFolderSuggestions(card.querySelector("datalist"));
importButton.addEventListener("click", async () => {
const folder = card.querySelector(".share-import__folder input").value.trim();
card.querySelectorAll("button").forEach((button) => (button.disabled = true));
const paths = await gpxPanel.importFiles(files, folder);
if (paths.length > 0) {
await discard();
card.remove();
return;
}
// Nothing made it in (wrong file type, server error) — keep them
// parked so another try or a discard is still possible.
const status = card.querySelector(".share-import__status");
status.hidden = false;
status.textContent = "Import failed — only GPX, FIT, GeoJSON and ZIP files can be imported.";
card.querySelectorAll("button").forEach((button) => (button.disabled = false));
});
}
function withExtension(name, type) {
return KNOWN_EXTENSION.test(name) ? name : `${name}${EXTENSION_BY_TYPE[type] || ".gpx"}`;
}
async function fillFolderSuggestions(datalist) {
try {
const tree = await (await fetch("/api/gpx/tree")).json();
const folders = [];
const walk = (node) =>
(node.children || []).forEach((child) => {
if (child.type !== "folder") return;
folders.push(child.path);
walk(child);
});
walk(tree);
datalist.innerHTML = folders.map((path) => `<option value="${escapeHtml(path)}">`).join("");
} catch {
// suggestions only — typing a folder name still works
}
}