@textbus/adapter-react 5.2.1 → 5.2.3

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/dist/index.esm.js CHANGED
@@ -1,102 +1,138 @@
1
- import { makeError as h, VElement as l, VTextNode as y, Adapter as g } from "@textbus/core";
2
- import { DomAdapter as m } from "@textbus/platform-browser";
3
- import { createElement as d, useState as E, useEffect as i } from "react";
4
- import { ReflectiveInjector as R } from "@viewfly/core";
5
- import { merge as C } from "@tanbo/stream";
6
- const u = h("ReactAdapter");
7
- class f extends m {
1
+ import { makeError, VElement, VTextNode, Adapter } from "@textbus/core";
2
+ import { DomAdapter } from "@textbus/platform-browser";
3
+ import { createElement, useState, useEffect } from "react";
4
+ import { ReflectiveInjector } from "@viewfly/core";
5
+ import { merge } from "@tanbo/stream";
6
+ const adapterError = makeError("ReactAdapter");
7
+ class ReactAdapter extends DomAdapter {
8
8
  components = {};
9
- constructor(a, c) {
9
+ constructor(components, mount) {
10
10
  super({
11
- createCompositionNode(e, r) {
12
- return new l("span", {
11
+ createCompositionNode(compositionState, updateNativeCompositionNode) {
12
+ return new VElement("span", {
13
13
  style: {
14
14
  textDecoration: "underline"
15
15
  },
16
- ref: (t) => {
17
- r(t);
16
+ ref: (node) => {
17
+ updateNativeCompositionNode(node);
18
18
  }
19
19
  }, [
20
- new y(e.text)
20
+ new VTextNode(compositionState.text)
21
21
  ]);
22
22
  },
23
- getParentNode(e) {
24
- return e.parentNode;
23
+ getParentNode(node) {
24
+ return node.parentNode;
25
25
  },
26
- getChildNodes(e) {
27
- return Array.from(e.childNodes);
26
+ getChildNodes(parentElement) {
27
+ return Array.from(parentElement.childNodes);
28
28
  },
29
- isNativeElementNode(e) {
30
- return e instanceof Element;
29
+ isNativeElementNode(node) {
30
+ return node instanceof Element;
31
31
  },
32
- getChildByIndex(e, r) {
33
- return e.childNodes[r];
32
+ getChildByIndex(parentElement, index) {
33
+ return parentElement.childNodes[index];
34
34
  },
35
- getAndUpdateSlotRootNativeElement(e, r) {
36
- const t = e.attrs.get("ref");
37
- t ? e.attrs.set("ref", (n) => {
38
- r(n), typeof t == "function" ? t(n) : t.current || (t.current = n);
39
- }) : e.attrs.set("ref", r);
35
+ getAndUpdateSlotRootNativeElement(vEle, update) {
36
+ const currentRef = vEle.attrs.get("ref");
37
+ if (currentRef) {
38
+ vEle.attrs.set("ref", (v) => {
39
+ update(v);
40
+ if (typeof currentRef === "function") {
41
+ currentRef(v);
42
+ } else if (!currentRef.current) {
43
+ currentRef.current = v;
44
+ }
45
+ });
46
+ } else {
47
+ vEle.attrs.set("ref", update);
48
+ }
40
49
  },
41
- componentRender: (e) => {
42
- const r = this.components[e.name] || this.components["*"];
43
- if (r)
44
- return e.changeMarker.rendered(), d(r, {
45
- key: e.id,
46
- component: e
50
+ componentRender: (component) => {
51
+ const comp = this.components[component.name] || this.components["*"];
52
+ if (comp) {
53
+ component.changeMarker.rendered();
54
+ return createElement(comp, {
55
+ key: component.id,
56
+ component
47
57
  });
48
- throw u(`cannot found view component \`${e.name}\`!`);
58
+ }
59
+ throw adapterError(`cannot found view component \`${component.name}\`!`);
49
60
  },
50
- vElementToViewElement(e, r) {
51
- const t = {
52
- ...Array.from(e.attrs).reduce((n, o) => (n[o[0]] = o[1], n), {})
61
+ vElementToViewElement(vNode, children) {
62
+ const props = {
63
+ ...Array.from(vNode.attrs).reduce((a, b) => {
64
+ a[b[0]] = b[1];
65
+ return a;
66
+ }, {})
53
67
  };
54
- return e.classes.size && (t.className = Array.from(e.classes).join(" ")), e.styles && (t.style = Array.from(e.styles).reduce((n, o) => (n[o[0]] = o[1], n), {})), d(e.tagName, t, ...r);
68
+ if (vNode.classes.size) {
69
+ props.className = Array.from(vNode.classes).join(" ");
70
+ }
71
+ if (vNode.styles) {
72
+ props.style = Array.from(vNode.styles).reduce((a, b) => {
73
+ a[b[0]] = b[1];
74
+ return a;
75
+ }, {});
76
+ }
77
+ return createElement(vNode.tagName, props, ...children);
55
78
  }
56
- }, c), Object.keys(a).forEach((e) => {
57
- this.components[e] = (r) => {
58
- const t = r.component, [n, o] = E(Math.random());
59
- i(() => {
60
- const s = C(t.changeMarker.onChange, t.changeMarker.onForceChange).subscribe(() => {
61
- t.changeMarker.dirty && o(Math.random());
79
+ }, mount);
80
+ Object.keys(components).forEach((key) => {
81
+ this.components[key] = (props) => {
82
+ const component = props.component;
83
+ const [updateKey, refreshUpdateKey] = useState(Math.random());
84
+ useEffect(() => {
85
+ const sub = merge(component.changeMarker.onChange, component.changeMarker.onForceChange).subscribe(() => {
86
+ if (component.changeMarker.dirty) {
87
+ refreshUpdateKey(Math.random());
88
+ }
62
89
  });
63
90
  return () => {
64
- s.unsubscribe();
91
+ sub.unsubscribe();
65
92
  };
66
- }, []), i(() => {
93
+ }, []);
94
+ useEffect(() => {
67
95
  this.onViewUpdated.next();
68
- }, [n]);
69
- const p = a[e]({
70
- component: t,
71
- rootRef: (s) => {
72
- s ? this.componentRootElementCaches.set(t, s) : this.componentRootElementCaches.get(t) === s && this.componentRootElementCaches.remove(t);
96
+ }, [updateKey]);
97
+ const vNode = components[key]({
98
+ component,
99
+ rootRef: (rootNode) => {
100
+ if (rootNode) {
101
+ this.componentRootElementCaches.set(component, rootNode);
102
+ } else {
103
+ if (this.componentRootElementCaches.get(component) === rootNode) {
104
+ this.componentRootElementCaches.remove(component);
105
+ }
106
+ }
107
+ }
108
+ });
109
+ useEffect(() => {
110
+ if (!this.componentRootElementCaches.get(component)) {
111
+ throw adapterError(`Component \`${component.name}\` is not bound to rootRef, you must bind rootRef to the root element node of the component view.`);
73
112
  }
74
113
  });
75
- return i(() => {
76
- if (!this.componentRootElementCaches.get(t))
77
- throw u(`Component \`${t.name}\` is not bound to rootRef, you must bind rootRef to the root element node of the component view.`);
78
- }), p;
114
+ return vNode;
79
115
  };
80
116
  });
81
117
  }
82
- render(a, c) {
83
- const e = new R(c, [{
84
- provide: g,
118
+ render(rootComponent, injector) {
119
+ const childInjector = new ReflectiveInjector(injector, [{
120
+ provide: Adapter,
85
121
  useValue: this
86
122
  }, {
87
- provide: m,
123
+ provide: DomAdapter,
88
124
  useValue: this
89
125
  }, {
90
- provide: f,
126
+ provide: ReactAdapter,
91
127
  useValue: this
92
128
  }]);
93
- return super.render(a, e);
129
+ return super.render(rootComponent, childInjector);
94
130
  }
95
131
  copy() {
96
132
  document.execCommand("copy");
97
133
  }
98
134
  }
99
135
  export {
100
- f as ReactAdapter
136
+ ReactAdapter
101
137
  };
102
138
  //# sourceMappingURL=index.esm.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.esm.js","sources":["../src/react-adapter.ts"],"sourcesContent":["import { Adapter, Component, CompositionState, makeError, VElement, ViewMount, VTextNode } from '@textbus/core'\nimport { DomAdapter } from '@textbus/platform-browser'\nimport { createElement, JSX, useEffect, useState } from 'react'\nimport { Injector, ReflectiveInjector } from '@viewfly/core'\nimport { merge } from '@tanbo/stream'\n\nconst adapterError = makeError('ReactAdapter')\n\nexport interface ViewComponentProps<T extends Component> {\n component: T\n rootRef: ((rootNode: Element) => void)\n}\n\nexport interface ReactAdapterComponents {\n [key: string]: (props: ViewComponentProps<any>) => JSX.Element\n}\n\nexport class ReactAdapter extends DomAdapter<JSX.Element, JSX.Element> {\n private components: Record<string, (props: {component: Component}) => JSX.Element> = {}\n\n constructor(components: ReactAdapterComponents,\n mount: ViewMount<JSX.Element, Element>) {\n super({\n createCompositionNode(compositionState: CompositionState,\n updateNativeCompositionNode: (nativeNode: (Element | null)) => void): VElement {\n return new VElement('span', {\n style: {\n textDecoration: 'underline'\n },\n ref: (node: Element) => {\n updateNativeCompositionNode(node)\n }\n }, [\n new VTextNode(compositionState.text)\n ])\n },\n getParentNode(node: Element | Text): Element | null {\n return (node as Node).parentNode as Element\n },\n getChildNodes(parentElement: Element): Array<Element | Text> {\n return Array.from(parentElement.childNodes) as Element[]\n },\n isNativeElementNode(node: Element | Text): node is Element {\n return node instanceof Element\n },\n getChildByIndex(parentElement, index) {\n return parentElement.childNodes[index] as Element\n },\n getAndUpdateSlotRootNativeElement(vEle: VElement, update: (nativeElement: (Element | null)) => void) {\n const currentRef = vEle.attrs.get('ref')\n if (currentRef) {\n vEle.attrs.set('ref', (v: Element) => {\n update(v)\n if (typeof currentRef === 'function') {\n currentRef(v)\n } else if (!currentRef.current) {\n currentRef.current = v\n }\n })\n } else {\n vEle.attrs.set('ref', update)\n }\n },\n componentRender: (component: Component<any>): JSX.Element => {\n const comp = this.components[component.name] || this.components['*']\n if (comp) {\n component.changeMarker.rendered()\n return createElement(comp, {\n key: component.id,\n component\n })\n }\n throw adapterError(`cannot found view component \\`${component.name}\\`!`)\n },\n vElementToViewElement(vNode: VElement, children: Array<string | JSX.Element>): JSX.Element {\n const props: any = {\n ...(Array.from(vNode.attrs).reduce((a, b) => {\n a[b[0]] = b[1]\n return a\n }, {} as Record<string, any>))\n }\n if (vNode.classes.size) {\n props.className = Array.from(vNode.classes).join(' ')\n }\n if (vNode.styles) {\n props.style = Array.from(vNode.styles).reduce((a, b) => {\n a[b[0]] = b[1]\n return a\n }, {} as Record<string, any>)\n }\n return createElement(vNode.tagName, props, ...children)\n }\n }, mount)\n Object.keys(components).forEach(key => {\n this.components[key] = (props: {component: Component}) => {\n const component = props.component\n const [updateKey, refreshUpdateKey] = useState(Math.random())\n\n useEffect(() => {\n const sub = merge(component.changeMarker.onChange, component.changeMarker.onForceChange).subscribe(() => {\n if (component.changeMarker.dirty) {\n refreshUpdateKey(Math.random())\n }\n })\n return () => {\n sub.unsubscribe()\n }\n }, [])\n useEffect(() => {\n this.onViewUpdated.next()\n }, [updateKey])\n const vNode = components[key]({\n component,\n rootRef: (rootNode: Element) => {\n if (rootNode) {\n this.componentRootElementCaches.set(component, rootNode)\n } else {\n // 当组件移动层级位置到原位置之前并重新渲染后,由于时序的原因,再删除缓存会导致组件找不到对应视图节点\n // eslint-disable-next-line no-lonely-if\n if (this.componentRootElementCaches.get(component) === rootNode) {\n this.componentRootElementCaches.remove(component)\n }\n }\n }\n })\n useEffect(() => {\n if (!this.componentRootElementCaches.get(component)) {\n // eslint-disable-next-line max-len\n throw adapterError(`Component \\`${component.name}\\` is not bound to rootRef, you must bind rootRef to the root element node of the component view.`)\n }\n })\n return vNode\n }\n })\n }\n\n override render(rootComponent: Component, injector: Injector): void | (() => void) {\n const childInjector = new ReflectiveInjector(injector, [{\n provide: Adapter,\n useValue: this\n }, {\n provide: DomAdapter,\n useValue: this\n }, {\n provide: ReactAdapter,\n useValue: this\n }])\n return super.render(rootComponent, childInjector)\n }\n\n override copy() {\n document.execCommand('copy')\n }\n}\n"],"names":["adapterError","makeError","ReactAdapter","DomAdapter","components","mount","compositionState","updateNativeCompositionNode","VElement","node","VTextNode","parentElement","index","vEle","update","currentRef","v","component","comp","createElement","vNode","children","props","a","b","key","updateKey","refreshUpdateKey","useState","useEffect","sub","merge","rootNode","rootComponent","injector","childInjector","ReflectiveInjector","Adapter"],"mappings":";;;;;AAMA,MAAMA,IAAeC,EAAU,cAAc;AAWtC,MAAMC,UAAqBC,EAAqC;AAAA,EAC7D,aAA6E,CAAA;AAAA,EAErF,YAAYC,GACAC,GAAwC;AAClD,UAAM;AAAA,MACJ,sBAAsBC,GACAC,GAA+E;AACnG,eAAO,IAAIC,EAAS,QAAQ;AAAA,UAC1B,OAAO;AAAA,YACL,gBAAgB;AAAA,UAAA;AAAA,UAElB,KAAK,CAACC,MAAkB;AACtB,YAAAF,EAA4BE,CAAI;AAAA,UAClC;AAAA,QAAA,GACC;AAAA,UACD,IAAIC,EAAUJ,EAAiB,IAAI;AAAA,QAAA,CACpC;AAAA,MACH;AAAA,MACA,cAAcG,GAAsC;AAClD,eAAQA,EAAc;AAAA,MACxB;AAAA,MACA,cAAcE,GAA+C;AAC3D,eAAO,MAAM,KAAKA,EAAc,UAAU;AAAA,MAC5C;AAAA,MACA,oBAAoBF,GAAuC;AACzD,eAAOA,aAAgB;AAAA,MACzB;AAAA,MACA,gBAAgBE,GAAeC,GAAO;AACpC,e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,EAC7B;AACF;"}
1
+ {"version":3,"file":"index.esm.js","sources":["../src/react-adapter.ts"],"sourcesContent":["import { Adapter, Component, CompositionState, makeError, VElement, ViewMount, VTextNode } from '@textbus/core'\nimport { DomAdapter } from '@textbus/platform-browser'\nimport { createElement, JSX, useEffect, useState } from 'react'\nimport { Injector, ReflectiveInjector } from '@viewfly/core'\nimport { merge } from '@tanbo/stream'\n\nconst adapterError = makeError('ReactAdapter')\n\nexport interface ViewComponentProps<T extends Component> {\n component: T\n rootRef: ((rootNode: Element) => void)\n}\n\nexport interface ReactAdapterComponents {\n [key: string]: (props: ViewComponentProps<any>) => JSX.Element\n}\n\nexport class ReactAdapter extends DomAdapter<JSX.Element, JSX.Element> {\n private components: Record<string, (props: {component: Component}) => JSX.Element> = {}\n\n constructor(components: ReactAdapterComponents,\n mount: ViewMount<JSX.Element, Element>) {\n super({\n createCompositionNode(compositionState: CompositionState,\n updateNativeCompositionNode: (nativeNode: (Element | null)) => void): VElement {\n return new VElement('span', {\n style: {\n textDecoration: 'underline'\n },\n ref: (node: Element) => {\n updateNativeCompositionNode(node)\n }\n }, [\n new VTextNode(compositionState.text)\n ])\n },\n getParentNode(node: Element | Text): Element | null {\n return (node as Node).parentNode as Element\n },\n getChildNodes(parentElement: Element): Array<Element | Text> {\n return Array.from(parentElement.childNodes) as Element[]\n },\n isNativeElementNode(node: Element | Text): node is Element {\n return node instanceof Element\n },\n getChildByIndex(parentElement, index) {\n return parentElement.childNodes[index] as Element\n },\n getAndUpdateSlotRootNativeElement(vEle: VElement, update: (nativeElement: (Element | null)) => void) {\n const currentRef = vEle.attrs.get('ref')\n if (currentRef) {\n vEle.attrs.set('ref', (v: Element) => {\n update(v)\n if (typeof currentRef === 'function') {\n currentRef(v)\n } else if (!currentRef.current) {\n currentRef.current = v\n }\n })\n } else {\n vEle.attrs.set('ref', update)\n }\n },\n componentRender: (component: Component<any>): JSX.Element => {\n const comp = this.components[component.name] || this.components['*']\n if (comp) {\n component.changeMarker.rendered()\n return createElement(comp, {\n key: component.id,\n component\n })\n }\n throw adapterError(`cannot found view component \\`${component.name}\\`!`)\n },\n vElementToViewElement(vNode: VElement, children: Array<string | JSX.Element>): JSX.Element {\n const props: any = {\n ...(Array.from(vNode.attrs).reduce((a, b) => {\n a[b[0]] = b[1]\n return a\n }, {} as Record<string, any>))\n }\n if (vNode.classes.size) {\n props.className = Array.from(vNode.classes).join(' ')\n }\n if (vNode.styles) {\n props.style = Array.from(vNode.styles).reduce((a, b) => {\n a[b[0]] = b[1]\n return a\n }, {} as Record<string, any>)\n }\n return createElement(vNode.tagName, props, ...children)\n }\n }, mount)\n Object.keys(components).forEach(key => {\n this.components[key] = (props: {component: Component}) => {\n const component = props.component\n const [updateKey, refreshUpdateKey] = useState(Math.random())\n\n useEffect(() => {\n const sub = merge(component.changeMarker.onChange, component.changeMarker.onForceChange).subscribe(() => {\n if (component.changeMarker.dirty) {\n refreshUpdateKey(Math.random())\n }\n })\n return () => {\n sub.unsubscribe()\n }\n }, [])\n useEffect(() => {\n this.onViewUpdated.next()\n }, [updateKey])\n const vNode = components[key]({\n component,\n rootRef: (rootNode: Element) => {\n if (rootNode) {\n this.componentRootElementCaches.set(component, rootNode)\n } else {\n // 当组件移动层级位置到原位置之前并重新渲染后,由于时序的原因,再删除缓存会导致组件找不到对应视图节点\n // eslint-disable-next-line no-lonely-if\n if (this.componentRootElementCaches.get(component) === rootNode) {\n this.componentRootElementCaches.remove(component)\n }\n }\n }\n })\n useEffect(() => {\n if (!this.componentRootElementCaches.get(component)) {\n // eslint-disable-next-line max-len\n throw adapterError(`Component \\`${component.name}\\` is not bound to rootRef, you must bind rootRef to the root element node of the component view.`)\n }\n })\n return vNode\n }\n })\n }\n\n override render(rootComponent: Component, injector: Injector): void | (() => void) {\n const childInjector = new ReflectiveInjector(injector, [{\n provide: Adapter,\n useValue: this\n }, {\n provide: DomAdapter,\n useValue: this\n }, {\n provide: ReactAdapter,\n useValue: this\n }])\n return super.render(rootComponent, childInjector)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rB,GAAG,CAAC,SAAS,CAAC;AACd,cAAM,QAAQ,WAAW,GAAG,EAAE;AAAA,UAC5B;AAAA,UACA,SAAS,CAAC,aAAsB;AAC9B,gBAAI,UAAU;AACZ,mBAAK,2BAA2B,IAAI,WAAW,QAAQ;AAAA,YACzD,OAAO;AAGL,kBAAI,KAAK,2BAA2B,IAAI,SAAS,MAAM,UAAU;AAC/D,qBAAK,2BAA2B,OAAO,SAAS;AAAA,cAClD;AAAA,YACF;AAAA,UACF;AAAA,QAAA,CACD;AACD,kBAAU,MAAM;AACd,cAAI,CAAC,KAAK,2BAA2B,IAAI,SAAS,GAAG;AAEnD,kBAAM,aAAa,eAAe,UAAU,IAAI,mGAAmG;AAAA,UACrJ;AAAA,QACF,CAAC;AACD,eAAO;AAAA,MACT;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAES,OAAO,eAA0B,UAAyC;AACjF,UAAM,gBAAgB,IAAI,mBAAmB,UAAU,CAAC;AAAA,MACtD,SAAS;AAAA,MACT,UAAU;AAAA,IAAA,GACT;AAAA,MACD,SAAS;AAAA,MACT,UAAU;AAAA,IAAA,GACT;AAAA,MACD,SAAS;AAAA,MACT,UAAU;AAAA,IAAA,CACX,CAAC;AACF,WAAO,MAAM,OAAO,eAAe,aAAa;AAAA,EAClD;AAAA,EAES,OAAO;AACd,aAAS,YAAY,MAAM;AAAA,EAC7B;AACF;"}
package/dist/index.js CHANGED
@@ -1,2 +1,138 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("@textbus/core"),d=require("@textbus/platform-browser"),s=require("react"),h=require("@viewfly/core"),p=require("@tanbo/stream"),f=i.makeError("ReactAdapter");class m extends d.DomAdapter{components={};constructor(a,u){super({createCompositionNode(e,r){return new i.VElement("span",{style:{textDecoration:"underline"},ref:t=>{r(t)}},[new i.VTextNode(e.text)])},getParentNode(e){return e.parentNode},getChildNodes(e){return Array.from(e.childNodes)},isNativeElementNode(e){return e instanceof Element},getChildByIndex(e,r){return e.childNodes[r]},getAndUpdateSlotRootNativeElement(e,r){const t=e.attrs.get("ref");t?e.attrs.set("ref",n=>{r(n),typeof t=="function"?t(n):t.current||(t.current=n)}):e.attrs.set("ref",r)},componentRender:e=>{const r=this.components[e.name]||this.components["*"];if(r)return e.changeMarker.rendered(),s.createElement(r,{key:e.id,component:e});throw f(`cannot found view component \`${e.name}\`!`)},vElementToViewElement(e,r){const t={...Array.from(e.attrs).reduce((n,o)=>(n[o[0]]=o[1],n),{})};return e.classes.size&&(t.className=Array.from(e.classes).join(" ")),e.styles&&(t.style=Array.from(e.styles).reduce((n,o)=>(n[o[0]]=o[1],n),{})),s.createElement(e.tagName,t,...r)}},u),Object.keys(a).forEach(e=>{this.components[e]=r=>{const t=r.component,[n,o]=s.useState(Math.random());s.useEffect(()=>{const c=p.merge(t.changeMarker.onChange,t.changeMarker.onForceChange).subscribe(()=>{t.changeMarker.dirty&&o(Math.random())});return()=>{c.unsubscribe()}},[]),s.useEffect(()=>{this.onViewUpdated.next()},[n]);const l=a[e]({component:t,rootRef:c=>{c?this.componentRootElementCaches.set(t,c):this.componentRootElementCaches.get(t)===c&&this.componentRootElementCaches.remove(t)}});return s.useEffect(()=>{if(!this.componentRootElementCaches.get(t))throw f(`Component \`${t.name}\` is not bound to rootRef, you must bind rootRef to the root element node of the component view.`)}),l}})}render(a,u){const e=new h.ReflectiveInjector(u,[{provide:i.Adapter,useValue:this},{provide:d.DomAdapter,useValue:this},{provide:m,useValue:this}]);return super.render(a,e)}copy(){document.execCommand("copy")}}exports.ReactAdapter=m;
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const core = require("@textbus/core");
4
+ const platformBrowser = require("@textbus/platform-browser");
5
+ const react = require("react");
6
+ const core$1 = require("@viewfly/core");
7
+ const stream = require("@tanbo/stream");
8
+ const adapterError = core.makeError("ReactAdapter");
9
+ class ReactAdapter extends platformBrowser.DomAdapter {
10
+ components = {};
11
+ constructor(components, mount) {
12
+ super({
13
+ createCompositionNode(compositionState, updateNativeCompositionNode) {
14
+ return new core.VElement("span", {
15
+ style: {
16
+ textDecoration: "underline"
17
+ },
18
+ ref: (node) => {
19
+ updateNativeCompositionNode(node);
20
+ }
21
+ }, [
22
+ new core.VTextNode(compositionState.text)
23
+ ]);
24
+ },
25
+ getParentNode(node) {
26
+ return node.parentNode;
27
+ },
28
+ getChildNodes(parentElement) {
29
+ return Array.from(parentElement.childNodes);
30
+ },
31
+ isNativeElementNode(node) {
32
+ return node instanceof Element;
33
+ },
34
+ getChildByIndex(parentElement, index) {
35
+ return parentElement.childNodes[index];
36
+ },
37
+ getAndUpdateSlotRootNativeElement(vEle, update) {
38
+ const currentRef = vEle.attrs.get("ref");
39
+ if (currentRef) {
40
+ vEle.attrs.set("ref", (v) => {
41
+ update(v);
42
+ if (typeof currentRef === "function") {
43
+ currentRef(v);
44
+ } else if (!currentRef.current) {
45
+ currentRef.current = v;
46
+ }
47
+ });
48
+ } else {
49
+ vEle.attrs.set("ref", update);
50
+ }
51
+ },
52
+ componentRender: (component) => {
53
+ const comp = this.components[component.name] || this.components["*"];
54
+ if (comp) {
55
+ component.changeMarker.rendered();
56
+ return react.createElement(comp, {
57
+ key: component.id,
58
+ component
59
+ });
60
+ }
61
+ throw adapterError(`cannot found view component \`${component.name}\`!`);
62
+ },
63
+ vElementToViewElement(vNode, children) {
64
+ const props = {
65
+ ...Array.from(vNode.attrs).reduce((a, b) => {
66
+ a[b[0]] = b[1];
67
+ return a;
68
+ }, {})
69
+ };
70
+ if (vNode.classes.size) {
71
+ props.className = Array.from(vNode.classes).join(" ");
72
+ }
73
+ if (vNode.styles) {
74
+ props.style = Array.from(vNode.styles).reduce((a, b) => {
75
+ a[b[0]] = b[1];
76
+ return a;
77
+ }, {});
78
+ }
79
+ return react.createElement(vNode.tagName, props, ...children);
80
+ }
81
+ }, mount);
82
+ Object.keys(components).forEach((key) => {
83
+ this.components[key] = (props) => {
84
+ const component = props.component;
85
+ const [updateKey, refreshUpdateKey] = react.useState(Math.random());
86
+ react.useEffect(() => {
87
+ const sub = stream.merge(component.changeMarker.onChange, component.changeMarker.onForceChange).subscribe(() => {
88
+ if (component.changeMarker.dirty) {
89
+ refreshUpdateKey(Math.random());
90
+ }
91
+ });
92
+ return () => {
93
+ sub.unsubscribe();
94
+ };
95
+ }, []);
96
+ react.useEffect(() => {
97
+ this.onViewUpdated.next();
98
+ }, [updateKey]);
99
+ const vNode = components[key]({
100
+ component,
101
+ rootRef: (rootNode) => {
102
+ if (rootNode) {
103
+ this.componentRootElementCaches.set(component, rootNode);
104
+ } else {
105
+ if (this.componentRootElementCaches.get(component) === rootNode) {
106
+ this.componentRootElementCaches.remove(component);
107
+ }
108
+ }
109
+ }
110
+ });
111
+ react.useEffect(() => {
112
+ if (!this.componentRootElementCaches.get(component)) {
113
+ throw adapterError(`Component \`${component.name}\` is not bound to rootRef, you must bind rootRef to the root element node of the component view.`);
114
+ }
115
+ });
116
+ return vNode;
117
+ };
118
+ });
119
+ }
120
+ render(rootComponent, injector) {
121
+ const childInjector = new core$1.ReflectiveInjector(injector, [{
122
+ provide: core.Adapter,
123
+ useValue: this
124
+ }, {
125
+ provide: platformBrowser.DomAdapter,
126
+ useValue: this
127
+ }, {
128
+ provide: ReactAdapter,
129
+ useValue: this
130
+ }]);
131
+ return super.render(rootComponent, childInjector);
132
+ }
133
+ copy() {
134
+ document.execCommand("copy");
135
+ }
136
+ }
137
+ exports.ReactAdapter = ReactAdapter;
2
138
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/react-adapter.ts"],"sourcesContent":["import { Adapter, Component, CompositionState, makeError, VElement, ViewMount, VTextNode } from '@textbus/core'\nimport { DomAdapter } from '@textbus/platform-browser'\nimport { createElement, JSX, useEffect, useState } from 'react'\nimport { Injector, ReflectiveInjector } from '@viewfly/core'\nimport { merge } from '@tanbo/stream'\n\nconst adapterError = makeError('ReactAdapter')\n\nexport interface ViewComponentProps<T extends Component> {\n component: T\n rootRef: ((rootNode: Element) => void)\n}\n\nexport interface ReactAdapterComponents {\n [key: string]: (props: ViewComponentProps<any>) => JSX.Element\n}\n\nexport class ReactAdapter extends DomAdapter<JSX.Element, JSX.Element> {\n private components: Record<string, (props: {component: Component}) => JSX.Element> = {}\n\n constructor(components: ReactAdapterComponents,\n mount: ViewMount<JSX.Element, Element>) {\n super({\n createCompositionNode(compositionState: CompositionState,\n updateNativeCompositionNode: (nativeNode: (Element | null)) => void): VElement {\n return new VElement('span', {\n style: {\n textDecoration: 'underline'\n },\n ref: (node: Element) => {\n updateNativeCompositionNode(node)\n }\n }, [\n new VTextNode(compositionState.text)\n ])\n },\n getParentNode(node: Element | Text): Element | null {\n return (node as Node).parentNode as Element\n },\n getChildNodes(parentElement: Element): Array<Element | Text> {\n return Array.from(parentElement.childNodes) as Element[]\n },\n isNativeElementNode(node: Element | Text): node is Element {\n return node instanceof Element\n },\n getChildByIndex(parentElement, index) {\n return parentElement.childNodes[index] as Element\n },\n getAndUpdateSlotRootNativeElement(vEle: VElement, update: (nativeElement: (Element | null)) => void) {\n const currentRef = vEle.attrs.get('ref')\n if (currentRef) {\n vEle.attrs.set('ref', (v: Element) => {\n update(v)\n if (typeof currentRef === 'function') {\n currentRef(v)\n } else if (!currentRef.current) {\n currentRef.current = v\n }\n })\n } else {\n vEle.attrs.set('ref', update)\n }\n },\n componentRender: (component: Component<any>): JSX.Element => {\n const comp = this.components[component.name] || this.components['*']\n if (comp) {\n component.changeMarker.rendered()\n return createElement(comp, {\n key: component.id,\n component\n })\n }\n throw adapterError(`cannot found view component \\`${component.name}\\`!`)\n },\n vElementToViewElement(vNode: VElement, children: Array<string | JSX.Element>): JSX.Element {\n const props: any = {\n ...(Array.from(vNode.attrs).reduce((a, b) => {\n a[b[0]] = b[1]\n return a\n }, {} as Record<string, any>))\n }\n if (vNode.classes.size) {\n props.className = Array.from(vNode.classes).join(' ')\n }\n if (vNode.styles) {\n props.style = Array.from(vNode.styles).reduce((a, b) => {\n a[b[0]] = b[1]\n return a\n }, {} as Record<string, any>)\n }\n return createElement(vNode.tagName, props, ...children)\n }\n }, mount)\n Object.keys(components).forEach(key => {\n this.components[key] = (props: {component: Component}) => {\n const component = props.component\n const [updateKey, refreshUpdateKey] = useState(Math.random())\n\n useEffect(() => {\n const sub = merge(component.changeMarker.onChange, component.changeMarker.onForceChange).subscribe(() => {\n if (component.changeMarker.dirty) {\n refreshUpdateKey(Math.random())\n }\n })\n return () => {\n sub.unsubscribe()\n }\n }, [])\n useEffect(() => {\n this.onViewUpdated.next()\n }, [updateKey])\n const vNode = components[key]({\n component,\n rootRef: (rootNode: Element) => {\n if (rootNode) {\n this.componentRootElementCaches.set(component, rootNode)\n } else {\n // 当组件移动层级位置到原位置之前并重新渲染后,由于时序的原因,再删除缓存会导致组件找不到对应视图节点\n // eslint-disable-next-line no-lonely-if\n if (this.componentRootElementCaches.get(component) === rootNode) {\n this.componentRootElementCaches.remove(component)\n }\n }\n }\n })\n useEffect(() => {\n if (!this.componentRootElementCaches.get(component)) {\n // eslint-disable-next-line max-len\n throw adapterError(`Component \\`${component.name}\\` is not bound to rootRef, you must bind rootRef to the root element node of the component view.`)\n }\n })\n return vNode\n }\n })\n }\n\n override render(rootComponent: Component, injector: Injector): void | (() => void) {\n const childInjector = new ReflectiveInjector(injector, [{\n provide: Adapter,\n useValue: this\n }, {\n provide: DomAdapter,\n useValue: this\n }, {\n provide: ReactAdapter,\n useValue: this\n }])\n return super.render(rootComponent, childInjector)\n }\n\n override copy() {\n document.execCommand('copy')\n }\n}\n"],"names":["adapterError","makeError","ReactAdapter","DomAdapter","components","mount","compositionState","updateNativeCompositionNode","VElement","node","VTextNode","parentElement","index","vEle","update","currentRef","v","component","comp","createElement","vNode","children","props","a","b","key","updateKey","refreshUpdateKey","useState","useEffect","sub","merge","rootNode","rootComponent","injector","childInjector","ReflectiveInjector","Adapter"],"mappings":"iOAMMA,EAAeC,EA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F,CAAC,CACH,CAES,OAAOa,EAA0BC,EAAyC,CACjF,MAAMC,EAAgB,IAAIC,qBAAmBF,EAAU,CAAC,CACtD,QAASG,EAAAA,QACT,SAAU,IAAA,EACT,CACD,QAASlC,EAAAA,WACT,SAAU,IAAA,EACT,CACD,QAASD,EACT,SAAU,IAAA,CACX,CAAC,EACF,OAAO,MAAM,OAAO+B,EAAeE,CAAa,CAClD,CAES,MAAO,CACd,SAAS,YAAY,MAAM,CAC7B,CACF"}
1
+ {"version":3,"file":"index.js","sources":["../src/react-adapter.ts"],"sourcesContent":["import { Adapter, Component, CompositionState, makeError, VElement, ViewMount, VTextNode } from '@textbus/core'\nimport { DomAdapter } from '@textbus/platform-browser'\nimport { createElement, JSX, useEffect, useState } from 'react'\nimport { Injector, ReflectiveInjector } from '@viewfly/core'\nimport { merge } from '@tanbo/stream'\n\nconst adapterError = makeError('ReactAdapter')\n\nexport interface ViewComponentProps<T extends Component> {\n component: T\n rootRef: ((rootNode: Element) => void)\n}\n\nexport interface ReactAdapterComponents {\n [key: string]: (props: ViewComponentProps<any>) => JSX.Element\n}\n\nexport class ReactAdapter extends DomAdapter<JSX.Element, JSX.Element> {\n private components: Record<string, (props: {component: Component}) => JSX.Element> = {}\n\n constructor(components: ReactAdapterComponents,\n mount: ViewMount<JSX.Element, Element>) {\n super({\n createCompositionNode(compositionState: CompositionState,\n updateNativeCompositionNode: (nativeNode: (Element | null)) => void): VElement {\n return new VElement('span', {\n style: {\n textDecoration: 'underline'\n },\n ref: (node: Element) => {\n updateNativeCompositionNode(node)\n }\n }, [\n new VTextNode(compositionState.text)\n ])\n },\n getParentNode(node: Element | Text): Element | null {\n return (node as Node).parentNode as Element\n },\n getChildNodes(parentElement: Element): Array<Element | Text> {\n return Array.from(parentElement.childNodes) as Element[]\n },\n isNativeElementNode(node: Element | Text): node is Element {\n return node instanceof Element\n },\n getChildByIndex(parentElement, index) {\n return parentElement.childNodes[index] as Element\n },\n getAndUpdateSlotRootNativeElement(vEle: VElement, update: (nativeElement: (Element | null)) => void) {\n const currentRef = vEle.attrs.get('ref')\n if (currentRef) {\n vEle.attrs.set('ref', (v: Element) => {\n update(v)\n if (typeof currentRef === 'function') {\n currentRef(v)\n } else if (!currentRef.current) {\n currentRef.current = v\n }\n })\n } else {\n vEle.attrs.set('ref', update)\n }\n },\n componentRender: (component: Component<any>): JSX.Element => {\n const comp = this.components[component.name] || this.components['*']\n if (comp) {\n component.changeMarker.rendered()\n return createElement(comp, {\n key: component.id,\n component\n })\n }\n throw adapterError(`cannot found view component \\`${component.name}\\`!`)\n },\n vElementToViewElement(vNode: VElement, children: Array<string | JSX.Element>): JSX.Element {\n const props: any = {\n ...(Array.from(vNode.attrs).reduce((a, b) => {\n a[b[0]] = b[1]\n return a\n }, {} as Record<string, any>))\n }\n if (vNode.classes.size) {\n props.className = Array.from(vNode.classes).join(' ')\n }\n if (vNode.styles) {\n props.style = Array.from(vNode.styles).reduce((a, b) => {\n a[b[0]] = b[1]\n return a\n }, {} as Record<string, any>)\n }\n return createElement(vNode.tagName, props, ...children)\n }\n }, mount)\n Object.keys(components).forEach(key => {\n this.components[key] = (props: {component: Component}) => {\n const component = props.component\n const [updateKey, refreshUpdateKey] = useState(Math.random())\n\n useEffect(() => {\n const sub = merge(component.changeMarker.onChange, component.changeMarker.onForceChange).subscribe(() => {\n if (component.changeMarker.dirty) {\n refreshUpdateKey(Math.random())\n }\n })\n return () => {\n sub.unsubscribe()\n }\n }, [])\n useEffect(() => {\n this.onViewUpdated.next()\n }, [updateKey])\n const vNode = components[key]({\n component,\n rootRef: (rootNode: Element) => {\n if (rootNode) {\n this.componentRootElementCaches.set(component, rootNode)\n } else {\n // 当组件移动层级位置到原位置之前并重新渲染后,由于时序的原因,再删除缓存会导致组件找不到对应视图节点\n // eslint-disable-next-line no-lonely-if\n if (this.componentRootElementCaches.get(component) === rootNode) {\n this.componentRootElementCaches.remove(component)\n }\n }\n }\n })\n useEffect(() => {\n if (!this.componentRootElementCaches.get(component)) {\n // eslint-disable-next-line max-len\n throw adapterError(`Component \\`${component.name}\\` is not bound to rootRef, you must bind rootRef to the root element node of the component view.`)\n }\n })\n return vNode\n }\n })\n }\n\n override render(rootComponent: Component, injector: Injector): void | (() => void) {\n const childInjector = new ReflectiveInjector(injector, [{\n provide: Adapter,\n useValue: this\n }, {\n provide: DomAdapter,\n useValue: this\n }, {\n provide: ReactAdapter,\n useValue: this\n }])\n return super.render(rootComponent, childInjector)\n }\n\n override copy() {\n document.execCommand('copy')\n }\n}\n"],"names":["makeError","DomAdapter","VElement","VTextNode","createElement","useState","useEffect","merge","ReflectiveInjector","Adapter"],"mappings":";;;;;;;AAMA,MAAM,eAAeA,KAAAA,UAAU,cAAc;AAWtC,MAAM,qBAAqBC,gBAAAA,WAAqC;AAAA,EAC7D,aAA6E,CAAA;AAAA,EAErF,YAAY,YACA,OAAwC;AAClD,UAAM;AAAA,MACJ,sBAAsB,kBACA,6BAA+E;AACnG,eAAO,IAAIC,KAAAA,SAAS,QAAQ;AAAA,UAC1B,OAAO;AAAA,YACL,gBAAgB;AAAA,UAAA;AAAA,UAElB,KAAK,CAAC,SAAkB;AACtB,wCAA4B,IAAI;AAAA,UAClC;AAAA,QAAA,GACC;AAAA,UACD,IAAIC,KAAAA,UAAU,iBAAiB,IAAI;AAAA,QAAA,CACpC;AAAA,MACH;AAAA,MACA,cAAc,MAAsC;AAClD,eAAQ,KAAc;AAAA,MACxB;AAAA,MACA,cAAc,eAA+C;AAC3D,eAAO,MAAM,KAAK,cAAc,UAAU;AAAA,MAC5C;AAAA,MACA,oBAAoB,MAAuC;AACzD,eAAO,gBAAgB;AAAA,MACzB;AAAA,MACA,gBAAgB,eAAe,OAAO;AACpC,eAAO,cAAc,WAAW,KAAK;AAAA,MACvC;AAAA,MACA,kCAAkC,MAAgB,QAAmD;AACnG,cAAM,aAAa,KAAK,MAAM,IAAI,KAAK;AACvC,YAAI,YAAY;AACd,eAAK,MAAM,IAAI,OAAO,CAAC,MAAe;AACp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,eAAe,aAAa;AAAA,EAClD;AAAA,EAES,OAAO;AACd,aAAS,YAAY,MAAM;AAAA,EAC7B;AACF;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@textbus/adapter-react",
3
- "version": "5.2.1",
3
+ "version": "5.2.3",
4
4
  "description": "Textbus is a rich text editor and framework that is highly customizable and extensible to achieve rich wysiwyg effects.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.esm.js",
@@ -33,9 +33,9 @@
33
33
  ],
34
34
  "dependencies": {
35
35
  "@tanbo/stream": "^1.2.7",
36
- "@viewfly/core": "^2.2.0 || >=3.0.0-alpha.0 <4",
37
- "@textbus/core": "^5.2.1",
38
- "@textbus/platform-browser": "^5.2.1"
36
+ "@viewfly/core": "^2.2.0 || >=3.0.0-alpha.3 <4",
37
+ "@textbus/core": "^5.2.3",
38
+ "@textbus/platform-browser": "^5.2.3"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "react": "^17.0.0 || ^18.0.0"