Skip to content

Commit 6a0482a

Browse files
Aarebeccaantv
andauthored
refactor(elements): move context from style to config (#6525)
Co-authored-by: antv <[email protected]>
1 parent 42b71ea commit 6a0482a

File tree

12 files changed

+31
-47
lines changed

12 files changed

+31
-47
lines changed

packages/g6/__tests__/unit/utils/element.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('element', () => {
4545
},
4646
};
4747

48-
const edge = new Polyline({ style: { sourceNode: 'node-1', targetNode: 'node-2', context } });
48+
const edge = new Polyline({ style: { sourceNode: 'node-1', targetNode: 'node-2' }, context });
4949

5050
it('isNode', () => {
5151
const rect = new Rect({ style: { width: 10, height: 10 } });

packages/g6/__tests__/unit/utils/event.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('event', () => {
2020
},
2121
};
2222

23-
const edge = new Polyline({ style: { sourceNode: 'node-1', targetNode: 'node-2', context } });
23+
const edge = new Polyline({ style: { sourceNode: 'node-1', targetNode: 'node-2' }, context });
2424

2525
it('eventTargetOf', () => {
2626
expect(eventTargetOf(node1)?.type).toEqual('node');

packages/g6/src/elements/base-element.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import type { IAnimation } from '@antv/g';
22
import type { RuntimeContext } from '../runtime/types';
3-
import type { BaseElementStyleProps, Keyframe } from '../types';
3+
import type { Keyframe } from '../types';
4+
import type { BaseShapeStyleProps } from './shapes';
45
import { BaseShape } from './shapes';
56

6-
export abstract class BaseElement<T extends BaseElementStyleProps> extends BaseShape<T> {
7+
export abstract class BaseElement<T extends BaseShapeStyleProps> extends BaseShape<T> {
78
protected get context(): RuntimeContext {
8-
return this.attributes.context!;
9+
// @ts-expect-error skip type-check
10+
return this.config.context;
911
}
1012

1113
protected get parsedAttributes() {

packages/g6/src/elements/combos/base-combo.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ export abstract class BaseCombo<S extends BaseComboStyleProps = BaseComboStylePr
136136
}
137137

138138
protected getContentBBox(attributes: Required<S>): AABB {
139-
const { context, childrenNode = [], padding } = attributes;
140-
const children = childrenNode.map((id) => context!.element!.getElement(id)).filter(Boolean);
139+
const { childrenNode = [], padding } = attributes;
140+
const children = childrenNode.map((id) => this.context!.element!.getElement(id)).filter(Boolean);
141141
if (children.length === 0) {
142142
const bbox = new AABB();
143143
const { x = 0, y = 0, size } = attributes;
@@ -181,8 +181,8 @@ export abstract class BaseCombo<S extends BaseComboStyleProps = BaseComboStylePr
181181
}
182182

183183
protected getCollapsedMarkerText(type: CollapsedMarkerStyleProps['type'], attributes: Required<S>): string {
184-
const { context, childrenData = [] } = attributes;
185-
const { model } = context!;
184+
const { childrenData = [] } = attributes;
185+
const { model } = this.context;
186186

187187
if (type === 'descendant-count') return model.getDescendantsData(this.id).length.toString();
188188
if (type === 'child-count') return childrenData.length.toString();
@@ -196,12 +196,12 @@ export abstract class BaseCombo<S extends BaseComboStyleProps = BaseComboStylePr
196196
}
197197

198198
public getComboPosition(attributes: Required<S>): Point {
199-
const { x = 0, y = 0, collapsed, context, childrenData = [] } = attributes;
199+
const { x = 0, y = 0, collapsed, childrenData = [] } = attributes;
200200

201201
if (childrenData.length === 0) return [+x, +y, 0];
202202

203203
if (collapsed) {
204-
const { model } = context!;
204+
const { model } = this.context;
205205
const descendants = model.getDescendantsData(this.id).filter((datum) => !model.isCombo(idOf(datum)));
206206

207207
if (descendants.length > 0 && descendants.some(hasPosition)) {

packages/g6/src/elements/edges/base-edge.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Image, Path } from '@antv/g';
33
import type { PathArray } from '@antv/util';
44
import { isFunction, pick } from '@antv/util';
55
import type {
6-
BaseElementStyleProps,
76
Edge,
87
EdgeArrowStyleProps,
98
EdgeBadgeStyleProps,
@@ -26,7 +25,7 @@ import * as Symbol from '../../utils/symbol';
2625
import { getWordWrapWidthByEnds } from '../../utils/text';
2726
import { BaseElement } from '../base-element';
2827
import { effect } from '../effect';
29-
import type { BadgeStyleProps, LabelStyleProps } from '../shapes';
28+
import type { BadgeStyleProps, BaseShapeStyleProps, LabelStyleProps } from '../shapes';
3029
import { Badge, Label } from '../shapes';
3130

3231
/**
@@ -35,7 +34,7 @@ import { Badge, Label } from '../shapes';
3534
* <en/> Base style properties of the edge
3635
*/
3736
export interface BaseEdgeStyleProps
38-
extends BaseElementStyleProps,
37+
extends BaseShapeStyleProps,
3938
Prefix<'label', EdgeLabelStyleProps>,
4039
Prefix<'halo', PathStyleProps>,
4140
Prefix<'badge', EdgeBadgeStyleProps>,
@@ -219,13 +218,13 @@ export abstract class BaseEdge extends BaseElement<BaseEdgeStyleProps> implement
219218
}
220219

221220
protected get sourceNode() {
222-
const { context, sourceNode: source } = this.parsedAttributes;
223-
return context.element!.getElement<Node>(source)!;
221+
const { sourceNode: source } = this.parsedAttributes;
222+
return this.context.element!.getElement<Node>(source)!;
224223
}
225224

226225
protected get targetNode() {
227-
const { context, targetNode: target } = this.parsedAttributes;
228-
return context.element!.getElement<Node>(target)!;
226+
const { targetNode: target } = this.parsedAttributes;
227+
return this.context.element!.getElement<Node>(target)!;
229228
}
230229

231230
protected getKeyStyle(attributes: ParsedBaseEdgeStyleProps): PathStyleProps {

packages/g6/src/elements/nodes/base-node.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type { CategoricalPalette } from '../../palettes/types';
44
import type { RuntimeContext } from '../../runtime/types';
55
import type { NodeData } from '../../spec';
66
import type {
7-
BaseElementStyleProps,
87
ID,
98
Node,
109
NodeBadgeStyleProps,
@@ -28,7 +27,7 @@ import { getWordWrapWidthByBox } from '../../utils/text';
2827
import { setVisibility } from '../../utils/visibility';
2928
import { BaseElement } from '../base-element';
3029
import { effect } from '../effect';
31-
import type { BadgeStyleProps, IconStyleProps, LabelStyleProps } from '../shapes';
30+
import type { BadgeStyleProps, BaseShapeStyleProps, IconStyleProps, LabelStyleProps } from '../shapes';
3231
import { Badge, Icon, Label } from '../shapes';
3332
import { connectImage, dispatchPositionChange } from '../shapes/image';
3433

@@ -38,7 +37,7 @@ import { connectImage, dispatchPositionChange } from '../shapes/image';
3837
* <en/> Base node style props
3938
*/
4039
export interface BaseNodeStyleProps
41-
extends BaseElementStyleProps,
40+
extends BaseShapeStyleProps,
4241
Prefix<'label', NodeLabelStyleProps>,
4342
Prefix<'halo', BaseStyleProps>,
4443
Prefix<'icon', IconStyleProps>,

packages/g6/src/elements/shapes/base-shape.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,7 @@ export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends
160160
*/
161161
public getGraphicStyle<T extends Record<string, any>>(
162162
style: T,
163-
): Omit<
164-
T,
165-
'x' | 'y' | 'z' | 'transform' | 'transformOrigin' | 'className' | 'class' | 'context' | 'zIndex' | 'visibility'
166-
> {
163+
): Omit<T, 'x' | 'y' | 'z' | 'transform' | 'transformOrigin' | 'className' | 'class' | 'zIndex' | 'visibility'> {
167164
return getSubShapeStyle(style);
168165
}
169166

packages/g6/src/exports.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ export type {
234234
} from './transforms';
235235
export type { DrawData } from './transforms/types';
236236
export type {
237-
BaseElementStyleProps,
238237
CardinalPlacement,
239238
CollapsedMarkerStyleProps,
240239
Combo,

packages/g6/src/global.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import '@antv/g';
2+
import type { RuntimeContext } from './runtime/types';
23

34
declare module '@antv/g' {
45
interface BaseStyleProps {
@@ -9,4 +10,8 @@ declare module '@antv/g' {
910
*/
1011
$layer?: string;
1112
}
13+
14+
interface DisplayObjectConfig {
15+
context?: RuntimeContext;
16+
}
1217
}

packages/g6/src/runtime/element.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -387,16 +387,11 @@ export class ElementController {
387387

388388
this.emit(new ElementLifeCycleEvent(GraphEvent.BEFORE_ELEMENT_CREATE, elementType, datum), context);
389389

390-
// if (context.stage === 'expand') {
391-
// // 如果是展开的元素,需要将其 zIndex 提升至目标元素的上层
392-
// const targetZIndex = this.getElementZIndex(context.target!);
393-
// if (!style.zIndex || style.zIndex < targetZIndex) style.zIndex = targetZIndex + (style.zIndex ?? 0);
394-
// }
395390
const element = this.container.appendChild(
396391
new Ctor({
397392
id,
393+
context: this.context,
398394
style: {
399-
context: this.context,
400395
...style,
401396
},
402397
}),

0 commit comments

Comments
 (0)