@geolonia/embed 5.0.3 → 5.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/.github/workflows/build.yml +9 -4
  2. package/.vscode/settings.json +27 -0
  3. package/CHANGELOG.md +9 -0
  4. package/README.md +71 -2
  5. package/dist/embed +1 -1
  6. package/dist/embed-chunks/12cbd0d1cb4cf744c32f.js +2 -0
  7. package/dist/embed-chunks/1492645e3d07fd3de940.js +1 -0
  8. package/dist/embed-chunks/8a521e8312a5b4da123d.js +2 -0
  9. package/dist/embed-chunks/8a521e8312a5b4da123d.js.LICENSE.txt +6 -0
  10. package/dist/embed-core.d.ts +14 -0
  11. package/dist/embed-core.js +2 -0
  12. package/dist/embed-core.js.LICENSE.txt +4 -0
  13. package/dist/{src/embed.d.ts → embed.d.ts} +2 -37
  14. package/dist/embed.js +1 -1
  15. package/dist/embed.js.LICENSE.txt +1 -1
  16. package/dist/lib/keyring.d.ts +45 -0
  17. package/dist/{src/lib → lib}/parse-atts.d.ts +1 -1
  18. package/dist/{src/lib → lib}/simplestyle.d.ts +2 -0
  19. package/dist/{src/lib → lib}/util.d.ts +3 -3
  20. package/dist/types.d.ts +38 -0
  21. package/dist/version.d.ts +1 -0
  22. package/e2e/basic.spec.ts +6 -1
  23. package/e2e/external-style.spec.ts +25 -0
  24. package/e2e/map.spec.ts +12 -12
  25. package/package.json +15 -8
  26. package/src/embed-core.ts +22 -0
  27. package/src/embed.ts +2 -42
  28. package/src/lib/geolonia-map.ts +4 -3
  29. package/src/lib/geolonia-marker.ts +1 -1
  30. package/src/lib/keyring.test.ts +58 -15
  31. package/src/lib/keyring.ts +53 -1
  32. package/src/lib/parse-atts.test.ts +2 -2
  33. package/src/lib/parse-atts.ts +5 -1
  34. package/src/lib/render.ts +1 -1
  35. package/src/lib/simplestyle.test.ts +94 -56
  36. package/src/lib/simplestyle.ts +75 -17
  37. package/src/lib/util.test.ts +136 -96
  38. package/src/lib/util.ts +55 -10
  39. package/src/testability.test.ts +13 -10
  40. package/src/types.ts +44 -0
  41. package/tsconfig.build.json +9 -0
  42. package/tsconfig.json +1 -0
  43. package/vitest.config.ts +8 -0
  44. package/webpack.config.js +45 -14
  45. package/.mocharc.js +0 -9
  46. package/dist/e2e/basic.spec.d.ts +0 -8
  47. package/dist/e2e/helper.d.ts +0 -4
  48. package/dist/e2e/map.spec.d.ts +0 -1
  49. package/dist/embed-chunks/2489af4328c51b71e92d.js +0 -2
  50. package/dist/playwright.config.d.ts +0 -9
  51. package/dist/src/lib/keyring.d.ts +0 -29
  52. package/dist/src/lib/keyring.test.d.ts +0 -1
  53. package/dist/src/lib/parse-atts.test.d.ts +0 -1
  54. package/dist/src/lib/simplestyle.test.d.ts +0 -1
  55. package/dist/src/lib/util.test.d.ts +0 -1
  56. package/dist/src/testability.test.d.ts +0 -1
  57. package/dist/src/version.d.ts +0 -1
  58. /package/dist/embed-chunks/{2489af4328c51b71e92d.js.LICENSE.txt → 12cbd0d1cb4cf744c32f.js.LICENSE.txt} +0 -0
  59. /package/dist/{src/lib → lib}/CustomAttributionControl.d.ts +0 -0
  60. /package/dist/{src/lib → lib}/controls/geolonia-control.d.ts +0 -0
  61. /package/dist/{src/lib → lib}/geolonia-map.d.ts +0 -0
  62. /package/dist/{src/lib → lib}/geolonia-marker.d.ts +0 -0
  63. /package/dist/{src/lib → lib}/maplibre-util.d.ts +0 -0
  64. /package/dist/{src/lib → lib}/render.d.ts +0 -0
  65. /package/dist/{src/lib → lib}/simplestyle-vector.d.ts +0 -0
  66. /package/dist/{src/types-internal.d.ts → types-internal.d.ts} +0 -0
