index.es.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. import { createCmdKey as O, commandsTimerCtx as I, CommandsReady as R, commandsCtx as d, editorStateTimerCtx as A, SchemaReady as l, inputRulesCtx as C, schemaCtx as g, schemaTimerCtx as E, marksCtx as w, nodesCtx as k, prosePluginsCtx as p, editorViewTimerCtx as D, nodeViewCtx as v, markViewCtx as $, InitReady as N, remarkPluginsCtx as b, editorViewCtx as f, serializerCtx as H, parserCtx as M, editorStateOptionsCtx as L } from "@milkdown/core";
  2. import { createTimer as F, createSlice as U } from "@milkdown/ctx";
  3. import { customAlphabet as q } from "nanoid";
  4. import { missingMarkInSchema as P, missingNodeInSchema as j } from "@milkdown/exception";
  5. import { keymap as z } from "@milkdown/prose/keymap";
  6. import { NodeType as T, DOMSerializer as B, Slice as K } from "@milkdown/prose/model";
  7. import { EditorState as G } from "@milkdown/prose/state";
  8. const J = q("abcedfghicklmn", 10);
  9. function y(n, o, r) {
  10. const t = F(r || J());
  11. let e = !1;
  12. const a = (s) => (s.record(t), s.update(o, (i) => i.concat(t)), async () => {
  13. const u = await n(s, a, () => {
  14. s.done(t), e = !0;
  15. });
  16. return e || s.done(t), () => {
  17. s.update(o, (c) => c.filter((m) => m !== t)), s.clearTimer(t), u == null || u();
  18. };
  19. });
  20. return a.timer = t, a;
  21. }
  22. function re(n, o) {
  23. const r = O(n), t = (e) => async () => {
  24. t.key = r, await e.wait(R);
  25. const a = o(e);
  26. return e.get(d).create(r, a), t.run = (s) => e.get(d).call(n, s), () => {
  27. e.get(d).remove(r);
  28. };
  29. };
  30. return t;
  31. }
  32. function ae(n, o, r) {
  33. const t = O(n);
  34. return y(
  35. async (e, a) => {
  36. await e.wait(R);
  37. const s = await o(e);
  38. return e.get(d).create(t, s), a.run = (i) => e.get(d).call(n, i), a.key = t, () => {
  39. e.get(d).remove(t);
  40. };
  41. },
  42. I,
  43. r
  44. );
  45. }
  46. function oe(n) {
  47. const o = (r) => async () => {
  48. await r.wait(l);
  49. const t = n(r);
  50. return r.update(C, (e) => [...e, t]), o.inputRule = t, () => {
  51. r.update(C, (e) => e.filter((a) => a !== t));
  52. };
  53. };
  54. return o;
  55. }
  56. function se(n, o) {
  57. return y(
  58. async (r, t) => {
  59. await r.wait(l);
  60. const e = await n(r);
  61. return r.update(C, (a) => [...a, e]), t.inputRule = e, () => {
  62. r.update(C, (a) => a.filter((s) => s !== e));
  63. };
  64. },
  65. A,
  66. o
  67. );
  68. }
  69. function Q(n, o) {
  70. const r = (t) => async () => {
  71. const e = o(t);
  72. return t.update(w, (a) => [...a.filter((s) => s[0] !== n), [n, e]]), r.id = n, r.schema = e, () => {
  73. t.update(w, (a) => a.filter(([s]) => s !== n));
  74. };
  75. };
  76. return r.type = (t) => {
  77. const e = t.get(g).marks[n];
  78. if (!e)
  79. throw P(n);
  80. return e;
  81. }, r;
  82. }
  83. function ie(n, o, r) {
  84. const t = y(
  85. async (e, a, s) => {
  86. const i = await o(e);
  87. return e.update(w, (u) => [...u.filter((c) => c[0] !== n), [n, i]]), a.id = n, a.schema = i, s(), () => {
  88. e.update(w, (u) => u.filter(([c]) => c !== n));
  89. };
  90. },
  91. E,
  92. r
  93. );
  94. return t.type = (e) => {
  95. const a = e.get(g).marks[n];
  96. if (!a)
  97. throw P(n);
  98. return a;
  99. }, t;
  100. }
  101. function W(n, o) {
  102. const r = (t) => async () => {
  103. const e = o(t);
  104. return t.update(k, (a) => [...a.filter((s) => s[0] !== n), [n, e]]), r.id = n, r.schema = e, () => {
  105. t.update(k, (a) => a.filter(([s]) => s !== n));
  106. };
  107. };
  108. return r.type = (t) => {
  109. const e = t.get(g).nodes[n];
  110. if (!e)
  111. throw j(n);
  112. return e;
  113. }, r;
  114. }
  115. function ce(n, o, r) {
  116. const t = y(
  117. async (e, a, s) => {
  118. const i = await o(e);
  119. return e.update(k, (u) => [...u.filter((c) => c[0] !== n), [n, i]]), a.id = n, a.schema = i, s(), () => {
  120. e.update(k, (u) => u.filter(([c]) => c !== n));
  121. };
  122. },
  123. E,
  124. r
  125. );
  126. return t.type = (e) => {
  127. const a = e.get(g).nodes[n];
  128. if (!a)
  129. throw j(n);
  130. return a;
  131. }, t;
  132. }
  133. function ue(n) {
  134. let o;
  135. const r = (t) => async () => (await t.wait(l), o = n(t), t.update(p, (e) => [...e, o]), () => {
  136. t.update(p, (e) => e.filter((a) => a !== o));
  137. });
  138. return r.plugin = () => o, r.key = () => o.spec.key, r;
  139. }
  140. function me(n, o) {
  141. let r;
  142. const t = y(
  143. async (e) => (await e.wait(l), r = await n(e), e.update(p, (a) => [...a, r]), () => {
  144. e.update(p, (a) => a.filter((s) => s !== r));
  145. }),
  146. A,
  147. o
  148. );
  149. return t.plugin = () => r, t.key = () => r.spec.key, t;
  150. }
  151. function X(n) {
  152. const o = (r) => async () => {
  153. await r.wait(l);
  154. const t = n(r), e = z(t);
  155. return r.update(p, (a) => [...a, e]), o.keymap = t, () => {
  156. r.update(p, (a) => a.filter((s) => s !== e));
  157. };
  158. };
  159. return o;
  160. }
  161. function pe(n, o) {
  162. return y(
  163. async (r, t) => {
  164. await r.wait(l);
  165. const e = await n(r), a = z(e);
  166. return r.update(p, (s) => [...s, a]), t.keymap = e, () => {
  167. r.update(p, (s) => s.filter((i) => i !== a));
  168. };
  169. },
  170. A,
  171. o
  172. );
  173. }
  174. function le(n, o) {
  175. const r = (t) => async () => {
  176. await t.wait(l);
  177. const e = o(t);
  178. return n.type(t) instanceof T ? t.update(v, (a) => [...a, [n.id, e]]) : t.update($, (a) => [...a, [n.id, e]]), r.view = e, r.type = n, () => {
  179. n.type(t) instanceof T ? t.update(v, (a) => a.filter((s) => s[0] !== n.id)) : t.update($, (a) => a.filter((s) => s[0] !== n.id));
  180. };
  181. };
  182. return r;
  183. }
  184. function de(n, o, r) {
  185. return y(
  186. async (t, e) => {
  187. await t.wait(l);
  188. const a = await o(t);
  189. return n.type(t) instanceof T ? t.update(v, (s) => [...s, [n.id, a]]) : t.update($, (s) => [...s, [n.id, a]]), e.view = a, e.type = n, () => {
  190. n.type(t) instanceof T ? t.update(v, (s) => s.filter((i) => i[0] !== n.id)) : t.update($, (s) => s.filter((i) => i[0] !== n.id));
  191. };
  192. },
  193. D,
  194. r
  195. );
  196. }
  197. function h(n, o) {
  198. const r = U(n, o), t = (e) => (e.inject(r), () => () => {
  199. e.remove(r);
  200. });
  201. return t.key = r, t;
  202. }
  203. function fe(n, o) {
  204. const r = h(o, n), t = W(n, (a) => a.get(r.key)(a)), e = [r, t];
  205. return e.id = t.id, e.node = t, e.type = (a) => t.type(a), e.schema = t.schema, e.ctx = r, e.key = r.key, e.extendSchema = (a) => (s) => () => {
  206. const i = s.get(r.key), c = a(i)(s);
  207. s.update(k, (m) => [...m.filter((S) => S[0] !== n), [n, c]]), e.schema = c;
  208. }, e;
  209. }
  210. function ye(n, o) {
  211. const r = h(o, n), t = Q(n, (a) => a.get(r.key)(a)), e = [r, t];
  212. return e.id = t.id, e.mark = t, e.type = t.type, e.schema = t.schema, e.ctx = r, e.key = r.key, e.extendSchema = (a) => (s) => () => {
  213. const i = s.get(r.key), c = a(i)(s);
  214. s.update(w, (m) => [...m.filter((S) => S[0] !== n), [n, c]]), e.schema = c;
  215. }, e;
  216. }
  217. function ge(n, o) {
  218. const r = Object.fromEntries(Object.entries(o).map(([s, { shortcuts: i }]) => [s, i])), t = h(r, `${n}Keymap`), e = X((s) => {
  219. const i = s.get(t.key), u = Object.entries(o).flatMap(([c, { command: m }]) => [i[c]].flat().map((V) => [V, m(s)]));
  220. return Object.fromEntries(u);
  221. }), a = [t, e];
  222. return a.ctx = t, a.shortcuts = e, a.key = t.key, a.keymap = e.keymap, a;
  223. }
  224. const he = (n, o = () => ({})) => h(o, `${n}Attr`), we = (n, o = () => ({})) => h(o, `${n}Attr`);
  225. function ke(n, o, r) {
  226. const t = h(r ?? {}, n), e = (s) => async () => {
  227. await s.wait(N);
  228. const u = {
  229. plugin: o(s),
  230. options: s.get(t.key)
  231. };
  232. return s.update(b, (c) => [...c, u]), () => {
  233. s.update(b, (c) => c.filter((m) => m !== u));
  234. };
  235. }, a = [t, e];
  236. return a.id = n, a.plugin = e, a.options = t, a;
  237. }
  238. function Se(n, o) {
  239. return (r) => r.get(d).call(n, o);
  240. }
  241. function Ce() {
  242. return (n) => {
  243. const o = n.get(f), { tr: r } = o.state, t = Object.assign(Object.create(r), r).setTime(Date.now());
  244. return o.dispatch(t);
  245. };
  246. }
  247. function ve() {
  248. return (n) => {
  249. const o = document.createElement("div"), r = n.get(g), t = n.get(f), e = B.fromSchema(r).serializeFragment(t.state.doc.content);
  250. return o.appendChild(e), o.innerHTML;
  251. };
  252. }
  253. function $e() {
  254. return (n) => {
  255. const o = n.get(f);
  256. return n.get(H)(o.state.doc);
  257. };
  258. }
  259. function Te(n) {
  260. return (o) => {
  261. const r = o.get(f), e = o.get(M)(n);
  262. if (!e)
  263. return;
  264. const a = r.state.selection.content();
  265. return r.dispatch(
  266. r.state.tr.replaceSelection(new K(e.content, a.openStart, a.openEnd)).scrollIntoView()
  267. );
  268. };
  269. }
  270. function Ae() {
  271. return (n) => {
  272. const o = n.get(f), r = [];
  273. return o.state.doc.descendants((e) => {
  274. e.type.name === "heading" && e.attrs.level && r.push({ text: e.textContent, level: e.attrs.level, id: e.attrs.id });
  275. }), r;
  276. };
  277. }
  278. function be(n, o = !1) {
  279. return (r) => {
  280. const t = r.get(f), a = r.get(M)(n);
  281. if (!a)
  282. return;
  283. if (!o) {
  284. const { state: m } = t;
  285. return t.dispatch(m.tr.replace(0, m.doc.content.size, new K(a.content, 0, 0)));
  286. }
  287. const s = r.get(g), i = r.get(L), u = r.get(p), c = G.create({
  288. schema: s,
  289. doc: a,
  290. plugins: u,
  291. ...i
  292. });
  293. t.updateState(c);
  294. };
  295. }
  296. function Oe(n, o) {
  297. return (r) => {
  298. const t = r.get(f), { tr: e } = t.state, a = e.doc.nodeAt(n);
  299. if (!a)
  300. return;
  301. const s = o(a.attrs);
  302. return t.dispatch(e.setNodeMarkup(n, void 0, s));
  303. };
  304. }
  305. const Re = (...n) => {
  306. const o = n.length;
  307. let r = o;
  308. for (; r--; )
  309. if (typeof n[r] != "function")
  310. throw new TypeError("Expected a function");
  311. return (...t) => {
  312. let e = 0, a = o ? n[e](...t) : t[0];
  313. for (; ++e < o; )
  314. a = n[e](a);
  315. return a;
  316. };
  317. };
  318. export {
  319. re as $command,
  320. ae as $commandAsync,
  321. h as $ctx,
  322. oe as $inputRule,
  323. se as $inputRuleAsync,
  324. Q as $mark,
  325. ie as $markAsync,
  326. we as $markAttr,
  327. ye as $markSchema,
  328. W as $node,
  329. ce as $nodeAsync,
  330. he as $nodeAttr,
  331. fe as $nodeSchema,
  332. ue as $prose,
  333. me as $proseAsync,
  334. ke as $remark,
  335. X as $shortcut,
  336. pe as $shortcutAsync,
  337. ge as $useKeymap,
  338. le as $view,
  339. de as $viewAsync,
  340. y as addTimer,
  341. Se as callCommand,
  342. Ce as forceUpdate,
  343. ve as getHTML,
  344. $e as getMarkdown,
  345. Te as insert,
  346. J as nanoid,
  347. Ae as outline,
  348. Re as pipe,
  349. be as replaceAll,
  350. Oe as setAttr
  351. };
  352. //# sourceMappingURL=index.es.js.map