@geolonia/embed 5.0.2 → 5.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.
@@ -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) {
@@ -123,7 +123,7 @@ describe('Tests for util.js', () => {
123
123
  assert.deepEqual(false, getContainer({ container: 'fail-element' }));
124
124
  });
125
125
 
126
- it('should merge legacyoptions into options as expected.', () => {
126
+ it('should merge legacy options into options as expected.', () => {
127
127
  const dom = new JSDOM(`<html><body>
128
128
  <div id="test-element"></div>
129
129
  </body></html>`);
@@ -174,24 +174,98 @@ describe('Tests for util.js', () => {
174
174
  const atts = {
175
175
  apiUrl: 'https://example.com',
176
176
  key: '1234',
177
+ lang: 'en',
177
178
  };
178
179
 
180
+ // 従来モード: 論理名(API キー必須)
179
181
  assert.deepEqual(
180
182
  'https://cdn.geolonia.com/style/hello/world/en.json',
181
183
  getStyle('hello/world', atts),
182
184
  );
185
+
186
+ // URL モード: https://(外部 URL、API キー不要)
183
187
  assert.deepEqual(
184
188
  'https://example.com/style.json',
185
189
  getStyle('https://example.com/style.json', atts),
186
190
  );
191
+
192
+ // URL モード: 相対パス ./(外部 URL、API キー不要)
187
193
  assert.deepEqual(
188
194
  'https://base.example.com/parent/style.json',
189
195
  getStyle('./style.json', atts),
190
196
  );
197
+
198
+ // URL モード: 絶対パス /(外部 URL、API キー不要)
191
199
  assert.deepEqual(
192
200
  'https://base.example.com/style.json',
193
201
  getStyle('/style.json', atts),
194
202
  );
203
+
204
+ // URL モード: .json で終わる(外部 URL として解決)
205
+ assert.deepEqual(
206
+ 'https://base.example.com/parent/custom.json',
207
+ getStyle('custom.json', atts),
208
+ );
209
+
210
+ // デフォルト: 空文字列(API キー必須)
211
+ assert.deepEqual(
212
+ 'https://cdn.geolonia.com/style/geolonia/basic-v2/en.json',
213
+ getStyle('', atts),
214
+ );
215
+
216
+ // デフォルト: null/undefined(API キー必須)
217
+ assert.deepEqual(
218
+ 'https://cdn.geolonia.com/style/geolonia/basic-v2/en.json',
219
+ getStyle(null, atts),
220
+ );
221
+
222
+ // 日本語環境でのデフォルト
223
+ const attsJa = { ...atts, lang: 'ja' };
224
+ assert.deepEqual(
225
+ 'https://cdn.geolonia.com/style/geolonia/basic-v2/ja.json',
226
+ getStyle('', attsJa),
227
+ );
228
+
229
+ // 日本語環境での論理名
230
+ assert.deepEqual(
231
+ 'https://cdn.geolonia.com/style/geolonia/basic/ja.json',
232
+ getStyle('geolonia/basic', attsJa),
233
+ );
234
+ });
235
+
236
+ it('should throw error when using Geolonia styles without API key', () => {
237
+ const attsNoKey = {
238
+ apiUrl: 'https://example.com',
239
+ key: '',
240
+ lang: 'en',
241
+ };
242
+
243
+ // 論理名: API キー無しでエラー
244
+ assert.throws(
245
+ () => getStyle('geolonia/basic', attsNoKey),
246
+ /API key is required/,
247
+ );
248
+
249
+ // デフォルト: API キー無しでエラー
250
+ assert.throws(
251
+ () => getStyle('', attsNoKey),
252
+ /API key is required/,
253
+ );
254
+
255
+ // Geolonia CDN の URL: API キー無しでエラー
256
+ assert.throws(
257
+ () => getStyle('https://cdn.geolonia.com/style/geolonia/basic/en.json', attsNoKey),
258
+ /API key is required/,
259
+ );
260
+
261
+ // 外部 URL: API キー無しでも OK
262
+ assert.doesNotThrow(() => {
263
+ getStyle('https://tile.openstreetmap.jp/styles/osm-bright/style.json', attsNoKey);
264
+ });
265
+
266
+ assert.doesNotThrow(() => {
267
+ getStyle('./my-style.json', attsNoKey);
268
+ });
195
269
  });
196
270
 
197
271
  it('should handle maplibregl options `minZoom` and `maxZoom` well', () => {
@@ -249,7 +323,7 @@ describe('Tests for util.js', () => {
249
323
  assert.strictEqual(attributeValue, parseSimpleVector(attributeValue));
250
324
  });
251
325
 
252
- it('should parse simple vector value with customtile ID', () => {
326
+ it('should parse simple vector value with custom tile ID', () => {
253
327
  const attributeValue = 'ct_123';
254
328
  assert.strictEqual(
255
329
  'geolonia://tiles/custom/ct_123',
package/src/lib/util.ts CHANGED
@@ -50,12 +50,12 @@ export function isGeoloniaTilesHost(url: string | URL): boolean {
50
50
  }
51
51
 
52
52
  export function checkPermission() {
53
- // It looks that isn't iFrame, so returs true.
53
+ // It looks that isn't iFrame, so returns true.
54
54
  if (window.self === window.parent) {
55
55
  return true;
56
56
  }
57
57
 
58
- // Always returs true if API key is loaded.
58
+ // Always returns true if API key is loaded.
59
59
  if (keyring.apiKey) {
60
60
  return true;
61
61
  }
@@ -106,7 +106,7 @@ export function checkPermission() {
106
106
 
107
107
  /**
108
108
  * `window.parent` will be blocked if same origin policy is activated.
109
- * So, it should be catched.
109
+ * So, it should be caught.
110
110
  */
111
111
  try {
112
112
  if (window.self.location.origin === window.top.location.origin) {
@@ -147,7 +147,7 @@ export function isScrollable() {
147
147
  }
148
148
 
149
149
  /**
150
- * Detects the object is HTMLElment?
150
+ * Detects the object is HTMLElement?
151
151
  *
152
152
  * @param {*} o
153
153
  */
@@ -215,16 +215,61 @@ export function handleMarkerOptions(
215
215
  }
216
216
 
217
217
  export function getStyle(style, atts) {
218
+ // Geolonia スタイルを使う場合は API キー必須
219
+ if (keyring.isGeoloniaStyleCheck(style) && !atts.key) {
220
+ throw new Error('[Geolonia] API key is required to use Geolonia styles. Please provide an API key or use an external style URL.');
221
+ }
222
+
223
+ // 空の場合はデフォルト(既存挙動を維持)
224
+ if (!style || style === '') {
225
+ if (atts.lang === 'ja') {
226
+ return 'https://cdn.geolonia.com/style/geolonia/basic-v2/ja.json';
227
+ } else {
228
+ return 'https://cdn.geolonia.com/style/geolonia/basic-v2/en.json';
229
+ }
230
+ }
231
+
232
+ // URL モード: http:// or https:// で始まる、または相対パス(/、./、../)
218
233
  const styleUrl = isURL(style);
219
234
  if (styleUrl) {
235
+ // Mixed Content の警告
236
+ if (
237
+ location.protocol === 'https:' &&
238
+ styleUrl.startsWith('http:')
239
+ ) {
240
+ console.warn( // eslint-disable-line no-console
241
+ `[Geolonia] Mixed Content Warning: You are loading a style from an insecure HTTP URL (${styleUrl}) on an HTTPS page. This may fail in modern browsers.`,
242
+ );
243
+ }
220
244
  return styleUrl;
221
- } else {
222
- if (atts.lang === 'ja') {
223
- return `https://cdn.geolonia.com/style/${style}/ja.json`;
224
- } else {
225
- return `https://cdn.geolonia.com/style/${style}/en.json`;
245
+ }
246
+
247
+ // *.json で終わる場合も URL として扱う(相対パスの可能性)
248
+ if (style.endsWith('.json')) {
249
+ try {
250
+ const absoluteUrl = new URL(style, location.href).href;
251
+ if (
252
+ location.protocol === 'https:' &&
253
+ absoluteUrl.startsWith('http:')
254
+ ) {
255
+ console.warn( // eslint-disable-line no-console
256
+ `[Geolonia] Mixed Content Warning: You are loading a style from an insecure HTTP URL (${absoluteUrl}) on an HTTPS page. This may fail in modern browsers.`,
257
+ );
258
+ }
259
+ return absoluteUrl;
260
+ } catch (error) {
261
+ console.error('[Geolonia] Failed to resolve style URL:', style, error); // eslint-disable-line no-console
262
+ // フォールバック: そのまま返す
263
+ return style;
226
264
  }
227
265
  }
266
+
267
+ // 従来モード: geolonia/basic などの論理名
268
+ if (atts.lang === 'ja') {
269
+ return `https://cdn.geolonia.com/style/${style}/ja.json`;
270
+ } else {
271
+ return `https://cdn.geolonia.com/style/${style}/en.json`;
272
+ }
228
273
  }
229
274
 
230
275
  // params are the parameters that have been passed to new geolonia.Map(params)
@@ -269,7 +314,7 @@ export function getOptions(container, params, atts): MapOptions {
269
314
  /**
270
315
  *
271
316
  * @param {string} an data-*-control Embed attribute
272
- * @returns { enabled: bolean, position: 'top-right' | 'bottom-right' | 'bottom-left' | 'top-left' | void }
317
+ * @returns { enabled: boolean, position: 'top-right' | 'bottom-right' | 'bottom-left' | 'top-left' | void }
273
318
  */
274
319
  export function parseControlOption(att) {
275
320
  const normalizedAtt = att.toLowerCase();
package/src/types.ts ADDED
@@ -0,0 +1,44 @@
1
+ import type GeoloniaMap from './lib/geolonia-map';
2
+
3
+ export type EmbedAttributes = {
4
+ lat: string | number;
5
+ lng: string | number;
6
+ zoom: string | number;
7
+ bearing: string | number;
8
+ pitch: string | number;
9
+ hash: string;
10
+ marker: string;
11
+ markerColor: string;
12
+ openPopup: string;
13
+ customMarker: string;
14
+ customMarkerOffset: string;
15
+ gestureHandling: string;
16
+ navigationControl: string;
17
+ geolocateControl: string;
18
+ fullscreenControl: string;
19
+ scaleControl: string;
20
+ geoloniaControl: string;
21
+ geojson: string;
22
+ cluster: string;
23
+ clusterColor: string;
24
+ style: string;
25
+ lang: string;
26
+ plugin: string;
27
+ key: string;
28
+ apiUrl: string;
29
+ loader: string;
30
+ minZoom: string | number;
31
+ maxZoom: string | number;
32
+ '3d': string;
33
+ [otherKey: string]: string | number;
34
+ };
35
+
36
+ export type EmbedPlugin<
37
+ PluginAttributes extends { [otherKey: string]: string } = {
38
+ [otherKey: string]: string;
39
+ },
40
+ > = (
41
+ map: GeoloniaMap,
42
+ target: HTMLElement,
43
+ atts: EmbedAttributes & PluginAttributes,
44
+ ) => void;
package/webpack.config.js CHANGED
@@ -10,19 +10,8 @@ const plugins = [
10
10
  if (process.env.ANALYZE === 'true') {
11
11
  plugins.push(new BundleAnalyzerPlugin());
12
12
  }
13
- module.exports = {
14
- entry: './src/embed.ts',
15
- output: {
16
- path: path.join(__dirname, 'dist'),
17
- filename: 'embed.js',
18
- chunkFilename: path.join('embed-chunks', '[chunkhash].js'),
19
- clean: true,
20
- publicPath: 'auto',
21
- library: {
22
- name: 'geoloniaEmbed',
23
- type: 'umd',
24
- },
25
- },
13
+
14
+ const sharedConfig = {
26
15
  plugins: plugins,
27
16
  module: {
28
17
  rules: [
@@ -47,3 +36,40 @@ module.exports = {
47
36
  extensions: ['.tsx', '.ts', '.js'],
48
37
  },
49
38
  };
39
+
40
+ const embedConfig = {
41
+ name: 'embed',
42
+ ...sharedConfig,
43
+ entry: './src/embed.ts',
44
+ output: {
45
+ path: path.join(__dirname, 'dist'),
46
+ filename: 'embed.js',
47
+ chunkFilename: path.join('embed-chunks', '[chunkhash].js'),
48
+ clean: true,
49
+ publicPath: 'auto',
50
+ library: {
51
+ name: 'geoloniaEmbed',
52
+ type: 'umd',
53
+ },
54
+ },
55
+ };
56
+
57
+ const embedCoreConfig = {
58
+ name: 'embed-core',
59
+ dependencies: ['embed'],
60
+ ...sharedConfig,
61
+ entry: './src/embed-core.ts',
62
+ output: {
63
+ path: path.join(__dirname, 'dist'),
64
+ filename: 'embed-core.js',
65
+ chunkFilename: path.join('embed-chunks', '[chunkhash].js'),
66
+ clean: false,
67
+ publicPath: 'auto',
68
+ library: {
69
+ name: 'geoloniaEmbedCore',
70
+ type: 'umd',
71
+ },
72
+ },
73
+ };
74
+
75
+ module.exports = [embedConfig, embedCoreConfig];