index.es.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import l, { createContext as g, useContext as a, useRef as u, useEffect as E, useState as f, useMemo as y, useCallback as m, useLayoutEffect as v } from "react";
  2. const s = g({});
  3. function I() {
  4. const { dom: t, editor: o, setLoading: e, editorFactory: r } = a(s), i = u(null);
  5. return E(() => {
  6. const n = i.current;
  7. if (!r || !n)
  8. return;
  9. t.current = n;
  10. const d = r(n);
  11. if (d)
  12. return e(!0), d.create().then((c) => {
  13. o.current = c;
  14. }).finally(() => {
  15. e(!1);
  16. }).catch(console.error), () => {
  17. var c;
  18. (c = o.current) == null || c.destroy();
  19. };
  20. }, [t, o, r, e]), i;
  21. }
  22. const C = () => {
  23. const t = I();
  24. return /* @__PURE__ */ l.createElement("div", { "data-milkdown-root": !0, ref: t });
  25. }, R = ({ children: t }) => {
  26. const o = u(void 0), [e, r] = f(void 0), i = u(), [n, d] = f(!0), c = y(() => ({
  27. loading: n,
  28. dom: o,
  29. editor: i,
  30. setLoading: d,
  31. editorFactory: e,
  32. setEditorFactory: r
  33. }), [n, e]);
  34. return /* @__PURE__ */ l.createElement(s.Provider, { value: c }, t);
  35. };
  36. function k(t, o = []) {
  37. const e = a(s), r = m(t, o);
  38. return v(() => {
  39. e.setEditorFactory(() => r);
  40. }, [e, r]), {
  41. loading: e.loading,
  42. get: () => e.editor.current
  43. };
  44. }
  45. function F() {
  46. const t = a(s), o = m(() => t.editor.current, [t.editor]);
  47. return [t.loading, o];
  48. }
  49. export {
  50. C as Milkdown,
  51. R as MilkdownProvider,
  52. k as useEditor,
  53. F as useInstance
  54. };
  55. //# sourceMappingURL=index.es.js.map