77import React from 'react' ;
88import ReactDOM from 'react-dom' ;
99import { I18nContext } from 'ui/i18n' ;
10- import { npStart } from 'ui/new_platform' ;
10+ import { CoreStart } from '../../../../../../../src/core/public' ;
11+ import { StartDeps } from '../../plugin' ;
1112import {
1213 IEmbeddable ,
1314 EmbeddableFactory ,
@@ -28,86 +29,88 @@ const embeddablesRegistry: {
2829 [ key : string ] : IEmbeddable ;
2930} = { } ;
3031
31- const renderEmbeddable = ( embeddableObject : IEmbeddable , domNode : HTMLElement ) => {
32- return (
33- < div
34- className = { CANVAS_EMBEDDABLE_CLASSNAME }
35- style = { { width : domNode . offsetWidth , height : domNode . offsetHeight , cursor : 'auto' } }
36- >
37- < I18nContext >
38- < EmbeddablePanel
39- embeddable = { embeddableObject }
40- getActions = { npStart . plugins . uiActions . getTriggerCompatibleActions }
41- getEmbeddableFactory = { npStart . plugins . embeddable . getEmbeddableFactory }
42- getAllEmbeddableFactories = { npStart . plugins . embeddable . getEmbeddableFactories }
43- notifications = { npStart . core . notifications }
44- overlays = { npStart . core . overlays }
45- inspector = { npStart . plugins . inspector }
46- SavedObjectFinder = { getSavedObjectFinder (
47- npStart . core . savedObjects ,
48- npStart . core . uiSettings
49- ) }
50- />
51- </ I18nContext >
52- </ div >
53- ) ;
32+ const renderEmbeddableFactory = ( core : CoreStart , plugins : StartDeps ) => {
33+ return ( embeddableObject : IEmbeddable , domNode : HTMLElement ) => {
34+ return (
35+ < div
36+ className = { CANVAS_EMBEDDABLE_CLASSNAME }
37+ style = { { width : domNode . offsetWidth , height : domNode . offsetHeight , cursor : 'auto' } }
38+ >
39+ < I18nContext >
40+ < EmbeddablePanel
41+ embeddable = { embeddableObject }
42+ getActions = { plugins . uiActions . getTriggerCompatibleActions }
43+ getEmbeddableFactory = { plugins . embeddable . getEmbeddableFactory }
44+ getAllEmbeddableFactories = { plugins . embeddable . getEmbeddableFactories }
45+ notifications = { core . notifications }
46+ overlays = { core . overlays }
47+ inspector = { plugins . inspector }
48+ SavedObjectFinder = { getSavedObjectFinder ( core . savedObjects , core . uiSettings ) }
49+ />
50+ </ I18nContext >
51+ </ div >
52+ ) ;
53+ } ;
5454} ;
5555
56- const embeddable = ( ) => ( {
57- name : 'embeddable' ,
58- displayName : strings . getDisplayName ( ) ,
59- help : strings . getHelpDescription ( ) ,
60- reuseDomNode : true ,
61- render : async (
62- domNode : HTMLElement ,
63- { input , embeddableType } : EmbeddableExpression < EmbeddableInput > ,
64- handlers : RendererHandlers
65- ) => {
66- const uniqueId = handlers . getElementId ( ) ;
67-
68- if ( ! embeddablesRegistry [ uniqueId ] ) {
69- const factory = Array . from ( npStart . plugins . embeddable . getEmbeddableFactories ( ) ) . find (
70- embeddableFactory => embeddableFactory . type === embeddableType
71- ) as EmbeddableFactory < EmbeddableInput > ;
72-
73- if ( ! factory ) {
74- handlers . done ( ) ;
75- throw new EmbeddableFactoryNotFoundError ( embeddableType ) ;
76- }
77-
78- const embeddableObject = await factory . createFromSavedObject ( input . id , input ) ;
56+ export const embeddableRendererFactory = ( core : CoreStart , plugins : StartDeps ) => {
57+ const renderEmbeddable = renderEmbeddableFactory ( core , plugins ) ;
58+ return ( ) => ( {
59+ name : 'embeddable' ,
60+ displayName : strings . getDisplayName ( ) ,
61+ help : strings . getHelpDescription ( ) ,
62+ reuseDomNode : true ,
63+ render : async (
64+ domNode : HTMLElement ,
65+ { input , embeddableType } : EmbeddableExpression < EmbeddableInput > ,
66+ handlers : RendererHandlers
67+ ) => {
68+ const uniqueId = handlers . getElementId ( ) ;
69+
70+ if ( ! embeddablesRegistry [ uniqueId ] ) {
71+ const factory = Array . from ( plugins . embeddable . getEmbeddableFactories ( ) ) . find (
72+ embeddableFactory => embeddableFactory . type === embeddableType
73+ ) as EmbeddableFactory < EmbeddableInput > ;
74+
75+ if ( ! factory ) {
76+ handlers . done ( ) ;
77+ throw new EmbeddableFactoryNotFoundError ( embeddableType ) ;
78+ }
7979
80- embeddablesRegistry [ uniqueId ] = embeddableObject ;
81- ReactDOM . unmountComponentAtNode ( domNode ) ;
80+ const embeddableObject = await factory . createFromSavedObject ( input . id , input ) ;
8281
83- const subscription = embeddableObject . getInput$ ( ) . subscribe ( function ( updatedInput ) {
84- const updatedExpression = embeddableInputToExpression ( updatedInput , embeddableType ) ;
82+ embeddablesRegistry [ uniqueId ] = embeddableObject ;
83+ ReactDOM . unmountComponentAtNode ( domNode ) ;
8584
86- if ( updatedExpression ) {
87- handlers . onEmbeddableInputChange ( updatedExpression ) ;
88- }
89- } ) ;
85+ const subscription = embeddableObject . getInput$ ( ) . subscribe ( function ( updatedInput ) {
86+ const updatedExpression = embeddableInputToExpression ( updatedInput , embeddableType ) ;
9087
91- ReactDOM . render ( renderEmbeddable ( embeddableObject , domNode ) , domNode , ( ) => handlers . done ( ) ) ;
88+ if ( updatedExpression ) {
89+ handlers . onEmbeddableInputChange ( updatedExpression ) ;
90+ }
91+ } ) ;
9292
93- handlers . onResize ( ( ) => {
9493 ReactDOM . render ( renderEmbeddable ( embeddableObject , domNode ) , domNode , ( ) =>
9594 handlers . done ( )
9695 ) ;
97- } ) ;
9896
99- handlers . onDestroy ( ( ) => {
100- subscription . unsubscribe ( ) ;
101- handlers . onEmbeddableDestroyed ( ) ;
97+ handlers . onResize ( ( ) => {
98+ ReactDOM . render ( renderEmbeddable ( embeddableObject , domNode ) , domNode , ( ) =>
99+ handlers . done ( )
100+ ) ;
101+ } ) ;
102102
103- delete embeddablesRegistry [ uniqueId ] ;
103+ handlers . onDestroy ( ( ) => {
104+ subscription . unsubscribe ( ) ;
105+ handlers . onEmbeddableDestroyed ( ) ;
104106
105- return ReactDOM . unmountComponentAtNode ( domNode ) ;
106- } ) ;
107- } else {
108- embeddablesRegistry [ uniqueId ] . updateInput ( input ) ;
109- }
110- } ,
111- } ) ;
107+ delete embeddablesRegistry [ uniqueId ] ;
112108
113- export { embeddable } ;
109+ return ReactDOM . unmountComponentAtNode ( domNode ) ;
110+ } ) ;
111+ } else {
112+ embeddablesRegistry [ uniqueId ] . updateInput ( input ) ;
113+ }
114+ } ,
115+ } ) ;
116+ } ;
0 commit comments