Skip to content

Commit

Permalink
Merge pull request #84 from grafana/remove-editing-stuff
Browse files Browse the repository at this point in the history
SceneObject: Remove interfaces and props for editing
  • Loading branch information
torkelo authored Mar 20, 2023
2 parents 3858612 + 09678f5 commit d2379e7
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 88 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.22 ()

* Removal of isEditing from SceneComponentProps (also $editor from SceneObjectState, and sceneGraph.getSceneEditor)

# 0.21 (2023-03-17)

**SceneObject subscribeToState parameter change**
Expand Down
4 changes: 2 additions & 2 deletions packages/scenes/src/components/NestedScene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class NestedScene extends SceneObjectBase<NestedSceneState> {
};
}

export function NestedSceneRenderer({ model, isEditing }: SceneComponentProps<NestedScene>) {
export function NestedSceneRenderer({ model }: SceneComponentProps<NestedScene>) {
const { title, isCollapsed, canCollapse, canRemove, body, actions } = model.useState();
const styles = useStyles2(getStyles);

Expand Down Expand Up @@ -85,7 +85,7 @@ export function NestedSceneRenderer({ model, isEditing }: SceneComponentProps<Ne
</Stack>
<div className={styles.actions}>{toolbarActions}</div>
</div>
{!isCollapsed && <body.Component model={body} isEditing={isEditing} />}
{!isCollapsed && <body.Component model={body} />}
</div>
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/scenes/src/components/SceneByFrameRepeater.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export class SceneByFrameRepeater extends SceneObjectBase<SceneByFrameRepeaterSt
this.state.body.setState({ children: newChildren });
}

public static Component = ({ model, isEditing }: SceneComponentProps<SceneByFrameRepeater>) => {
public static Component = ({ model }: SceneComponentProps<SceneByFrameRepeater>) => {
const { body } = model.useState();
return <body.Component model={body} isEditing={isEditing} />;
return <body.Component model={body} />;
};
}
6 changes: 3 additions & 3 deletions packages/scenes/src/components/layout/SceneFlexLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class SceneFlexLayout extends SceneObjectBase<SceneFlexLayoutState> imple
}
}

