index.es.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. var ve = (e, t, r) => {
  2. if (!t.has(e))
  3. throw TypeError("Cannot " + r);
  4. };
  5. var s = (e, t, r) => (ve(e, t, "read from private field"), r ? r.call(e) : t.get(e)), d = (e, t, r) => {
  6. if (t.has(e))
  7. throw TypeError("Cannot add the same private member more than once");
  8. t instanceof WeakSet ? t.add(e) : t.set(e, r);
  9. }, c = (e, t, r, i) => (ve(e, t, "write to private field"), i ? i.call(e, r) : t.set(e, r), r);
  10. import { createTimer as v, createSlice as o, Container as Se, Clock as We, Ctx as Ye } from "@milkdown/ctx";
  11. import { Schema as $e, DOMParser as qe, Node as He } from "@milkdown/prose/model";
  12. import de from "remark-parse";
  13. import he from "remark-stringify";
  14. import { unified as le } from "unified";
  15. import { ctxCallOutOfScope as Pe, docTypeError as Je, callCommandBeforeEditorView as Fe } from "@milkdown/exception";
  16. import { ParserState as Ge, SerializerState as Qe } from "@milkdown/transformer";
  17. import { PluginKey as Re, Plugin as be, EditorState as Ue } from "@milkdown/prose/state";
  18. import { EditorView as Xe } from "@milkdown/prose/view";
  19. import { customInputRules as Ze } from "@milkdown/prose";
  20. import { chainCommands as xe, deleteSelection as et, joinBackward as tt, selectNodeBackward as rt, baseKeymap as st } from "@milkdown/prose/commands";
  21. import { undoInputRule as it } from "@milkdown/prose/inputrules";
  22. import { keymap as nt } from "@milkdown/prose/keymap";
  23. function k(e, t) {
  24. return e.meta = {
  25. package: "@milkdown/core",
  26. group: "System",
  27. ...t
  28. }, e;
  29. }
  30. const De = {
  31. strong: (e, t, r, i) => {
  32. const n = e.marker || r.options.strong || "*", a = r.enter("strong"), h = r.createTracker(i);
  33. let m = h.move(n + n);
  34. return m += h.move(
  35. r.containerPhrasing(e, {
  36. before: m,
  37. after: n,
  38. ...h.current()
  39. })
  40. ), m += h.move(n + n), a(), m;
  41. },
  42. emphasis: (e, t, r, i) => {
  43. const n = e.marker || r.options.emphasis || "*", a = r.enter("emphasis"), h = r.createTracker(i);
  44. let m = h.move(n);
  45. return m += h.move(
  46. r.containerPhrasing(e, {
  47. before: m,
  48. after: n,
  49. ...h.current()
  50. })
  51. ), m += h.move(n), a(), m;
  52. }
  53. }, L = v("ConfigReady");
  54. function ot(e) {
  55. const t = (r) => (r.record(L), async () => (await e(r), r.done(L), () => {
  56. r.clearTimer(L);
  57. }));
  58. return k(t, {
  59. displayName: "Config"
  60. }), t;
  61. }
  62. const M = v("InitReady"), G = o([], "initTimer"), ke = o({}, "editor"), ue = o([], "inputRules"), N = o([], "prosePlugins"), pe = o([], "remarkPlugins"), fe = o([], "nodeView"), ye = o([], "markView"), P = o(le().use(de).use(he), "remark"), Q = o({
  63. handlers: De
  64. }, "remarkStringifyOptions");
  65. function at(e) {
  66. const t = (r) => (r.inject(ke, e).inject(N, []).inject(pe, []).inject(ue, []).inject(fe, []).inject(ye, []).inject(Q, {
  67. handlers: De
  68. }).inject(P, le().use(de).use(he)).inject(G, [L]).record(M), async () => {
  69. await r.waitTimers(G);
  70. const i = r.get(Q);
  71. return r.set(P, le().use(de).use(he, i)), r.done(M), () => {
  72. r.remove(ke).remove(N).remove(pe).remove(ue).remove(fe).remove(ye).remove(Q).remove(P).remove(G).clearTimer(M);
  73. };
  74. });
  75. return k(t, {
  76. displayName: "Init"
  77. }), t;
  78. }
  79. const R = v("SchemaReady"), U = o([], "schemaTimer"), b = o({}, "schema"), X = o([], "nodes"), Z = o([], "marks");
  80. function Te(e) {
  81. var t;
  82. return {
  83. ...e,
  84. parseDOM: (t = e.parseDOM) == null ? void 0 : t.map((r) => ({ priority: e.priority, ...r }))
  85. };
  86. }
  87. const Ee = (e) => (e.inject(b, {}).inject(X, []).inject(Z, []).inject(U, [M]).record(R), async () => {
  88. await e.waitTimers(U);
  89. const t = e.get(P), i = e.get(pe).reduce((m, f) => m.use(f.plugin, f.options), t);
  90. e.set(P, i);
  91. const n = Object.fromEntries(e.get(X).map(([m, f]) => [m, Te(f)])), a = Object.fromEntries(e.get(Z).map(([m, f]) => [m, Te(f)])), h = new $e({ nodes: n, marks: a });
  92. return e.set(b, h), e.done(R), () => {
  93. e.remove(b).remove(X).remove(Z).remove(U).clearTimer(R);
  94. };
  95. });
  96. k(Ee, {
  97. displayName: "Schema"
  98. });
  99. const W = v("ParserReady"), Ie = () => {
  100. throw Pe();
  101. }, Y = o(Ie, "parser"), x = o([], "parserTimer"), Ve = (e) => (e.inject(Y, Ie).inject(x, [R]).record(W), async () => {
  102. await e.waitTimers(x);
  103. const t = e.get(P), r = e.get(b);
  104. return e.set(Y, Ge.create(r, t)), e.done(W), () => {
  105. e.remove(Y).remove(x).clearTimer(W);
  106. };
  107. });
  108. k(Ve, {
  109. displayName: "Parser"
  110. });
  111. const $ = v("SerializerReady"), ee = o([], "serializerTimer"), Me = () => {
  112. throw Pe();
  113. }, te = o(Me, "serializer"), Ne = (e) => (e.inject(te, Me).inject(ee, [R]).record($), async () => {
  114. await e.waitTimers(ee);
  115. const t = e.get(P), r = e.get(b);
  116. return e.set(te, Qe.create(r, t)), e.done($), () => {
  117. e.remove(te).remove(ee).clearTimer($);
  118. };
  119. });
  120. k(Ne, {
  121. displayName: "Serializer"
  122. });
  123. const re = o("", "defaultValue"), V = o({}, "editorState"), se = o((e) => e, "stateOptions"), ie = o([], "editorStateTimer"), q = v("EditorStateReady");
  124. function ct(e, t, r) {
  125. if (typeof e == "string")
  126. return t(e);
  127. if (e.type === "html")
  128. return qe.fromSchema(r).parse(e.dom);
  129. if (e.type === "json")
  130. return He.fromJSON(r, e.value);
  131. throw Je(e);
  132. }
  133. const mt = new Re("MILKDOWN_STATE_TRACKER");
  134. function dt(e) {
  135. const t = xe(
  136. it,
  137. et,
  138. tt,
  139. rt
  140. );
  141. return e.Backspace = t, e;
  142. }
  143. const _e = (e) => (e.inject(re, "").inject(V, {}).inject(se, (t) => t).inject(ie, [W, $, J]).record(q), async () => {
  144. await e.waitTimers(ie);
  145. const t = e.get(b), r = e.get(Y), i = e.get(ue), n = e.get(se), a = e.get(N), h = e.get(re), m = ct(h, r, t), f = [
  146. ...a,
  147. new be({
  148. key: mt,
  149. state: {
  150. init: () => {
  151. },
  152. apply: (Be, F, ft, Le) => {
  153. e.set(V, Le);
  154. }
  155. }
  156. }),
  157. Ze({ rules: i }),
  158. nt(dt(st))
  159. ];
  160. e.set(N, f);
  161. const B = n({
  162. schema: t,
  163. doc: m,
  164. plugins: f
  165. }), l = Ue.create(B);
  166. return e.set(V, l), e.done(q), () => {
  167. e.remove(re).remove(V).remove(se).remove(ie).clearTimer(q);
  168. };
  169. });
  170. k(_e, {
  171. displayName: "EditorState"
  172. });
  173. const ne = v("EditorViewReady"), H = o({}, "editorView"), oe = o([], "editorViewTimer"), ae = o({}, "editorViewOptions"), ce = o(null, "root"), we = o(null, "rootDOM"), ge = o({}, "rootAttrs");
  174. function ht(e, t) {
  175. const r = document.createElement("div");
  176. r.className = "milkdown", e.appendChild(r), t.set(we, r);
  177. const i = t.get(ge);
  178. return Object.entries(i).forEach(([n, a]) => r.setAttribute(n, a)), r;
  179. }
  180. function lt(e) {
  181. e.classList.add("editor"), e.setAttribute("role", "textbox");
  182. }
  183. const ut = new Re("MILKDOWN_VIEW_CLEAR"), ze = (e) => (e.inject(ce, document.body).inject(H, {}).inject(ae, {}).inject(we, null).inject(ge, {}).inject(oe, [q]).record(ne), async () => {
  184. await e.wait(M);
  185. const t = e.get(ce) || document.body, r = typeof t == "string" ? document.querySelector(t) : t;
  186. e.update(N, (f) => [
  187. new be({
  188. key: ut,
  189. view: (B) => {
  190. const l = r ? ht(r, e) : void 0;
  191. return (() => {
  192. if (l && r) {
  193. const F = B.dom;
  194. r.replaceChild(l, F), l.appendChild(F);
  195. }
  196. })(), {
  197. destroy: () => {
  198. l != null && l.parentNode && (l == null || l.parentNode.replaceChild(B.dom, l)), l == null || l.remove();
  199. }
  200. };
  201. }
  202. }),
  203. ...f
  204. ]), await e.waitTimers(oe);
  205. const i = e.get(V), n = e.get(ae), a = Object.fromEntries(e.get(fe)), h = Object.fromEntries(e.get(ye)), m = new Xe(r, {
  206. state: i,
  207. nodeViews: a,
  208. markViews: h,
  209. ...n
  210. });
  211. return lt(m.dom), e.set(H, m), e.done(ne), () => {
  212. m == null || m.destroy(), e.remove(ce).remove(H).remove(ae).remove(we).remove(ge).remove(oe).clearTimer(ne);
  213. };
  214. });
  215. k(ze, {
  216. displayName: "EditorView"
  217. });
  218. var T, g;
  219. class Ke {
  220. constructor() {
  221. d(this, T, void 0);
  222. d(this, g, void 0);
  223. c(this, T, new Se()), c(this, g, null), this.setCtx = (t) => {
  224. c(this, g, t);
  225. };
  226. }
  227. get ctx() {
  228. return s(this, g);
  229. }
  230. /// Register a command into the manager.
  231. create(t, r) {
  232. const i = t.create(s(this, T).sliceMap);
  233. return i.set(r), i;
  234. }
  235. get(t) {
  236. return s(this, T).get(t).get();
  237. }
  238. remove(t) {
  239. return s(this, T).remove(t);
  240. }
  241. call(t, r) {
  242. if (s(this, g) == null)
  243. throw Fe();
  244. const n = this.get(t)(r), a = s(this, g).get(H);
  245. return n(a.state, a.dispatch, a);
  246. }
  247. }
  248. T = new WeakMap(), g = new WeakMap();
  249. function Et(e = "cmdKey") {
  250. return o(() => () => !1, e);
  251. }
  252. const je = o(new Ke(), "commands"), me = o([R], "commandsTimer"), J = v("CommandsReady"), Ae = (e) => {
  253. const t = new Ke();
  254. return t.setCtx(e), e.inject(je, t).inject(me, [R]).record(J), async () => (await e.waitTimers(me), e.done(J), () => {
  255. e.remove(je).remove(me).clearTimer(J);
  256. });
  257. };
  258. k(Ae, {
  259. displayName: "Commands"
  260. });
  261. var pt = /* @__PURE__ */ ((e) => (e.Idle = "Idle", e.OnCreate = "OnCreate", e.Created = "Created", e.OnDestroy = "OnDestroy", e.Destroyed = "Destroyed", e))(pt || {}), j, p, y, D, _, z, u, w, O, K, S, E, A, C, I;
  262. const Ce = class Ce {
  263. constructor() {
  264. d(this, j, void 0);
  265. d(this, p, void 0);
  266. d(this, y, void 0);
  267. d(this, D, void 0);
  268. d(this, _, void 0);
  269. d(this, z, void 0);
  270. d(this, u, void 0);
  271. d(this, w, void 0);
  272. d(this, O, void 0);
  273. d(this, K, void 0);
  274. d(this, S, void 0);
  275. d(this, E, void 0);
  276. d(this, A, void 0);
  277. d(this, C, void 0);
  278. d(this, I, void 0);
  279. c(this, j, !1), c(this, p, "Idle"), c(this, y, []), c(this, D, () => {
  280. }), c(this, _, new Se()), c(this, z, new We()), c(this, u, /* @__PURE__ */ new Map()), c(this, w, /* @__PURE__ */ new Map()), c(this, O, new Ye(s(this, _), s(this, z))), c(this, K, () => {
  281. const t = ot(async (i) => {
  282. await Promise.all(s(this, y).map((n) => n(i)));
  283. }), r = [
  284. Ee,
  285. Ve,
  286. Ne,
  287. Ae,
  288. _e,
  289. ze,
  290. at(this),
  291. t
  292. ];
  293. s(this, S).call(this, r, s(this, w));
  294. }), c(this, S, (t, r) => {
  295. t.forEach((i) => {
  296. const n = s(this, O).produce(s(this, j) ? i.meta : void 0), a = i(n);
  297. r.set(i, { ctx: n, handler: a, cleanup: void 0 });
  298. });
  299. }), c(this, E, (t, r = !1) => Promise.all(
  300. [t].flat().map((i) => {
  301. const n = s(this, u).get(i), a = n == null ? void 0 : n.cleanup;
  302. return r ? s(this, u).delete(i) : s(this, u).set(i, { ctx: void 0, handler: void 0, cleanup: void 0 }), typeof a == "function" ? a() : a;
  303. })
  304. )), c(this, A, async () => {
  305. await Promise.all([...s(this, w).entries()].map(([t, { cleanup: r }]) => typeof r == "function" ? r() : r)), s(this, w).clear();
  306. }), c(this, C, (t) => {
  307. c(this, p, t), s(this, D).call(this, t);
  308. }), c(this, I, (t) => [...t.entries()].map(async ([r, i]) => {
  309. const { ctx: n, handler: a } = i;
  310. if (!a)
  311. return;
  312. const h = await a();
  313. t.set(r, { ctx: n, handler: a, cleanup: h });
  314. })), this.enableInspector = (t = !0) => (c(this, j, t), this), this.onStatusChange = (t) => (c(this, D, t), this), this.config = (t) => (s(this, y).push(t), this), this.removeConfig = (t) => (c(this, y, s(this, y).filter((r) => r !== t)), this), this.use = (t) => {
  315. const r = [t].flat();
  316. return r.flat().forEach((i) => {
  317. s(this, u).set(i, {
  318. ctx: void 0,
  319. handler: void 0,
  320. cleanup: void 0
  321. });
  322. }), s(this, p) === "Created" && s(this, S).call(this, r, s(this, u)), this;
  323. }, this.remove = async (t) => s(this, p) === "OnCreate" ? (console.warn("[Milkdown]: You are trying to remove plugins when the editor is creating, this is not recommended, please check your code."), new Promise((r) => {
  324. setTimeout(() => {
  325. r(this.remove(t));
  326. }, 50);
  327. })) : (await s(this, E).call(this, [t].flat(), !0), this), this.create = async () => s(this, p) === "OnCreate" ? this : (s(this, p) === "Created" && await this.destroy(), s(this, C).call(this, "OnCreate"), s(this, K).call(this), s(this, S).call(this, [...s(this, u).keys()], s(this, u)), await Promise.all(
  328. [
  329. s(this, I).call(this, s(this, w)),
  330. s(this, I).call(this, s(this, u))
  331. ].flat()
  332. ), s(this, C).call(this, "Created"), this), this.destroy = async (t = !1) => s(this, p) === "Destroyed" || s(this, p) === "OnDestroy" ? this : s(this, p) === "OnCreate" ? new Promise((r) => {
  333. setTimeout(() => {
  334. r(this.destroy(t));
  335. }, 50);
  336. }) : (t && c(this, y, []), s(this, C).call(this, "OnDestroy"), await s(this, E).call(this, [...s(this, u).keys()], t), await s(this, A).call(this), s(this, C).call(this, "Destroyed"), this), this.action = (t) => t(s(this, O)), this.inspect = () => s(this, j) ? [...s(this, w).values(), ...s(this, u).values()].map(({ ctx: t }) => {
  337. var r;
  338. return (r = t == null ? void 0 : t.inspector) == null ? void 0 : r.read();
  339. }).filter((t) => !!t) : (console.warn("[Milkdown]: You are trying to collect inspection when inspector is disabled, please enable inspector by `editor.enableInspector()` first."), []);
  340. }
  341. /// Create a new editor instance.
  342. static make() {
  343. return new Ce();
  344. }
  345. /// Get the ctx of the editor.
  346. get ctx() {
  347. return s(this, O);
  348. }
  349. /// Get the status of the editor.
  350. get status() {
  351. return s(this, p);
  352. }
  353. };
  354. j = new WeakMap(), p = new WeakMap(), y = new WeakMap(), D = new WeakMap(), _ = new WeakMap(), z = new WeakMap(), u = new WeakMap(), w = new WeakMap(), O = new WeakMap(), K = new WeakMap(), S = new WeakMap(), E = new WeakMap(), A = new WeakMap(), C = new WeakMap(), I = new WeakMap();
  355. let Oe = Ce;
  356. export {
  357. Ke as CommandManager,
  358. J as CommandsReady,
  359. L as ConfigReady,
  360. Oe as Editor,
  361. q as EditorStateReady,
  362. pt as EditorStatus,
  363. ne as EditorViewReady,
  364. M as InitReady,
  365. W as ParserReady,
  366. R as SchemaReady,
  367. $ as SerializerReady,
  368. Ae as commands,
  369. je as commandsCtx,
  370. me as commandsTimerCtx,
  371. ot as config,
  372. Et as createCmdKey,
  373. re as defaultValueCtx,
  374. ke as editorCtx,
  375. _e as editorState,
  376. V as editorStateCtx,
  377. se as editorStateOptionsCtx,
  378. ie as editorStateTimerCtx,
  379. ze as editorView,
  380. H as editorViewCtx,
  381. ae as editorViewOptionsCtx,
  382. oe as editorViewTimerCtx,
  383. ct as getDoc,
  384. at as init,
  385. G as initTimerCtx,
  386. ue as inputRulesCtx,
  387. ye as markViewCtx,
  388. Z as marksCtx,
  389. fe as nodeViewCtx,
  390. X as nodesCtx,
  391. Ve as parser,
  392. Y as parserCtx,
  393. x as parserTimerCtx,
  394. N as prosePluginsCtx,
  395. P as remarkCtx,
  396. pe as remarkPluginsCtx,
  397. Q as remarkStringifyOptionsCtx,
  398. ge as rootAttrsCtx,
  399. ce as rootCtx,
  400. we as rootDOMCtx,
  401. Ee as schema,
  402. b as schemaCtx,
  403. U as schemaTimerCtx,
  404. Ne as serializer,
  405. te as serializerCtx,
  406. ee as serializerTimerCtx
  407. };
  408. //# sourceMappingURL=index.es.js.map