Skip to content

Commit

Permalink
Merge pull request #2 from Heenawter/suggested-replace-changes_2023-0…
Browse files Browse the repository at this point in the history
…8-07

Replace embeddable + panel changes
  • Loading branch information
nickpeihl authored Aug 8, 2023
2 parents ff28220 + 2b463fc commit 8fb0ed5
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
} from '@kbn/embeddable-plugin/public';
import { Toast } from '@kbn/core/public';

import { DashboardPanelState } from '../../common';
import { pluginServices } from '../services/plugin_services';
import { dashboardReplacePanelActionStrings } from './_dashboard_actions_strings';
import { DashboardContainer } from '../dashboard_container';
Expand Down Expand Up @@ -58,30 +57,15 @@ export class ReplacePanelFlyout extends React.Component<Props> {

public onReplacePanel = async (savedObjectId: string, type: string, name: string) => {
const { panelToRemove, container } = this.props;
const { w, h, x, y } = (container.getInput().panels[panelToRemove.id] as DashboardPanelState)
.gridData;

const { id } = await container.addNewEmbeddable<SavedObjectEmbeddableInput>(type, {
savedObjectId,
});

const { [panelToRemove.id]: omit, ...panels } = container.getInput().panels;

container.updateInput({
panels: {
...panels,
[id]: {
...panels[id],
gridData: {
...(panels[id] as DashboardPanelState).gridData,
w,
h,
x,
y,
},
} as DashboardPanelState,
const id = await container.replaceEmbeddable<SavedObjectEmbeddableInput>(
panelToRemove.id,
{
savedObjectId,
},
});
type,
true
);

(container as DashboardContainer).setHighlightPanelId(id);
this.showToast(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,46 +47,15 @@ export async function replacePanel(
newPanelState: Partial<PanelState>,
generateNewId?: boolean
): Promise<string> {
let panels;
let panelId;

if (generateNewId) {
// replace panel can be called with generateNewId in order to totally destroy and recreate the embeddable
panelId = uuidv4();
panels = { ...this.input.panels };
delete panels[previousPanelState.explicitInput.id];
panels[panelId] = {
...previousPanelState,
...newPanelState,
gridData: {
...previousPanelState.gridData,
i: panelId,
},
explicitInput: {
...newPanelState.explicitInput,
id: panelId,
},
};
} else {
// Because the embeddable type can change, we have to operate at the container level here
panelId = previousPanelState.explicitInput.id;
panels = {
...this.input.panels,
[panelId]: {
...previousPanelState,
...newPanelState,
gridData: {
...previousPanelState.gridData,
},
explicitInput: {
...newPanelState.explicitInput,
id: panelId,
},
},
};
}

await this.updateInput({ panels });
const panelId = await this.replaceEmbeddable(
previousPanelState.explicitInput.id,
{
...newPanelState.explicitInput,
id: previousPanelState.explicitInput.id,
},
newPanelState.type,
generateNewId
);
return panelId;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
EmbeddableStateTransfer,
EmbeddableInput,
Container,
isReferenceOrValueEmbeddable,
} from '../..';

export const ACTION_EDIT_PANEL = 'editPanel';
Expand Down Expand Up @@ -95,12 +94,7 @@ export class EditPanelAction implements Action<ActionContext> {
const oldExplicitInput = embeddable.getExplicitInput();
const newExplicitInput = await factory.getExplicitInput(oldExplicitInput, embeddable.parent);
embeddable.parent?.replaceEmbeddable(embeddable.id, newExplicitInput);
if (
isReferenceOrValueEmbeddable(embeddable) &&
embeddable.inputIsRefType(embeddable.getInput())
) {
embeddable.reload();
}

return;
}

Expand Down
38 changes: 25 additions & 13 deletions src/plugins/embeddable/public/lib/containers/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,12 @@ export abstract class Container<
EEI extends EmbeddableInput = EmbeddableInput,
EEO extends EmbeddableOutput = EmbeddableOutput,
E extends IEmbeddable<EEI, EEO> = IEmbeddable<EEI, EEO>
>(id: string, newExplicitInput: Partial<EEI>, newType?: string) {
>(
id: string,
newExplicitInput: Partial<EEI>,
newType?: string,
generateNewId?: boolean
): Promise<string> {
if (!this.input.panels[id]) {
throw new PanelNotFoundError();
}
Expand All @@ -186,21 +191,28 @@ export abstract class Container<
if (!factory) {
throw new EmbeddableFactoryNotFoundError(newType);
}
this.updateInput({
panels: {
...this.input.panels,
[id]: {
...this.input.panels[id],
explicitInput: { ...newExplicitInput, id },
type: newType,
},
},
} as Partial<TContainerInput>);
} else {
this.updateInputForChild(id, newExplicitInput);
}

const panels = { ...this.input.panels };
const oldPanel = panels[id];

if (generateNewId) {
delete panels[id];
id = uuidv4();
}
this.updateInput({
panels: {
...panels,
[id]: {
...oldPanel,
explicitInput: { ...newExplicitInput, id },
type: newType ?? oldPanel.type,
},
},
} as Partial<TContainerInput>);

await this.untilEmbeddableLoaded<E>(id);
return id;
}

public removeEmbeddable(embeddableId: string) {
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/embeddable/public/lib/containers/i_container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export interface IContainer<
>(
id: string,
newExplicitInput: Partial<EEI>,
newType?: string
): void;
newType?: string,
generateNewId?: boolean
): Promise<string>;
}

0 comments on commit 8fb0ed5

Please sign in to comment.