@geolonia/embed 3.3.0 → 4.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.
Files changed (59) hide show
  1. package/.eslintrc.js +1 -2
  2. package/.github/workflows/build.yml +1 -1
  3. package/.mocharc.js +9 -0
  4. package/.nvmrc +1 -1
  5. package/.tool-versions +1 -0
  6. package/PLUGIN.md +1 -1
  7. package/dist/embed +1 -1
  8. package/dist/embed-chunks/4472555f7ea90b733a8e.js +2 -0
  9. package/dist/embed-chunks/4b3a60cc98a6be867ce3.js +1 -0
  10. package/dist/embed.d.ts +62 -0
  11. package/dist/embed.js +1 -1
  12. package/dist/lib/CustomAttributionControl.d.ts +45 -0
  13. package/dist/lib/controls/geolonia-control.d.ts +7 -0
  14. package/dist/lib/geolonia-map.d.ts +22 -0
  15. package/dist/lib/geolonia-marker.d.ts +10 -0
  16. package/dist/lib/keyring.d.ts +29 -0
  17. package/dist/lib/keyring.test.d.ts +1 -0
  18. package/dist/lib/maplibre-util.d.ts +27 -0
  19. package/dist/lib/parse-atts.d.ts +6 -0
  20. package/dist/lib/parse-atts.test.d.ts +1 -0
  21. package/dist/lib/render.d.ts +5 -0
  22. package/dist/lib/simplestyle-vector.d.ts +26 -0
  23. package/dist/lib/simplestyle.d.ts +29 -0
  24. package/dist/lib/simplestyle.test.d.ts +1 -0
  25. package/dist/lib/util.d.ts +57 -0
  26. package/dist/lib/util.test.d.ts +1 -0
  27. package/dist/types-internal.d.ts +4 -0
  28. package/dist/version.d.ts +1 -0
  29. package/package.json +18 -24
  30. package/scripts/build-version.mjs +12 -0
  31. package/src/embed.ts +85 -0
  32. package/src/lib/{CustomAttributionControl.js → CustomAttributionControl.ts} +14 -2
  33. package/src/lib/controls/geolonia-control.ts +38 -0
  34. package/src/lib/{geolonia-map.js → geolonia-map.ts} +45 -48
  35. package/src/lib/{geolonia-marker.js → geolonia-marker.ts} +10 -9
  36. package/src/lib/keyring.test.ts +66 -0
  37. package/src/lib/keyring.ts +86 -0
  38. package/src/lib/{maplibre-util.js → maplibre-util.ts} +1 -1
  39. package/src/lib/{parse-atts.test.js → parse-atts.test.ts} +5 -1
  40. package/src/lib/{parse-atts.js → parse-atts.ts} +14 -9
  41. package/src/lib/render.ts +100 -0
  42. package/src/lib/{simplestyle-vector.js → simplestyle-vector.ts} +4 -3
  43. package/src/lib/{simplestyle.test.js → simplestyle.test.ts} +26 -27
  44. package/src/lib/{simplestyle.js → simplestyle.ts} +9 -8
  45. package/src/lib/{util.test.js → util.test.ts} +51 -46
  46. package/src/lib/{util.js → util.ts} +16 -15
  47. package/src/style.css +1 -5
  48. package/src/types-internal.ts +4 -0
  49. package/tsconfig.json +28 -0
  50. package/webpack.config.js +7 -5
  51. package/.babelrc +0 -12
  52. package/dist/embed-chunks/32f7b26f64d2caa77f2b.js +0 -2
  53. package/dist/embed-chunks/3d467b2d527e4e5de1b8.js +0 -1
  54. package/src/embed.d.ts +0 -58
  55. package/src/embed.js +0 -119
  56. package/src/lib/parse-api-key.js +0 -77
  57. package/src/lib/parse-api-key.test.js +0 -102
  58. package/src/lib/providers/amazon.js +0 -116
  59. /package/dist/embed-chunks/{32f7b26f64d2caa77f2b.js.LICENSE.txt → 4472555f7ea90b733a8e.js.LICENSE.txt} +0 -0
@@ -3,19 +3,21 @@
3
3
 
4
4
  import assert from 'assert';
5
5
  import nodeFetch from 'node-fetch';
6
+ import { random } from './util';
6
7
 
