Skip to content

Commit d2379e7

Browse files
authored
Merge pull request #84 from grafana/remove-editing-stuff
SceneObject: Remove interfaces and props for editing
2 parents 3858612 + 09678f5 commit d2379e7

File tree

8 files changed

+22
-88
lines changed

8 files changed

+22
-88
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.22 ()
2+
3+
* Removal of isEditing from SceneComponentProps (also $editor from SceneObjectState, and sceneGraph.getSceneEditor)
4+
15
# 0.21 (2023-03-17)
26

37
**SceneObject subscribeToState parameter change**

packages/scenes/src/components/NestedScene.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class NestedScene extends SceneObjectBase<NestedSceneState> {
4545
};
4646
}
4747

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

@@ -85,7 +85,7 @@ export function NestedSceneRenderer({ model, isEditing }: SceneComponentProps<Ne
8585
</Stack>
8686
<div className={styles.actions}>{toolbarActions}</div>
8787
</div>
88-
{!isCollapsed && <body.Component model={body} isEditing={isEditing} />}
88+
{!isCollapsed && <body.Component model={body} />}
8989
</div>
9090
);
9191
}

packages/scenes/src/components/SceneByFrameRepeater.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ export class SceneByFrameRepeater extends SceneObjectBase<SceneByFrameRepeaterSt
4343
this.state.body.setState({ children: newChildren });
4444
}
4545

46-
public static Component = ({ model, isEditing }: SceneComponentProps<SceneByFrameRepeater>) => {
46+
public static Component = ({ model }: SceneComponentProps<SceneByFrameRepeater>) => {
4747
const { body } = model.useState();
48-
return <body.Component model={body} isEditing={isEditing} />;
48+
return <body.Component model={body} />;
4949
};
5050
}

packages/scenes/src/components/layout/SceneFlexLayout.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class SceneFlexLayout extends SceneObjectBase<SceneFlexLayoutState> imple
3333
}
3434
}
3535

