Skip to content

Commit 51cec5c

Browse files
committed
pass iframe window instead of mapbox-specific instance
1 parent 34b7ff1 commit 51cec5c

5 files changed

Lines changed: 41 additions & 32 deletions

File tree

packages/pages-components/src/components/map/map.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,7 @@ export const Map = ({
8484

8585
const iframeWindow = typeof document !== "undefined"
8686
? (document.getElementById("preview-frame") as HTMLIFrameElement).contentWindow ?? undefined
87-
: undefined
88-
const mapboxInstance = (iframeWindow as Window & { mapboxgl?: typeof mapboxgl })?.mapboxgl ?? mapboxgl;
89-
mapboxInstance.accessToken = apiKey ?? "";
87+
: undefined;
9088

9189
const newMap = new MapOptions()
9290
.withControlEnabled(controls)
@@ -99,7 +97,8 @@ export const Map = ({
9997
.withProviderOptions(providerOptions)
10098
.withSinglePinZoom(singleZoom)
10199
.withWrapper(wrapper.current)
102-
.withInstance(mapboxInstance)
100+
.withIframeWindow(iframeWindow)
101+
.withApiKey(apiKey)
103102
.build();
104103

105104
setMap(newMap);

packages/pages-components/src/map/map.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ class MapOptions {
6060
providerOptions: ProviderMapOptions | object;
6161
singlePinZoom: number;
6262
wrapper: HTMLElement | null;
63-
instance: typeof mapboxgl;
63+
iframeWindow: Window | undefined;
64+
apiKey: string | undefined;
6465
/**
6566
* Initialize with default options
6667
*/
@@ -81,7 +82,6 @@ class MapOptions {
8182
this.providerOptions = {};
8283
this.singlePinZoom = 14;
8384
this.wrapper = null;
84-
this.instance = mapboxgl;
8585
}
8686

8787
/**
@@ -192,8 +192,20 @@ class MapOptions {
192192
return this;
193193
}
194194

195-
withInstance(instance: typeof mapboxgl): MapOptions {
196-
this.instance = instance;
195+
/**
196+
* @param iframeWindow - The window of the iframe that the map will be rendered in.
197+
* This is only necessary if the map is being rendered in an iframe.
198+
*/
199+
withIframeWindow(iframeWindow: Window | undefined): MapOptions {
200+
this.iframeWindow = iframeWindow;
201+
return this;
202+
}
203+
204+
/**
205+
* @param apiKey - The API key to use for the map provider, if required.
206+
*/
207+
withApiKey(apiKey: string | undefined): MapOptions {
208+
this.apiKey = apiKey;
197209
return this;
198210
}
199211

@@ -230,7 +242,6 @@ class Map {
230242
_panHandler?: PanHandler;
231243
_panStartHandler?: PanStartHandler;
232244
_map: ProviderMap;
233-
_instance: typeof mapboxgl;
234245

235246
constructor(options: MapOptions) {
236247
assertInstance(options, MapOptions);
@@ -279,13 +290,12 @@ class Map {
279290
.withPanHandler(() => this.panHandler())
280291
.withPanStartHandler(() => this.panStartHandler())
281292
.withProviderOptions(options.providerOptions)
282-
.withInstance(options.instance)
293+
.withIframeWindow(options.iframeWindow)
294+
.withApiKey(options.apiKey)
283295
.build();
284296

285297
this.setZoomCenter(this._defaultZoom, this._defaultCenter);
286298
this._currentBounds = this.getBounds();
287-
288-
this._instance = options.instance;
289299
}
290300

291301
/**
@@ -401,7 +411,7 @@ class Map {
401411
* instance with the same provider as this map
402412
*/
403413
newPinOptions(): MapPinOptions {
404-
return new MapPinOptions().withProvider(this._provider).withInstance(this._instance);
414+
return new MapPinOptions().withProvider(this._provider);
405415
}
406416

407417
/**

packages/pages-components/src/map/mapPin.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class MapPinOptions {
2424
provider: MapProvider | null;
2525
type: string;
2626
hasPinUrl: boolean;
27-
instance: typeof mapboxgl;
2827

2928
constructor() {
3029
this.coordinate = new Coordinate(0, 0);
@@ -34,7 +33,6 @@ class MapPinOptions {
3433
this.provider = null;
3534
this.type = "";
3635
this.hasPinUrl = false;
37-
this.instance = mapboxgl;
3836
}
3937

4038
/**
@@ -97,14 +95,6 @@ class MapPinOptions {
9795
return this;
9896
}
9997

100-
/**
101-
* @param instance - The mapboxgl instance to use, in case it's loaded in an iframe
102-
*/
103-
withInstance(instance: typeof mapboxgl): MapPinOptions {
104-
this.instance = instance;
105-
return this;
106-
}
107-
10898
build(): MapPin {
10999
return new MapPin(this);
110100
}
@@ -130,7 +120,6 @@ class MapPin {
130120
_map: Map | null;
131121
_pin: ProviderPin;
132122
_status: object;
133-
_instance: typeof mapboxgl;
134123

135124
constructor(options: MapPinOptions) {
136125
assertInstance(options, MapPinOptions);
@@ -165,15 +154,12 @@ class MapPin {
165154
.withFocusHandler((focused: boolean) => this._focusHandler(focused))
166155
.withHoverHandler((hovered: boolean) => this._hoverHandler(hovered))
167156
.withHasPinUrl(options.hasPinUrl)
168-
.withInstance(options.instance)
169157
.build();
170158

171159
this._pin.setCoordinate(options.coordinate);
172160

173161
this._status = {};
174162
this.setStatus(this._status);
175-
176-
this._instance = options.instance;
177163
}
178164

179165
/**

packages/pages-components/src/map/providerMap.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ class ProviderMapOptions {
1616
panHandler: PanHandler;
1717
panStartHandler: PanStartHandler;
1818
providerOptions: { [key: string]: any };
19-
instance: typeof mapboxgl;
19+
iframeWindow: Window | undefined;
20+
apiKey: string | undefined;
2021

2122
constructor(provider: MapProvider, wrapper: HTMLElement | null) {
2223
assertInstance(provider, MapProvider);
@@ -31,7 +32,6 @@ class ProviderMapOptions {
3132
this.panHandler = () => null;
3233
this.panStartHandler = () => null;
3334
this.providerOptions = {};
34-
this.instance = mapboxgl;
3535
}
3636

3737
/**
@@ -71,8 +71,20 @@ class ProviderMapOptions {
7171
return this;
7272
}
7373

74-
withInstance(instance: typeof mapboxgl): ProviderMapOptions {
75-
this.instance = instance;
74+
/**
75+
* @param iframeWindow - The window of the iframe that the map will be rendered in.
76+
* This is only necessary if the map is being rendered in an iframe.
77+
*/
78+
withIframeWindow(iframeWindow: Window | undefined): ProviderMapOptions {
79+
this.iframeWindow = iframeWindow;
80+
return this;
81+
}
82+
83+
/**
84+
* @param apiKey - The API key to use for the map provider, if required.
85+
*/
86+
withApiKey(apiKey: string | undefined): ProviderMapOptions {
87+
this.apiKey = apiKey;
7688
return this;
7789
}
7890

packages/pages-components/src/map/providers/mapbox.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ class MapboxMap extends ProviderMap {
1616
constructor(options: ProviderMapOptions) {
1717
super(options);
1818

19-
this.instance = options.instance;
19+
const mapboxInstance = (options?.iframeWindow as Window & { mapboxgl?: typeof mapboxgl })?.mapboxgl ?? mapboxgl;
20+
mapboxInstance.accessToken = options?.apiKey ?? "";
21+
this.instance = mapboxInstance;
2022

2123
if (options.wrapper) {
2224
this.map = new this.instance.Map({

0 commit comments

Comments
 (0)