Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/plugins/vis_type_vega/public/vega_view/vega_map_view/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
*/

import { i18n } from '@kbn/i18n';
import { Map, Style, NavigationControl, MapboxOptions } from 'mapbox-gl';
import { Map, Style, MapboxOptions } from 'mapbox-gl';

import { View, parse } from 'vega';
// @ts-expect-error
import mapboxgl from 'mapbox-gl/dist/mapbox-gl-csp';
import { initTmsRasterLayer, initVegaLayer } from './layers';
import { VegaBaseView } from '../vega_base_view';
import { getMapServiceSettings } from '../../services';
Expand All @@ -22,11 +24,17 @@ import {
userConfiguredLayerId,
vegaLayerId,
} from './constants';

import { validateZoomSettings, injectMapPropsIntoSpec } from './utils';

import './vega_map_view.scss';

// @ts-expect-error
import mbRtlPlugin from '!!file-loader!@mapbox/mapbox-gl-rtl-text/mapbox-gl-rtl-text.min.js';
// @ts-expect-error
import mbWorkerUrl from '!!file-loader!mapbox-gl/dist/mapbox-gl-csp-worker';

mapboxgl.workerUrl = mbWorkerUrl;
mapboxgl.setRTLTextPlugin(mbRtlPlugin);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thomasneirynck can you give me more info about this worker? Why are we adding this? Sorry, I don't have a lot of experience with mapbox :D

@thomasneirynck thomasneirynck May 11, 2021

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @stratoula, this bring vegamaps in line with how mapbox maps are instantiated in Maps (

// @ts-expect-error
import mbRtlPlugin from '!!file-loader!@mapbox/mapbox-gl-rtl-text/mapbox-gl-rtl-text.min.js';
// @ts-expect-error
import mbWorkerUrl from '!!file-loader!mapbox-gl/dist/mapbox-gl-csp-worker';
mapboxgl.workerUrl = mbWorkerUrl;
mapboxgl.setRTLTextPlugin(mbRtlPlugin);
)

These workers are spawned by mapbox-gl.

This PR changes Vegamaps, so it constructs mapbox with a static-worker script (pointing explicitly to mbWorkerUrl), iso. an inlined string which is the default way mapbox instantiates its workers.

The latter approach was hitting on some possible violations of Kibana's CSP. See #51675.

Apart from that, construction is now the same between Maps and Vega, so it would also give us a pathway to extract mapbox-gl into a separate bundle, reducing footprint.


async function updateVegaView(mapBoxInstance: Map, vegaView: View) {
const mapCanvas = mapBoxInstance.getCanvas();
const { lat, lng } = mapBoxInstance.getCenter();
Expand Down Expand Up @@ -115,7 +123,7 @@ export class VegaMapView extends VegaBaseView {
// In some cases, Vega may be initialized twice, e.g. after awaiting...
if (!this._$container) return;

const mapBoxInstance = new Map({
const mapBoxInstance = new mapboxgl.Map({
style,
customAttribution,
container: this._$container.get(0),
Expand All @@ -142,7 +150,7 @@ export class VegaMapView extends VegaBaseView {

private initControls(mapBoxInstance: Map) {
if (this.shouldShowZoomControl) {
mapBoxInstance.addControl(new NavigationControl({ showCompass: false }), 'top-left');
mapBoxInstance.addControl(new mapboxgl.NavigationControl({ showCompass: false }), 'top-left');
}

// disable map rotation using right click + drag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

import React from 'react';
import 'mapbox-gl/dist/mapbox-gl.css';
import _ from 'lodash';
import { i18n } from '@kbn/i18n';
import { AppLeaveAction, AppMountParameters } from 'kibana/public';
Expand Down