Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into kbn-84763-route-han…
Browse files Browse the repository at this point in the history
…dler-context
  • Loading branch information
pgayvallet committed Apr 21, 2022
2 parents c9941df + a3fd86c commit b483dcc
Show file tree
Hide file tree
Showing 108 changed files with 1,835 additions and 1,078 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,10 @@ module.exports = {
},
],
'react-hooks/rules-of-hooks': 'error', // Checks rules of Hooks
'react-hooks/exhaustive-deps': ['error', { additionalHooks: '^useFetcher$' }],
'react-hooks/exhaustive-deps': [
'error',
{ additionalHooks: '^(useFetcher|useProgressiveFetcher)$' },
],
},
},
{
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
"@elastic/charts": "46.0.1",
"@elastic/datemath": "5.0.3",
"@elastic/elasticsearch": "npm:@elastic/[email protected]",
"@elastic/ems-client": "8.2.0",
"@elastic/ems-client": "8.3.0",
"@elastic/eui": "54.0.0",
"@elastic/filesaver": "1.1.2",
"@elastic/node-crypto": "1.2.1",
Expand Down Expand Up @@ -307,7 +307,7 @@
"lru-cache": "^4.1.5",
"lz-string": "^1.4.4",
"mapbox-gl-draw-rectangle-mode": "1.0.4",
"maplibre-gl": "1.15.2",
"maplibre-gl": "2.1.9",
"markdown-it": "^12.3.2",
"md5": "^2.1.0",
"mdast-util-to-hast": "10.0.1",
Expand Down
47 changes: 26 additions & 21 deletions packages/kbn-mapbox-gl/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,56 @@

import type {
Map,
LayerSpecification,
Source,
GeoJSONSource,
VectorSource,
Layer,
AnyLayer,
FeatureIdentifier,
Style,
MapboxOptions,
VectorTileSource,
StyleSpecification,
MapEvent,
MapOptions,
MapMouseEvent,
MapSourceDataEvent,
LngLat,
LngLatBounds,
Point2D,
PointLike,
MapboxGeoJSONFeature,
Point,
MapGeoJSONFeature,
CustomLayerInterface,
FilterSpecification,
FeatureIdentifier,
} from 'maplibre-gl';

// @ts-expect-error
import mapboxglDist from 'maplibre-gl/dist/maplibre-gl-csp';
import maplibreglDist from 'maplibre-gl/dist/maplibre-gl-csp';
// @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!maplibre-gl/dist/maplibre-gl-csp-worker';
import 'maplibre-gl/dist/maplibre-gl.css';

const mapboxgl: any = mapboxglDist;
mapboxgl.workerUrl = mbWorkerUrl;
mapboxgl.setRTLTextPlugin(mbRtlPlugin);
const maplibregl: any = maplibreglDist;
maplibregl.workerUrl = mbWorkerUrl;
maplibregl.setRTLTextPlugin(mbRtlPlugin);

export { mapboxgl };
export { maplibregl };

export type {
Map,
LayerSpecification,
StyleSpecification,
Source,
GeoJSONSource,
VectorSource,
Layer,
AnyLayer,
FeatureIdentifier,
Style,
MapboxOptions,
VectorTileSource,
MapEvent,
MapOptions,
MapMouseEvent,
MapSourceDataEvent,
LngLat,
LngLatBounds,
Point2D,
PointLike,
MapboxGeoJSONFeature,
Point,
MapGeoJSONFeature,
CustomLayerInterface,
FilterSpecification,
FeatureIdentifier,
};
5 changes: 5 additions & 0 deletions src/core/types/elasticsearch/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,11 @@ export type AggregateOf<
reverse_nested: {
doc_count: number;
} & SubAggregateOf<TAggregationContainer, TDocument>;
random_sampler: {
seed: number;
probability: number;
doc_count: number;
} & SubAggregateOf<TAggregationContainer, TDocument>;
sampler: {
doc_count: number;
} & SubAggregateOf<TAggregationContainer, TDocument>;
Expand Down
2 changes: 1 addition & 1 deletion src/dev/license_checker/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const DEV_ONLY_LICENSE_ALLOWED = ['MPL-2.0'];
export const LICENSE_OVERRIDES = {
'[email protected]': ['Eclipse Distribution License - v 1.0'], // cf. https://github.com/bjornharrtell/jsts
'@mapbox/[email protected]': ['MIT'], // license in readme https://github.com/tmcw/jsonlint
'@elastic/ems-client@8.2.0': ['Elastic License 2.0'],
'@elastic/ems-client@8.3.0': ['Elastic License 2.0'],
'@elastic/[email protected]': ['SSPL-1.0 OR Elastic License 2.0'],
'[email protected]': ['CC-BY-4.0'], // retired ODC‑By license https://github.com/mattcg/language-subtag-registry
};
78 changes: 74 additions & 4 deletions src/plugins/console/public/lib/autocomplete/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ import { i18n } from '@kbn/i18n';

// TODO: All of these imports need to be moved to the core editor so that it can inject components from there.
import {
getTopLevelUrlCompleteComponents,
getEndpointBodyCompleteComponents,
getGlobalAutocompleteComponents,
getTopLevelUrlCompleteComponents,
getUnmatchedEndpointComponents,
// @ts-ignore
} from '../kb/kb';

import { createTokenIterator } from '../../application/factories';
import { Position, Token, Range, CoreEditor } from '../../types';
import type { CoreEditor, Position, Range, Token } from '../../types';
import type RowParser from '../row_parser';

import * as utils from '../utils';

// @ts-ignore
import { populateContext } from './engine';
import { AutoCompleteContext, ResultTerm } from './types';
import type { AutoCompleteContext, DataAutoCompleteRulesOneOf, ResultTerm } from './types';
// @ts-ignore
import { URL_PATH_END_MARKER } from './components';

Expand Down Expand Up @@ -349,14 +349,84 @@ export default function ({
});
}

