@textbus/adapter-vue 5.2.2 → 5.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -1,94 +1,139 @@
1
- import { makeError as g, VElement as y, VTextNode as R, Adapter as C } from "@textbus/core";
2
- import { h as i, ref as w, getCurrentInstance as E, onMounted as x, onUpdated as v, onUnmounted as A } from "vue";
3
- import { DomAdapter as m } from "@textbus/platform-browser";
4
- import { ReflectiveInjector as V } from "@viewfly/core";
5
- import { merge as k } from "@tanbo/stream";
6
- const u = g("VueAdapter");
7
- class f extends m {
1
+ import { makeError, VElement, VTextNode, Adapter } from "@textbus/core";
2
+ import { h, ref, getCurrentInstance, onMounted, onUpdated, onUnmounted } from "vue";
3
+ import { DomAdapter } from "@textbus/platform-browser";
4
+ import { ReflectiveInjector } from "@viewfly/core";
5
+ import { merge } from "@tanbo/stream";
6
+ const adapterError = makeError("VueAdapter");
7
+ class VueAdapter extends DomAdapter {
8
8
  // private compositionRef = ref<Element>()
9
9
  componentRefs = /* @__PURE__ */ new WeakMap();
10
10
  components = {};
11
- constructor(c, a) {
11
+ constructor(components, mount) {
12
12
  super({
13
- createCompositionNode: (e, o) => new y("span", {
14
- style: {
15
- textDecoration: "underline"
16
- },
17
- ref: o
18
- }, [
19
- new R(e.text)
20
- ]),
21
- getParentNode(e) {
22
- return e.parentNode;
13
+ createCompositionNode: (compositionState, updateNativeCompositionNode) => {
14
+ return new VElement("span", {
15
+ style: {
16
+ textDecoration: "underline"
17
+ },
18
+ ref: updateNativeCompositionNode
19
+ }, [
20
+ new VTextNode(compositionState.text)
21
+ ]);
23
22
  },
24
- getChildNodes(e) {
25
- return Array.from(e.childNodes);
23
+ getParentNode(node) {
24
+ return node.parentNode;
26
25
  },
27
- isNativeElementNode(e) {
28
- return e instanceof Element;
26
+ getChildNodes(parentElement) {
27
+ return Array.from(parentElement.childNodes);
29
28
  },
30
- getChildByIndex(e, o) {
31
- return e.childNodes[o];
29
+ isNativeElementNode(node) {
30
+ return node instanceof Element;
32
31
  },
33
- getAndUpdateSlotRootNativeElement(e, o) {
34
- const t = e.attrs.get("ref");
35
- t ? e.attrs.set("ref", (n) => {
36
- o(n), typeof t == "function" ? t(n) : t.value || (t.value = n);
37
- }) : e.attrs.set("ref", o);
32
+ getChildByIndex(parentElement, index) {
33
+ return parentElement.childNodes[index];
38
34
  },
39
- componentRender: (e) => {
40
- const o = this.components[e.name] || this.components["*"];
41
- if (o) {
42
- let t = this.componentRefs.get(e);
43
- return t || (t = w(), this.componentRefs.set(e, t)), i(o, {
44
- component: e,
45
- rootRef: t,
46
- key: e.id
35
+ getAndUpdateSlotRootNativeElement(vElement, update) {
36
+ const currentRef = vElement.attrs.get("ref");
37
+ if (currentRef) {
38
+ vElement.attrs.set("ref", (v) => {
39
+ update(v);
40
+ if (typeof currentRef === "function") {
41
+ currentRef(v);
42
+ } else if (!currentRef.value) {
43
+ currentRef.value = v;
44
+ }
47
45
  });
46
+ } else {
47
+ vElement.attrs.set("ref", update);
48
48
  }
49
- throw u(`cannot found view component \`${e.name}\`!`);
50
49
  },
51
- vElementToViewElement(e, o) {
52
- const t = {
53
- ...Array.from(e.attrs).reduce((n, r) => (n[r[0]] = r[1], n), {})
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 = ref();
56
+ this.componentRefs.set(component, rootRef);
57
+ }
58
+ return 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 = {
68
+ ...Array.from(vNode.attrs).reduce((a, b) => {
69
+ a[b[0]] = b[1];
70
+ return a;
71
+ }, {})
54
72
  };
55
- return e.classes.size && (t.class = Array.from(e.classes).join(" ")), e.styles && (t.style = Array.from(e.styles).reduce((n, r) => (n[r[0]] = r[1], n), {})), i(e.tagName, t, ...o);
73
+ if (vNode.classes.size) {
74
+ props.class = Array.from(vNode.classes).join(" ");
75
+ }
76
+ if (vNode.styles) {
77
+ props.style = Array.from(vNode.styles).reduce((a, b) => {
78
+ a[b[0]] = b[1];
79
+ return a;
80
+ }, {});
81
+ }
82
+ return h(vNode.tagName, props, ...children);
56
83
  }
57
- }, a), Object.keys(c).forEach((e) => {
58
- const o = c[e], t = o.setup, n = this;
59
- o.setup = function(r, d, ...l) {
60
- const s = r.component, p = E(), h = k(s.changeMarker.onChange, s.changeMarker.onForceChange).subscribe(() => {
61
- s.changeMarker.dirty && p.proxy.$forceUpdate();
84
+ }, mount);
85
+ Object.keys(components).forEach((key) => {
86
+ const vueComponent = components[key];
87
+ const setup = vueComponent.setup;
88
+ const self = this;
89
+ vueComponent.setup = function(props, context, ...args) {
90
+ const component = props.component;
91
+ const vueInstance = getCurrentInstance();
92
+ const sub = merge(component.changeMarker.onChange, component.changeMarker.onForceChange).subscribe(() => {
93
+ if (component.changeMarker.dirty) {
94
+ vueInstance.proxy.$forceUpdate();
95
+ }
96
+ });
97
+ onMounted(() => {
98
+ if (props.rootRef.value) {
99
+ self.componentRootElementCaches.set(component, props.rootRef.value);
100
+ } else {
101
+ self.componentRootElementCaches.remove(component);
102
+ }
103
+ });
104
+ onUpdated(() => {
105
+ component.changeMarker.rendered();
106
+ self.onViewUpdated.next();
107
+ if (!(self.componentRefs.get(component)?.value instanceof Element)) {
108
+ throw adapterError(`Component \`${component.name}\` is not bound to rootRef, you must bind rootRef to the root element node of the component view.`);
109
+ }
110
+ });
111
+ onUnmounted(() => {
112
+ sub.unsubscribe();
62
113
  });
63
- return x(() => {
64
- r.rootRef.value ? n.componentRootElementCaches.set(s, r.rootRef.value) : n.componentRootElementCaches.remove(s);
65
- }), v(() => {
66
- if (s.changeMarker.rendered(), n.onViewUpdated.next(), !(n.componentRefs.get(s)?.value instanceof Element))
67
- throw u(`Component \`${s.name}\` is not bound to rootRef, you must bind rootRef to the root element node of the component view.`);
68
- }), A(() => {
69
- h.unsubscribe();
70
- }), t(r, d, ...l);
71
- }, this.components[e] = o;
114
+ return setup(props, context, ...args);
115
+ };
116
+ this.components[key] = vueComponent;
72
117
  });
73
118
  }
74
- render(c, a) {
75
- const e = new V(a, [{
76
- provide: C,
119
+ render(rootComponent, injector) {
120
+ const childrenInjector = new ReflectiveInjector(injector, [{
121
+ provide: Adapter,
77
122
  useValue: this
78
123
  }, {
79
- provide: m,
124
+ provide: DomAdapter,
80
125
  useValue: this
81
126
  }, {
82
- provide: f,
127
+ provide: VueAdapter,
83
128
  useValue: this
84
129
  }]);
85
- return super.render(c, e);
130
+ return super.render(rootComponent, childrenInjector);
86
131
  }
87
132
  copy() {
88
133
  document.execCommand("copy");
89
134
  }
90
135
  }
91
136
  export {
92
- f as VueAdapter
137
+ VueAdapter
93
138
  };
94
139
  //# sourceMappingURL=index.esm.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.esm.js","sources":["../src/vue-adapter.ts"],"sourcesContent":["import { Adapter, Component, CompositionState, makeError, VElement, ViewMount, VTextNode } from '@textbus/core'\nimport { DefineComponent, getCurrentInstance, h, onMounted, onUnmounted, onUpdated, ref, Ref, VNode } from 'vue'\nimport { DomAdapter } from '@textbus/platform-browser'\nimport { Injector, ReflectiveInjector } from '@viewfly/core'\nimport { merge } from '@tanbo/stream'\n\nconst adapterError = makeError('VueAdapter')\n\nexport interface ViewComponentProps<T extends Component> {\n component: T\n rootRef: Ref<Element | undefined>\n}\n\nexport interface VueAdapterComponents {\n [key: string]: DefineComponent<ViewComponentProps<any>>\n}\n\nexport class VueAdapter extends DomAdapter<VNode, VNode> {\n // private compositionRef = ref<Element>()\n private componentRefs = new WeakMap<Component, Ref<Element | undefined>>()\n private components: Record<string, DefineComponent<ViewComponentProps<any>>> = {}\n\n constructor(components: VueAdapterComponents,\n mount: ViewMount<VNode, 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: updateNativeCompositionNode\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(vElement: VElement, update: (nativeElement: (Element | null)) => void) {\n const currentRef = vElement.attrs.get('ref')\n if (currentRef) {\n vElement.attrs.set('ref', (v: Element) => {\n update(v)\n if (typeof currentRef === 'function') {\n currentRef(v)\n } else if (!currentRef.value) {\n currentRef.value = v\n }\n })\n } else {\n vElement.attrs.set('ref', update)\n }\n },\n componentRender: (component: Component<any>): VNode => {\n const comp = this.components[component.name] || this.components['*']\n if (comp) {\n let rootRef = this.componentRefs.get(component)\n if (!rootRef) {\n rootRef = ref<Element>()\n this.componentRefs.set(component, rootRef)\n }\n return h(comp, {\n component,\n rootRef,\n key: component.id\n })\n }\n throw adapterError(`cannot found view component \\`${component.name}\\`!`)\n },\n vElementToViewElement(vNode: VElement, children: Array<string | VNode>): VNode {\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.class = 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 h(vNode.tagName, props, ...children)\n }\n }, mount)\n\n // watchEffect(() => {\n // this.compositionNode = this.compositionRef.value || null\n // })\n\n Object.keys(components).forEach(key => {\n const vueComponent = components[key]\n const setup = vueComponent.setup!\n const self = this\n vueComponent.setup = function (props: ViewComponentProps<Component>, context, ...args: any[]) {\n const component = props.component\n const vueInstance = getCurrentInstance()!\n const sub = merge(component.changeMarker.onChange, component.changeMarker.onForceChange).subscribe(() => {\n if (component.changeMarker.dirty) {\n vueInstance.proxy!.$forceUpdate()\n }\n })\n onMounted(() => {\n if (props.rootRef.value) {\n self.componentRootElementCaches.set(component, props.rootRef.value)\n } else {\n self.componentRootElementCaches.remove(component)\n }\n })\n onUpdated(() => {\n component.changeMarker.rendered()\n self.onViewUpdated.next()\n\n if (!(self.componentRefs.get(component)?.value instanceof Element)) {\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 onUnmounted(() => {\n sub.unsubscribe()\n })\n return (setup as any)(props, context, ...args)\n }\n this.components[key] = vueComponent\n })\n }\n\n override render(rootComponent: Component, injector: Injector): void | (() => void) {\n const childrenInjector = new ReflectiveInjector(injector, [{\n provide: Adapter,\n useValue: this\n }, {\n provide: DomAdapter,\n useValue: this,\n }, {\n provide: VueAdapter,\n useValue: this\n }])\n return super.render(rootComponent, childrenInjector)\n }\n\n override copy() {\n document.execCommand('copy')\n }\n}\n"],"names":["adapterError","makeError","VueAdapter","DomAdapter","components","mount","compositionState","updateNativeCompositionNode","VElement","VTextNode","node","parentElement","index","vElement","update","currentRef","v","component","comp","rootRef","ref","h","vNode","children","props","a","b","key","vueComponent","setup","self","context","args","vueInstance","getCurrentInstance","sub","merge","onMounted","onUpdated","onUnmounted","rootComponent","injector","childrenInjector","ReflectiveInjector","Adapter"],"mappings":";;;;;AAMA,MAAMA,IAAeC,EAAU,YAAY;AAWpC,MAAMC,UAAmBC,EAAyB;AAAA;AAAA,EAE/C,oCAAoB,QAAA;AAAA,EACpB,aAAuE,CAAA;AAAA,EAE/E,YAAYC,GACAC,GAAkC;AAC5C,UAAM;AAAA,MACJ,uBAAuB,CAACC,GACAC,MACf,IAAIC,EAAS,QAAQ;AAAA,QAC1B,OAA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AcL,GAAOO,GAAS,GAAGC,CAAI;AAAA,MAC/C,GACA,KAAK,WAAWL,CAAG,IAAIC;AAAA,IACzB,CAAC;AAAA,EACH;AAAA,EAES,OAAOY,GAA0BC,GAAyC;AACjF,UAAMC,IAAmB,IAAIC,EAAmBF,GAAU,CAAC;AAAA,MACzD,SAASG;AAAA,MACT,UAAU;AAAA,IAAA,GACT;AAAA,MACD,SAASzC;AAAA,MACT,UAAU;AAAA,IAAA,GACT;AAAA,MACD,SAASD;AAAA,MACT,UAAU;AAAA,IAAA,CACX,CAAC;AACF,WAAO,MAAM,OAAOsC,GAAeE,CAAgB;AAAA,EACrD;AAAA,EAES,OAAO;AACd,aAAS,YAAY,MAAM;AAAA,EAC7B;AACF;"}
1
+ {"version":3,"file":"index.esm.js","sources":["../src/vue-adapter.ts"],"sourcesContent":["import { Adapter, Component, CompositionState, makeError, VElement, ViewMount, VTextNode } from '@textbus/core'\nimport { DefineComponent, getCurrentInstance, h, onMounted, onUnmounted, onUpdated, ref, Ref, VNode } from 'vue'\nimport { DomAdapter } from '@textbus/platform-browser'\nimport { Injector, ReflectiveInjector } from '@viewfly/core'\nimport { merge } from '@tanbo/stream'\n\nconst adapterError = makeError('VueAdapter')\n\nexport interface ViewComponentProps<T extends Component> {\n component: T\n rootRef: Ref<Element | undefined>\n}\n\nexport interface VueAdapterComponents {\n [key: string]: DefineComponent<ViewComponentProps<any>>\n}\n\nexport class VueAdapter extends DomAdapter<VNode, VNode> {\n // private compositionRef = ref<Element>()\n private componentRefs = new WeakMap<Component, Ref<Element | undefined>>()\n private components: Record<string, DefineComponent<ViewComponentProps<any>>> = {}\n\n constructor(components: VueAdapterComponents,\n mount: ViewMount<VNode, 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: updateNativeCompositionNode\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(vElement: VElement, update: (nativeElement: (Element | null)) => void) {\n const currentRef = vElement.attrs.get('ref')\n if (currentRef) {\n vElement.attrs.set('ref', (v: Element) => {\n update(v)\n if (typeof currentRef === 'function') {\n currentRef(v)\n } else if (!currentRef.value) {\n currentRef.value = v\n }\n })\n } else {\n vElement.attrs.set('ref', update)\n }\n },\n componentRender: (component: Component<any>): VNode => {\n const comp = this.components[component.name] || this.components['*']\n if (comp) {\n let rootRef = this.componentRefs.get(component)\n if (!rootRef) {\n rootRef = ref<Element>()\n this.componentRefs.set(component, rootRef)\n }\n return h(comp, {\n component,\n rootRef,\n key: component.id\n })\n }\n throw adapterError(`cannot found view component \\`${component.name}\\`!`)\n },\n vElementToViewElement(vNode: VElement, children: Array<string | VNode>): VNode {\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.class = 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 h(vNode.tagName, props, ...children)\n }\n }, mount)\n\n // watchEffect(() => {\n // this.compositionNode = this.compositionRef.value || null\n // })\n\n Object.keys(components).forEach(key => {\n const vueComponent = components[key]\n const setup = vueComponent.setup!\n const self = this\n vueComponent.setup = function (props: ViewComponentProps<Component>, context, ...args: any[]) {\n const component = props.component\n const vueInstance = getCurrentInstance()!\n const sub = merge(component.changeMarker.onChange, component.changeMarker.onForceChange).subscribe(() => {\n if (component.changeMarker.dirty) {\n vueInstance.proxy!.$forceUpdate()\n }\n })\n onMounted(() => {\n if (props.rootRef.value) {\n self.componentRootElementCaches.set(component, props.rootRef.value)\n } else {\n self.componentRootElementCaches.remove(component)\n }\n })\n onUpdated(() => {\n component.changeMarker.rendered()\n self.onViewUpdated.next()\n\n if (!(self.componentRefs.get(component)?.value instanceof Element)) {\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 onUnmounted(() => {\n sub.unsubscribe()\n })\n return (setup as any)(props, context, ...args)\n }\n this.components[key] = vueComponent\n })\n }\n\n override render(rootComponent: Component, injector: Injector): void | (() => void) {\n const childrenInjector = new ReflectiveInjector(injector, [{\n provide: Adapter,\n useValue: this\n }, {\n provide: DomAdapter,\n useValue: this,\n }, {\n provide: VueAdapter,\n useValue: this\n }])\n return super.render(rootComponent, childrenInjector)\n }\n\n override copy() {\n document.execCommand('copy')\n }\n}\n"],"names":[],"mappings":";;;;;AAMA,MAAM,eAAe,UAAU,YAAY;AAWpC,MAAM,mBAAmB,WAAyB;AAAA;AAAA,EAE/C,oCAAoB,QAAA;AAAA,EACpB,aAAuE,CAAA;AAAA,EAE/E,YAAY,YACA,OAAkC;AAC5C,UAAM;AAAA,MACJ,uBAAuB,CAAC,kBACA,gCAAkF;AACxG,eAAO,IAAI,SAAS,QAAQ;AAAA,UAC1B,OAAO;AAAA,YACL,gBAAgB;AAAA,UAAA;AAAA,UAElB,KAAK;AAAA,QAAA,GACJ;AAAA,UACD,IAAI,UAAU,iBAAiB,IAAI;AAAA,QAAA,CACpC;AAAA,MACH;AAAA,MACA,cAAc,MAAsC;AAClD,eAAQ,KAAc;AAAA,MACxB;AAAA,MACA,cAAc,eAA+C;AAC3D,eAAO,MAAM,KAAK,cAAc,UAAU;AAAA,MAC5C;AAAA,MACA,oBAAoB,MAAuC;AACzD,eAAO,gBAAgB;AAAA,MACzB;AAAA,MACA,gBAAgB,eAAe,OAAO;AACpC,eAAO,cAAc,W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AAA,IAAA,GACT;AAAA,MACD,SAAS;AAAA,MACT,UAAU;AAAA,IAAA,GACT;AAAA,MACD,SAAS;AAAA,MACT,UAAU;AAAA,IAAA,CACX,CAAC;AACF,WAAO,MAAM,OAAO,eAAe,gBAAgB;AAAA,EACrD;AAAA,EAES,OAAO;AACd,aAAS,YAAY,MAAM;AAAA,EAC7B;AACF;"}
package/dist/index.js CHANGED
@@ -1,2 +1,139 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("@textbus/core"),c=require("vue"),f=require("@textbus/platform-browser"),y=require("@viewfly/core"),R=require("@tanbo/stream"),m=i.makeError("VueAdapter");class d extends f.DomAdapter{componentRefs=new WeakMap;components={};constructor(a,u){super({createCompositionNode:(e,r)=>new i.VElement("span",{style:{textDecoration:"underline"},ref:r},[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.value||(t.value=n)}):e.attrs.set("ref",r)},componentRender:e=>{const r=this.components[e.name]||this.components["*"];if(r){let t=this.componentRefs.get(e);return t||(t=c.ref(),this.componentRefs.set(e,t)),c.h(r,{component:e,rootRef:t,key:e.id})}throw m(`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.class=Array.from(e.classes).join(" ")),e.styles&&(t.style=Array.from(e.styles).reduce((n,o)=>(n[o[0]]=o[1],n),{})),c.h(e.tagName,t,...r)}},u),Object.keys(a).forEach(e=>{const r=a[e],t=r.setup,n=this;r.setup=function(o,l,...p){const s=o.component,h=c.getCurrentInstance(),g=R.merge(s.changeMarker.onChange,s.changeMarker.onForceChange).subscribe(()=>{s.changeMarker.dirty&&h.proxy.$forceUpdate()});return c.onMounted(()=>{o.rootRef.value?n.componentRootElementCaches.set(s,o.rootRef.value):n.componentRootElementCaches.remove(s)}),c.onUpdated(()=>{if(s.changeMarker.rendered(),n.onViewUpdated.next(),!(n.componentRefs.get(s)?.value instanceof Element))throw m(`Component \`${s.name}\` is not bound to rootRef, you must bind rootRef to the root element node of the component view.`)}),c.onUnmounted(()=>{g.unsubscribe()}),t(o,l,...p)},this.components[e]=r})}render(a,u){const e=new y.ReflectiveInjector(u,[{provide:i.Adapter,useValue:this},{provide:f.DomAdapter,useValue:this},{provide:d,useValue:this}]);return super.render(a,e)}copy(){document.execCommand("copy")}}exports.VueAdapter=d;
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const core = require("@textbus/core");
4
+ const vue = require("vue");
5
+ const platformBrowser = require("@textbus/platform-browser");
6
+ const core$1 = require("@viewfly/core");
7
+ const stream = require("@tanbo/stream");
8
+ const adapterError = core.makeError("VueAdapter");
9
+ class VueAdapter extends platformBrowser.DomAdapter {
10
+ // private compositionRef = ref<Element>()
11
+ componentRefs = /* @__PURE__ */ new WeakMap();
12
+ components = {};
13
+ constructor(components, mount) {
14
+ super({
15
+ createCompositionNode: (compositionState, updateNativeCompositionNode) => {
16
+ return new core.VElement("span", {
17
+ style: {
18
+ textDecoration: "underline"
19
+ },
20
+ ref: updateNativeCompositionNode
21
+ }, [
22
+ new core.VTextNode(compositionState.text)
23
+ ]);
24
+ },
25
+ getParentNode(node) {
26
+ return node.parentNode;
27
+ },
28
+ getChildNodes(parentElement) {
29
+ return Array.from(parentElement.childNodes);
30
+ },
31
+ isNativeElementNode(node) {
32
+ return node instanceof Element;
33
+ },
34
+ getChildByIndex(parentElement, index) {
35
+ return parentElement.childNodes[index];
36
+ },
37
+ getAndUpdateSlotRootNativeElement(vElement, update) {
38
+ const currentRef = vElement.attrs.get("ref");
39
+ if (currentRef) {
40
+ vElement.attrs.set("ref", (v) => {
41
+ update(v);
42
+ if (typeof currentRef === "function") {
43
+ currentRef(v);
44
+ } else if (!currentRef.value) {
45
+ currentRef.value = v;
46
+ }
47
+ });
48
+ } else {
49
+ vElement.attrs.set("ref", update);
50
+ }
51
+ },
52
+ componentRender: (component) => {
53
+ const comp = this.components[component.name] || this.components["*"];
54
+ if (comp) {
55
+ let rootRef = this.componentRefs.get(component);
56
+ if (!rootRef) {
57
+ rootRef = vue.ref();
58
+ this.componentRefs.set(component, rootRef);
59
+ }
60
+ return vue.h(comp, {
61
+ component,
62
+ rootRef,
63
+ key: component.id
64
+ });
65
+ }
66
+ throw adapterError(`cannot found view component \`${component.name}\`!`);
67
+ },
68
+ vElementToViewElement(vNode, children) {
69
+ const props = {
70
+ ...Array.from(vNode.attrs).reduce((a, b) => {
71
+ a[b[0]] = b[1];
72
+ return a;
73
+ }, {})
74
+ };
75
+ if (vNode.classes.size) {
76
+ props.class = Array.from(vNode.classes).join(" ");
77
+ }
78
+ if (vNode.styles) {
79
+ props.style = Array.from(vNode.styles).reduce((a, b) => {
80
+ a[b[0]] = b[1];
81
+ return a;
82
+ }, {});
83
+ }
84
+ return vue.h(vNode.tagName, props, ...children);
85
+ }
86
+ }, mount);
87
+ Object.keys(components).forEach((key) => {
88
+ const vueComponent = components[key];
89
+ const setup = vueComponent.setup;
90
+ const self = this;
91
+ vueComponent.setup = function(props, context, ...args) {
92
+ const component = props.component;
93
+ const vueInstance = vue.getCurrentInstance();
94
+ const sub = stream.merge(component.changeMarker.onChange, component.changeMarker.onForceChange).subscribe(() => {
95
+ if (component.changeMarker.dirty) {
96
+ vueInstance.proxy.$forceUpdate();
97
+ }
98
+ });
99
+ vue.onMounted(() => {
100
+ if (props.rootRef.value) {
101
+ self.componentRootElementCaches.set(component, props.rootRef.value);
102
+ } else {
103
+ self.componentRootElementCaches.remove(component);
104
+ }
105
+ });
106
+ vue.onUpdated(() => {
107
+ component.changeMarker.rendered();
108
+ self.onViewUpdated.next();
109
+ if (!(self.componentRefs.get(component)?.value instanceof Element)) {
110
+ throw adapterError(`Component \`${component.name}\` is not bound to rootRef, you must bind rootRef to the root element node of the component view.`);
111
+ }
112
+ });
113
+ vue.onUnmounted(() => {
114
+ sub.unsubscribe();
115
+ });
116
+ return setup(props, context, ...args);
117
+ };
118
+ this.components[key] = vueComponent;
119
+ });
120
+ }
121
+ render(rootComponent, injector) {
122
+ const childrenInjector = new core$1.ReflectiveInjector(injector, [{
123
+ provide: core.Adapter,
124
+ useValue: this
125
+ }, {
126
+ provide: platformBrowser.DomAdapter,
127
+ useValue: this
128
+ }, {
129
+ provide: VueAdapter,
130
+ useValue: this
131
+ }]);
132
+ return super.render(rootComponent, childrenInjector);
133
+ }
134
+ copy() {
135
+ document.execCommand("copy");
136
+ }
137
+ }
138
+ exports.VueAdapter = VueAdapter;
2
139
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/vue-adapter.ts"],"sourcesContent":["import { Adapter, Component, CompositionState, makeError, VElement, ViewMount, VTextNode } from '@textbus/core'\nimport { DefineComponent, getCurrentInstance, h, onMounted, onUnmounted, onUpdated, ref, Ref, VNode } from 'vue'\nimport { DomAdapter } from '@textbus/platform-browser'\nimport { Injector, ReflectiveInjector } from '@viewfly/core'\nimport { merge } from '@tanbo/stream'\n\nconst adapterError = makeError('VueAdapter')\n\nexport interface ViewComponentProps<T extends Component> {\n component: T\n rootRef: Ref<Element | undefined>\n}\n\nexport interface VueAdapterComponents {\n [key: string]: DefineComponent<ViewComponentProps<any>>\n}\n\nexport class VueAdapter extends DomAdapter<VNode, VNode> {\n // private compositionRef = ref<Element>()\n private componentRefs = new WeakMap<Component, Ref<Element | undefined>>()\n private components: Record<string, DefineComponent<ViewComponentProps<any>>> = {}\n\n constructor(components: VueAdapterComponents,\n mount: ViewMount<VNode, 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: updateNativeCompositionNode\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(vElement: VElement, update: (nativeElement: (Element | null)) => void) {\n const currentRef = vElement.attrs.get('ref')\n if (currentRef) {\n vElement.attrs.set('ref', (v: Element) => {\n update(v)\n if (typeof currentRef === 'function') {\n currentRef(v)\n } else if (!currentRef.value) {\n currentRef.value = v\n }\n })\n } else {\n vElement.attrs.set('ref', update)\n }\n },\n componentRender: (component: Component<any>): VNode => {\n const comp = this.components[component.name] || this.components['*']\n if (comp) {\n let rootRef = this.componentRefs.get(component)\n if (!rootRef) {\n rootRef = ref<Element>()\n this.componentRefs.set(component, rootRef)\n }\n return h(comp, {\n component,\n rootRef,\n key: component.id\n })\n }\n throw adapterError(`cannot found view component \\`${component.name}\\`!`)\n },\n vElementToViewElement(vNode: VElement, children: Array<string | VNode>): VNode {\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.class = 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 h(vNode.tagName, props, ...children)\n }\n }, mount)\n\n // watchEffect(() => {\n // this.compositionNode = this.compositionRef.value || null\n // })\n\n Object.keys(components).forEach(key => {\n const vueComponent = components[key]\n const setup = vueComponent.setup!\n const self = this\n vueComponent.setup = function (props: ViewComponentProps<Component>, context, ...args: any[]) {\n const component = props.component\n const vueInstance = getCurrentInstance()!\n const sub = merge(component.changeMarker.onChange, component.changeMarker.onForceChange).subscribe(() => {\n if (component.changeMarker.dirty) {\n vueInstance.proxy!.$forceUpdate()\n }\n })\n onMounted(() => {\n if (props.rootRef.value) {\n self.componentRootElementCaches.set(component, props.rootRef.value)\n } else {\n self.componentRootElementCaches.remove(component)\n }\n })\n onUpdated(() => {\n component.changeMarker.rendered()\n self.onViewUpdated.next()\n\n if (!(self.componentRefs.get(component)?.value instanceof Element)) {\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 onUnmounted(() => {\n sub.unsubscribe()\n })\n return (setup as any)(props, context, ...args)\n }\n this.components[key] = vueComponent\n })\n }\n\n override render(rootComponent: Component, injector: Injector): void | (() => void) {\n const childrenInjector = new ReflectiveInjector(injector, [{\n provide: Adapter,\n useValue: this\n }, {\n provide: DomAdapter,\n useValue: this,\n }, {\n provide: VueAdapter,\n useValue: this\n }])\n return super.render(rootComponent, childrenInjector)\n }\n\n override copy() {\n document.execCommand('copy')\n }\n}\n"],"names":["adapterError","makeError","VueAdapter","DomAdapter","components","mount","compositionState","updateNativeCompositionNode","VElement","VTextNode","node","parentElement","index","vElement","update","currentRef","v","component","comp","rootRef","ref","h","vNode","children","props","a","b","key","vueComponent","setup","self","context","args","vueInstance","getCurrentInstance","sub","merge","onMounted","onUpdated","onUnmounted","rootComponent","injector","childrenInjector","ReflectiveInjector","Adapter"],"mappings":"+NAMMA,EAAeC,EAAAA,UAAU,YAAY,EAWpC,MAAMC,UAAmBC,EAAAA,UAAyB,CAE/C,kBAAoB,QACpB,WAAuE,CAAA,EAE/E,YAAYC,EACAC,EAAkC,CAC5C,MAAM,CACJ,sBAAuB,CAACC,EACAC,IACf,IAAIC,EAAAA,SAAS,OAAQ,CAC1B,MAAO,CACL,eAAgB,WAAA,EAElB,IAAKD,CAAA,EACJ,CACD,IAAIE,EAAAA,UAAUH,EAAiB,IAAI,CAAA,CACpC,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F,OAAO,MAAM,OAAOsC,EAAeE,CAAgB,CACrD,CAES,MAAO,CACd,SAAS,YAAY,MAAM,CAC7B,CACF"}
1
+ {"version":3,"file":"index.js","sources":["../src/vue-adapter.ts"],"sourcesContent":["import { Adapter, Component, CompositionState, makeError, VElement, ViewMount, VTextNode } from '@textbus/core'\nimport { DefineComponent, getCurrentInstance, h, onMounted, onUnmounted, onUpdated, ref, Ref, VNode } from 'vue'\nimport { DomAdapter } from '@textbus/platform-browser'\nimport { Injector, ReflectiveInjector } from '@viewfly/core'\nimport { merge } from '@tanbo/stream'\n\nconst adapterError = makeError('VueAdapter')\n\nexport interface ViewComponentProps<T extends Component> {\n component: T\n rootRef: Ref<Element | undefined>\n}\n\nexport interface VueAdapterComponents {\n [key: string]: DefineComponent<ViewComponentProps<any>>\n}\n\nexport class VueAdapter extends DomAdapter<VNode, VNode> {\n // private compositionRef = ref<Element>()\n private componentRefs = new WeakMap<Component, Ref<Element | undefined>>()\n private components: Record<string, DefineComponent<ViewComponentProps<any>>> = {}\n\n constructor(components: VueAdapterComponents,\n mount: ViewMount<VNode, 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: updateNativeCompositionNode\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(vElement: VElement, update: (nativeElement: (Element | null)) => void) {\n const currentRef = vElement.attrs.get('ref')\n if (currentRef) {\n vElement.attrs.set('ref', (v: Element) => {\n update(v)\n if (typeof currentRef === 'function') {\n currentRef(v)\n } else if (!currentRef.value) {\n currentRef.value = v\n }\n })\n } else {\n vElement.attrs.set('ref', update)\n }\n },\n componentRender: (component: Component<any>): VNode => {\n const comp = this.components[component.name] || this.components['*']\n if (comp) {\n let rootRef = this.componentRefs.get(component)\n if (!rootRef) {\n rootRef = ref<Element>()\n this.componentRefs.set(component, rootRef)\n }\n return h(comp, {\n component,\n rootRef,\n key: component.id\n })\n }\n throw adapterError(`cannot found view component \\`${component.name}\\`!`)\n },\n vElementToViewElement(vNode: VElement, children: Array<string | VNode>): VNode {\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.class = 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 h(vNode.tagName, props, ...children)\n }\n }, mount)\n\n // watchEffect(() => {\n // this.compositionNode = this.compositionRef.value || null\n // })\n\n Object.keys(components).forEach(key => {\n const vueComponent = components[key]\n const setup = vueComponent.setup!\n const self = this\n vueComponent.setup = function (props: ViewComponentProps<Component>, context, ...args: any[]) {\n const component = props.component\n const vueInstance = getCurrentInstance()!\n const sub = merge(component.changeMarker.onChange, component.changeMarker.onForceChange).subscribe(() => {\n if (component.changeMarker.dirty) {\n vueInstance.proxy!.$forceUpdate()\n }\n })\n onMounted(() => {\n if (props.rootRef.value) {\n self.componentRootElementCaches.set(component, props.rootRef.value)\n } else {\n self.componentRootElementCaches.remove(component)\n }\n })\n onUpdated(() => {\n component.changeMarker.rendered()\n self.onViewUpdated.next()\n\n if (!(self.componentRefs.get(component)?.value instanceof Element)) {\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 onUnmounted(() => {\n sub.unsubscribe()\n })\n return (setup as any)(props, context, ...args)\n }\n this.components[key] = vueComponent\n })\n }\n\n override render(rootComponent: Component, injector: Injector): void | (() => void) {\n const childrenInjector = new ReflectiveInjector(injector, [{\n provide: Adapter,\n useValue: this\n }, {\n provide: DomAdapter,\n useValue: this,\n }, {\n provide: VueAdapter,\n useValue: this\n }])\n return super.render(rootComponent, childrenInjector)\n }\n\n override copy() {\n document.execCommand('copy')\n }\n}\n"],"names":["makeError","DomAdapter","VElement","VTextNode","ref","h","getCurrentInstance","merge","onMounted","onUpdated","onUnmounted","ReflectiveInjector","Adapter"],"mappings":";;;;;;;AAMA,MAAM,eAAeA,KAAAA,UAAU,YAAY;AAWpC,MAAM,mBAAmBC,gBAAAA,WAAyB;AAAA;AAAA,EAE/C,oCAAoB,QAAA;AAAA,EACpB,aAAuE,CAAA;AAAA,EAE/E,YAAY,YACA,OAAkC;AAC5C,UAAM;AAAA,MACJ,uBAAuB,CAAC,kBACA,gCAAkF;AACxG,eAAO,IAAIC,KAAAA,SAAS,QAAQ;AAAA,UAC1B,OAAO;AAAA,YACL,gBAAgB;AAAA,UAAA;AAAA,UAElB,KAAK;AAAA,QAAA,GACJ;AAAA,UACD,IAAIC,KAAAA,UAAU,iBAAiB,IAAI;AAAA,QAAA,CACpC;AAAA,MACH;AAAA,MACA,cAAc,MAAsC;AAClD,eAAQ,KAAc;AAAA,MACxB;AAAA,MACA,cAAc,eAA+C;AAC3D,eAAO,MAAM,KAAK,cAAc,UAAU;AAAA,MAC5C;AAAA,MACA,oBAAoB,MAAuC;AACzD,eAAO,gBAAgB;AAAA,MACzB;AAAA,MACA,gBAAgB,eAAe,OAAO;AACpC,eAAO,cAAc,WAAW,KAAK;AAAA,MACvC;AAAA,MACA,kCAAkC,UAAoB,QAAmD;AACvG,cAAM,aAAa,SAAS,MAAM,IAAI,KAAK;AAC3C,YAAI,YAAY;AACd,mBAAS,MAAM,IAAI,OAAO,CAAC,MAAe;AACxC,mBAAO,CAAC;AACR,gBAAI,OAAO,eAAe,YAAY;AACpC,yBAAW,CAAC;AAAA,YACd,WAAW,CAA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EACrD;AAAA,EAES,OAAO;AACd,aAAS,YAAY,MAAM;AAAA,EAC7B;AACF;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@textbus/adapter-vue",
3
- "version": "5.2.2",
3
+ "version": "5.2.3",
4
4
  "description": "Textbus is a rich text editor and framework that is highly customizable and extensible to achieve rich wysiwyg effects.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.esm.js",
@@ -33,9 +33,9 @@
33
33
  ],
34
34
  "dependencies": {
35
35
  "@tanbo/stream": "^1.2.7",
36
- "@viewfly/core": "^2.2.0 || >=3.0.0-alpha.0 <4",
37
- "@textbus/core": "^5.2.2",
38
- "@textbus/platform-browser": "^5.2.2"
36
+ "@viewfly/core": "^2.2.0 || >=3.0.0-alpha.3 <4",
37
+ "@textbus/core": "^5.2.3",
38
+ "@textbus/platform-browser": "^5.2.3"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "vue": "^3.4.28"