only try to load .svg source files as svg overlay, don't recreate svg if it's already loaded

This commit is contained in:
Gwendolyn 2023-12-07 12:06:50 +01:00
parent ff1df86e2e
commit 8f0d61895b

View file

@ -95,36 +95,40 @@ editor = {
c3nav_api.get('mapdata/sources') c3nav_api.get('mapdata/sources')
.then(async sources => { .then(async sources => {
const svg_promises = [];
for (var i = 0; i < sources.length; i++) { for (var i = 0; i < sources.length; i++) {
const source = sources[i]; const source = sources[i];
editor.sources[source.id] = source; editor.sources[source.id] = source;
source.layer = L.imageOverlay('/editor/sourceimage/' + source.name, L.GeoJSON.coordsToLatLngs(source.bounds), {opacity: 0.3}); const bounds = L.GeoJSON.coordsToLatLngs(source.bounds);
editor._sources_control.addOverlay(source.layer, `${source.name} (image overlay)`); options = {opacity: 0.3};
source.svg_el = document.createElementNS("http://www.w3.org/2000/svg", "svg"); source.layer = L.imageOverlay('/editor/sourceimage/' + source.name, bounds, options);
source.svg_el.setAttribute('xmlns', "http://www.w3.org/2000/svg"); const is_svg = source.name.endsWith('.svg');
source.svg_layer = L.svgOverlay(source.svg_el, L.GeoJSON.coordsToLatLngs(source.bounds), {opacity: 0.3}); editor._sources_control.addOverlay(source.layer, is_svg ? `${source.name} (image overlay)` : source.name);
editor._sources_control.addOverlay(source.svg_layer, `${source.name} (svg overlay)`);
source.svg_layer.on('add', async function () { if (is_svg) {
let promise; source.svg_el = document.createElementNS("http://www.w3.org/2000/svg", "svg");
if (source.svg_promise) { source.svg_el.setAttribute('xmlns', "http://www.w3.org/2000/svg");
promise = source.svg_promise; source.svg_layer = L.svgOverlay(source.svg_el, bounds, options);
} else { editor._sources_control.addOverlay(source.svg_layer, `${source.name} (svg overlay)`);
source.svg_promise = promise = fetch(`/editor/sourceimage/${source.name}`) source.svg_layer.on('add', function () {
if (source.svg_promise) return;
source.svg_promise = fetch(`/editor/sourceimage/${source.name}`)
.then(r => { .then(r => {
if (!r.ok) { if (!r.ok) {
throw 'could not load source svg'; throw 'could not load source svg';
} }
return r.text(); return r.text();
})
.then(src => {
const root = (new DOMParser).parseFromString(src, 'image/svg+xml').documentElement;
for (const attr of root.attributes) {
source.svg_el.attributes.setNamedItem(attr.cloneNode(true));
}
source.svg_el.replaceChildren(...root.children);
}); });
} })
let src = await promise; }
const root = (new DOMParser).parseFromString(src, 'image/svg+xml').documentElement;
for (const attr of root.attributes) {
source.svg_el.attributes.setNamedItem(attr.cloneNode(true));
}
source.svg_el.replaceChildren(...root.children);
})
} }
if (sources.length) editor._sources_control.addTo(editor.map); if (sources.length) editor._sources_control.addTo(editor.map);
}) })
@ -459,7 +463,6 @@ editor = {
editor._source_image_layer.setBounds(bounds) editor._source_image_layer.setBounds(bounds)
} else { } else {
editor._source_image_layer = L.imageOverlay('/editor/sourceimage/'+content.find('[name=name]').val(), bounds, {opacity: 0.3, zIndex: 10000}); editor._source_image_layer = L.imageOverlay('/editor/sourceimage/'+content.find('[name=name]').val(), bounds, {opacity: 0.3, zIndex: 10000});
// TODO: svg support
editor._source_image_layer.addTo(editor.map); editor._source_image_layer.addTo(editor.map);
if (content.find('[data-new]').length) { if (content.find('[data-new]').length) {
editor.map.fitBounds(bounds, {padding: [30, 50]}); editor.map.fitBounds(bounds, {padding: [30, 50]});