UNPKG

reactjs-tiptap-editor

Version:

A modern WYSIWYG rich text editor based on tiptap and shadcn ui for React

153 lines (152 loc) 4.23 kB
import { s as y, E as x } from "./clsx-DaPvp9ji.js"; import { TextSelection as b, AllSelection as I } from "@tiptap/pm/state"; import { jsx as d, Fragment as m, jsxs as A } from "react/jsx-runtime"; import { u as T, d as K, A as p } from "./index-RcSPeQHn.js"; import "react"; import "./theme.js"; var a = /* @__PURE__ */ ((t) => (t[t.max = 7] = "max", t[t.min = 0] = "min", t[t.more = 1] = "more", t[t.less = -1] = "less", t))(a || {}); function g(t, n, e) { return t < n ? n : t > e ? e : t; } function S(t, n, e, o) { const { doc: r, selection: s } = t; if (!r || !s || !(s instanceof b || s instanceof I)) return t; const { from: i, to: c } = s; return r.nodesBetween(i, c, (u, l) => { const h = u.type; return e.includes(h.name) ? (t = M(t, l, n), !1) : !y(u.type.name, o.extensionManager.extensions); }), t; } function M(t, n, e) { if (!t.doc) return t; const o = t.doc.nodeAt(n); if (!o) return t; const i = g((o.attrs.indent || 0) + e, 0, 7); if (i === o.attrs.indent) return t; const c = { ...o.attrs, indent: i }; return t.setNodeMarkup(n, o.type, c, o.marks); } function f({ delta: t, types: n }) { return ({ state: e, dispatch: o, editor: r }) => { const { selection: s } = e; let { tr: i } = e; return i = i.setSelection(s), i = S(i, t, n, r), i.docChanged ? (o && o(i), !0) : !1; }; } function D() { const t = T(k.name), { indent: n, outdent: e } = (t == null ? void 0 : t.componentProps) ?? {}, { editorDisabled: o } = K(), r = () => { o || n != null && n.action && (n == null || n.action()); }, s = () => { o || e != null && e.action && (e == null || e.action()); }; return t ? /* @__PURE__ */ A(m, { children: [ /* @__PURE__ */ d( p, { action: r, disabled: o, icon: n == null ? void 0 : n.icon, shortcutKeys: n == null ? void 0 : n.shortcutKeys, tooltip: n == null ? void 0 : n.tooltip } ), /* @__PURE__ */ d( p, { action: s, disabled: o, icon: e == null ? void 0 : e.icon, shortcutKeys: e == null ? void 0 : e.shortcutKeys, tooltip: e == null ? void 0 : e.tooltip } ) ] }) : /* @__PURE__ */ d(m, {}); } const k = /* @__PURE__ */ x.create({ name: "richtextIndentOutdent", // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-expect-error addOptions() { var t; return { ...(t = this.parent) == null ? void 0 : t.call(this), types: ["paragraph", "heading", "blockquote"], minIndent: a.min, maxIndent: a.max, button({ editor: n, t: e, extension: o }) { var r, s; return { componentProps: { indent: { action: () => { n.commands.indent(); }, shortcutKeys: ((r = o.options.shortcutKeys) == null ? void 0 : r[0]) ?? ["Tab"], icon: "IndentIncrease", tooltip: e("editor.indent.tooltip") }, outdent: { action: () => { n.commands.outdent(); }, shortcutKeys: ((s = o.options.shortcutKeys) == null ? void 0 : s[1]) ?? ["Shift", "Tab"], icon: "IndentDecrease", tooltip: e("editor.outdent.tooltip") } } }; } }; }, addGlobalAttributes() { return [ { types: this.options.types, attributes: { indent: { default: 0, parseHTML: (t) => { const n = t.dataset.indent; return (n ? Number.parseInt(n, 10) : 0) || 0; }, renderHTML: (t) => t.indent ? { "data-indent": t.indent } : {} } } } ]; }, addCommands() { return { indent: () => f({ delta: a.more, types: this.options.types }), outdent: () => f({ delta: a.less, types: this.options.types }) }; }, addKeyboardShortcuts() { return { Tab: () => this.editor.commands.indent(), "Shift-Tab": () => this.editor.commands.outdent() }; } }); export { k as I, D as R, a, M as s };