@geolonia/embed 4.0.0 → 4.1.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 (57) hide show
  1. package/.eslintrc.js +1 -2
  2. package/.github/workflows/build.yml +1 -1
  3. package/.mocharc.js +9 -0
  4. package/.tool-versions +1 -1
  5. package/PLUGIN.md +1 -1
  6. package/dist/embed +1 -1
  7. package/dist/embed-chunks/4472555f7ea90b733a8e.js +2 -0
  8. package/dist/embed-chunks/4b3a60cc98a6be867ce3.js +1 -0
  9. package/dist/embed.d.ts +62 -0
  10. package/dist/embed.js +1 -1
  11. package/dist/lib/CustomAttributionControl.d.ts +45 -0
  12. package/dist/lib/controls/geolonia-control.d.ts +7 -0
  13. package/dist/lib/geolonia-map.d.ts +22 -0
  14. package/dist/lib/geolonia-marker.d.ts +10 -0
  15. package/dist/lib/keyring.d.ts +29 -0
  16. package/dist/lib/keyring.test.d.ts +1 -0
  17. package/dist/lib/maplibre-util.d.ts +27 -0
  18. package/dist/lib/parse-atts.d.ts +6 -0
  19. package/dist/lib/parse-atts.test.d.ts +1 -0
  20. package/dist/lib/render.d.ts +5 -0
  21. package/dist/lib/simplestyle-vector.d.ts +26 -0
  22. package/dist/lib/simplestyle.d.ts +29 -0
  23. package/dist/lib/simplestyle.test.d.ts +1 -0
  24. package/dist/lib/util.d.ts +57 -0
  25. package/dist/lib/util.test.d.ts +1 -0
  26. package/dist/types-internal.d.ts +4 -0
  27. package/dist/version.d.ts +1 -0
  28. package/package.json +17 -22
  29. package/scripts/build-version.mjs +12 -0
  30. package/src/embed.ts +85 -0
  31. package/src/lib/{CustomAttributionControl.js → CustomAttributionControl.ts} +14 -2
  32. package/src/lib/controls/{geolonia-control.js → geolonia-control.ts} +6 -2
  33. package/src/lib/{geolonia-map.js → geolonia-map.ts} +44 -47
  34. package/src/lib/{geolonia-marker.js → geolonia-marker.ts} +10 -9
  35. package/src/lib/keyring.test.ts +66 -0
  36. package/src/lib/keyring.ts +86 -0
  37. package/src/lib/{maplibre-util.js → maplibre-util.ts} +1 -1
  38. package/src/lib/{parse-atts.test.js → parse-atts.test.ts} +5 -1
  39. package/src/lib/{parse-atts.js → parse-atts.ts} +14 -9
  40. package/src/lib/render.ts +100 -0
  41. package/src/lib/{simplestyle-vector.js → simplestyle-vector.ts} +4 -3
  42. package/src/lib/{simplestyle.test.js → simplestyle.test.ts} +26 -27
  43. package/src/lib/{simplestyle.js → simplestyle.ts} +9 -8
  44. package/src/lib/{util.test.js → util.test.ts} +51 -46
  45. package/src/lib/{util.js → util.ts} +15 -14
  46. package/src/types-internal.ts +4 -0
  47. package/tsconfig.json +28 -0
  48. package/webpack.config.js +7 -5
  49. package/.babelrc +0 -12
  50. package/dist/embed-chunks/3d467b2d527e4e5de1b8.js +0 -1
  51. package/dist/embed-chunks/eb491da30c0af7f50004.js +0 -2
  52. package/src/embed.d.ts +0 -58
  53. package/src/embed.js +0 -119
  54. package/src/lib/parse-api-key.js +0 -77
  55. package/src/lib/parse-api-key.test.js +0 -102
  56. package/src/lib/providers/amazon.js +0 -116
  57. /package/dist/embed-chunks/{eb491da30c0af7f50004.js.LICENSE.txt → 4472555f7ea90b733a8e.js.LICENSE.txt} +0 -0
