@geolonia/embed 2.0.1 → 2.1.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geolonia/embed",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "description": "Geolonia embed JS API",
5
5
  "main": "dist/embed.js",
6
6
  "types": "src/embed.d.ts",
@@ -37,6 +37,7 @@
37
37
  "jsdom": "^16.2.2",
38
38
  "jsdom-global": "^3.0.2",
39
39
  "mocha": "^5.2.0",
40
+ "node-fetch": "2",
40
41
  "pngjs": "^6.0.0",
41
42
  "prettier-eslint": "^12.0.0",
42
43
  "prettier-eslint-cli": "^5.0.0",
@@ -5,6 +5,9 @@ import GeoloniaControl from '@geolonia/mbgl-geolonia-control';
5
5
  import GestureHandling from '@geolonia/mbgl-gesture-handling';
6
6
  import parseAtts from './parse-atts';
7
7
 
8
+ import SimpleStyle from './simplestyle';
9
+ import SimpleStyleVector from './simplestyle-vector';
10
+
8
11
  import * as util from './util';
9
12
 
10
13
  const isCssSelector = (string) => {
@@ -200,28 +203,26 @@ export default class GeoloniaMap extends maplibregl.Map {
200
203
 
201
204
  if (atts.simpleVector) {
202
205
  const simpleVectorAttributeValue = util.parseSimpleVector(atts.simpleVector);
203
- const { default: SimpleStyleVector } = await import('./simplestyle-vector');
204
206
  new SimpleStyleVector(simpleVectorAttributeValue).addTo(map);
205
207
  }
206
208
 
207
209
  if (atts.geojson) {
208
210
  const el = isCssSelector(atts.geojson);
209
- const { default: SimpleStyle } = await import('./simplestyle');
211
+ let json;
210
212
  if (el) {
211
- const json = JSON.parse(el.textContent);
212
- new SimpleStyle(json, {
213
- cluster: (atts.cluster === 'on'),
214
- clusterColor: atts.clusterColor,
215
- }).addTo(map);
213
+ json = JSON.parse(el.textContent);
216
214
  } else {
217
- fetch(atts.geojson).then((response) => {
218
- return response.json();
219
- }).then((json) => {
220
- new SimpleStyle(json, {
221
- cluster: (atts.cluster === 'on'),
222
- clusterColor: atts.clusterColor,
223
- }).addTo(map);
224
- });
215
+ json = atts.geojson;
216
+ }
217
+
218
+ const ss = new SimpleStyle(json, {
219
+ cluster: (atts.cluster === 'on'),
220
+ clusterColor: atts.clusterColor,
221
+ });
222
+ ss.addTo(map);
223
+
224
+ if (!container.dataset || (!container.dataset.lng && !container.dataset.lat)) {
225
+ ss.fitBounds();
225
226
  }
226
227
  }
227
228
 
@@ -2,7 +2,6 @@
2
2
 
3
3
  import maplibregl from 'maplibre-gl';
4
4
  import turfCenter from '@turf/center';
5
- import sanitizeHtml from 'sanitize-html';
6
5
 
7
6
  const textColor = '#000000';
8
7
  const textHaloColor = '#FFFFFF';
@@ -204,7 +203,8 @@ class SimpleStyleVector {
204
203
  this.setPopup(map, 'vt-circle-simple-style-points');
205
204
  }
206
205
 
207
- setPopup(map, source) {
206
+ async setPopup(map, source) {
207
+ const { default: sanitizeHtml } = await import('sanitize-html');
208
208
  map.on('click', source, (e) => {
209
209
  const center = turfCenter(e.features[0]).geometry.coordinates;
210
210
  const description = e.features[0].properties.description;
@@ -3,30 +3,39 @@
3
3
  import maplibregl from 'maplibre-gl';
4
4
  import geojsonExtent from '@mapbox/geojson-extent';
5
5
  import turfCenter from '@turf/center';
6
- import sanitizeHtml from 'sanitize-html';
6
+ import { isURL } from './util';
7
7
 
8
8
  const textColor = '#000000';
9
9
  const textHaloColor = '#FFFFFF';
10
10
  const backgroundColor = 'rgba(255, 0, 0, 0.4)';
11
11
  const strokeColor = '#FFFFFF';
12
12
 
13
+ const template = {
14
+ 'type': 'FeatureCollection',
15
+ 'features': [],
16
+ };
17
+
13
18
  class SimpleStyle {
14
19
  constructor(geojson, options) {
15
- this.geojson = geojson;
20
+
21
+ this.callFitBounds = false;
22
+
23
+ this.setGeoJSON(geojson);
16
24
 
17
25
  this.options = {
18
26
  id: 'geolonia-simple-style',
19
27
  cluster: true,
20
28
  heatmap: false, // TODO: It should support heatmap.
21
29
  clusterColor: '#ff0000',
22
- duration: 3000,
23
- padding: 30,
24
30
  ...options,
25
31
  };
26
32
  }
27
33
 
28
34
  updateData(geojson) {
29
- const features = geojson.features;
35
+
36
+ this.setGeoJSON(geojson);
37
+
38
+ const features = this.geojson.features;
30
39
  const polygonandlines = features.filter((feature) => (feature.geometry.type.toLowerCase() !== 'point'));
31
40
  const points = features.filter((feature) => (feature.geometry.type.toLowerCase() === 'point'));
32
41
 
@@ -40,8 +49,6 @@ class SimpleStyle {
40
49
  'features': points,
41
50
  });
42
51
 
43
- this.geojson = geojson;
44
-
45
52
  return this;
46
53
  }
47
54
 
@@ -116,16 +123,24 @@ class SimpleStyle {
116
123
  this.setPointGeometries();
117
124
  this.setCluster();
118
125
 
119
- const container = this.map.getContainer();
126
+ return this;
127
+ }
120
128
 
121
- if (!container.dataset || (!container.dataset.lng && !container.dataset.lat)) {
122
- const bounds = geojsonExtent(this.geojson);
123
- if (bounds) {
124
- this.map.fitBounds(bounds, {
125
- duration: this.options.duration,
126
- padding: this.options.padding,
127
- });
128
- }
129
+ fitBounds(options = {}) {
130
+
131
+ this.callFitBounds = true;
132
+
133
+ const _options = {
134
+ duration: 3000,
135
+ padding: 30,
136
+ ...options,
137
+ };
138
+
139
+ const bounds = geojsonExtent(this.geojson);
140
+ if (bounds) {
141
+ window.requestAnimationFrame(() => {
142
+ this.map.fitBounds(bounds, _options);
143
+ });
129
144
  }
130
145
 
131
146
  return this;
@@ -231,7 +246,8 @@ class SimpleStyle {
231
246
  this.setPopup(this.map, `${this.options.id}-circle-points`);
232
247
  }
233
248
 
234
- setPopup(map, source) {
249
+ async setPopup(map, source) {
250
+ const { default: sanitizeHtml } = await import('sanitize-html');
235
251
  map.on('click', source, (e) => {
236
252
  const center = turfCenter(e.features[0]).geometry.coordinates;
237
253
  const description = e.features[0].properties.description;
@@ -302,6 +318,43 @@ class SimpleStyle {
302
318
  this.map.getCanvas().style.cursor = '';
303
319
  });
304
320
  }
321
+
322
+ setGeoJSON(geojson) {
323
+
324
+ if (typeof geojson === 'string' && isURL(geojson)) {
325
+
326
+ this.geojson = template;
327
+
328
+ const fetchGeoJSON = async () => {
329
+
330
+ try {
331
+
332
+ const response = await window.fetch(geojson);
333
+ const data = await response.json();
334
+ this.geojson = data;
335
+ this.updateData(data);
336
+
337
+ if (this.callFitBounds) {
338
+
339
+ this.fitBounds();
340
+ this.callFitBounds = false;
341
+
342
+ }
343
+
344
+ } catch (error) {
345
+
346
+ console.error('Failed to load GeoJSON:', error); // eslint-disable-line no-console
347
+ }
348
+
349
+ };
350
+
351
+ this._loadingPromise = fetchGeoJSON();
352
+
353
+ } else {
354
+ this.geojson = geojson;
355
+ }
356
+
357
+ }
305
358
  }
306
359
 
307
360
  export default SimpleStyle;
@@ -1,6 +1,11 @@
1
1
  'use strict';
2
2
 
3
3
  import assert from 'assert';
4
+ import nodeFetch from 'node-fetch';
5
+
6
+ window.URL.createObjectURL = () => {}; // To prevent `TypeError: window.URL.createObjectURL is not a function`
7
+ window.requestAnimationFrame = (cb) => cb();
8
+ window.fetch = nodeFetch;
4
9
 
5
10
  class Map {
6
11
  constructor(json, options) {
@@ -69,11 +74,10 @@ const geojson = {
69
74
 
70
75
  describe('Tests for simpleStyle()', () => {
71
76
  it('should has sources and layers as expected', async () => {
72
- window.URL.createObjectURL = () => {}; // To prevent `TypeError: window.URL.createObjectURL is not a function`
73
77
  const { default: simpleStyle } = await import('./simplestyle');
74
78
 
75
79
  const map = new Map();
76
- new simpleStyle(geojson).addTo(map);
80
+ new simpleStyle(geojson).addTo(map).fitBounds();
77
81
 
78
82
  assert.deepEqual([ 'geolonia-simple-style', 'geolonia-simple-style-points' ], Object.keys(map.sources));
79
83
  assert.deepEqual(8, map.layers.length);
@@ -81,11 +85,10 @@ describe('Tests for simpleStyle()', () => {
81
85
  });
82
86
 
83
87
  it('should has sources and layers as expected with custom IDs', async () => {
84
- window.URL.createObjectURL = () => {}; // To prevent `TypeError: window.URL.createObjectURL is not a function`
85
88
  const { default: simpleStyle } = await import('./simplestyle');
86
89
 
87
90
  const map = new Map();
88
- new simpleStyle(geojson, {id: 'hello-world'}).addTo(map);
91
+ new simpleStyle(geojson, {id: 'hello-world'}).addTo(map).fitBounds();
89
92
 
90
93
  assert.deepEqual([ 'hello-world', 'hello-world-points' ], Object.keys(map.sources));
91
94
  assert.deepEqual(8, map.layers.length);
@@ -93,7 +96,6 @@ describe('Tests for simpleStyle()', () => {
93
96
  });
94
97
 
95
98
  it('should has sources and layers as expected with empty GeoJSON', async () => {
96
- window.URL.createObjectURL = () => {}; // To prevent `TypeError: window.URL.createObjectURL is not a function`
97
99
  const { default: simpleStyle } = await import('./simplestyle');
98
100
 
99
101
  const map = new Map();
@@ -103,7 +105,7 @@ describe('Tests for simpleStyle()', () => {
103
105
  'features': [],
104
106
  };
105
107
 
106
- new simpleStyle(empty, {id: 'hello-world'}).addTo(map);
108
+ new simpleStyle(empty, {id: 'hello-world'}).addTo(map).fitBounds();
107
109
 
108
110
  assert.deepEqual([ 'hello-world', 'hello-world-points' ], Object.keys(map.sources));
109
111
  assert.deepEqual(8, map.layers.length);
@@ -111,7 +113,6 @@ describe('Tests for simpleStyle()', () => {
111
113
  });
112
114
 
113
115
  it('should update GeoJSON', async () => {
114
- window.URL.createObjectURL = () => {}; // To prevent `TypeError: window.URL.createObjectURL is not a function`
115
116
  const { default: simpleStyle } = await import('./simplestyle');
116
117
 
117
118
  const map = new Map();
@@ -121,7 +122,7 @@ describe('Tests for simpleStyle()', () => {
121
122
  'features': [],
122
123
  };
123
124
 
124
- const ss = new simpleStyle(empty).addTo(map); // The GeoJSON is empty.
125
+ const ss = new simpleStyle(empty).addTo(map).fitBounds();
125
126
 
126
127
  assert.deepEqual([ 'geolonia-simple-style', 'geolonia-simple-style-points' ], Object.keys(map.sources));
127
128
  assert.deepEqual(8, map.layers.length);
@@ -132,4 +133,106 @@ describe('Tests for simpleStyle()', () => {
132
133
  assert.deepEqual(false, map.bounds); // `fitBounds()` doesn't fire.
133
134
  assert.deepEqual(1, map.sources['geolonia-simple-style-points'].data.features.length);
134
135
  });
136
+
137
+ it('should load GeoJSON from url', async () => {
138
+ const { default: simpleStyle } = await import('./simplestyle');
139
+
140
+ const map = new Map();
141
+ const geojson = 'https://gist.githubusercontent.com/miya0001/56c3dc174f5cdf1d9565cbca0fbd3c48/raw/c13330036d28ef547a8a87cb6df3fa12de19ddb6/test.geojson';
142
+ const ss = new simpleStyle(geojson);
143
+ ss.addTo(map).fitBounds();
144
+
145
+ await ss._loadingPromise;
146
+
147
+ const geometry = map.sources['geolonia-simple-style'].data.features[0].geometry;
148
+ const coordinates = geometry.coordinates;
149
+ const type = geometry.type;
150
+
151
+ const expectCoordinates = [
152
+ [
153
+ 139.6870422363281,
154
+ 35.73425097869431,
155
+ ],
156
+ [
157
+ 139.76943969726562,
158
+ 35.73425097869431,
159
+ ],
160
+ [
161
+ 139.73922729492188,
162
+ 35.66399091134812,
163
+ ],
164
+ [
165
+ 139.70352172851562,
166
+ 35.698571062054015,
167
+ ],
168
+ ];
169
+
170
+ assert.deepEqual(expectCoordinates, coordinates);
171
+ assert.deepEqual('LineString', type);
172
+ assert.deepEqual(true, map.bounds);
173
+ });
174
+
175
+ it('should load empty GeoJSON when failed to fetch GeoJSON', async () => {
176
+ const { default: simpleStyle } = await import('./simplestyle');
177
+
178
+ const map = new Map();
179
+ const geojson = 'https://example.com/404.geojson';
180
+ const ss = new simpleStyle(geojson);
181
+ ss.addTo(map).fitBounds();
182
+
183
+ await ss._loadingPromise;
184
+
185
+ assert.deepEqual([ 'geolonia-simple-style', 'geolonia-simple-style-points' ], Object.keys(map.sources));
186
+ assert.deepEqual(8, map.layers.length);
187
+ assert.deepEqual(false, map.bounds);
188
+ });
189
+
190
+ it('should update GeoJSON from url', async () => {
191
+ const { default: simpleStyle } = await import('./simplestyle');
192
+
193
+ const map = new Map();
194
+ const empty = {
195
+ 'type': 'FeatureCollection',
196
+ 'features': [],
197
+ };
198
+
199
+ const ss = new simpleStyle(empty);
200
+ ss.addTo(map).fitBounds();
201
+
202
+ await ss._loadingPromise;
203
+
204
+ const geojson = 'https://gist.githubusercontent.com/miya0001/56c3dc174f5cdf1d9565cbca0fbd3c48/raw/c13330036d28ef547a8a87cb6df3fa12de19ddb6/test.geojson';
205
+
206
+ ss.updateData(geojson);
207
+
208
+ await ss._loadingPromise;
209
+
210
+ const geometry = map.sources['geolonia-simple-style'].data.features[0].geometry;
211
+ const coordinates = geometry.coordinates;
212
+ const type = geometry.type;
213
+
214
+ const expectCoordinates = [
215
+ [
216
+ 139.6870422363281,
217
+ 35.73425097869431,
218
+ ],
219
+ [
220
+ 139.76943969726562,
221
+ 35.73425097869431,
222
+ ],
223
+ [
224
+ 139.73922729492188,
225
+ 35.66399091134812,
226
+ ],
227
+ [
228
+ 139.70352172851562,
229
+ 35.698571062054015,
230
+ ],
231
+ ];
232
+
233
+ assert.deepEqual(expectCoordinates, coordinates);
234
+ assert.deepEqual('LineString', type);
235
+ assert.deepEqual(true, map.bounds);
236
+ });
237
+
135
238
  });
@@ -1 +0,0 @@
1
- "use strict";(self.webpackChunk_geolonia_embed=self.webpackChunk_geolonia_embed||[]).push([[577],{7577:(e,t,o)=>{o.r(t),o.d(t,{default:()=>y}),o(2526),o(1817),o(9070);var i=o(2739),l=o.n(i),r=o(1235),s=o(8007),a=o.n(s);function n(e,t){for(var o=0;o<t.length;o++){var i=t[o];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}var c="#000000",u="#FFFFFF",g="rgba(255, 0, 0, 0.4)",p="#FFFFFF";const y=function(){function e(t){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.sourceName="vt-geolonia-simple-style",this.url=t}var t,o;return t=e,(o=[{key:"addTo",value:function(e){var t=this,o=e.getContainer();if(!o.dataset||!o.dataset.lng&&!o.dataset.lat){var i=!1;e.on("sourcedata",(function(o){if(o.sourceId===t.sourceName){var l=e.getSource(o.sourceId);!0===(l&&l.loaded())&&(i||(i=!0,e.fitBounds(l.bounds,{duration:0,padding:30})))}}))}e.addSource(this.sourceName,{type:"vector",url:this.url}),this.setPolygonGeometries(e),this.setLineGeometries(e),e.addLayer({id:"vt-geolonia-simple-style-polygon-symbol",type:"symbol",source:this.sourceName,"source-layer":"g-simplestyle-v1",filter:["==","$type","Polygon"],paint:{"text-color":["string",["get","text-color"],c],"text-halo-color":["string",["get","text-halo-color"],u],"text-halo-width":1},layout:{"text-field":["get","title"],"text-font":["Noto Sans Regular"],"text-size":12,"text-max-width":12,"text-allow-overlap":!1}}),e.addLayer({id:"vt-geolonia-simple-style-linestring-symbol",type:"symbol",source:this.sourceName,"source-layer":"g-simplestyle-v1",filter:["==","$type","LineString"],paint:{"text-color":["string",["get","text-color"],c],"text-halo-color":["string",["get","text-halo-color"],u],"text-halo-width":1},layout:{"symbol-placement":"line","text-field":["get","title"],"text-font":["Noto Sans Regular"],"text-size":12,"text-max-width":12,"text-allow-overlap":!1}}),this.setPointGeometries(e)}},{key:"setPolygonGeometries",value:function(e){e.addLayer({id:"vt-geolonia-simple-style-polygon",type:"fill",source:this.sourceName,"source-layer":"g-simplestyle-v1",filter:["==","$type","Polygon"],paint:{"fill-color":["string",["get","fill"],g],"fill-opacity":["number",["get","fill-opacity"],1],"fill-outline-color":["string",["get","stroke"],p]}}),this.setPopup(e,"vt-geolonia-simple-style-polygon")}},{key:"setLineGeometries",value:function(e){e.addLayer({id:"vt-geolonia-simple-style-linestring",type:"line",source:this.sourceName,"source-layer":"g-simplestyle-v1",filter:["==","$type","LineString"],paint:{"line-width":["number",["get","stroke-width"],2],"line-color":["string",["get","stroke"],g],"line-opacity":["number",["get","stroke-opacity"],1]},layout:{"line-cap":"round","line-join":"round"}}),this.setPopup(e,"vt-geolonia-simple-style-linestring")}},{key:"setPointGeometries",value:function(e){e.addLayer({id:"vt-circle-simple-style-points",type:"circle",source:this.sourceName,"source-layer":"g-simplestyle-v1",filter:["==","$type","Point"],paint:{"circle-radius":["case",["==","small",["get","marker-size"]],7,["==","large",["get","marker-size"]],13,9],"circle-color":["string",["get","marker-color"],g],"circle-opacity":["number",["get","fill-opacity"],1],"circle-stroke-width":["number",["get","stroke-width"],1],"circle-stroke-color":["string",["get","stroke"],p],"circle-stroke-opacity":["number",["get","stroke-opacity"],1]}}),e.addLayer({id:"vt-geolonia-simple-style-points",type:"symbol",source:this.sourceName,"source-layer":"g-simplestyle-v1",filter:["==","$type","Point"],paint:{"text-color":["string",["get","text-color"],c],"text-halo-color":["string",["get","text-halo-color"],u],"text-halo-width":1},layout:{"icon-image":["case",["==","large",["get","marker-size"]],["image",["concat",["get","marker-symbol"],"-15"]],["image",["concat",["get","marker-symbol"],"-11"]]],"text-field":["get","title"],"text-font":["Noto Sans Regular"],"text-size":12,"text-anchor":"top","text-max-width":12,"text-offset":["case",["==","small",["get","marker-size"]],["literal",[0,.6]],["==","large",["get","marker-size"]],["literal",[0,1.2]],["literal",[0,.8]]],"text-allow-overlap":!1}}),this.setPopup(e,"vt-circle-simple-style-points")}},{key:"setPopup",value:function(e,t){e.on("click",t,(function(t){var o=(0,r.Z)(t.features[0]).geometry.coordinates,i=t.features[0].properties.description;i&&(new(l().Popup)).setLngLat(o).setHTML(a()(i)).addTo(e)})),e.on("mouseenter",t,(function(t){t.features[0].properties.description&&(e.getCanvas().style.cursor="pointer")})),e.on("mouseleave",t,(function(){e.getCanvas().style.cursor=""}))}}])&&n(t.prototype,o),e}()}}]);