@@ -1,16 +1,45 @@
1
1
  /* eslint-disable no-loss-of-precision */
2
2
  'use strict';
3
3
 
4
- import assert from 'assert';
5
- import nodeFetch from 'node-fetch';
4
+ import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest';
6
5
  import { random } from './util';
7
6
 
8
- window.URL.createObjectURL ||= (_: Blob | MediaSource) => 'dummy'; // To prevent `TypeError: window.URL.createObjectURL is not a function`
9
- window.requestAnimationFrame = (cb) => {
10
- cb(performance.now());
11
- return random(999999);
7
+ const originalRequestAnimationFrame = window.requestAnimationFrame;
8
+ const originalCreateObjectURL = window.URL.createObjectURL;
9
+
10
+ beforeAll(() => {
11
+ window.URL.createObjectURL ||= (_: Blob | MediaSource) => 'dummy';
12
+ window.requestAnimationFrame = (cb) => {
13
+ cb(performance.now());
14
+ return random(999999);
15
+ };
16
+ });
17
+
18
+ afterAll(() => {
19
+ window.requestAnimationFrame = originalRequestAnimationFrame;
20
+ if (originalCreateObjectURL) {
21
+ window.URL.createObjectURL = originalCreateObjectURL;
22
+ }
23
+ });
24
+
25
+ const sampleRemoteGeoJSON = {
26
+ type: 'FeatureCollection',
27
+ features: [
28
+ {
29
+ type: 'Feature',
30
+ properties: {},
31
+ geometry: {
32
+ type: 'LineString',
33
+ coordinates: [
34
+ [139.6870422363281, 35.73425097869431],
35
+ [139.76943969726562, 35.73425097869431],
36
+ [139.73922729492188, 35.66399091134812],
37
+ [139.70352172851562, 35.698571062054015],
38
+ ],
39
+ },
40
+ },
41
+ ],
12
42
  };
13
- window.fetch = nodeFetch;
14
43
 
15
44
  class Map {
16
45
  public bounds = false;
@@ -53,7 +82,6 @@ class Map {
53
82
  return { dataset: true };
54
83
  }
55
84
 
56
- // It should not be fired if GeoJSON is empty.
57
85
  fitBounds() {
58
86
  this.bounds = true;
59
87
  }
@@ -80,24 +108,21 @@ describe('Tests for simpleStyle()', () => {
80
108
  const map = new Map();
81
109
  new SimpleStyle(geojson).addTo(map).fitBounds();
82
110
 
83
- assert.deepEqual(
111
+ expect(Object.keys(map.sources)).toEqual(
84
112
  ['geolonia-simple-style', 'geolonia-simple-style-points'],
85
- Object.keys(map.sources),
86
113
  );
87
- assert.deepEqual(8, map.layers.length);
88
- assert.deepEqual(true, map.bounds);
89
- assert.deepEqual(
90
- true,
114
+ expect(map.layers.length).toEqual(8);
115
+ expect(map.bounds).toEqual(true);
116
+ expect(
91
117
  map.layers.find(
92
118
  (layer) => layer.id === 'geolonia-simple-style-symbol-points',
93
119
  ).layout['icon-allow-overlap'],
94
- );
95
- assert.deepEqual(
96
- true,
120
+ ).toEqual(true);
121
+ expect(
97
122
  map.layers.find(
98
123
  (layer) => layer.id === 'geolonia-simple-style-symbol-points',
99
124
  ).layout['text-allow-overlap'],
100
- );
125
+ ).toEqual(true);
101
126
  });
102
127
 
103
128
  it('should has sources and layers as expected with custom IDs', async () => {
@@ -106,12 +131,11 @@ describe('Tests for simpleStyle()', () => {
106
131
  const map = new Map();
107
132
  new SimpleStyle(geojson, { id: 'hello-world' }).addTo(map).fitBounds();
108
133
 
109
- assert.deepEqual(
134
+ expect(Object.keys(map.sources)).toEqual(
110
135
  ['hello-world', 'hello-world-points'],
111
- Object.keys(map.sources),
112
136
  );
113
- assert.deepEqual(8, map.layers.length);
114
- assert.deepEqual(true, map.bounds);
137
+ expect(map.layers.length).toEqual(8);
138
+ expect(map.bounds).toEqual(true);
115
139
  });
116
140
 
117
141
  it('should has sources and layers as expected with empty GeoJSON', async () => {
@@ -126,12 +150,11 @@ describe('Tests for simpleStyle()', () => {
126
150
 
127
151
  new SimpleStyle(empty, { id: 'hello-world' }).addTo(map).fitBounds();
128
152
 
129
- assert.deepEqual(
153
+ expect(Object.keys(map.sources)).toEqual(
130
154
  ['hello-world', 'hello-world-points'],
131
- Object.keys(map.sources),
132
155
  );
133
- assert.deepEqual(8, map.layers.length);
134
- assert.deepEqual(false, map.bounds);
156
+ expect(map.layers.length).toEqual(8);
157
+ expect(map.bounds).toEqual(false);
135
158
  });
136
159
 
137
160
  it('should update GeoJSON', async () => {
@@ -146,32 +169,34 @@ describe('Tests for simpleStyle()', () => {
146
169
 
147
170
  const ss = new SimpleStyle(empty).addTo(map).fitBounds();
148
171
 
149
- assert.deepEqual(
172
+ expect(Object.keys(map.sources)).toEqual(
150
173
  ['geolonia-simple-style', 'geolonia-simple-style-points'],
151
- Object.keys(map.sources),
152
174
  );
153
- assert.deepEqual(8, map.layers.length);
154
- assert.deepEqual(false, map.bounds);
155
- assert.deepEqual(
156
- 0,
175
+ expect(map.layers.length).toEqual(8);
176
+ expect(map.bounds).toEqual(false);
177
+ expect(
157
178
  map.sources['geolonia-simple-style-points'].data.features.length,
158
- );
179
+ ).toEqual(0);
159
180
 
160
- ss.updateData(geojson); // The GeoJSON is not empty.
161
- assert.deepEqual(false, map.bounds); // `fitBounds()` doesn't fire.
162
- assert.deepEqual(
163
- 1,
181
+ ss.updateData(geojson);
182
+ expect(map.bounds).toEqual(false);
183
+ expect(
164
184
  map.sources['geolonia-simple-style-points'].data.features.length,
165
- );
185
+ ).toEqual(1);
166
186
  });
167
187
 
168
188
  it('should load GeoJSON from url', async () => {
169
189
  const { SimpleStyle } = await import('./simplestyle');
190
+ const fetchSpy = vi.spyOn(window, 'fetch').mockResolvedValue(
191
+ new Response(JSON.stringify(sampleRemoteGeoJSON), {
192
+ status: 200,
193
+ headers: { 'content-type': 'application/json' },
194
+ }),
195
+ );
170
196
 
171
197
  const map = new Map();
172
- const geojson =
173
- 'https://gist.githubusercontent.com/miya0001/56c3dc174f5cdf1d9565cbca0fbd3c48/raw/c13330036d28ef547a8a87cb6df3fa12de19ddb6/test.geojson';
174
- const ss = new SimpleStyle(geojson);
198
+ const geojsonUrl = 'https://example.com/test.geojson';
199
+ const ss = new SimpleStyle(geojsonUrl);
175
200
  ss.addTo(map).fitBounds();
176
201
 
177
202
  await ss._loadingPromise;
@@ -188,31 +213,43 @@ describe('Tests for simpleStyle()', () => {
188
213
  [139.70352172851562, 35.698571062054015],
189
214
  ];
190
215
 
191
- assert.deepEqual(expectCoordinates, coordinates);
192
- assert.deepEqual('LineString', type);
193
- assert.deepEqual(true, map.bounds);
216
+ expect(coordinates).toEqual(expectCoordinates);
217
+ expect(type).toEqual('LineString');
218
+ expect(map.bounds).toEqual(true);
219
+
220
+ fetchSpy.mockRestore();
194
221
  });
195
222
 
196
223
  it('should load empty GeoJSON when failed to fetch GeoJSON', async () => {
197
224
  const { SimpleStyle } = await import('./simplestyle');
225
+ const fetchSpy = vi.spyOn(window, 'fetch').mockResolvedValue(
226
+ new Response('Not Found', { status: 404 }),
227
+ );
198
228
 
199
229
  const map = new Map();
200
- const geojson = 'https://example.com/404.geojson';
201
- const ss = new SimpleStyle(geojson);
230
+ const geojsonUrl = 'https://example.com/404.geojson';
231
+ const ss = new SimpleStyle(geojsonUrl);
202
232
  ss.addTo(map).fitBounds();
203
233
 
204
234
  await ss._loadingPromise;
205
235
 
206
- assert.deepEqual(
236
+ expect(Object.keys(map.sources)).toEqual(
207
237
  ['geolonia-simple-style', 'geolonia-simple-style-points'],
208
- Object.keys(map.sources),
209
238
  );
210
- assert.deepEqual(8, map.layers.length);
211
- assert.deepEqual(false, map.bounds);
239
+ expect(map.layers.length).toEqual(8);
240
+ expect(map.bounds).toEqual(false);
241
+
242
+ fetchSpy.mockRestore();
212
243
  });
213
244
 
214
245
  it('should update GeoJSON from url', async () => {
215
246
  const { SimpleStyle } = await import('./simplestyle');
247
+ const fetchSpy = vi.spyOn(window, 'fetch').mockResolvedValue(
248
+ new Response(JSON.stringify(sampleRemoteGeoJSON), {
249
+ status: 200,
250
+ headers: { 'content-type': 'application/json' },
251
+ }),
252
+ );
216
253
 
217
254
  const map = new Map();
218
255
  const empty = {
@@ -225,10 +262,9 @@ describe('Tests for simpleStyle()', () => {
225
262
 
226
263
  await ss._loadingPromise;
227
264
 
228
- const geojson =
229
- 'https://gist.githubusercontent.com/miya0001/56c3dc174f5cdf1d9565cbca0fbd3c48/raw/c13330036d28ef547a8a87cb6df3fa12de19ddb6/test.geojson';
265
+ const geojsonUrl = 'https://example.com/test.geojson';
230
266
 
231
- ss.updateData(geojson);
267
+ ss.updateData(geojsonUrl);
232
268
 
233
269
  await ss._loadingPromise;
234
270
 
@@ -244,8 +280,10 @@ describe('Tests for simpleStyle()', () => {
244
280
  [139.70352172851562, 35.698571062054015],
245
281
  ];
246
282
 
247
- assert.deepEqual(expectCoordinates, coordinates);
248
- assert.deepEqual('LineString', type);
249
- assert.deepEqual(true, map.bounds);
283
+ expect(coordinates).toEqual(expectCoordinates);
284
+ expect(type).toEqual('LineString');
285
+ expect(map.bounds).toEqual(true);
286
+
287
+ fetchSpy.mockRestore();
250
288
  });
251
289
  });
@@ -21,6 +21,7 @@ export class SimpleStyle {
21
21
  private geojson;
22
22
  private map;
23
23
  private options;
24
+ private _eventHandlers: { event: string; layer: string; handler: (...args) => void }[] = [];
24
25
 
25
26
  constructor(geojson, options?) {
26
27
  this.setGeoJSON(geojson);
@@ -38,7 +39,7 @@ export class SimpleStyle {
38
39
  this.setGeoJSON(geojson);
39
40
 
40
41
  const features = this.geojson.features;
41
- const polygonandlines = features.filter(
42
+ const polygonAndLines = features.filter(
42
43
  (feature) => feature.geometry.type.toLowerCase() !== 'point',
43
44
  );
44
45
  const points = features.filter(
@@ -47,7 +48,7 @@ export class SimpleStyle {
47
48
 
48
49
  this.map.getSource(this.options.id).setData({
49
50
  type: 'FeatureCollection',
50
- features: polygonandlines,
51
+ features: polygonAndLines,
51
52
  });
52
53
 
53
54
  this.map.getSource(`${this.options.id}-points`).setData({
@@ -62,7 +63,7 @@ export class SimpleStyle {
62
63
  this.map = map;
63
64
 
64
65
  const features = this.geojson.features;
65
- const polygonandlines = features.filter(
66
+ const polygonAndLines = features.filter(
66
67
  (feature) => feature.geometry.type.toLowerCase() !== 'point',
67
68
  );
68
69
  const points = features.filter(
@@ -73,7 +74,7 @@ export class SimpleStyle {
73
74
  type: 'geojson',
74
75
  data: {
75
76
  type: 'FeatureCollection',
76
- features: polygonandlines,
77
+ features: polygonAndLines,
77
78
  },
78
79
  });
79
80
 
@@ -270,7 +271,7 @@ export class SimpleStyle {
270
271
  }
271
272
 
272
273
  async setPopup(map, source) {
273
- map.on('click', source, async (e) => {
274
+ const clickHandler = async (e) => {
274
275
  const center = turfCenter(e.features[0]).geometry.coordinates as [
275
276
  number,
276
277
  number,
@@ -284,17 +285,27 @@ export class SimpleStyle {
284
285
  .setHTML(sanitizedDescription)
285
286
  .addTo(map);
286
287
  }
287
- });
288
+ };
288
289
 
289
- map.on('mouseenter', source, (e) => {
290
+ const mouseEnterHandler = (e) => {
290
291
  if (e.features[0].properties.description) {
291
292
  map.getCanvas().style.cursor = 'pointer';
292
293
  }
293
- });
294
+ };
294
295
 
295
- map.on('mouseleave', source, () => {
296
+ const mouseLeaveHandler = () => {
296
297
  map.getCanvas().style.cursor = '';
297
- });
298
+ };
299
+
300
+ map.on('click', source, clickHandler);
301
+ map.on('mouseenter', source, mouseEnterHandler);
302
+ map.on('mouseleave', source, mouseLeaveHandler);
303
+
304
+ this._eventHandlers.push(
305
+ { event: 'click', layer: source, handler: clickHandler },
306
+ { event: 'mouseenter', layer: source, handler: mouseEnterHandler },
307
+ { event: 'mouseleave', layer: source, handler: mouseLeaveHandler },
308
+ );
298
309
  }
299
310
 
300
311
  /**
@@ -325,9 +336,11 @@ export class SimpleStyle {
325
336
  },
326
337
  });
327
338
 
328
- this.map.on('click', `${this.options.id}-clusters`, async (e) => {
339
+ const clusterLayer = `${this.options.id}-clusters`;
340
+
341
+ const clusterClickHandler = async (e) => {
329
342
  const features = this.map.queryRenderedFeatures(e.point, {
330
- layers: [`${this.options.id}-clusters`],
343
+ layers: [clusterLayer],
331
344
  });
332
345
  const clusterId = features[0].properties.cluster_id;
333
346
  const zoom = await this.map
@@ -338,15 +351,60 @@ export class SimpleStyle {
338
351
  center: features[0].geometry.coordinates,
339
352
  zoom: zoom,
340
353
  });
341
- });
354
+ };
342
355
 
343
- this.map.on('mouseenter', `${this.options.id}-clusters`, () => {
356
+ const clusterEnterHandler = () => {
344
357
  this.map.getCanvas().style.cursor = 'pointer';
345
- });
358
+ };
346
359
 
347
- this.map.on('mouseleave', `${this.options.id}-clusters`, () => {
360
+ const clusterLeaveHandler = () => {
348
361
  this.map.getCanvas().style.cursor = '';
349
- });
362
+ };
363
+
364
+ this.map.on('click', clusterLayer, clusterClickHandler);
365
+ this.map.on('mouseenter', clusterLayer, clusterEnterHandler);
366
+ this.map.on('mouseleave', clusterLayer, clusterLeaveHandler);
367
+
368
+ this._eventHandlers.push(
369
+ { event: 'click', layer: clusterLayer, handler: clusterClickHandler },
370
+ { event: 'mouseenter', layer: clusterLayer, handler: clusterEnterHandler },
371
+ { event: 'mouseleave', layer: clusterLayer, handler: clusterLeaveHandler },
372
+ );
373
+ }
374
+
375
+ remove() {
376
+ if (!this.map) return this;
377
+ const id = this.options.id;
378
+
379
+ for (const { event, layer, handler } of this._eventHandlers) {
380
+ this.map.off(event, layer, handler);
381
+ }
382
+ this._eventHandlers = [];
383
+
384
+ const layerIds = [
385
+ `${id}-polygon-symbol`,
386
+ `${id}-linestring-symbol`,
387
+ `${id}-circle-points`,
388
+ `${id}-symbol-points`,
389
+ `${id}-polygon`,
390
+ `${id}-linestring`,
391
+ `${id}-clusters`,
392
+ `${id}-cluster-count`,
393
+ ];
394
+ for (const layerId of layerIds) {
395
+ if (this.map.getLayer(layerId)) {
396
+ this.map.removeLayer(layerId);
397
+ }
398
+ }
399
+ for (const sourceId of [id, `${id}-points`]) {
400
+ if (this.map.getSource(sourceId)) {
401
+ this.map.removeSource(sourceId);
402
+ }
403
+ }
404
+
405
+ this.map.getCanvas().style.cursor = '';
406
+
407
+ return this;
350
408
  }
351
409
 
352
410
  setGeoJSON(geojson) {