@textbus/adapter-react 5.2.2 → 5.2.4

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,352 @@
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 {
8
- components = {};
9
- constructor(a, c) {
10
- super({
11
- createCompositionNode(e, r) {
12
- return new l("span", {
13
- style: {
14
- textDecoration: "underline"
15
- },
16
- ref: (t) => {
17
- r(t);
18
- }
19
- }, [
20
- new y(e.text)
21
- ]);
22
- },
23
- getParentNode(e) {
24
- return e.parentNode;
25
- },
26
- getChildNodes(e) {
27
- return Array.from(e.childNodes);
28
- },
29
- isNativeElementNode(e) {
30
- return e instanceof Element;
31
- },
32
- getChildByIndex(e, r) {
33
- return e.childNodes[r];
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);
40
- },
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
47
- });
48
- throw u(`cannot found view component \`${e.name}\`!`);
49
- },
50
- vElementToViewElement(e, r) {
51
- const t = {
52
- ...Array.from(e.attrs).reduce((n, o) => (n[o[0]] = o[1], n), {})
53
- };
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);
55
- }
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());
62
- });
63
- return () => {
64
- s.unsubscribe();
65
- };
66
- }, []), i(() => {
67
- 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);
73
- }
1
+ import { makeError, VElement, VTextNode, Adapter } from '@textbus/core';
2
+ import { DomAdapter } from '@textbus/platform-browser';
3
+ import { useState, useEffect, createElement } from 'react';
4
+ import { ReflectiveInjector } from '@viewfly/core';
5
+ import { merge } from '@tanbo/stream';
6
+
7
+ function _array_like_to_array(arr, len) {
8
+ if (len == null || len > arr.length) len = arr.length;
9
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
10
+ return arr2;
11
+ }
12
+ function _array_with_holes(arr) {
13
+ if (Array.isArray(arr)) return arr;
14
+ }
15
+ function _array_without_holes(arr) {
16
+ if (Array.isArray(arr)) return _array_like_to_array(arr);
17
+ }
18
+ function _assert_this_initialized(self) {
19
+ if (self === void 0) {
20
+ throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
21
+ }
22
+ return self;
23
+ }
24
+ function _call_super(_this, derived, args) {
25
+ derived = _get_prototype_of(derived);
26
+ return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
27
+ }
28
+ function _class_call_check(instance, Constructor) {
29
+ if (!(instance instanceof Constructor)) {
30
+ throw new TypeError("Cannot call a class as a function");
31
+ }
32
+ }
33
+ function _defineProperties(target, props) {
34
+ for(var i = 0; i < props.length; i++){
35
+ var descriptor = props[i];
36
+ descriptor.enumerable = descriptor.enumerable || false;
37
+ descriptor.configurable = true;
38
+ if ("value" in descriptor) descriptor.writable = true;
39
+ Object.defineProperty(target, descriptor.key, descriptor);
40
+ }
41
+ }
42
+ function _create_class(Constructor, protoProps, staticProps) {
43
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
44
+ return Constructor;
45
+ }
46
+ function _define_property(obj, key, value) {
47
+ if (key in obj) {
48
+ Object.defineProperty(obj, key, {
49
+ value: value,
50
+ enumerable: true,
51
+ configurable: true,
52
+ writable: true
74
53
  });
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;
79
- };
54
+ } else {
55
+ obj[key] = value;
56
+ }
57
+ return obj;
58
+ }
59
+ function _get(target, property, receiver) {
60
+ if (typeof Reflect !== "undefined" && Reflect.get) {
61
+ _get = Reflect.get;
62
+ } else {
63
+ _get = function get(target, property, receiver) {
64
+ var base = _super_prop_base(target, property);
65
+ if (!base) return;
66
+ var desc = Object.getOwnPropertyDescriptor(base, property);
67
+ if (desc.get) {
68
+ return desc.get.call(receiver || target);
69
+ }
70
+ return desc.value;
71
+ };
72
+ }
73
+ return _get(target, property, receiver || target);
74
+ }
75
+ function _get_prototype_of(o) {
76
+ _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
77
+ return o.__proto__ || Object.getPrototypeOf(o);
78
+ };
79
+ return _get_prototype_of(o);
80
+ }
81
+ function _inherits(subClass, superClass) {
82
+ if (typeof superClass !== "function" && superClass !== null) {
83
+ throw new TypeError("Super expression must either be null or a function");
84
+ }
85
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
86
+ constructor: {
87
+ value: subClass,
88
+ writable: true,
89
+ configurable: true
90
+ }
80
91
  });
