hackmd-agent.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. function K(r) {
  2. return r && r.__esModule && Object.prototype.hasOwnProperty.call(r, "default") ? r.default : r;
  3. }
  4. var $ = { exports: {} }, d = {};
  5. Object.defineProperty(d, "__esModule", {
  6. value: !0
  7. });
  8. d.DATA_CLONE_ERROR = d.MESSAGE = d.REJECTED = d.FULFILLED = d.REPLY = d.CALL = d.HANDSHAKE_REPLY = d.HANDSHAKE = void 0;
  9. const B = "handshake";
  10. d.HANDSHAKE = B;
  11. const j = "handshake-reply";
  12. d.HANDSHAKE_REPLY = j;
  13. const J = "call";
  14. d.CALL = J;
  15. const W = "reply";
  16. d.REPLY = W;
  17. const k = "fulfilled";
  18. d.FULFILLED = k;
  19. const q = "rejected";
  20. d.REJECTED = q;
  21. const Q = "message";
  22. d.MESSAGE = Q;
  23. const X = "DataCloneError";
  24. d.DATA_CLONE_ERROR = X;
  25. var S = {};
  26. Object.defineProperty(S, "__esModule", {
  27. value: !0
  28. });
  29. S.ERR_NO_IFRAME_SRC = S.ERR_NOT_IN_IFRAME = S.ERR_CONNECTION_TIMEOUT = S.ERR_CONNECTION_DESTROYED = void 0;
  30. const Z = "ConnectionDestroyed";
  31. S.ERR_CONNECTION_DESTROYED = Z;
  32. const ee = "ConnectionTimeout";
  33. S.ERR_CONNECTION_TIMEOUT = ee;
  34. const re = "NotInIframe";
  35. S.ERR_NOT_IN_IFRAME = re;
  36. const te = "NoIframeSrc";
  37. S.ERR_NO_IFRAME_SRC = te;
  38. var F = { exports: {} };
  39. (function(r, e) {
  40. Object.defineProperty(e, "__esModule", {
  41. value: !0
  42. }), e.default = void 0;
  43. var t = () => {
  44. const n = [];
  45. let u = !1;
  46. return {
  47. destroy() {
  48. u = !0, n.forEach((a) => {
  49. a();
  50. });
  51. },
  52. onDestroy(a) {
  53. u ? a() : n.push(a);
  54. }
  55. };
  56. };
  57. e.default = t, r.exports = e.default;
  58. })(F, F.exports);
  59. var ne = F.exports, H = { exports: {} }, M = {};
  60. Object.defineProperty(M, "__esModule", {
  61. value: !0
  62. });
  63. M.deserializeError = M.serializeError = void 0;
  64. const oe = (r) => {
  65. let e = r.name, t = r.message, n = r.stack;
  66. return {
  67. name: e,
  68. message: t,
  69. stack: n
  70. };
  71. };
  72. M.serializeError = oe;
  73. const ae = (r) => {
  74. const e = new Error();
  75. return Object.keys(r).forEach((t) => e[t] = r[t]), e;
  76. };
  77. M.deserializeError = ae;
  78. (function(r, e) {
  79. Object.defineProperty(e, "__esModule", {
  80. value: !0
  81. }), e.default = void 0;
  82. var t = d, n = M, u = (a, v, O) => {
  83. const i = a.localName, f = a.local, s = a.remote, h = a.originForSending, p = a.originForReceiving;
  84. let g = !1;
  85. O(`${i}: Connecting call receiver`);
  86. const L = (_) => {
  87. if (_.source !== s || _.data.penpal !== t.CALL)
  88. return;
  89. if (_.origin !== p) {
  90. O(`${i} received message from origin ${_.origin} which did not match expected origin ${p}`);
  91. return;
  92. }
  93. const m = _.data, C = m.methodName, l = m.args, T = m.id;
  94. O(`${i}: Received ${C}() call`);
  95. const E = (c) => (R) => {
  96. if (O(`${i}: Sending ${C}() reply`), g) {
  97. O(`${i}: Unable to send ${C}() reply due to destroyed connection`);
  98. return;
  99. }
  100. const N = {
  101. penpal: t.REPLY,
  102. id: T,
  103. resolution: c,
  104. returnValue: R
  105. };
  106. c === t.REJECTED && R instanceof Error && (N.returnValue = (0, n.serializeError)(R), N.returnValueIsError = !0);
  107. try {
  108. s.postMessage(N, h);
  109. } catch (D) {
  110. throw D.name === t.DATA_CLONE_ERROR && s.postMessage({
  111. penpal: t.REPLY,
  112. id: T,
  113. resolution: t.REJECTED,
  114. returnValue: (0, n.serializeError)(D),
  115. returnValueIsError: !0
  116. }, h), D;
  117. }
  118. };
  119. new Promise((c) => c(v[C].apply(v, l))).then(E(t.FULFILLED), E(t.REJECTED));
  120. };
  121. return f.addEventListener(t.MESSAGE, L), () => {
  122. g = !0, f.removeEventListener(t.MESSAGE, L);
  123. };
  124. };
  125. e.default = u, r.exports = e.default;
  126. })(H, H.exports);
  127. var ie = H.exports, b = { exports: {} }, x = { exports: {} };
  128. (function(r, e) {
  129. Object.defineProperty(e, "__esModule", {
  130. value: !0
  131. }), e.default = void 0;
  132. let t = 0;
  133. var n = () => ++t;
  134. e.default = n, r.exports = e.default;
  135. })(x, x.exports);
  136. var de = x.exports;
  137. (function(r, e) {
  138. Object.defineProperty(e, "__esModule", {
  139. value: !0
  140. }), e.default = void 0;
  141. var t = d, n = S, u = v(de), a = M;
  142. function v(i) {
  143. return i && i.__esModule ? i : { default: i };
  144. }
  145. var O = (i, f, s, h, p) => {
  146. const g = f.localName, L = f.local, _ = f.remote, m = f.originForSending, C = f.originForReceiving;
  147. let l = !1;
  148. p(`${g}: Connecting call sender`);
  149. const T = (E) => function() {
  150. for (var c = arguments.length, R = new Array(c), N = 0; N < c; N++)
  151. R[N] = arguments[N];
  152. p(`${g}: Sending ${E}() call`);
  153. let D;
  154. try {
  155. _.closed && (D = !0);
  156. } catch {
  157. D = !0;
  158. }
  159. if (D && h(), l) {
  160. const w = new Error(`Unable to send ${E}() call due to destroyed connection`);
  161. throw w.code = n.ERR_CONNECTION_DESTROYED, w;
  162. }
  163. return new Promise((w, P) => {
  164. const y = (0, u.default)(), A = (o) => {
  165. if (o.source !== _ || o.data.penpal !== t.REPLY || o.data.id !== y)
  166. return;
  167. if (o.origin !== C) {
  168. p(`${g} received message from origin ${o.origin} which did not match expected origin ${C}`);
  169. return;
  170. }
  171. p(`${g}: Received ${E}() reply`), L.removeEventListener(t.MESSAGE, A);
  172. let I = o.data.returnValue;
  173. o.data.returnValueIsError && (I = (0, a.deserializeError)(I)), (o.data.resolution === t.FULFILLED ? w : P)(I);
  174. };
  175. L.addEventListener(t.MESSAGE, A), _.postMessage({
  176. penpal: t.CALL,
  177. id: y,
  178. methodName: E,
  179. args: R
  180. }, m);
  181. });
  182. };
  183. return s.reduce((E, c) => (E[c] = T(c), E), i), () => {
  184. l = !0;
  185. };
  186. };
  187. e.default = O, r.exports = e.default;
  188. })(b, b.exports);
  189. var ce = b.exports, Y = { exports: {} };
  190. (function(r, e) {
  191. Object.defineProperty(e, "__esModule", {
  192. value: !0
  193. }), e.default = void 0;
  194. var t = (n) => function() {
  195. if (n) {
  196. for (var u = arguments.length, a = new Array(u), v = 0; v < u; v++)
  197. a[v] = arguments[v];
  198. console.log("[Penpal]", ...a);
  199. }
  200. };
  201. e.default = t, r.exports = e.default;
  202. })(Y, Y.exports);
  203. var se = Y.exports;
  204. (function(r, e) {
  205. Object.defineProperty(e, "__esModule", {
  206. value: !0
  207. }), e.default = void 0;
  208. var t = d, n = S, u = i(ne), a = i(ie), v = i(ce), O = i(se);
  209. function i(s) {
  210. return s && s.__esModule ? s : { default: s };
  211. }
  212. var f = function() {
  213. let h = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {}, p = h.parentOrigin, g = p === void 0 ? "*" : p, L = h.methods, _ = L === void 0 ? {} : L, m = h.timeout, C = h.debug;
  214. const l = (0, O.default)(C);
  215. if (window === window.top) {
  216. const w = new Error("connectToParent() must be called within an iframe");
  217. throw w.code = n.ERR_NOT_IN_IFRAME, w;
  218. }
  219. const T = (0, u.default)(), E = T.destroy, c = T.onDestroy, R = window, N = R.parent;
  220. return {
  221. promise: new Promise((w, P) => {
  222. let y;
  223. m !== void 0 && (y = setTimeout(() => {
  224. const o = new Error(`Connection to parent timed out after ${m}ms`);
  225. o.code = n.ERR_CONNECTION_TIMEOUT, P(o), E();
  226. }, m));
  227. const A = (o) => {
  228. try {
  229. clearTimeout();
  230. } catch {
  231. return;
  232. }
  233. if (o.source !== N || o.data.penpal !== t.HANDSHAKE_REPLY)
  234. return;
  235. if (g !== "*" && g !== o.origin) {
  236. l(`Child received handshake reply from origin ${o.origin} which did not match expected origin ${g}`);
  237. return;
  238. }
  239. l("Child: Received handshake reply"), R.removeEventListener(t.MESSAGE, A);
  240. const I = {
  241. localName: "Child",
  242. local: R,
  243. remote: N,
  244. originForSending: o.origin === "null" ? "*" : o.origin,
  245. originForReceiving: o.origin
  246. }, V = {}, z = (0, a.default)(I, _, l);
  247. c(z);
  248. const U = (0, v.default)(V, I, o.data.methodNames, E, l);
  249. c(U), clearTimeout(y), w(V);
  250. };
  251. R.addEventListener(t.MESSAGE, A), c(() => {
  252. R.removeEventListener(t.MESSAGE, A);
  253. const o = new Error("Connection destroyed");
  254. o.code = n.ERR_CONNECTION_DESTROYED, P(o);
  255. }), l("Child: Sending handshake"), N.postMessage({
  256. penpal: t.HANDSHAKE,
  257. methodNames: Object.keys(_)
  258. }, g);
  259. }),
  260. destroy: E
  261. };
  262. };
  263. e.default = f, r.exports = e.default;
  264. })($, $.exports);
  265. var le = $.exports;
  266. const ue = /* @__PURE__ */ K(le);
  267. function Ee(r, e, t) {
  268. var n = t || {}, u = n.noTrailing, a = u === void 0 ? !1 : u, v = n.noLeading, O = v === void 0 ? !1 : v, i = n.debounceMode, f = i === void 0 ? void 0 : i, s, h = !1, p = 0;
  269. function g() {
  270. s && clearTimeout(s);
  271. }
  272. function L(m) {
  273. var C = m || {}, l = C.upcomingOnly, T = l === void 0 ? !1 : l;
  274. g(), h = !T;
  275. }
  276. function _() {
  277. for (var m = arguments.length, C = new Array(m), l = 0; l < m; l++)
  278. C[l] = arguments[l];
  279. var T = this, E = Date.now() - p;
  280. if (h)
  281. return;
  282. function c() {
  283. p = Date.now(), e.apply(T, C);
  284. }
  285. function R() {
  286. s = void 0;
  287. }
  288. !O && f && !s && c(), g(), f === void 0 && E > r ? O ? (p = Date.now(), a || (s = setTimeout(f ? R : c, r))) : c() : a !== !0 && (s = setTimeout(f ? R : c, f === void 0 ? r - E : r));
  289. }
  290. return _.cancel = L, _;
  291. }
  292. function fe(r, e, t) {
  293. var n = t || {}, u = n.atBegin, a = u === void 0 ? !1 : u;
  294. return Ee(r, e, {
  295. debounceMode: a !== !1
  296. });
  297. }
  298. const ge = !1, _e = "<%= origin %>";
  299. function Re() {
  300. return window.editor.doc.getValue();
  301. }
  302. function G(r) {
  303. window.editor.doc.setValue(r);
  304. }
  305. function ve(r) {
  306. if (window.cmClient != null) {
  307. G(r);
  308. return;
  309. }
  310. const e = setInterval(() => {
  311. window.cmClient != null && (clearInterval(e), G(r));
  312. }, 250);
  313. }
  314. function pe(r) {
  315. window.growi.notifyBodyChanges(r);
  316. }
  317. const me = fe(800, pe);
  318. function Ce(r) {
  319. window.growi.saveWithShortcut(r);
  320. }
  321. function Oe() {
  322. const r = window.CodeMirror, e = window.editor;
  323. r == null || e == null || (e.on("change", (t, n) => {
  324. n.origin !== "ignoreHistory" && me(t.doc.getValue());
  325. }), r.commands.save = function(t) {
  326. Ce(t.doc.getValue());
  327. }, delete e.options.extraKeys["Cmd-S"], delete e.options.extraKeys["Ctrl-S"]);
  328. }
  329. function he() {
  330. ue({
  331. parentOrigin: _e,
  332. // Methods child is exposing to parent
  333. methods: {
  334. getValue() {
  335. return Re();
  336. },
  337. setValue(e) {
  338. G(e);
  339. },
  340. setValueOnInit(e) {
  341. ve(e);
  342. }
  343. },
  344. debug: ge
  345. }).promise.then((e) => {
  346. window.growi = e;
  347. }).catch((e) => {
  348. console.log(e);
  349. });
  350. }
  351. (function() {
  352. if (window === window.parent) {
  353. console.log("[GROWI] Loading agent for HackMD is not processed because currently not in iframe");
  354. return;
  355. }
  356. console.log("[HackMD] Loading GROWI agent for HackMD..."), window.addEventListener("load", () => {
  357. Oe();
  358. }), he(), console.log("[HackMD] GROWI agent for HackMD has successfully loaded.");
  359. })();
  360. //# sourceMappingURL=hackmd-agent.js.map