@textbus/adapter-vue 4.0.0-alpha.8 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bundles/index.esm.js +92 -87
- package/bundles/index.js +91 -86
- package/bundles/public-api.d.ts +1 -1
- package/bundles/vue-adapter.d.ts +16 -0
- package/package.json +6 -6
- package/bundles/adapter.d.ts +0 -22
package/bundles/index.esm.js
CHANGED
@@ -1,23 +1,94 @@
|
|
1
|
-
import {
|
2
|
-
import {
|
3
|
-
import { makeError, VElement, VTextNode, replaceEmpty } from '@textbus/core';
|
1
|
+
import { makeError, VElement, VTextNode } from '@textbus/core';
|
2
|
+
import { ref, h, getCurrentInstance, onMounted, onUpdated, onUnmounted } from 'vue';
|
4
3
|
import { DomAdapter } from '@textbus/platform-browser';
|
5
4
|
|
6
5
|
const adapterError = makeError('VueAdapter');
|
7
|
-
|
8
|
-
* Textbus 桥接 Vue 渲染能力适配器,用于在 Vue 项目中渲染 Textbus 数据
|
9
|
-
*/
|
10
|
-
class Adapter extends DomAdapter {
|
6
|
+
class VueAdapter extends DomAdapter {
|
11
7
|
constructor(components, mount) {
|
12
|
-
super(
|
13
|
-
|
14
|
-
|
8
|
+
super({
|
9
|
+
createCompositionNode: (compositionState, updateNativeCompositionNode) => {
|
10
|
+
return new VElement('span', {
|
11
|
+
style: {
|
12
|
+
textDecoration: 'underline'
|
13
|
+
},
|
14
|
+
ref: updateNativeCompositionNode
|
15
|
+
}, [
|
16
|
+
new VTextNode(compositionState.text)
|
17
|
+
]);
|
18
|
+
},
|
19
|
+
getParentNode(node) {
|
20
|
+
return node.parentNode;
|
21
|
+
},
|
22
|
+
getChildNodes(parentElement) {
|
23
|
+
return Array.from(parentElement.childNodes);
|
24
|
+
},
|
25
|
+
isNativeElementNode(node) {
|
26
|
+
return node instanceof HTMLElement;
|
27
|
+
},
|
28
|
+
getChildByIndex(parentElement, index) {
|
29
|
+
return parentElement.childNodes[index];
|
30
|
+
},
|
31
|
+
getAndUpdateSlotRootNativeElement(vElement, update) {
|
32
|
+
const currentRef = vElement.attrs.get('ref');
|
33
|
+
if (currentRef) {
|
34
|
+
vElement.attrs.set('ref', (v) => {
|
35
|
+
update(v);
|
36
|
+
if (typeof currentRef === 'function') {
|
37
|
+
currentRef(v);
|
38
|
+
}
|
39
|
+
else if (!currentRef.value) {
|
40
|
+
currentRef.value = v;
|
41
|
+
}
|
42
|
+
});
|
43
|
+
}
|
44
|
+
else {
|
45
|
+
vElement.attrs.set('ref', update);
|
46
|
+
}
|
47
|
+
},
|
48
|
+
componentRender: (component) => {
|
49
|
+
const comp = this.components[component.name] || this.components['*'];
|
50
|
+
if (comp) {
|
51
|
+
let rootRef = this.componentRefs.get(component);
|
52
|
+
if (!rootRef) {
|
53
|
+
rootRef = ref();
|
54
|
+
this.componentRefs.set(component, rootRef);
|
55
|
+
}
|
56
|
+
return h(comp, {
|
57
|
+
component,
|
58
|
+
rootRef,
|
59
|
+
key: component.id
|
60
|
+
});
|
61
|
+
}
|
62
|
+
throw adapterError(`cannot found view component \`${component.name}\`!`);
|
63
|
+
},
|
64
|
+
vElementToViewElement(vNode, children) {
|
65
|
+
const props = Object.assign({}, (Array.from(vNode.attrs).reduce((a, b) => {
|
66
|
+
a[b[0]] = b[1];
|
67
|
+
return a;
|
68
|
+
}, {})));
|
69
|
+
if (vNode.classes.size) {
|
70
|
+
props.class = Array.from(vNode.classes).join(' ');
|
71
|
+
}
|
72
|
+
if (vNode.styles) {
|
73
|
+
props.style = Array.from(vNode.styles).reduce((a, b) => {
|
74
|
+
a[b[0]] = b[1];
|
75
|
+
return a;
|
76
|
+
}, {});
|
77
|
+
}
|
78
|
+
return h(vNode.tagName, props, ...children);
|
79
|
+
}
|
80
|
+
}, mount);
|
81
|
+
// private compositionRef = ref<HTMLElement>()
|
15
82
|
this.componentRefs = new WeakMap();
|
83
|
+
this.components = {};
|
84
|
+
// watchEffect(() => {
|
85
|
+
// this.compositionNode = this.compositionRef.value || null
|
86
|
+
// })
|
16
87
|
Object.keys(components).forEach(key => {
|
17
88
|
const vueComponent = components[key];
|
18
89
|
const setup = vueComponent.setup;
|
19
90
|
const self = this;
|
20
|
-
vueComponent.setup = function (props) {
|
91
|
+
vueComponent.setup = function (props, context, ...args) {
|
21
92
|
const component = props.component;
|
22
93
|
const vueInstance = getCurrentInstance();
|
23
94
|
const sub = component.changeMarker.onChange.subscribe(() => {
|
@@ -34,91 +105,25 @@ class Adapter extends DomAdapter {
|
|
34
105
|
}
|
35
106
|
});
|
36
107
|
onUpdated(() => {
|
108
|
+
var _a;
|
109
|
+
component.changeMarker.rendered();
|
37
110
|
self.onViewUpdated.next();
|
111
|
+
if (!(((_a = self.componentRefs.get(component)) === null || _a === void 0 ? void 0 : _a.value) instanceof HTMLElement)) {
|
112
|
+
// eslint-disable-next-line max-len
|
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
|
+
}
|
38
115
|
});
|
39
116
|
onUnmounted(() => {
|
40
117
|
sub.unsubscribe();
|
41
118
|
});
|
42
|
-
return setup(props);
|
119
|
+
return setup(props, context, ...args);
|
43
120
|
};
|
44
121
|
this.components[key] = vueComponent;
|
45
122
|
});
|
46
123
|
}
|
47
|
-
|
48
|
-
|
49
|
-
if (comp) {
|
50
|
-
component.changeMarker.rendered();
|
51
|
-
let rootRef = this.componentRefs.get(component);
|
52
|
-
if (!rootRef) {
|
53
|
-
rootRef = ref();
|
54
|
-
this.componentRefs.set(component, rootRef);
|
55
|
-
}
|
56
|
-
return h(comp, {
|
57
|
-
component,
|
58
|
-
rootRef,
|
59
|
-
key: component.id
|
60
|
-
});
|
61
|
-
}
|
62
|
-
throw adapterError(`cannot found view component \`${component.name}\`!`);
|
63
|
-
}
|
64
|
-
slotRender(slot, slotHostRender, renderEnv) {
|
65
|
-
const vElement = slot.toTree(slotHostRender, renderEnv);
|
66
|
-
this.slotRootVElementCaches.set(slot, vElement);
|
67
|
-
const vNodeToJSX = (vNode) => {
|
68
|
-
const children = [];
|
69
|
-
for (let i = 0; i < vNode.children.length; i++) {
|
70
|
-
const child = vNode.children[i];
|
71
|
-
if (child instanceof VElement) {
|
72
|
-
children.push(vNodeToJSX(child));
|
73
|
-
}
|
74
|
-
else if (child instanceof VTextNode) {
|
75
|
-
children.push(replaceEmpty(child.textContent));
|
76
|
-
}
|
77
|
-
else {
|
78
|
-
children.push(this.componentRender(child));
|
79
|
-
}
|
80
|
-
}
|
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
|
-
return h(vNode.tagName, props, ...children);
|
95
|
-
};
|
96
|
-
const refFn = (nativeNode) => {
|
97
|
-
if (!nativeNode) {
|
98
|
-
this.slotRootNativeElementCaches.remove(nativeNode);
|
99
|
-
}
|
100
|
-
else {
|
101
|
-
this.slotRootNativeElementCaches.set(slot, nativeNode);
|
102
|
-
}
|
103
|
-
};
|
104
|
-
const currentRef = vElement.attrs.get('ref');
|
105
|
-
if (currentRef) {
|
106
|
-
vElement.attrs.set('ref', (v) => {
|
107
|
-
refFn(v);
|
108
|
-
if (typeof currentRef === 'function') {
|
109
|
-
currentRef(v);
|
110
|
-
}
|
111
|
-
else if (!currentRef.value) {
|
112
|
-
currentRef.value = v;
|
113
|
-
}
|
114
|
-
});
|
115
|
-
}
|
116
|
-
else {
|
117
|
-
vElement.attrs.set('ref', refFn);
|
118
|
-
}
|
119
|
-
slot.changeMarker.rendered();
|
120
|
-
return vNodeToJSX(vElement);
|
124
|
+
copy() {
|
125
|
+
document.execCommand('copy');
|
121
126
|
}
|
122
127
|
}
|
123
128
|
|
124
|
-
export {
|
129
|
+
export { VueAdapter };
|
package/bundles/index.js
CHANGED
@@ -1,25 +1,96 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
-
var vue = require('vue');
|
4
|
-
var stream = require('@tanbo/stream');
|
5
3
|
var core = require('@textbus/core');
|
4
|
+
var vue = require('vue');
|
6
5
|
var platformBrowser = require('@textbus/platform-browser');
|
7
6
|
|
8
7
|
const adapterError = core.makeError('VueAdapter');
|
9
|
-
|
10
|
-
* Textbus 桥接 Vue 渲染能力适配器,用于在 Vue 项目中渲染 Textbus 数据
|
11
|
-
*/
|
12
|
-
class Adapter extends platformBrowser.DomAdapter {
|
8
|
+
class VueAdapter extends platformBrowser.DomAdapter {
|
13
9
|
constructor(components, mount) {
|
14
|
-
super(
|
15
|
-
|
16
|
-
|
10
|
+
super({
|
11
|
+
createCompositionNode: (compositionState, updateNativeCompositionNode) => {
|
12
|
+
return new core.VElement('span', {
|
13
|
+
style: {
|
14
|
+
textDecoration: 'underline'
|
15
|
+
},
|
16
|
+
ref: updateNativeCompositionNode
|
17
|
+
}, [
|
18
|
+
new core.VTextNode(compositionState.text)
|
19
|
+
]);
|
20
|
+
},
|
21
|
+
getParentNode(node) {
|
22
|
+
return node.parentNode;
|
23
|
+
},
|
24
|
+
getChildNodes(parentElement) {
|
25
|
+
return Array.from(parentElement.childNodes);
|
26
|
+
},
|
27
|
+
isNativeElementNode(node) {
|
28
|
+
return node instanceof HTMLElement;
|
29
|
+
},
|
30
|
+
getChildByIndex(parentElement, index) {
|
31
|
+
return parentElement.childNodes[index];
|
32
|
+
},
|
33
|
+
getAndUpdateSlotRootNativeElement(vElement, update) {
|
34
|
+
const currentRef = vElement.attrs.get('ref');
|
35
|
+
if (currentRef) {
|
36
|
+
vElement.attrs.set('ref', (v) => {
|
37
|
+
update(v);
|
38
|
+
if (typeof currentRef === 'function') {
|
39
|
+
currentRef(v);
|
40
|
+
}
|
41
|
+
else if (!currentRef.value) {
|
42
|
+
currentRef.value = v;
|
43
|
+
}
|
44
|
+
});
|
45
|
+
}
|
46
|
+
else {
|
47
|
+
vElement.attrs.set('ref', update);
|
48
|
+
}
|
49
|
+
},
|
50
|
+
componentRender: (component) => {
|
51
|
+
const comp = this.components[component.name] || this.components['*'];
|
52
|
+
if (comp) {
|
53
|
+
let rootRef = this.componentRefs.get(component);
|
54
|
+
if (!rootRef) {
|
55
|
+
rootRef = vue.ref();
|
56
|
+
this.componentRefs.set(component, rootRef);
|
57
|
+
}
|
58
|
+
return vue.h(comp, {
|
59
|
+
component,
|
60
|
+
rootRef,
|
61
|
+
key: component.id
|
62
|
+
});
|
63
|
+
}
|
64
|
+
throw adapterError(`cannot found view component \`${component.name}\`!`);
|
65
|
+
},
|
66
|
+
vElementToViewElement(vNode, children) {
|
67
|
+
const props = Object.assign({}, (Array.from(vNode.attrs).reduce((a, b) => {
|
68
|
+
a[b[0]] = b[1];
|
69
|
+
return a;
|
70
|
+
}, {})));
|
71
|
+
if (vNode.classes.size) {
|
72
|
+
props.class = Array.from(vNode.classes).join(' ');
|
73
|
+
}
|
74
|
+
if (vNode.styles) {
|
75
|
+
props.style = Array.from(vNode.styles).reduce((a, b) => {
|
76
|
+
a[b[0]] = b[1];
|
77
|
+
return a;
|
78
|
+
}, {});
|
79
|
+
}
|
80
|
+
return vue.h(vNode.tagName, props, ...children);
|
81
|
+
}
|
82
|
+
}, mount);
|
83
|
+
// private compositionRef = ref<HTMLElement>()
|
17
84
|
this.componentRefs = new WeakMap();
|
85
|
+
this.components = {};
|
86
|
+
// watchEffect(() => {
|
87
|
+
// this.compositionNode = this.compositionRef.value || null
|
88
|
+
// })
|
18
89
|
Object.keys(components).forEach(key => {
|
19
90
|
const vueComponent = components[key];
|
20
91
|
const setup = vueComponent.setup;
|
21
92
|
const self = this;
|
22
|
-
vueComponent.setup = function (props) {
|
93
|
+
vueComponent.setup = function (props, context, ...args) {
|
23
94
|
const component = props.component;
|
24
95
|
const vueInstance = vue.getCurrentInstance();
|
25
96
|
const sub = component.changeMarker.onChange.subscribe(() => {
|
@@ -36,91 +107,25 @@ class Adapter extends platformBrowser.DomAdapter {
|
|
36
107
|
}
|
37
108
|
});
|
38
109
|
vue.onUpdated(() => {
|
110
|
+
var _a;
|
111
|
+
component.changeMarker.rendered();
|
39
112
|
self.onViewUpdated.next();
|
113
|
+
if (!(((_a = self.componentRefs.get(component)) === null || _a === void 0 ? void 0 : _a.value) instanceof HTMLElement)) {
|
114
|
+
// eslint-disable-next-line max-len
|
115
|
+
throw adapterError(`Component \`${component.name}\` is not bound to rootRef, you must bind rootRef to the root element node of the component view.`);
|
116
|
+
}
|
40
117
|
});
|
41
118
|
vue.onUnmounted(() => {
|
42
119
|
sub.unsubscribe();
|
43
120
|
});
|
44
|
-
return setup(props);
|
121
|
+
return setup(props, context, ...args);
|
45
122
|
};
|
46
123
|
this.components[key] = vueComponent;
|
47
124
|
});
|
48
125
|
}
|
49
|
-
|
50
|
-
|
51
|
-
if (comp) {
|
52
|
-
component.changeMarker.rendered();
|
53
|
-
let rootRef = this.componentRefs.get(component);
|
54
|
-
if (!rootRef) {
|
55
|
-
rootRef = vue.ref();
|
56
|
-
this.componentRefs.set(component, rootRef);
|
57
|
-
}
|
58
|
-
return vue.h(comp, {
|
59
|
-
component,
|
60
|
-
rootRef,
|
61
|
-
key: component.id
|
62
|
-
});
|
63
|
-
}
|
64
|
-
throw adapterError(`cannot found view component \`${component.name}\`!`);
|
65
|
-
}
|
66
|
-
slotRender(slot, slotHostRender, renderEnv) {
|
67
|
-
const vElement = slot.toTree(slotHostRender, renderEnv);
|
68
|
-
this.slotRootVElementCaches.set(slot, vElement);
|
69
|
-
const vNodeToJSX = (vNode) => {
|
70
|
-
const children = [];
|
71
|
-
for (let i = 0; i < vNode.children.length; i++) {
|
72
|
-
const child = vNode.children[i];
|
73
|
-
if (child instanceof core.VElement) {
|
74
|
-
children.push(vNodeToJSX(child));
|
75
|
-
}
|
76
|
-
else if (child instanceof core.VTextNode) {
|
77
|
-
children.push(core.replaceEmpty(child.textContent));
|
78
|
-
}
|
79
|
-
else {
|
80
|
-
children.push(this.componentRender(child));
|
81
|
-
}
|
82
|
-
}
|
83
|
-
const props = Object.assign({}, (Array.from(vNode.attrs).reduce((a, b) => {
|
84
|
-
a[b[0]] = b[1];
|
85
|
-
return a;
|
86
|
-
}, {})));
|
87
|
-
if (vNode.classes.size) {
|
88
|
-
props.class = Array.from(vNode.classes).join(' ');
|
89
|
-
}
|
90
|
-
if (vNode.styles) {
|
91
|
-
props.style = Array.from(vNode.styles).reduce((a, b) => {
|
92
|
-
a[b[0]] = b[1];
|
93
|
-
return a;
|
94
|
-
}, {});
|
95
|
-
}
|
96
|
-
return vue.h(vNode.tagName, props, ...children);
|
97
|
-
};
|
98
|
-
const refFn = (nativeNode) => {
|
99
|
-
if (!nativeNode) {
|
100
|
-
this.slotRootNativeElementCaches.remove(nativeNode);
|
101
|
-
}
|
102
|
-
else {
|
103
|
-
this.slotRootNativeElementCaches.set(slot, nativeNode);
|
104
|
-
}
|
105
|
-
};
|
106
|
-
const currentRef = vElement.attrs.get('ref');
|
107
|
-
if (currentRef) {
|
108
|
-
vElement.attrs.set('ref', (v) => {
|
109
|
-
refFn(v);
|
110
|
-
if (typeof currentRef === 'function') {
|
111
|
-
currentRef(v);
|
112
|
-
}
|
113
|
-
else if (!currentRef.value) {
|
114
|
-
currentRef.value = v;
|
115
|
-
}
|
116
|
-
});
|
117
|
-
}
|
118
|
-
else {
|
119
|
-
vElement.attrs.set('ref', refFn);
|
120
|
-
}
|
121
|
-
slot.changeMarker.rendered();
|
122
|
-
return vNodeToJSX(vElement);
|
126
|
+
copy() {
|
127
|
+
document.execCommand('copy');
|
123
128
|
}
|
124
129
|
}
|
125
130
|
|
126
|
-
exports.
|
131
|
+
exports.VueAdapter = VueAdapter;
|
package/bundles/public-api.d.ts
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export * from './adapter';
|
1
|
+
export * from './vue-adapter';
|
@@ -0,0 +1,16 @@
|
|
1
|
+
import { Component, ViewMount } from '@textbus/core';
|
2
|
+
import { DefineComponent, Ref, VNode } from 'vue';
|
3
|
+
import { DomAdapter } from '@textbus/platform-browser';
|
4
|
+
export interface ViewComponentProps<T extends Component> {
|
5
|
+
component: T;
|
6
|
+
rootRef: Ref<HTMLElement | undefined>;
|
7
|
+
}
|
8
|
+
export interface VueAdapterComponents {
|
9
|
+
[key: string]: DefineComponent<ViewComponentProps<any>>;
|
10
|
+
}
|
11
|
+
export declare class VueAdapter extends DomAdapter<VNode, VNode> {
|
12
|
+
private componentRefs;
|
13
|
+
private components;
|
14
|
+
constructor(components: VueAdapterComponents, mount: ViewMount<VNode, HTMLElement>);
|
15
|
+
copy(): void;
|
16
|
+
}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@textbus/adapter-vue",
|
3
|
-
"version": "4.0.0
|
3
|
+
"version": "4.0.0",
|
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,10 @@
|
|
25
25
|
"typescript editor"
|
26
26
|
],
|
27
27
|
"dependencies": {
|
28
|
-
"@tanbo/stream": "^1.2.
|
29
|
-
"@textbus/core": "^4.0.0
|
30
|
-
"@textbus/platform-browser": "^4.0.0
|
31
|
-
"vue": "^3.
|
28
|
+
"@tanbo/stream": "^1.2.5",
|
29
|
+
"@textbus/core": "^4.0.0",
|
30
|
+
"@textbus/platform-browser": "^4.0.0",
|
31
|
+
"vue": "^3.4.28"
|
32
32
|
},
|
33
33
|
"devDependencies": {
|
34
34
|
"@rollup/plugin-commonjs": "^23.0.2",
|
@@ -48,5 +48,5 @@
|
|
48
48
|
"bugs": {
|
49
49
|
"url": "https://github.com/textbus/textbus.git/issues"
|
50
50
|
},
|
51
|
-
"gitHead": "
|
51
|
+
"gitHead": "cf4fd289b73bc777124a32fe42bb58eba05a34f1"
|
52
52
|
}
|
package/bundles/adapter.d.ts
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
import { DefineComponent, Ref, VNode } from 'vue';
|
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 | undefined>;
|
8
|
-
}
|
9
|
-
export interface ReactAdapterComponents {
|
10
|
-
[key: string]: DefineComponent<ViewComponentProps>;
|
11
|
-
}
|
12
|
-
/**
|
13
|
-
* Textbus 桥接 Vue 渲染能力适配器,用于在 Vue 项目中渲染 Textbus 数据
|
14
|
-
*/
|
15
|
-
export declare class Adapter extends DomAdapter<VNode, VNode> {
|
16
|
-
onViewUpdated: Subject<void>;
|
17
|
-
private components;
|
18
|
-
private componentRefs;
|
19
|
-
constructor(components: ReactAdapterComponents, mount: (host: HTMLElement, root: VNode) => (void | (() => void)));
|
20
|
-
componentRender(component: ComponentInstance): VNode;
|
21
|
-
slotRender(slot: Slot, slotHostRender: (children: Array<VElement | VTextNode | ComponentInstance>) => VElement, renderEnv?: any): VNode;
|
22
|
-
}
|