Skip to content

Commit

Permalink
Fixes issue #992: Allow pasting GeoJSON links (#996)
Browse files Browse the repository at this point in the history
Edited description, strict equality, and test timeouts

allow pasting of links ending in json or geojson which may have different content type

Added tests for files with (geo)json extensions

Cleaned up and synched tests
  • Loading branch information
yushan-mu authored Dec 6, 2024
1 parent b194b30 commit ccf7b1f
Show file tree
Hide file tree
Showing 8 changed files with 547 additions and 18 deletions.
61 changes: 43 additions & 18 deletions src/mapml/utils/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,29 +646,54 @@ export const Util = {
return [column, row];
},

// Pastes text to a mapml-viewer/map element(mapEl), text can be a mapml link, geojson, or a map-layer
// used for pasting layers through ctrl+v, drag/drop, and pasting through the contextmenu
// Pastes text to a mapml-viewer/map element(mapEl), text can be a mapml link, GeoJSON link, GeoJSON,
// or a map-layer used for pasting layers through ctrl+v, drag/drop, and pasting through the contextmenu
// _pasteLayer: HTMLElement Str -> None
// Effects: append a map-layer element to mapEl, if it is valid
_pasteLayer: function (mapEl, text) {
_pasteLayer: async function (mapEl, text) {
try {
// try to process text as a link
new URL(text);
// create a new <map-layer> child of the <mapml-viewer> element
let l =
'<map-layer src="' +
text +
'" label="' +
mapEl.locale.dfLayer +
'" checked=""></map-layer>';
mapEl.insertAdjacentHTML('beforeend', l);
mapEl.lastElementChild.whenReady().catch(() => {
if (mapEl) {
// should invoke lifecyle callbacks automatically by removing it from DOM
mapEl.removeChild(mapEl.lastChild);
// get the content type of the link
const response = await fetch(text);
const contentType = response.headers.get('Content-Type');
// get the file extension by removing any query strings that may exist
let ext = response.url
.split('?')[0]
.split('#')[0]
.split('.')
.pop()
.toLowerCase();
if (
contentType === 'application/json' ||
contentType === 'application/geo+json' ||
ext === 'geojson' ||
ext === 'json'
) {
// try to process as GeoJSON
const textContent = await response.text();
try {
mapEl.geojson2mapml(JSON.parse(textContent));
} catch {
console.log('Error parsing GeoJSON from: ' + text);
}
// garbage collect it
l = null;
});
} else {
// try to process as a mapml file
// create a new <map-layer> child of the <mapml-viewer> element
let l =
'<map-layer src="' +
text +
'" label="' +
mapEl.locale.dfLayer +
'" checked=""></map-layer>';
mapEl.insertAdjacentHTML('beforeend', l);
mapEl.lastElementChild.whenReady().catch(() => {
if (mapEl) {
// should invoke lifecyle callbacks automatically by removing it from DOM
mapEl.removeChild(mapEl.lastChild);
}
});
}
} catch (err) {
text = text
.replace(/(<!--.*?-->)|(<!--[\S\s]+?-->)|(<!--[\S\s]*?$)/g, '')
Expand Down
276 changes: 276 additions & 0 deletions test/e2e/data/geojson/geojsonFeature.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-75.70780569986765,
45.5920359149936
],
[
-75.73525628546578,
45.59108894489039
],
[
-75.76243975673378,
45.58825724974241
],
[
-75.7890916789868,
45.58356838256187
],
[
-75.81495294677363,
45.57706795783245
],
[
-75.83977237381661,
45.568819194325386
],
[
-75.86330919462635,
45.558902282226015
],
[
-75.8853354504608,
45.547413581621775
],
[
-75.90563823403548,
45.534464661229705
],
[
-75.92402176947941,
45.520181187936664
],
[
-75.94030930641726,
45.504701679264016
],
[
-75.95434480968453,
45.488176132233605
],
[
-75.96599442899006,
45.470764543288006
],
[
-75.97514773576597,
45.45263533489585
],
[
-75.98171871743018,
45.433963705246484
],
[
-75.98564652227138,
45.414929918005164
],
[
-75.98689595109964,
45.39571754946352
],
[
-75.98545769463844,
45.37651171058487
],
[
-75.98134831832753,
45.35749726141881
],
[
-75.97460999872716,
45.33885703515545
],
[
-75.9653100180382,
45.320770088720245
],
[
-75.95354002536402,
45.3034099962873
],
[
-75.93941507522847,
45.28694320143128
],
[
-75.92307245552915,
45.271527442856325
],
[
-75.90467031854962,
45.25731026775368
],
[
-75.88438612988863,
45.24442764585996
],
[
-75.86241495120186,
45.23300269623039
],
[
-75.8389675735094,
45.22314453761789
],
[
-75.81426851851788,
45.21494727217167
],
[
-75.78855392596125,
45.2084891109474
],
[
-75.76206934539545,
45.203831648465346
],
[
-75.73506745121092,
45.2010192922696
],
[
-75.70780569986765,
45.20007885213734
],
[
-75.68054394852436,
45.2010192922696
],
[
-75.65354205433984,
45.203831648465346
],
[
-75.62705747377403,
45.2084891109474
],
[
-75.6013428812174,
45.21494727217167
],
[
-75.5766438262259,
45.22314453761789
],
[
-75.55319644853343,
45.23300269623039
],
[
-75.53122526984666,
45.24442764585996
],
[
-75.51094108118566,
45.25731026775368
],
[
-75.49253894420613,
45.271527442856325
],
[
-75.47619632450683,
45.28694320143128
],
[
-75.46207137437128,
45.3034099962873
],
[
-75.45030138169709,
45.320770088720245
],
[
-75.44100140100812,
45.33885703515545
],
[
-75.43426308140776,
45.35749726141881
],
[
-75.43015370509684,
45.37651171058487
],
[
-75.42871544863564,
45.39571754946352
],
[
-75.4299648774639,
45.414929918005164
],
[
-75.4338926823051,
45.433963705246484
],
[
-75.44046366396931,
45.45263533489585
],
[
-75.44961697074523,
45.470764543288006
],
[
-75.46126659005076,
45.488176132233605
],
[
-75.47530209331804,
45.504701679264016
],
[
-75.49158963025587,
45.520181187936664
],
[
-75.5099731656998,
45.534464661229705
],
[
-75.53027594927448,
45.547413581621775
],
[
-75.55230220510894,
45.558902282226015
],
[
-75.57583902591867,
45.568819194325386
],
[
-75.60065845296165,
45.57706795783245
],
[
-75.6265197207485,
45.58356838256187
],
[
-75.6531716430015,
45.58825724974241
],
[
-75.6803551142695,
45.59108894489039
],
[
-75.70780569986765,
45.5920359149936
]
]
]
}
}
]
}
Loading

0 comments on commit ccf7b1f

Please sign in to comment.