@visactor/react-vrender-utils 0.9.0-alpha.0
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/README.md +9 -0
- package/cjs/Html.d.ts +16 -0
- package/cjs/Html.js +82 -0
- package/cjs/Html.js.map +1 -0
- package/cjs/index.d.ts +1 -0
- package/cjs/index.js +21 -0
- package/cjs/index.js.map +1 -0
- package/dist/reactvrender.utils.js.js +27 -0
- package/dist/reactvrender.utils.js.min.js +27 -0
- package/es/Html.d.ts +16 -0
- package/es/Html.js +55 -0
- package/es/Html.js.map +1 -0
- package/es/index.d.ts +1 -0
- package/es/index.js +2 -0
- package/es/index.js.map +1 -0
- package/package.json +50 -0
package/es/Html.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { HTMLAttributes, PropsWithChildren } from 'react';
|
|
2
|
+
export type HtmlTransformAttrs = {
|
|
3
|
+
x: number;
|
|
4
|
+
y: number;
|
|
5
|
+
scaleX: number;
|
|
6
|
+
scaleY: number;
|
|
7
|
+
rotateDeg: number;
|
|
8
|
+
skewX: number;
|
|
9
|
+
skewY: number;
|
|
10
|
+
};
|
|
11
|
+
export type HtmlProps = PropsWithChildren<{
|
|
12
|
+
divProps?: HTMLAttributes<HTMLDivElement>;
|
|
13
|
+
transform?: boolean;
|
|
14
|
+
transformFunc?: (attrs: HtmlTransformAttrs) => HtmlTransformAttrs;
|
|
15
|
+
}>;
|
|
16
|
+
export declare const Html: ({ children, divProps, transform, transformFunc }: HtmlProps) => JSX.Element;
|
package/es/Html.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
var __rest = this && this.__rest || function(s, e) {
|
|
2
|
+
var t = {};
|
|
3
|
+
for (var p in s) Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0 && (t[p] = s[p]);
|
|
4
|
+
if (null != s && "function" == typeof Object.getOwnPropertySymbols) {
|
|
5
|
+
var i = 0;
|
|
6
|
+
for (p = Object.getOwnPropertySymbols(s); i < p.length; i++) e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]) && (t[p[i]] = s[p[i]]);
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
import React from "react";
|
|
12
|
+
|
|
13
|
+
import * as ReactDOM from "react-dom";
|
|
14
|
+
|
|
15
|
+
import { ShadowRoot } from "@visactor/react-vrender";
|
|
16
|
+
|
|
17
|
+
const needForceStyle = el => {
|
|
18
|
+
const pos = window.getComputedStyle(el).position;
|
|
19
|
+
return !("absolute" === pos || "relative" === pos);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const Html = ({children: children, divProps: divProps, transform: transform, transformFunc: transformFunc}) => {
|
|
23
|
+
const groupRef = React.useRef(null), [div] = React.useState((() => document.createElement("div"))), root = React.useMemo((() => ReactDOM.createRoot(div)), [ div ]), shouldTransform = null == transform || transform, handleGroupRerender = () => {
|
|
24
|
+
if (shouldTransform && groupRef.current) {
|
|
25
|
+
if (!groupRef.current.shouldUpdateGlobalMatrix()) return;
|
|
26
|
+
let attrs = groupRef.current.globalTransMatrix.toTransformAttrs();
|
|
27
|
+
transformFunc && (attrs = transformFunc(attrs)), div.style.position = "absolute",
|
|
28
|
+
div.style.zIndex = "10", div.style.top = "0px", div.style.left = "0px", div.style.transform = `translate(${attrs.x}px, ${attrs.y}px) rotate(${attrs.rotateDeg}deg) scaleX(${attrs.scaleX}) scaleY(${attrs.scaleY})`,
|
|
29
|
+
div.style.transformOrigin = "top left";
|
|
30
|
+
} else div.style.position = "", div.style.zIndex = "", div.style.top = "", div.style.left = "",
|
|
31
|
+
div.style.transform = "", div.style.transformOrigin = "";
|
|
32
|
+
const _a = divProps || {}, {style: style} = _a, restProps = __rest(_a, [ "style" ]);
|
|
33
|
+
Object.assign(div.style, style), Object.assign(div, restProps);
|
|
34
|
+
};
|
|
35
|
+
return React.useEffect((() => {
|
|
36
|
+
var _a, _b;
|
|
37
|
+
const group = groupRef.current;
|
|
38
|
+
if (!(null == group ? void 0 : group.stage)) return;
|
|
39
|
+
const stage = group.stage, htmlContainer = null === (_b = null === (_a = stage.window) || void 0 === _a ? void 0 : _a.getContainer) || void 0 === _b ? void 0 : _b.call(_a);
|
|
40
|
+
return htmlContainer ? (htmlContainer.appendChild(div), shouldTransform && needForceStyle(htmlContainer) && (htmlContainer.style.position = "relative"),
|
|
41
|
+
stage.hooks.beforeRender.tap(`${group._uid}-render`, handleGroupRerender), handleGroupRerender(),
|
|
42
|
+
() => {
|
|
43
|
+
var _a;
|
|
44
|
+
stage.hooks.beforeRender.unTap(`${group._uid}-render`, handleGroupRerender), null === (_a = div.parentNode) || void 0 === _a || _a.removeChild(div);
|
|
45
|
+
}) : void 0;
|
|
46
|
+
}), [ shouldTransform ]), React.useLayoutEffect((() => {
|
|
47
|
+
handleGroupRerender();
|
|
48
|
+
}), [ divProps ]), React.useLayoutEffect((() => {
|
|
49
|
+
root.render(children);
|
|
50
|
+
})), React.useLayoutEffect((() => () => {
|
|
51
|
+
root.unmount();
|
|
52
|
+
}), []), React.createElement(ShadowRoot, {
|
|
53
|
+
ref: groupRef
|
|
54
|
+
});
|
|
55
|
+
};
|
package/es/Html.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/Html.tsx"],"names":[],"mappings":";;;;;;;;;;;AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAG1B,OAAO,KAAK,QAAQ,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAIrD,MAAM,cAAc,GAAG,CAAC,EAAkB,EAAE,EAAE;IAC5C,MAAM,GAAG,GAAG,MAAM,CAAC,gBAAgB,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC;IACjD,MAAM,EAAE,GAAG,GAAG,KAAK,UAAU,IAAI,GAAG,KAAK,UAAU,CAAC;IACpD,OAAO,CAAC,EAAE,CA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,GAAG,UAAU,CAAC;SAC3C;QAED,KAAK,CAAC,KAAK,CAAC,YAAY,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,IAAI,SAAS,EAAE,mBAAmB,CAAC,CAAC;QAE1E,mBAAmB,EAAE,CAAC;QACtB,OAAO,GAAG,EAAE;;YACV,KAAK,CAAC,KAAK,CAAC,YAAY,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,IAAI,SAAS,EAAE,mBAAmB,CAAC,CAAC;YAC5E,MAAA,GAAG,CAAC,UAAU,0CAAE,WAAW,CAAC,GAAG,CAAC,CAAC;QACnC,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,eAAe,CAAC,CAAC,CAAC;IAEtB,KAAK,CAAC,eAAe,CAAC,GAAG,EAAE;QACzB,mBAAmB,EAAE,CAAC;IACxB,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;IAEf,KAAK,CAAC,eAAe,CAAC,GAAG,EAAE;QACzB,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IACxB,CAAC,CAAC,CAAC;IAEH,KAAK,CAAC,eAAe,CAAC,GAAG,EAAE;QACzB,OAAO,GAAG,EAAE;YACV,IAAI,CAAC,OAAO,EAAE,CAAC;QACjB,CAAC,CAAC;IACJ,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,oBAAC,UAAU,IAAC,GAAG,EAAE,QAAe,GAAI,CAAC;AAC9C,CAAC,CAAC","file":"Html.js","sourcesContent":["import React from 'react';\n// eslint-disable-next-line no-duplicate-imports\nimport type { HTMLAttributes, PropsWithChildren } from 'react';\nimport * as ReactDOM from 'react-dom';\nimport { ShadowRoot } from '@visactor/react-vrender';\n// eslint-disable-next-line no-duplicate-imports\nimport type { IShadowRoot } from '@visactor/vrender';\n\nconst needForceStyle = (el: HTMLDivElement) => {\n const pos = window.getComputedStyle(el).position;\n const ok = pos === 'absolute' || pos === 'relative';\n return !ok;\n};\n\nexport type HtmlTransformAttrs = {\n x: number;\n y: number;\n scaleX: number;\n scaleY: number;\n rotateDeg: number;\n skewX: number;\n skewY: number;\n};\n\nexport type HtmlProps = PropsWithChildren<{\n divProps?: HTMLAttributes<HTMLDivElement>;\n transform?: boolean;\n transformFunc?: (attrs: HtmlTransformAttrs) => HtmlTransformAttrs;\n}>;\n\nexport const Html = ({ children, divProps, transform, transformFunc }: HtmlProps) => {\n const groupRef = React.useRef<IShadowRoot>(null);\n\n const [div] = React.useState(() => document.createElement('div'));\n const root = React.useMemo(() => (ReactDOM as any).createRoot(div), [div]);\n\n const shouldTransform = transform ?? true;\n\n const handleGroupRerender = () => {\n if (shouldTransform && groupRef.current) {\n if (!groupRef.current.shouldUpdateGlobalMatrix()) {\n return;\n }\n\n const matrix = groupRef.current.globalTransMatrix;\n let attrs = matrix.toTransformAttrs();\n if (transformFunc) {\n attrs = transformFunc(attrs);\n }\n div.style.position = 'absolute';\n div.style.zIndex = '10';\n div.style.top = '0px';\n div.style.left = '0px';\n // eslint-disable-next-line max-len\n div.style.transform = `translate(${attrs.x}px, ${attrs.y}px) rotate(${attrs.rotateDeg}deg) scaleX(${attrs.scaleX}) scaleY(${attrs.scaleY})`;\n div.style.transformOrigin = 'top left';\n } else {\n div.style.position = '';\n div.style.zIndex = '';\n div.style.top = '';\n div.style.left = '';\n div.style.transform = ``;\n div.style.transformOrigin = '';\n }\n const { style, ...restProps } = divProps || {};\n Object.assign(div.style, style);\n Object.assign(div, restProps);\n };\n\n React.useEffect(() => {\n const group = groupRef.current;\n if (!group?.stage) {\n return;\n }\n const stage = group.stage;\n const htmlContainer = stage.window?.getContainer?.();\n if (!htmlContainer) {\n return;\n }\n htmlContainer.appendChild(div);\n\n if (shouldTransform && needForceStyle(htmlContainer)) {\n htmlContainer.style.position = 'relative';\n }\n\n stage.hooks.beforeRender.tap(`${group._uid}-render`, handleGroupRerender);\n\n handleGroupRerender();\n return () => {\n stage.hooks.beforeRender.unTap(`${group._uid}-render`, handleGroupRerender);\n div.parentNode?.removeChild(div);\n };\n }, [shouldTransform]);\n\n React.useLayoutEffect(() => {\n handleGroupRerender();\n }, [divProps]);\n\n React.useLayoutEffect(() => {\n root.render(children);\n });\n\n React.useLayoutEffect(() => {\n return () => {\n root.unmount();\n };\n }, []);\n\n return <ShadowRoot ref={groupRef as any} />;\n};\n"]}
|
package/es/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Html';
|
package/es/index.js
ADDED
package/es/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC","file":"index.js","sourcesContent":["export * from './Html';\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@visactor/react-vrender-utils",
|
|
3
|
+
"version": "0.9.0-alpha.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"main": "cjs/index.js",
|
|
7
|
+
"module": "es/index.js",
|
|
8
|
+
"types": "es/index.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"cjs",
|
|
11
|
+
"es",
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"peerDependencies": {
|
|
15
|
+
"react": "^18.2.0",
|
|
16
|
+
"react-dom": "^18.2.0"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@visactor/vrender": "0.9.0-alpha.0",
|
|
20
|
+
"@visactor/react-vrender": "0.9.0-alpha.0",
|
|
21
|
+
"@visactor/vutils": "0.9.0-alpha.2",
|
|
22
|
+
"react-reconciler": "^0.29.0",
|
|
23
|
+
"tslib": "^2.3.1"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@rushstack/eslint-patch": "~1.1.4",
|
|
27
|
+
"react": "16.13.0",
|
|
28
|
+
"react-dom": "16.13.0",
|
|
29
|
+
"@types/react": "16.9.49",
|
|
30
|
+
"@types/react-dom": "16.9.8",
|
|
31
|
+
"@vitejs/plugin-react": "3.1.0",
|
|
32
|
+
"eslint": "~8.18.0",
|
|
33
|
+
"vite": "3.2.6",
|
|
34
|
+
"typescript": "4.9.5",
|
|
35
|
+
"@internal/ts-config": "0.0.1",
|
|
36
|
+
"@internal/bundler": "0.0.1",
|
|
37
|
+
"@internal/eslint-config": "0.0.1"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"compile": "tsc --noEmit",
|
|
44
|
+
"eslint": "eslint --debug --fix src/",
|
|
45
|
+
"build": "bundle",
|
|
46
|
+
"dev": "bundle --clean -f es -w",
|
|
47
|
+
"test": "",
|
|
48
|
+
"start": "vite ./vite"
|
|
49
|
+
}
|
|
50
|
+
}
|