Skip to content

Commit

Permalink
fix: 调整一些细节
Browse files Browse the repository at this point in the history
  • Loading branch information
zengyue committed Oct 9, 2021
1 parent 95ff644 commit f79664a
Show file tree
Hide file tree
Showing 17 changed files with 256 additions and 183 deletions.
12 changes: 3 additions & 9 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,10 @@ module.exports = {
collectCoverage: false,
collectCoverageFrom: [
'packages/*/src/**/*.{ts,tsx,js}',
'!**/node_modules/**'
'!**/node_modules/**',
],
testRegex: '/test/.*\\.test\\.tsx?$',
moduleFileExtensions: [
'ts',
'tsx',
'js',
'json'
],
transform: {
"\\.(less|css)$": 'jest-less-loader'
}
'\\.(less|css)$': 'jest-less-loader',
},
};
12 changes: 5 additions & 7 deletions packages/f2-next/src/base/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Component {
context: any;
refs: {
[key: string]: any;
}
};
container: any;
layout: Layout;
updater: any;
Expand All @@ -33,22 +33,20 @@ class Component {
this.updater = updater;
this.state = {};
this.animate = animate;
this.__shouldRender = true;
}
init({ container, layout }) {
this.layout = layout;
this.container = container;
}
// TODO beforeMount
willMount() {
}
willMount() {}
// TODO mounted
mount() {
}
mount() {}
setState(partialState) {
this.updater.enqueueSetState(this, partialState);
}
beforeUpdate() {
}
beforeUpdate() {}
update(props: any) {
this.__props = props;
this.props = props;
Expand Down
100 changes: 60 additions & 40 deletions packages/f2-next/src/canvas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,35 @@ import { px2hd } from '../util';
import { createUpdater } from './updater';

interface ChartUpdateProps {
pixelRatio?: number,
width?: number | string,
height?: number | string,
padding?: (number | string)[],
animate?: boolean,
children?: any
pixelRatio?: number;
width?: number | string;
height?: number | string;
padding?: (number | string)[];
animate?: boolean;
children?: any;
}

interface ChartProps extends ChartUpdateProps {
context: any,
context: any;
}

interface IF2Canvas {
container: any;
container: any;
}

function measureText(context: CanvasRenderingContext2D) {
return (text: string, font) => {
const {
fontSize = 12,
fontFamily = 'normal',
fontStyle = 'normal',
fontWeight = 'normal',
fontVariant = 'normal',
} = this.px2hd(font);

context.font = `${fontStyle} ${fontVariant} ${fontWeight} ${fontSize}px ${fontFamily}`;
return context.measureText(text);
};
}

// 顶层Canvas标签
Expand All @@ -35,7 +50,14 @@ class Canvas extends Component implements IF2Canvas {

constructor(props: ChartProps) {
super(props);
const { context, pixelRatio, width, height, animate = true, children } = props;
const {
context,
pixelRatio,
width,
height,
animate = true,
children,
} = props;
this.context = context;

// 创建G的canvas
Expand All @@ -46,21 +68,36 @@ class Canvas extends Component implements IF2Canvas {
height,
});

const {
width: canvasWidth,
height: canvasHeight
} = canvas._attrs;
const { width: canvasWidth, height: canvasHeight } = canvas._attrs;

// 初始化默认的布局
const layout = new Layout({
width: canvasWidth,
height: canvasHeight
height: canvasHeight,
});

const updater = createUpdater(this);

const componentTree = createComponentTree(children, { canvas: this, width: canvasWidth, height: canvasHeight, context, layout });
const component = new Container({ children: componentTree, animate }, {}, updater);
const global = {
width: canvasWidth,
height: canvasHeight,
context,
px2hd,
measureText: measureText(context),
};

const componentTree = createComponentTree(children, {
canvas: this,
width: canvasWidth,
height: canvasHeight,
context,
layout,
});
const component = new Container(
{ children: componentTree, animate },
{},
updater
);
component.init({ layout, container: canvas });

this.canvas = canvas;
Expand Down Expand Up @@ -118,18 +155,15 @@ class Canvas extends Component implements IF2Canvas {
// 只处理数据,和children的变化
const { children } = props;

const {
const { width, height, context } = canvas._attrs;

const componentTree = createComponentTree(children, {
canvas: this,
width,
height,
context,
} = canvas._attrs;

const componentTree = createComponentTree(children, { canvas: this, width, height, context, layout });


// if (equal(this.componentTree, componentTree)) {
// return;
// }
layout,
});

component.update({ children: componentTree });
this.render();
Expand All @@ -140,21 +174,7 @@ class Canvas extends Component implements IF2Canvas {
component.destroy();
}

px2hd = px2hd

measureText(text, font) {
const { context } = this;
const {
fontSize = 12,
fontFamily = 'normal',
fontStyle = 'normal',
fontWeight = 'normal',
fontVariant = 'normal',
} = this.px2hd(font);

context.font = `${fontStyle} ${fontVariant} ${fontWeight} ${fontSize}px ${fontFamily}`;
return context.measureText(text);
}
px2hd = px2hd;
}

export default Canvas;
3 changes: 1 addition & 2 deletions packages/f2-next/src/canvas/updater.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

function createUpdater(canvas) {
const setStateQueue = [];
const renderQueue = [];
Expand All @@ -16,7 +15,7 @@ function createUpdater(canvas) {
}

// 如果stateChange是一个方法,也就是setState的第二种形式
if (typeof state === "function") {
if (typeof state === 'function') {
Object.assign(
component.state,
state(component.prevState, component.props)
Expand Down
1 change: 1 addition & 0 deletions packages/f2-next/src/chart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class Chart
let { left, top, width, height } = coordLayout;
// @ts-ignore
const childLayout = component.getLayout();
if (!childLayout) return;
const { position, width: childWidth, height: childHeight } = childLayout;
// @ts-ignore
component.setLayout({
Expand Down
8 changes: 8 additions & 0 deletions packages/f2-next/src/children.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { map, extendMap } from './util';

const Children = {
map,
toArray: extendMap,
};

export default Children;
Loading

0 comments on commit f79664a

Please sign in to comment.