@geolonia/embed 4.3.0 → 5.0.2

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 (63) hide show
  1. package/.github/workflows/build.yml +99 -36
  2. package/.nvmrc +1 -1
  3. package/.prettierrc.json +4 -0
  4. package/.tool-versions +1 -1
  5. package/README.md +7 -1
  6. package/dist/e2e/basic.spec.d.ts +8 -0
  7. package/dist/e2e/helper.d.ts +4 -0
  8. package/dist/embed +2 -1
  9. package/dist/embed-chunks/2489af4328c51b71e92d.js +2 -0
  10. package/dist/embed-chunks/6e05bbf5f391fda5b858.js +1 -0
  11. package/dist/embed.js +2 -1
  12. package/dist/embed.js.LICENSE.txt +4 -0
  13. package/dist/playwright.config.d.ts +9 -0
  14. package/dist/{embed.d.ts → src/embed.d.ts} +3 -1
  15. package/dist/{lib → src/lib}/geolonia-map.d.ts +10 -2
  16. package/dist/{lib → src/lib}/geolonia-marker.d.ts +1 -2
  17. package/dist/{lib → src/lib}/maplibre-util.d.ts +1 -3
  18. package/dist/{lib → src/lib}/util.d.ts +6 -3
  19. package/dist/src/lib/util.test.d.ts +1 -0
  20. package/dist/src/testability.test.d.ts +1 -0
  21. package/dist/src/version.d.ts +1 -0
  22. package/e2e/basic.spec.ts +52 -0
  23. package/e2e/helper.ts +12 -0
  24. package/e2e/map.spec.ts +69 -0
  25. package/eslint.config.js +232 -0
  26. package/package.json +25 -12
  27. package/playwright.config.ts +79 -0
  28. package/src/embed.ts +13 -7
  29. package/src/lib/CustomAttributionControl.ts +52 -29
  30. package/src/lib/geolonia-map.ts +144 -38
  31. package/src/lib/geolonia-marker.ts +9 -6
  32. package/src/lib/keyring.test.ts +18 -0
  33. package/src/lib/keyring.ts +11 -13
  34. package/src/lib/maplibre-util.ts +31 -19
  35. package/src/lib/parse-atts.test.ts +2 -1
  36. package/src/lib/render.ts +18 -7
  37. package/src/lib/simplestyle-vector.ts +42 -13
  38. package/src/lib/simplestyle.test.ts +80 -69
  39. package/src/lib/simplestyle.ts +61 -43
  40. package/src/lib/util.test.ts +108 -21
  41. package/src/lib/util.ts +115 -32
  42. package/src/testability.test.ts +46 -0
  43. package/tsconfig.json +3 -1
  44. package/webpack.config.js +10 -1
  45. package/.eslintignore +0 -3
  46. package/.eslintrc.js +0 -14
  47. package/.prettierrc.js +0 -1
  48. package/dist/embed-chunks/b52b89229e47dc93c64b.js +0 -2
  49. package/dist/embed-chunks/d0f087405f7ae2fcf1c1.js +0 -1
  50. package/dist/version.d.ts +0 -1
  51. /package/dist/{lib/keyring.test.d.ts → e2e/map.spec.d.ts} +0 -0
  52. /package/dist/embed-chunks/{b52b89229e47dc93c64b.js.LICENSE.txt → 2489af4328c51b71e92d.js.LICENSE.txt} +0 -0
  53. /package/dist/{lib → src/lib}/CustomAttributionControl.d.ts +0 -0
  54. /package/dist/{lib → src/lib}/controls/geolonia-control.d.ts +0 -0
  55. /package/dist/{lib → src/lib}/keyring.d.ts +0 -0
  56. /package/dist/{lib/parse-atts.test.d.ts → src/lib/keyring.test.d.ts} +0 -0
  57. /package/dist/{lib → src/lib}/parse-atts.d.ts +0 -0
  58. /package/dist/{lib/simplestyle.test.d.ts → src/lib/parse-atts.test.d.ts} +0 -0
  59. /package/dist/{lib → src/lib}/render.d.ts +0 -0
  60. /package/dist/{lib → src/lib}/simplestyle-vector.d.ts +0 -0
  61. /package/dist/{lib → src/lib}/simplestyle.d.ts +0 -0
  62. /package/dist/{lib/util.test.d.ts → src/lib/simplestyle.test.d.ts} +0 -0
  63. /package/dist/{types-internal.d.ts → src/types-internal.d.ts} +0 -0