/**
* Get a different set of templates based on the value configured in the request.
* For example, when creating a snapshot repository of different types (`fs`, `url` etc),
* different properties are inserted in the textarea based on the type.
* E.g. https://github.com/elastic/kibana/blob/main/src/plugins/console/server/lib/spec_definitions/json/overrides/snapshot.create_repository.json
*/
function getConditionalTemplate(
name: string,
autocompleteRules: Record<string, unknown> | null | undefined
) {
const obj = autocompleteRules && autocompleteRules[name];

if (obj) {
const currentLineNumber = editor.getCurrentPosition().lineNumber;

if (hasOneOfIn(obj)) {
// Get the line number of value that should provide different templates based on that
const startLine = getStartLineNumber(currentLineNumber, obj.__one_of);
// Join line values from start to current line
const lines = editor.getLines(startLine, currentLineNumber).join('\n');
// Get the correct template by comparing the autocomplete rules against the lines
const prop = getProperty(lines, obj.__one_of);
if (prop && prop.__template) {
return prop.__template;
}
}
}
}

/**
* Check if object has a property of '__one_of'
*/
function hasOneOfIn(value: unknown): value is { __one_of: DataAutoCompleteRulesOneOf[] } {
return typeof value === 'object' && value !== null && '__one_of' in value;
}

/**
* Get the start line of value that matches the autocomplete rules condition
*/
function getStartLineNumber(currentLine: number, rules: DataAutoCompleteRulesOneOf[]): number {
if (currentLine === 1) {
return currentLine;
}
const value = editor.getLineValue(currentLine);
const prop = getProperty(value, rules);
if (prop) {
return currentLine;
}
return getStartLineNumber(currentLine - 1, rules);
}

/**
* Get the matching property based on the given condition
*/
function getProperty(condition: string, rules: DataAutoCompleteRulesOneOf[]) {
return rules.find((rule) => {
if (rule.__condition && rule.__condition.lines_regex) {
return new RegExp(rule.__condition.lines_regex, 'm').test(condition);
}
return false;
});
}