81
- }
82
- render(a, c) {
83
- const e = new R(c, [{
84
- provide: g,
85
- useValue: this
86
- }, {
87
- provide: m,
88
- useValue: this
89
- }, {
90
- provide: f,
91
- useValue: this
92
- }]);
93
- return super.render(a, e);
94
- }
95
- copy() {
96
- document.execCommand("copy");
97
- }
98
- }
99
- export {
100
- f as ReactAdapter
101
- };
92
+ if (superClass) _set_prototype_of(subClass, superClass);
93
+ }
94
+ function _instanceof(left, right) {
95
+ "@swc/helpers - instanceof";
96
+ if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
97
+ return !!right[Symbol.hasInstance](left);
98
+ } else {
99
+ return left instanceof right;
100
+ }
101
+ }
102
+ function _iterable_to_array(iter) {
103
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
104
+ }
105
+ function _iterable_to_array_limit(arr, i) {
106
+ var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
107
+ if (_i == null) return;
108
+ var _arr = [];
109
+ var _n = true;
110
+ var _d = false;
111
+ var _s, _e;
112
+ try {
113
+ for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
114
+ _arr.push(_s.value);
115
+ if (i && _arr.length === i) break;
116
+ }
117
+ } catch (err) {
118
+ _d = true;
119
+ _e = err;
120
+ } finally{
121
+ try {
122
+ if (!_n && _i["return"] != null) _i["return"]();
123
+ } finally{
124
+ if (_d) throw _e;
125
+ }
126
+ }
127
+ return _arr;
128
+ }
129
+ function _non_iterable_rest() {
130
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
131
+ }
132
+ function _non_iterable_spread() {
133
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
134
+ }
135
+ function _object_spread(target) {
136
+ for(var i = 1; i < arguments.length; i++){
137
+ var source = arguments[i] != null ? arguments[i] : {};
138
+ var ownKeys = Object.keys(source);
139
+ if (typeof Object.getOwnPropertySymbols === "function") {
140
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
141
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
142
+ }));
143
+ }
144
+ ownKeys.forEach(function(key) {
145
+ _define_property(target, key, source[key]);
146
+ });
147
+ }
148
+ return target;
149
+ }
150
+ function _possible_constructor_return(self, call) {
151
+ if (call && (_type_of(call) === "object" || typeof call === "function")) {
152
+ return call;
153
+ }
154
+ return _assert_this_initialized(self);
155
+ }
156
+ function _set_prototype_of(o, p) {
157
+ _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
158
+ o.__proto__ = p;
159
+ return o;
160
+ };
161
+ return _set_prototype_of(o, p);
162
+ }
163
+ function _sliced_to_array(arr, i) {
164
+ return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
165
+ }
166
+ function _super_prop_base(object, property) {
167
+ while(!Object.prototype.hasOwnProperty.call(object, property)){
168
+ object = _get_prototype_of(object);
169
+ if (object === null) break;
170
+ }
171
+ return object;
172
+ }
173
+ function _to_consumable_array(arr) {
174
+ return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
175
+ }
176
+ function _type_of(obj) {
177
+ "@swc/helpers - typeof";
178
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
179
+ }
180
+ function _unsupported_iterable_to_array(o, minLen) {
181
+ if (!o) return;
182
+ if (typeof o === "string") return _array_like_to_array(o, minLen);
183
+ var n = Object.prototype.toString.call(o).slice(8, -1);
184
+ if (n === "Object" && o.constructor) n = o.constructor.name;
185
+ if (n === "Map" || n === "Set") return Array.from(n);
186
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
187
+ }
188
+ function _is_native_reflect_construct() {
189
+ try {
190
+ var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
191
+ } catch (_) {}
192
+ return (_is_native_reflect_construct = function() {
193
+ return !!result;
194
+ })();
195
+ }
196
+ var adapterError = makeError('ReactAdapter');
197
+ var ReactAdapter = /*#__PURE__*/ function(DomAdapter1) {
198
+ _inherits(ReactAdapter, DomAdapter1);
199
+ function ReactAdapter(components, mount) {
200
+ _class_call_check(this, ReactAdapter);
201
+ var _this;
202
+ _this = _call_super(this, ReactAdapter, [
203
+ {
204
+ createCompositionNode: function createCompositionNode(compositionState, updateNativeCompositionNode) {
205
+ return new VElement('span', {
206
+ style: {
207
+ textDecoration: 'underline'
208
+ },
209
+ ref: function ref(node) {
210
+ updateNativeCompositionNode(node);
211
+ }
212
+ }, [
213
+ new VTextNode(compositionState.text)
214
+ ]);
215
+ },
216
+ getParentNode: function getParentNode(node) {
217
+ return node.parentNode;
218
+ },
219
+ getChildNodes: function getChildNodes(parentElement) {
220
+ return Array.from(parentElement.childNodes);
221
+ },
222
+ isNativeElementNode: function isNativeElementNode(node) {
223
+ return _instanceof(node, Element);
224
+ },
225
+ getChildByIndex: function getChildByIndex(parentElement, index) {
226
+ return parentElement.childNodes[index];
227
+ },
228
+ getAndUpdateSlotRootNativeElement: function getAndUpdateSlotRootNativeElement(vEle, update) {
229
+ var currentRef = vEle.attrs.get('ref');
230
+ if (currentRef) {
231
+ vEle.attrs.set('ref', function(v) {
232
+ update(v);
233
+ if (typeof currentRef === 'function') {
234
+ currentRef(v);
235
+ } else if (!currentRef.current) {
236
+ currentRef.current = v;
237
+ }
238
+ });
239
+ } else {
240
+ vEle.attrs.set('ref', update);
241
+ }
242
+ },
243
+ componentRender: function componentRender(component) {
244
+ var comp = _assert_this_initialized(_this).components[component.name] || _assert_this_initialized(_this).components['*'];
245
+ if (comp) {
246
+ component.changeMarker.rendered();
247
+ return createElement(comp, {
248
+ key: component.id,
249
+ component: component
250
+ });
251
+ }
252
+ throw adapterError("cannot found view component `".concat(component.name, "`!"));
253
+ },
254
+ vElementToViewElement: function vElementToViewElement(vNode, children) {
255
+ var props = _object_spread({}, Array.from(vNode.attrs).reduce(function(a, b) {
256
+ a[b[0]] = b[1];
257
+ return a;
258
+ }, {}));
259
+ if (vNode.classes.size) {
260
+ props.className = Array.from(vNode.classes).join(' ');
261
+ }
262
+ if (vNode.styles) {
263
+ props.style = Array.from(vNode.styles).reduce(function(a, b) {
264
+ a[b[0]] = b[1];
265
+ return a;
266
+ }, {});
267
+ }
268
+ return createElement.apply(void 0, [
269
+ vNode.tagName,
270
+ props
271
+ ].concat(_to_consumable_array(children)));
272
+ }
273
+ },
274
+ mount
275
+ ]), _define_property(_this, "components", {});
276
+ Object.keys(components).forEach(function(key) {
277
+ _this.components[key] = function(props) {
278
+ var component = props.component;
279
+ var _useState = _sliced_to_array(useState(Math.random()), 2), updateKey = _useState[0], refreshUpdateKey = _useState[1];
280
+ useEffect(function() {
281
+ var sub = merge(component.changeMarker.onChange, component.changeMarker.onForceChange).subscribe(function() {
282
+ if (component.changeMarker.dirty) {
283
+ refreshUpdateKey(Math.random());
284
+ }
285
+ });
286
+ return function() {
287
+ sub.unsubscribe();
288
+ };
289
+ }, []);
290
+ useEffect(function() {
291
+ _this.onViewUpdated.next();
292
+ }, [
293
+ updateKey
294
+ ]);
295
+ var vNode = components[key]({
296
+ component: component,
297
+ rootRef: function rootRef(rootNode) {
298
+ if (rootNode) {
299
+ _this.componentRootElementCaches.set(component, rootNode);
300
+ } else {
301
+ // 当组件移动层级位置到原位置之前并重新渲染后,由于时序的原因,再删除缓存会导致组件找不到对应视图节点
302
+ // eslint-disable-next-line no-lonely-if
303
+ if (_this.componentRootElementCaches.get(component) === rootNode) {
304
+ _this.componentRootElementCaches.remove(component);
305
+ }
306
+ }
307
+ }
308
+ });
309
+ useEffect(function() {
310
+ if (!_this.componentRootElementCaches.get(component)) {
311
+ // eslint-disable-next-line max-len
312
+ throw adapterError("Component `".concat(component.name, "` is not bound to rootRef, you must bind rootRef to the root element node of the component view."));
313
+ }
314
+ });
315
+ return vNode;
316
+ };
317
+ });
318
+ return _this;
319
+ }
320
+ _create_class(ReactAdapter, [
321
+ {
322
+ key: "render",
323
+ value: function render(rootComponent, injector) {
324
+ var childInjector = new ReflectiveInjector(injector, [
325
+ {
326
+ provide: Adapter,
327
+ useValue: this
328
+ },
329
+ {
330
+ provide: DomAdapter,
331
+ useValue: this
332
+ },
333
+ {
334
+ provide: ReactAdapter,
335
+ useValue: this
336
+ }
337
+ ]);
338
+ return _get(_get_prototype_of(ReactAdapter.prototype), "render", this).call(this, rootComponent, childInjector);
339
+ }
340
+ },
341
+ {
342
+ key: "copy",
343
+ value: function copy() {
344
+ document.execCommand('copy');
345
+ }
346
+ }
347
+ ]);
348
+ return ReactAdapter;
349
+ }(DomAdapter);
350
+
351
+ export { ReactAdapter };
102
352
  //# 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,M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ACF,WAAO,MAAM,OAAO+B,GAAeE,CAAa;AAAA,EAClD;AAAA,EAES,OAAO;AACd,aAAS,YAAY,MAAM;AAAA,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)\n }\n\n override copy() {\n document.execCommand('copy')\n }\n}\n"],"names":["adapterError","makeError","ReactAdapter","components","mount","createCompositionNode","compositionState","updateNativeCompositionNode","VElement","style","textDecoration","ref","node","VTextNode","text","getParentNode","parentNode","getChildNodes","parentElement","Array","from","childNodes","isNativeElementNode","Element","getChildByIndex","index","getAndUpdateSlotRootNativeElement","vEle","update","currentRef","attrs","get","set","v","current","componentRender","component","comp","name","changeMarker","rendered","createElement","key","id","vElementToViewElement","vNode","children","props","reduce","a","b","classes","size","className","join","styles","tagName","Object","keys","forEach","useState","Math","random","updateKey","refreshUpdateKey","useEffect","sub","merge","onChange","onForceChange","subscribe","dirty","unsubscribe","onViewUpdated","next","rootRef","rootNode","componentRootElementCaches","remove","render","rootComponent","injector","childInjector","ReflectiveInjector","provide","Adapter","useValue","DomAdapter","copy","document","execCommand"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA,IAAMA,eAAeC,SAAAA,CAAU,cAAA,CAAA;AAWxB,IAAA,YAAMC,iBAAN,SAAA,WAAA,EAAA;AAAMA,IAAAA,SAAAA,CAAAA,YAAAA,EAAAA,WAAAA,CAAAA;aAAAA,YAAAA,CAGCC,UAAkC,EAClCC,KAAsC,EAAA;AAJvCF,QAAAA,iBAAAA,CAAAA,IAAAA,EAAAA,YAAAA,CAAAA;;gBAKT,WAAA,CAAA,IAAA,EALSA,YAAAA,EAAAA;AAKH,YAAA;AACJG,gBAAAA,qBAAAA,EAAAA,SAAAA,qBAAAA,CAAsBC,g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pC,aAAa,aAAC,CAA6B,MAAA,CAAfoC,SAAAA,CAAUE,IAAI,EAAC,kGAAA,CAAA,CAAA;AACnD,oBAAA;AACF,gBAAA,CAAA,CAAA;gBACA,OAAOO,KAAAA;AACT,YAAA,CAAA;AACF,QAAA,CAAA,CAAA;;;AApHS3C,IAAAA,aAAAA,CAAAA,YAAAA,EAAAA;;YAuHF6E,GAAAA,EAAAA,QAAAA;mBAAT,SAASA,MAAAA,CAAOC,aAAwB,EAAEC,QAAkB,EAAA;gBAC1D,IAAMC,aAAAA,GAAgB,IAAIC,kBAAAA,CAAmBF,QAAAA,EAAU;AAAC,oBAAA;wBACtDG,OAAAA,EAASC,OAAAA;AACTC,wBAAAA,QAAAA,EAAU;AACZ,qBAAA;AAAG,oBAAA;wBACDF,OAAAA,EAASG,UAAAA;AACTD,wBAAAA,QAAAA,EAAU;AACZ,qBAAA;AAAG,oBAAA;wBACDF,OAAAA,EA/HOlF,YAAAA;AAgIPoF,wBAAAA,QAAAA,EAAU;AACZ;AAAE,iBAAA,CAAA;AACF,gBAAA,OAAO,IAAA,CAAA,iBAAA,CAlIEpF,YAAAA,CAAAA,SAAAA,CAAAA,EAkII6E,QAAAA,EAAN,IAAK,aAAQC,aAAAA,EAAeE,aAAAA,CAAAA;AACrC,YAAA;;;YAESM,GAAAA,EAAAA,MAAAA;mBAAT,SAASA,IAAAA,GAAAA;AACPC,gBAAAA,QAAAA,CAASC,WAAW,CAAC,MAAA,CAAA;AACvB,YAAA;;;AAvIWxF,IAAAA,OAAAA,YAAAA;EAAqBqF,UAAAA;;;;"}
