Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Maps] decouple SYNCHRONIZE_MOVEMENT_ACTION from Embeddable framework #175642

Merged
merged 8 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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 @@ -21,7 +21,7 @@ export const apiHasType = (api: unknown | null): api is HasType => {

export const apiIsOfType = <T extends string = string>(
api: unknown | null,
typeToCheck: string
typeToCheck: T
): api is HasType<T> => {
return Boolean(api && (api as HasType).type) && (api as HasType).type === typeToCheck;
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
* Side Public License, v 1.
*/

import { type HasType, apiIsOfType } from '@kbn/presentation-publishing';
import { VisParams } from '../../types';
import Vis from '../../vis';

export interface HasVisualizeConfig {
export type HasVisualizeConfig = HasType<'visualization'> & {
getVis: () => Vis<VisParams>;
}
};

export const apiHasVisualizeConfig = (api: unknown): api is HasVisualizeConfig => {
return Boolean(api && typeof (api as HasVisualizeConfig).getVis === 'function');
return Boolean(
api &&
apiIsOfType(api, 'visualization') &&
typeof (api as HasVisualizeConfig).getVis === 'function'
);
};
3 changes: 2 additions & 1 deletion src/plugins/visualizations/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
"@kbn/logging",
"@kbn/content-management-table-list-view-common",
"@kbn/chart-expressions-common",
"@kbn/shared-ux-utility"
"@kbn/shared-ux-utility",
"@kbn/presentation-publishing"
],
"exclude": [
"target/**/*",
Expand Down
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,19 @@
/*
* 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 HasType, apiIsOfType } from '@kbn/presentation-publishing';
import { Document } from '../../persistence';

export type HasLensConfig = HasType<'lens'> & {
getSavedVis: () => Readonly<Document | undefined>;
};

export const apiHasLensConfig = (api: unknown): api is HasLensConfig => {
return Boolean(
api && apiIsOfType(api, 'lens') && typeof (api as HasLensConfig).getSavedVis === 'function'
);
};
2 changes: 2 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,7 @@

import { LensPlugin } from './plugin';

export { apiHasLensConfig } from './embeddable/interfaces/has_lens_config';
export type {
EmbeddableComponentProps,
EmbeddableComponent,
Expand Down Expand Up @@ -109,6 +110,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
3 changes: 2 additions & 1 deletion x-pack/plugins/lens/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@
"@kbn/shared-ux-utility",
"@kbn/text-based-editor",
"@kbn/managed-content-badge",
"@kbn/sort-predicates"
"@kbn/sort-predicates",
"@kbn/presentation-publishing"
],
"exclude": ["target/**/*"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function isLegacyMap(embeddable: Embeddable) {
type LegacyMapApi = HasType & Partial<HasVisualizeConfig>;

export function isLegacyMapApi(api: LegacyMapApi) {
if (api.type !== 'visualization' || !apiHasVisualizeConfig(api)) {
if (!apiHasVisualizeConfig(api)) {
return false;
}
return ['region_map', 'tile_map'].includes(api.getVis().type?.name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { i18n } from '@kbn/i18n';
import { type EmbeddableApiContext, apiHasType } from '@kbn/presentation-publishing';
import { type EmbeddableApiContext, apiHasType, apiIsOfType } from '@kbn/presentation-publishing';
import { createAction } from '@kbn/ui-actions-plugin/public';
import { type FilterByMapExtentActionApi } from './types';
import { MAP_SAVED_OBJECT_TYPE } from '../../../common/constants';
Expand Down Expand Up @@ -55,7 +55,7 @@ export const filterByMapExtentAction = createAction<EmbeddableApiContext>({
if (!isApiCompatible(embeddable)) return false;
return embeddable.disableTriggers
? false
: embeddable.type === MAP_SAVED_OBJECT_TYPE || isLegacyMapApi(embeddable);
: apiIsOfType(embeddable, MAP_SAVED_OBJECT_TYPE) || isLegacyMapApi(embeddable);
},
execute: async ({ embeddable }: EmbeddableApiContext) => {
const { openModal } = await import('./modal');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
import type { HasDisableTriggers, HasParentApi, HasType } from '@kbn/presentation-publishing';
import type { HasVisualizeConfig } from '@kbn/visualizations-plugin/public';

export type FilterByMapExtentActionApi = HasType &
export type FilterByMapExtentActionApi = HasType<'visualization' | 'map'> &
Partial<HasDisableTriggers & HasParentApi<HasType> & HasVisualizeConfig>;
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,25 @@
* 2.0.
*/

import type { Embeddable as LensEmbeddable } from '@kbn/lens-plugin/public';
import { apiIsOfType } from '@kbn/presentation-publishing';
import { type HasLensConfig, 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'
) {
if (apiHasLensConfig(api) && (api as HasLensConfig).getSavedVis()?.visualizationType === 'lnsChoropleth') {
return true;
}

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

return embeddable.type === MAP_SAVED_OBJECT_TYPE;
return apiIsOfType(api, 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<'lens' | 'visualization' | 'map'> &
Partial<HasLensConfig & HasVisualizeConfig>;