qlfy-postmate 1.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/README.md ADDED
File without changes
package/lib/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { default as ChildPostmate } from './postMeta/ChildPostmate.js';
2
+ import { default as ParentPostmate } from './postMeta/ParentPostmate.js';
3
+ export { ChildPostmate, ParentPostmate };
package/lib/index.mjs ADDED
@@ -0,0 +1,225 @@
1
+ var u = Object.defineProperty;
2
+ var g = (r, t, e) => t in r ? u(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
3
+ var n = (r, t, e) => g(r, typeof t != "symbol" ? t + "" : t, e);
4
+ const l = "application/x-postmate-v1+json";
5
+ let w = 0;
6
+ const y = {
7
+ handshake: 1,
8
+ "handshake-reply": 1,
9
+ call: 1,
10
+ emit: 1,
11
+ reply: 1,
12
+ request: 1
13
+ };
14
+ function f(r, t) {
15
+ return (typeof t != "string" || r.origin === t) && !!r.data && (typeof r.data != "object" || "postmate" in r.data) && r.data.type === l && !!y[r.data.postmate];
16
+ }
17
+ class E {
18
+ constructor(t) {
19
+ this.parent = t.parent, this.frame = t.frame, this.child = t.child, this.childOrigin = t.childOrigin, this.events = {}, this.listener = (e) => {
20
+ if (!f(e, this.childOrigin)) return;
21
+ const { value: s = {} } = e.data, { name: a, data: i } = s;
22
+ e.data.postmate === "emit" && a in this.events && this.events[a].call(this, i);
23
+ }, this.parent.addEventListener("message", this.listener, !1);
24
+ }
25
+ get(t) {
26
+ return new h.Promise((e) => {
27
+ const s = ++w, a = (i) => {
28
+ i.data.uid === s && i.data.postmate === "reply" && (this.parent.removeEventListener("message", a, !1), e(i.data.value));
29
+ };
30
+ this.parent.addEventListener("message", a, !1), this.child.postMessage(
31
+ {
32
+ postmate: "request",
33
+ type: l,
34
+ property: t,
35
+ uid: s
36
+ },
37
+ this.childOrigin
38
+ );
39
+ });
40
+ }
41
+ call(t, e) {
42
+ this.child.postMessage(
43
+ {
44
+ postmate: "call",
45
+ type: l,
46
+ property: t,
47
+ data: e
48
+ },
49
+ this.childOrigin
50
+ );
51
+ }
52
+ on(t, e) {
53
+ this.events[t] = e;
54
+ }
55
+ destroy() {
56
+ window.removeEventListener("message", this.listener, !1), this.frame.parentNode.removeChild(this.frame);
57
+ }
58
+ }
59
+ class P {
60
+ constructor(t) {
61
+ this.model = t.model, this.parent = t.parent, this.parentOrigin = t.parentOrigin, this.child = t.child, this.child.addEventListener("message", (e) => {
62
+ if (!f(e, this.parentOrigin)) return;
63
+ const { postmate: s, property: a, uid: i, data: d } = e.data, o = this.model[a];
64
+ if (s !== "call") {
65
+ const m = typeof o == "function" ? o() : o;
66
+ h.Promise.resolve(m).then((c) => {
67
+ e.source.postMessage(
68
+ {
69
+ property: a,
70
+ postmate: "reply",
71
+ type: l,
72
+ uid: i,
73
+ value: c
74
+ },
75
+ e.origin
76
+ );
77
+ });
78
+ } else a in this.model && typeof o == "function" && o(d);
79
+ });
80
+ }
81
+ emit(t, e) {
82
+ this.parent.postMessage(
83
+ {
84
+ postmate: "emit",
85
+ type: l,
86
+ value: { name: t, data: e }
87
+ },
88
+ this.parentOrigin
89
+ );
90
+ }
91
+ }
92
+ class h {
93
+ constructor({ container: t = document.body, model: e, url: s, name: a, classListArray: i = [] }) {
94
+ return this.parent = window, this.frame = document.createElement("iframe"), this.frame.name = a || "", this.frame.classList.add(...i), t.appendChild(this.frame), this.child = this.frame.contentWindow, this.model = e || {}, this.sendHandshake(s);
95
+ }
96
+ sendHandshake(t) {
97
+ const e = (() => {
98
+ const i = document.createElement("a");
99
+ i.href = t;
100
+ const d = i.protocol.length > 4 ? i.protocol : window.location.protocol, o = i.host.length ? i.port === "80" || i.port === "443" ? i.hostname : i.host : window.location.host;
101
+ return i.origin || `${d}//${o}`;
102
+ })();
103
+ let s = 0, a;
104
+ return new h.Promise((i, d) => {
105
+ const o = (p) => {
106
+ if (!f(p, e)) return !1;
107
+ p.data.postmate === "handshake-reply" ? (clearInterval(a), this.parent.removeEventListener("message", o, !1), this.childOrigin = p.origin, i(new E(this))) : d("Failed handshake");
108
+ };
109
+ this.parent.addEventListener("message", o, !1);
110
+ const m = () => {
111
+ s++, this.child.postMessage(
112
+ {
113
+ postmate: "handshake",
114
+ type: l,
115
+ model: this.model
116
+ },
117
+ e
118
+ ), s === 5 && clearInterval(a);
119
+ }, c = () => {
120
+ m(), a = setInterval(m, 500);
121
+ };
122
+ this.frame.onload = c, this.frame.attachEvent && this.frame.attachEvent("onload", c), this.frame.src = t;
123
+ });
124
+ }
125
+ }
126
+ class k {
127
+ constructor(t) {
128
+ return this.child = window, this.model = t, this.parent = window.parent, this.sendHandshakeReply();
129
+ }
130
+ sendHandshakeReply() {
131
+ return new h.Promise((t, e) => {
132
+ this.child.addEventListener("message", (s) => {
133
+ if (s.data.postmate) {
134
+ if (s.data.postmate !== "handshake") return e("Handshake Reply Failed");
135
+ this.child.removeEventListener("message", this, !1), s.source.postMessage(
136
+ {
137
+ postmate: "handshake-reply",
138
+ type: l
139
+ },
140
+ s.origin
141
+ ), this.parentOrigin = s.origin;
142
+ const a = s.data.model;
143
+ a && Object.keys(a).forEach((i) => {
144
+ this.model[i] = a[i];
145
+ }), t(new P(this));
146
+ }
147
+ }, !1);
148
+ });
149
+ }
150
+ }
151
+ h.debug = !1;
152
+ h.Promise = (() => {
153
+ try {
154
+ return typeof window < "u" ? window.Promise : Promise;
155
+ } catch {
156
+ return Promise;
157
+ }
158
+ })();
159
+ h.Model = k;
160
+ class C {
161
+ constructor(t, e) {
162
+ n(this, "childPostmate", null);
163
+ n(this, "parentPostmate", null);
164
+ n(this, "isGetData", !1);
165
+ n(this, "getDataCllBack", null);
166
+ window.frameElement == null ? this.init(t, e) : console.warn("为嵌入主系统");
167
+ }
168
+ init(t, e) {
169
+ const s = this;
170
+ this.childPostmate = new h.Model({
171
+ // 定义子页面可以暴露给父页面的方法
172
+ baseData(a) {
173
+ s.isGetData ? s.getDataCllBack(a) : t(a), s.isGetData = !1;
174
+ }
175
+ }).then((a) => {
176
+ s.parentPostmate = a, s.getData(), e(a);
177
+ });
178
+ }
179
+ // 获取数据
180
+ getData(t = "") {
181
+ if (!this.parentPostmate) return console.warn("父页面未连接");
182
+ t ? (this.isGetData = !0, this.getDataCllBack = t) : (this.isGetData = !1, this.getDataCllBack = null), this.parentPostmate.emit("requestBaseData");
183
+ }
184
+ }
185
+ class H {
186
+ constructor(t, e, s = !0, a = "", i = "") {
187
+ n(this, "postmateChild", null);
188
+ n(this, "userInfo");
189
+ n(this, "token");
190
+ n(this, "isHideHeader", !0);
191
+ n(this, "iframeEle", "");
192
+ n(this, "iframeUrl", "");
193
+ this.userInfo = t, this.token = e, this.isHideHeader = s, this.iframeEle = a, this.iframeUrl = i, this.iframeEle && this.iframeUrl && this.init();
194
+ }
195
+ async init(t = "") {
196
+ new h({
197
+ container: this.iframeEle,
198
+ url: this.iframeUrl
199
+ }).then((s) => {
200
+ this.postmateChild = s, s.on("requestBaseData", (a) => {
201
+ console.log("接收到子页面的请求,参数为:", a), this.sendData();
202
+ }), t && t();
203
+ });
204
+ }
205
+ /** 设置iframe Dom */
206
+ setIframeEle(t) {
207
+ this.iframeEle = t, this.iframeEle && this.iframeUrl && this.init();
208
+ }
209
+ /** 设置iframe url */
210
+ setIframeUrl(t) {
211
+ this.iframeUrl = t, this.iframeEle && this.iframeUrl && this.init();
212
+ }
213
+ /** 向子页面发送消息 */
214
+ sendData() {
215
+ this.postmateChild && this.postmateChild.call("baseData", {
216
+ userInfo: this.userInfo,
217
+ token: this.token,
218
+ isHideHeader: this.isHideHeader
219
+ });
220
+ }
221
+ }
222
+ export {
223
+ C as ChildPostmate,
224
+ H as ParentPostmate
225
+ };
@@ -0,0 +1 @@
1
+ (function(r,n){typeof exports=="object"&&typeof module<"u"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):(r=typeof globalThis<"u"?globalThis:r||self,n(r["qlfy-postmate"]={}))})(this,function(r){"use strict";var C=Object.defineProperty;var v=(r,n,m)=>n in r?C(r,n,{enumerable:!0,configurable:!0,writable:!0,value:m}):r[n]=m;var l=(r,n,m)=>v(r,typeof n!="symbol"?n+"":n,m);const n="application/x-postmate-v1+json";let m=0;const y={handshake:1,"handshake-reply":1,call:1,emit:1,reply:1,request:1};function u(o,t){return(typeof t!="string"||o.origin===t)&&!!o.data&&(typeof o.data!="object"||"postmate"in o.data)&&o.data.type===n&&!!y[o.data.postmate]}class w{constructor(t){this.parent=t.parent,this.frame=t.frame,this.child=t.child,this.childOrigin=t.childOrigin,this.events={},this.listener=e=>{if(!u(e,this.childOrigin))return;const{value:s={}}=e.data,{name:i,data:a}=s;e.data.postmate==="emit"&&i in this.events&&this.events[i].call(this,a)},this.parent.addEventListener("message",this.listener,!1)}get(t){return new h.Promise(e=>{const s=++m,i=a=>{a.data.uid===s&&a.data.postmate==="reply"&&(this.parent.removeEventListener("message",i,!1),e(a.data.value))};this.parent.addEventListener("message",i,!1),this.child.postMessage({postmate:"request",type:n,property:t,uid:s},this.childOrigin)})}call(t,e){this.child.postMessage({postmate:"call",type:n,property:t,data:e},this.childOrigin)}on(t,e){this.events[t]=e}destroy(){window.removeEventListener("message",this.listener,!1),this.frame.parentNode.removeChild(this.frame)}}class E{constructor(t){this.model=t.model,this.parent=t.parent,this.parentOrigin=t.parentOrigin,this.child=t.child,this.child.addEventListener("message",e=>{if(!u(e,this.parentOrigin))return;const{postmate:s,property:i,uid:a,data:c}=e.data,d=this.model[i];if(s!=="call"){const p=typeof d=="function"?d():d;h.Promise.resolve(p).then(f=>{e.source.postMessage({property:i,postmate:"reply",type:n,uid:a,value:f},e.origin)})}else i in this.model&&typeof d=="function"&&d(c)})}emit(t,e){this.parent.postMessage({postmate:"emit",type:n,value:{name:t,data:e}},this.parentOrigin)}}class h{constructor({container:t=document.body,model:e,url:s,name:i,classListArray:a=[]}){return this.parent=window,this.frame=document.createElement("iframe"),this.frame.name=i||"",this.frame.classList.add(...a),t.appendChild(this.frame),this.child=this.frame.contentWindow,this.model=e||{},this.sendHandshake(s)}sendHandshake(t){const e=(()=>{const a=document.createElement("a");a.href=t;const c=a.protocol.length>4?a.protocol:window.location.protocol,d=a.host.length?a.port==="80"||a.port==="443"?a.hostname:a.host:window.location.host;return a.origin||`${c}//${d}`})();let s=0,i;return new h.Promise((a,c)=>{const d=g=>{if(!u(g,e))return!1;g.data.postmate==="handshake-reply"?(clearInterval(i),this.parent.removeEventListener("message",d,!1),this.childOrigin=g.origin,a(new w(this))):c("Failed handshake")};this.parent.addEventListener("message",d,!1);const p=()=>{s++,this.child.postMessage({postmate:"handshake",type:n,model:this.model},e),s===5&&clearInterval(i)},f=()=>{p(),i=setInterval(p,500)};this.frame.onload=f,this.frame.attachEvent&&this.frame.attachEvent("onload",f),this.frame.src=t})}}class P{constructor(t){return this.child=window,this.model=t,this.parent=window.parent,this.sendHandshakeReply()}sendHandshakeReply(){return new h.Promise((t,e)=>{this.child.addEventListener("message",s=>{if(s.data.postmate){if(s.data.postmate!=="handshake")return e("Handshake Reply Failed");this.child.removeEventListener("message",this,!1),s.source.postMessage({postmate:"handshake-reply",type:n},s.origin),this.parentOrigin=s.origin;const i=s.data.model;i&&Object.keys(i).forEach(a=>{this.model[a]=i[a]}),t(new E(this))}},!1)})}}h.debug=!1,h.Promise=(()=>{try{return typeof window<"u"?window.Promise:Promise}catch{return Promise}})(),h.Model=P;class k{constructor(t,e){l(this,"childPostmate",null);l(this,"parentPostmate",null);l(this,"isGetData",!1);l(this,"getDataCllBack",null);window.frameElement==null?this.init(t,e):console.warn("为嵌入主系统")}init(t,e){const s=this;this.childPostmate=new h.Model({baseData(i){s.isGetData?s.getDataCllBack(i):t(i),s.isGetData=!1}}).then(i=>{s.parentPostmate=i,s.getData(),e(i)})}getData(t=""){if(!this.parentPostmate)return console.warn("父页面未连接");t?(this.isGetData=!0,this.getDataCllBack=t):(this.isGetData=!1,this.getDataCllBack=null),this.parentPostmate.emit("requestBaseData")}}class D{constructor(t,e,s=!0,i="",a=""){l(this,"postmateChild",null);l(this,"userInfo");l(this,"token");l(this,"isHideHeader",!0);l(this,"iframeEle","");l(this,"iframeUrl","");this.userInfo=t,this.token=e,this.isHideHeader=s,this.iframeEle=i,this.iframeUrl=a,this.iframeEle&&this.iframeUrl&&this.init()}async init(t=""){new h({container:this.iframeEle,url:this.iframeUrl}).then(s=>{this.postmateChild=s,s.on("requestBaseData",i=>{console.log("接收到子页面的请求,参数为:",i),this.sendData()}),t&&t()})}setIframeEle(t){this.iframeEle=t,this.iframeEle&&this.iframeUrl&&this.init()}setIframeUrl(t){this.iframeUrl=t,this.iframeEle&&this.iframeUrl&&this.init()}sendData(){this.postmateChild&&this.postmateChild.call("baseData",{userInfo:this.userInfo,token:this.token,isHideHeader:this.isHideHeader})}}r.ChildPostmate=k,r.ParentPostmate=D,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "qlfy-postmate",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "types": "lib/index.d.ts",
6
+ "module": "lib/index.umd.js",
7
+ "main": "lib/index.umd.js",
8
+ "keywords": [
9
+ "postmate"
10
+ ],
11
+ "files": [
12
+ "lib",
13
+ "README.md",
14
+ "package.json"
15
+ ],
16
+ "scripts": {
17
+ "dev": "vite",
18
+ "build": "run-p type-check \"build-only {@}\" --",
19
+ "preview": "vite preview",
20
+ "build-only": "vite build",
21
+ "type-check": "vue-tsc --build",
22
+ "format": "prettier --write src/"
23
+ },
24
+ "dependencies": {
25
+ "@vitejs/plugin-vue-jsx": "^4.2.0",
26
+ "element-plus": "^2.8.4",
27
+ "rollup-plugin-copy": "^3.5.0",
28
+ "vite-plugin-compression": "^0.5.1",
29
+ "vite-plugin-dts": "^4.5.4",
30
+ "vue": "^3.5.13"
31
+ },
32
+ "devDependencies": {
33
+ "@tsconfig/node22": "^22.0.1",
34
+ "@types/node": "^22.14.0",
35
+ "@vitejs/plugin-vue": "^5.2.3",
36
+ "@vue/tsconfig": "^0.7.0",
37
+ "npm-run-all2": "^7.0.2",
38
+ "prettier": "3.5.3",
39
+ "sass": "^1.89.0",
40
+ "sass-loader": "^16.0.5",
41
+ "typescript": "~5.8.0",
42
+ "vite": "^6.2.4",
43
+ "vite-plugin-vue-devtools": "^7.7.2",
44
+ "vue-tsc": "^2.2.8"
45
+ }
46
+ }