@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 ADDED
@@ -0,0 +1,9 @@
1
+ # @visactor/react-vrender-utils
2
+
3
+ ## Description
4
+
5
+ ## Usage
6
+
7
+ ```typescript
8
+ import { xxx } from '@visactor/react-vrender-utils';
9
+ ```
package/cjs/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/cjs/Html.js ADDED
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+
3
+ var __createBinding = this && this.__createBinding || (Object.create ? function(o, m, k, k2) {
4
+ void 0 === k2 && (k2 = k);
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ desc && !("get" in desc ? !m.__esModule : desc.writable || desc.configurable) || (desc = {
7
+ enumerable: !0,
8
+ get: function() {
9
+ return m[k];
10
+ }
11
+ }), Object.defineProperty(o, k2, desc);
12
+ } : function(o, m, k, k2) {
13
+ void 0 === k2 && (k2 = k), o[k2] = m[k];
14
+ }), __setModuleDefault = this && this.__setModuleDefault || (Object.create ? function(o, v) {
15
+ Object.defineProperty(o, "default", {
16
+ enumerable: !0,
17
+ value: v
18
+ });
19
+ } : function(o, v) {
20
+ o.default = v;
21
+ }), __importStar = this && this.__importStar || function(mod) {
22
+ if (mod && mod.__esModule) return mod;
23
+ var result = {};
24
+ if (null != mod) for (var k in mod) "default" !== k && Object.prototype.hasOwnProperty.call(mod, k) && __createBinding(result, mod, k);
25
+ return __setModuleDefault(result, mod), result;
26
+ }, __rest = this && this.__rest || function(s, e) {
27
+ var t = {};
28
+ for (var p in s) Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0 && (t[p] = s[p]);
29
+ if (null != s && "function" == typeof Object.getOwnPropertySymbols) {
30
+ var i = 0;
31
+ 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]]);
32
+ }
33
+ return t;
34
+ }, __importDefault = this && this.__importDefault || function(mod) {
35
+ return mod && mod.__esModule ? mod : {
36
+ default: mod
37
+ };
38
+ };
39
+
40
+ Object.defineProperty(exports, "__esModule", {
41
+ value: !0
42
+ }), exports.Html = void 0;
43
+
44
+ const react_1 = __importDefault(require("react")), ReactDOM = __importStar(require("react-dom")), react_vrender_1 = require("@visactor/react-vrender"), needForceStyle = el => {
45
+ const pos = window.getComputedStyle(el).position;
46
+ return !("absolute" === pos || "relative" === pos);
47
+ }, Html = ({children: children, divProps: divProps, transform: transform, transformFunc: transformFunc}) => {
48
+ const groupRef = react_1.default.useRef(null), [div] = react_1.default.useState((() => document.createElement("div"))), root = react_1.default.useMemo((() => ReactDOM.createRoot(div)), [ div ]), shouldTransform = null == transform || transform, handleGroupRerender = () => {
49
+ if (shouldTransform && groupRef.current) {
50
+ if (!groupRef.current.shouldUpdateGlobalMatrix()) return;
51
+ let attrs = groupRef.current.globalTransMatrix.toTransformAttrs();
52
+ transformFunc && (attrs = transformFunc(attrs)), div.style.position = "absolute",
53
+ 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})`,
54
+ div.style.transformOrigin = "top left";
55
+ } else div.style.position = "", div.style.zIndex = "", div.style.top = "", div.style.left = "",
56
+ div.style.transform = "", div.style.transformOrigin = "";
57
+ const _a = divProps || {}, {style: style} = _a, restProps = __rest(_a, [ "style" ]);
58
+ Object.assign(div.style, style), Object.assign(div, restProps);
59
+ };
60
+ return react_1.default.useEffect((() => {
61
+ var _a, _b;
62
+ const group = groupRef.current;
63
+ if (!(null == group ? void 0 : group.stage)) return;
64
+ 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);
65
+ return htmlContainer ? (htmlContainer.appendChild(div), shouldTransform && needForceStyle(htmlContainer) && (htmlContainer.style.position = "relative"),
66
+ stage.hooks.beforeRender.tap(`${group._uid}-render`, handleGroupRerender), handleGroupRerender(),
67
+ () => {
68
+ var _a;
69
+ stage.hooks.beforeRender.unTap(`${group._uid}-render`, handleGroupRerender), null === (_a = div.parentNode) || void 0 === _a || _a.removeChild(div);
70
+ }) : void 0;
71
+ }), [ shouldTransform ]), react_1.default.useLayoutEffect((() => {
72
+ handleGroupRerender();
73
+ }), [ divProps ]), react_1.default.useLayoutEffect((() => {
74
+ root.render(children);
75
+ })), react_1.default.useLayoutEffect((() => () => {
76
+ root.unmount();
77
+ }), []), react_1.default.createElement(react_vrender_1.ShadowRoot, {
78
+ ref: groupRef
79
+ });
80
+ };
81
+
82
+ exports.Html = Html;
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/Html.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kDAA0B;AAG1B,oDAAsC;AACtC,2DAAqD;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,OAA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AAQ,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,eAAK,CAAC,eAAe,CAAC,GAAG,EAAE;QACzB,mBAAmB,EAAE,CAAC;IACxB,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;IAEf,eAAK,CAAC,eAAe,CAAC,GAAG,EAAE;QACzB,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IACxB,CAAC,CAAC,CAAC;IAEH,eAAK,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,8BAAC,0BAAU,IAAC,GAAG,EAAE,QAAe,GAAI,CAAC;AAC9C,CAAC,CAAC;AA/EW,QAAA,IAAI,QA+Ef","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/cjs/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './Html';
package/cjs/index.js ADDED
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+
3
+ var __createBinding = this && this.__createBinding || (Object.create ? function(o, m, k, k2) {
4
+ void 0 === k2 && (k2 = k);
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ desc && !("get" in desc ? !m.__esModule : desc.writable || desc.configurable) || (desc = {
7
+ enumerable: !0,
8
+ get: function() {
9
+ return m[k];
10
+ }
11
+ }), Object.defineProperty(o, k2, desc);
12
+ } : function(o, m, k, k2) {
13
+ void 0 === k2 && (k2 = k), o[k2] = m[k];
14
+ }), __exportStar = this && this.__exportStar || function(m, exports) {
15
+ for (var p in m) "default" === p || Object.prototype.hasOwnProperty.call(exports, p) || __createBinding(exports, m, p);
16
+ };
17
+
18
+ Object.defineProperty(exports, "__esModule", {
19
+ value: !0
20
+ }), __exportStar(require("./Html"), exports);
21
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yCAAuB","file":"index.js","sourcesContent":["export * from './Html';\n"]}