36-
function FlexLayoutRenderer({ model, isEditing }: SceneComponentProps<SceneFlexLayout>) {
36+
function FlexLayoutRenderer({ model }: SceneComponentProps<SceneFlexLayout>) {
3737
const { direction = 'row', children, wrap } = model.useState();
3838
const style: CSSProperties = {
3939
flexGrow: 1,
@@ -48,7 +48,7 @@ function FlexLayoutRenderer({ model, isEditing }: SceneComponentProps<SceneFlexL
4848
return (
4949
<div style={style}>
5050
{children.map((item) => (
51-
<FlexLayoutChildComponent key={item.state.key} item={item} direction={direction} isEditing={isEditing} />
51+
<FlexLayoutChildComponent key={item.state.key} item={item} direction={direction} />
5252
))}
5353
</div>
5454
);
@@ -67,7 +67,7 @@ function FlexLayoutChildComponent({
6767

6868
return (
6969
<div style={getItemStyles(direction, placement)}>
70-
<item.Component model={item} isEditing={isEditing} />
70+
<item.Component model={item} />
7171
</div>
7272
);
7373
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
import React, { useEffect } from 'react';
22

3-
import { SceneComponentProps, SceneEditor, SceneObject } from './types';
3+
import { SceneComponentProps, SceneObject } from './types';
44

5-
function SceneComponentWrapperWithoutMemo<T extends SceneObject>({
6-
model,
7-
isEditing,
8-
...otherProps
9-
}: SceneComponentProps<T>) {
5+
function SceneComponentWrapperWithoutMemo<T extends SceneObject>({ model, ...otherProps }: SceneComponentProps<T>) {
106
const Component = (model as any).constructor['Component'] ?? EmptyRenderer;
11-
const inner = <Component {...otherProps} model={model} isEditing={isEditing} />;
127

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

25-
if (!isEditing) {
26-
return inner;
27-
}
28-
29-
const editor = getSceneEditor(model);
30-
const EditWrapper = getSceneEditor(model).getEditComponentWrapper();
31-
32-
return (
33-
<EditWrapper model={model} editor={editor}>
34-
{inner}
35-
</EditWrapper>
36-
);
20+
return <Component {...otherProps} model={model} />;
3721
}
3822

3923
export const SceneComponentWrapper = React.memo(SceneComponentWrapperWithoutMemo);
4024

4125
function EmptyRenderer<T>(_: SceneComponentProps<T>): React.ReactElement | null {
4226
return null;
4327
}
44-
45-
function getSceneEditor(sceneObject: SceneObject): SceneEditor {
46-
const { $editor } = sceneObject.state;
47-
if ($editor) {
48-
return $editor;
49-
}
50-
51-
if (sceneObject.parent) {
52-
return getSceneEditor(sceneObject.parent);
53-
}
54-
55-
throw new Error('No editor found in scene tree');
56-
}

packages/scenes/src/core/SceneObjectBase.tsx

-7
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,6 @@ export abstract class SceneObjectBase<TState extends SceneObjectState = SceneObj
7676
return SceneComponentWrapper;
7777
}
7878

79-
/**
80-
* Temporary solution, should be replaced by declarative options
81-
*/
82-
public get Editor(): SceneComponent<this> {
83-
return ((this as any).constructor['Editor'] ?? (() => null)) as SceneComponent<this>;
84-
}
85-
8679
private setParent() {
8780
forEachSceneObjectInState(this._state, (child) => (child._parent = this));
8881
}

packages/scenes/src/core/sceneGraph.ts

+1-18
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { DefaultTimeRange, EmptyDataNode, EmptyVariableSet } from '../variables/
44
import { sceneInterpolator } from '../variables/interpolation/sceneInterpolator';
55
import { VariableCustomFormatterFn, SceneVariables } from '../variables/types';
66

7-
import { SceneDataState, SceneEditor, SceneLayout, SceneObject, SceneTimeRangeLike } from './types';
7+
import { SceneDataState, SceneLayout, SceneObject, SceneTimeRangeLike } from './types';
88
import { lookupVariable } from '../variables/lookupVariable';
99

1010
/**
@@ -54,22 +54,6 @@ export function getTimeRange(sceneObject: SceneObject): SceneTimeRangeLike {
5454
return DefaultTimeRange;
5555
}
5656

57-
/**
58-
* Will walk up the scene object graph to the closest $editor scene object
59-
*/
60-
export function getSceneEditor(sceneObject: SceneObject): SceneEditor {
61-
const { $editor } = sceneObject.state;
62-
if ($editor) {
63-
return $editor;
64-
}
65-
66-
if (sceneObject.parent) {
67-
return getSceneEditor(sceneObject.parent);
68-
}
69-
70-
throw new Error('No editor found in scene tree');
71-
}
72-
7357
/**
7458
* Will walk up the scene object graph to the closest $layout scene object
7559
*/
@@ -126,7 +110,6 @@ export const sceneGraph = {
126110
getVariables,
127111
getData,
128112
getTimeRange,
129-
getSceneEditor,
130113
getLayout,
131114
interpolate,
132115
lookupVariable,

packages/scenes/src/core/types.ts

+7-24
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export interface SceneObjectStatePlain {
1818
key?: string;
1919
$timeRange?: SceneTimeRangeLike;
2020
$data?: SceneDataProvider;
21-
$editor?: SceneEditor;
2221
$variables?: SceneVariables;
2322
}
2423

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

4443
export interface SceneComponentProps<T> {
4544
model: T;
46-
isEditing?: boolean;
4745
}
4846

49-
export type SceneComponent<TModel> = React.FunctionComponent<SceneComponentProps<TModel>>;
47+
export interface SceneComponentWrapperProps {
48+
model: SceneObject;
49+
children: React.ReactNode;
50+
}
51+
52+
export type SceneComponent<TModel> = (props: SceneComponentProps<TModel>) => React.ReactElement | null;
53+
export type SceneComponentCustomWrapper = (props: SceneComponentWrapperProps) => React.ReactElement | null;
5054

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

106-
/** To be replaced by declarative method */
107-
Editor(props: SceneComponentProps<SceneObject<TState>>): React.ReactElement | null;
108-
109110
/** Force a re-render, should only be needed when variable values change */
110111
forceRender(): void;
111112

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

134-
export interface SceneEditorState extends SceneObjectStatePlain {
135-
hoverObject?: SceneObjectRef;
136-
selectedObject?: SceneObjectRef;
137-
}
138-
139-
export interface SceneEditor extends SceneObject<SceneEditorState> {
140-
onMouseEnterObject(model: SceneObject): void;
141-
onMouseLeaveObject(model: SceneObject): void;
142-
onSelectObject(model: SceneObject): void;
143-
getEditComponentWrapper(): React.ComponentType<SceneComponentEditWrapperProps>;
144-
}
145-
146-
interface SceneComponentEditWrapperProps {
147-
editor: SceneEditor;
148-
model: SceneObject;
149-
children: React.ReactNode;
150-
}
151-
152135
export interface SceneTimeRangeState extends SceneObjectStatePlain {
153136
from: string;
154137
to: string;

0 commit comments

Comments
 (0)