function applyTerm(term: {
value?: string;
context?: AutoCompleteContext;
template?: { __raw: boolean; value: string };
template?: { __raw?: boolean; value?: string; [key: string]: unknown };
insertValue?: string;
}) {
const context = term.context!;

if (context?.endpoint && term.value) {
const { data_autocomplete_rules: autocompleteRules } = context.endpoint;
const template = getConditionalTemplate(term.value, autocompleteRules);
if (template) {
term.template = template;
}
}
// make sure we get up to date replacement info.
addReplacementInfoToContext(context, editor.getCurrentPosition(), term.insertValue);

Expand Down
9 changes: 9 additions & 0 deletions src/plugins/console/public/lib/autocomplete/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ export interface ResultTerm {
value?: string;
}

export interface DataAutoCompleteRulesOneOf {
__condition?: {
lines_regex: string;
};
__template: Record<string, unknown>;
[key: string]: unknown;
}

export interface AutoCompleteContext {
autoCompleteSet?: null | ResultTerm[];
endpoint?: null | {
Expand All @@ -24,6 +32,7 @@ export interface AutoCompleteContext {
bodyAutocompleteRootComponents: unknown;
id?: string;
documentation?: string;
data_autocomplete_rules?: Record<string, unknown> | null;
};
urlPath?: null | unknown;
urlParamsTokenPath?: Array<Record<string, string>> | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import type { Style } from '@kbn/mapbox-gl';
import type { StyleSpecification } from '@kbn/mapbox-gl';

export const vegaLayerId = 'vega';
export const defaultMapConfig = {
Expand All @@ -15,7 +15,7 @@ export const defaultMapConfig = {
tileSize: 256,
};

export const defaultMabBoxStyle: Style = {
export const defaultMabBoxStyle: StyleSpecification = {
/**
* according to the MapBox documentation that value should be '8'
* @see (https://docs.mapbox.com/mapbox-gl-js/style-spec/root/#version)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ import {
} from '../../services';
import { initVegaLayer, initTmsRasterLayer } from './layers';

import { mapboxgl } from '@kbn/mapbox-gl';
import { maplibregl } from '@kbn/mapbox-gl';

jest.mock('@kbn/mapbox-gl', () => {
const zoomTo = jest.fn();
const setCenter = jest.fn();
const fitBounds = jest.fn();
return {
mapboxgl: {
maplibregl: {
mocks: {
zoomTo,
setCenter,
Expand Down Expand Up @@ -171,7 +171,7 @@ describe('vega_map_view/view', () => {
await vegaMapView.init();

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

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

expect(mapboxgl.NavigationControl).toHaveBeenCalled();
expect(maplibregl.NavigationControl).toHaveBeenCalled();
});

describe('setMapView', () => {
let vegaMapView: VegaMapView;
beforeEach(async () => {
vegaMapView = await createVegaMapView();
await vegaMapView.init();
mapboxgl.mocks.setCenter.mockReset();
mapboxgl.mocks.zoomTo.mockReset();
mapboxgl.mocks.fitBounds.mockReset();
maplibregl.mocks.setCenter.mockReset();
maplibregl.mocks.zoomTo.mockReset();
maplibregl.mocks.fitBounds.mockReset();
});

test('should set just lat lng', async () => {
vegaMapView.setMapViewHandler(1, 2);
expect(mapboxgl.mocks.setCenter).toHaveBeenCalledWith({ lat: 1, lng: 2 });
expect(maplibregl.mocks.setCenter).toHaveBeenCalledWith({ lat: 1, lng: 2 });
});

test('should set just lng lat via array', async () => {
vegaMapView.setMapViewHandler([1, 2]);
expect(mapboxgl.mocks.setCenter).toHaveBeenCalledWith({ lat: 2, lng: 1 });
expect(maplibregl.mocks.setCenter).toHaveBeenCalledWith({ lat: 2, lng: 1 });
});

test('should set lat lng and zoom', async () => {
vegaMapView.setMapViewHandler(1, 2, 6);
expect(mapboxgl.mocks.setCenter).toHaveBeenCalledWith({ lat: 1, lng: 2 });
expect(mapboxgl.mocks.zoomTo).toHaveBeenCalledWith(6);
expect(maplibregl.mocks.setCenter).toHaveBeenCalledWith({ lat: 1, lng: 2 });
expect(maplibregl.mocks.zoomTo).toHaveBeenCalledWith(6);
});

test('should set bounds', async () => {
vegaMapView.setMapViewHandler([
[1, 2],
[6, 7],
]);
expect(mapboxgl.mocks.fitBounds).toHaveBeenCalledWith([
expect(maplibregl.mocks.fitBounds).toHaveBeenCalledWith([
{ lat: 2, lng: 1 },
{ lat: 7, lng: 6 },
]);
Expand Down
Loading

0 comments on commit b483dcc

Please sign in to comment.