docmee-ui-sdk-iframe.min.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. var R = Object.defineProperty;
  2. var q = (e, r, t) => r in e ? R(e, r, { enumerable: !0, configurable: !0, writable: !0, value: t }) : e[r] = t;
  3. var u = (e, r, t) => q(e, typeof r != "symbol" ? r + "" : r, t);
  4. const I = "%[a-f0-9]{2}", F = new RegExp("(" + I + ")|([^%]+?)", "gi"), b = new RegExp("(" + I + ")+", "gi");
  5. console.log('加载js文件');
  6. function l(e, r) {
  7. try {
  8. return [decodeURIComponent(e.join(""))];
  9. } catch {
  10. }
  11. if (e.length === 1)
  12. return e;
  13. r = r || 1;
  14. const t = e.slice(0, r), n = e.slice(r);
  15. return Array.prototype.concat.call([], l(t), l(n));
  16. }
  17. function D(e) {
  18. try {
  19. return decodeURIComponent(e);
  20. } catch {
  21. let r = e.match(F) || [];
  22. for (let t = 1; t < r.length; t++)
  23. e = l(r, t).join(""), r = e.match(F) || [];
  24. return e;
  25. }
  26. }
  27. function L(e) {
  28. const r = {
  29. "%FE%FF": "��",
  30. "%FF%FE": "��"
  31. };
  32. let t = b.exec(e);
  33. for (; t;) {
  34. try {
  35. r[t[0]] = decodeURIComponent(t[0]);
  36. } catch {
  37. const a = D(t[0]);
  38. a !== t[0] && (r[t[0]] = a);
  39. }
  40. t = b.exec(e);
  41. }
  42. r["%C2"] = "�";
  43. const n = Object.keys(r);
  44. for (const a of n)
  45. e = e.replace(new RegExp(a, "g"), r[a]);
  46. return e;
  47. }
  48. function P(e) {
  49. if (typeof e != "string")
  50. throw new TypeError("Expected `encodedURI` to be of type `string`, got `" + typeof e + "`");
  51. try {
  52. return decodeURIComponent(e);
  53. } catch {
  54. return L(e);
  55. }
  56. }
  57. function M(e, r) {
  58. if (!(typeof e == "string" && typeof r == "string"))
  59. throw new TypeError("Expected the arguments to be of type `string`");
  60. if (e === "" || r === "")
  61. return [];
  62. const t = e.indexOf(r);
  63. return t === -1 ? [] : [
  64. e.slice(0, t),
  65. e.slice(t + r.length)
  66. ];
  67. }
  68. function k(e, r) {
  69. const t = {};
  70. if (Array.isArray(r))
  71. for (const n of r) {
  72. const a = Object.getOwnPropertyDescriptor(e, n);
  73. a != null && a.enumerable && Object.defineProperty(t, n, a);
  74. }
  75. else
  76. for (const n of Reflect.ownKeys(e)) {
  77. const a = Object.getOwnPropertyDescriptor(e, n);
  78. if (a.enumerable) {
  79. const i = e[n];
  80. r(n, i, e) && Object.defineProperty(t, n, a);
  81. }
  82. }
  83. return t;
  84. }
  85. const H = (e) => e == null, T = (e) => encodeURIComponent(e).replaceAll(/[!'()*]/g, (r) => `%${r.charCodeAt(0).toString(16).toUpperCase()}`), m = Symbol("encodeFragmentIdentifier");
  86. function B(e) {
  87. switch (e.arrayFormat) {
  88. case "index":
  89. return (r) => (t, n) => {
  90. const a = t.length;
  91. return n === void 0 || e.skipNull && n === null || e.skipEmptyString && n === "" ? t : n === null ? [
  92. ...t,
  93. [f(r, e), "[", a, "]"].join("")
  94. ] : [
  95. ...t,
  96. [f(r, e), "[", f(a, e), "]=", f(n, e)].join("")
  97. ];
  98. };
  99. case "bracket":
  100. return (r) => (t, n) => n === void 0 || e.skipNull && n === null || e.skipEmptyString && n === "" ? t : n === null ? [
  101. ...t,
  102. [f(r, e), "[]"].join("")
  103. ] : [
  104. ...t,
  105. [f(r, e), "[]=", f(n, e)].join("")
  106. ];
  107. case "colon-list-separator":
  108. return (r) => (t, n) => n === void 0 || e.skipNull && n === null || e.skipEmptyString && n === "" ? t : n === null ? [
  109. ...t,
  110. [f(r, e), ":list="].join("")
  111. ] : [
  112. ...t,
  113. [f(r, e), ":list=", f(n, e)].join("")
  114. ];
  115. case "comma":
  116. case "separator":
  117. case "bracket-separator": {
  118. const r = e.arrayFormat === "bracket-separator" ? "[]=" : "=";
  119. return (t) => (n, a) => a === void 0 || e.skipNull && a === null || e.skipEmptyString && a === "" ? n : (a = a === null ? "" : a, n.length === 0 ? [[f(t, e), r, f(a, e)].join("")] : [[n, f(a, e)].join(e.arrayFormatSeparator)]);
  120. }
  121. default:
  122. return (r) => (t, n) => n === void 0 || e.skipNull && n === null || e.skipEmptyString && n === "" ? t : n === null ? [
  123. ...t,
  124. f(r, e)
  125. ] : [
  126. ...t,
  127. [f(r, e), "=", f(n, e)].join("")
  128. ];
  129. }
  130. }
  131. function W(e) {
  132. let r;
  133. switch (e.arrayFormat) {
  134. case "index":
  135. return (t, n, a) => {
  136. if (r = /\[(\d*)]$/.exec(t), t = t.replace(/\[\d*]$/, ""), !r) {
  137. a[t] = n;
  138. return;
  139. }
  140. a[t] === void 0 && (a[t] = {}), a[t][r[1]] = n;
  141. };
  142. case "bracket":
  143. return (t, n, a) => {
  144. if (r = /(\[])$/.exec(t), t = t.replace(/\[]$/, ""), !r) {
  145. a[t] = n;
  146. return;
  147. }
  148. if (a[t] === void 0) {
  149. a[t] = [n];
  150. return;
  151. }
  152. a[t] = [...a[t], n];
  153. };
  154. case "colon-list-separator":
  155. return (t, n, a) => {
  156. if (r = /(:list)$/.exec(t), t = t.replace(/:list$/, ""), !r) {
  157. a[t] = n;
  158. return;
  159. }
  160. if (a[t] === void 0) {
  161. a[t] = [n];
  162. return;
  163. }
  164. a[t] = [...a[t], n];
  165. };
  166. case "comma":
  167. case "separator":
  168. return (t, n, a) => {
  169. const i = typeof n == "string" && n.includes(e.arrayFormatSeparator), s = typeof n == "string" && !i && d(n, e).includes(e.arrayFormatSeparator);
  170. n = s ? d(n, e) : n;
  171. const o = i || s ? n.split(e.arrayFormatSeparator).map((c) => d(c, e)) : n === null ? n : d(n, e);
  172. a[t] = o;
  173. };
  174. case "bracket-separator":
  175. return (t, n, a) => {
  176. const i = /(\[])$/.test(t);
  177. if (t = t.replace(/\[]$/, ""), !i) {
  178. a[t] = n && d(n, e);
  179. return;
  180. }
  181. const s = n === null ? [] : n.split(e.arrayFormatSeparator).map((o) => d(o, e));
  182. if (a[t] === void 0) {
  183. a[t] = s;
  184. return;
  185. }
  186. a[t] = [...a[t], ...s];
  187. };
  188. default:
  189. return (t, n, a) => {
  190. if (a[t] === void 0) {
  191. a[t] = n;
  192. return;
  193. }
  194. a[t] = [...[a[t]].flat(), n];
  195. };
  196. }
  197. }
  198. function E(e) {
  199. if (typeof e != "string" || e.length !== 1)
  200. throw new TypeError("arrayFormatSeparator must be single character string");
  201. }
  202. function f(e, r) {
  203. return r.encode ? r.strict ? T(e) : encodeURIComponent(e) : e;
  204. }
  205. function d(e, r) {
  206. return r.decode ? P(e) : e;
  207. }
  208. function x(e) {
  209. return Array.isArray(e) ? e.sort() : typeof e == "object" ? x(Object.keys(e)).sort((r, t) => Number(r) - Number(t)).map((r) => e[r]) : e;
  210. }
  211. function A(e) {
  212. const r = e.indexOf("#");
  213. return r !== -1 && (e = e.slice(0, r)), e;
  214. }
  215. function V(e) {
  216. let r = "";
  217. const t = e.indexOf("#");
  218. return t !== -1 && (r = e.slice(t)), r;
  219. }
  220. function w(e, r) {
  221. return r.parseNumbers && !Number.isNaN(Number(e)) && typeof e == "string" && e.trim() !== "" ? e = Number(e) : r.parseBooleans && e !== null && (e.toLowerCase() === "true" || e.toLowerCase() === "false") && (e = e.toLowerCase() === "true"), e;
  222. }
  223. function h(e) {
  224. e = A(e);
  225. const r = e.indexOf("?");
  226. return r === -1 ? "" : e.slice(r + 1);
  227. }
  228. function g(e, r) {
  229. r = {
  230. decode: !0,
  231. sort: !0,
  232. arrayFormat: "none",
  233. arrayFormatSeparator: ",",
  234. parseNumbers: !1,
  235. parseBooleans: !1,
  236. ...r
  237. }, E(r.arrayFormatSeparator);
  238. const t = W(r), n = /* @__PURE__ */ Object.create(null);
  239. if (typeof e != "string" || (e = e.trim().replace(/^[?#&]/, ""), !e))
  240. return n;
  241. for (const a of e.split("&")) {
  242. if (a === "")
  243. continue;
  244. const i = r.decode ? a.replaceAll("+", " ") : a;
  245. let [s, o] = M(i, "=");
  246. s === void 0 && (s = i), o = o === void 0 ? null : ["comma", "separator", "bracket-separator"].includes(r.arrayFormat) ? o : d(o, r), t(d(s, r), o, n);
  247. }
  248. for (const [a, i] of Object.entries(n))
  249. if (typeof i == "object" && i !== null)
  250. for (const [s, o] of Object.entries(i))
  251. i[s] = w(o, r);
  252. else
  253. n[a] = w(i, r);
  254. return r.sort === !1 ? n : (r.sort === !0 ? Object.keys(n).sort() : Object.keys(n).sort(r.sort)).reduce((a, i) => {
  255. const s = n[i];
  256. return a[i] = s && typeof s == "object" && !Array.isArray(s) ? x(s) : s, a;
  257. }, /* @__PURE__ */ Object.create(null));
  258. }
  259. function C(e, r) {
  260. if (!e)
  261. return "";
  262. r = {
  263. encode: !0,
  264. strict: !0,
  265. arrayFormat: "none",
  266. arrayFormatSeparator: ",",
  267. ...r
  268. }, E(r.arrayFormatSeparator);
  269. const t = (s) => r.skipNull && H(e[s]) || r.skipEmptyString && e[s] === "", n = B(r), a = {};
  270. for (const [s, o] of Object.entries(e))
  271. t(s) || (a[s] = o);
  272. const i = Object.keys(a);
  273. return r.sort !== !1 && i.sort(r.sort), i.map((s) => {
  274. const o = e[s];
  275. return o === void 0 ? "" : o === null ? f(s, r) : Array.isArray(o) ? o.length === 0 && r.arrayFormat === "bracket-separator" ? f(s, r) + "[]" : o.reduce(n(s), []).join("&") : f(s, r) + "=" + f(o, r);
  276. }).filter((s) => s.length > 0).join("&");
  277. }
  278. function O(e, r) {
  279. var a;
  280. r = {
  281. decode: !0,
  282. ...r
  283. };
  284. let [t, n] = M(e, "#");
  285. return t === void 0 && (t = e), {
  286. url: ((a = t == null ? void 0 : t.split("?")) == null ? void 0 : a[0]) ?? "",
  287. query: g(h(e), r),
  288. ...r && r.parseFragmentIdentifier && n ? { fragmentIdentifier: d(n, r) } : {}
  289. };
  290. }
  291. function _(e, r) {
  292. r = {
  293. encode: !0,
  294. strict: !0,
  295. [m]: !0,
  296. ...r
  297. };
  298. const t = A(e.url).split("?")[0] || "", n = h(e.url), a = {
  299. ...g(n, { sort: !1 }),
  300. ...e.query
  301. };
  302. let i = C(a, r);
  303. i && (i = `?${i}`);
  304. let s = V(e.url);
  305. if (typeof e.fragmentIdentifier == "string") {
  306. const o = new URL(t);
  307. o.hash = e.fragmentIdentifier, s = r[m] ? o.hash : `#${e.fragmentIdentifier}`;
  308. }
  309. return `${t}${i}${s}`;
  310. }
  311. function U(e, r, t) {
  312. t = {
  313. parseFragmentIdentifier: !0,
  314. [m]: !1,
  315. ...t
  316. };
  317. const { url: n, query: a, fragmentIdentifier: i } = O(e, t);
  318. return _({
  319. url: n,
  320. query: k(a, r),
  321. fragmentIdentifier: i
  322. }, t);
  323. }
  324. function K(e, r, t) {
  325. const n = Array.isArray(r) ? (a) => !r.includes(a) : (a, i) => !r(a, i);
  326. return U(e, n, t);
  327. }
  328. const z = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  329. __proto__: null,
  330. exclude: K,
  331. extract: h,
  332. parse: g,
  333. parseUrl: O,
  334. pick: U,
  335. stringify: C,
  336. stringifyUrl: _
  337. }, Symbol.toStringTag, { value: "Module" })), j = {
  338. BASE_URL: "https://docmee.cn"
  339. }, G = () => j.BASE_URL, S = (e) => {
  340. const r = $[e], t = G();
  341. return t.endsWith("/") ? `${t}${r}` : `${t}/${r}`;
  342. }, $ = {
  343. dashboard: "sdk-ui/dashboard",
  344. editor: "sdk-ui/editor",
  345. creator: "sdk-ui/creator/0",
  346. customTemplate: "sdk-ui/custom-template",
  347. templateCreator: "sdk-ui/custom-template-creator",
  348. templateMarker: "sdk-ui/marker"
  349. };
  350. class J {
  351. /**
  352. *
  353. * @param {DocmeeUIConstructorOptions} options
  354. */
  355. constructor({
  356. token: r,
  357. page: t = "dashboard",
  358. container: n,
  359. pptId: a,
  360. onMessage: i,
  361. DOMAIN: s,
  362. ...o
  363. }) {
  364. u(this, "docmeeHref", S("dashboard"));
  365. u(this, "query", { iframe: "1" });
  366. u(this, "iframe", null);
  367. u(this, "onMessage", () => Promise.resolve(!0));
  368. u(this, "iframeMounted", !1);
  369. u(this, "initInterval", null);
  370. this.onMessage = i, this.container = n, location.protocol.startsWith("file") && console.log(
  371. "%c %s",
  372. "color: red; background-color: #f7c600",
  373. "🔴 不能在file协议下运行,请启动一个http服务来运行! 🔴 "
  374. ), r || console.log(
  375. "%c 初始化时,token不能为空!",
  376. "color: #d7514f; background-color: #2e2e2e"
  377. ), s && (j.BASE_URL = s), this.init({ token: r, page: t, pptId: a, ...o });
  378. }
  379. _postMessage(r) {
  380. var t, n;
  381. if (!this.iframe.contentWindow) throw new Error("iframe未挂载!");
  382. (n = (t = this.iframe) == null ? void 0 : t.contentWindow) == null || n.postMessage(r, this.docmeeHref);
  383. }
  384. init({ token: r, page: t = "dashboard", ...n }) {
  385. if (t === "editor" && !n.pptId)
  386. throw new Error("初始化editor页面时,必须传入pptId");
  387. this.query = Object.assign({}, this.query, n), this.docmeeHref = S(t), this.updateToken(r), this._initIframe(!0);
  388. }
  389. // 初始化iframe
  390. _initIframe(r) {
  391. const t = this.container, n = document.createElement("iframe"), a = location.href, i = z.stringifyUrl({
  392. url: this.docmeeHref,
  393. query: r ? { iframe: 1, targetOrigin: a } : this.query
  394. });
  395. n.src = i, n.style.width = "100%", n.style.height = "100%", n.style.border = "0", n.style.outline = "none", n.style.padding = "0px", n.setAttribute("allowfullscreen", "true"), this.iframe = n, this.iframeMounted = !1, t.innerHTML = "", t.appendChild(n), this.iframe.addEventListener("load", () => {
  396. let s = 0;
  397. setTimeout(() => {
  398. this.iframeMounted || (this.initInterval = setInterval(() => {
  399. if (this.iframeMounted || s >= 5)
  400. return s = 0, clearInterval(this.initInterval);
  401. r && this._postMessage({
  402. type: "transParams",
  403. data: this.query
  404. }), s++;
  405. }, 200));
  406. }, 300), window.addEventListener("message", async (o) => {
  407. var y, p;
  408. if (o.source !== this.iframe.contentWindow) return;
  409. const c = o.data;
  410. if (r && (c.type === "mounted" || c.type === "invalid-token") && (this.iframeMounted = !0, c.type === "mounted" && this._postMessage({
  411. type: "transParams",
  412. data: this.query
  413. })), c.type === "user-info" && (this.iframeMounted = !0), c.type.startsWith("before")) {
  414. const N = await ((y = this.onMessage) == null ? void 0 : y.call(this, c));
  415. this._postMessage({ data: N, type: `recover_${c.type}` });
  416. } else
  417. (p = this.onMessage) == null || p.call(this, c);
  418. });
  419. });
  420. }
  421. /**
  422. * 更新用户token
  423. * @param {string} latestToken 新的token
  424. */
  425. updateToken(r) {
  426. /(a|s)k_.+/.test(r) || console.error("token 错误!"), this.token = r, this.query.token = r, this.iframeMounted && this._postMessage({
  427. type: "transParams",
  428. data: {
  429. token: r
  430. }
  431. });
  432. }
  433. /**
  434. * 卸载iframe
  435. */
  436. destroy() {
  437. this.container.innerHTML = "";
  438. }
  439. /**
  440. * 发送消息
  441. * @param {{type: 'warning' | 'success' | 'error' | 'info', content: string}}
  442. */
  443. sendMessage(r) {
  444. this._postMessage({ type: "message", data: r });
  445. }
  446. getInfo() {
  447. this._postMessage({ type: "getInfo" });
  448. }
  449. navigate({ page: r, pptId: t, templateId: n }) {
  450. if (!$[r]) throw new Error(`页面${r} 不存在`);
  451. this._postMessage({
  452. type: "nav",
  453. data: {
  454. page: r,
  455. token: this.token,
  456. pptId: t,
  457. templateId: n
  458. }
  459. });
  460. }
  461. changeCreatorData(r, t = !1) {
  462. this._postMessage({
  463. type: "transParams",
  464. data: { creatorData: { ...r, createNow: t } }
  465. });
  466. }
  467. updateTemplate(r) {
  468. this._postMessage({
  469. type: "changeTemplateById",
  470. data: { templateId: r }
  471. });
  472. }
  473. showTemplateDialog(r = "system") {
  474. this._postMessage({
  475. type: "showTemplateDialog",
  476. data: { type: r }
  477. });
  478. }
  479. getCurrentPptInfo() {
  480. this._postMessage({
  481. type: "getCurrentPptInfo"
  482. });
  483. }
  484. importCSS(r) {
  485. this._postMessage({ type: "importCSS", data: { css: r } });
  486. }
  487. }
  488. window.DocmeeUI = J;