UNPKG

@blocknote/react

Version:

A "Notion-style" block-based extensible text editor built on top of Prosemirror and Tiptap.

258 lines (257 loc) 7.74 kB
import { createContext as e, useCallback as t, useContext as n, useDebugValue as r, useEffect as i, useLayoutEffect as a, useMemo as o, useRef as s, useState as c } from "react"; import { FloatingFocusManager as l, FloatingPortal as u, autoUpdate as d, useDismiss as f, useFloating as p, useHover as m, useInteractions as h, useMergeRefs as g, useTransitionStatus as _, useTransitionStyles as v } from "@floating-ui/react"; import { useStore as y } from "@tanstack/react-store"; import { BlockNoteEditor as b, BlockNoteSchema as x, createParagraphBlockSpec as S, defaultStyleSpecs as C } from "@blocknote/core"; import { Fragment as w, jsx as T, jsxs as E } from "react/jsx-runtime"; import D from "fast-deep-equal/es6/react.js"; import { useSyncExternalStoreWithSelector as O } from "use-sync-external-store/shim/with-selector"; import { posToDOMRect as k } from "@tiptap/core"; //#region src/editor/BlockNoteContext.ts var A = e(void 0); function j(e) { return n(A); } //#endregion //#region src/hooks/useBlockNoteEditor.ts function M(e) { let t = j(e); if (!t?.editor) throw Error("useBlockNoteEditor was called outside of a BlockNoteContext provider or BlockNoteView component"); return t.editor; } //#endregion //#region src/hooks/useExtension.ts function N(e, t) { let n = (t?.editor ?? M()).getExtension(e); if (!n) throw Error("Extension not found", { cause: { plugin: e } }); return n; } function P(e, t) { let { store: n } = N(e, t); if (!n) throw Error("Store not found on plugin", { cause: { plugin: e } }); return y(n, t?.selector); } //#endregion //#region src/components/Popovers/GenericPopover.tsx function F(e) { let t = new DOMRect(), n = "getBoundingClientRect" in e ? () => e.getBoundingClientRect() : () => e.element.getBoundingClientRect(); return () => e.element && (e.cacheMountedBoundingClientRect ?? !0) ? (e.element.isConnected && (t = n()), t) : n(); } function I(e, t) { return e ? t ? (n, r, i) => { let a = e(n, r, i), o = t(n, r, i); return () => { a?.(), o?.(); }; } : e : t; } var L = (e) => { let t = M(), n = e.portalElement === null ? typeof document < "u" ? document.body : void 0 : e.portalElement ?? t?.portalElement; if (!n) throw Error("Portal element not found"); let { whileElementsMounted: r, ...a } = e.useFloatingOptions ?? {}, { refs: o, floatingStyles: c, context: y } = p({ whileElementsMounted: I(d, e.useFloatingOptions?.whileElementsMounted), ...a }), { isMounted: b, styles: x } = v(y, e.useTransitionStylesProps), { status: S } = _(y, e.useTransitionStatusProps), { getFloatingProps: C } = h([f(y, e.useDismissProps), m(y, { enabled: !1, ...e.useHoverProps })]), w = s(""), E = s(null), D = g([E, o.setFloating]); if (i(() => { if (e.reference) { let n = "element" in e.reference ? e.reference.element : void 0; n !== void 0 && (e.focusManagerProps?.disabled || !t.isWithinEditor(n)) && o.setReference(n), o.setPositionReference({ getBoundingClientRect: F(e.reference), contextElement: n }); } }, [ e.reference, o, e.focusManagerProps?.disabled, t ]), i(() => { (S === "initial" || S === "open") && E.current?.innerHTML && (w.current = E.current.innerHTML); }, [ S, e.reference, e.children ]), !b) return !1; let O = { ...e.elementProps, style: { display: "flex", ...e.elementProps?.style, zIndex: `calc(var(--bn-ui-base-z-index, 0) + ${e.elementProps?.style?.zIndex || 0})`, ...c, ...x }, ...C() }; return S === "close" ? /* @__PURE__ */ T(u, { root: n, children: /* @__PURE__ */ T("div", { ref: D, ...O, dangerouslySetInnerHTML: { __html: w.current } }) }) : e.focusManagerProps?.disabled ? /* @__PURE__ */ T(u, { root: n, children: /* @__PURE__ */ T("div", { ref: D, ...O, children: e.children }) }) : /* @__PURE__ */ T(u, { root: n, children: /* @__PURE__ */ T(l, { ...e.focusManagerProps, context: y, children: /* @__PURE__ */ T("div", { ref: D, ...O, children: e.children }) }) }); }, R = e(void 0); function z() { return n(R); } //#endregion //#region src/i18n/dictionary.ts function B() { return j().editor.dictionary; } //#endregion //#region src/hooks/useEditorState.ts var V = typeof window < "u" ? a : i, H = class { transactionNumber = 0; lastTransactionNumber = 0; lastSnapshot; editor; subscribers = /* @__PURE__ */ new Set(); constructor(e) { this.editor = e, this.lastSnapshot = { editor: e, transactionNumber: 0 }, this.getSnapshot = this.getSnapshot.bind(this), this.getServerSnapshot = this.getServerSnapshot.bind(this), this.watch = this.watch.bind(this), this.subscribe = this.subscribe.bind(this); } getSnapshot() { return this.transactionNumber === this.lastTransactionNumber ? this.lastSnapshot : (this.lastTransactionNumber = this.transactionNumber, this.lastSnapshot = { editor: this.editor, transactionNumber: this.transactionNumber }, this.lastSnapshot); } getServerSnapshot() { return { editor: null, transactionNumber: 0 }; } subscribe(e) { return this.subscribers.add(e), () => { this.subscribers.delete(e); }; } watch(e, t) { if (this.editor = e, this.editor) { let e = () => { this.transactionNumber += 1, this.subscribers.forEach((e) => e()); }, n = this.editor._tiptapEditor, r = { all: [ "transaction", "create", "mount", "unmount" ], mount: [ "create", "mount", "unmount" ], selection: ["selectionUpdate"], change: ["update"] }; for (let i of r[t]) n.on(i, e); return () => { for (let i of r[t]) n.off(i, e); }; } } }; function U(e) { let t = j(), n = e.editor || t?.editor || null, i = e.on || "all", [a] = c(() => new H(n)), o = O(a.subscribe, a.getSnapshot, a.getServerSnapshot, e.selector, e.equalityFn ?? D); return V(() => a.watch(n, i), [ n, a, i ]), r(o), o; } //#endregion //#region src/hooks/useEditorDomElement.ts function W(e) { let t = j(); return e ||= t?.editor, U({ editor: e, selector: (e) => e.editor?.domElement, equalityFn: (e, t) => e === t, on: "mount" }); } //#endregion //#region src/components/Popovers/PositionPopover.tsx var G = (e) => { let { position: t, children: n, portalElement: r, ...i } = e, { from: a, to: s } = t || {}, c = M(), l = W(); return /* @__PURE__ */ T(L, { reference: o(() => { if (!(a === void 0 || s === void 0)) return { element: l?.firstElementChild || void 0, getBoundingClientRect: () => k(c.prosemirrorView, a, s ?? a) }; }, [ c, l, a, s ]), portalElement: r, ...i, children: t !== void 0 && n }); }, K = (e = {}, t = []) => o(() => { let t = b.create(e); return window && (window.ProseMirror = t._tiptapEditor), t; }, t), q = (e) => { let [n, r] = c(!1), a = U({ editor: e.editor, selector: ({ editor: e }) => e.isEmpty }), o = z(), s = t(() => { r(!0); }, []), l = t(() => { r(!1); }, []); return i(() => { e.editable && e.autoFocus && e.editor.focus(); }, [ e.autoFocus, e.editable, e.editor ]), /* @__PURE__ */ E(w, { children: [/* @__PURE__ */ T(o.Comments.Editor, { autoFocus: e.autoFocus, className: "bn-comment-editor", editor: e.editor, onFocus: s, onBlur: l, editable: e.editable }), e.actions && /* @__PURE__ */ T("div", { className: "bn-comment-actions-wrapper", children: e.actions({ isFocused: n, isEmpty: a }) })] }); }, { textColor: J, backgroundColor: Y, ...X } = C, Z = x.create({ blockSpecs: { paragraph: S() }, styleSpecs: X }); //#endregion export { W as a, R as c, F as d, N as f, j as g, A as h, G as i, z as l, M as m, q as n, U as o, P as p, K as r, B as s, Z as t, L as u }; //# sourceMappingURL=defaultCommentEditorSchema-3DnX6Knw.js.map