package/src/embed.js DELETED
@@ -1,119 +0,0 @@
1
- /**
2
- * @file Entry for embed.js
3
- */
4
-
5
- import maplibregl from 'maplibre-gl';
6
- import 'maplibre-gl/dist/maplibre-gl.css';
7
- import './style.css';
8
- import GeoloniaMap from './lib/geolonia-map';
9
- import GeoloniaMarker from './lib/geolonia-marker';
10
- import { AmazonLocationServiceMapProvider } from './lib/providers/amazon';
11
- import * as util from './lib/util';
12
- import parseAtts from './lib/parse-atts';
13
- import { parseApiKey } from './lib/parse-api-key';
14
- import pkg from '../package.json';
15
- import SimpleStyle from './lib/simplestyle';
16
- import * as pmtiles from 'pmtiles';
17
-
18
- const protocol = new pmtiles.Protocol();
19
- maplibregl.addProtocol('pmtiles', protocol.tile);
20
-
21
- if ( util.checkPermission() ) {
22
- let isDOMContentLoaded = false;
23
- const alreadyRenderedMaps = [];
24
- const plugins = [];
25
- const isRemoved = Symbol('map-is-removed');
26
-
27
- // Create the initial window.geolonia object if it doesn't exist.
28
- parseApiKey(document);
29
-
30
- /**
31
- *
32
- * @param {HTMLElement} target
33
- */
34
- const renderGeoloniaMap = (target) => {
35
- const map = new GeoloniaMap(target);
36
-
37
- // detect if the map removed manually
38
- map.on('remove', () => {
39
- map[isRemoved] = true;
40
- });
41
-
42
- // remove map instance automatically if the container removed.
43
- // prevent memory leak
44
- const observer = new MutationObserver((mutationRecords) => {
45
- const removed = mutationRecords.some((record) => [...record.removedNodes].some((node) => node === target));
46
- if (removed && !map[isRemoved]) {
47
- map.remove();
48
- }
49
- });
50
- observer.observe(target.parentNode, { childList: true });
51
-
52
- // plugin
53
- const atts = parseAtts(target);
54
- if (isDOMContentLoaded && !map[isRemoved]) {
55
- plugins.forEach((plugin) => plugin(map, target, atts));
56
- } else {
57
- alreadyRenderedMaps.push({ map, target: target, atts });
58
- }
59
- };
60
-
61
- document.addEventListener('DOMContentLoaded', () => {
62
- isDOMContentLoaded = true;
63
- alreadyRenderedMaps.forEach(({ map, target, atts }) => {
64
- if (!map[isRemoved]) {
65
- plugins.forEach((plugin) => plugin(map, target, atts));
66
- }
67
- });
68
- // clear
69
- alreadyRenderedMaps.splice(0, alreadyRenderedMaps.length);
70
- });
71
-
72
- const observer = new IntersectionObserver((entries) => {
73
- entries.forEach((item) => {
74
- if (!item.isIntersecting) {
75
- return;
76
- }
77
- renderGeoloniaMap(item.target);
78
- observer.unobserve(item.target);
79
- });
80
- });
81
-
82
- const containers = document.querySelectorAll('.geolonia[data-lazy-loading="off"]');
83
- const lazyContainers = document.querySelectorAll('.geolonia:not([data-lazy-loading="off"])');
84
-
85
- window.geolonia =
86
- window.maplibregl =
87
- window.mapboxgl = // Embed API backward compatibility
88
- Object.assign(window.geolonia, maplibregl);
89
-
90
- // This is required for correct initialization! Don't delete!
91
- const { key } = parseApiKey(document);
92
- if (key === 'no-api-key') {
93
- console.error('[Geolonia] Missing API key.') // eslint-disable-line
94
- }
95
-
96
- window.geolonia.Map = GeoloniaMap;
97
- window.geolonia.simpleStyle = // backward compatibility
98
- window.geolonia.SimpleStyle =
99
- SimpleStyle;
100
- window.geolonia.Marker = GeoloniaMarker;
101
- window.geolonia.AmazonLocationServiceMapProvider = AmazonLocationServiceMapProvider;
102
- window.geolonia.embedVersion = pkg.version;
103
- window.geolonia.registerPlugin = (plugin) => {
104
- plugins.push(plugin);
105
- return void 0;
106
- };
107
-
108
- // render Map immediately
109
- for (let i = 0; i < containers.length; i++) {
110
- renderGeoloniaMap(containers[i]);
111
- }
112
-
113
- // set intersection observer
114
- for (let i = 0; i < lazyContainers.length; i++) {
115
- observer.observe(lazyContainers[i]);
116
- }
117
- } else {
118
- console.error( '[Geolonia] We are very sorry, but we can\'t display our map in iframe.' ) // eslint-disable-line
119
- }
@@ -1,77 +0,0 @@
1
- import urlParse from 'url-parse';
2
-
3
- function getParsedApiKey() {
4
- const geolonia = window.geolonia || {};
5
- if (geolonia._apiKey && geolonia._stage) {
6
- return {
7
- key: geolonia._apiKey,
8
- stage: geolonia._stage,
9
- };
10
- }
11
- return null;
12
- }
13
-
14
- function initApiKey(document) {
15
- if (typeof window.geolonia === 'undefined') {
16
- window.geolonia = {};
17
- }
18
-
19
- const scripts = document.currentScript ?
20
- [document.currentScript]
21
- :
22
- document.getElementsByTagName('script');
23
-
24
- const params = {
25
- key: 'no-api-key',
26
- stage: 'dev',
27
- };
28
-
29
- for (const script of scripts) {
30
- const { pathname, query } = urlParse(script.src);
31
- const q = new URLSearchParams(query.replace(/^\?/, ''));
32
-
33
- if (q.get('geolonia-api-key')) {
34
- params.key = q.get('geolonia-api-key') || 'YOUR-API-KEY';
35
-
36
- const res = pathname.match( /^\/(v[0-9.]+)\/embed/ );
37
- if (res && res[1]) {
38
- params.stage = res[1];
39
- }
40
-
41
- break;
42
- }
43
- }
44
-
45
- window.geolonia._apiKey ||= params.key;
46
- window.geolonia._stage ||= params.stage;
47
-
48
- return {
49
- key: window.geolonia._apiKey,
50
- stage: window.geolonia._stage,
51
- };
52
- }
53
-
54
- /**
55
- * Parses the API key and stage from the initial window.geolonia object, or from
56
- * the URL of the current script tag.
57
- *
58
- * This function will create `window.geolonia` if it doesn't already exist.
59
- * If window.geolonia._apiKey and window.geolonia._stage don't exist,
60
- * this function will set them to the detected values.
61
- * If window.geolonia._apiKey and window.geolonia._stage do exist,
62
- * this function will return them immediately.
63
- * If either window.geolonia._apiKey or window.geolonia._stage exist,
64
- * that value will override the detected value.
65
- *
66
- * The default values are: {key: 'no-api-key', stage: 'dev'}
67
- * @param {Document} document
68
- * @returns { key: string, stage: string }
69
- */
70
- export function parseApiKey(document) {
71
- const apiKey = getParsedApiKey();
72
- if (apiKey) {
73
- return apiKey;
74
- }
75
-
76
- return initApiKey(document);
77
- }
@@ -1,102 +0,0 @@
1
- import { parseApiKey } from './parse-api-key';
2
- import { JSDOM } from 'jsdom';
3
- import assert from 'assert';
4
-
5
- describe('parse api key from dom', () => {
6
- beforeEach(() => {
7
- delete window.geolonia;
8
- });
9
-
10
- it('should parse with geolonia flag', () => {
11
- const { document: mocDocument } = new JSDOM(`<html><body>
12
- <script src="https://external.example.com/?geolonia-api-key=abc"></script>
13
- </body></html>`).window;
14
-
15
- window.geolonia = {};
16
-
17
- const params = parseApiKey(mocDocument);
18
- assert.deepEqual('abc', params.key);
19
- assert.deepEqual('dev', params.stage);
20
- });
21
-
22
- it('should override API key and stage with pre-initialized window.geolonia', () => {
23
- const { document: mocDocument } = new JSDOM(`<html><body>
24
- <script src="https://external.example.com/?geolonia-api-key=abc"></script>
25
- </body></html>`).window;
26
-
27
- window.geolonia = {
28
- _stage: 'testStage',
29
- _apiKey: 'testApiKey',
30
- };
31
-
32
- const params = parseApiKey(mocDocument);
33
- assert.deepEqual('testApiKey', params.key);
34
- assert.deepEqual('testStage', params.stage);
35
- });
36
-
37
- it('should override only stage with pre-initialized window.geolonia', () => {
38
- const { document: mocDocument } = new JSDOM(`<html><body>
39
- <script src="https://external.example.com/?geolonia-api-key=abc"></script>
40
- </body></html>`).window;
41
-
42
- window.geolonia = {
43
- _stage: 'testStage',
44
- };
45
-
46
- const params = parseApiKey(mocDocument);
47
- assert.deepEqual('abc', params.key);
48
- assert.deepEqual('testStage', params.stage);
49
- });
50
-
51
- it('should parse with geolonia flag', () => {
52
- const { document: mocDocument } = new JSDOM(`<html><body>
53
- <script src="https://external.example.com/jquery.js"></script>
54
- <script src="https://external.example.com/?geolonia-api-key=def"></script>
55
- </body></html>`).window;
56
-
57
- window.geolonia = {};
58
-
59
- const params = parseApiKey(mocDocument);
60
- assert.deepEqual('def', params.key);
61
- assert.deepEqual('dev', params.stage);
62
- });
63
-
64
- it('should be "YOUR-API-KEY" and "dev"', () => {
65
- const { document: mocDocument } = new JSDOM(`<html><body>
66
- <script src="https://external.example.com/jquery.js"></script>
67
- <script type="text/javascript" src="https://api.geolonia.com/dev/embed?geolonia-api-key=YOUR-API-KEY"></script>
68
- </body></html>`).window;
69
-
70
- window.geolonia = {};
71
-
72
- const params = parseApiKey(mocDocument);
73
- assert.deepEqual('YOUR-API-KEY', params.key);
74
- assert.deepEqual('dev', params.stage);
75
- });
76
-
77
- it('should be "YOUR-API-KEY" and "v1"', () => {
78
- const { document: mocDocument } = new JSDOM(`<html><body>
79
- <script src="https://external.example.com/jquery.js"></script>
80
- <script type="text/javascript" src="https://api.geolonia.com/v1/embed?geolonia-api-key=YOUR-API-KEY"></script>
81
- </body></html>`).window;
82
-
83
- window.geolonia = {};
84
-
85
- const params = parseApiKey(mocDocument);
86
- assert.deepEqual('YOUR-API-KEY', params.key);
87
- assert.deepEqual('v1', params.stage);
88
- });
89
-
90
- it('should be "YOUR-API-KEY" and "v123.4"', () => {
91
- const { document: mocDocument } = new JSDOM(`<html><body>
92
- <script src="https://external.example.com/jquery.js"></script>
93
- <script type="text/javascript" src="https://api.geolonia.com/v123.4/embed?geolonia-api-key=YOUR-API-KEY"></script>
94
- </body></html>`).window;
95
-
96
- window.geolonia = {};
97
-
98
- const params = parseApiKey(mocDocument);
99
- assert.deepEqual('YOUR-API-KEY', params.key);
100
- assert.deepEqual('v123.4', params.stage);
101
- });
102
- });
@@ -1,116 +0,0 @@
1
- import 'whatwg-fetch';
2
- import { getContainer } from '../util';
3
- import { GeoloniaControl } from '../controls/geolonia-control';
4
-
5
- const AWS_SDK_URL = 'https://sdk.amazonaws.com/js/aws-sdk-2.775.0.min.js';
6
- const AMPLIFY_URL = 'https://unpkg.com/@aws-amplify/core@3.7.0/dist/aws-amplify-core.min.js';
7
- const STYLE_URL = 'https://geolonia.github.io/embed/docs/amzn-loc-style.json';
8
- export class AmazonLocationServiceMapProvider {
9
-
10
- constructor(awsconfig = {}) {
11
- const { cognitoIdentityPoolId, mapName = 'explore.map' } = awsconfig;
12
- if (!cognitoIdentityPoolId) {
13
- throw new Error('Invalid options. awsconfig.cognitoIdentityPoolId is required.');
14
- }
15
- this.cognitoIdentityPoolId = cognitoIdentityPoolId;
16
- this.mapName = mapName;
17
- }
18
-
19
- /**
20
- * Load AWS SDK et al. if not exists.
21
- * @param {{timeout}}
22
- * @returns Promise<{aws_amplify_core, AWS, geolonia}>
23
- */
24
- _loadAwsSdk({ timeout }) {
25
- const { aws_amplify_core, AWS, geolonia } = window;
26
- if (aws_amplify_core && AWS && geolonia) {
27
- return Promise.resolve({ aws_amplify_core, AWS, geolonia });
28
- } else {
29
- // Dynamic loading
30
- const AwsSdk = document.createElement('script');
31
- const Amplify = document.createElement('script');
32
- AwsSdk.setAttribute('src', AWS_SDK_URL);
33
- Amplify.setAttribute('src', AMPLIFY_URL);
34
- document.body.appendChild(AwsSdk);
35
- document.body.appendChild(Amplify);
36
-
37
- // Wait until ready
38
- return new Promise((resolve, reject) => {
39
- let isTimeout = false;
40
- setTimeout(() => { isTimeout = true; }, timeout);
41
- const timerId = setInterval(() => {
42
- const { aws_amplify_core, AWS, geolonia } = window;
43
- if (aws_amplify_core && AWS && geolonia) {
44
- clearInterval(timerId);
45
- return resolve({ aws_amplify_core, AWS, geolonia });
46
- } else if (isTimeout) {
47
- clearInterval(timerId);
48
- return reject(new Error('Failed to load the AWS SDK.'));
49
- }
50
- }, 50);
51
- });
52
- }
53
- }
54
-
55
- _fetchStyle(url) {
56
- return fetch(url).then((res) => res.json());
57
- }
58
-
59
- async initMap(options) {
60
- const { aws_amplify_core, AWS, geolonia } = await this._loadAwsSdk({ timeout: 10000 });
61
-
62
- // Sign with AWS SDK
63
- const { Signer } = aws_amplify_core;
64
- const region = this.cognitoIdentityPoolId.split(':')[0];
65
- const credentials = new AWS.CognitoIdentityCredentials({
66
- IdentityPoolId: this.cognitoIdentityPoolId,
67
- }, { region });
68
- await credentials.getPromise();
69
- const transformRequest = (url, resourceType) => {
70
- if (url.includes('amazonaws.com')) {
71
- // only sign AWS requests (with the signature as part of the query string)
72
- return {
73
- url: Signer.signUrl(url, {
74
- access_key: credentials.accessKeyId,
75
- secret_key: credentials.secretAccessKey,
76
- session_token: credentials.sessionToken,
77
- }),
78
- };
79
- }
80
-
81
- // Additional transformation
82
- if (typeof options.transformRequest === 'function') {
83
- return options.transformRequest(url, resourceType);
84
- } else {
85
- return { url };
86
- }
87
- };
88
-
89
- const mapOptions = {
90
- minZoom: 1, // no 0/0/0 tile
91
- ...options,
92
- transformRequest,
93
- };
94
-
95
- // override style if not specified
96
- const container = getContainer(options.container);
97
- if (container && !container.dataset.style) {
98
- const style = await this._fetchStyle(STYLE_URL);
99
- style.sources.omv.tiles = style.sources.omv.tiles.map((url) => url.replace('explore.map', this.mapName));
100
- mapOptions.style = style;
101
- }
102
-
103
- const map = new geolonia.Map(mapOptions);
104
- try {
105
- map._controls.forEach((control) => {
106
- if (control instanceof GeoloniaControl) {
107
- map.removeControl(control);
108
- }
109
- });
110
- } catch (error) {
111
- // nothing to do
112
- }
113
-
114
- return map;
115
- }
116
- }