@geolonia/embed 6.0.0 → 6.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.
- package/CHANGELOG.md +24 -0
- package/README.md +54 -6
- package/dist/embed +1 -1
- package/dist/embed-chunks/1b101382794d13cd280c.js +2 -0
- package/dist/embed-chunks/856a68e16ab05c382e6f.js +2 -0
- package/dist/embed-chunks/9a1056aa012055998dc3.js +1 -0
- package/dist/embed-chunks/{5f80e0235f4b38778d0d.js → bf6d08239720e38dacd6.js} +1 -1
- package/dist/embed-core.js +1 -1
- package/dist/embed.js +1 -1
- package/dist/lib/util.d.ts +0 -5
- package/dist/types.d.ts +145 -7
- package/dist/version.d.ts +1 -1
- package/e2e/data-driven.spec.ts +9 -2
- package/e2e/iframe.spec.ts +37 -22
- package/e2e/mask.spec.ts +75 -0
- package/e2e/projection-raster-tiles.spec.ts +71 -0
- package/package.json +2 -2
- package/pnpm-workspace.yaml +6 -0
- package/src/lib/parse-atts.test.ts +531 -4
- package/src/lib/parse-atts.ts +261 -25
- package/src/lib/render.ts +99 -106
- package/src/lib/util.ts +0 -76
- package/src/types.ts +145 -7
- package/src/version.ts +1 -1
- package/dist/embed-chunks/9734b8bdf9fe84261b54.js +0 -2
- package/dist/embed-chunks/ae36f6b8e34f85ae3045.js +0 -2
- package/dist/embed-chunks/c83bb7c9a4a7ed9d5b23.js +0 -1
- /package/dist/embed-chunks/{9734b8bdf9fe84261b54.js.LICENSE.txt → 1b101382794d13cd280c.js.LICENSE.txt} +0 -0
- /package/dist/embed-chunks/{ae36f6b8e34f85ae3045.js.LICENSE.txt → 856a68e16ab05c382e6f.js.LICENSE.txt} +0 -0
package/src/lib/parse-atts.ts
CHANGED
|
@@ -7,8 +7,14 @@ import {
|
|
|
7
7
|
keyring,
|
|
8
8
|
type ChoroplethOptions,
|
|
9
9
|
type CircleOptions,
|
|
10
|
+
type ColorMap,
|
|
11
|
+
type ExtrusionColorOptions,
|
|
12
|
+
type ExtrusionHeightOptions,
|
|
13
|
+
type ExtrusionOptions,
|
|
10
14
|
type GeoloniaMapOptions,
|
|
11
15
|
type HeatmapOptions,
|
|
16
|
+
type MaskOptions,
|
|
17
|
+
type RasterTilesOptions,
|
|
12
18
|
} from '@geolonia/maps-core';
|
|
13
19
|
import type { EmbedAttributes } from '../types';
|
|
14
20
|
|
|
@@ -35,17 +41,6 @@ export default (
|
|
|
35
41
|
(container as unknown as { dataset: DOMStringMap }).dataset = {};
|
|
36
42
|
}
|
|
37
43
|
|
|
38
|
-
let lang: string;
|
|
39
|
-
if (container.dataset.lang && container.dataset.lang === 'auto') {
|
|
40
|
-
lang = getLang();
|
|
41
|
-
} else if (container.dataset.lang && container.dataset.lang === 'ja') {
|
|
42
|
-
lang = 'ja';
|
|
43
|
-
} else if (container.dataset.lang && container.dataset.lang !== 'ja') {
|
|
44
|
-
lang = 'en';
|
|
45
|
-
} else {
|
|
46
|
-
lang = getLang();
|
|
47
|
-
}
|
|
48
|
-
|
|
49
44
|
// Check if using Geolonia style or external style
|
|
50
45
|
const style = container.dataset.style || 'geolonia/basic-v2';
|
|
51
46
|
keyring.isGeoloniaStyle = keyring.isGeoloniaStyleCheck(style);
|
|
@@ -73,7 +68,10 @@ export default (
|
|
|
73
68
|
cluster: 'on',
|
|
74
69
|
clusterColor: '#ff0000',
|
|
75
70
|
style: 'geolonia/basic-v2',
|
|
76
|
-
lang
|
|
71
|
+
// `data-lang` が指定されていれば、末尾の `...container.dataset` がその生の値で
|
|
72
|
+
// 上書きし、ja / en への解決は maps-core が行う。この値が残るのは
|
|
73
|
+
// `data-lang` が未指定のときだけ。
|
|
74
|
+
lang: getLang(),
|
|
77
75
|
plugin: 'off',
|
|
78
76
|
key: keyring.apiKey,
|
|
79
77
|
apiUrl: `https://api.geolonia.com/${keyring.stage}`,
|
|
@@ -122,8 +120,13 @@ const isCssSelector = (value: string): Element | false => {
|
|
|
122
120
|
|
|
123
121
|
/**
|
|
124
122
|
* Resolve a `data-geojson` value into what maps-core's SimpleStyle accepts:
|
|
125
|
-
* a URL
|
|
126
|
-
*
|
|
123
|
+
* a URL string, or the parsed content of the inline element a CSS selector
|
|
124
|
+
* points at. The content is not validated; it must be a FeatureCollection.
|
|
125
|
+
*
|
|
126
|
+
* Any other value is returned unchanged, and maps-core usually cannot draw it.
|
|
127
|
+
* Throws when the element's content is not valid JSON. See
|
|
128
|
+
* https://github.com/geolonia/embed/issues/570 for what happens with
|
|
129
|
+
* unsupported values.
|
|
127
130
|
*/
|
|
128
131
|
const resolveGeojson = (value: string): string | FeatureCollection => {
|
|
129
132
|
const el = isCssSelector(value);
|
|
@@ -134,25 +137,87 @@ const resolveGeojson = (value: string): string | FeatureCollection => {
|
|
|
134
137
|
};
|
|
135
138
|
|
|
136
139
|
/**
|
|
137
|
-
* Split a comma-separated list
|
|
138
|
-
*
|
|
140
|
+
* Split a comma-separated list. Commas inside parentheses (`rgb(0, 0, 0)`) are
|
|
141
|
+
* part of the item, not separators. Items are trimmed and empty ones dropped.
|
|
139
142
|
*/
|
|
140
|
-
const
|
|
141
|
-
const
|
|
143
|
+
const splitList = (value: string): string[] => {
|
|
144
|
+
const items: string[] = [];
|
|
142
145
|
let depth = 0;
|
|
143
146
|
let current = '';
|
|
144
147
|
for (const char of value) {
|
|
145
148
|
if (char === '(') depth++;
|
|
146
149
|
if (char === ')') depth = Math.max(0, depth - 1);
|
|
147
150
|
if (char === ',' && depth === 0) {
|
|
148
|
-
|
|
151
|
+
items.push(current);
|
|
149
152
|
current = '';
|
|
150
153
|
} else {
|
|
151
154
|
current += char;
|
|
152
155
|
}
|
|
153
156
|
}
|
|
154
|
-
|
|
155
|
-
return
|
|
157
|
+
items.push(current);
|
|
158
|
+
return items.map((c) => c.trim()).filter((c) => c !== '');
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
/** Split a comma-separated list of CSS colors (see {@link splitList}). */
|
|
162
|
+
const parseColors = (value: string): string[] => splitList(value);
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Index of the last `:` outside parentheses, or -1. CSS colors have no `:`
|
|
166
|
+
* outside parentheses, so the last one separates the value from the color and
|
|
167
|
+
* a value may itself contain `:` (`10:30:#ff0000`).
|
|
168
|
+
*/
|
|
169
|
+
const lastPairSeparator = (item: string): number => {
|
|
170
|
+
let depth = 0;
|
|
171
|
+
let index = -1;
|
|
172
|
+
for (let i = 0; i < item.length; i++) {
|
|
173
|
+
const char = item[i];
|
|
174
|
+
if (char === '(') depth++;
|
|
175
|
+
if (char === ')') depth = Math.max(0, depth - 1);
|
|
176
|
+
if (char === ':' && depth === 0) index = i;
|
|
177
|
+
}
|
|
178
|
+
return index;
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Read a `-colors` attribute as either a list of colors or a value-to-color
|
|
183
|
+
* map (maps-core's `ColorMap`).
|
|
184
|
+
*
|
|
185
|
+
* - When no item has a `value:color` pair, it is a list of colors, as before.
|
|
186
|
+
* - When at least one item has a pair, it is a map. Items without a pair and
|
|
187
|
+
* pairs with an empty color are ignored with a `console.warn`. The value and
|
|
188
|
+
* the color are trimmed, so `: #fff` maps the empty value. A later pair for
|
|
189
|
+
* the same value wins.
|
|
190
|
+
*
|
|
191
|
+
* Returns `undefined` when nothing usable is left, so maps-core uses its
|
|
192
|
+
* default colors (an empty map would paint every feature in the fallback
|
|
193
|
+
* color instead).
|
|
194
|
+
*/
|
|
195
|
+
const parseColorsOrMap = (
|
|
196
|
+
value: string,
|
|
197
|
+
attribute: string,
|
|
198
|
+
): string[] | ColorMap | undefined => {
|
|
199
|
+
const items = splitList(value);
|
|
200
|
+
if (!items.some((item) => lastPairSeparator(item) !== -1)) {
|
|
201
|
+
return items.length > 0 ? items : undefined;
|
|
202
|
+
}
|
|
203
|
+
// On `{}`, assigning to `__proto__` goes to the prototype setter (which drops
|
|
204
|
+
// a string) instead of creating a key, so a `__proto__` value would be lost.
|
|
205
|
+
const map: ColorMap = Object.create(null) as ColorMap;
|
|
206
|
+
let size = 0;
|
|
207
|
+
for (const item of items) {
|
|
208
|
+
const index = lastPairSeparator(item);
|
|
209
|
+
const color = index === -1 ? '' : item.slice(index + 1).trim();
|
|
210
|
+
if (color === '') {
|
|
211
|
+
// eslint-disable-next-line no-console
|
|
212
|
+
console.warn(
|
|
213
|
+
`[Geolonia] ${attribute}: ignored "${item}", which is not a "value:color" pair with a color.`,
|
|
214
|
+
);
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
map[item.slice(0, index).trim()] = color;
|
|
218
|
+
size++;
|
|
219
|
+
}
|
|
220
|
+
return size > 0 ? map : undefined;
|
|
156
221
|
};
|
|
157
222
|
|
|
158
223
|
/**
|
|
@@ -184,8 +249,14 @@ const dataDrivenOptions = (
|
|
|
184
249
|
|
|
185
250
|
if (atts.choropleth) {
|
|
186
251
|
const choropleth: ChoroplethOptions = { property: String(atts.choropleth) };
|
|
187
|
-
|
|
188
|
-
|
|
252
|
+
const colors = atts.choroplethColors
|
|
253
|
+
? parseColorsOrMap(
|
|
254
|
+
String(atts.choroplethColors),
|
|
255
|
+
'data-choropleth-colors',
|
|
256
|
+
)
|
|
257
|
+
: undefined;
|
|
258
|
+
if (colors) {
|
|
259
|
+
choropleth.colors = colors;
|
|
189
260
|
}
|
|
190
261
|
const range = atts.choroplethRange
|
|
191
262
|
? parseRange(String(atts.choroplethRange))
|
|
@@ -217,8 +288,11 @@ const dataDrivenOptions = (
|
|
|
217
288
|
if (atts.circleColor) {
|
|
218
289
|
circle.color = String(atts.circleColor);
|
|
219
290
|
}
|
|
220
|
-
|
|
221
|
-
|
|
291
|
+
const colors = atts.circleColors
|
|
292
|
+
? parseColorsOrMap(String(atts.circleColors), 'data-circle-colors')
|
|
293
|
+
: undefined;
|
|
294
|
+
if (colors) {
|
|
295
|
+
circle.colors = colors;
|
|
222
296
|
}
|
|
223
297
|
options.circle = circle;
|
|
224
298
|
}
|
|
@@ -226,6 +300,150 @@ const dataDrivenOptions = (
|
|
|
226
300
|
return options;
|
|
227
301
|
};
|
|
228
302
|
|
|
303
|
+
/**
|
|
304
|
+
* Read a numeric attribute, or `undefined` when it is empty or not a finite
|
|
305
|
+
* number. `Number('')` is `0`, so empty strings are checked first.
|
|
306
|
+
*/
|
|
307
|
+
const finiteNumber = (value: unknown): number | undefined => {
|
|
308
|
+
if (value === undefined || String(value).trim() === '') {
|
|
309
|
+
return undefined;
|
|
310
|
+
}
|
|
311
|
+
const n = Number(value);
|
|
312
|
+
return Number.isFinite(n) ? n : undefined;
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Convert `data-raster-tiles` and its `-attribution` / `-opacity` / `-size` /
|
|
317
|
+
* `-min-zoom` / `-max-zoom` attributes into maps-core's `rasterTiles` option.
|
|
318
|
+
* The other attributes only apply when `data-raster-tiles` is present.
|
|
319
|
+
* Values maps-core would reject (a URL without `{z}`/`{x}`/`{y}`, an opacity
|
|
320
|
+
* outside 0 to 1) are passed through; maps-core warns about or clamps them.
|
|
321
|
+
*/
|
|
322
|
+
const rasterTilesOption = (
|
|
323
|
+
atts: EmbedAttributes,
|
|
324
|
+
): RasterTilesOptions | undefined => {
|
|
325
|
+
const url = String(atts.rasterTiles ?? '').trim();
|
|
326
|
+
if (!url) {
|
|
327
|
+
return undefined;
|
|
328
|
+
}
|
|
329
|
+
const rasterTiles: RasterTilesOptions = { url };
|
|
330
|
+
const attribution = String(atts.rasterTilesAttribution ?? '').trim();
|
|
331
|
+
if (attribution) {
|
|
332
|
+
rasterTiles.attribution = attribution;
|
|
333
|
+
}
|
|
334
|
+
const opacity = finiteNumber(atts.rasterTilesOpacity);
|
|
335
|
+
if (opacity !== undefined) {
|
|
336
|
+
rasterTiles.opacity = opacity;
|
|
337
|
+
}
|
|
338
|
+
const tileSize = finiteNumber(atts.rasterTilesSize);
|
|
339
|
+
if (tileSize !== undefined) {
|
|
340
|
+
rasterTiles.tileSize = tileSize;
|
|
341
|
+
}
|
|
342
|
+
const minZoom = finiteNumber(atts.rasterTilesMinZoom);
|
|
343
|
+
if (minZoom !== undefined) {
|
|
344
|
+
rasterTiles.minZoom = minZoom;
|
|
345
|
+
}
|
|
346
|
+
const maxZoom = finiteNumber(atts.rasterTilesMaxZoom);
|
|
347
|
+
if (maxZoom !== undefined) {
|
|
348
|
+
rasterTiles.maxZoom = maxZoom;
|
|
349
|
+
}
|
|
350
|
+
return rasterTiles;
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
/** Read a string attribute trimmed, or `undefined` when it is empty. */
|
|
354
|
+
const trimmed = (value: unknown): string | undefined => {
|
|
355
|
+
const text = String(value ?? '').trim();
|
|
356
|
+
return text === '' ? undefined : text;
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Convert `data-extrusion` and its `-max-height` / `-height-scale` /
|
|
361
|
+
* `-height-range` / `-color` / `-colors` / `-color-range` attributes into
|
|
362
|
+
* maps-core's `extrusion` option. The other attributes only apply when
|
|
363
|
+
* `data-extrusion` is present. Values that cannot be read (not a finite number,
|
|
364
|
+
* not a `min,max` range) are left out so maps-core uses its defaults.
|
|
365
|
+
*/
|
|
366
|
+
const extrusionOption = (
|
|
367
|
+
atts: EmbedAttributes,
|
|
368
|
+
): string | ExtrusionOptions | undefined => {
|
|
369
|
+
const property = trimmed(atts.extrusion);
|
|
370
|
+
if (!property) {
|
|
371
|
+
return undefined;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const height: ExtrusionHeightOptions = { property };
|
|
375
|
+
const maxHeight = finiteNumber(atts.extrusionMaxHeight);
|
|
376
|
+
if (maxHeight !== undefined) {
|
|
377
|
+
height.maxHeight = maxHeight;
|
|
378
|
+
}
|
|
379
|
+
const heightScale = finiteNumber(atts.extrusionHeightScale);
|
|
380
|
+
if (heightScale !== undefined) {
|
|
381
|
+
height.heightScale = heightScale;
|
|
382
|
+
}
|
|
383
|
+
const heightRange = atts.extrusionHeightRange
|
|
384
|
+
? parseRange(String(atts.extrusionHeightRange))
|
|
385
|
+
: undefined;
|
|
386
|
+
if (heightRange) {
|
|
387
|
+
height.range = heightRange;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// 色のプロパティを省略すると、maps-core は高さと同じプロパティで色分けする。
|
|
391
|
+
const color: ExtrusionColorOptions = {
|
|
392
|
+
property: trimmed(atts.extrusionColor) ?? property,
|
|
393
|
+
};
|
|
394
|
+
const colors = atts.extrusionColors
|
|
395
|
+
? parseColorsOrMap(String(atts.extrusionColors), 'data-extrusion-colors')
|
|
396
|
+
: undefined;
|
|
397
|
+
if (colors) {
|
|
398
|
+
color.colors = colors;
|
|
399
|
+
}
|
|
400
|
+
const colorRange = atts.extrusionColorRange
|
|
401
|
+
? parseRange(String(atts.extrusionColorRange))
|
|
402
|
+
: undefined;
|
|
403
|
+
if (colorRange) {
|
|
404
|
+
color.range = colorRange;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
if (Object.keys(height).length === 1 && Object.keys(color).length === 1) {
|
|
408
|
+
return color.property === property
|
|
409
|
+
? property
|
|
410
|
+
: { height: property, color: color.property };
|
|
411
|
+
}
|
|
412
|
+
return { height, color };
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Convert `data-mask` and its `-color` / `-opacity` / `-outline` attributes
|
|
417
|
+
* into maps-core's `mask` option. The other attributes only apply when
|
|
418
|
+
* `data-mask` is present. `data-mask` is passed as is (trimmed); maps-core
|
|
419
|
+
* reports a value that is not a URL with `console.error` and adds no mask.
|
|
420
|
+
*/
|
|
421
|
+
const maskOption = (
|
|
422
|
+
atts: EmbedAttributes,
|
|
423
|
+
): string | MaskOptions | undefined => {
|
|
424
|
+
const geojson = trimmed(atts.mask);
|
|
425
|
+
if (!geojson) {
|
|
426
|
+
return undefined;
|
|
427
|
+
}
|
|
428
|
+
const mask: MaskOptions = { geojson };
|
|
429
|
+
const color = trimmed(atts.maskColor);
|
|
430
|
+
if (color) {
|
|
431
|
+
mask.color = color;
|
|
432
|
+
}
|
|
433
|
+
const opacity = finiteNumber(atts.maskOpacity);
|
|
434
|
+
if (opacity !== undefined) {
|
|
435
|
+
mask.opacity = opacity;
|
|
436
|
+
}
|
|
437
|
+
if (
|
|
438
|
+
String(atts.maskOutline ?? '')
|
|
439
|
+
.trim()
|
|
440
|
+
.toLowerCase() === 'on'
|
|
441
|
+
) {
|
|
442
|
+
mask.outline = true;
|
|
443
|
+
}
|
|
444
|
+
return Object.keys(mask).length === 1 ? geojson : mask;
|
|
445
|
+
};
|
|
446
|
+
|
|
229
447
|
/**
|
|
230
448
|
* Convert normalised {@link EmbedAttributes} into a typed `GeoloniaMapOptions`
|
|
231
449
|
* for maps-core's `GeoloniaMap` constructor.
|
|
@@ -298,6 +516,24 @@ export const attsToOptions = (
|
|
|
298
516
|
}
|
|
299
517
|
Object.assign(options, dataDrivenOptions(atts));
|
|
300
518
|
|
|
519
|
+
// `globe` / `mercator` 以外の値は maps-core が console.error に出して無視する。
|
|
520
|
+
const projection = String(atts.projection ?? '').trim();
|
|
521
|
+
if (projection) {
|
|
522
|
+
options.projection = projection as GeoloniaMapOptions['projection'];
|
|
523
|
+
}
|
|
524
|
+
const rasterTiles = rasterTilesOption(atts);
|
|
525
|
+
if (rasterTiles) {
|
|
526
|
+
options.rasterTiles = rasterTiles;
|
|
527
|
+
}
|
|
528
|
+
const extrusion = extrusionOption(atts);
|
|
529
|
+
if (extrusion) {
|
|
530
|
+
options.extrusion = extrusion;
|
|
531
|
+
}
|
|
532
|
+
const mask = maskOption(atts);
|
|
533
|
+
if (mask) {
|
|
534
|
+
options.mask = mask;
|
|
535
|
+
}
|
|
536
|
+
|
|
301
537
|
if (
|
|
302
538
|
atts.minZoom !== '' &&
|
|
303
539
|
(Number(atts.minZoom) === 0 || Number(atts.minZoom))
|
package/src/lib/render.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import 'maplibre-gl/dist/maplibre-gl.css';
|
|
2
2
|
import '../style.css';
|
|
3
3
|
import { GeoloniaMap, keyring } from '@geolonia/maps-core';
|
|
4
|
-
import {
|
|
4
|
+
import { parseScriptTagApiKey } from './util';
|
|
5
5
|
import parseAtts, { attsToOptions } from './parse-atts';
|
|
6
6
|
import type {
|
|
7
7
|
EmbedAttributes,
|
|
@@ -39,8 +39,8 @@ export const renderGeoloniaMap = () => {
|
|
|
39
39
|
|
|
40
40
|
// Backward compat: maps-core's keyring no longer scans the DOM, but legacy
|
|
41
41
|
// embeds set the key only on the container via `data-key`. Seed the keyring
|
|
42
|
-
// from the first container-side key so
|
|
43
|
-
//
|
|
42
|
+
// from the first container-side key so Geolonia-style validation sees the
|
|
43
|
+
// same effective key as before.
|
|
44
44
|
// Per-container `data-key` still flows through parseAtts → attsToOptions.
|
|
45
45
|
if (!keyring.apiKey) {
|
|
46
46
|
const keyed = document.querySelector<HTMLElement>('.geolonia[data-key]');
|
|
@@ -50,123 +50,116 @@ export const renderGeoloniaMap = () => {
|
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// detect if the map removed manually
|
|
85
|
-
map.on('remove', () => {
|
|
86
|
-
map[isRemoved] = true;
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
// remove map instance automatically if the container removed.
|
|
90
|
-
// prevent memory leak
|
|
91
|
-
const observer = new MutationObserver((mutationRecords) => {
|
|
92
|
-
const removed = mutationRecords.some((record) =>
|
|
93
|
-
[...record.removedNodes].some((node) => node === target),
|
|
94
|
-
);
|
|
95
|
-
if (removed && !map[isRemoved]) {
|
|
96
|
-
map.remove();
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
// 親が無いコンテナは DOM から外されることがないので監視しない。
|
|
100
|
-
if (target.parentNode) {
|
|
101
|
-
observer.observe(target.parentNode, { childList: true });
|
|
102
|
-
}
|
|
53
|
+
let isDOMContentLoaded = false;
|
|
54
|
+
const isRemoved = Symbol('map-is-removed');
|
|
55
|
+
|
|
56
|
+
// 手動で remove された地図に印を付けるための symbol プロパティ。
|
|
57
|
+
type RemovableMap = GeoloniaMap & { [isRemoved]?: boolean };
|
|
58
|
+
|
|
59
|
+
const alreadyRenderedMaps: {
|
|
60
|
+
map: RemovableMap;
|
|
61
|
+
target: HTMLElement;
|
|
62
|
+
atts: EmbedAttributes;
|
|
63
|
+
}[] = [];
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
*
|
|
67
|
+
* @param {HTMLElement} target
|
|
68
|
+
*/
|
|
69
|
+
const renderSingleMap = (target: HTMLElement) => {
|
|
70
|
+
const atts = parseAtts(target);
|
|
71
|
+
|
|
72
|
+
// Dedup: if this container was already initialised (e.g. constructed
|
|
73
|
+
// programmatically before the lazy observer fired), reuse the instance.
|
|
74
|
+
// Must happen before extractPopupContent(), otherwise we'd wipe the
|
|
75
|
+
// already-rendered map DOM out of the container.
|
|
76
|
+
let map: RemovableMap | undefined = target.geoloniaMap;
|
|
77
|
+
if (!map) {
|
|
78
|
+
// Grab inline popup content before maps-core clears/renders the container.
|
|
79
|
+
extractPopupContent(target);
|
|
80
|
+
map = new GeoloniaMap(attsToOptions(target, atts));
|
|
81
|
+
}
|
|
103
82
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
} else {
|
|
108
|
-
alreadyRenderedMaps.push({ map, target: target, atts });
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
document.addEventListener('DOMContentLoaded', () => {
|
|
113
|
-
isDOMContentLoaded = true;
|
|
114
|
-
alreadyRenderedMaps.forEach(({ map, target, atts }) => {
|
|
115
|
-
if (!map[isRemoved]) {
|
|
116
|
-
plugins.forEach((plugin) => plugin(map, target, atts));
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
// clear
|
|
120
|
-
alreadyRenderedMaps.splice(0, alreadyRenderedMaps.length);
|
|
83
|
+
// detect if the map removed manually
|
|
84
|
+
map.on('remove', () => {
|
|
85
|
+
map[isRemoved] = true;
|
|
121
86
|
});
|
|
122
87
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
// Not throw error because, following maps will not be rendered.
|
|
133
|
-
console.error('[Geolonia] Failed to initialize map', e); // eslint-disable-line
|
|
134
|
-
}
|
|
135
|
-
observer.unobserve(item.target);
|
|
136
|
-
});
|
|
88
|
+
// remove map instance automatically if the container removed.
|
|
89
|
+
// prevent memory leak
|
|
90
|
+
const observer = new MutationObserver((mutationRecords) => {
|
|
91
|
+
const removed = mutationRecords.some((record) =>
|
|
92
|
+
[...record.removedNodes].some((node) => node === target),
|
|
93
|
+
);
|
|
94
|
+
if (removed && !map[isRemoved]) {
|
|
95
|
+
map.remove();
|
|
96
|
+
}
|
|
137
97
|
});
|
|
98
|
+
// 親が無いコンテナは DOM から外されることがないので監視しない。
|
|
99
|
+
if (target.parentNode) {
|
|
100
|
+
observer.observe(target.parentNode, { childList: true });
|
|
101
|
+
}
|
|
138
102
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
);
|
|
145
|
-
|
|
146
|
-
// This is required for correct initialization! Don't delete!
|
|
147
|
-
if (keyring.isGeoloniaStyle && !keyring.apiKey) {
|
|
148
|
-
console.error('[Geolonia] Missing API key.'); // eslint-disable-line
|
|
103
|
+
// plugin
|
|
104
|
+
if (isDOMContentLoaded && !map[isRemoved]) {
|
|
105
|
+
plugins.forEach((plugin) => plugin(map, target, atts));
|
|
106
|
+
} else {
|
|
107
|
+
alreadyRenderedMaps.push({ map, target: target, atts });
|
|
149
108
|
}
|
|
109
|
+
};
|
|
150
110
|
|
|
151
|
-
|
|
152
|
-
|
|
111
|
+
document.addEventListener('DOMContentLoaded', () => {
|
|
112
|
+
isDOMContentLoaded = true;
|
|
113
|
+
alreadyRenderedMaps.forEach(({ map, target, atts }) => {
|
|
114
|
+
if (!map[isRemoved]) {
|
|
115
|
+
plugins.forEach((plugin) => plugin(map, target, atts));
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
// clear
|
|
119
|
+
alreadyRenderedMaps.splice(0, alreadyRenderedMaps.length);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const observer = new IntersectionObserver((entries) => {
|
|
123
|
+
entries.forEach((item) => {
|
|
124
|
+
if (!item.isIntersecting) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
153
127
|
try {
|
|
154
|
-
|
|
128
|
+
// 監視対象は下の querySelectorAll<HTMLElement> で集めた要素に限られる。
|
|
129
|
+
renderSingleMap(item.target as HTMLElement);
|
|
155
130
|
} catch (e) {
|
|
156
131
|
// Not throw error because, following maps will not be rendered.
|
|
157
132
|
console.error('[Geolonia] Failed to initialize map', e); // eslint-disable-line
|
|
158
133
|
}
|
|
159
|
-
|
|
134
|
+
observer.unobserve(item.target);
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
const containers = document.querySelectorAll<HTMLElement>(
|
|
139
|
+
'.geolonia[data-lazy-loading="off"]',
|
|
140
|
+
);
|
|
141
|
+
const lazyContainers = document.querySelectorAll<HTMLElement>(
|
|
142
|
+
'.geolonia:not([data-lazy-loading="off"])',
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
// This is required for correct initialization! Don't delete!
|
|
146
|
+
if (keyring.isGeoloniaStyle && !keyring.apiKey) {
|
|
147
|
+
console.error('[Geolonia] Missing API key.'); // eslint-disable-line
|
|
148
|
+
}
|
|
160
149
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
150
|
+
// render Map immediately
|
|
151
|
+
for (let i = 0; i < containers.length; i++) {
|
|
152
|
+
try {
|
|
153
|
+
renderSingleMap(containers[i]);
|
|
154
|
+
} catch (e) {
|
|
155
|
+
// Not throw error because, following maps will not be rendered.
|
|
156
|
+
console.error('[Geolonia] Failed to initialize map', e); // eslint-disable-line
|
|
164
157
|
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// set intersection observer
|
|
161
|
+
for (let i = 0; i < lazyContainers.length; i++) {
|
|
162
|
+
observer.observe(lazyContainers[i]);
|
|
170
163
|
}
|
|
171
164
|
};
|
|
172
165
|
|
package/src/lib/util.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
import { keyring } from '@geolonia/maps-core';
|
|
4
|
-
|
|
5
3
|
/**
|
|
6
4
|
* Parse the Geolonia API key from the `?geolonia-api-key=` query of the
|
|
7
5
|
* embed `<script>` tag (backward compatibility).
|
|
@@ -41,77 +39,3 @@ export function parseScriptTagApiKey(
|
|
|
41
39
|
|
|
42
40
|
return null;
|
|
43
41
|
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Detect whether the embed is allowed to render in the current (possibly
|
|
47
|
-
* iframed) context. Embed-specific: not provided by maps-core.
|
|
48
|
-
*/
|
|
49
|
-
export function checkPermission() {
|
|
50
|
-
// It looks that isn't iFrame, so returns true.
|
|
51
|
-
if (window.self === window.parent) {
|
|
52
|
-
return true;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// Always returns true if API key is loaded.
|
|
56
|
-
if (keyring.apiKey) {
|
|
57
|
-
return true;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* For the https://codepen.io/
|
|
62
|
-
* iFrame による Codepen の地図の認可外のサイトへの埋め込みを許可しない
|
|
63
|
-
*/
|
|
64
|
-
if (
|
|
65
|
-
window.self.location.origin === 'https://cdpn.io' ||
|
|
66
|
-
window.self.location.origin === 'https://codepen.io'
|
|
67
|
-
) {
|
|
68
|
-
if (
|
|
69
|
-
window.self !== window.parent &&
|
|
70
|
-
window.document.referrer.indexOf('https://codepen.io') === 0
|
|
71
|
-
) {
|
|
72
|
-
return true;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* For the https://jsfiddle.net/
|
|
78
|
-
*/
|
|
79
|
-
if (window.self.location.origin === 'https://fiddle.jshell.net') {
|
|
80
|
-
if (
|
|
81
|
-
window.self !== window.parent &&
|
|
82
|
-
window.document.referrer.indexOf('https://jsfiddle.net') === 0
|
|
83
|
-
) {
|
|
84
|
-
return true;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* For the https://codesandbox.io/
|
|
90
|
-
*
|
|
91
|
-
* Note:
|
|
92
|
-
* codesandbox.io has two preview window, one is in right sidebar with iframe and
|
|
93
|
-
* another one is in new window.
|
|
94
|
-
*/
|
|
95
|
-
if (window.self.location.origin.match(/csb\.app$/)) {
|
|
96
|
-
if (
|
|
97
|
-
window.self !== window.parent &&
|
|
98
|
-
window.document.referrer.indexOf('https://codesandbox.io') === 0
|
|
99
|
-
) {
|
|
100
|
-
return true;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* `window.parent` will be blocked if same origin policy is activated.
|
|
106
|
-
* So, it should be caught.
|
|
107
|
-
*/
|
|
108
|
-
try {
|
|
109
|
-
if (window.self.location.origin === window.top?.location.origin) {
|
|
110
|
-
return true;
|
|
111
|
-
}
|
|
112
|
-
} catch {
|
|
113
|
-
return false;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
return false;
|
|
117
|
-
}
|