@geolonia/embed 2.0.0-pre.0 → 2.0.0-pre.4
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/.eslintrc.js +1 -0
- package/.github/workflows/build.yml +9 -2
- package/README.md +7 -1
- package/dist/embed +1 -0
- package/dist/embed-chunks/1dad19808d6d2de0bb8c.js +1 -0
- package/dist/embed-chunks/635ae96b64eb1d5a7397.js +1 -0
- package/dist/embed-chunks/aeb2140c0187adbc1cd0.js +2 -0
- package/dist/embed-chunks/aeb2140c0187adbc1cd0.js.LICENSE.txt +6 -0
- package/dist/embed.js +1 -17
- package/package.json +10 -10
- package/src/embed.d.ts +8 -7
- package/src/embed.js +18 -7
- package/src/lib/geolonia-map.js +13 -3
- package/src/lib/util.js +18 -4
- package/src/lib/util.test.js +8 -1
- package/src/style.css +7 -0
- package/webpack.config.js +6 -3
package/package.json
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geolonia/embed",
|
|
3
|
-
"version": "2.0.0-pre.
|
|
3
|
+
"version": "2.0.0-pre.4",
|
|
4
4
|
"description": "Geolonia embed JS API",
|
|
5
5
|
"main": "dist/embed.js",
|
|
6
6
|
"types": "src/embed.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"start": "webpack
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"build:docs": "webpack -p --config ./docs/webpack.config.js",
|
|
12
|
-
"build:embed": "webpack -p --config ./webpack.config.js",
|
|
8
|
+
"start": "webpack serve --config ./docs/webpack.config.js",
|
|
9
|
+
"build": "webpack --mode production && cp ./dist/embed.js ./dist/embed && cp -r ./dist/* ./docs/",
|
|
10
|
+
"analyze": "ANALYZE=true webpack --mode production",
|
|
13
11
|
"format": "prettier-eslint \"./src/**/*.js\" --write",
|
|
14
12
|
"lint": "eslint \"src/**/*.js\"",
|
|
15
13
|
"test": "npm run lint && mocha ./src/**/*.test.js --recursive -r jsdom-global/register -r @babel/register",
|
|
@@ -31,6 +29,8 @@
|
|
|
31
29
|
"@babel/plugin-transform-classes": "^7.10.1",
|
|
32
30
|
"@babel/preset-env": "^7.10.2",
|
|
33
31
|
"@babel/register": "^7.10.1",
|
|
32
|
+
"@geolonia/eslint-config": "^0.0.1",
|
|
33
|
+
"babel-eslint": "^10.1.0",
|
|
34
34
|
"babel-loader": "^8.1.0",
|
|
35
35
|
"css-loader": "^2.1.0",
|
|
36
36
|
"eslint": "^7.16.0",
|
|
@@ -43,12 +43,12 @@
|
|
|
43
43
|
"puppeteer": "^9.1.1",
|
|
44
44
|
"style-loader": "^0.23.1",
|
|
45
45
|
"svg-inline-loader": "^0.8.2",
|
|
46
|
-
"webpack": "^
|
|
47
|
-
"webpack-
|
|
48
|
-
"webpack-
|
|
46
|
+
"webpack": "^5.59.1",
|
|
47
|
+
"webpack-bundle-analyzer": "^4.5.0",
|
|
48
|
+
"webpack-cli": "^4.9.1",
|
|
49
|
+
"webpack-dev-server": "^4.3.1"
|
|
49
50
|
},
|
|
50
51
|
"dependencies": {
|
|
51
|
-
"@geolonia/eslint-config": "https://github.com/geolonia/eslint-config",
|
|
52
52
|
"@geolonia/mbgl-geolonia-control": "^0.4.0",
|
|
53
53
|
"@geolonia/mbgl-gesture-handling": "^1.0.15",
|
|
54
54
|
"@mapbox/geojson-extent": "^0.3.2",
|
package/src/embed.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import * as maplibregl from 'maplibre-gl';
|
|
2
2
|
|
|
3
|
-
export
|
|
4
|
-
export
|
|
3
|
+
export class Map extends maplibregl.Map {}
|
|
4
|
+
export class Marker extends maplibregl.Marker {}
|
|
5
|
+
|
|
5
6
|
export type EmbedAttributes = {
|
|
6
7
|
lat: string;
|
|
7
8
|
lng: string;
|
|
@@ -40,11 +41,11 @@ export type EmbedPlugin<PluginAttributes extends { [otherKey: string]: string }
|
|
|
40
41
|
declare global {
|
|
41
42
|
interface Window {
|
|
42
43
|
geolonia: {
|
|
43
|
-
accessToken: string
|
|
44
|
-
baseApiUrl: string
|
|
45
|
-
Map: Map
|
|
46
|
-
Marker: Marker
|
|
47
|
-
registerPlugin: (embedPlugin: EmbedPlugin) => void
|
|
44
|
+
accessToken: string;
|
|
45
|
+
baseApiUrl: string;
|
|
46
|
+
Map: typeof Map;
|
|
47
|
+
Marker: typeof Marker;
|
|
48
|
+
registerPlugin: (embedPlugin: EmbedPlugin) => void;
|
|
48
49
|
}
|
|
49
50
|
}
|
|
50
51
|
}
|
package/src/embed.js
CHANGED
|
@@ -12,12 +12,13 @@ import { AmazonLocationServiceMapProvider } from './lib/providers/amazon';
|
|
|
12
12
|
import * as util from './lib/util';
|
|
13
13
|
import parseAtts from './lib/parse-atts';
|
|
14
14
|
import parseApiKey from './lib/parse-api-key';
|
|
15
|
-
import
|
|
15
|
+
import pkg from '../package.json';
|
|
16
16
|
|
|
17
17
|
if ( util.checkPermission() ) {
|
|
18
18
|
let isDOMContentLoaded = false;
|
|
19
19
|
const alreadyRenderedMaps = [];
|
|
20
20
|
const plugins = [];
|
|
21
|
+
const isRemoved = Symbol('map-is-removed');
|
|
21
22
|
|
|
22
23
|
/**
|
|
23
24
|
*
|
|
@@ -26,9 +27,14 @@ if ( util.checkPermission() ) {
|
|
|
26
27
|
const renderGeoloniaMap = (target) => {
|
|
27
28
|
const map = new GeoloniaMap(target);
|
|
28
29
|
|
|
30
|
+
// detect if the map removed manually
|
|
31
|
+
map.on('remove', () => {
|
|
32
|
+
map[isRemoved] = true;
|
|
33
|
+
});
|
|
34
|
+
|
|
29
35
|
// plugin
|
|
30
36
|
const atts = parseAtts(target);
|
|
31
|
-
if (isDOMContentLoaded) {
|
|
37
|
+
if (isDOMContentLoaded && !map[isRemoved]) {
|
|
32
38
|
plugins.forEach((plugin) => plugin(map, target, atts));
|
|
33
39
|
} else {
|
|
34
40
|
alreadyRenderedMaps.push({ map, target: target, atts });
|
|
@@ -37,9 +43,11 @@ if ( util.checkPermission() ) {
|
|
|
37
43
|
|
|
38
44
|
document.addEventListener('DOMContentLoaded', () => {
|
|
39
45
|
isDOMContentLoaded = true;
|
|
40
|
-
alreadyRenderedMaps.forEach(({ map, target, atts }) =>
|
|
41
|
-
|
|
42
|
-
|
|
46
|
+
alreadyRenderedMaps.forEach(({ map, target, atts }) => {
|
|
47
|
+
if (!map[isRemoved]) {
|
|
48
|
+
plugins.forEach((plugin) => plugin(map, target, atts));
|
|
49
|
+
}
|
|
50
|
+
});
|
|
43
51
|
// clear
|
|
44
52
|
alreadyRenderedMaps.splice(0, alreadyRenderedMaps.length);
|
|
45
53
|
});
|
|
@@ -57,7 +65,10 @@ if ( util.checkPermission() ) {
|
|
|
57
65
|
const containers = document.querySelectorAll('.geolonia[data-lazy-loading="off"]');
|
|
58
66
|
const lazyContainers = document.querySelectorAll('.geolonia:not([data-lazy-loading="off"])');
|
|
59
67
|
|
|
60
|
-
window.geolonia =
|
|
68
|
+
window.geolonia =
|
|
69
|
+
window.maplibregl =
|
|
70
|
+
window.mapboxgl = // Embed API backward compatibility
|
|
71
|
+
maplibregl;
|
|
61
72
|
|
|
62
73
|
// This is required for correct initialization! Don't delete!
|
|
63
74
|
const { key } = parseApiKey(document);
|
|
@@ -68,7 +79,7 @@ if ( util.checkPermission() ) {
|
|
|
68
79
|
window.geolonia.Map = GeoloniaMap;
|
|
69
80
|
window.geolonia.Marker = GeoloniaMarker;
|
|
70
81
|
window.geolonia.AmazonLocationServiceMapProvider = AmazonLocationServiceMapProvider;
|
|
71
|
-
window.geolonia.embedVersion = version;
|
|
82
|
+
window.geolonia.embedVersion = pkg.version;
|
|
72
83
|
window.geolonia.registerPlugin = (plugin) => {
|
|
73
84
|
plugins.push(plugin);
|
|
74
85
|
return void 0;
|
package/src/lib/geolonia-map.js
CHANGED
|
@@ -3,8 +3,6 @@ import 'promise-polyfill/src/polyfill';
|
|
|
3
3
|
import maplibregl from 'maplibre-gl';
|
|
4
4
|
import GeoloniaControl from '@geolonia/mbgl-geolonia-control';
|
|
5
5
|
import GestureHandling from '@geolonia/mbgl-gesture-handling';
|
|
6
|
-
import SimpleStyle from './simplestyle';
|
|
7
|
-
import SimpleStyleVector from './simplestyle-vector';
|
|
8
6
|
import parseAtts from './parse-atts';
|
|
9
7
|
|
|
10
8
|
import * as util from './util';
|
|
@@ -192,7 +190,7 @@ export default class GeoloniaMap extends maplibregl.Map {
|
|
|
192
190
|
}
|
|
193
191
|
});
|
|
194
192
|
|
|
195
|
-
map.on('styledata', (event) => {
|
|
193
|
+
map.on('styledata', async (event) => {
|
|
196
194
|
const map = event.target;
|
|
197
195
|
|
|
198
196
|
if (!this.__styleExtensionLoadRequired) {
|
|
@@ -202,11 +200,13 @@ export default class GeoloniaMap extends maplibregl.Map {
|
|
|
202
200
|
|
|
203
201
|
if (atts.simpleVector) {
|
|
204
202
|
const simpleVectorAttributeValue = util.parseSimpleVector(atts.simpleVector);
|
|
203
|
+
const { default: SimpleStyleVector } = await import('./simplestyle-vector');
|
|
205
204
|
new SimpleStyleVector(simpleVectorAttributeValue).addTo(map);
|
|
206
205
|
}
|
|
207
206
|
|
|
208
207
|
if (atts.geojson) {
|
|
209
208
|
const el = isCssSelector(atts.geojson);
|
|
209
|
+
const { default: SimpleStyle } = await import('./simplestyle');
|
|
210
210
|
if (el) {
|
|
211
211
|
const json = JSON.parse(el.textContent);
|
|
212
212
|
new SimpleStyle(json, {
|
|
@@ -241,6 +241,16 @@ export default class GeoloniaMap extends maplibregl.Map {
|
|
|
241
241
|
}
|
|
242
242
|
});
|
|
243
243
|
|
|
244
|
+
// handle Geolonia Server errors
|
|
245
|
+
map.on('error', async (error) => {
|
|
246
|
+
if (
|
|
247
|
+
error.error &&
|
|
248
|
+
error.error.status === 402
|
|
249
|
+
) {
|
|
250
|
+
util.handleRestrictedMode(map);
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
|
|
244
254
|
container.geoloniaMap = map;
|
|
245
255
|
|
|
246
256
|
return map;
|
package/src/lib/util.js
CHANGED
|
@@ -130,17 +130,21 @@ export function isDomElement(o) {
|
|
|
130
130
|
* Possibility args are HTMLElement or CSS selector or object that has container property.
|
|
131
131
|
*
|
|
132
132
|
* @param {*} arg
|
|
133
|
+
* @return {HTMLElement | false}
|
|
133
134
|
*/
|
|
134
135
|
export function getContainer(arg) {
|
|
135
136
|
if (isDomElement(arg)) {
|
|
136
137
|
return arg;
|
|
137
|
-
} else if (typeof arg === 'string'
|
|
138
|
-
|
|
138
|
+
} else if (typeof arg === 'string') {
|
|
139
|
+
const el = document.querySelector(arg) || document.getElementById(arg);
|
|
140
|
+
return el || false;
|
|
141
|
+
|
|
139
142
|
} else if (arg.container) {
|
|
140
143
|
if (isDomElement(arg.container)) {
|
|
141
144
|
return arg.container;
|
|
142
|
-
} else if (
|
|
143
|
-
|
|
145
|
+
} else if (typeof arg.container === 'string') {
|
|
146
|
+
const el = document.querySelector(arg.container) || document.getElementById(arg.container);
|
|
147
|
+
return el || false;
|
|
144
148
|
}
|
|
145
149
|
}
|
|
146
150
|
|
|
@@ -260,3 +264,13 @@ export const parseSimpleVector = (attributeValue) => {
|
|
|
260
264
|
return `geolonia://tiles/custom/${attributeValue}`;
|
|
261
265
|
}
|
|
262
266
|
};
|
|
267
|
+
|
|
268
|
+
export const handleRestrictedMode = (map) => {
|
|
269
|
+
if (!map._geolonia_restricted_mode_handled) {
|
|
270
|
+
map._geolonia_restricted_mode_handled = true;
|
|
271
|
+
const container = map.getContainer();
|
|
272
|
+
map.remove();
|
|
273
|
+
container.innerHTML = '';
|
|
274
|
+
container.classList.add('geolonia__restricted-mode-image-container');
|
|
275
|
+
}
|
|
276
|
+
};
|
package/src/lib/util.test.js
CHANGED
|
@@ -67,12 +67,19 @@ describe('Tests for util.js', () => {
|
|
|
67
67
|
const params = { container: el };
|
|
68
68
|
assert.deepEqual(el, util.getContainer(params));
|
|
69
69
|
|
|
70
|
+
// specify as a selector
|
|
70
71
|
assert.deepEqual(el, util.getContainer('#test-element'));
|
|
71
72
|
assert.deepEqual(el, util.getContainer({ container: '#test-element' }));
|
|
72
73
|
|
|
74
|
+
// specify as an id attribute value
|
|
75
|
+
assert.deepEqual(el, util.getContainer('test-element'));
|
|
76
|
+
assert.deepEqual(el, util.getContainer({ container: 'test-element' }));
|
|
77
|
+
|
|
78
|
+
// negative cases
|
|
73
79
|
assert.deepEqual(false, util.getContainer('#fail-element'));
|
|
74
80
|
assert.deepEqual(false, util.getContainer({ container: '#fail-element' }));
|
|
75
|
-
|
|
81
|
+
assert.deepEqual(false, util.getContainer('fail-element'));
|
|
82
|
+
assert.deepEqual(false, util.getContainer({ container: 'fail-element' })); });
|
|
76
83
|
|
|
77
84
|
it('should merge legacyoptions into options as expected.', () => {
|
|
78
85
|
const dom = new JSDOM(`<html><body>
|
package/src/style.css
CHANGED
|
@@ -102,3 +102,10 @@
|
|
|
102
102
|
opacity: 0.5;
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
|
+
|
|
106
|
+
/* Style for restricted mode */
|
|
107
|
+
.geolonia__restricted-mode-image-container {
|
|
108
|
+
background-image: url("https://geolonia.github.io/embed/restricted.jpg");
|
|
109
|
+
background-position: center center;
|
|
110
|
+
background-size: cover;
|
|
111
|
+
}
|
package/webpack.config.js
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
const path = require('path')
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
|
|
2
3
|
|
|
3
4
|
module.exports = {
|
|
4
5
|
entry: './src/embed.js',
|
|
5
6
|
output: {
|
|
6
7
|
path: path.join(__dirname, 'dist'),
|
|
7
8
|
filename: 'embed.js',
|
|
9
|
+
chunkFilename: path.join('embed-chunks', '[chunkhash].js'),
|
|
10
|
+
clean: true,
|
|
8
11
|
},
|
|
9
|
-
|
|
12
|
+
plugins: process.env.ANALYZE === 'true' ? [new BundleAnalyzerPlugin()] : [],
|
|
10
13
|
module: {
|
|
11
14
|
rules: [
|
|
12
15
|
{
|
|
@@ -28,4 +31,4 @@ module.exports = {
|
|
|
28
31
|
},
|
|
29
32
|
],
|
|
30
33
|
},
|
|
31
|
-
}
|
|
34
|
+
};
|