Skip to content

Commit 373db63

Browse files
committed
feat: add html node
1 parent a79efb4 commit 373db63

File tree

6 files changed

+164
-17
lines changed

6 files changed

+164
-17
lines changed

packages/core/index.html

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,30 @@
4343
border: 2px solid #000;
4444
background: #fff;
4545
}
46+
.uml-wrapper {
47+
background: #68FCE2;
48+
width: 100%;
49+
height: 100%;
50+
border-radius: 10px;
51+
border: 2px solid #838382;
52+
box-sizing: border-box;
53+
}
54+
.uml-head {
55+
text-align: center;
56+
line-height: 30px;
57+
font-size: 16px;
58+
font-weight: bold;
59+
}
60+
.uml-body {
61+
border-top: 1px solid #838382;
62+
border-bottom: 1px solid #838382;
63+
padding: 5px 10px;
64+
font-size: 12px;
65+
}
66+
.uml-footer {
67+
padding: 5px 10px;
68+
font-size: 14px;
69+
}
4670
</style>
4771
</head>
4872

@@ -70,6 +94,42 @@
7094

7195
<!-- 自定义节点 -->
7296
<script>
97+
class UmlModel extends HtmlNodeModel {
98+
setAttributes() {
99+
const width = 200;
100+
const height = 130;
101+
this.width = width;
102+
this.height = height;
103+
this.anchorsOffset = [
104+
[width / 2, 0],
105+
[0, height / 2],
106+
[-width / 2, 0],
107+
[0, -height/2],
108+
]
109+
}
110+
}
111+
class UmlNode extends HtmlNode {
112+
setHtml(rootEl) {
113+
console.log(111)
114+
const el = document.createElement('div');
115+
el.className = 'uml-wrapper';
116+
const html = `
117+
<div>
118+
<div class="uml-head">Head</div>
119+
<div class="uml-body">
120+
<div>+ $Name</div>
121+
<div>+ $Body</div>
122+
</div>
123+
<div class="uml-footer">
124+
<div>+ setHead(Head $head)</div>
125+
<div>+ setBody(Body $body)</div>
126+
</div>
127+
</div>
128+
`
129+
el.innerHTML = html;
130+
rootEl.appendChild(el);
131+
}
132+
}
73133
// square
74134
class SquareModel extends RectNodeModel {
75135
setAttributes() {
@@ -275,14 +335,18 @@
275335
view: Connection,
276336
model: LineEdgeModel,
277337
})
278-
338+
lf.register({
339+
type: 'uml',
340+
view: UmlNode,
341+
model: UmlModel,
342+
})
279343
lf.setTheme({});
280344
lf.setDefaultEdgeType('polyline');
281345

282346
lf.render({
283347
nodes: [
284348
{
285-
type: 'rect',
349+
type: 'uml',
286350
x: 100,
287351
y: 100,
288352
id: 10,
@@ -378,21 +442,6 @@
378442
}
379443
]
380444
});
381-
382-
// console.log(lf.graphModel);
383-
// lf.createEdge({
384-
// sourceNodeId: 10,
385-
// targetNodeId: 11,
386-
// // text: '222',
387-
// });
388-
// lf.updateText('edge_2222', '你是打算啊')
389-
// lf.createEdge({
390-
// sourceNodeId: 10,
391-
// targetNodeId: 50,
392-
// text: '222',
393-
// type: 'bezier'
394-
// });
395-
// console.log(lf.graphModel);
396445
}
397446
</script>
398447

packages/core/src/LogicFlow.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,11 @@ export default class LogicFlow {
274274
view: _View.DiamondNode,
275275
model: _Model.DiamondNodeModel,
276276
});
277+
this._registerElement({
278+
type: 'html',
279+
view: _View.HtmlNode,
280+
model: _Model.HtmlNodeModel,
281+
});
277282
}
278283

279284
// 全局操作----------------------------------------------
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { computed, observable } from 'mobx';
2+
import { Point } from '../../type';
3+
import BaseNodeModel from './BaseNodeModel';
4+
import { ModelType } from '../../constant/constant';
5+
import GraphModel from '../GraphModel';
6+
import { defaultTheme } from '../../constant/DefaultTheme';
7+
8+
class HtmlNodeModel extends BaseNodeModel {
9+
modelType = ModelType.RECT_NODE;
10+
@observable radius = defaultTheme.rect.radius;
11+
12+
constructor(data, graphModel: GraphModel) {
13+
super(data, graphModel, 'rect');
14+
}
15+
16+
@computed get anchors(): Point[] {
17+
const {
18+
anchorsOffset, x, y, width, height,
19+
} = this;
20+
if (anchorsOffset && anchorsOffset.length > 0) {
21+
return this.getAnchorsByOffset();
22+
}
23+
return [
24+
{ x, y: y - height / 2 },
25+
{ x: x + width / 2, y },
26+
{ x, y: y + height / 2 },
27+
{ x: x - width / 2, y },
28+
];
29+
}
30+
}
31+
32+
export { HtmlNodeModel };
33+
export default HtmlNodeModel;

packages/core/src/model/node/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from './EllipseNodeModel';
55
export * from './PolygonNodeModel';
66
export * from './RectNodeModel';
77
export * from './TextNodeModel';
8+
export * from './HtmlNodeModel';
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { h } from 'preact';
2+
import BaseNode from './BaseNode';
3+
import { RectNodeModel } from '../../model';
4+
import GraphModel from '../../model/GraphModel';
5+
import EventEmitter from '../../event/eventEmitter';
6+
7+
type IProps = {
8+
model: RectNodeModel;
9+
graphModel: GraphModel;
10+
eventCenter: EventEmitter;
11+
};
12+
13+
export default class HtmlNode extends BaseNode {
14+
rootElId: string;
15+
constructor(props) {
16+
super(props);
17+
this.rootElId = `htmlNode_${props.model.id}`;
18+
}
19+
get rootEl() {
20+
return document.querySelector(`#${this.rootElId}`) as HTMLElement;
21+
}
22+
getShapeStyle() {
23+
const attributes = super.getShapeStyle();
24+
const { model: { radius } } = this.props as IProps;
25+
return {
26+
...attributes,
27+
radius,
28+
};
29+
}
30+
getAttributes() {
31+
const attributes = super.getAttributes();
32+
const style = this.getShapeStyle();
33+
return {
34+
...attributes,
35+
...style,
36+
};
37+
}
38+
setHtml(rootEl: HTMLElement) {
39+
rootEl.appendChild(document.createElement('div'));
40+
}
41+
componentDidMount() {
42+
this.setHtml(this.rootEl);
43+
}
44+
getShape() {
45+
const attributes = this.getAttributes();
46+
const { x, y, width, height } = attributes;
47+
return (
48+
<foreignObject
49+
x={x - width / 2}
50+
y={y - height / 2}
51+
width={width}
52+
height={height}
53+
id={this.rootElId}
54+
/>
55+
);
56+
}
57+
}

packages/core/src/view/node/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import PolygonNode from './PolygonNode';
55
import DiamondNode from './DiamondNode';
66
import EllipseNode from './EllipseNode';
77
import TextNode from './TextNode';
8+
import HtmlNode from './HtmlNode';
89

910
export {
1011
BaseNode,
@@ -14,4 +15,5 @@ export {
1415
DiamondNode,
1516
EllipseNode,
1617
TextNode,
18+
HtmlNode,
1719
};

0 commit comments

Comments
 (0)