7
- window.URL.createObjectURL = () => {}; // To prevent `TypeError: window.URL.createObjectURL is not a function`
8
- window.requestAnimationFrame = (cb) => cb();
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);
12
+ };
9
13
  window.fetch = nodeFetch;
10
14
 
11
15
  class Map {
12
- constructor(json, options) {
13
- this.json = json;
14
- this.options = options;
15
- this.sources = {};
16
- this.layers = [];
17
- this.bounds = false;
18
- }
16
+ public bounds = false;
17
+ public layers = [];
18
+ public sources = {};
19
+
20
+ constructor(private json?, private options?) {}
19
21
 
20
22
  addSource(id, source) {
21
23
  this.sources[id] = source;
@@ -31,10 +33,7 @@ class Map {
31
33
 
32
34
  getSource(id) {
33
35
  class getSource {
34
- constructor(id, sources) {
35
- this.id = id;
36
- this.sources = sources;
37
- }
36
+ constructor(private id, private sources) {}
38
37
  setData(geojson) {
39
38
  this.sources[this.id] = {
40
39
  type: 'geojson',
@@ -75,10 +74,10 @@ const geojson = {
75
74
 
76
75
  describe('Tests for simpleStyle()', () => {
77
76
  it('should has sources and layers as expected', async () => {
78
- const { default: simpleStyle } = await import('./simplestyle');
77
+ const { SimpleStyle } = await import('./simplestyle');
79
78
 
80
79
  const map = new Map();
81
- new simpleStyle(geojson).addTo(map).fitBounds();
80
+ new SimpleStyle(geojson).addTo(map).fitBounds();
82
81
 
83
82
  assert.deepEqual([ 'geolonia-simple-style', 'geolonia-simple-style-points' ], Object.keys(map.sources));
84
83
  assert.deepEqual(8, map.layers.length);
@@ -88,10 +87,10 @@ describe('Tests for simpleStyle()', () => {
88
87
  });
89
88
 
90
89
  it('should has sources and layers as expected with custom IDs', async () => {
91
- const { default: simpleStyle } = await import('./simplestyle');
90
+ const { SimpleStyle } = await import('./simplestyle');
92
91
 
93
92
  const map = new Map();
94
- new simpleStyle(geojson, {id: 'hello-world'}).addTo(map).fitBounds();
93
+ new SimpleStyle(geojson, {id: 'hello-world'}).addTo(map).fitBounds();
95
94
 
96
95
  assert.deepEqual([ 'hello-world', 'hello-world-points' ], Object.keys(map.sources));
97
96
  assert.deepEqual(8, map.layers.length);
@@ -99,7 +98,7 @@ describe('Tests for simpleStyle()', () => {
99
98
  });
100
99
 
101
100
  it('should has sources and layers as expected with empty GeoJSON', async () => {
102
- const { default: simpleStyle } = await import('./simplestyle');
101
+ const { SimpleStyle } = await import('./simplestyle');
103
102
 
104
103
  const map = new Map();
105
104
 
@@ -108,7 +107,7 @@ describe('Tests for simpleStyle()', () => {
108
107
  'features': [],
109
108
  };
110
109
 
111
- new simpleStyle(empty, {id: 'hello-world'}).addTo(map).fitBounds();
110
+ new SimpleStyle(empty, {id: 'hello-world'}).addTo(map).fitBounds();
112
111
 
113
112
  assert.deepEqual([ 'hello-world', 'hello-world-points' ], Object.keys(map.sources));
114
113
  assert.deepEqual(8, map.layers.length);
@@ -116,7 +115,7 @@ describe('Tests for simpleStyle()', () => {
116
115
  });
117
116
 
118
117
  it('should update GeoJSON', async () => {
119
- const { default: simpleStyle } = await import('./simplestyle');
118
+ const { SimpleStyle } = await import('./simplestyle');
120
119
 
121
120
  const map = new Map();
122
121
 
@@ -125,7 +124,7 @@ describe('Tests for simpleStyle()', () => {
125
124
  'features': [],
126
125
  };
127
126
 
128
- const ss = new simpleStyle(empty).addTo(map).fitBounds();
127
+ const ss = new SimpleStyle(empty).addTo(map).fitBounds();
129
128
 
130
129
  assert.deepEqual([ 'geolonia-simple-style', 'geolonia-simple-style-points' ], Object.keys(map.sources));
131
130
  assert.deepEqual(8, map.layers.length);
@@ -138,11 +137,11 @@ describe('Tests for simpleStyle()', () => {
138
137
  });
139
138
 
140
139
  it('should load GeoJSON from url', async () => {
141
- const { default: simpleStyle } = await import('./simplestyle');
140
+ const { SimpleStyle } = await import('./simplestyle');
142
141
 
143
142
  const map = new Map();
144
143
  const geojson = 'https://gist.githubusercontent.com/miya0001/56c3dc174f5cdf1d9565cbca0fbd3c48/raw/c13330036d28ef547a8a87cb6df3fa12de19ddb6/test.geojson';
145
- const ss = new simpleStyle(geojson);
144
+ const ss = new SimpleStyle(geojson);
146
145
  ss.addTo(map).fitBounds();
147
146
 
148
147
  await ss._loadingPromise;
@@ -176,11 +175,11 @@ describe('Tests for simpleStyle()', () => {
176
175
  });
177
176
 
178
177
  it('should load empty GeoJSON when failed to fetch GeoJSON', async () => {
179
- const { default: simpleStyle } = await import('./simplestyle');
178
+ const { SimpleStyle } = await import('./simplestyle');
180
179
 
181
180
  const map = new Map();
182
181
  const geojson = 'https://example.com/404.geojson';
183
- const ss = new simpleStyle(geojson);
182
+ const ss = new SimpleStyle(geojson);
184
183
  ss.addTo(map).fitBounds();
185
184
 
186
185
  await ss._loadingPromise;
@@ -191,7 +190,7 @@ describe('Tests for simpleStyle()', () => {
191
190
  });
192
191
 
193
192
  it('should update GeoJSON from url', async () => {
194
- const { default: simpleStyle } = await import('./simplestyle');
193
+ const { SimpleStyle } = await import('./simplestyle');
195
194
 
196
195
  const map = new Map();
197
196
  const empty = {
@@ -199,7 +198,7 @@ describe('Tests for simpleStyle()', () => {
199
198
  'features': [],
200
199
  };
201
200
 
202
- const ss = new simpleStyle(empty);
201
+ const ss = new SimpleStyle(empty);
203
202
  ss.addTo(map).fitBounds();
204
203
 
205
204
  await ss._loadingPromise;
@@ -15,11 +15,14 @@ const template = {
15
15
  'features': [],
16
16
  };
17
17
 
18
- class SimpleStyle {
19
- constructor(geojson, options) {
20
-
21
- this.callFitBounds = false;
22
-
18
+ export class SimpleStyle {
19
+ public _loadingPromise: Promise<unknown>;
20
+ private callFitBounds = false;
21
+ private geojson;
22
+ private map;
23
+ private options;
24
+
25
+ constructor(geojson, options?) {
23
26
  this.setGeoJSON(geojson);
24
27
 
25
28
  this.options = {
@@ -253,7 +256,7 @@ class SimpleStyle {
253
256
 
254
257
  async setPopup(map, source) {
255
258
  map.on('click', source, async (e) => {
256
- const center = turfCenter(e.features[0]).geometry.coordinates;
259
+ const center = turfCenter(e.features[0]).geometry.coordinates as [ number, number ];
257
260
  const description = e.features[0].properties.description;
258
261
 
259
262
  if (description) {
@@ -361,5 +364,3 @@ class SimpleStyle {
361
364
 
362
365
  }
363
366
  }
364
-
365
- export default SimpleStyle;
@@ -2,46 +2,49 @@
2
2
 
3
3
  import assert from 'assert';
4
4
  import { JSDOM } from 'jsdom';
5
- import * as util from './util';
5
+ import { getContainer, getLang, getOptions, getStyle, handleMarkerOptions, isDomElement, isURL, parseControlOption, parseSimpleVector, sanitizeDescription } from './util';
6
6
 
7
7
  const base = 'https://base.example.com/parent/';
8
8
 
9
9
  before(() => {
10
- global.location = { href: base };
10
+ global.location = {
11
+ ...global.location,
12
+ href: base,
13
+ };
11
14
  });
12
15
 
13
16
  describe('Tests for util.js', () => {
14
17
  it('URL should be detected', () => {
15
- assert.deepEqual(true, !!util.isURL('http://example.com'));
18
+ assert.deepEqual(true, !!isURL('http://example.com'));
16
19
  });
17
20
 
18
21
  it('URL with SSL should be detected', () => {
19
- assert.deepEqual(true, !!util.isURL('https://example.com'));
22
+ assert.deepEqual(true, !!isURL('https://example.com'));
20
23
  });
21
24
 
22
25
  it('Path should be detected', () => {
23
26
  assert.deepEqual(
24
27
  'https://base.example.com/parent/example-path',
25
- util.isURL('./example-path'),
28
+ isURL('./example-path'),
26
29
  );
27
30
  });
28
31
 
29
32
  it('Parent path should be detected', () => {
30
33
  assert.deepEqual(
31
34
  'https://base.example.com/example-path',
32
- util.isURL('../example-path'),
35
+ isURL('../example-path'),
33
36
  );
34
37
  });
35
38
 
36
39
  it('Absolute path should be detected', () => {
37
40
  assert.deepEqual(
38
41
  'https://base.example.com/example-path',
39
- util.isURL('/example-path'),
42
+ isURL('/example-path'),
40
43
  );
41
44
  });
42
45
 
43
46
  it('Name should not be detected', () => {
44
- assert.deepEqual(false, util.isURL('example.com/hello'));
47
+ assert.deepEqual(false, isURL('example.com/hello'));
45
48
  });
46
49
 
47
50
  it('should detect the object is DOM correctly', () => {
@@ -49,9 +52,9 @@ describe('Tests for util.js', () => {
49
52
  <div class="test-class"></div>
50
53
  </body></html>`).window;
51
54
 
52
- assert.deepEqual(true, util.isDomElement(mocDocument.querySelector('.test-class')));
53
- assert.deepEqual(false, util.isDomElement('hello world'));
54
- assert.deepEqual(false, util.isDomElement({ hello: 'world' }));
55
+ assert.deepEqual(true, isDomElement(mocDocument.querySelector('.test-class')));
56
+ assert.deepEqual(false, isDomElement('hello world'));
57
+ assert.deepEqual(false, isDomElement({ hello: 'world' }));
55
58
  });
56
59
 
57
60
  it('should be able to get dom', () => {
@@ -62,24 +65,24 @@ describe('Tests for util.js', () => {
62
65
  global.window = dom.window;
63
66
  global.document = dom.window.document;
64
67
 
65
- const el = document.querySelector('#test-element');
66
- assert.deepEqual(el, util.getContainer(el));
68
+ const el = document.querySelector('#test-element') as HTMLElement;
69
+ assert.deepEqual(el, getContainer(el));
67
70
  const params = { container: el };
68
- assert.deepEqual(el, util.getContainer(params));
71
+ assert.deepEqual(el, getContainer(params));
69
72
 
70
73
  // specify as a selector
71
- assert.deepEqual(el, util.getContainer('#test-element'));
72
- assert.deepEqual(el, util.getContainer({ container: '#test-element' }));
74
+ assert.deepEqual(el, getContainer('#test-element'));
75
+ assert.deepEqual(el, getContainer({ container: '#test-element' }));
73
76
 
74
77
  // specify as an id attribute value
75
- assert.deepEqual(el, util.getContainer('test-element'));
76
- assert.deepEqual(el, util.getContainer({ container: 'test-element' }));
78
+ assert.deepEqual(el, getContainer('test-element'));
79
+ assert.deepEqual(el, getContainer({ container: 'test-element' }));
77
80
 
78
81
  // negative cases
79
- assert.deepEqual(false, util.getContainer('#fail-element'));
80
- assert.deepEqual(false, util.getContainer({ container: '#fail-element' }));
81
- assert.deepEqual(false, util.getContainer('fail-element'));
82
- assert.deepEqual(false, util.getContainer({ container: 'fail-element' })); });
82
+ assert.deepEqual(false, getContainer('#fail-element'));
83
+ assert.deepEqual(false, getContainer({ container: '#fail-element' }));
84
+ assert.deepEqual(false, getContainer('fail-element'));
85
+ assert.deepEqual(false, getContainer({ container: 'fail-element' })); });
83
86
 
84
87
  it('should merge legacyoptions into options as expected.', () => {
85
88
  const dom = new JSDOM(`<html><body>
@@ -89,36 +92,38 @@ describe('Tests for util.js', () => {
89
92
  global.window = dom.window;
90
93
  global.document = dom.window.document;
91
94
 
92
- const options1 = util.handleMarkerOptions(document.getElementById('test-element'), { foo: 'bar' });
95
+ const options1 = handleMarkerOptions(document.getElementById('test-element'), { color: '#FF1122' });
93
96
  assert.deepEqual(document.getElementById('test-element'), options1.element);
94
- assert.deepEqual('bar', options1.foo);
97
+ assert.deepEqual(options1.color, '#FF1122');
95
98
 
96
- const options2 = util.handleMarkerOptions(false, { foo: 'bar' });
97
- assert.deepEqual('bar', options2.foo);
99
+ const options2 = handleMarkerOptions(false, { color: '#FF1122' });
100
+ assert.deepEqual(options2.color, '#FF1122');
98
101
 
99
- const options3 = util.handleMarkerOptions({ hello: 'world' }, { foo: 'bar' });
100
- assert.deepEqual('world', options3.hello);
102
+ const options3 = handleMarkerOptions({ scale: 2 }, { color: '#FF1122' });
103
+ assert.deepEqual(options3.scale, 2);
101
104
  });
102
105
 
103
106
  describe('language detection', () => {
104
107
  it('should work with Chrome', () => {
105
108
  global.window = {
109
+ // @ts-ignore forcefully assigning values to readonly properties
106
110
  navigator: {
107
111
  language: 'ja',
108
112
  languages: ['ja', 'en', 'en-US', 'ar'],
109
113
  },
110
114
  };
111
- assert.equal(util.getLang(), 'ja');
115
+ assert.equal(getLang(), 'ja');
112
116
  });
113
117
 
114
118
  it('should work with iOS safari', () => {
115
119
  global.window = {
120
+ // @ts-ignore forcefully assigning values to readonly properties
116
121
  navigator: {
117
122
  language: 'ja-JP',
118
123
  languages: ['ja-JP'],
119
124
  },
120
125
  };
121
- assert.equal(util.getLang(), 'ja');
126
+ assert.equal(getLang(), 'ja');
122
127
  });
123
128
  });
124
129
 
@@ -128,23 +133,23 @@ describe('Tests for util.js', () => {
128
133
  key: '1234',
129
134
  };
130
135
 
131
- assert.deepEqual('https://cdn.geolonia.com/style/hello/world/en.json', util.getStyle('hello/world', atts));
132
- assert.deepEqual('https://example.com/style.json', util.getStyle('https://example.com/style.json', atts));
133
- assert.deepEqual('https://base.example.com/parent/style.json', util.getStyle('./style.json', atts));
134
- assert.deepEqual('https://base.example.com/style.json', util.getStyle('/style.json', atts));
136
+ assert.deepEqual('https://cdn.geolonia.com/style/hello/world/en.json', getStyle('hello/world', atts));
137
+ assert.deepEqual('https://example.com/style.json', getStyle('https://example.com/style.json', atts));
138
+ assert.deepEqual('https://base.example.com/parent/style.json', getStyle('./style.json', atts));
139
+ assert.deepEqual('https://base.example.com/style.json', getStyle('/style.json', atts));
135
140
  });
136
141
 
137
142
  it('should handle maplibregl options `minZoom` and `maxZoom` well', () => {
138
143
  {
139
144
  const atts = { minZoom: '', maxZoom: '10' };
140
- const options = util.getOptions({}, {}, atts);
145
+ const options = getOptions({}, {}, atts);
141
146
  assert.deepEqual('undefined', typeof options.minZoom);
142
147
  assert.deepEqual(10, options.maxZoom);
143
148
  }
144
149
 
145
150
  {
146
151
  const atts = { minZoom: '0', maxZoom: '' };
147
- const options = util.getOptions({}, {}, atts);
152
+ const options = getOptions({}, {}, atts);
148
153
  assert.deepEqual(0, options.minZoom);
149
154
  assert.deepEqual('undefined', typeof options.maxZoom);
150
155
  }
@@ -152,7 +157,7 @@ describe('Tests for util.js', () => {
152
157
  {
153
158
  const atts = { minZoom: '0', maxZoom: '' };
154
159
  const params = { minZoom: 7 };
155
- const options = util.getOptions({}, params, atts);
160
+ const options = getOptions({}, params, atts);
156
161
  assert.deepEqual(7, options.minZoom);
157
162
  assert.deepEqual('undefined', typeof options.maxZoom);
158
163
  }
@@ -160,21 +165,21 @@ describe('Tests for util.js', () => {
160
165
 
161
166
  it('should handle control position options.', () => {
162
167
  const att = 'top-left';
163
- const { enabled, position } = util.parseControlOption(att);
168
+ const { enabled, position } = parseControlOption(att);
164
169
  assert.strictEqual(true, enabled);
165
170
  assert.strictEqual('top-left', position);
166
171
  });
167
172
 
168
173
  it('should handle control position on.', () => {
169
174
  const att = 'on';
170
- const { enabled, position } = util.parseControlOption(att);
175
+ const { enabled, position } = parseControlOption(att);
171
176
  assert.strictEqual(true, enabled);
172
177
  assert.strictEqual(void 0, position);
173
178
  });
174
179
 
175
180
  it('should handle control position off.', () => {
176
181
  const att = 'off';
177
- const { enabled, position } = util.parseControlOption(att);
182
+ const { enabled, position } = parseControlOption(att);
178
183
  assert.strictEqual(false, enabled);
179
184
  assert.strictEqual(void 0, position);
180
185
  });
@@ -183,7 +188,7 @@ describe('Tests for util.js', () => {
183
188
  const attributeValue = 'https://example.com/path/to/tile.json';
184
189
  assert.strictEqual(
185
190
  attributeValue,
186
- util.parseSimpleVector(attributeValue),
191
+ parseSimpleVector(attributeValue),
187
192
  );
188
193
  });
189
194
 
@@ -191,7 +196,7 @@ describe('Tests for util.js', () => {
191
196
  const attributeValue = 'geolonia://tiles/username/ct_123';
192
197
  assert.strictEqual(
193
198
  attributeValue,
194
- util.parseSimpleVector(attributeValue),
199
+ parseSimpleVector(attributeValue),
195
200
  );
196
201
  });
197
202
 
@@ -199,7 +204,7 @@ describe('Tests for util.js', () => {
199
204
  const attributeValue = 'ct_123';
200
205
  assert.strictEqual(
201
206
  'geolonia://tiles/custom/ct_123',
202
- util.parseSimpleVector(attributeValue),
207
+ parseSimpleVector(attributeValue),
203
208
  );
204
209
  });
205
210
 
@@ -208,7 +213,7 @@ describe('Tests for util.js', () => {
208
213
  const description = '<script>alert("hello");</script>ここが集合場所です。13時までに集合してください。';
209
214
  assert.strictEqual(
210
215
  'ここが集合場所です。13時までに集合してください。',
211
- await util.sanitizeDescription(description),
216
+ await sanitizeDescription(description),
212
217
  );
213
218
  });
214
219
 
@@ -217,7 +222,7 @@ describe('Tests for util.js', () => {
217
222
  const description = '<img decoding="auto" src="hibiya-park.jpeg" /><br />ここが集合場所です。13時までに集合してください。';
218
223
  assert.strictEqual(
219
224
  '<img src="hibiya-park.jpeg" /><br />ここが集合場所です。13時までに集合してください。',
220
- await util.sanitizeDescription(description),
225
+ await sanitizeDescription(description),
221
226
  );
222
227
  });
223
228
 
@@ -225,7 +230,7 @@ describe('Tests for util.js', () => {
225
230
  const description = '<span class="red">ここが集合場所です。13時までに集合してください。</span>';
226
231
  assert.strictEqual(
227
232
  '<span class="red">ここが集合場所です。13時までに集合してください。</span>',
228
- await util.sanitizeDescription(description),
233
+ await sanitizeDescription(description),
229
234
  );
230
235
  });
231
236
  });
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
- import { parseApiKey } from './parse-api-key';
3
+ import { keyring } from './keyring';
4
+ import type { MapOptions, MarkerOptions } from 'maplibre-gl';
4
5
 
5
6
  /**
6
7
  *
@@ -28,10 +29,8 @@ export function checkPermission() {
28
29
  return true;
29
30
  }
30
31
 
31
- const query = parseApiKey(window.document);
32
-
33
- // Always returs true if API key isn't 'YOUR-API-KEY'.
34
- if (query.key !== 'YOUR-API-KEY') {
32
+ // Always returs true if API key is loaded.
33
+ if (keyring.apiKey) {
35
34
  return true;
36
35
  }
37
36
 
@@ -114,7 +113,7 @@ export function isScrollable() {
114
113
  *
115
114
  * @param {*} o
116
115
  */
117
- export function isDomElement(o) {
116
+ export function isDomElement(o): o is HTMLElement {
118
117
  return (
119
118
  typeof HTMLElement === 'object' ? o instanceof HTMLElement : // DOM2
120
119
  o && typeof o === 'object' && o !== null && o.nodeType === 1 && typeof o.nodeName === 'string'
@@ -128,18 +127,18 @@ export function isDomElement(o) {
128
127
  * @param {*} arg
129
128
  * @return {HTMLElement | false}
130
129
  */
131
- export function getContainer(arg) {
130
+ export function getContainer(arg: HTMLElement | string | { container: HTMLElement | string; }): HTMLElement | false {
132
131
  if (isDomElement(arg)) {
133
132
  return arg;
134
133
  } else if (typeof arg === 'string') {
135
- const el = document.querySelector(arg) || document.getElementById(arg);
134
+ const el = document.querySelector(arg) as HTMLElement || document.getElementById(arg);
136
135
  return el || false;
137
136
 
138
137
  } else if (arg.container) {
139
138
  if (isDomElement(arg.container)) {
140
139
  return arg.container;
141
140
  } else if (typeof arg.container === 'string') {
142
- const el = document.querySelector(arg.container) || document.getElementById(arg.container);
141
+ const el = document.querySelector(arg.container) as HTMLElement || document.getElementById(arg.container);
143
142
  return el || false;
144
143
  }
145
144
  }
@@ -153,10 +152,10 @@ export function getContainer(arg) {
153
152
  * @param {*} options
154
153
  * @param {*} legacyOptions
155
154
  */
156
- export function handleMarkerOptions(options, legacyOptions) {
155
+ export function handleMarkerOptions(options: MarkerOptions | null | undefined | false, legacyOptions: MarkerOptions) {
157
156
  if (options && isDomElement(options)) {
158
157
  options = {
159
- element: options,
158
+ element: options as HTMLElement,
160
159
  ...legacyOptions,
161
160
  };
162
161
  } else if (!options) {
@@ -181,7 +180,7 @@ export function getStyle(style, atts) {
181
180
 
182
181
  // params are the parameters that have been passed to new geolonia.Map(params)
183
182
  // atts are the data-XYZ attributes that are on the container
184
- export function getOptions(container, params, atts) {
183
+ export function getOptions(container, params, atts): MapOptions {
185
184
  if (params.container) {
186
185
  delete params.container; // Don't overwrite container.
187
186
  }
@@ -190,7 +189,7 @@ export function getOptions(container, params, atts) {
190
189
  params = {}; // `params` is HTMLElement, so we shouldn't merge it into options.
191
190
  }
192
191
 
193
- const options = {
192
+ const options: MapOptions = {
194
193
  style: atts.style || params.style, // Validation for value of `style` will be processed on `setStyle()`.
195
194
  container,
196
195
  center: [parseFloat(atts.lng), parseFloat(atts.lat)],
@@ -198,7 +197,7 @@ export function getOptions(container, params, atts) {
198
197
  pitch: parseFloat(atts.pitch),
199
198
  zoom: parseFloat(atts.zoom),
200
199
  hash: (atts.hash === 'on'),
201
- localIdeographFontFamily: undefined,
200
+ localIdeographFontFamily: 'sans-serif',
202
201
  attributionControl: false,
203
202
  };
204
203
 
@@ -246,7 +245,7 @@ export const getSessionId = (digit) => {
246
245
  return sessionId;
247
246
  } else {
248
247
  const array = new Uint8Array(digit / 2);
249
- (window.crypto || window.msCrypto).getRandomValues(array);
248
+ window.crypto.getRandomValues(array);
250
249
  const value = Array
251
250
  .from(array, (dec) => dec.toString(16).padStart(2, '0'))
252
251
  .join('');
@@ -280,3 +279,5 @@ export const sanitizeDescription = async (description) => {
280
279
  allowedAttributes: {...sanitizeHtml.defaults.allowedAttributes, '*': ['class']},
281
280
  });
282
281
  };
282
+
283
+ export const random = (max: number): number => Math.floor(Math.random() * max);
package/src/style.css CHANGED
@@ -112,22 +112,18 @@
112
112
 
113
113
  /* CSS for Attribution */
114
114
  /* Fix: https://github.com/geolonia/embed/issues/291 */
115
- .mapboxgl-map,
116
115
  .maplibregl-map {
117
116
  display: block!important;
118
117
  }
119
118
 
120
- .maplibregl-ctrl-attrib-inner.mapboxgl-ctrl-attrib-inner {
119
+ .maplibregl-ctrl-attrib-inner {
121
120
  display: block!important;
122
121
  }
123
122
 
124
- .mapboxgl-ctrl-attrib.mapboxgl-compact .mapboxgl-ctrl-attrib-inner,
125
123
  .maplibregl-ctrl-attrib.maplibregl-compact .maplibregl-ctrl-attrib-inner {
126
124
  display: none!important;
127
125
  }
128
126
 
129
- .mapboxgl-ctrl-attrib.mapboxgl-compact-show .mapboxgl-ctrl-attrib-inner,
130
- .mapboxgl-ctrl-attrib.mapboxgl-compact .mapboxgl-ctrl-attrib-button,
131
127
  .maplibregl-ctrl-attrib.maplibregl-compact-show .maplibregl-ctrl-attrib-inner,
132
128
  .maplibregl-ctrl-attrib.maplibregl-compact .maplibregl-ctrl-attrib-button {
133
129
  display: block!important;
@@ -0,0 +1,4 @@
1
+ declare module '*.svg' {
2
+ const content: string;
3
+ export default content;
4
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2018",
4
+ "lib": [
5
+ "dom",
6
+ "dom.iterable",
7
+ "esnext"
8
+ ],
9
+ "declaration": true,
10
+ "declarationDir": "dist",
11
+ "skipLibCheck": true,
12
+ "sourceMap": true,
13
+ "esModuleInterop": true,
14
+ "allowSyntheticDefaultImports": true,
15
+ // "strict": true,
16
+ "forceConsistentCasingInFileNames": true,
17
+ "module": "esnext",
18
+ "moduleResolution": "node",
19
+ "resolveJsonModule": true,
20
+ "isolatedModules": true,
21
+ "jsx": "react-jsx",
22
+ "noFallthroughCasesInSwitch": true,
23
+ "newLine": "lf"
24
+ },
25
+ "include": [
26
+ "src"
27
+ ]
28
+ }
package/webpack.config.js CHANGED
@@ -2,12 +2,13 @@ const path = require('path');
2
2
  const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
3
3
 
4
4
  module.exports = {
5
- entry: './src/embed.js',
5
+ entry: './src/embed.ts',
6
6
  output: {
7
7
  path: path.join(__dirname, 'dist'),
8
8
  filename: 'embed.js',
9
9
  chunkFilename: path.join('embed-chunks', '[chunkhash].js'),
10
10
  clean: true,
11
+ publicPath: '',
11
12
  },
12
13
  plugins: process.env.ANALYZE === 'true' ? [new BundleAnalyzerPlugin()] : [],
13
14
  module: {
@@ -19,11 +20,9 @@ module.exports = {
19
20
  },
20
21
  },
21
22
  {
22
- test: /\.m?js$/,
23
+ test: /\.tsx?$/,
24
+ use: 'ts-loader',
23
25
  exclude: /node_modules/,
24
- use: {
25
- loader: 'babel-loader',
26
- },
27
26
  },
28
27
  {
29
28
  test: /\.css$/,
@@ -31,4 +30,7 @@ module.exports = {
31
30
  },
32
31
  ],
33
32
  },
33
+ resolve: {
34
+ extensions: ['.tsx', '.ts', '.js'],
35
+ },
34
36
  };
package/.babelrc DELETED
@@ -1,12 +0,0 @@
1
- {
2
- "plugins": ["@babel/plugin-transform-classes"],
3
- "presets": [
4
- [
5
- "@babel/preset-env",
6
- {
7
- "useBuiltIns": "usage",
8
- "corejs": 3
9
- }
10
- ]
11
- ]
12
- }