package/dist/index.js CHANGED
@@ -1,2 +1,356 @@
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
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const core = require('@textbus/core');
6
+ const platformBrowser = require('@textbus/platform-browser');
7
+ const react = require('react');
8
+ const core$1 = require('@viewfly/core');
9
+ const stream = require('@tanbo/stream');
10
+
11
+ function _array_like_to_array(arr, len) {
12
+ if (len == null || len > arr.length) len = arr.length;
13
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
14
+ return arr2;
15
+ }
16
+ function _array_with_holes(arr) {
17
+ if (Array.isArray(arr)) return arr;
18
+ }
19
+ function _array_without_holes(arr) {
20
+ if (Array.isArray(arr)) return _array_like_to_array(arr);
21
+ }
22
+ function _assert_this_initialized(self) {
23
+ if (self === void 0) {
24
+ throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
25
+ }
26
+ return self;
27
+ }
28
+ function _call_super(_this, derived, args) {
29
+ derived = _get_prototype_of(derived);
30
+ return _possible_constructor_return(_this, _is_native_reflect_construct() ? Reflect.construct(derived, args || [], _get_prototype_of(_this).constructor) : derived.apply(_this, args));
31
+ }
32
+ function _class_call_check(instance, Constructor) {
33
+ if (!(instance instanceof Constructor)) {
34
+ throw new TypeError("Cannot call a class as a function");
35
+ }
36
+ }
37
+ function _defineProperties(target, props) {
38
+ for(var i = 0; i < props.length; i++){
39
+ var descriptor = props[i];
40
+ descriptor.enumerable = descriptor.enumerable || false;
41
+ descriptor.configurable = true;
42
+ if ("value" in descriptor) descriptor.writable = true;
43
+ Object.defineProperty(target, descriptor.key, descriptor);
44
+ }
45
+ }
46
+ function _create_class(Constructor, protoProps, staticProps) {
47
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
48
+ return Constructor;
49
+ }
50
+ function _define_property(obj, key, value) {
51
+ if (key in obj) {
52
+ Object.defineProperty(obj, key, {
53
+ value: value,
54
+ enumerable: true,
55
+ configurable: true,
56
+ writable: true
57
+ });
58
+ } else {
59
+ obj[key] = value;
60
+ }
61
+ return obj;
62
+ }
63
+ function _get(target, property, receiver) {
64
+ if (typeof Reflect !== "undefined" && Reflect.get) {
65
+ _get = Reflect.get;
66
+ } else {
67
+ _get = function get(target, property, receiver) {
68
+ var base = _super_prop_base(target, property);
69
+ if (!base) return;
70
+ var desc = Object.getOwnPropertyDescriptor(base, property);
71
+ if (desc.get) {
72
+ return desc.get.call(receiver || target);
73
+ }
74
+ return desc.value;
75
+ };
76
+ }
77
+ return _get(target, property, receiver || target);
78
+ }
79
+ function _get_prototype_of(o) {
80
+ _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
81
+ return o.__proto__ || Object.getPrototypeOf(o);
82
+ };
83
+ return _get_prototype_of(o);
84
+ }
85
+ function _inherits(subClass, superClass) {
86
+ if (typeof superClass !== "function" && superClass !== null) {
87
+ throw new TypeError("Super expression must either be null or a function");
88
+ }
89
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
90
+ constructor: {
91
+ value: subClass,
92
+ writable: true,
93
+ configurable: true
94
+ }
95
+ });
96
+ if (superClass) _set_prototype_of(subClass, superClass);
97
+ }
98
+ function _instanceof(left, right) {
99
+ "@swc/helpers - instanceof";
100
+ if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
101
+ return !!right[Symbol.hasInstance](left);
102
+ } else {
103
+ return left instanceof right;
104
+ }
105
+ }
106
+ function _iterable_to_array(iter) {
107
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
108
+ }
109
+ function _iterable_to_array_limit(arr, i) {
110
+ var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
111
+ if (_i == null) return;
112
+ var _arr = [];
113
+ var _n = true;
114
+ var _d = false;
115
+ var _s, _e;
116
+ try {
117
+ for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
118
+ _arr.push(_s.value);
119
+ if (i && _arr.length === i) break;
120
+ }
121
+ } catch (err) {
122
+ _d = true;
123
+ _e = err;
124
+ } finally{
125
+ try {
126
+ if (!_n && _i["return"] != null) _i["return"]();
127
+ } finally{
128
+ if (_d) throw _e;
129
+ }
130
+ }
131
+ return _arr;
132
+ }
133
+ function _non_iterable_rest() {
134
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
135
+ }
136
+ function _non_iterable_spread() {
137
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
138
+ }
139
+ function _object_spread(target) {
140
+ for(var i = 1; i < arguments.length; i++){
141
+ var source = arguments[i] != null ? arguments[i] : {};
142
+ var ownKeys = Object.keys(source);
143
+ if (typeof Object.getOwnPropertySymbols === "function") {
144
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
145
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
146
+ }));
147
+ }
148
+ ownKeys.forEach(function(key) {
149
+ _define_property(target, key, source[key]);
150
+ });
151
+ }
152
+ return target;
153
+ }
154
+ function _possible_constructor_return(self, call) {
155
+ if (call && (_type_of(call) === "object" || typeof call === "function")) {
156
+ return call;
157
+ }
158
+ return _assert_this_initialized(self);
159
+ }
160
+ function _set_prototype_of(o, p) {
161
+ _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
162
+ o.__proto__ = p;
163
+ return o;
164
+ };
165
+ return _set_prototype_of(o, p);
166
+ }
167
+ function _sliced_to_array(arr, i) {
168
+ return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
169
+ }
170
+ function _super_prop_base(object, property) {
171
+ while(!Object.prototype.hasOwnProperty.call(object, property)){
172
+ object = _get_prototype_of(object);
173
+ if (object === null) break;
174
+ }
175
+ return object;
176
+ }
177
+ function _to_consumable_array(arr) {
178
+ return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
179
+ }
180
+ function _type_of(obj) {
181
+ "@swc/helpers - typeof";
182
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
183
+ }
184
+ function _unsupported_iterable_to_array(o, minLen) {
185
+ if (!o) return;
186
+ if (typeof o === "string") return _array_like_to_array(o, minLen);
187
+ var n = Object.prototype.toString.call(o).slice(8, -1);
188
+ if (n === "Object" && o.constructor) n = o.constructor.name;
189
+ if (n === "Map" || n === "Set") return Array.from(n);
190
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
191
+ }
192
+ function _is_native_reflect_construct() {
193
+ try {
194
+ var result = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
195
+ } catch (_) {}
196
+ return (_is_native_reflect_construct = function() {
197
+ return !!result;
198
+ })();
199
+ }
200
+ var adapterError = core.makeError('ReactAdapter');
201
+ var ReactAdapter = /*#__PURE__*/ function(DomAdapter1) {
202
+ _inherits(ReactAdapter, DomAdapter1);
203
+ function ReactAdapter(components, mount) {
204
+ _class_call_check(this, ReactAdapter);
205
+ var _this;
206
+ _this = _call_super(this, ReactAdapter, [
207
+ {
208
+ createCompositionNode: function createCompositionNode(compositionState, updateNativeCompositionNode) {
209
+ return new core.VElement('span', {
210
+ style: {
211
+ textDecoration: 'underline'
212
+ },
213
+ ref: function ref(node) {
214
+ updateNativeCompositionNode(node);
215
+ }
216
+ }, [
217
+ new core.VTextNode(compositionState.text)
218
+ ]);
219
+ },
220
+ getParentNode: function getParentNode(node) {
221
+ return node.parentNode;
222
+ },
223
+ getChildNodes: function getChildNodes(parentElement) {
224
+ return Array.from(parentElement.childNodes);
225
+ },
226
+ isNativeElementNode: function isNativeElementNode(node) {
227
+ return _instanceof(node, Element);
228
+ },
229
+ getChildByIndex: function getChildByIndex(parentElement, index) {
230
+ return parentElement.childNodes[index];
231
+ },
232
+ getAndUpdateSlotRootNativeElement: function getAndUpdateSlotRootNativeElement(vEle, update) {
233
+ var currentRef = vEle.attrs.get('ref');
234
+ if (currentRef) {
235
+ vEle.attrs.set('ref', function(v) {
236
+ update(v);
237
+ if (typeof currentRef === 'function') {
238
+ currentRef(v);
239
+ } else if (!currentRef.current) {
240
+ currentRef.current = v;
241
+ }
242
+ });
243
+ } else {
244
+ vEle.attrs.set('ref', update);
245
+ }
246
+ },
247
+ componentRender: function componentRender(component) {
248
+ var comp = _assert_this_initialized(_this).components[component.name] || _assert_this_initialized(_this).components['*'];
249
+ if (comp) {
250
+ component.changeMarker.rendered();
251
+ return react.createElement(comp, {
252
+ key: component.id,
253
+ component: component
254
+ });
255
+ }
256
+ throw adapterError("cannot found view component `".concat(component.name, "`!"));
257
+ },
258
+ vElementToViewElement: function vElementToViewElement(vNode, children) {
259
+ var props = _object_spread({}, Array.from(vNode.attrs).reduce(function(a, b) {
260
+ a[b[0]] = b[1];
261
+ return a;
262
+ }, {}));
263
+ if (vNode.classes.size) {
264
+ props.className = Array.from(vNode.classes).join(' ');
265
+ }
266
+ if (vNode.styles) {
267
+ props.style = Array.from(vNode.styles).reduce(function(a, b) {
268
+ a[b[0]] = b[1];
269
+ return a;
270
+ }, {});
271
+ }
272
+ return react.createElement.apply(void 0, [
273
+ vNode.tagName,
274
+ props
275
+ ].concat(_to_consumable_array(children)));
276
+ }
277
+ },
278
+ mount
279
+ ]), _define_property(_this, "components", {});
280
+ Object.keys(components).forEach(function(key) {
281
+ _this.components[key] = function(props) {
282
+ var component = props.component;
283
+ var _useState = _sliced_to_array(react.useState(Math.random()), 2), updateKey = _useState[0], refreshUpdateKey = _useState[1];
284
+ react.useEffect(function() {
285
+ var sub = stream.merge(component.changeMarker.onChange, component.changeMarker.onForceChange).subscribe(function() {
286
+ if (component.changeMarker.dirty) {
287
+ refreshUpdateKey(Math.random());
288
+ }
289
+ });
290
+ return function() {
291
+ sub.unsubscribe();
292
+ };
293
+ }, []);
294
+ react.useEffect(function() {
295
+ _this.onViewUpdated.next();
296
+ }, [
297
+ updateKey
298
+ ]);
299
+ var vNode = components[key]({
300
+ component: component,
301
+ rootRef: function rootRef(rootNode) {
302
+ if (rootNode) {
303
+ _this.componentRootElementCaches.set(component, rootNode);
304
+ } else {
305
+ // 当组件移动层级位置到原位置之前并重新渲染后,由于时序的原因,再删除缓存会导致组件找不到对应视图节点
306
+ // eslint-disable-next-line no-lonely-if
307
+ if (_this.componentRootElementCaches.get(component) === rootNode) {
308
+ _this.componentRootElementCaches.remove(component);
309
+ }
310
+ }
311
+ }
312
+ });
313
+ react.useEffect(function() {
314
+ if (!_this.componentRootElementCaches.get(component)) {
315
+ // eslint-disable-next-line max-len
316
+ throw adapterError("Component `".concat(component.name, "` is not bound to rootRef, you must bind rootRef to the root element node of the component view."));
317
+ }
318
+ });
319
+ return vNode;
320
+ };
321
+ });
322
+ return _this;
323
+ }
324
+ _create_class(ReactAdapter, [
325
+ {
326
+ key: "render",
327
+ value: function render(rootComponent, injector) {
328
+ var childInjector = new core$1.ReflectiveInjector(injector, [
329
+ {
330
+ provide: core.Adapter,
331
+ useValue: this
332
+ },
333
+ {
334
+ provide: platformBrowser.DomAdapter,
335
+ useValue: this
336
+ },
337
+ {
338
+ provide: ReactAdapter,
339
+ useValue: this
340
+ }
341
+ ]);
342
+ return _get(_get_prototype_of(ReactAdapter.prototype), "render", this).call(this, rootComponent, childInjector);
343
+ }
344
+ },
345
+ {
346
+ key: "copy",
347
+ value: function copy() {
348
+ document.execCommand('copy');
349
+ }
350
+ }
351
+ ]);
352
+ return ReactAdapter;
353
+ }(platformBrowser.DomAdapter);
354
+
355
+ exports.ReactAdapter = ReactAdapter;
2
356
  //# 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,EAAAA,UAAU,cAAc,EAWtC,MA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AA0BC,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":["adapterError","makeError","ReactAdapter","components","mount","createCompositionNode","compositionState","updateNativeCompositionNode","VElement","style","textDecoration","ref","node","VTextNode","text","getParentNode","parentNode","getChildNodes","parentElement","Array","from","childNodes","isNativeElementNode","Element","getChildByIndex","index","getAndUpdateSlotRootNativeElement","vEle","update","currentRef","attrs","get","set","v","current","componentRender","component","comp","name","changeMarker","rendered","createElement","key","id","vElementToViewElement","vNode","children","props","reduce","a","b","classes","size","className","join","styles","tagName","Object","keys","forEach","useState","Math","random","updateKey","refreshUpdateKey","useEffect","sub","merge","onChange","onForceChange","subscribe","dirty","unsubscribe","onViewUpdated","next","rootRef","rootNode","componentRootElementCaches","remove","render","rootComponent","injector","childInjector","ReflectiveInjector","provide","Adapter","useValue","DomAdapter","copy","document","execCommand"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA,IAAMA,eAAeC,cAAAA,CAAU,cAAA,CAAA;AAWxB,IAAA,YAAMC,iBAAN,SAAA,WAAA,EAAA;AAAMA,IAAAA,SAAAA,CAAAA,YAAAA,EAAAA,WAAAA,CAAAA;aAAAA,YAAAA,CAGCC,UAAkC,EAClCC,KAAsC,EAAA;AAJvCF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",
3
+ "version": "5.2.4",
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.2",
38
- "@textbus/platform-browser": "^5.2.2"
36
+ "@viewfly/core": "^2.2.0 || >=3.0.0-alpha.3 <4",
37
+ "@textbus/platform-browser": "^5.2.4",
38
+ "@textbus/core": "^5.2.4"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "react": "^17.0.0 || ^18.0.0"