Skip to content

Commit

Permalink
[Maps] decouple SYNCHRONIZE_MOVEMENT_ACTION action from Embeddable fr…
Browse files Browse the repository at this point in the history
…amework
  • Loading branch information
nreese committed Jan 25, 2024
1 parent 4565b1b commit 7f4a8ca
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 19 deletions.
2 changes: 2 additions & 0 deletions x-pack/plugins/lens/public/embeddable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
*/

export * from './embeddable';

export { type HasLensConfig, apiHasLensConfig } from './interfaces/has_lens_config';
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { Document } from '../../persistence';

export interface HasLensConfig {
getSavedVis: () => Readonly<Document | undefined>;
}

export const apiHasLensConfig = (api: unknown): api is HasLensConfig => {
return Boolean(api && typeof (api as HasLensConfig).getSavedVis === 'function');
};
4 changes: 4 additions & 0 deletions x-pack/plugins/lens/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import { LensPlugin } from './plugin';

export {
apiHasLensConfig,
} from './embeddable/interfaces/has_lens_config';
export type {
EmbeddableComponentProps,
EmbeddableComponent,
Expand Down Expand Up @@ -109,6 +112,7 @@ export type {
export type { InlineEditLensEmbeddableContext } from './trigger_actions/open_lens_config/in_app_embeddable_edit/types';

export type {
HasLensConfig,
LensEmbeddableInput,
LensSavedObjectAttributes,
Embeddable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
*/

import { i18n } from '@kbn/i18n';
import { type EmbeddableApiContext, apiHasType } from '@kbn/presentation-publishing';
import { createAction } from '@kbn/ui-actions-plugin/public';
import type { SynchronizeMovementActionContext } from './types';
import type { SynchronizeMovementActionApi } from './types';

export const SYNCHRONIZE_MOVEMENT_ACTION = 'SYNCHRONIZE_MOVEMENT_ACTION';

export const synchronizeMovementAction = createAction<SynchronizeMovementActionContext>({
export const isApiCompatible = (api: unknown | null): api is SynchronizeMovementActionApi =>
Boolean(apiHasType(api));

export const synchronizeMovementAction = createAction<EmbeddableApiContext>({
id: SYNCHRONIZE_MOVEMENT_ACTION,
type: SYNCHRONIZE_MOVEMENT_ACTION,
order: 21,
getDisplayName: ({ embeddable }: SynchronizeMovementActionContext) => {
getDisplayName: () => {
return i18n.translate('xpack.maps.synchronizeMovementAction.title', {
defaultMessage: 'Synchronize map movement',
});
Expand All @@ -29,11 +33,12 @@ export const synchronizeMovementAction = createAction<SynchronizeMovementActionC
getIconType: () => {
return 'crosshairs';
},
isCompatible: async (context: SynchronizeMovementActionContext) => {
isCompatible: async ({ embeddable }: EmbeddableApiContext) => {
if (!isApiCompatible(embeddable)) return false;
const { isCompatible } = await import('./is_compatible');
return isCompatible(context);
return isCompatible(embeddable);
},
execute: async (context: SynchronizeMovementActionContext) => {
execute: async () => {
const { openModal } = await import('./modal');
openModal();
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@
* 2.0.
*/

import type { Embeddable as LensEmbeddable } from '@kbn/lens-plugin/public';
import { apiHasLensConfig } from '@kbn/lens-plugin/public';
import { MAP_SAVED_OBJECT_TYPE } from '../../../common/constants';
import { isLegacyMap } from '../../legacy_visualizations/is_legacy_map';
import { isLegacyMapApi } from '../../legacy_visualizations/is_legacy_map';
import { mapEmbeddablesSingleton } from '../../embeddable/map_embeddables_singleton';
import type { SynchronizeMovementActionContext } from './types';
import type { SynchronizeMovementActionApi } from './types';

export function isCompatible({ embeddable }: SynchronizeMovementActionContext) {
export function isCompatible(api: SynchronizeMovementActionApi) {
if (!mapEmbeddablesSingleton.hasMultipleMaps()) {
return false;
}

if (
embeddable.type === 'lens' &&
typeof (embeddable as LensEmbeddable).getSavedVis === 'function' &&
(embeddable as LensEmbeddable).getSavedVis()?.visualizationType === 'lnsChoropleth'
api.type === 'lens' &&
apiHasLensConfig(api) &&
api.getSavedVis()?.visualizationType === 'lnsChoropleth'
) {
return true;
}

if (isLegacyMap(embeddable)) {
if (isLegacyMapApi(api)) {
return true;
}

return embeddable.type === MAP_SAVED_OBJECT_TYPE;
return api.type === MAP_SAVED_OBJECT_TYPE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
* 2.0.
*/

import type { Embeddable, EmbeddableInput } from '@kbn/embeddable-plugin/public';
import type { HasType } from '@kbn/presentation-publishing';
import type { HasLensConfig } from '@kbn/lens-plugin/public';
import type { HasVisualizeConfig } from '@kbn/visualizations-plugin/public';

export interface SynchronizeMovementActionContext {
embeddable: Embeddable<EmbeddableInput>;
}
export type SynchronizeMovementActionApi = HasType &
Partial<HasLensConfig & HasVisualizeConfig>;

0 comments on commit 7f4a8ca

Please sign in to comment.