@geolonia/embed 5.2.1 → 6.0.0-pre.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.
- package/.github/workflows/build.yml +38 -19
- package/CHANGELOG.md +9 -0
- package/README.md +24 -1
- package/dist/embed +1 -1
- package/dist/embed-chunks/4ada77b78cc0cc5ccef9.js +1 -0
- package/dist/embed-chunks/8176630945512b29f294.js +2 -0
- package/dist/embed-chunks/c783fffcd28083f7d5cb.js +2 -0
- package/dist/embed-chunks/f2124097fda51d0079ee.js +1 -0
- package/dist/embed-core.d.ts +6 -7
- package/dist/embed-core.js +1 -1
- package/dist/embed.d.ts +2 -4
- package/dist/embed.js +1 -1
- package/dist/lib/parse-atts.d.ts +19 -0
- package/dist/lib/render.d.ts +1 -1
- package/dist/lib/util.d.ts +14 -55
- package/dist/types.d.ts +155 -1
- package/dist/version.d.ts +1 -1
- package/e2e/auto-render.spec.ts +44 -0
- package/e2e/basic.spec.ts +9 -50
- package/e2e/cleanup.spec.ts +43 -0
- package/e2e/data-attributes.spec.ts +56 -0
- package/e2e/globals.spec.ts +51 -0
- package/e2e/helper.ts +170 -4
- package/e2e/iframe.spec.ts +35 -0
- package/e2e/lazy-loading.spec.ts +32 -0
- package/e2e/legacy-constructor.spec.ts +102 -0
- package/e2e/plugin.spec.ts +25 -0
- package/e2e/pmtiles.spec.ts +13 -0
- package/package.json +8 -13
- package/playwright.config.ts +6 -32
- package/src/embed-core.ts +12 -14
- package/src/embed.ts +2 -4
- package/src/lib/parse-atts.test.ts +186 -18
- package/src/lib/parse-atts.ts +151 -5
- package/src/lib/render.ts +50 -12
- package/src/lib/util.ts +37 -350
- package/src/types.ts +155 -1
- package/src/version.ts +1 -0
- package/typedoc.json +13 -0
- package/vitest.config.ts +7 -0
- package/dist/embed-chunks/12cbd0d1cb4cf744c32f.js +0 -2
- package/dist/embed-chunks/8a521e8312a5b4da123d.js +0 -2
- package/dist/lib/CustomAttributionControl.d.ts +0 -47
- package/dist/lib/controls/geolonia-control.d.ts +0 -7
- package/dist/lib/geolonia-map.d.ts +0 -30
- package/dist/lib/geolonia-marker.d.ts +0 -9
- package/dist/lib/keyring.d.ts +0 -45
- package/dist/lib/maplibre-util.d.ts +0 -25
- package/dist/lib/simplestyle-vector.d.ts +0 -26
- package/dist/lib/simplestyle.d.ts +0 -31
- package/e2e/external-style.spec.ts +0 -25
- package/e2e/map.spec.ts +0 -69
- package/src/lib/CustomAttributionControl.ts +0 -394
- package/src/lib/controls/geolonia-control.ts +0 -38
- package/src/lib/geolonia-map.ts +0 -429
- package/src/lib/geolonia-marker.ts +0 -53
- package/src/lib/keyring.test.ts +0 -127
- package/src/lib/keyring.ts +0 -136
- package/src/lib/maplibre-util.ts +0 -137
- package/src/lib/marker.svg +0 -1
- package/src/lib/simplestyle-vector.ts +0 -257
- package/src/lib/simplestyle.test.ts +0 -289
- package/src/lib/simplestyle.ts +0 -435
- package/src/lib/util.test.ts +0 -364
- /package/dist/embed-chunks/{12cbd0d1cb4cf744c32f.js.LICENSE.txt → 8176630945512b29f294.js.LICENSE.txt} +0 -0
- /package/dist/embed-chunks/{8a521e8312a5b4da123d.js.LICENSE.txt → c783fffcd28083f7d5cb.js.LICENSE.txt} +0 -0
|
@@ -1,27 +1,34 @@
|
|
|
1
1
|
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import { keyring } from './keyring';
|
|
5
|
-
|
|
6
|
-
describe('tests for parse Attributes', () => {
|
|
7
|
-
const prevWindow = global.window;
|
|
2
|
+
import { keyring, getStyle } from '@geolonia/maps-core';
|
|
3
|
+
import parseAtts, { attsToOptions } from './parse-atts';
|
|
8
4
|
|
|
5
|
+
/**
|
|
6
|
+
* parse-atts is the embed-specific "wrapper" layer: it reads `data-*`
|
|
7
|
+
* attributes off a container and (a) exposes them to plugins as
|
|
8
|
+
* `EmbedAttributes` and (b) converts them into a typed `GeoloniaMapOptions`
|
|
9
|
+
* for maps-core via `attsToOptions()`.
|
|
10
|
+
*/
|
|
11
|
+
describe('parseAtts', () => {
|
|
9
12
|
beforeEach(() => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
keyring.reset();
|
|
14
|
+
keyring.setApiKey('YOUR-API-KEY');
|
|
15
|
+
keyring.setStage('v1');
|
|
16
|
+
// navigator.languages is read-only; force a deterministic language.
|
|
17
|
+
Object.defineProperty(window.navigator, 'languages', {
|
|
18
|
+
value: ['ja'],
|
|
19
|
+
configurable: true,
|
|
20
|
+
});
|
|
21
|
+
});
|
|
15
22
|
|
|
23
|
+
afterEach(() => {
|
|
16
24
|
keyring.reset();
|
|
17
25
|
});
|
|
18
26
|
|
|
19
|
-
it('
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
27
|
+
it('returns default EmbedAttributes merged with keyring key/stage', () => {
|
|
28
|
+
const container = document.createElement('div');
|
|
29
|
+
|
|
30
|
+
const atts = parseAtts(container);
|
|
23
31
|
|
|
24
|
-
const atts = parseAtts(mocDocument);
|
|
25
32
|
expect(atts).toEqual({
|
|
26
33
|
lat: 0,
|
|
27
34
|
lng: 0,
|
|
@@ -57,9 +64,170 @@ describe('tests for parse Attributes', () => {
|
|
|
57
64
|
});
|
|
58
65
|
});
|
|
59
66
|
|
|
67
|
+
it('overrides defaults with data-* attributes', () => {
|
|
68
|
+
const container = document.createElement('div');
|
|
69
|
+
container.dataset.lat = '35';
|
|
70
|
+
container.dataset.lng = '139';
|
|
71
|
+
container.dataset.zoom = '10';
|
|
72
|
+
container.dataset.marker = 'off';
|
|
73
|
+
|
|
74
|
+
const atts = parseAtts(container);
|
|
75
|
+
|
|
76
|
+
expect(atts.lat).toBe('35');
|
|
77
|
+
expect(atts.lng).toBe('139');
|
|
78
|
+
expect(atts.zoom).toBe('10');
|
|
79
|
+
expect(atts.marker).toBe('off');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('forces gestureHandling/navigationControl off when non-interactive', () => {
|
|
83
|
+
const container = document.createElement('div');
|
|
84
|
+
const atts = parseAtts(container, { interactive: false });
|
|
85
|
+
expect(atts.gestureHandling).toBe('off');
|
|
86
|
+
expect(atts.navigationControl).toBe('off');
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
describe('attsToOptions', () => {
|
|
91
|
+
beforeEach(() => {
|
|
92
|
+
keyring.reset();
|
|
93
|
+
keyring.setApiKey('YOUR-API-KEY');
|
|
94
|
+
keyring.setStage('v1');
|
|
95
|
+
Object.defineProperty(window.navigator, 'languages', {
|
|
96
|
+
value: ['ja'],
|
|
97
|
+
configurable: true,
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
60
101
|
afterEach(() => {
|
|
61
|
-
global.window = prevWindow;
|
|
62
102
|
keyring.reset();
|
|
63
|
-
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('converts on/off flags into typed GeoloniaMapOptions', () => {
|
|
106
|
+
const container = document.createElement('div');
|
|
107
|
+
container.dataset.hash = 'on';
|
|
108
|
+
container.dataset.marker = 'off';
|
|
109
|
+
container.dataset['3d'] = 'on';
|
|
110
|
+
|
|
111
|
+
const options = attsToOptions(container, parseAtts(container));
|
|
112
|
+
|
|
113
|
+
expect(options.container).toBe(container);
|
|
114
|
+
expect(options.hash).toBe(true);
|
|
115
|
+
expect(options.marker).toBe(false);
|
|
116
|
+
expect(options['3d']).toBe(true);
|
|
117
|
+
expect(options.style).toBe('geolonia/basic-v2');
|
|
118
|
+
expect(options.apiKey).toBe('YOUR-API-KEY');
|
|
119
|
+
expect(options.stage).toBe('v1');
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('maps control positions and on/off to boolean | ControlPosition', () => {
|
|
123
|
+
const container = document.createElement('div');
|
|
124
|
+
container.dataset.navigationControl = 'top-right';
|
|
125
|
+
container.dataset.geolocateControl = 'on';
|
|
126
|
+
container.dataset.scaleControl = 'off';
|
|
127
|
+
|
|
128
|
+
const options = attsToOptions(container, parseAtts(container));
|
|
129
|
+
|
|
130
|
+
expect(options.navigationControl).toBe('top-right');
|
|
131
|
+
expect(options.geolocateControl).toBe(true);
|
|
132
|
+
expect(options.scaleControl).toBe(false);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('sets center only when both data-lat and data-lng are present', () => {
|
|
136
|
+
const withCoords = document.createElement('div');
|
|
137
|
+
withCoords.dataset.lat = '35';
|
|
138
|
+
withCoords.dataset.lng = '139';
|
|
139
|
+
expect(attsToOptions(withCoords, parseAtts(withCoords)).center).toEqual([
|
|
140
|
+
139, 35,
|
|
141
|
+
]);
|
|
142
|
+
|
|
143
|
+
const withoutCoords = document.createElement('div');
|
|
144
|
+
expect(attsToOptions(withoutCoords, parseAtts(withoutCoords)).center).toBe(
|
|
145
|
+
undefined,
|
|
146
|
+
);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('parses customMarkerOffset and zoom bounds', () => {
|
|
150
|
+
const container = document.createElement('div');
|
|
151
|
+
container.dataset.customMarkerOffset = '10, -5';
|
|
152
|
+
container.dataset.minZoom = '3';
|
|
153
|
+
container.dataset.maxZoom = '18';
|
|
154
|
+
|
|
155
|
+
const options = attsToOptions(container, parseAtts(container));
|
|
156
|
+
|
|
157
|
+
expect(options.customMarkerOffset).toEqual([10, -5]);
|
|
158
|
+
expect(options.minZoom).toBe(3);
|
|
159
|
+
expect(options.maxZoom).toBe(18);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('honours data-max-zoom="0" (0 is a valid bound, not falsy)', () => {
|
|
163
|
+
const container = document.createElement('div');
|
|
164
|
+
container.dataset.minZoom = '0';
|
|
165
|
+
container.dataset.maxZoom = '0';
|
|
166
|
+
|
|
167
|
+
const options = attsToOptions(container, parseAtts(container));
|
|
168
|
+
|
|
169
|
+
expect(options.minZoom).toBe(0);
|
|
170
|
+
expect(options.maxZoom).toBe(0);
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
// data-lang の言語解決 (embed#462 / maps-core#50)。
|
|
175
|
+
// 正しい契約 (maps-core#50 のテストが基準):
|
|
176
|
+
// - `ja` / `ja-jp` のみ日本語スタイル、それ以外は英語スタイル
|
|
177
|
+
// - `auto` はブラウザ言語に追従
|
|
178
|
+
// embed の責務: `data-lang` を options.lang へ転送する (最終的な ja/en 判定は
|
|
179
|
+
// maps-core の getStyle が担う)。未指定時のみ embed が getLang() で具体値に解決する。
|
|
180
|
+
describe('data-lang 言語解決', () => {
|
|
181
|
+
beforeEach(() => {
|
|
182
|
+
keyring.reset();
|
|
183
|
+
keyring.setApiKey('KEY');
|
|
184
|
+
});
|
|
185
|
+
afterEach(() => keyring.reset());
|
|
186
|
+
|
|
187
|
+
const setNav = (langs: string[]) =>
|
|
188
|
+
Object.defineProperty(window.navigator, 'languages', {
|
|
189
|
+
value: langs,
|
|
190
|
+
configurable: true,
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
const optionsFor = (dataLang: string | undefined, navLangs: string[]) => {
|
|
194
|
+
setNav(navLangs);
|
|
195
|
+
const c = document.createElement('div');
|
|
196
|
+
if (dataLang !== undefined) c.dataset.lang = dataLang;
|
|
197
|
+
return attsToOptions(c, parseAtts(c));
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
it('data-lang="en" は browser が ja でも英語スタイルになる (ja にならない)', () => {
|
|
201
|
+
const opts = optionsFor('en', ['ja']);
|
|
202
|
+
expect(opts.lang).toBe('en');
|
|
203
|
+
expect(getStyle('geolonia/basic-v2', { lang: 'en', apiKey: 'KEY' })).toContain(
|
|
204
|
+
'/en.json',
|
|
205
|
+
);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it('data-lang="ja" は browser が en でも日本語スタイルになる', () => {
|
|
209
|
+
const opts = optionsFor('ja', ['en']);
|
|
210
|
+
expect(opts.lang).toBe('ja');
|
|
211
|
+
expect(getStyle('geolonia/basic-v2', { lang: 'ja', apiKey: 'KEY' })).toContain(
|
|
212
|
+
'/ja.json',
|
|
213
|
+
);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it('data-lang="ja-jp" は日本語スタイルになる (maps-core#50)', () => {
|
|
217
|
+
const opts = optionsFor('ja-jp', ['en']);
|
|
218
|
+
expect(opts.lang).toBe('ja-jp');
|
|
219
|
+
expect(
|
|
220
|
+
getStyle('geolonia/basic-v2', { lang: 'ja-jp', apiKey: 'KEY' }),
|
|
221
|
+
).toContain('/ja.json');
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('data-lang 未指定はブラウザ言語に追従する', () => {
|
|
225
|
+
expect(optionsFor(undefined, ['en']).lang).toBe('en');
|
|
226
|
+
expect(optionsFor(undefined, ['ja']).lang).toBe('ja');
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('data-lang="auto" は maps-core へ委譲される (auto のまま転送)', () => {
|
|
230
|
+
expect(optionsFor('auto', ['en']).lang).toBe('auto');
|
|
231
|
+
expect(optionsFor('auto', ['ja']).lang).toBe('auto');
|
|
64
232
|
});
|
|
65
233
|
});
|
package/src/lib/parse-atts.ts
CHANGED
|
@@ -1,14 +1,26 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import { getLang } from '
|
|
3
|
+
import type { ControlPosition } from 'maplibre-gl';
|
|
4
|
+
import { getLang, keyring, type GeoloniaMapOptions } from '@geolonia/maps-core';
|
|
5
5
|
import type { EmbedAttributes } from '../types';
|
|
6
6
|
|
|
7
7
|
type ParseAttsParams = {
|
|
8
8
|
interactive?: boolean;
|
|
9
9
|
};
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Read the `data-*` attributes off a map container and normalise them into the
|
|
13
|
+
* embed's `EmbedAttributes` shape (string values, `'on'`/`'off'` flags).
|
|
14
|
+
*
|
|
15
|
+
* This is the value handed to registered plugins, so its shape is kept stable
|
|
16
|
+
* for backward compatibility. The map itself is constructed from
|
|
17
|
+
* {@link attsToOptions}, which converts these into a typed
|
|
18
|
+
* `GeoloniaMapOptions` for maps-core.
|
|
19
|
+
*/
|
|
20
|
+
export default (
|
|
21
|
+
container,
|
|
22
|
+
params: ParseAttsParams = {},
|
|
23
|
+
): EmbedAttributes => {
|
|
12
24
|
if (!container.dataset) {
|
|
13
25
|
container.dataset = {};
|
|
14
26
|
}
|
|
@@ -24,8 +36,6 @@ export default (container, params: ParseAttsParams = {}): EmbedAttributes => {
|
|
|
24
36
|
lang = getLang();
|
|
25
37
|
}
|
|
26
38
|
|
|
27
|
-
keyring.parse(container);
|
|
28
|
-
|
|
29
39
|
// Check if using Geolonia style or external style
|
|
30
40
|
const style = container.dataset.style || 'geolonia/basic-v2';
|
|
31
41
|
keyring.isGeoloniaStyle = keyring.isGeoloniaStyleCheck(style);
|
|
@@ -65,3 +75,139 @@ export default (container, params: ParseAttsParams = {}): EmbedAttributes => {
|
|
|
65
75
|
...container.dataset,
|
|
66
76
|
};
|
|
67
77
|
};
|
|
78
|
+
|
|
79
|
+
const CONTROL_POSITIONS: ControlPosition[] = [
|
|
80
|
+
'top-right',
|
|
81
|
+
'bottom-right',
|
|
82
|
+
'bottom-left',
|
|
83
|
+
'top-left',
|
|
84
|
+
];
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Convert a `data-*-control` value (`'on'` / `'off'` / a position string) into
|
|
88
|
+
* the `boolean | ControlPosition` maps-core expects.
|
|
89
|
+
*/
|
|
90
|
+
const controlOption = (att: string | number): boolean | ControlPosition => {
|
|
91
|
+
const value = String(att).toLowerCase();
|
|
92
|
+
if ((CONTROL_POSITIONS as string[]).includes(value)) {
|
|
93
|
+
return value as ControlPosition;
|
|
94
|
+
}
|
|
95
|
+
return value === 'on';
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Decide whether a value is a CSS selector pointing at an inline element
|
|
100
|
+
* (e.g. a `<script type="application/json">`) rather than a URL / path.
|
|
101
|
+
*/
|
|
102
|
+
const isCssSelector = (value: string): Element | false => {
|
|
103
|
+
if (/^https?:\/\//.test(value) || /^\.?\.?\//.test(value)) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
try {
|
|
107
|
+
return document.querySelector(value) || false;
|
|
108
|
+
} catch {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Resolve a `data-geojson` value into what maps-core's SimpleStyle accepts:
|
|
115
|
+
* a URL / inline JSON string, or a parsed FeatureCollection when the value is a
|
|
116
|
+
* CSS selector referencing an inline element.
|
|
117
|
+
*/
|
|
118
|
+
const resolveGeojson = (
|
|
119
|
+
value: string,
|
|
120
|
+
): string | GeoJSON.FeatureCollection => {
|
|
121
|
+
const el = isCssSelector(value);
|
|
122
|
+
if (el && el.textContent) {
|
|
123
|
+
return JSON.parse(el.textContent) as GeoJSON.FeatureCollection;
|
|
124
|
+
}
|
|
125
|
+
return value;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Convert normalised {@link EmbedAttributes} into a typed `GeoloniaMapOptions`
|
|
130
|
+
* for maps-core's `GeoloniaMap` constructor.
|
|
131
|
+
*
|
|
132
|
+
* `center` is only set when both `data-lat` and `data-lng` are present so that
|
|
133
|
+
* maps-core keeps its default marker gating (marker requires a center) and
|
|
134
|
+
* GeoJSON `fitBounds` (skipped only when an explicit center is given).
|
|
135
|
+
*/
|
|
136
|
+
export const attsToOptions = (
|
|
137
|
+
container: HTMLElement,
|
|
138
|
+
atts: EmbedAttributes,
|
|
139
|
+
): GeoloniaMapOptions => {
|
|
140
|
+
const options: GeoloniaMapOptions = {
|
|
141
|
+
container,
|
|
142
|
+
style: String(atts.style),
|
|
143
|
+
lang: atts.lang as 'ja' | 'en' | 'auto',
|
|
144
|
+
bearing: parseFloat(String(atts.bearing)) || 0,
|
|
145
|
+
pitch: parseFloat(String(atts.pitch)) || 0,
|
|
146
|
+
zoom: parseFloat(String(atts.zoom)) || 0,
|
|
147
|
+
hash: atts.hash === 'on',
|
|
148
|
+
marker: atts.marker === 'on',
|
|
149
|
+
markerColor: String(atts.markerColor),
|
|
150
|
+
openPopup: atts.openPopup === 'on',
|
|
151
|
+
loader: atts.loader !== 'off',
|
|
152
|
+
gestureHandling: atts.gestureHandling !== 'off',
|
|
153
|
+
navigationControl: controlOption(atts.navigationControl),
|
|
154
|
+
geolocateControl: controlOption(atts.geolocateControl),
|
|
155
|
+
fullscreenControl: controlOption(atts.fullscreenControl),
|
|
156
|
+
scaleControl: controlOption(atts.scaleControl),
|
|
157
|
+
geoloniaControl: controlOption(atts.geoloniaControl),
|
|
158
|
+
cluster: atts.cluster === 'on',
|
|
159
|
+
clusterColor: String(atts.clusterColor),
|
|
160
|
+
'3d': atts['3d'] === 'on',
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
const apiKey = String(atts.key || '');
|
|
164
|
+
if (apiKey) {
|
|
165
|
+
options.apiKey = apiKey;
|
|
166
|
+
}
|
|
167
|
+
const stage = String(atts.stage || '');
|
|
168
|
+
if (stage) {
|
|
169
|
+
options.stage = stage;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const dataset = container.dataset || {};
|
|
173
|
+
const hasLat = dataset.lat !== undefined && dataset.lat !== '';
|
|
174
|
+
const hasLng = dataset.lng !== undefined && dataset.lng !== '';
|
|
175
|
+
if (hasLat && hasLng) {
|
|
176
|
+
options.center = [
|
|
177
|
+
parseFloat(String(atts.lng)),
|
|
178
|
+
parseFloat(String(atts.lat)),
|
|
179
|
+
];
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (atts.customMarker) {
|
|
183
|
+
options.customMarker = String(atts.customMarker);
|
|
184
|
+
}
|
|
185
|
+
if (atts.customMarkerOffset) {
|
|
186
|
+
const [x, y] = String(atts.customMarkerOffset)
|
|
187
|
+
.split(',')
|
|
188
|
+
.map((n) => Number(n.trim()));
|
|
189
|
+
options.customMarkerOffset = [x || 0, y || 0];
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (atts.geojson) {
|
|
193
|
+
options.geojson = resolveGeojson(String(atts.geojson));
|
|
194
|
+
}
|
|
195
|
+
if (atts.simpleVector) {
|
|
196
|
+
options.simpleVector = String(atts.simpleVector);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (
|
|
200
|
+
atts.minZoom !== '' &&
|
|
201
|
+
(Number(atts.minZoom) === 0 || Number(atts.minZoom))
|
|
202
|
+
) {
|
|
203
|
+
options.minZoom = Number(atts.minZoom);
|
|
204
|
+
}
|
|
205
|
+
if (
|
|
206
|
+
atts.maxZoom !== '' &&
|
|
207
|
+
(Number(atts.maxZoom) === 0 || Number(atts.maxZoom))
|
|
208
|
+
) {
|
|
209
|
+
options.maxZoom = Number(atts.maxZoom);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return options;
|
|
213
|
+
};
|
package/src/lib/render.ts
CHANGED
|
@@ -1,31 +1,70 @@
|
|
|
1
|
-
import maplibregl from 'maplibre-gl';
|
|
2
1
|
import 'maplibre-gl/dist/maplibre-gl.css';
|
|
3
2
|
import '../style.css';
|
|
4
|
-
import GeoloniaMap from '
|
|
5
|
-
import { checkPermission } from './util';
|
|
6
|
-
import parseAtts from './parse-atts';
|
|
7
|
-
import { keyring } from './keyring';
|
|
8
|
-
import { Protocol } from 'pmtiles';
|
|
3
|
+
import { GeoloniaMap, keyring } from '@geolonia/maps-core';
|
|
4
|
+
import { checkPermission, parseScriptTagApiKey } from './util';
|
|
5
|
+
import parseAtts, { attsToOptions } from './parse-atts';
|
|
9
6
|
|
|
10
7
|
const plugins = [];
|
|
11
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Move any inline HTML the author placed inside the map container into
|
|
11
|
+
* `data-popup-content`, which is where maps-core reads the marker popup body
|
|
12
|
+
* from. The container itself is emptied so the leftover markup doesn't render
|
|
13
|
+
* behind the map. (Old embed read `container.innerHTML` directly; maps-core
|
|
14
|
+
* takes options only.)
|
|
15
|
+
*/
|
|
16
|
+
const extractPopupContent = (target: HTMLElement) => {
|
|
17
|
+
const content = target.innerHTML.trim();
|
|
18
|
+
if (content) {
|
|
19
|
+
target.dataset.popupContent = content;
|
|
20
|
+
}
|
|
21
|
+
target.innerHTML = '';
|
|
22
|
+
};
|
|
23
|
+
|
|
12
24
|
export const renderGeoloniaMap = () => {
|
|
13
|
-
|
|
14
|
-
|
|
25
|
+
// maps-core's keyring no longer scans the DOM, so parse the embed script
|
|
26
|
+
// tag here (`?geolonia-api-key=`) and push the key/stage into the keyring.
|
|
27
|
+
const parsed = parseScriptTagApiKey();
|
|
28
|
+
if (parsed) {
|
|
29
|
+
keyring.setApiKey(parsed.apiKey);
|
|
30
|
+
keyring.setStage(parsed.stage);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Backward compat: maps-core's keyring no longer scans the DOM, but legacy
|
|
34
|
+
// embeds set the key only on the container via `data-key`. Seed the keyring
|
|
35
|
+
// from the first container-side key so the iframe permission check below
|
|
36
|
+
// (and Geolonia-style validation) sees the same effective key as before.
|
|
37
|
+
// Per-container `data-key` still flows through parseAtts → attsToOptions.
|
|
38
|
+
if (!keyring.apiKey) {
|
|
39
|
+
const keyed = document.querySelector<HTMLElement>('.geolonia[data-key]');
|
|
40
|
+
const dataKey = keyed?.dataset.key;
|
|
41
|
+
if (dataKey) {
|
|
42
|
+
keyring.setApiKey(dataKey);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
15
45
|
|
|
16
46
|
if (checkPermission()) {
|
|
17
47
|
let isDOMContentLoaded = false;
|
|
18
48
|
const alreadyRenderedMaps = [];
|
|
19
49
|
const isRemoved = Symbol('map-is-removed');
|
|
20
50
|
|
|
21
|
-
keyring.parse();
|
|
22
|
-
|
|
23
51
|
/**
|
|
24
52
|
*
|
|
25
53
|
* @param {HTMLElement} target
|
|
26
54
|
*/
|
|
27
55
|
const renderSingleMap = (target) => {
|
|
28
|
-
const
|
|
56
|
+
const atts = parseAtts(target);
|
|
57
|
+
|
|
58
|
+
// Dedup: if this container was already initialised (e.g. constructed
|
|
59
|
+
// programmatically before the lazy observer fired), reuse the instance.
|
|
60
|
+
// Must happen before extractPopupContent(), otherwise we'd wipe the
|
|
61
|
+
// already-rendered map DOM out of the container.
|
|
62
|
+
let map = target.geoloniaMap;
|
|
63
|
+
if (!map) {
|
|
64
|
+
// Grab inline popup content before maps-core clears/renders the container.
|
|
65
|
+
extractPopupContent(target);
|
|
66
|
+
map = new GeoloniaMap(attsToOptions(target, atts));
|
|
67
|
+
}
|
|
29
68
|
|
|
30
69
|
// detect if the map removed manually
|
|
31
70
|
map.on('remove', () => {
|
|
@@ -45,7 +84,6 @@ export const renderGeoloniaMap = () => {
|
|
|
45
84
|
observer.observe(target.parentNode, { childList: true });
|
|
46
85
|
|
|
47
86
|
// plugin
|
|
48
|
-
const atts = parseAtts(target);
|
|
49
87
|
if (isDOMContentLoaded && !map[isRemoved]) {
|
|
50
88
|
plugins.forEach((plugin) => plugin(map, target, atts));
|
|
51
89
|
} else {
|