function FlexLayoutRenderer({ model, isEditing }: SceneComponentProps<SceneFlexLayout>) {
function FlexLayoutRenderer({ model }: SceneComponentProps<SceneFlexLayout>) {
const { direction = 'row', children, wrap } = model.useState();
const style: CSSProperties = {
flexGrow: 1,
Expand All @@ -48,7 +48,7 @@ function FlexLayoutRenderer({ model, isEditing }: SceneComponentProps<SceneFlexL
return (
<div style={style}>
{children.map((item) => (
<FlexLayoutChildComponent key={item.state.key} item={item} direction={direction} isEditing={isEditing} />
<FlexLayoutChildComponent key={item.state.key} item={item} direction={direction} />
))}
</div>
);
Expand All @@ -67,7 +67,7 @@ function FlexLayoutChildComponent({

return (
<div style={getItemStyles(direction, placement)}>
<item.Component model={item} isEditing={isEditing} />
<item.Component model={item} />
</div>
);
}
Expand Down
35 changes: 3 additions & 32 deletions packages/scenes/src/core/SceneComponentWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import React, { useEffect } from 'react';

import { SceneComponentProps, SceneEditor, SceneObject } from './types';
import { SceneComponentProps, SceneObject } from './types';

function SceneComponentWrapperWithoutMemo<T extends SceneObject>({
model,
isEditing,
...otherProps
}: SceneComponentProps<T>) {
function SceneComponentWrapperWithoutMemo<T extends SceneObject>({ model, ...otherProps }: SceneComponentProps<T>) {
const Component = (model as any).constructor['Component'] ?? EmptyRenderer;
const inner = <Component {...otherProps} model={model} isEditing={isEditing} />;

// Handle component activation state state
useEffect(() => {
Expand All @@ -22,35 +17,11 @@ function SceneComponentWrapperWithoutMemo<T extends SceneObject>({
};
}, [model]);

if (!isEditing) {
return inner;
}

const editor = getSceneEditor(model);
const EditWrapper = getSceneEditor(model).getEditComponentWrapper();

return (
<EditWrapper model={model} editor={editor}>
{inner}
</EditWrapper>
);
return <Component {...otherProps} model={model} />;
}

export const SceneComponentWrapper = React.memo(SceneComponentWrapperWithoutMemo);

function EmptyRenderer<T>(_: SceneComponentProps<T>): React.ReactElement | null {
return null;
}

function getSceneEditor(sceneObject: SceneObject): SceneEditor {
const { $editor } = sceneObject.state;
if ($editor) {
return $editor;
}

if (sceneObject.parent) {
return getSceneEditor(sceneObject.parent);
}

throw new Error('No editor found in scene tree');
}
7 changes: 0 additions & 7 deletions packages/scenes/src/core/SceneObjectBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,6 @@ export abstract class SceneObjectBase<TState extends SceneObjectState = SceneObj
return SceneComponentWrapper;
}

/**
* Temporary solution, should be replaced by declarative options
*/
public get Editor(): SceneComponent<this> {
return ((this as any).constructor['Editor'] ?? (() => null)) as SceneComponent<this>;
}

private setParent() {
forEachSceneObjectInState(this._state, (child) => (child._parent = this));
}
Expand Down
19 changes: 1 addition & 18 deletions packages/scenes/src/core/sceneGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DefaultTimeRange, EmptyDataNode, EmptyVariableSet } from '../variables/
import { sceneInterpolator } from '../variables/interpolation/sceneInterpolator';
import { VariableCustomFormatterFn, SceneVariables } from '../variables/types';

import { SceneDataState, SceneEditor, SceneLayout, SceneObject, SceneTimeRangeLike } from './types';
import { SceneDataState, SceneLayout, SceneObject, SceneTimeRangeLike } from './types';
import { lookupVariable } from '../variables/lookupVariable';

/**
Expand Down Expand Up @@ -54,22 +54,6 @@ export function getTimeRange(sceneObject: SceneObject): SceneTimeRangeLike {
return DefaultTimeRange;
}

/**
* Will walk up the scene object graph to the closest $editor scene object
*/
export function getSceneEditor(sceneObject: SceneObject): SceneEditor {
const { $editor } = sceneObject.state;
if ($editor) {
return $editor;
}

if (sceneObject.parent) {
return getSceneEditor(sceneObject.parent);
}

throw new Error('No editor found in scene tree');
}

/**
* Will walk up the scene object graph to the closest $layout scene object
*/
Expand Down Expand Up @@ -126,7 +110,6 @@ export const sceneGraph = {
getVariables,
getData,
getTimeRange,
getSceneEditor,
getLayout,
interpolate,
lookupVariable,
Expand Down
31 changes: 7 additions & 24 deletions packages/scenes/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export interface SceneObjectStatePlain {
key?: string;
$timeRange?: SceneTimeRangeLike;
$data?: SceneDataProvider;
$editor?: SceneEditor;
$variables?: SceneVariables;
}

Expand All @@ -43,10 +42,15 @@ export interface SceneLayoutChildOptions {

export interface SceneComponentProps<T> {
model: T;
isEditing?: boolean;
}

export type SceneComponent<TModel> = React.FunctionComponent<SceneComponentProps<TModel>>;
export interface SceneComponentWrapperProps {
model: SceneObject;
children: React.ReactNode;
}

export type SceneComponent<TModel> = (props: SceneComponentProps<TModel>) => React.ReactElement | null;
export type SceneComponentCustomWrapper = (props: SceneComponentWrapperProps) => React.ReactElement | null;

export interface SceneDataState extends SceneObjectStatePlain {
data?: PanelData;
Expand Down Expand Up @@ -103,9 +107,6 @@ export interface SceneObject<TState extends SceneObjectState = SceneObjectState>
/** A React component to use for rendering the object */
Component(props: SceneComponentProps<SceneObject<TState>>): React.ReactElement | null;

/** To be replaced by declarative method */
Editor(props: SceneComponentProps<SceneObject<TState>>): React.ReactElement | null;

/** Force a re-render, should only be needed when variable values change */
forceRender(): void;

Expand All @@ -131,24 +132,6 @@ export interface SceneLayout<T extends SceneLayoutState = SceneLayoutState> exte
getDragClassCancel?(): string;
}

export interface SceneEditorState extends SceneObjectStatePlain {
hoverObject?: SceneObjectRef;
selectedObject?: SceneObjectRef;
}

export interface SceneEditor extends SceneObject<SceneEditorState> {
onMouseEnterObject(model: SceneObject): void;
onMouseLeaveObject(model: SceneObject): void;
onSelectObject(model: SceneObject): void;
getEditComponentWrapper(): React.ComponentType<SceneComponentEditWrapperProps>;
}

interface SceneComponentEditWrapperProps {
editor: SceneEditor;
model: SceneObject;
children: React.ReactNode;
}

export interface SceneTimeRangeState extends SceneObjectStatePlain {
from: string;
to: string;
Expand Down

0 comments on commit d2379e7

Please sign in to comment.