From 412dfc97f79d114fd39573a1a5ce47142cbdcf30 Mon Sep 17 00:00:00 2001 From: ppisljar Date: Wed, 5 Aug 2020 04:24:13 -0700 Subject: [PATCH] dynamic actions --- .../embeddables/dynamic_action_enhancement.ts | 29 +++++++++++++++++++ .../embeddable_enhanced/public/plugin.ts | 6 +++- .../public/dynamic_actions/action_factory.ts | 13 +++++++++ .../action_factory_definition.ts | 5 ++++ .../ui_actions_enhanced/public/plugin.ts | 5 +++- .../ui_actions_service_enhancements.ts | 24 ++++++++++++++- 6 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 x-pack/plugins/embeddable_enhanced/public/embeddables/dynamic_action_enhancement.ts diff --git a/x-pack/plugins/embeddable_enhanced/public/embeddables/dynamic_action_enhancement.ts b/x-pack/plugins/embeddable_enhanced/public/embeddables/dynamic_action_enhancement.ts new file mode 100644 index 000000000000000..c576176c9b9f309 --- /dev/null +++ b/x-pack/plugins/embeddable_enhanced/public/embeddables/dynamic_action_enhancement.ts @@ -0,0 +1,29 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { EnhancementRegistryDefinition } from '../../../../../src/plugins/embeddable/public'; +import { SavedObjectReference, Serializable } from '../../../../../src/core/types'; +import { StartServicesGetter } from '../../../../../src/plugins/kibana_utils/public'; +import { StartDependencies } from '../plugin'; + +export interface DynamicActionEnhancementDeps { + start: StartServicesGetter>; +} + +export const dynamicActionEnhancement = (start) => { + return { + id: 'dynamicActions', + migrate: (state: Serializable) => { + return start.uiActionsEnhanced.migrateEvent(state); + }, + extract: (state: Serializable) => { + return start.uiActionsEnhanced.extractEvent(state); + }, + inject: (state: Serializable, refereces: SavedObjectReference[]) => { + return start.uiActionsEnhanced.injectEvent(state, references); + }, + }; +}; diff --git a/x-pack/plugins/embeddable_enhanced/public/plugin.ts b/x-pack/plugins/embeddable_enhanced/public/plugin.ts index fd0bcc2023269ea..02ae04400478966 100644 --- a/x-pack/plugins/embeddable_enhanced/public/plugin.ts +++ b/x-pack/plugins/embeddable_enhanced/public/plugin.ts @@ -30,6 +30,7 @@ import { AdvancedUiActionsStart, } from '../../ui_actions_enhanced/public'; import { PanelNotificationsAction, ACTION_PANEL_NOTIFICATIONS } from './actions'; +import { createStartServicesGetter } from '../../../../src/plugins/kibana_utils/public'; declare module '../../../../src/plugins/ui_actions/public' { export interface ActionContextMapping { @@ -62,14 +63,17 @@ export class EmbeddableEnhancedPlugin public setup(core: CoreSetup, plugins: SetupDependencies): SetupContract { this.setCustomEmbeddableFactoryProvider(plugins); + const start = createStartServicesGetter(core.getStartServices); + const panelNotificationAction = new PanelNotificationsAction(); plugins.uiActionsEnhanced.registerAction(panelNotificationAction); plugins.uiActionsEnhanced.attachAction(PANEL_NOTIFICATION_TRIGGER, panelNotificationAction.id); + plugins.embeddable.registerEmbeddableEnhancement(dynamicActionEnhancement(start)); return {}; } - public start(core: CoreStart, plugins: StartDependencies): StartContract { + public start(core: CoreStart, plugins: StartDependenciesStartDependencies): StartContract { this.uiActions = plugins.uiActionsEnhanced; return {}; diff --git a/x-pack/plugins/ui_actions_enhanced/public/dynamic_actions/action_factory.ts b/x-pack/plugins/ui_actions_enhanced/public/dynamic_actions/action_factory.ts index 95b7941b48ed353..f9829975074d727 100644 --- a/x-pack/plugins/ui_actions_enhanced/public/dynamic_actions/action_factory.ts +++ b/x-pack/plugins/ui_actions_enhanced/public/dynamic_actions/action_factory.ts @@ -11,6 +11,7 @@ import { Configurable } from '../../../../../src/plugins/kibana_utils/public'; import { SerializedAction } from './types'; import { ILicense } from '../../../licensing/public'; import { UiActionsActionDefinition as ActionDefinition } from '../../../../../src/plugins/ui_actions/public'; +import { SavedObjectReference } from '../../../../../src/core/types'; export class ActionFactory< Config extends object = object, @@ -74,4 +75,16 @@ export class ActionFactory< }, }; } + + public migrate(state: unknown) { + return this.def.migrate ? this.def.migrate(state) : state; + } + + public extract(state: SerializedEvent) { + return this.def.extract ? this.def.extract(state) : { state, references: [] }; + } + + public inject(state: SerializedEvent, references: SavedObjectReference[]) { + return this.def.inject ? this.def.inject(state, references) : state; + } } diff --git a/x-pack/plugins/ui_actions_enhanced/public/dynamic_actions/action_factory_definition.ts b/x-pack/plugins/ui_actions_enhanced/public/dynamic_actions/action_factory_definition.ts index d63f69ba5ab729f..f42ee747622abd1 100644 --- a/x-pack/plugins/ui_actions_enhanced/public/dynamic_actions/action_factory_definition.ts +++ b/x-pack/plugins/ui_actions_enhanced/public/dynamic_actions/action_factory_definition.ts @@ -11,6 +11,7 @@ import { UiActionsActionDefinition as ActionDefinition, UiActionsPresentable as Presentable, } from '../../../../../src/plugins/ui_actions/public'; +import { SavedObjectReference } from '../../../../../src/core/types'; /** * This is a convenience interface for registering new action factories. @@ -42,4 +43,8 @@ export interface ActionFactoryDefinition< create( serializedAction: Omit, 'factoryId'> ): ActionDefinition; + + migrate?(state: unknown): SerializedEvent; + extract?(state: SerializedEvent): { state: SerializedEvent; references: SavedObjectReference[] }; + inject?(state: SerializedEvent, references: SavedObjectReference[]): SerializedEvent; } diff --git a/x-pack/plugins/ui_actions_enhanced/public/plugin.ts b/x-pack/plugins/ui_actions_enhanced/public/plugin.ts index a625ea2e2118bc4..c45d98c5df63d5e 100644 --- a/x-pack/plugins/ui_actions_enhanced/public/plugin.ts +++ b/x-pack/plugins/ui_actions_enhanced/public/plugin.ts @@ -53,7 +53,10 @@ export interface SetupContract export interface StartContract extends UiActionsStart, - Pick { + Pick< + UiActionsServiceEnhancements, + 'getActionFactory' | 'getActionFactories' | 'migrateEvent' | 'extractEvent' | 'injectEvent' + > { FlyoutManageDrilldowns: ReturnType; } diff --git a/x-pack/plugins/ui_actions_enhanced/public/services/ui_actions_service_enhancements.ts b/x-pack/plugins/ui_actions_enhanced/public/services/ui_actions_service_enhancements.ts index bd05659d59e9d89..78bff8c76f2fce6 100644 --- a/x-pack/plugins/ui_actions_enhanced/public/services/ui_actions_service_enhancements.ts +++ b/x-pack/plugins/ui_actions_enhanced/public/services/ui_actions_service_enhancements.ts @@ -5,9 +5,10 @@ */ import { ActionFactoryRegistry } from '../types'; -import { ActionFactory, ActionFactoryDefinition } from '../dynamic_actions'; +import { ActionFactory, ActionFactoryDefinition, SerializedEvent } from '../dynamic_actions'; import { DrilldownDefinition } from '../drilldowns'; import { ILicense } from '../../../licensing/common/types'; +import { SavedObjectReference } from '../../../../../src/core/types'; export interface UiActionsServiceEnhancementsParams { readonly actionFactories?: ActionFactoryRegistry; @@ -103,4 +104,25 @@ export class UiActionsServiceEnhancements { this.registerActionFactory(actionFactory); }; + + public migrateEvent(event: unknown) { + return this.actionFactories.has(event.id) + ? this.actionFactories.get(event.id).migrate(event) + : event; + } + + public extractEvent(event: SerializedEvent) { + return this.actionFactories.has(event.id) + ? this.actionFactories.get(event.id).extract(event) + : { + event, + references: [], + }; + } + + public injectEvent(event: SerializedEvent, references: SavedObjectReference[]) { + return this.actionFactories.has(event.id) + ? this.actionFactories.get(event.id).inject(event, references) + : event; + } }