Skip to content

Commit 6a12c8c

Browse files
author
Corey Robertson
authored
[Canvas] Use unique Id for Canvas Embeddables (#56783)
* Use uniqueID for embeddable registry * Adds type to renderer
1 parent f18d591 commit 6a12c8c

File tree

5 files changed

+20
-5
lines changed

5 files changed

+20
-5
lines changed

x-pack/legacy/plugins/canvas/canvas_plugin_src/renderers/embeddable/embeddable.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ const embeddable = () => ({
7070
{ input, embeddableType }: EmbeddableExpression<EmbeddableInput>,
7171
handlers: RendererHandlers
7272
) => {
73-
if (!embeddablesRegistry[input.id]) {
73+
const uniqueId = handlers.getElementId();
74+
75+
if (!embeddablesRegistry[uniqueId]) {
7476
const factory = Array.from(start.getEmbeddableFactories()).find(
7577
embeddableFactory => embeddableFactory.type === embeddableType
7678
) as EmbeddableFactory<EmbeddableInput>;
@@ -82,7 +84,7 @@ const embeddable = () => ({
8284

8385
const embeddableObject = await factory.createFromSavedObject(input.id, input);
8486

85-
embeddablesRegistry[input.id] = embeddableObject;
87+
embeddablesRegistry[uniqueId] = embeddableObject;
8688
ReactDOM.unmountComponentAtNode(domNode);
8789

8890
const subscription = embeddableObject.getInput$().subscribe(function(updatedInput) {
@@ -100,12 +102,12 @@ const embeddable = () => ({
100102
subscription.unsubscribe();
101103
handlers.onEmbeddableDestroyed();
102104

103-
delete embeddablesRegistry[input.id];
105+
delete embeddablesRegistry[uniqueId];
104106

105107
return ReactDOM.unmountComponentAtNode(domNode);
106108
});
107109
} else {
108-
embeddablesRegistry[input.id].updateInput(input);
110+
embeddablesRegistry[uniqueId].updateInput(input);
109111
}
110112
},
111113
});

x-pack/legacy/plugins/canvas/public/components/element_content/element_content.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export const ElementContent = compose(
5454
onComplete,
5555
onEmbeddableInputChange,
5656
onEmbeddableDestroyed,
57+
getElementId,
5758
} = handlers;
5859

5960
return Style.it(
@@ -76,7 +77,14 @@ export const ElementContent = compose(
7677
config={renderable.value}
7778
css={renderable.css} // This is an actual CSS stylesheet string, it will be scoped by RenderElement
7879
size={size} // Size is only passed for the purpose of triggering the resize event, it isn't really used otherwise
79-
handlers={{ getFilter, setFilter, done, onEmbeddableInputChange, onEmbeddableDestroyed }}
80+
handlers={{
81+
getFilter,
82+
setFilter,
83+
done,
84+
onEmbeddableInputChange,
85+
onEmbeddableDestroyed,
86+
getElementId,
87+
}}
8088
/>
8189
</ElementShareContainer>
8290
</div>

x-pack/legacy/plugins/canvas/public/components/element_wrapper/lib/handlers.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export const createHandlers = dispatch => {
3636
completeFn = fn;
3737
},
3838

39+
getElementId: () => element.id,
40+
3941
onEmbeddableInputChange(embeddableExpression) {
4042
dispatch(updateEmbeddableExpression({ elementId: element.id, embeddableExpression }));
4143
},

x-pack/legacy/plugins/canvas/shareable_runtime/components/rendered_element.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export class RenderedElementComponent extends PureComponent<Props> {
6767
done: () => {},
6868
onDestroy: () => {},
6969
onResize: () => {},
70+
getElementId: () => '',
7071
setFilter: () => {},
7172
getFilter: () => '',
7273
onEmbeddableInputChange: () => {},

x-pack/legacy/plugins/canvas/types/renderers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ type GenericCallback = (callback: () => void) => void;
99
export interface RendererHandlers {
1010
/** Handler to invoke when an element has finished rendering */
1111
done: () => void;
12+
/** Get the id of the element being rendered. Can be used as a unique ID in a render function */
13+
getElementId: () => string;
1214
/** Handler to invoke when an element is deleted or changes to a different render type */
1315
onDestroy: GenericCallback;
1416
/** Handler to invoke when an element's dimensions have changed*/

0 commit comments

Comments
 (0)