@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.
Files changed (66) hide show
  1. package/.github/workflows/build.yml +38 -19
  2. package/CHANGELOG.md +9 -0
  3. package/README.md +24 -1
  4. package/dist/embed +1 -1
  5. package/dist/embed-chunks/4ada77b78cc0cc5ccef9.js +1 -0
  6. package/dist/embed-chunks/8176630945512b29f294.js +2 -0
  7. package/dist/embed-chunks/c783fffcd28083f7d5cb.js +2 -0
  8. package/dist/embed-chunks/f2124097fda51d0079ee.js +1 -0
  9. package/dist/embed-core.d.ts +6 -7
  10. package/dist/embed-core.js +1 -1
  11. package/dist/embed.d.ts +2 -4
  12. package/dist/embed.js +1 -1
  13. package/dist/lib/parse-atts.d.ts +19 -0
  14. package/dist/lib/render.d.ts +1 -1
  15. package/dist/lib/util.d.ts +14 -55
  16. package/dist/types.d.ts +155 -1
  17. package/dist/version.d.ts +1 -1
  18. package/e2e/auto-render.spec.ts +44 -0
  19. package/e2e/basic.spec.ts +9 -50
  20. package/e2e/cleanup.spec.ts +43 -0
  21. package/e2e/data-attributes.spec.ts +56 -0
  22. package/e2e/globals.spec.ts +51 -0
  23. package/e2e/helper.ts +170 -4
  24. package/e2e/iframe.spec.ts +35 -0
  25. package/e2e/lazy-loading.spec.ts +32 -0
  26. package/e2e/legacy-constructor.spec.ts +102 -0
  27. package/e2e/plugin.spec.ts +25 -0
  28. package/e2e/pmtiles.spec.ts +13 -0
  29. package/package.json +8 -13
  30. package/playwright.config.ts +6 -32
  31. package/src/embed-core.ts +12 -14
  32. package/src/embed.ts +2 -4
  33. package/src/lib/parse-atts.test.ts +186 -18
  34. package/src/lib/parse-atts.ts +151 -5
  35. package/src/lib/render.ts +50 -12
  36. package/src/lib/util.ts +37 -350
  37. package/src/types.ts +155 -1
  38. package/src/version.ts +1 -0
  39. package/typedoc.json +13 -0
  40. package/vitest.config.ts +7 -0
  41. package/dist/embed-chunks/12cbd0d1cb4cf744c32f.js +0 -2
  42. package/dist/embed-chunks/8a521e8312a5b4da123d.js +0 -2
  43. package/dist/lib/CustomAttributionControl.d.ts +0 -47
  44. package/dist/lib/controls/geolonia-control.d.ts +0 -7
  45. package/dist/lib/geolonia-map.d.ts +0 -30
  46. package/dist/lib/geolonia-marker.d.ts +0 -9
  47. package/dist/lib/keyring.d.ts +0 -45
  48. package/dist/lib/maplibre-util.d.ts +0 -25
  49. package/dist/lib/simplestyle-vector.d.ts +0 -26
  50. package/dist/lib/simplestyle.d.ts +0 -31
  51. package/e2e/external-style.spec.ts +0 -25
  52. package/e2e/map.spec.ts +0 -69
  53. package/src/lib/CustomAttributionControl.ts +0 -394
  54. package/src/lib/controls/geolonia-control.ts +0 -38
  55. package/src/lib/geolonia-map.ts +0 -429
  56. package/src/lib/geolonia-marker.ts +0 -53
  57. package/src/lib/keyring.test.ts +0 -127
  58. package/src/lib/keyring.ts +0 -136
  59. package/src/lib/maplibre-util.ts +0 -137
  60. package/src/lib/marker.svg +0 -1
  61. package/src/lib/simplestyle-vector.ts +0 -257
  62. package/src/lib/simplestyle.test.ts +0 -289
  63. package/src/lib/simplestyle.ts +0 -435
  64. package/src/lib/util.test.ts +0 -364
  65. /package/dist/embed-chunks/{12cbd0d1cb4cf744c32f.js.LICENSE.txt → 8176630945512b29f294.js.LICENSE.txt} +0 -0
  66. /package/dist/embed-chunks/{8a521e8312a5b4da123d.js.LICENSE.txt → c783fffcd28083f7d5cb.js.LICENSE.txt} +0 -0
