OverviewFilesCommitsIssuesDocsReleasesPeers

keet-deobfuscated / @holepunchto / keet-editor / src / extensions / helpers / exit-block.js

51 lines · javascript ref:
import { TextSelection as e } from "prosemirror-state"
export function exitAfterBlock() {
  const { editor: t, name: r } = this,
    { state: n, view: o, schema: a } = t,
    { nodes: s } = a,
    { from: c, to: i } = n.selection,
    { $head: d, $anchor: p } = n.selection
  if (!d.sameParent(p)) return
  let l
  if (
    (n.doc.nodesBetween(c - 1, i - 1, (e) => {
      e.type.name === r && (l = !0)
    }),
    !l)
  )
    return
  const f = d.after(),
    { depth: m } = d
  if (i !== f - 1) return
  let h = n.tr
  ;(f + m >= n.tr.doc.content.size && (h = h.insert(f + m - 1, s.paragraph.createAndFill(""))),
    (h = h.setSelection(e.create(h.doc, f + m))))
  const u = n.apply(h)
  return (o.updateState(u), !0)
}
export function exitBeforeBlock() {
  const { editor: t, name: r } = this,
    { state: n, view: o, schema: a } = t,
    { nodes: s } = a,
    { from: c, to: i } = n.selection,
    { $head: d, $anchor: p } = n.selection,
    { depth: l, parentOffset: f } = d
  if (0 !== f) return
  if (!d.sameParent(p)) return
  let m
  if (
    (n.doc.nodesBetween(c - 1, i - 1, (e) => {
      e.type.name === r && (m = !0)
    }),
    !m)
  )
    return
  const h = c - l,
    u = n.doc.resolve(h).nodeAfter
  if (u?.type.name !== this.name) return
  let y = n.tr
  ;((y = y.insert(h, s.paragraph.createAndFill(""))), (y = y.setSelection(e.create(y.doc, h))))
  const B = n.apply(y)
  return (o.updateState(B), !0)
}