package/src/lib/util.ts CHANGED
@@ -1,21 +1,33 @@
1
1
  'use strict';
2
2
 
3
3
  import { keyring } from './keyring';
4
- import type { MapOptions, MarkerOptions } from 'maplibre-gl';
4
+ import type {
5
+ GetResourceResponse,
6
+ MapOptions,
7
+ MarkerOptions,
8
+ ExpiryData,
9
+ } from 'maplibre-gl';
10
+
11
+ // GetImageCallback extracted from maplibre-gl v3.6.2 ( https://github.com/maplibre/maplibre-gl-js/releases/tag/v3.6.2 )
12
+ export type GetImageCallback = (
13
+ error?: Error | null,
14
+ image?: HTMLImageElement | ImageBitmap | null,
15
+ expiry?: ExpiryData | null,
16
+ ) => void;
5
17
 
6
18
  /**
7
19
  *
8
20
  * @param {string} str target URL string
9
21
  * @return {string|false} Resolved URL or false if not resolved
10
22
  */
11
- export function isURL(str) {
23
+ export function isURL(str: string): string | false {
12
24
  if (str.match(/^https?:\/\//)) {
13
25
  return str;
14
26
  } else if (str.match(/^\//) || str.match(/^\.\.?/)) {
15
27
  try {
16
28
  return new URL(str, location.href).href;
17
29
  } catch (error) {
18
- console.error('[Geolonia]', error) // eslint-disable-line
30
+ console.error('[Geolonia]', error); // eslint-disable-line
19
31
  return false;
20
32
  }
21
33
  }
@@ -23,6 +35,20 @@ export function isURL(str) {
23
35
  return false;
24
36
  }
25
37
 
38
+ export function isGeoloniaTilesHost(url: string | URL): boolean {
39
+ try {
40
+ const urlObj = typeof url === 'string' ? new URL(url) : url;
41
+
42
+ return (
43
+ // NOTE: geolonia use tileserver.geolonia.com or *.tiles.geolonia.com for tile server. Other domains is NOT allowed.
44
+ urlObj.hostname === 'tileserver.geolonia.com' ||
45
+ urlObj.hostname.endsWith('.tiles.geolonia.com')
46
+ );
47
+ } catch {
48
+ return false;
49
+ }
50
+ }
51
+
26
52
  export function checkPermission() {
27
53
  // It looks that isn't iFrame, so returs true.
28
54
  if (window.self === window.parent) {
@@ -42,7 +68,10 @@ export function checkPermission() {
42
68
  window.self.location.origin === 'https://cdpn.io' ||
43
69
  window.self.location.origin === 'https://codepen.io'
44
70
  ) {
45
- if (window.self !== window.parent && window.document.referrer.indexOf('https://codepen.io') === 0) {
71
+ if (
72
+ window.self !== window.parent &&
73
+ window.document.referrer.indexOf('https://codepen.io') === 0
74
+ ) {
46
75
  return true;
47
76
  }
48
77
  }
@@ -51,7 +80,10 @@ export function checkPermission() {
51
80
  * For the https://jsfiddle.net/
52
81
  */
53
82
  if (window.self.location.origin === 'https://fiddle.jshell.net') {
54
- if (window.self !== window.parent && window.document.referrer.indexOf('https://jsfiddle.net') === 0) {
83
+ if (
84
+ window.self !== window.parent &&
85
+ window.document.referrer.indexOf('https://jsfiddle.net') === 0
86
+ ) {
55
87
  return true;
56
88
  }
57
89
  }
@@ -64,7 +96,10 @@ export function checkPermission() {
64
96
  * another one is in new window.
65
97
  */
66
98
  if (window.self.location.origin.match(/csb\.app$/)) {
67
- if (window.self !== window.parent && window.document.referrer.indexOf('https://codesandbox.io') === 0) {
99
+ if (
100
+ window.self !== window.parent &&
101
+ window.document.referrer.indexOf('https://codesandbox.io') === 0
102
+ ) {
68
103
  return true;
69
104
  }
70
105
  }
@@ -77,7 +112,7 @@ export function checkPermission() {
77
112
  if (window.self.location.origin === window.top.location.origin) {
78
113
  return true;
79
114
  }
80
- } catch (e) {
115
+ } catch {
81
116
  return false;
82
117
  }
83
118
 
@@ -85,11 +120,11 @@ export function checkPermission() {
85
120
  }
86
121
 
87
122
  export function getLang() {
88
- const lang = (
89
- window.navigator.languages &&
90
- window.navigator.languages[0] &&
91
- window.navigator.languages[0].toLowerCase()
92
- ) || window.navigator.language.toLowerCase();
123
+ const lang =
124
+ (window.navigator.languages &&
125
+ window.navigator.languages[0] &&
126
+ window.navigator.languages[0].toLowerCase()) ||
127
+ window.navigator.language.toLowerCase();
93
128
 
94
129
  if (lang === 'ja' || lang === 'ja-jp') {
95
130
  return 'ja';
@@ -105,7 +140,10 @@ export function isScrollable() {
105
140
  const body = document.body;
106
141
  const html = document.documentElement;
107
142
 
108
- return body.scrollHeight > body.clientHeight || html.scrollHeight > html.clientHeight;
143
+ return (
144
+ body.scrollHeight > body.clientHeight ||
145
+ html.scrollHeight > html.clientHeight
146
+ );
109
147
  }
110
148
 
111
149
  /**
@@ -114,10 +152,13 @@ export function isScrollable() {
114
152
  * @param {*} o
115
153
  */
116
154
  export function isDomElement(o): o is HTMLElement {
117
- return (
118
- typeof HTMLElement === 'object' ? o instanceof HTMLElement : // DOM2
119
- o && typeof o === 'object' && o !== null && o.nodeType === 1 && typeof o.nodeName === 'string'
120
- );
155
+ return typeof HTMLElement === 'object'
156
+ ? o instanceof HTMLElement // DOM2
157
+ : o &&
158
+ typeof o === 'object' &&
159
+ o !== null &&
160
+ o.nodeType === 1 &&
161
+ typeof o.nodeName === 'string';
121
162
  }
122
163
 
123
164
  /**
@@ -127,18 +168,23 @@ export function isDomElement(o): o is HTMLElement {
127
168
  * @param {*} arg
128
169
  * @return {HTMLElement | false}
129
170
  */
130
- export function getContainer(arg: HTMLElement | string | { container: HTMLElement | string; }): HTMLElement | false {
171
+ export function getContainer(
172
+ arg: HTMLElement | string | { container: HTMLElement | string },
173
+ ): HTMLElement | false {
131
174
  if (isDomElement(arg)) {
132
175
  return arg;
133
176
  } else if (typeof arg === 'string') {
134
- const el = document.querySelector(arg) as HTMLElement || document.getElementById(arg);
177
+ const el =
178
+ (document.querySelector(arg) as HTMLElement) ||
179
+ document.getElementById(arg);
135
180
  return el || false;
136
-
137
181
  } else if (arg.container) {
138
182
  if (isDomElement(arg.container)) {
139
183
  return arg.container;
140
184
  } else if (typeof arg.container === 'string') {
141
- const el = document.querySelector(arg.container) as HTMLElement || document.getElementById(arg.container);
185
+ const el =
186
+ (document.querySelector(arg.container) as HTMLElement) ||
187
+ document.getElementById(arg.container);
142
188
  return el || false;
143
189
  }
144
190
  }
@@ -152,7 +198,10 @@ export function getContainer(arg: HTMLElement | string | { container: HTMLElemen
152
198
  * @param {*} options
153
199
  * @param {*} legacyOptions
154
200
  */
155
- export function handleMarkerOptions(options: MarkerOptions | null | undefined | false, legacyOptions: MarkerOptions) {
201
+ export function handleMarkerOptions(
202
+ options: MarkerOptions | null | undefined | false,
203
+ legacyOptions: MarkerOptions,
204
+ ) {
156
205
  if (options && isDomElement(options)) {
157
206
  options = {
158
207
  element: options as HTMLElement,
@@ -196,12 +245,15 @@ export function getOptions(container, params, atts): MapOptions {
196
245
  bearing: parseFloat(atts.bearing),
197
246
  pitch: parseFloat(atts.pitch),
198
247
  zoom: parseFloat(atts.zoom),
199
- hash: (atts.hash === 'on'),
248
+ hash: atts.hash === 'on',
200
249
  localIdeographFontFamily: 'sans-serif',
201
250
  attributionControl: false,
202
251
  };
203
252
 
204
- if (atts.minZoom !== '' && (Number(atts.minZoom) === 0 || Number(atts.minZoom))) {
253
+ if (
254
+ atts.minZoom !== '' &&
255
+ (Number(atts.minZoom) === 0 || Number(atts.minZoom))
256
+ ) {
205
257
  options.minZoom = Number(atts.minZoom);
206
258
  }
207
259
 
@@ -221,7 +273,11 @@ export function getOptions(container, params, atts): MapOptions {
221
273
  */
222
274
  export function parseControlOption(att) {
223
275
  const normalizedAtt = att.toLowerCase();
224
- if (['top-right', 'bottom-right', 'bottom-left', 'top-left'].includes(normalizedAtt)) {
276
+ if (
277
+ ['top-right', 'bottom-right', 'bottom-left', 'top-left'].includes(
278
+ normalizedAtt,
279
+ )
280
+ ) {
225
281
  return { enabled: true, position: normalizedAtt };
226
282
  } else if (['on', 'off'].includes(normalizedAtt)) {
227
283
  return { enabled: normalizedAtt === 'on', position: void 0 };
@@ -246,9 +302,9 @@ export const getSessionId = (digit) => {
246
302
  } else {
247
303
  const array = new Uint8Array(digit / 2);
248
304
  window.crypto.getRandomValues(array);
249
- const value = Array
250
- .from(array, (dec) => dec.toString(16).padStart(2, '0'))
251
- .join('');
305
+ const value = Array.from(array, (dec) =>
306
+ dec.toString(16).padStart(2, '0'),
307
+ ).join('');
252
308
  sessionId = value;
253
309
  return value;
254
310
  }
@@ -273,7 +329,6 @@ export const handleRestrictedMode = (map) => {
273
329
  };
274
330
 
275
331
  export const handleErrorMode = (container) => {
276
-
277
332
  const errorContainer = document.createElement('div');
278
333
  errorContainer.classList.add('geolonia__error-container');
279
334
 
@@ -284,7 +339,8 @@ export const handleErrorMode = (container) => {
284
339
  div.appendChild(h1);
285
340
 
286
341
  div.classList.add('geolonia__error-message');
287
- div.innerHTML += '<div class="geolonia__error-message-description">地図の初期化に失敗しました。管理者にお問い合わせ下さい。開発者の方は開発者ツールでエラー詳細をご確認下さい。</div>';
342
+ div.innerHTML +=
343
+ '<div class="geolonia__error-message-description">地図の初期化に失敗しました。管理者にお問い合わせ下さい。開発者の方は開発者ツールでエラー詳細をご確認下さい。</div>';
288
344
 
289
345
  errorContainer.appendChild(div);
290
346
  container.appendChild(errorContainer);
@@ -293,10 +349,37 @@ export const handleErrorMode = (container) => {
293
349
  export const sanitizeDescription = async (description) => {
294
350
  const { default: sanitizeHtml } = await import('sanitize-html');
295
351
  return sanitizeHtml(description, {
296
- allowedTags: sanitizeHtml.defaults.allowedTags.concat([ 'img' ]),
297
- allowedAttributes: {...sanitizeHtml.defaults.allowedAttributes, '*': ['class']},
352
+ allowedTags: sanitizeHtml.defaults.allowedTags.concat(['img']),
353
+ allowedAttributes: {
354
+ ...sanitizeHtml.defaults.allowedAttributes,
355
+ '*': ['class'],
356
+ },
298
357
  });
299
358
  };
300
359
 
301
360
  export const random = (max: number): number => Math.floor(Math.random() * max);
302
361
 
362
+ // This function is used to provide backward compatibility with callback invocations,
363
+ // so we ignore ESLint rules for this function.
364
+ // Note: this cannot be a generic promiseToCallback function because the callback is not
365
+ // the traditional (error, result) style callback.
366
+ export function loadImageCompatibility(
367
+ promise: Promise<GetResourceResponse<HTMLImageElement | ImageBitmap>>,
368
+ callback: GetImageCallback,
369
+ ): void {
370
+ promise
371
+ // eslint-disable-next-line promise/prefer-await-to-then
372
+ .then((response) => {
373
+ // eslint-disable-next-line promise/no-callback-in-promise
374
+ callback(null, response.data, {
375
+ cacheControl: response.cacheControl,
376
+ expires: response.expires,
377
+ });
378
+ return;
379
+ })
380
+ // eslint-disable-next-line promise/prefer-await-to-then
381
+ .catch((error) => {
382
+ // eslint-disable-next-line promise/no-callback-in-promise
383
+ callback(error);
384
+ });
385
+ }
@@ -0,0 +1,46 @@
1
+ import { JSDOM } from 'jsdom';
2
+ import assert from 'assert';
3
+ import maplibregl from 'maplibre-gl';
4
+
5
+ /**
6
+ * このテストは、jsdom環境ではmaplibre-glのMapインスタンスが
7
+ * WebGL不足のため生成できないことを「動作証跡付き」でドキュメント化するものです。
8
+ * 実際の描画テストはE2Eテストで行ってください。
9
+ */
10
+
11
+ describe('maplibre-gl jsdom limitation', () => {
12
+ it('jsdom上ではMaplibre Mapインスタンス生成時にエラー(WebGL未対応のため)', () => {
13
+ const dom = new JSDOM(
14
+ '<!DOCTYPE html><html><body><div id="map"></div></body></html>',
15
+ {
16
+ url: 'http://localhost',
17
+ pretendToBeVisual: true, // jsdomを視覚的な環境として
18
+ },
19
+ );
20
+ // @ts-ignore
21
+ global.window = dom.window;
22
+ global.document = dom.window.document;
23
+ let errorCaught = null;
24
+ try {
25
+ new maplibregl.Map({
26
+ container: 'map',
27
+ style: 'https://demotiles.maplibre.org/style.json',
28
+ center: [139.767, 35.681],
29
+ zoom: 10,
30
+ });
31
+ } catch (err) {
32
+ errorCaught = err;
33
+ }
34
+ assert(errorCaught instanceof Error);
35
+ // 期待される理由: WebGLが利用できない旨のエラーメッセージ
36
+ assert(
37
+ /webgl|WebGL|context|device/i.test(errorCaught.message),
38
+ `Error message should mention WebGL or context, but got: ${errorCaught.message}`,
39
+ );
40
+ });
41
+
42
+ it('【NOTE】描画テストが必要な場合はE2E(ブラウザ実行)で行うこと', () => {
43
+ // このテストは常に成功。ドキュメント用途。
44
+ assert.strictEqual(true, true);
45
+ });
46
+ });
package/tsconfig.json CHANGED
@@ -23,6 +23,8 @@
23
23
  "newLine": "lf"
24
24
  },
25
25
  "include": [
26
- "src"
26
+ "src",
27
+ "e2e",
28
+ "playwright.config.ts",
27
29
  ]
28
30
  }
package/webpack.config.js CHANGED
@@ -1,6 +1,15 @@
1
1
  const path = require('path');
2
2
  const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
3
+ const { DefinePlugin } = require('webpack');
3
4
 
5
+ const plugins = [
6
+ new DefinePlugin({
7
+ 'process.env.MAP_PLATFORM_STAGE': JSON.stringify(process.env.MAP_PLATFORM_STAGE || 'dev'),
8
+ }),
9
+ ];
10
+ if (process.env.ANALYZE === 'true') {
11
+ plugins.push(new BundleAnalyzerPlugin());
12
+ }
4
13
  module.exports = {
5
14
  entry: './src/embed.ts',
6
15
  output: {
@@ -14,7 +23,7 @@ module.exports = {
14
23
  type: 'umd',
15
24
  },
16
25
  },
17
- plugins: process.env.ANALYZE === 'true' ? [new BundleAnalyzerPlugin()] : [],
26
+ plugins: plugins,
18
27
  module: {
19
28
  rules: [
20
29
  {
package/.eslintignore DELETED
@@ -1,3 +0,0 @@
1
- /node_modules/
2
- /docs
3
- /dist
package/.eslintrc.js DELETED
@@ -1,14 +0,0 @@
1
- module.exports = {
2
- env: {
3
- browser: true,
4
- commonjs: true,
5
- es6: true,
6
- node: true,
7
- mocha: true,
8
- },
9
- extends: ['@geolonia'],
10
- parserOptions: {
11
- sourceType: 'module',
12
- project: './tsconfig.json',
13
- },
14
- };
package/.prettierrc.js DELETED
@@ -1 +0,0 @@
1
- module.exports = {}