Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ import {
setUISettings,
} from '../../services';
import { initVegaLayer, initTmsRasterLayer } from './layers';
import { Map, NavigationControl, Style } from 'mapbox-gl';

jest.mock('mapbox-gl', () => ({
// @ts-expect-error
import mapboxgl from 'mapbox-gl/dist/mapbox-gl-csp';

jest.mock('mapbox-gl/dist/mapbox-gl-csp', () => ({
setRTLTextPlugin: jest.fn(),
Map: jest.fn().mockImplementation(() => ({
getLayer: () => '',
removeLayer: jest.fn(),
Expand Down Expand Up @@ -75,9 +78,10 @@ describe('vega_map_view/view', () => {
setUISettings(coreStart.uiSettings);

const getTmsService = jest.fn().mockReturnValue(({
getVectorStyleSheet: (): Style => ({
getVectorStyleSheet: () => ({
version: 8,
sources: {},
// @ts-expect-error
layers: [],
}),
getMaxZoom: async () => 20,
Expand Down Expand Up @@ -144,7 +148,7 @@ describe('vega_map_view/view', () => {
await vegaMapView.init();

const { longitude, latitude, scrollWheelZoom } = vegaMapView._parser.mapConfig;
expect(Map).toHaveBeenCalledWith({
expect(mapboxgl.Map).toHaveBeenCalledWith({
style: {
version: 8,
sources: {},
Expand All @@ -170,7 +174,7 @@ describe('vega_map_view/view', () => {
await vegaMapView.init();

const { longitude, latitude, scrollWheelZoom } = vegaMapView._parser.mapConfig;
expect(Map).toHaveBeenCalledWith({
expect(mapboxgl.Map).toHaveBeenCalledWith({
style: {
version: 8,
sources: {},
Expand All @@ -195,7 +199,7 @@ describe('vega_map_view/view', () => {

await vegaMapView.init();

expect(NavigationControl).toHaveBeenCalled();
expect(mapboxgl.NavigationControl).toHaveBeenCalled();
});
});
});
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 type { 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

Copy link
Copy Markdown
Contributor Author

@thomasneirynck thomasneirynck May 11, 2021

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