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 @@ -5,14 +5,14 @@
* 2.0.
*/

import type { MapSerializedState } from '@kbn/maps-plugin/public';
import type { MapByReferenceState, MapEmbeddableState } from '@kbn/maps-plugin/common';

export function toExpression(input: MapSerializedState & { id: string }): string {
export function toExpression(input: MapEmbeddableState & { id: string }): string {
const expressionParts = [] as string[];

expressionParts.push('savedMap');

expressionParts.push(`id="${input.savedObjectId}"`);
expressionParts.push(`id="${(input as MapByReferenceState).savedObjectId}"`);

if (input.title !== undefined) {
expressionParts.push(`title="${input.title}"`);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type { EnhancementsRegistry } from '@kbn/embeddable-plugin/common/enhancements/registry';
import type { Reference } from '@kbn/content-management-utils';
import type { MapByReferenceState, MapByValueState, MapEmbeddableState } from '../types';
import type { StoredMapEmbeddableState } from './types';
import { MAP_SAVED_OBJECT_TYPE } from '../../constants';
import { extractReferences } from '../../migrations/references';

export const MAP_SAVED_OBJECT_REF_NAME = 'savedObjectRef';

export function getTransformIn(transformEnhancementsIn: EnhancementsRegistry['transformIn']) {
function transformIn(state: MapEmbeddableState): {
state: StoredMapEmbeddableState;
references: Reference[];
} {
const { enhancementsState, enhancementsReferences } = state.enhancements
? transformEnhancementsIn(state.enhancements)
: { enhancementsState: undefined, enhancementsReferences: [] };

// by ref
if ((state as MapByReferenceState).savedObjectId) {
const { savedObjectId, ...rest } = state as MapByReferenceState;
return {
state: {
...rest,
...(enhancementsState ? { enhancements: enhancementsState } : {}),
} as StoredMapEmbeddableState,
references: [
{
name: MAP_SAVED_OBJECT_REF_NAME,
Copy link
Contributor

Choose a reason for hiding this comment

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

This is looking really clear!

type: MAP_SAVED_OBJECT_TYPE,
id: savedObjectId!,
},
...enhancementsReferences,
],
};
}

// by value
if ((state as MapByValueState).attributes) {
const { attributes, references } = extractReferences({
attributes: (state as MapByValueState).attributes,
});

return {
state: {
...state,
...(enhancementsState ? { enhancements: enhancementsState } : {}),
attributes,
} as MapByValueState,
references: [...references, ...enhancementsReferences],
};
}

throw new Error('Unable to extract references from Map state, unexpected state');
}
return transformIn;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type { Reference } from '@kbn/content-management-utils/src/types';
import type { EnhancementsRegistry } from '@kbn/embeddable-plugin/common/enhancements/registry';
import type { StoredMapEmbeddableState } from './types';
import { MAP_SAVED_OBJECT_REF_NAME } from './get_transform_in';
import type { MapByValueState } from '../types';
import { injectReferences } from '../../migrations/references';
import { MAP_SAVED_OBJECT_TYPE } from '../../constants';

export function getTransformOut(transformEnhancementsOut: EnhancementsRegistry['transformOut']) {
function transformOut(state: StoredMapEmbeddableState, references?: Reference[]) {
const enhancementsState = state.enhancements
? transformEnhancementsOut(state.enhancements, references ?? [])
: undefined;

// by ref
const savedObjectRef = (references ?? []).find(
(ref) => MAP_SAVED_OBJECT_TYPE === ref.type && ref.name === MAP_SAVED_OBJECT_REF_NAME
);
if (savedObjectRef) {
return {
...state,
...(enhancementsState ? { enhancements: enhancementsState } : {}),
Copy link
Contributor

Choose a reason for hiding this comment

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

It could be neat if there was a one-step way to do this.

savedObjectId: savedObjectRef.id,
};
}

// by value
if ((state as MapByValueState).attributes) {
return {
...state,
...(enhancementsState ? { enhancements: enhancementsState } : {}),
attributes: injectReferences({
attributes: (state as MapByValueState).attributes,
references: references ?? [],
}).attributes,
};
}

throw new Error('Unable to inject references from Map state, unexpected state');
}
return transformOut;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type { EnhancementsRegistry } from '@kbn/embeddable-plugin/common/enhancements/registry';
import { getTransformIn } from './get_transform_in';
import { getTransformOut } from './get_transform_out';

export function getTransforms(
transformEnhancementsIn: EnhancementsRegistry['transformIn'],
transformEnhancementsOut: EnhancementsRegistry['transformOut']
) {
return {
transformIn: getTransformIn(transformEnhancementsIn),
transformOut: getTransformOut(transformEnhancementsOut),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* 2.0.
*/

export type { MapEmbeddablePersistableState } from './types';
export { extract } from './extract';
export { inject } from './inject';
import type { MapByReferenceState, MapByValueState } from '../types';

export type StoredMapByReferenceState = Omit<MapByReferenceState, 'savedObjectId'>;

export type StoredMapEmbeddableState = StoredMapByReferenceState | MapByValueState;
29 changes: 25 additions & 4 deletions x-pack/platform/plugins/shared/maps/common/embeddable/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,30 @@
* 2.0.
*/

import type { SerializableRecord } from '@kbn/utility-types';
import type { EmbeddableStateWithType } from '@kbn/embeddable-plugin/common';
import type { DynamicActionsSerializedState } from '@kbn/embeddable-enhanced-plugin/public';
import type { SerializedTimeRange, SerializedTitles } from '@kbn/presentation-publishing';
import type { MapCenterAndZoom, MapExtent, MapSettings } from '../descriptor_types';
import type { MapAttributes } from '../content_management';

export type MapEmbeddablePersistableState = EmbeddableStateWithType & {
attributes: SerializableRecord;
export type MapEmbeddableBaseState = SerializedTimeRange &
SerializedTitles &
Partial<DynamicActionsSerializedState> & {
isLayerTOCOpen?: boolean;
openTOCDetails?: string[];
mapCenter?: MapCenterAndZoom;
mapBuffer?: MapExtent;
mapSettings?: Partial<MapSettings>;
hiddenLayers?: string[];
filterByMapExtent?: boolean;
isMovementSynchronized?: boolean;
};

export type MapByReferenceState = MapEmbeddableBaseState & {
savedObjectId: string;
};

export type MapByValueState = MapEmbeddableBaseState & {
attributes: MapAttributes;
};

export type MapEmbeddableState = MapByReferenceState | MapByValueState;
2 changes: 2 additions & 0 deletions x-pack/platform/plugins/shared/maps/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ export type {
VectorStyleDescriptor,
VectorSourceRequestMeta,
} from './descriptor_types';

export type { MapEmbeddableState, MapByReferenceState, MapByValueState } from './embeddable/types';
2 changes: 1 addition & 1 deletion x-pack/platform/plugins/shared/maps/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export type {
export type { MapsSetupApi, MapsStartApi } from './api';
export type { CreateLayerDescriptorParams } from './classes/sources/es_search_source/create_layer_descriptor';

export { type MapApi, type MapSerializedState, isMapApi } from './react_embeddable/types';
export { type MapApi, isMapApi } from './react_embeddable/types';

export type { EMSTermJoinConfig, SampleValuesConfig } from './ems_autosuggest';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import useMountedState from 'react-use/lib/useMountedState';
import type { Subscription } from 'rxjs';
import { EmbeddableRenderer } from '@kbn/embeddable-plugin/public';
import type { LayerDescriptor } from '../../common/descriptor_types';
import type { MapEmbeddableState } from '../../common';
import { INITIAL_LOCATION, MAP_SAVED_OBJECT_TYPE } from '../../common';
import { createBasemapLayerDescriptor } from '../classes/layers/create_basemap_layer_descriptor';
import type { MapApi, MapSerializedState } from '../react_embeddable/types';
import type { MapApi } from '../react_embeddable/types';

export interface Props {
passiveLayer: LayerDescriptor;
Expand Down Expand Up @@ -50,7 +51,7 @@ export function PassiveMap(props: Props) {

return (
<div className="mapEmbeddableContainer">
<EmbeddableRenderer<MapSerializedState, MapApi>
<EmbeddableRenderer<MapEmbeddableState, MapApi>
type={MAP_SAVED_OBJECT_TYPE}
getParentApi={() => ({
hideFilterActions: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
export { MapRenderer } from './map_renderer/map_renderer';
export { PassiveMap } from '../lens/passive_map';
export { mapEmbeddableFactory } from './map_react_embeddable';
export { getTransforms } from '../../common/embeddable/transforms/get_transforms';
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ import { setGotoWithCenter, setMapSettings } from '../actions';
import type { MapExtent } from '../../common/descriptor_types';
import { getUiActions } from '../kibana_services';
import { getGeoFieldsLabel } from './get_geo_fields_label';
import type { MapApi, MapSerializedState } from './types';
import type { MapApi } from './types';
import { setOnMapMove } from '../reducers/non_serializable_instances';
import type { MapEmbeddableState } from '../../common';

export const crossPanelActionsComparators: StateComparators<
Pick<MapSerializedState, 'isMovementSynchronized' | 'filterByMapExtent'>
Pick<MapEmbeddableState, 'isMovementSynchronized' | 'filterByMapExtent'>
> = {
isMovementSynchronized: 'referenceEquality',
filterByMapExtent: 'referenceEquality',
Expand All @@ -50,7 +51,7 @@ export function initializeCrossPanelActions({
getActionContext: () => ActionExecutionContext;
getApi: () => MapApi | undefined;
savedMap: SavedMap;
state: MapSerializedState;
state: MapEmbeddableState;
uuid: string;
}) {
const isMovementSynchronized$ = new BehaviorSubject<boolean | undefined>(
Expand Down Expand Up @@ -225,7 +226,7 @@ export function initializeCrossPanelActions({
anyStateChange$: merge(isMovementSynchronized$, isFilterByMapExtent$).pipe(
map(() => undefined)
),
reinitializeState: (lastSaved: MapSerializedState) => {
reinitializeState: (lastSaved: MapEmbeddableState) => {
setIsMovementSynchronized(lastSaved.isMovementSynchronized);
setIsFilterByMapExtent(lastSaved.filterByMapExtent);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import { apiHasAppContext } from '@kbn/presentation-publishing';
import { APP_ID, getEditPath, getFullPath, MAP_EMBEDDABLE_NAME } from '../../common/constants';
import { getEmbeddableService, getHttp, getMapsCapabilities } from '../kibana_services';
import type { MapSerializedState } from './types';
import type { MapEmbeddableState } from '../../common';

export function initializeEditApi(
uuid: string,
getState: () => MapSerializedState,
getState: () => MapEmbeddableState,
parentApi?: unknown,
savedObjectId?: string
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
setQuery,
setReadOnly,
} from '../actions';
import type { MapSerializedState } from './types';
import type { MapByReferenceState, MapEmbeddableState } from '../../common';
import { getCharts, getExecutionContextService } from '../kibana_services';
import type { EventHandlers } from '../reducers/non_serializable_instances';
import {
Expand All @@ -57,7 +57,7 @@ function getHiddenLayerIds(state: MapStoreState) {

export const reduxSyncComparators: StateComparators<
Pick<
MapSerializedState,
MapEmbeddableState,
'hiddenLayers' | 'isLayerTOCOpen' | 'mapCenter' | 'mapBuffer' | 'openTOCDetails'
>
> = {
Expand All @@ -84,7 +84,7 @@ export function initializeReduxSync({
uuid,
}: {
savedMap: SavedMap;
state: MapSerializedState;
state: MapEmbeddableState;
syncColors$?: PublishingSubject<boolean | undefined>;
uuid: string;
}) {
Expand Down Expand Up @@ -143,7 +143,9 @@ export function initializeReduxSync({
showTimesliderToggleButton: false,
})
);
store.dispatch(setExecutionContext(getExecutionContext(uuid, state.savedObjectId)));
store.dispatch(
setExecutionContext(getExecutionContext(uuid, (state as MapByReferenceState).savedObjectId))
);

const filters$ = new BehaviorSubject<Filter[] | undefined>(undefined);
const query$ = new BehaviorSubject<AggregateQuery | Query | undefined>(undefined);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@
*/

import type { HasLibraryTransforms, SerializedPanelState } from '@kbn/presentation-publishing';
import { extractReferences } from '../../common/migrations/references';
import { getCore, getCoreOverlays } from '../kibana_services';
import type { MapAttributes } from '../../common/content_management';
import { checkForDuplicateTitle, getMapClient } from '../content_management';
import { MAP_EMBEDDABLE_NAME } from '../../common/constants';
import type { MapSerializedState } from './types';
import type { MapByValueState, MapByReferenceState, MapEmbeddableState } from '../../common';

export function getByReferenceState(state: MapSerializedState | undefined, savedObjectId: string) {
const { attributes, ...byRefState } = state ?? {};
export function getByReferenceState(state: MapEmbeddableState | undefined, savedObjectId: string) {
const { attributes, ...byRefState } = (state as MapByValueState) ?? {};
return {
...byRefState,
savedObjectId,
};
}

export function getByValueState(state: MapSerializedState | undefined, attributes: MapAttributes) {
const { savedObjectId, ...byValueState } = state ?? {};
export function getByValueState(state: MapEmbeddableState | undefined, attributes: MapAttributes) {
const { savedObjectId, ...byValueState } = (state as MapByReferenceState) ?? {};
return {
...byValueState,
attributes,
Expand All @@ -30,9 +31,9 @@ export function getByValueState(state: MapSerializedState | undefined, attribute

export function initializeLibraryTransforms(
isByReference: boolean,
serializeByReference: (libraryId: string) => SerializedPanelState<MapSerializedState>,
serializeByValue: () => SerializedPanelState<MapSerializedState>
): HasLibraryTransforms<MapSerializedState, MapSerializedState> {
serializeByReference: (libraryId: string) => SerializedPanelState<MapByReferenceState>,
serializeByValue: () => SerializedPanelState<MapByValueState>
): HasLibraryTransforms<MapByReferenceState, MapByValueState> {
return {
canLinkToLibrary: async () => {
const { maps_v2: maps } = getCore().application.capabilities;
Expand All @@ -43,14 +44,17 @@ export function initializeLibraryTransforms(
},
saveToLibrary: async (title: string) => {
const state = serializeByValue();
const { attributes, references } = extractReferences({
attributes: state.rawState.attributes,
});
const {
item: { id: savedObjectId },
} = await getMapClient().create({
data: {
...(state.rawState?.attributes ?? {}),
...attributes,
title,
},
options: { references: state.references ?? [] },
options: { references },
});
return savedObjectId;
},
Expand Down
Loading