Skip to content

Commit

Permalink
feat: 调整jsx库
Browse files Browse the repository at this point in the history
  • Loading branch information
zengyue committed Dec 25, 2020
1 parent 7e317ef commit d3dfacb
Show file tree
Hide file tree
Showing 20 changed files with 131 additions and 112 deletions.
4 changes: 2 additions & 2 deletions .fatherrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ export default {
'component'
],
cjs: {
type: 'babel'
// type: 'babel'
},
esm: {
type: 'babel'
// type: 'babel'
},
extraBabelPlugins: [
['@babel/plugin-transform-react-jsx', {
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ coverage/
*.log
.vscode/
.docz/
.DS_Store
.DS_Store
package-lock.json
5 changes: 3 additions & 2 deletions packages/jsx/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "f2-jsx",
"version": "0.0.1",
"main": "lib/index.js",
"module": "es/index.js",
"main": "dist/index.js",
"module": "dist/index.esm.js",
"types": "dist/index.d.ts",
"dependencies": {
"@antv/f2": "~3.8.1",
"css-layout": "1.1.1"
Expand Down
Empty file removed packages/jsx/src/element/group.ts
Empty file.
Empty file removed packages/jsx/src/element/index.ts
Empty file.
8 changes: 0 additions & 8 deletions packages/jsx/src/element/rect.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/jsx/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import render from './render';
export {
render
};
};
6 changes: 4 additions & 2 deletions packages/jsx/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ declare global {
render(): any;
}
interface IntrinsicElements {
// group: any;
// rect: any;
group: any;
rect: any;
circle: any;
text: any;
}
}
}
Expand Down
46 changes: 0 additions & 46 deletions packages/jsx/src/jsx-runtime.ts

This file was deleted.

11 changes: 5 additions & 6 deletions packages/jsx/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ function extendArray(arr: any[]) {
}

function createElement(node, container, parentLayout) {
const { type, props, style, layout, children } = node;
const { type, props, layout, children } = node;
const { attrs } = props;
if (type === 'group') {
const element = container.addGroup({
attrs: style,
});
const element = container.addGroup();
// 只有group才需要处理children
if (children && children.length) {
for (let i = 0, len = children.length; i < len; i++) {
Expand All @@ -38,14 +37,14 @@ function createElement(node, container, parentLayout) {
}
const { width, height, left, top } = layout;
return container.addShape(type, {
...props,
attrs: {
...style,
...attrs,
x: left,
y: top,
width,
height,
},
...props,
});
}

Expand Down
1 change: 0 additions & 1 deletion packages/jsx/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const isArray = is('Array');
const isObject = is('Object');



function batch2hd(value: any) {
// 处理带px的数据
if (isString(value) && /^-?\d+px$/.test(value)) {
Expand Down
48 changes: 20 additions & 28 deletions packages/jsx/test/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/** @jsxImportSource .. */
// @ts-nocheck
import { canvas } from './global';
import render from '../src/render';

Expand All @@ -8,43 +7,36 @@ describe('index', () => {
canvas.clear();
const profile = (
<group style={{
// width: 200,
width: 200,
height: 100,
flexDirection: 'row',
// padding: 20,
flex: 1,
}}>
<rect style={{
// width: 20,
// height: 20,
fill: 'red'
}} />
width: 100,
flex: 1,
}}
attrs={{
fill: 'red',
}}
/>
<rect style={{
// marginLeft: 10,
// width: 20,
// height: 20,
fill: 'green'
flex: 1,
}}
attrs={{
fill: 'green'
}} />
<text style={{
// width: 20,
// margin: 20,
text: '111',
fill: '#000'
flex: 1,
}}
attrs={{
textBaseline: 'top',
text: '111',
fill: '#000'
}} />
</group>
);
// console.log(profile);
render(profile, canvas);

console.log(canvas);
canvas.draw();

// const group = render(
// <view>
// <text style={{ color: '#000' }}>ss</text>
// <text style={{ color: 'red' }}>aaa</text>
// </view>, canvas);
// canvas.draw();
// expect(group.get('children').length).toBe(2);
// expect(group.get('children')[1].get('attrs').x).toBe(0);
// expect(group.get('children')[1].get('attrs').y).toBe(18);
});
});
5 changes: 4 additions & 1 deletion packages/jsx/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "../../tsconfig.json"
"extends": "../../tsconfig.json",
"compilerOptions": {
"typeRoots": [ "/node_modules" ]
}
}
18 changes: 18 additions & 0 deletions packages/legend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "f2x-legend",
"version": "0.0.1",
"main": "lib/index.js",
"module": "es/index.js",
"dependencies": {
"@antv/f2": "^3.8.1"
},
"devDependencies": {
"react": "^17.0.1",
"react-dom": "^17.0.1"
},
"files": [
"src",
"dist"
],
"author": "yezengyue@gmail.com"
}
33 changes: 33 additions & 0 deletions packages/legend/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// @ts-nocheck
/** @jsxImportSource ../../jsx */

function model(View) {
return class LegendModel {
render() {
return <View />
}
}
}


export default model((props) => {
return (
<group style={{
flexDirection: 'row',
padding: 20
}}>
<text style={{
x: 10,
y: 10,
text: `aa `,
fill: '#000'
}} />
<text style={{
x: 60,
y: 10,
text: 'sold',
fill: '#000'
}} />
</group>
);
})
3 changes: 3 additions & 0 deletions packages/legend/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../tsconfig.json"
}
1 change: 1 addition & 0 deletions packages/legend/typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '../../typings';
16 changes: 13 additions & 3 deletions packages/react/src/chart/model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect, useState, Children } from 'react';
import F2 from '@antv/f2';
import render from '../../../jsx/src/render';

export default ({ canvasRef, pixelRatio, data, children }) => {
// const [chart, setChart] = useState();
Expand All @@ -14,15 +15,24 @@ export default ({ canvasRef, pixelRatio, data, children }) => {

chart.source(data);

Children.map(children, child => {
const components = Children.map(children, child => {
const { type, props } = child;
return type({
return new type({
...props,
chart,
});
});

// const frontPlot = chart.get('frontPlot');
const frontPlot = chart.get('frontPlot');
// @ts-ignore
chart.on('aftergeomdraw', () => {
// component render
for (let i = 0, len = components.length; i < len; i++) {
const component = components[i];
console.log(component);
// render(component.render(), frontPlot);
}
});

chart.render();
return;
Expand Down
30 changes: 19 additions & 11 deletions packages/react/src/interval/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
// @ts-nocheck

export default (props) => {
const { chart, position } = props;
const geom = chart.interval();
geom.position(position);
console.log('interval props:', props);
return null;
function model(View) {
return class I {
init() {
const { chart, position } = this.props;
const geom = chart.interval();
geom.position(position);
}
render() {
return <View />
};
}
}

// export default class Interval {
// render() {
// return null;
// }
// }
export default model((props) => {
// const { chart, position } = props;
// const geom = chart.interval();
// geom.position(position);
// console.log('interval props:', props);
return null;
});

3 changes: 3 additions & 0 deletions packages/react/test/chart.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/** @jsxImportSource react */
// @ts-nocheck

import ReactDOM from 'react-dom';
import Chart from '../src/chart';
import Interval from '../src/interval';
import Legned from '../../legend/src/index';

const style = document.createElement('style');
style.setAttribute('rel', 'text/css');
Expand Down Expand Up @@ -34,6 +36,7 @@ const App = () => {
data={ data }
>
<Interval position="genre*sold" />
<Legned />
</Chart>
);
}
Expand Down

0 comments on commit d3dfacb

Please sign in to comment.