@@ -1,136 +0,0 @@
1
- class Keyring {
2
- #apiKey: string;
3
- #stage: string;
4
- #isGeoloniaStyle: boolean;
5
-
6
- /**
7
- * @returns API key
8
- */
9
- get apiKey() {
10
- if (!this.#apiKey) {
11
- this.parse();
12
- }
13
- return this.#apiKey;
14
- }
15
- /**
16
- * @returns Whether using Geolonia style (true) or external style (false)
17
- */
18
- get isGeoloniaStyle() {
19
- return this.#isGeoloniaStyle;
20
- }
21
- /**
22
- * @param isGeoloniaStyle - Set whether using Geolonia style
23
- */
24
- set isGeoloniaStyle(value: boolean) {
25
- this.#isGeoloniaStyle = value;
26
- }
27
- /**
28
- * @param key - API Key to set
29
- */
30
- set apiKey(key: string) {
31
- this.#apiKey = key;
32
- }
33
- /**
34
- * @returns Stage name
35
- */
36
- get stage() {
37
- if (!this.#stage) {
38
- this.parse();
39
- }
40
- return this.#stage;
41
- }
42
- /**
43
- * @param stage - Stage name to set
44
- */
45
- set stage(stage: string) {
46
- this.#stage = stage;
47
- }
48
-
49
- /**
50
- * Parses the API key and stage from the URL of the current script tag.
51
- *
52
- * @param {Document} document
53
- */
54
- public parse(doc: Document = document) {
55
- const scripts: HTMLScriptElement[] | HTMLCollectionOf<HTMLScriptElement> =
56
- doc.currentScript
57
- ? [doc.currentScript as HTMLScriptElement]
58
- : doc.getElementsByTagName('script');
59
-
60
- for (const script of scripts) {
61
- const url = new URL(
62
- script.src.startsWith('https://') ||
63
- script.src.startsWith('http://') ||
64
- script.src.startsWith('//')
65
- ? script.src
66
- : `https://${location.host}/${script.src}`,
67
- );
68
- const apiKey = url.searchParams.get('geolonia-api-key');
69
-
70
- if (apiKey) {
71
- this.#apiKey = apiKey;
72
-
73
- this.#stage = process.env.MAP_PLATFORM_STAGE || 'dev';
74
-
75
- break;
76
- }
77
- }
78
-
79
- if (!this.#stage) {
80
- this.#stage = 'dev';
81
- }
82
-
83
- if (this.#isGeoloniaStyle && !this.#apiKey) {
84
- throw new Error('Cannot load API key.');
85
- }
86
- }
87
-
88
- /** Reset API key and stage for testing purpose */
89
- public reset() {
90
- this.#apiKey = undefined;
91
- this.#stage = undefined;
92
- this.#isGeoloniaStyle = false;
93
- }
94
-
95
- /**
96
- * Check if the given style is a Geolonia style (requires API key)
97
- * Note: This is a method, not the property. Use keyring.isGeoloniaStyle property
98
- * to check the current style type, or call this method to check a specific style.
99
- * @param style - Style name or URL
100
- * @returns true if it's a Geolonia style (requires API key)
101
- */
102
- public isGeoloniaStyleCheck(style: string): boolean {
103
- if (!style || style === '') {
104
- // Default style is Geolonia
105
- return true;
106
- }
107
-
108
- // Check if it's a Geolonia CDN or API URL
109
- if (style.startsWith('https://cdn.geolonia.com/style/') ||
110
- style.startsWith('https://api.geolonia.com/')) {
111
- return true;
112
- }
113
-
114
- // If it's a full URL (http:// or https://), it's external
115
- if (style.match(/^https?:\/\//)) {
116
- return false;
117
- }
118
-
119
- // If it ends with .json and doesn't contain geolonia.com, it's likely external
120
- if (style.endsWith('.json')) {
121
- try {
122
- const absoluteUrl = new URL(style, location.href).href;
123
- return absoluteUrl.includes('geolonia.com');
124
- } catch {
125
- return false;
126
- }
127
- }
128
-
129
- // Otherwise, it's a Geolonia logical name like "geolonia/basic"
130
- return true;
131
- }
132
- }
133
-
134
- const keyring = new Keyring(); // Singleton
135
-
136
- export { keyring };
@@ -1,137 +0,0 @@
1
- import Point from '@mapbox/point-geometry';
2
-
3
- /**
4
- * This class is a copy of maplibre-gl-js's util DOM class and rewrite it to JavaScript.
5
- * https://github.com/maplibre/maplibre-gl-js/blob/main/src/util/dom.ts
6
- * */
7
- export class DOM {
8
- static #docStyle =
9
- typeof window !== 'undefined' &&
10
- window.document &&
11
- window.document.documentElement.style;
12
-
13
- static #userSelect;
14
-
15
- static #selectProp = DOM.testProp([
16
- 'userSelect',
17
- 'MozUserSelect',
18
- 'WebkitUserSelect',
19
- 'msUserSelect',
20
- ]);
21
-
22
- static #transformProp = DOM.testProp(['transform', 'WebkitTransform']);
23
-
24
- static testProp(props) {
25
- if (!DOM.#docStyle) return props[0];
26
- for (let i = 0; i < props.length; i++) {
27
- if (props[i] in DOM.#docStyle) {
28
- return props[i];
29
- }
30
- }
31
- return props[0];
32
- }
33
-
34
- static create(tagName, className?, container?) {
35
- const el = window.document.createElement(tagName);
36
- if (className !== undefined) el.className = className;
37
- if (container) container.appendChild(el);
38
- return el;
39
- }
40
-
41
- static createNS(namespaceURI, tagName) {
42
- const el = window.document.createElementNS(namespaceURI, tagName);
43
- return el;
44
- }
45
-
46
- static disableDrag() {
47
- if (DOM.#docStyle && DOM.#selectProp) {
48
- DOM.#userSelect = DOM.#docStyle[DOM.#selectProp];
49
- DOM.#docStyle[DOM.#selectProp] = 'none';
50
- }
51
- }
52
-
53
- static enableDrag() {
54
- if (DOM.#docStyle && DOM.#selectProp) {
55
- DOM.#docStyle[DOM.#selectProp] = DOM.#userSelect;
56
- }
57
- }
58
-
59
- static setTransform(el, value) {
60
- el.style[DOM.#transformProp] = value;
61
- }
62
-
63
- static addEventListener(target, type, callback, options) {
64
- if ('passive' in options) {
65
- target.addEventListener(type, callback, options);
66
- } else {
67
- target.addEventListener(type, callback, options.capture);
68
- }
69
- }
70
-
71
- static removeEventListener(target, type, callback, options) {
72
- if ('passive' in options) {
73
- target.removeEventListener(type, callback, options);
74
- } else {
75
- target.removeEventListener(type, callback, options.capture);
76
- }
77
- }
78
-
79
- // Suppress the next click, but only if it's immediate.
80
- /*static #suppressClickInternal(e) {
81
- e.preventDefault();
82
- e.stopPropagation();
83
- window.removeEventListener('click', DOM['#transformProp'], true);
84
- }*/
85
-
86
- static suppressClick() {
87
- window.addEventListener('click', DOM.#transformProp, true);
88
- window.setTimeout(() => {
89
- window.removeEventListener('click', DOM.#transformProp, true);
90
- }, 0);
91
- }
92
-
93
- static mousePos(el, e) {
94
- const rect = el.getBoundingClientRect();
95
- return new Point(
96
- e.clientX - rect.left - el.clientLeft,
97
- e.clientY - rect.top - el.clientTop,
98
- );
99
- }
100
-
101
- static touchPos(el, touches) {
102
- const rect = el.getBoundingClientRect();
103
- const points = [];
104
- for (let i = 0; i < touches.length; i++) {
105
- points.push(
106
- new Point(
107
- touches[i].clientX - rect.left - el.clientLeft,
108
- touches[i].clientY - rect.top - el.clientTop,
109
- ),
110
- );
111
- }
112
- return points;
113
- }
114
-
115
- static mouseButton(e) {
116
- return e.button;
117
- }
118
-
119
- static remove(node) {
120
- if (node.parentNode) {
121
- node.parentNode.removeChild(node);
122
- }
123
- }
124
- }
125
-
126
- /**
127
- * This function is a copy of maplibre-gl-js's util function and rewrite it to JavaScript.
128
- * https://github.com/maplibre/maplibre-gl-js/blob/main/src/util/util.ts#L223-L228
129
- * */
130
- export function bindAll(fns, context) {
131
- fns.forEach((fn) => {
132
- if (!context[fn]) {
133
- return;
134
- }
135
- context[fn] = context[fn].bind(context);
136
- });
137
- }
@@ -1 +0,0 @@
1
- <svg viewBox="0 0 28 38" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd"><ellipse fill-opacity=".3" fill="#000" cx="14" cy="36.5" rx="3" ry="1.5"/><g transform="translate(1 1)"><path d="M26.495 13.018l.002.05c.011.32-.006.775-.068 1.295-.003.062-.003.062-.005.076l.001-.008a16.517 16.517 0 01-.247 1.5c-1.208 5.775-5.112 12.154-12.865 18.457l-.344.28-.319-.309-.537-.518-.621-.603c-.511-.498-.978-.96-1.445-1.434a68.208 68.208 0 01-3.37-3.656C2.894 23.714.411 19.456-.288 15.414A12.974 12.974 0 01-.48 13.43a2.981 2.981 0 00-.007-.132v-.013a3.206 3.206 0 01-.012-.252c0-.055 0-.055.003-.09v-.002l.002-.028-.002-.048a1.288 1.288 0 01-.003-.157l.024-.475h.01C-.05 5.12 5.827-.5 12.998-.5c7.45 0 13.49 6.05 13.497 13.518zm-.07 1.413v.003z" stroke="#FFF" fill="#666" fill-rule="nonzero" class="left"/><path d="M13 0c7.176 0 12.995 5.834 12.995 13.033 0 0 .031.472-.066 1.33v.011a15.863 15.863 0 01-.24 1.455c-.777 3.71-3.463 10.668-12.689 18.17V0z" fill="#2D2D2D" fill-rule="nonzero" class="right"/><circle fill="#FFF" cx="13" cy="13" r="6"/></g></g></svg>
@@ -1,257 +0,0 @@
1
- 'use strict';
2
-
3
- import maplibregl from 'maplibre-gl';
4
- import turfCenter from '@turf/center';
5
- import { sanitizeDescription } from './util';
6
-
7
- const textColor = '#000000';
8
- const textHaloColor = '#FFFFFF';
9
- const backgroundColor = 'rgba(255, 0, 0, 0.4)';
10
- const strokeColor = '#FFFFFF';
11
-
12
- class SimpleStyleVector {
13
- private sourceName: string;
14
-
15
- constructor(private url) {
16
- this.sourceName = 'vt-geolonia-simple-style';
17
- }
18
-
19
- addTo(map) {
20
- const container = map.getContainer();
21
-
22
- if (
23
- !container.dataset ||
24
- (!container.dataset.lng && !container.dataset.lat)
25
- ) {
26
- let initialZoomDone = false;
27
- map.on('sourcedata', (event) => {
28
- // skip events for sources that don't concern us
29
- if (event.sourceId !== this.sourceName) {
30
- return;
31
- }
32
-
33
- const source = map.getSource(event.sourceId);
34
- // query the map to see if the source is actually loaded or not. We can't trust `isSourceLoaded` in the event
35
- // because it's unreliable and often incorrect.
36
- const isLoaded = source && source.loaded();
37
-
38
- if (isLoaded !== true) {
39
- return;
40
- }
41
-
42
- // Only zoom once.
43
- if (initialZoomDone) {
44
- return;
45
- }
46
- initialZoomDone = true;
47
-
48
- map.fitBounds(source.bounds, {
49
- duration: 0,
50
- padding: 30,
51
- });
52
- });
53
- }
54
-
55
- map.addSource(this.sourceName, {
56
- type: 'vector',
57
- url: this.url,
58
- });
59
-
60
- this.setPolygonGeometries(map);
61
- this.setLineGeometries(map);
62
-
63
- map.addLayer({
64
- id: 'vt-geolonia-simple-style-polygon-symbol',
65
- type: 'symbol',
66
- source: this.sourceName,
67
- 'source-layer': 'g-simplestyle-v1',
68
- filter: ['==', '$type', 'Polygon'],
69
- paint: {
70
- 'text-color': ['string', ['get', 'text-color'], textColor],
71
- 'text-halo-color': [
72
- 'string',
73
- ['get', 'text-halo-color'],
74
- textHaloColor,
75
- ],
76
- 'text-halo-width': 1,
77
- },
78
- layout: {
79
- 'text-field': ['get', 'title'],
80
- 'text-font': ['Noto Sans Regular'],
81
- 'text-size': 12,
82
- 'text-max-width': 12,
83
- 'text-allow-overlap': false,
84
- },
85
- });
86
-
87
- map.addLayer({
88
- id: 'vt-geolonia-simple-style-linestring-symbol',
89
- type: 'symbol',
90
- source: this.sourceName,
91
- 'source-layer': 'g-simplestyle-v1',
92
- filter: ['==', '$type', 'LineString'],
93
- paint: {
94
- 'text-color': ['string', ['get', 'text-color'], textColor],
95
- 'text-halo-color': [
96
- 'string',
97
- ['get', 'text-halo-color'],
98
- textHaloColor,
99
- ],
100
- 'text-halo-width': 1,
101
- },
102
- layout: {
103
- 'symbol-placement': 'line',
104
- 'text-field': ['get', 'title'],
105
- 'text-font': ['Noto Sans Regular'],
106
- 'text-size': 12,
107
- 'text-max-width': 12,
108
- 'text-allow-overlap': false,
109
- },
110
- });
111
-
112
- this.setPointGeometries(map);
113
- }
114
-
115
- /**
116
- * Set polygon geometries.
117
- *
118
- * @param map
119
- */
120
- setPolygonGeometries(map) {
121
- map.addLayer({
122
- id: 'vt-geolonia-simple-style-polygon',
123
- type: 'fill',
124
- source: this.sourceName,
125
- 'source-layer': 'g-simplestyle-v1',
126
- filter: ['==', '$type', 'Polygon'],
127
- paint: {
128
- 'fill-color': ['string', ['get', 'fill'], backgroundColor],
129
- 'fill-opacity': ['number', ['get', 'fill-opacity'], 1.0],
130
- 'fill-outline-color': ['string', ['get', 'stroke'], strokeColor],
131
- },
132
- });
133
-
134
- this.setPopup(map, 'vt-geolonia-simple-style-polygon');
135
- }
136
-
137
- /**
138
- * Set line geometries.
139
- *
140
- * @param map
141
- */
142
- setLineGeometries(map) {
143
- map.addLayer({
144
- id: 'vt-geolonia-simple-style-linestring',
145
- type: 'line',
146
- source: this.sourceName,
147
- 'source-layer': 'g-simplestyle-v1',
148
- filter: ['==', '$type', 'LineString'],
149
- paint: {
150
- 'line-width': ['number', ['get', 'stroke-width'], 2],
151
- 'line-color': ['string', ['get', 'stroke'], backgroundColor],
152
- 'line-opacity': ['number', ['get', 'stroke-opacity'], 1.0],
153
- },
154
- layout: {
155
- 'line-cap': 'round',
156
- 'line-join': 'round',
157
- },
158
- });
159
-
160
- this.setPopup(map, 'vt-geolonia-simple-style-linestring');
161
- }
162
-
163
- /**
164
- * Setup point geometries.
165
- *
166
- * @param map
167
- */
168
- setPointGeometries(map) {
169
- map.addLayer({
170
- id: 'vt-circle-simple-style-points',
171
- type: 'circle',
172
- source: this.sourceName,
173
- 'source-layer': 'g-simplestyle-v1',
174
- filter: ['all', ['==', '$type', 'Point'], ['!has', 'marker-symbol']],
175
- paint: {
176
- 'circle-radius': [
177
- 'case',
178
- ['==', 'small', ['get', 'marker-size']],
179
- 7,
180
- ['==', 'large', ['get', 'marker-size']],
181
- 13,
182
- 9,
183
- ],
184
- 'circle-color': ['string', ['get', 'marker-color'], backgroundColor],
185
- 'circle-opacity': ['number', ['get', 'fill-opacity'], 1.0],
186
- 'circle-stroke-width': ['number', ['get', 'stroke-width'], 1],
187
- 'circle-stroke-color': ['string', ['get', 'stroke'], strokeColor],
188
- 'circle-stroke-opacity': ['number', ['get', 'stroke-opacity'], 1.0],
189
- },
190
- });
191
-
192
- map.addLayer({
193
- id: 'vt-geolonia-simple-style-points',
194
- type: 'symbol',
195
- source: this.sourceName,
196
- 'source-layer': 'g-simplestyle-v1',
197
- filter: ['==', '$type', 'Point'],
198
- paint: {
199
- 'text-color': ['string', ['get', 'text-color'], textColor],
200
- 'text-halo-color': [
201
- 'string',
202
- ['get', 'text-halo-color'],
203
- textHaloColor,
204
- ],
205
- 'text-halo-width': 1,
206
- },
207
- layout: {
208
- 'icon-image': ['get', 'marker-symbol'],
209
- 'text-field': ['get', 'title'],
210
- 'text-font': ['Noto Sans Regular'],
211
- 'text-size': 12,
212
- 'text-anchor': 'top',
213
- 'text-max-width': 12,
214
- 'text-offset': [
215
- 'case',
216
- ['==', 'small', ['get', 'marker-size']],
217
- ['literal', [0, 1]],
218
- ['==', 'large', ['get', 'marker-size']],
219
- ['literal', [0, 1.6]],
220
- ['literal', [0, 1.2]],
221
- ],
222
- 'text-allow-overlap': false,
223
- },
224
- });
225
-
226
- this.setPopup(map, 'vt-circle-simple-style-points');
227
- this.setPopup(map, 'vt-geolonia-simple-style-points');
228
- }
229
-
230
- async setPopup(map, source) {
231
- map.on('click', source, async (e) => {
232
- const center: [number, number] = turfCenter(e.features[0]).geometry
233
- .coordinates as [number, number];
234
- const description = e.features[0].properties.description;
235
-
236
- if (description) {
237
- const sanitizedDescription = await sanitizeDescription(description);
238
- new maplibregl.Popup()
239
- .setLngLat(center)
240
- .setHTML(sanitizedDescription)
241
- .addTo(map);
242
- }
243
- });
244
-
245
- map.on('mouseenter', source, (e) => {
246
- if (e.features[0].properties.description) {
247
- map.getCanvas().style.cursor = 'pointer';
248
- }
249
- });
250
-
251
- map.on('mouseleave', source, () => {
252
- map.getCanvas().style.cursor = '';
253
- });
254
- }
255
- }
256
-
257
- export default SimpleStyleVector;