@tradingaction/utils 2.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +24 -0
- package/README.md +5 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +3 -0
- package/lib/index.js.map +1 -0
- package/lib/withDeviceRatio.d.ts +62 -0
- package/lib/withDeviceRatio.js +35 -0
- package/lib/withDeviceRatio.js.map +1 -0
- package/lib/withSize.d.ts +59 -0
- package/lib/withSize.js +14 -0
- package/lib/withSize.js.map +1 -0
- package/package.json +48 -0
- package/src/index.ts +2 -0
- package/src/withDeviceRatio.tsx +53 -0
- package/src/withSize.tsx +23 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
https://github.com/reactivemarkets/react-financial-charts
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2015-2018 Ragu Ramaswamy
|
|
5
|
+
Copyright (c) 2016 Julien Renaux
|
|
6
|
+
Copyright (c) 2019 Reactive Markets
|
|
7
|
+
|
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
9
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
10
|
+
in the Software without restriction, including without limitation the rights
|
|
11
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
13
|
+
furnished to do so, subject to the following conditions:
|
|
14
|
+
|
|
15
|
+
The above copyright notice and this permission notice shall be included in
|
|
16
|
+
all copies or substantial portions of the Software.
|
|
17
|
+
|
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
24
|
+
THE SOFTWARE.
|
package/README.md
ADDED
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
export interface WithRatioProps {
|
|
3
|
+
readonly ratio: number;
|
|
4
|
+
}
|
|
5
|
+
export interface WithRatioState {
|
|
6
|
+
ratio: number;
|
|
7
|
+
}
|
|
8
|
+
export declare const withDeviceRatio: () => <TProps extends WithRatioProps>(OriginalComponent: React.ComponentClass<TProps, any>) => {
|
|
9
|
+
new (props: Omit<TProps, "ratio"> | Readonly<Omit<TProps, "ratio">>): {
|
|
10
|
+
readonly ref: React.RefObject<HTMLCanvasElement>;
|
|
11
|
+
componentDidMount(): void;
|
|
12
|
+
render(): JSX.Element;
|
|
13
|
+
context: any;
|
|
14
|
+
setState<K extends "ratio">(state: WithRatioState | ((prevState: Readonly<WithRatioState>, props: Readonly<Omit<TProps, "ratio">>) => WithRatioState | Pick<WithRatioState, K> | null) | Pick<WithRatioState, K> | null, callback?: (() => void) | undefined): void;
|
|
15
|
+
forceUpdate(callback?: (() => void) | undefined): void;
|
|
16
|
+
readonly props: Readonly<Omit<TProps, "ratio">> & Readonly<{
|
|
17
|
+
children?: React.ReactNode;
|
|
18
|
+
}>;
|
|
19
|
+
state: Readonly<WithRatioState>;
|
|
20
|
+
refs: {
|
|
21
|
+
[key: string]: React.ReactInstance;
|
|
22
|
+
};
|
|
23
|
+
shouldComponentUpdate?(nextProps: Readonly<Omit<TProps, "ratio">>, nextState: Readonly<WithRatioState>, nextContext: any): boolean;
|
|
24
|
+
componentWillUnmount?(): void;
|
|
25
|
+
componentDidCatch?(error: Error, errorInfo: React.ErrorInfo): void;
|
|
26
|
+
getSnapshotBeforeUpdate?(prevProps: Readonly<Omit<TProps, "ratio">>, prevState: Readonly<WithRatioState>): any;
|
|
27
|
+
componentDidUpdate?(prevProps: Readonly<Omit<TProps, "ratio">>, prevState: Readonly<WithRatioState>, snapshot?: any): void;
|
|
28
|
+
componentWillMount?(): void;
|
|
29
|
+
UNSAFE_componentWillMount?(): void;
|
|
30
|
+
componentWillReceiveProps?(nextProps: Readonly<Omit<TProps, "ratio">>, nextContext: any): void;
|
|
31
|
+
UNSAFE_componentWillReceiveProps?(nextProps: Readonly<Omit<TProps, "ratio">>, nextContext: any): void;
|
|
32
|
+
componentWillUpdate?(nextProps: Readonly<Omit<TProps, "ratio">>, nextState: Readonly<WithRatioState>, nextContext: any): void;
|
|
33
|
+
UNSAFE_componentWillUpdate?(nextProps: Readonly<Omit<TProps, "ratio">>, nextState: Readonly<WithRatioState>, nextContext: any): void;
|
|
34
|
+
};
|
|
35
|
+
new (props: Omit<TProps, "ratio">, context: any): {
|
|
36
|
+
readonly ref: React.RefObject<HTMLCanvasElement>;
|
|
37
|
+
componentDidMount(): void;
|
|
38
|
+
render(): JSX.Element;
|
|
39
|
+
context: any;
|
|
40
|
+
setState<K extends "ratio">(state: WithRatioState | ((prevState: Readonly<WithRatioState>, props: Readonly<Omit<TProps, "ratio">>) => WithRatioState | Pick<WithRatioState, K> | null) | Pick<WithRatioState, K> | null, callback?: (() => void) | undefined): void;
|
|
41
|
+
forceUpdate(callback?: (() => void) | undefined): void;
|
|
42
|
+
readonly props: Readonly<Omit<TProps, "ratio">> & Readonly<{
|
|
43
|
+
children?: React.ReactNode;
|
|
44
|
+
}>;
|
|
45
|
+
state: Readonly<WithRatioState>;
|
|
46
|
+
refs: {
|
|
47
|
+
[key: string]: React.ReactInstance;
|
|
48
|
+
};
|
|
49
|
+
shouldComponentUpdate?(nextProps: Readonly<Omit<TProps, "ratio">>, nextState: Readonly<WithRatioState>, nextContext: any): boolean;
|
|
50
|
+
componentWillUnmount?(): void;
|
|
51
|
+
componentDidCatch?(error: Error, errorInfo: React.ErrorInfo): void;
|
|
52
|
+
getSnapshotBeforeUpdate?(prevProps: Readonly<Omit<TProps, "ratio">>, prevState: Readonly<WithRatioState>): any;
|
|
53
|
+
componentDidUpdate?(prevProps: Readonly<Omit<TProps, "ratio">>, prevState: Readonly<WithRatioState>, snapshot?: any): void;
|
|
54
|
+
componentWillMount?(): void;
|
|
55
|
+
UNSAFE_componentWillMount?(): void;
|
|
56
|
+
componentWillReceiveProps?(nextProps: Readonly<Omit<TProps, "ratio">>, nextContext: any): void;
|
|
57
|
+
UNSAFE_componentWillReceiveProps?(nextProps: Readonly<Omit<TProps, "ratio">>, nextContext: any): void;
|
|
58
|
+
componentWillUpdate?(nextProps: Readonly<Omit<TProps, "ratio">>, nextState: Readonly<WithRatioState>, nextContext: any): void;
|
|
59
|
+
UNSAFE_componentWillUpdate?(nextProps: Readonly<Omit<TProps, "ratio">>, nextState: Readonly<WithRatioState>, nextContext: any): void;
|
|
60
|
+
};
|
|
61
|
+
contextType?: React.Context<any> | undefined;
|
|
62
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
export const withDeviceRatio = () => {
|
|
3
|
+
return (OriginalComponent) => {
|
|
4
|
+
return class WithRatio extends React.Component {
|
|
5
|
+
constructor() {
|
|
6
|
+
super(...arguments);
|
|
7
|
+
this.ref = React.createRef();
|
|
8
|
+
}
|
|
9
|
+
componentDidMount() {
|
|
10
|
+
var _a, _b, _c, _d, _e;
|
|
11
|
+
const { current } = this.ref;
|
|
12
|
+
if (current === null) {
|
|
13
|
+
this.setState({
|
|
14
|
+
ratio: 1,
|
|
15
|
+
});
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const context = current.getContext("2d");
|
|
19
|
+
const { devicePixelRatio } = window;
|
|
20
|
+
const backingStoreRatio = (_e = (_d = (_c = (_b = (_a = context.webkitBackingStorePixelRatio) !== null && _a !== void 0 ? _a : context.mozBackingStorePixelRatio) !== null && _b !== void 0 ? _b : context.msBackingStorePixelRatio) !== null && _c !== void 0 ? _c : context.oBackingStorePixelRatio) !== null && _d !== void 0 ? _d : context.backingStorePixelRatio) !== null && _e !== void 0 ? _e : 1;
|
|
21
|
+
this.setState({
|
|
22
|
+
ratio: devicePixelRatio / backingStoreRatio,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
render() {
|
|
26
|
+
const state = this.state;
|
|
27
|
+
if (state !== null) {
|
|
28
|
+
return React.createElement(OriginalComponent, Object.assign({}, this.props, { ratio: state.ratio }));
|
|
29
|
+
}
|
|
30
|
+
return React.createElement("canvas", { ref: this.ref });
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
//# sourceMappingURL=withDeviceRatio.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"withDeviceRatio.js","sourceRoot":"","sources":["../src/withDeviceRatio.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAU/B,MAAM,CAAC,MAAM,eAAe,GAAG,GAAG,EAAE;IAChC,OAAO,CAAgC,iBAA+C,EAAE,EAAE;QACtF,OAAO,MAAM,SAAU,SAAQ,KAAK,CAAC,SAAgD;YAA9E;;gBACa,QAAG,GAAG,KAAK,CAAC,SAAS,EAAqB,CAAC;YAqC/D,CAAC;YAnCU,iBAAiB;;gBACpB,MAAM,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC,GAAG,CAAC;gBAC7B,IAAI,OAAO,KAAK,IAAI,EAAE;oBAClB,IAAI,CAAC,QAAQ,CAAC;wBACV,KAAK,EAAE,CAAC;qBACX,CAAC,CAAC;oBAEH,OAAO;iBACV;gBAED,MAAM,OAAO,GAAQ,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;gBAE9C,MAAM,EAAE,gBAAgB,EAAE,GAAG,MAAM,CAAC;gBAEpC,MAAM,iBAAiB,GACnB,MAAA,MAAA,MAAA,MAAA,MAAA,OAAO,CAAC,4BAA4B,mCACpC,OAAO,CAAC,yBAAyB,mCACjC,OAAO,CAAC,wBAAwB,mCAChC,OAAO,CAAC,uBAAuB,mCAC/B,OAAO,CAAC,sBAAsB,mCAC9B,CAAC,CAAC;gBAEN,IAAI,CAAC,QAAQ,CAAC;oBACV,KAAK,EAAE,gBAAgB,GAAG,iBAAiB;iBAC9C,CAAC,CAAC;YACP,CAAC;YAEM,MAAM;gBACT,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;gBACzB,IAAI,KAAK,KAAK,IAAI,EAAE;oBAChB,OAAO,oBAAC,iBAAiB,oBAAM,IAAI,CAAC,KAAgB,IAAE,KAAK,EAAE,KAAK,CAAC,KAAK,IAAI,CAAC;iBAChF;gBAED,OAAO,gCAAQ,GAAG,EAAE,IAAI,CAAC,GAAG,GAAI,CAAC;YACrC,CAAC;SACJ,CAAC;IACN,CAAC,CAAC;AACN,CAAC,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { Props as AutoSizerProps } from "react-virtualized-auto-sizer";
|
|
3
|
+
export interface WithSizeProps {
|
|
4
|
+
readonly width: number;
|
|
5
|
+
readonly height: number;
|
|
6
|
+
}
|
|
7
|
+
export declare const withSize: (props?: Omit<AutoSizerProps, "children">) => <TProps extends WithSizeProps>(OriginalComponent: React.ComponentClass<TProps, any>) => {
|
|
8
|
+
new (props: Omit<TProps, "height" | "width"> | Readonly<Omit<TProps, "height" | "width">>): {
|
|
9
|
+
render(): JSX.Element;
|
|
10
|
+
context: any;
|
|
11
|
+
setState<K extends never>(state: {} | ((prevState: Readonly<{}>, props: Readonly<Omit<TProps, "height" | "width">>) => {} | Pick<{}, K> | null) | Pick<{}, K> | null, callback?: (() => void) | undefined): void;
|
|
12
|
+
forceUpdate(callback?: (() => void) | undefined): void;
|
|
13
|
+
readonly props: Readonly<Omit<TProps, "height" | "width">> & Readonly<{
|
|
14
|
+
children?: React.ReactNode;
|
|
15
|
+
}>;
|
|
16
|
+
state: Readonly<{}>;
|
|
17
|
+
refs: {
|
|
18
|
+
[key: string]: React.ReactInstance;
|
|
19
|
+
};
|
|
20
|
+
componentDidMount?(): void;
|
|
21
|
+
shouldComponentUpdate?(nextProps: Readonly<Omit<TProps, "height" | "width">>, nextState: Readonly<{}>, nextContext: any): boolean;
|
|
22
|
+
componentWillUnmount?(): void;
|
|
23
|
+
componentDidCatch?(error: Error, errorInfo: React.ErrorInfo): void;
|
|
24
|
+
getSnapshotBeforeUpdate?(prevProps: Readonly<Omit<TProps, "height" | "width">>, prevState: Readonly<{}>): any;
|
|
25
|
+
componentDidUpdate?(prevProps: Readonly<Omit<TProps, "height" | "width">>, prevState: Readonly<{}>, snapshot?: any): void;
|
|
26
|
+
componentWillMount?(): void;
|
|
27
|
+
UNSAFE_componentWillMount?(): void;
|
|
28
|
+
componentWillReceiveProps?(nextProps: Readonly<Omit<TProps, "height" | "width">>, nextContext: any): void;
|
|
29
|
+
UNSAFE_componentWillReceiveProps?(nextProps: Readonly<Omit<TProps, "height" | "width">>, nextContext: any): void;
|
|
30
|
+
componentWillUpdate?(nextProps: Readonly<Omit<TProps, "height" | "width">>, nextState: Readonly<{}>, nextContext: any): void;
|
|
31
|
+
UNSAFE_componentWillUpdate?(nextProps: Readonly<Omit<TProps, "height" | "width">>, nextState: Readonly<{}>, nextContext: any): void;
|
|
32
|
+
};
|
|
33
|
+
new (props: Omit<TProps, "height" | "width">, context: any): {
|
|
34
|
+
render(): JSX.Element;
|
|
35
|
+
context: any;
|
|
36
|
+
setState<K extends never>(state: {} | ((prevState: Readonly<{}>, props: Readonly<Omit<TProps, "height" | "width">>) => {} | Pick<{}, K> | null) | Pick<{}, K> | null, callback?: (() => void) | undefined): void;
|
|
37
|
+
forceUpdate(callback?: (() => void) | undefined): void;
|
|
38
|
+
readonly props: Readonly<Omit<TProps, "height" | "width">> & Readonly<{
|
|
39
|
+
children?: React.ReactNode;
|
|
40
|
+
}>;
|
|
41
|
+
state: Readonly<{}>;
|
|
42
|
+
refs: {
|
|
43
|
+
[key: string]: React.ReactInstance;
|
|
44
|
+
};
|
|
45
|
+
componentDidMount?(): void;
|
|
46
|
+
shouldComponentUpdate?(nextProps: Readonly<Omit<TProps, "height" | "width">>, nextState: Readonly<{}>, nextContext: any): boolean;
|
|
47
|
+
componentWillUnmount?(): void;
|
|
48
|
+
componentDidCatch?(error: Error, errorInfo: React.ErrorInfo): void;
|
|
49
|
+
getSnapshotBeforeUpdate?(prevProps: Readonly<Omit<TProps, "height" | "width">>, prevState: Readonly<{}>): any;
|
|
50
|
+
componentDidUpdate?(prevProps: Readonly<Omit<TProps, "height" | "width">>, prevState: Readonly<{}>, snapshot?: any): void;
|
|
51
|
+
componentWillMount?(): void;
|
|
52
|
+
UNSAFE_componentWillMount?(): void;
|
|
53
|
+
componentWillReceiveProps?(nextProps: Readonly<Omit<TProps, "height" | "width">>, nextContext: any): void;
|
|
54
|
+
UNSAFE_componentWillReceiveProps?(nextProps: Readonly<Omit<TProps, "height" | "width">>, nextContext: any): void;
|
|
55
|
+
componentWillUpdate?(nextProps: Readonly<Omit<TProps, "height" | "width">>, nextState: Readonly<{}>, nextContext: any): void;
|
|
56
|
+
UNSAFE_componentWillUpdate?(nextProps: Readonly<Omit<TProps, "height" | "width">>, nextState: Readonly<{}>, nextContext: any): void;
|
|
57
|
+
};
|
|
58
|
+
contextType?: React.Context<any> | undefined;
|
|
59
|
+
};
|
package/lib/withSize.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import AutoSizer from "react-virtualized-auto-sizer";
|
|
3
|
+
export const withSize = (props) => {
|
|
4
|
+
return (OriginalComponent) => {
|
|
5
|
+
return class WithSize extends React.Component {
|
|
6
|
+
render() {
|
|
7
|
+
return (React.createElement(AutoSizer, Object.assign({}, props), ({ height, width }) => {
|
|
8
|
+
return React.createElement(OriginalComponent, Object.assign({}, this.props, { height: height, width: width }));
|
|
9
|
+
}));
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=withSize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"withSize.js","sourceRoot":"","sources":["../src/withSize.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,SAAsC,MAAM,8BAA8B,CAAC;AAOlF,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,KAAwC,EAAE,EAAE;IACjE,OAAO,CAA+B,iBAA+C,EAAE,EAAE;QACrF,OAAO,MAAM,QAAS,SAAQ,KAAK,CAAC,SAA2C;YACpE,MAAM;gBACT,OAAO,CACH,oBAAC,SAAS,oBAAK,KAAK,GACf,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,EAAE;oBACnB,OAAO,oBAAC,iBAAiB,oBAAM,IAAI,CAAC,KAAgB,IAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,IAAI,CAAC;gBAC3F,CAAC,CACO,CACf,CAAC;YACN,CAAC;SACJ,CAAC;IACN,CAAC,CAAC;AACN,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tradingaction/utils",
|
|
3
|
+
"version": "2.0.13",
|
|
4
|
+
"description": "Utils for react-financial-charts",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"main": "./lib/index.js",
|
|
9
|
+
"typings": "./lib/index.d.ts",
|
|
10
|
+
"files": [
|
|
11
|
+
"lib",
|
|
12
|
+
"src"
|
|
13
|
+
],
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"author": "Reactive Markets",
|
|
16
|
+
"keywords": [
|
|
17
|
+
"charts",
|
|
18
|
+
"charting",
|
|
19
|
+
"stockcharts",
|
|
20
|
+
"finance",
|
|
21
|
+
"financial",
|
|
22
|
+
"finance-chart",
|
|
23
|
+
"react",
|
|
24
|
+
"d3"
|
|
25
|
+
],
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "https://github.com/reactivemarkets/react-financial-charts.git"
|
|
30
|
+
},
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/reactivemarkets/react-financial-charts/issues"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "npm run clean && npm run compile",
|
|
36
|
+
"clean": "rimraf lib",
|
|
37
|
+
"compile": "tsc -p tsconfig.json",
|
|
38
|
+
"watch": "tsc -p tsconfig.json --watch --preserveWatchOutput"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"react-virtualized-auto-sizer": "^1.0.16"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"react": "^16.0.0 || ^17.0.0 || ^18.0.0",
|
|
45
|
+
"react-dom": "^16.0.0 || ^17.0.0 || ^18.0.0"
|
|
46
|
+
},
|
|
47
|
+
"gitHead": "9c9bc635a2291c8da0e1dd5befa4000e96d83119"
|
|
48
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
export interface WithRatioProps {
|
|
4
|
+
readonly ratio: number;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface WithRatioState {
|
|
8
|
+
ratio: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const withDeviceRatio = () => {
|
|
12
|
+
return <TProps extends WithRatioProps>(OriginalComponent: React.ComponentClass<TProps>) => {
|
|
13
|
+
return class WithRatio extends React.Component<Omit<TProps, "ratio">, WithRatioState> {
|
|
14
|
+
public readonly ref = React.createRef<HTMLCanvasElement>();
|
|
15
|
+
|
|
16
|
+
public componentDidMount() {
|
|
17
|
+
const { current } = this.ref;
|
|
18
|
+
if (current === null) {
|
|
19
|
+
this.setState({
|
|
20
|
+
ratio: 1,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const context: any = current.getContext("2d");
|
|
27
|
+
|
|
28
|
+
const { devicePixelRatio } = window;
|
|
29
|
+
|
|
30
|
+
const backingStoreRatio =
|
|
31
|
+
context.webkitBackingStorePixelRatio ??
|
|
32
|
+
context.mozBackingStorePixelRatio ??
|
|
33
|
+
context.msBackingStorePixelRatio ??
|
|
34
|
+
context.oBackingStorePixelRatio ??
|
|
35
|
+
context.backingStorePixelRatio ??
|
|
36
|
+
1;
|
|
37
|
+
|
|
38
|
+
this.setState({
|
|
39
|
+
ratio: devicePixelRatio / backingStoreRatio,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public render() {
|
|
44
|
+
const state = this.state;
|
|
45
|
+
if (state !== null) {
|
|
46
|
+
return <OriginalComponent {...(this.props as TProps)} ratio={state.ratio} />;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return <canvas ref={this.ref} />;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
};
|
package/src/withSize.tsx
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import AutoSizer, { Props as AutoSizerProps } from "react-virtualized-auto-sizer";
|
|
3
|
+
|
|
4
|
+
export interface WithSizeProps {
|
|
5
|
+
readonly width: number;
|
|
6
|
+
readonly height: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const withSize = (props?: Omit<AutoSizerProps, "children">) => {
|
|
10
|
+
return <TProps extends WithSizeProps>(OriginalComponent: React.ComponentClass<TProps>) => {
|
|
11
|
+
return class WithSize extends React.Component<Omit<TProps, "width" | "height">> {
|
|
12
|
+
public render() {
|
|
13
|
+
return (
|
|
14
|
+
<AutoSizer {...props}>
|
|
15
|
+
{({ height, width }) => {
|
|
16
|
+
return <OriginalComponent {...(this.props as TProps)} height={height} width={width} />;
|
|
17
|
+
}}
|
|
18
|
+
</AutoSizer>
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
};
|