@textbus/adapter-viewfly 4.0.0-alpha.5 → 4.0.0-alpha.51

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.
@@ -1,24 +1,108 @@
1
- import { provide, onUpdated, useRef, jsx, Ref } from '@viewfly/core';
2
- import { Subject } from '@tanbo/stream';
3
- import { makeError, VElement, VTextNode, replaceEmpty } from '@textbus/core';
1
+ import { makeError, VElement, VTextNode, merge, Adapter, Subject } from '@textbus/core';
2
+ import { createDynamicRef, DynamicRef, jsx, withAnnotation, getCurrentInstance, onUpdated } from '@viewfly/core';
4
3
  import { DomAdapter } from '@textbus/platform-browser';
4
+ import { VDOMElement } from '@viewfly/platform-browser';
5
5
 
6
- const adapterError = makeError('ViewflyAdapter');
7
- /**
8
- * Textbus 桥接 [Viewfly](https://viewfly.org) 渲染能力适配器,用于在 Viewfly 项目中渲染 Textbus 数据
9
- */
10
- class Adapter extends DomAdapter {
6
+ const adapterError$1 = makeError('ViewflyDOMRenderer');
7
+ class ViewflyAdapter extends DomAdapter {
11
8
  constructor(components, mount) {
12
- super(mount);
13
- this.onViewUpdated = new Subject();
9
+ super({
10
+ createCompositionNode(compositionState, updateNativeCompositionNode) {
11
+ const ref = createDynamicRef(node => {
12
+ updateNativeCompositionNode(node);
13
+ return () => {
14
+ updateNativeCompositionNode(null);
15
+ };
16
+ });
17
+ return new VElement('span', {
18
+ style: {
19
+ textDecoration: 'underline'
20
+ },
21
+ ref
22
+ }, [
23
+ new VTextNode(compositionState.text)
24
+ ]);
25
+ },
26
+ getParentNode(node) {
27
+ return node.parentNode;
28
+ },
29
+ getChildNodes(parentElement) {
30
+ return Array.from(parentElement.childNodes);
31
+ },
32
+ isNativeElementNode(node) {
33
+ return node instanceof HTMLElement;
34
+ },
35
+ getChildByIndex(parentElement, index) {
36
+ return parentElement.childNodes[index];
37
+ },
38
+ getAndUpdateSlotRootNativeElement(vEle, update) {
39
+ const currentRef = vEle.attrs.get('ref');
40
+ const ref = createDynamicRef(nativeNode => {
41
+ update(nativeNode);
42
+ return () => {
43
+ update(null);
44
+ };
45
+ });
46
+ if (currentRef instanceof DynamicRef) {
47
+ vEle.attrs.set('ref', [currentRef, ref]);
48
+ }
49
+ else if (Array.isArray(currentRef)) {
50
+ currentRef.push(ref);
51
+ }
52
+ else {
53
+ vEle.attrs.set('ref', ref);
54
+ }
55
+ },
56
+ componentRender: (component) => {
57
+ const comp = this.components[component.name] || this.components['*'];
58
+ if (comp) {
59
+ let ref = this.componentRefs.get(component);
60
+ if (!ref) {
61
+ ref = createDynamicRef(rootNode => {
62
+ this.componentRootElementCaches.set(component, rootNode);
63
+ return () => {
64
+ this.componentRootElementCaches.remove(component);
65
+ };
66
+ });
67
+ this.componentRefs.set(component, ref);
68
+ }
69
+ return jsx(comp, {
70
+ component,
71
+ rootRef: ref
72
+ }, component.id);
73
+ }
74
+ throw adapterError$1(`cannot found view component \`${component.name}\`!`);
75
+ },
76
+ vElementToViewElement(vNode, children) {
77
+ const key = vNode.attrs.get('key');
78
+ vNode.attrs.delete('key');
79
+ const props = Object.assign({}, (Array.from(vNode.attrs).reduce((a, b) => {
80
+ a[b[0]] = b[1];
81
+ return a;
82
+ }, {})));
83
+ if (vNode.classes.size) {
84
+ props.class = Array.from(vNode.classes).join(' ');
85
+ }
86
+ if (vNode.styles) {
87
+ props.style = Array.from(vNode.styles).reduce((a, b) => {
88
+ a[b[0]] = b[1];
89
+ return a;
90
+ }, {});
91
+ }
92
+ if (children.length) {
93
+ props.children = children;
94
+ }
95
+ return jsx(vNode.tagName, props, key);
96
+ }
97
+ }, mount);
14
98
  this.components = {};
15
99
  this.componentRefs = new WeakMap();
16
100
  let isRoot = true;
17
- Object.keys(components).forEach(key => {
18
- this.components[key] = (props) => {
19
- const comp = provide([]);
101
+ Object.entries(components).forEach(([key, viewFlyComponent]) => {
102
+ this.components[key] = withAnnotation(Object.assign({}, viewFlyComponent.annotation), (props) => {
103
+ const comp = getCurrentInstance();
20
104
  const textbusComponent = props.component;
21
- textbusComponent.changeMarker.onChange.subscribe(() => {
105
+ merge(textbusComponent.changeMarker.onChange, textbusComponent.changeMarker.onForceChange).subscribe(() => {
22
106
  if (textbusComponent.changeMarker.dirty) {
23
107
  comp.markAsDirtied();
24
108
  }
@@ -30,85 +114,187 @@ class Adapter extends DomAdapter {
30
114
  isRoot = false;
31
115
  }
32
116
  onUpdated(() => {
117
+ const context = this.componentRendingStack[this.componentRendingStack.length - 1];
118
+ if (context === component) {
119
+ this.componentRendingStack.pop();
120
+ }
33
121
  textbusComponent.changeMarker.rendered();
122
+ if (!this.componentRootElementCaches.get(textbusComponent)) {
123
+ // eslint-disable-next-line max-len
124
+ throw adapterError$1(`Component \`${textbusComponent.name}\` is not bound to rootRef, you must bind rootRef to the root element node of the component view.`);
125
+ }
34
126
  });
35
- return components[key](props);
36
- };
127
+ const component = props.component;
128
+ const instance = viewFlyComponent(props);
129
+ if (typeof instance === 'function') {
130
+ return () => {
131
+ component.__slots__.forEach(i => this.renderedSlotCache.delete(i));
132
+ component.__slots__.length = 0;
133
+ this.componentRendingStack.push(component);
134
+ return instance();
135
+ };
136
+ }
137
+ const self = this;
138
+ return Object.assign(Object.assign({}, instance), { $render() {
139
+ component.__slots__.forEach(i => self.renderedSlotCache.delete(i));
140
+ component.__slots__.length = 0;
141
+ self.componentRendingStack.push(component);
142
+ return instance.$render();
143
+ } });
144
+ });
37
145
  });
38
146
  }
39
- componentRender(component) {
40
- const comp = this.components[component.name] || this.components['*'];
41
- if (comp) {
42
- component.changeMarker.rendered();
43
- let ref = this.componentRefs.get(component);
44
- if (!ref) {
45
- ref = useRef(rootNode => {
46
- this.componentRootElementCaches.set(component, rootNode);
147
+ copy() {
148
+ document.execCommand('copy');
149
+ }
150
+ }
151
+
152
+ const adapterError = makeError('ViewflyHTMLRenderer');
153
+ class ViewflyVDomAdapter extends Adapter {
154
+ constructor(components, mount) {
155
+ super({
156
+ createCompositionNode(compositionState, updateNativeCompositionNode) {
157
+ const ref = createDynamicRef(node => {
158
+ updateNativeCompositionNode(node);
47
159
  return () => {
48
- this.componentRootElementCaches.remove(component);
160
+ updateNativeCompositionNode(null);
49
161
  };
50
162
  });
51
- this.componentRefs.set(component, ref);
52
- }
53
- return jsx(comp, {
54
- component,
55
- rootRef: ref
56
- }, component.id);
57
- }
58
- throw adapterError(`cannot found view component \`${component.name}\`!`);
59
- }
60
- slotRender(slot, slotHostRender, renderEnv) {
61
- const vElement = slot.toTree(slotHostRender, renderEnv);
62
- this.slotRootVElementCaches.set(slot, vElement);
63
- const vNodeToJSX = (vNode) => {
64
- const children = [];
65
- for (let i = 0; i < vNode.children.length; i++) {
66
- const child = vNode.children[i];
67
- if (child instanceof VElement) {
68
- children.push(vNodeToJSX(child));
69
- }
70
- else if (child instanceof VTextNode) {
71
- children.push(replaceEmpty(child.textContent));
163
+ return new VElement('span', {
164
+ style: {
165
+ textDecoration: 'underline'
166
+ },
167
+ ref
168
+ }, [
169
+ new VTextNode(compositionState.text)
170
+ ]);
171
+ },
172
+ getParentNode(node) {
173
+ return node.parent;
174
+ },
175
+ getChildNodes(parentElement) {
176
+ return parentElement.children.slice();
177
+ },
178
+ isNativeElementNode(node) {
179
+ return node instanceof VDOMElement;
180
+ },
181
+ getChildByIndex(parentElement, index) {
182
+ return parentElement.children[index];
183
+ },
184
+ getAndUpdateSlotRootNativeElement(vEle, update) {
185
+ const currentRef = vEle.attrs.get('ref');
186
+ const ref = createDynamicRef(nativeNode => {
187
+ update(nativeNode);
188
+ return () => {
189
+ update(null);
190
+ };
191
+ });
192
+ if (currentRef instanceof DynamicRef) {
193
+ vEle.attrs.set('ref', [currentRef, ref]);
194
+ }
195
+ else if (Array.isArray(currentRef)) {
196
+ currentRef.push(ref);
72
197
  }
73
198
  else {
74
- children.push(this.componentRender(child));
199
+ vEle.attrs.set('ref', ref);
75
200
  }
76
- }
77
- const props = Object.assign({}, (Array.from(vNode.attrs).reduce((a, b) => {
78
- a[b[0]] = b[1];
79
- return a;
80
- }, {})));
81
- if (vNode.classes.size) {
82
- props.class = Array.from(vNode.classes).join(' ');
83
- }
84
- if (vNode.styles) {
85
- props.style = Array.from(vNode.styles).reduce((a, b) => {
201
+ },
202
+ componentRender: (component) => {
203
+ const comp = this.components[component.name] || this.components['*'];
204
+ if (comp) {
205
+ let ref = this.componentRefs.get(component);
206
+ if (!ref) {
207
+ ref = createDynamicRef(rootNode => {
208
+ this.componentRootElementCaches.set(component, rootNode);
209
+ return () => {
210
+ this.componentRootElementCaches.remove(component);
211
+ };
212
+ });
213
+ this.componentRefs.set(component, ref);
214
+ }
215
+ return jsx(comp, {
216
+ component,
217
+ rootRef: ref
218
+ }, component.id);
219
+ }
220
+ throw adapterError(`cannot found view component \`${component.name}\`!`);
221
+ },
222
+ vElementToViewElement(vNode, children) {
223
+ const key = vNode.attrs.get('key');
224
+ vNode.attrs.delete('key');
225
+ const props = Object.assign({}, (Array.from(vNode.attrs).reduce((a, b) => {
86
226
  a[b[0]] = b[1];
87
227
  return a;
88
- }, {});
89
- }
90
- if (children.length) {
91
- props.children = children;
228
+ }, {})));
229
+ if (vNode.classes.size) {
230
+ props.class = Array.from(vNode.classes).join(' ');
231
+ }
232
+ if (vNode.styles) {
233
+ props.style = Array.from(vNode.styles).reduce((a, b) => {
234
+ a[b[0]] = b[1];
235
+ return a;
236
+ }, {});
237
+ }
238
+ if (children.length) {
239
+ props.children = children;
240
+ }
241
+ return jsx(vNode.tagName, props, key);
92
242
  }
93
- return jsx(vNode.tagName, props);
94
- };
95
- const jsxNode = vNodeToJSX(vElement);
96
- const currentRef = jsxNode.props.ref;
97
- const ref = useRef(nativeNode => {
98
- this.slotRootNativeElementCaches.set(slot, nativeNode);
243
+ }, mount);
244
+ this.onViewUpdated = new Subject();
245
+ this.host = new VDOMElement('body');
246
+ this.components = {};
247
+ this.componentRefs = new WeakMap();
248
+ let isRoot = true;
249
+ Object.entries(components).forEach(([key, viewFlyComponent]) => {
250
+ this.components[key] = withAnnotation(Object.assign({}, viewFlyComponent.annotation), (props) => {
251
+ const comp = getCurrentInstance();
252
+ const textbusComponent = props.component;
253
+ merge(textbusComponent.changeMarker.onChange, textbusComponent.changeMarker.onForceChange).subscribe(() => {
254
+ if (textbusComponent.changeMarker.dirty) {
255
+ comp.markAsDirtied();
256
+ }
257
+ });
258
+ if (isRoot) {
259
+ onUpdated(() => {
260
+ this.onViewUpdated.next();
261
+ });
262
+ isRoot = false;
263
+ }
264
+ onUpdated(() => {
265
+ const context = this.componentRendingStack[this.componentRendingStack.length - 1];
266
+ if (context === component) {
267
+ this.componentRendingStack.pop();
268
+ }
269
+ textbusComponent.changeMarker.rendered();
270
+ if (!this.componentRootElementCaches.get(textbusComponent)) {
271
+ // eslint-disable-next-line max-len
272
+ throw adapterError(`Component \`${textbusComponent.name}\` is not bound to rootRef, you must bind rootRef to the root element node of the component view.`);
273
+ }
274
+ });
275
+ const component = props.component;
276
+ const instance = viewFlyComponent(props);
277
+ if (typeof instance === 'function') {
278
+ return () => {
279
+ component.__slots__.forEach(i => this.renderedSlotCache.delete(i));
280
+ component.__slots__.length = 0;
281
+ this.componentRendingStack.push(component);
282
+ return instance();
283
+ };
284
+ }
285
+ const self = this;
286
+ return Object.assign(Object.assign({}, instance), { $render() {
287
+ component.__slots__.forEach(i => self.renderedSlotCache.delete(i));
288
+ component.__slots__.length = 0;
289
+ self.componentRendingStack.push(component);
290
+ return instance.$render();
291
+ } });
292
+ });
99
293
  });
100
- if (currentRef instanceof Ref) {
101
- jsxNode.props.ref = [currentRef, ref];
102
- }
103
- else if (Array.isArray(currentRef)) {
104
- currentRef.push(ref);
105
- }
106
- else {
107
- jsxNode.props.ref = ref;
108
- }
109
- slot.changeMarker.rendered();
110
- return jsxNode;
294
+ }
295
+ copy() {
296
+ document.execCommand('copy');
111
297
  }
112
298
  }
113
299
 
114
- export { Adapter };
300
+ export { ViewflyAdapter, ViewflyVDomAdapter };
package/bundles/index.js CHANGED
@@ -1,26 +1,110 @@
1
1
  'use strict';
2
2
 
3
- var core$1 = require('@viewfly/core');
4
- var stream = require('@tanbo/stream');
5
3
  var core = require('@textbus/core');
4
+ var core$1 = require('@viewfly/core');
6
5
  var platformBrowser = require('@textbus/platform-browser');
6
+ var platformBrowser$1 = require('@viewfly/platform-browser');
7
7
 
8
- const adapterError = core.makeError('ViewflyAdapter');
9
- /**
10
- * Textbus 桥接 [Viewfly](https://viewfly.org) 渲染能力适配器,用于在 Viewfly 项目中渲染 Textbus 数据
11
- */
12
- class Adapter extends platformBrowser.DomAdapter {
8
+ const adapterError$1 = core.makeError('ViewflyDOMRenderer');
9
+ class ViewflyAdapter extends platformBrowser.DomAdapter {
13
10
  constructor(components, mount) {
14
- super(mount);
15
- this.onViewUpdated = new stream.Subject();
11
+ super({
12
+ createCompositionNode(compositionState, updateNativeCompositionNode) {
13
+ const ref = core$1.createDynamicRef(node => {
14
+ updateNativeCompositionNode(node);
15
+ return () => {
16
+ updateNativeCompositionNode(null);
17
+ };
18
+ });
19
+ return new core.VElement('span', {
20
+ style: {
21
+ textDecoration: 'underline'
22
+ },
23
+ ref
24
+ }, [
25
+ new core.VTextNode(compositionState.text)
26
+ ]);
27
+ },
28
+ getParentNode(node) {
29
+ return node.parentNode;
30
+ },
31
+ getChildNodes(parentElement) {
32
+ return Array.from(parentElement.childNodes);
33
+ },
34
+ isNativeElementNode(node) {
35
+ return node instanceof HTMLElement;
36
+ },
37
+ getChildByIndex(parentElement, index) {
38
+ return parentElement.childNodes[index];
39
+ },
40
+ getAndUpdateSlotRootNativeElement(vEle, update) {
41
+ const currentRef = vEle.attrs.get('ref');
42
+ const ref = core$1.createDynamicRef(nativeNode => {
43
+ update(nativeNode);
44
+ return () => {
45
+ update(null);
46
+ };
47
+ });
48
+ if (currentRef instanceof core$1.DynamicRef) {
49
+ vEle.attrs.set('ref', [currentRef, ref]);
50
+ }
51
+ else if (Array.isArray(currentRef)) {
52
+ currentRef.push(ref);
53
+ }
54
+ else {
55
+ vEle.attrs.set('ref', ref);
56
+ }
57
+ },
58
+ componentRender: (component) => {
59
+ const comp = this.components[component.name] || this.components['*'];
60
+ if (comp) {
61
+ let ref = this.componentRefs.get(component);
62
+ if (!ref) {
63
+ ref = core$1.createDynamicRef(rootNode => {
64
+ this.componentRootElementCaches.set(component, rootNode);
65
+ return () => {
66
+ this.componentRootElementCaches.remove(component);
67
+ };
68
+ });
69
+ this.componentRefs.set(component, ref);
70
+ }
71
+ return core$1.jsx(comp, {
72
+ component,
73
+ rootRef: ref
74
+ }, component.id);
75
+ }
76
+ throw adapterError$1(`cannot found view component \`${component.name}\`!`);
77
+ },
78
+ vElementToViewElement(vNode, children) {
79
+ const key = vNode.attrs.get('key');
80
+ vNode.attrs.delete('key');
81
+ const props = Object.assign({}, (Array.from(vNode.attrs).reduce((a, b) => {
82
+ a[b[0]] = b[1];
83
+ return a;
84
+ }, {})));
85
+ if (vNode.classes.size) {
86
+ props.class = Array.from(vNode.classes).join(' ');
87
+ }
88
+ if (vNode.styles) {
89
+ props.style = Array.from(vNode.styles).reduce((a, b) => {
90
+ a[b[0]] = b[1];
91
+ return a;
92
+ }, {});
93
+ }
94
+ if (children.length) {
95
+ props.children = children;
96
+ }
97
+ return core$1.jsx(vNode.tagName, props, key);
98
+ }
99
+ }, mount);
16
100
  this.components = {};
17
101
  this.componentRefs = new WeakMap();
18
102
  let isRoot = true;
19
- Object.keys(components).forEach(key => {
20
- this.components[key] = (props) => {
21
- const comp = core$1.provide([]);
103
+ Object.entries(components).forEach(([key, viewFlyComponent]) => {
104
+ this.components[key] = core$1.withAnnotation(Object.assign({}, viewFlyComponent.annotation), (props) => {
105
+ const comp = core$1.getCurrentInstance();
22
106
  const textbusComponent = props.component;
23
- textbusComponent.changeMarker.onChange.subscribe(() => {
107
+ core.merge(textbusComponent.changeMarker.onChange, textbusComponent.changeMarker.onForceChange).subscribe(() => {
24
108
  if (textbusComponent.changeMarker.dirty) {
25
109
  comp.markAsDirtied();
26
110
  }
@@ -32,85 +116,188 @@ class Adapter extends platformBrowser.DomAdapter {
32
116
  isRoot = false;
33
117
  }
34
118
  core$1.onUpdated(() => {
119
+ const context = this.componentRendingStack[this.componentRendingStack.length - 1];
120
+ if (context === component) {
121
+ this.componentRendingStack.pop();
122
+ }
35
123
  textbusComponent.changeMarker.rendered();
124
+ if (!this.componentRootElementCaches.get(textbusComponent)) {
125
+ // eslint-disable-next-line max-len
126
+ throw adapterError$1(`Component \`${textbusComponent.name}\` is not bound to rootRef, you must bind rootRef to the root element node of the component view.`);
127
+ }
36
128
  });
37
- return components[key](props);
38
- };
129
+ const component = props.component;
130
+ const instance = viewFlyComponent(props);
131
+ if (typeof instance === 'function') {
132
+ return () => {
133
+ component.__slots__.forEach(i => this.renderedSlotCache.delete(i));
134
+ component.__slots__.length = 0;
135
+ this.componentRendingStack.push(component);
136
+ return instance();
137
+ };
138
+ }
139
+ const self = this;
140
+ return Object.assign(Object.assign({}, instance), { $render() {
141
+ component.__slots__.forEach(i => self.renderedSlotCache.delete(i));
142
+ component.__slots__.length = 0;
143
+ self.componentRendingStack.push(component);
144
+ return instance.$render();
145
+ } });
146
+ });
39
147
  });
40
148
  }
41
- componentRender(component) {
42
- const comp = this.components[component.name] || this.components['*'];
43
- if (comp) {
44
- component.changeMarker.rendered();
45
- let ref = this.componentRefs.get(component);
46
- if (!ref) {
47
- ref = core$1.useRef(rootNode => {
48
- this.componentRootElementCaches.set(component, rootNode);
149
+ copy() {
150
+ document.execCommand('copy');
151
+ }
152
+ }
153
+
154
+ const adapterError = core.makeError('ViewflyHTMLRenderer');
155
+ class ViewflyVDomAdapter extends core.Adapter {
156
+ constructor(components, mount) {
157
+ super({
158
+ createCompositionNode(compositionState, updateNativeCompositionNode) {
159
+ const ref = core$1.createDynamicRef(node => {
160
+ updateNativeCompositionNode(node);
49
161
  return () => {
50
- this.componentRootElementCaches.remove(component);
162
+ updateNativeCompositionNode(null);
51
163
  };
52
164
  });
53
- this.componentRefs.set(component, ref);
54
- }
55
- return core$1.jsx(comp, {
56
- component,
57
- rootRef: ref
58
- }, component.id);
59
- }
60
- throw adapterError(`cannot found view component \`${component.name}\`!`);
61
- }
62
- slotRender(slot, slotHostRender, renderEnv) {
63
- const vElement = slot.toTree(slotHostRender, renderEnv);
64
- this.slotRootVElementCaches.set(slot, vElement);
65
- const vNodeToJSX = (vNode) => {
66
- const children = [];
67
- for (let i = 0; i < vNode.children.length; i++) {
68
- const child = vNode.children[i];
69
- if (child instanceof core.VElement) {
70
- children.push(vNodeToJSX(child));
71
- }
72
- else if (child instanceof core.VTextNode) {
73
- children.push(core.replaceEmpty(child.textContent));
165
+ return new core.VElement('span', {
166
+ style: {
167
+ textDecoration: 'underline'
168
+ },
169
+ ref
170
+ }, [
171
+ new core.VTextNode(compositionState.text)
172
+ ]);
173
+ },
174
+ getParentNode(node) {
175
+ return node.parent;
176
+ },
177
+ getChildNodes(parentElement) {
178
+ return parentElement.children.slice();
179
+ },
180
+ isNativeElementNode(node) {
181
+ return node instanceof platformBrowser$1.VDOMElement;
182
+ },
183
+ getChildByIndex(parentElement, index) {
184
+ return parentElement.children[index];
185
+ },
186
+ getAndUpdateSlotRootNativeElement(vEle, update) {
187
+ const currentRef = vEle.attrs.get('ref');
188
+ const ref = core$1.createDynamicRef(nativeNode => {
189
+ update(nativeNode);
190
+ return () => {
191
+ update(null);
192
+ };
193
+ });
194
+ if (currentRef instanceof core$1.DynamicRef) {
195
+ vEle.attrs.set('ref', [currentRef, ref]);
196
+ }
197
+ else if (Array.isArray(currentRef)) {
198
+ currentRef.push(ref);
74
199
  }
75
200
  else {
76
- children.push(this.componentRender(child));
201
+ vEle.attrs.set('ref', ref);
77
202
  }
78
- }
79
- const props = Object.assign({}, (Array.from(vNode.attrs).reduce((a, b) => {
80
- a[b[0]] = b[1];
81
- return a;
82
- }, {})));
83
- if (vNode.classes.size) {
84
- props.class = Array.from(vNode.classes).join(' ');
85
- }
86
- if (vNode.styles) {
87
- props.style = Array.from(vNode.styles).reduce((a, b) => {
203
+ },
204
+ componentRender: (component) => {
205
+ const comp = this.components[component.name] || this.components['*'];
206
+ if (comp) {
207
+ let ref = this.componentRefs.get(component);
208
+ if (!ref) {
209
+ ref = core$1.createDynamicRef(rootNode => {
210
+ this.componentRootElementCaches.set(component, rootNode);
211
+ return () => {
212
+ this.componentRootElementCaches.remove(component);
213
+ };
214
+ });
215
+ this.componentRefs.set(component, ref);
216
+ }
217
+ return core$1.jsx(comp, {
218
+ component,
219
+ rootRef: ref
220
+ }, component.id);
221
+ }
222
+ throw adapterError(`cannot found view component \`${component.name}\`!`);
223
+ },
224
+ vElementToViewElement(vNode, children) {
225
+ const key = vNode.attrs.get('key');
226
+ vNode.attrs.delete('key');
227
+ const props = Object.assign({}, (Array.from(vNode.attrs).reduce((a, b) => {
88
228
  a[b[0]] = b[1];
89
229
  return a;
90
- }, {});
91
- }
92
- if (children.length) {
93
- props.children = children;
230
+ }, {})));
231
+ if (vNode.classes.size) {
232
+ props.class = Array.from(vNode.classes).join(' ');
233
+ }
234
+ if (vNode.styles) {
235
+ props.style = Array.from(vNode.styles).reduce((a, b) => {
236
+ a[b[0]] = b[1];
237
+ return a;
238
+ }, {});
239
+ }
240
+ if (children.length) {
241
+ props.children = children;
242
+ }
243
+ return core$1.jsx(vNode.tagName, props, key);
94
244
  }
95
- return core$1.jsx(vNode.tagName, props);
96
- };
97
- const jsxNode = vNodeToJSX(vElement);
98
- const currentRef = jsxNode.props.ref;
99
- const ref = core$1.useRef(nativeNode => {
100
- this.slotRootNativeElementCaches.set(slot, nativeNode);
245
+ }, mount);
246
+ this.onViewUpdated = new core.Subject();
247
+ this.host = new platformBrowser$1.VDOMElement('body');
248
+ this.components = {};
249
+ this.componentRefs = new WeakMap();
250
+ let isRoot = true;
251
+ Object.entries(components).forEach(([key, viewFlyComponent]) => {
252
+ this.components[key] = core$1.withAnnotation(Object.assign({}, viewFlyComponent.annotation), (props) => {
253
+ const comp = core$1.getCurrentInstance();
254
+ const textbusComponent = props.component;
255
+ core.merge(textbusComponent.changeMarker.onChange, textbusComponent.changeMarker.onForceChange).subscribe(() => {
256
+ if (textbusComponent.changeMarker.dirty) {
257
+ comp.markAsDirtied();
258
+ }
259
+ });
260
+ if (isRoot) {
261
+ core$1.onUpdated(() => {
262
+ this.onViewUpdated.next();
263
+ });
264
+ isRoot = false;
265
+ }
266
+ core$1.onUpdated(() => {
267
+ const context = this.componentRendingStack[this.componentRendingStack.length - 1];
268
+ if (context === component) {
269
+ this.componentRendingStack.pop();
270
+ }
271
+ textbusComponent.changeMarker.rendered();
272
+ if (!this.componentRootElementCaches.get(textbusComponent)) {
273
+ // eslint-disable-next-line max-len
274
+ throw adapterError(`Component \`${textbusComponent.name}\` is not bound to rootRef, you must bind rootRef to the root element node of the component view.`);
275
+ }
276
+ });
277
+ const component = props.component;
278
+ const instance = viewFlyComponent(props);
279
+ if (typeof instance === 'function') {
280
+ return () => {
281
+ component.__slots__.forEach(i => this.renderedSlotCache.delete(i));
282
+ component.__slots__.length = 0;
283
+ this.componentRendingStack.push(component);
284
+ return instance();
285
+ };
286
+ }
287
+ const self = this;
288
+ return Object.assign(Object.assign({}, instance), { $render() {
289
+ component.__slots__.forEach(i => self.renderedSlotCache.delete(i));
290
+ component.__slots__.length = 0;
291
+ self.componentRendingStack.push(component);
292
+ return instance.$render();
293
+ } });
294
+ });
101
295
  });
102
- if (currentRef instanceof core$1.Ref) {
103
- jsxNode.props.ref = [currentRef, ref];
104
- }
105
- else if (Array.isArray(currentRef)) {
106
- currentRef.push(ref);
107
- }
108
- else {
109
- jsxNode.props.ref = ref;
110
- }
111
- slot.changeMarker.rendered();
112
- return jsxNode;
296
+ }
297
+ copy() {
298
+ document.execCommand('copy');
113
299
  }
114
300
  }
115
301
 
116
- exports.Adapter = Adapter;
302
+ exports.ViewflyAdapter = ViewflyAdapter;
303
+ exports.ViewflyVDomAdapter = ViewflyVDomAdapter;
@@ -1 +1,2 @@
1
- export * from './adapter';
1
+ export * from './viewfly-adapter';
2
+ export * from './viewfly-v-dom-adapter';
@@ -0,0 +1,16 @@
1
+ import { Component, ViewMount } from '@textbus/core';
2
+ import { DynamicRef } from '@viewfly/core';
3
+ import { DomAdapter } from '@textbus/platform-browser';
4
+ export interface ViewComponentProps<T extends Component> {
5
+ component: T;
6
+ rootRef: DynamicRef<HTMLElement>;
7
+ }
8
+ export interface ViewflyAdapterComponents {
9
+ [key: string]: JSXInternal.ComponentSetup<ViewComponentProps<any>>;
10
+ }
11
+ export declare class ViewflyAdapter extends DomAdapter<JSXInternal.ViewNode, JSXInternal.Element> {
12
+ private components;
13
+ private componentRefs;
14
+ constructor(components: ViewflyAdapterComponents, mount: ViewMount<JSXInternal.ViewNode, HTMLElement>);
15
+ copy(): void;
16
+ }
@@ -0,0 +1,18 @@
1
+ import { Component, ViewMount, Adapter, Subject } from '@textbus/core';
2
+ import { VDOMElement, VDOMText } from '@viewfly/platform-browser';
3
+ import { DynamicRef } from '@viewfly/core';
4
+ export interface ViewVDomComponentProps<T extends Component> {
5
+ component: T;
6
+ rootRef: DynamicRef<VDOMElement>;
7
+ }
8
+ export interface ViewflyVDomAdapterComponents {
9
+ [key: string]: JSXInternal.ComponentSetup<ViewVDomComponentProps<any>>;
10
+ }
11
+ export declare class ViewflyVDomAdapter extends Adapter<VDOMElement, VDOMText, JSXInternal.ViewNode, JSXInternal.Element> {
12
+ onViewUpdated: Subject<void>;
13
+ host: VDOMElement;
14
+ private components;
15
+ private componentRefs;
16
+ constructor(components: ViewflyVDomAdapterComponents, mount: ViewMount<JSXInternal.ViewNode, VDOMElement>);
17
+ copy(): void;
18
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@textbus/adapter-viewfly",
3
- "version": "4.0.0-alpha.5",
3
+ "version": "4.0.0-alpha.51",
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": "./bundles/index.js",
6
6
  "module": "./bundles/index.esm.js",
@@ -25,10 +25,11 @@
25
25
  "typescript editor"
26
26
  ],
27
27
  "dependencies": {
28
- "@tanbo/stream": "^1.2.0",
29
- "@textbus/core": "^4.0.0-alpha.5",
30
- "@textbus/platform-browser": "^4.0.0-alpha.5",
31
- "@viewfly/core": "^0.2.4"
28
+ "@tanbo/stream": "^1.2.3",
29
+ "@textbus/core": "^4.0.0-alpha.50",
30
+ "@textbus/platform-browser": "^4.0.0-alpha.51",
31
+ "@viewfly/core": "^1.0.0-alpha.4",
32
+ "@viewfly/platform-browser": "^1.0.0-alpha.4"
32
33
  },
33
34
  "devDependencies": {
34
35
  "@rollup/plugin-commonjs": "^23.0.2",
@@ -48,5 +49,5 @@
48
49
  "bugs": {
49
50
  "url": "https://github.com/textbus/textbus.git/issues"
50
51
  },
51
- "gitHead": "072831527fe07e7c21a14f3141d55b24d321a690"
52
+ "gitHead": "0b54e3b23238b09096449e8fccf0127820f8e206"
52
53
  }
@@ -1,22 +0,0 @@
1
- import { JSXComponent, JSXInternal, Ref } from '@viewfly/core';
2
- import { Subject } from '@tanbo/stream';
3
- import { Component, ComponentInstance, ExtractComponentInstanceType, Slot, VElement, VTextNode } from '@textbus/core';
4
- import { DomAdapter } from '@textbus/platform-browser';
5
- export interface ViewComponentProps<T extends Component = Component> {
6
- component: ExtractComponentInstanceType<T>;
7
- rootRef: Ref<HTMLElement>;
8
- }
9
- export interface ViewflyAdapterComponents {
10
- [key: string]: JSXInternal.ComponentSetup<ViewComponentProps>;
11
- }
12
- /**
13
- * Textbus 桥接 [Viewfly](https://viewfly.org) 渲染能力适配器,用于在 Viewfly 项目中渲染 Textbus 数据
14
- */
15
- export declare class Adapter extends DomAdapter<JSXComponent, JSXInternal.Element> {
16
- onViewUpdated: Subject<void>;
17
- private components;
18
- private componentRefs;
19
- constructor(components: ViewflyAdapterComponents, mount: (host: HTMLElement, root: JSXComponent) => (void | (() => void)));
20
- componentRender(component: ComponentInstance): JSXInternal.JSXNode;
21
- slotRender(slot: Slot, slotHostRender: (children: Array<VElement | VTextNode | ComponentInstance>) => VElement, renderEnv: any): JSXInternal.Element;
22
- }