UNPKG

vue-files-preview-inno

Version:

A tool for previewing files such as doc, excel, pdf, image, markdown, txt, audio, and video and so on.

7,169 lines 275 kB
import { EditorState as Nt, StateEffect as Ot, Facet as M, Transaction as xn, Annotation as Sn, EditorSelection as k, Prec as ei, Text as q, ChangeSet as hs, RangeSet as V, findClusterBreak as bt, RangeValue as as, findColumn as Le, StateField as fs, MapMode as xt, combineConfig as ii, codePointAt as cs, countColumn as ds, codePointSize as vn, RangeSetBuilder as Cn, CharCategory as Mn } from "../../state/dist/index.mjs";
import { StyleModule as Ct } from "../../../style-mod/src/style-mod.mjs";
import { keyName as kn, base as An, shift as Dn } from "../../../w3c-keyname/index.mjs";
function zt(n) {
  let t;
  return n.nodeType == 11 ? t = n.getSelection ? n : n.ownerDocument : t = n, t.getSelection();
}
function Pe(n, t) {
  return t ? n == t || n.contains(t.nodeType != 1 ? t.parentNode : t) : !1;
}
function le(n, t) {
  if (!t.anchorNode)
    return !1;
  try {
    return Pe(n, t.anchorNode);
  } catch {
    return !1;
  }
}
function Mt(n) {
  return n.nodeType == 3 ? ut(n, 0, n.nodeValue.length).getClientRects() : n.nodeType == 1 ? n.getClientRects() : [];
}
function Vt(n, t, e, i) {
  return e ? gi(n, t, e, i, -1) || gi(n, t, e, i, 1) : !1;
}
function dt(n) {
  for (var t = 0; ; t++)
    if (n = n.previousSibling, !n)
      return t;
}
function pe(n) {
  return n.nodeType == 1 && /^(DIV|P|LI|UL|OL|BLOCKQUOTE|DD|DT|H\d|SECTION|PRE)$/.test(n.nodeName);
}
function gi(n, t, e, i, s) {
  for (; ; ) {
    if (n == e && t == i)
      return !0;
    if (t == (s < 0 ? 0 : Q(n))) {
      if (n.nodeName == "DIV")
        return !1;
      let o = n.parentNode;
      if (!o || o.nodeType != 1)
        return !1;
      t = dt(n) + (s < 0 ? 0 : 1), n = o;
    } else if (n.nodeType == 1) {
      if (n = n.childNodes[t + (s < 0 ? -1 : 0)], n.nodeType == 1 && n.contentEditable == "false")
        return !1;
      t = s < 0 ? Q(n) : 0;
    } else
      return !1;
  }
}
function Q(n) {
  return n.nodeType == 3 ? n.nodeValue.length : n.childNodes.length;
}
function qt(n, t) {
  let e = t ? n.left : n.right;
  return { left: e, right: e, top: n.top, bottom: n.bottom };
}
function On(n) {
  let t = n.visualViewport;
  return t ? {
    left: 0,
    right: t.width,
    top: 0,
    bottom: t.height
  } : {
    left: 0,
    right: n.innerWidth,
    top: 0,
    bottom: n.innerHeight
  };
}
function us(n, t) {
  let e = t.width / n.offsetWidth, i = t.height / n.offsetHeight;
  return (e > 0.995 && e < 1.005 || !isFinite(e) || Math.abs(t.width - n.offsetWidth) < 1) && (e = 1), (i > 0.995 && i < 1.005 || !isFinite(i) || Math.abs(t.height - n.offsetHeight) < 1) && (i = 1), { scaleX: e, scaleY: i };
}
function Tn(n, t, e, i, s, o, r, l) {
  let h = n.ownerDocument, f = h.defaultView || window;
  for (let a = n, c = !1; a && !c; )
    if (a.nodeType == 1) {
      let d, u = a == h.body, p = 1, g = 1;
      if (u)
        d = On(f);
      else {
        if (/^(fixed|sticky)$/.test(getComputedStyle(a).position) && (c = !0), a.scrollHeight <= a.clientHeight && a.scrollWidth <= a.clientWidth) {
          a = a.assignedSlot || a.parentNode;
          continue;
        }
        let w = a.getBoundingClientRect();
        ({ scaleX: p, scaleY: g } = us(a, w)), d = {
          left: w.left,
          right: w.left + a.clientWidth * p,
          top: w.top,
          bottom: w.top + a.clientHeight * g
        };
      }
      let m = 0, b = 0;
      if (s == "nearest")
        t.top < d.top ? (b = t.top - (d.top + r), e > 0 && t.bottom > d.bottom + b && (b = t.bottom - d.bottom + r)) : t.bottom > d.bottom && (b = t.bottom - d.bottom + r, e < 0 && t.top - b < d.top && (b = t.top - (d.top + r)));
      else {
        let w = t.bottom - t.top, S = d.bottom - d.top;
        b = (s == "center" && w <= S ? t.top + w / 2 - S / 2 : s == "start" || s == "center" && e < 0 ? t.top - r : t.bottom - S + r) - d.top;
      }
      if (i == "nearest" ? t.left < d.left ? (m = t.left - (d.left + o), e > 0 && t.right > d.right + m && (m = t.right - d.right + o)) : t.right > d.right && (m = t.right - d.right + o, e < 0 && t.left < d.left + m && (m = t.left - (d.left + o))) : m = (i == "center" ? t.left + (t.right - t.left) / 2 - (d.right - d.left) / 2 : i == "start" == l ? t.left - o : t.right - (d.right - d.left) + o) - d.left, m || b)
        if (u)
          f.scrollBy(m, b);
        else {
          let w = 0, S = 0;
          if (b) {
            let x = a.scrollTop;
            a.scrollTop += b / g, S = (a.scrollTop - x) * g;
          }
          if (m) {
            let x = a.scrollLeft;
            a.scrollLeft += m / p, w = (a.scrollLeft - x) * p;
          }
          t = {
            left: t.left - w,
            top: t.top - S,
            right: t.right - w,
            bottom: t.bottom - S
          }, w && Math.abs(w - m) < 1 && (i = "nearest"), S && Math.abs(S - b) < 1 && (s = "nearest");
        }
      if (u)
        break;
      (t.top < d.top || t.bottom > d.bottom || t.left < d.left || t.right > d.right) && (t = {
        left: Math.max(t.left, d.left),
        right: Math.min(t.right, d.right),
        top: Math.max(t.top, d.top),
        bottom: Math.min(t.bottom, d.bottom)
      }), a = a.assignedSlot || a.parentNode;
    } else if (a.nodeType == 11)
      a = a.host;
    else
      break;
}
function Rn(n) {
  let t = n.ownerDocument, e, i;
  for (let s = n.parentNode; s && !(s == t.body || e && i); )
    if (s.nodeType == 1)
      !i && s.scrollHeight > s.clientHeight && (i = s), !e && s.scrollWidth > s.clientWidth && (e = s), s = s.assignedSlot || s.parentNode;
    else if (s.nodeType == 11)
      s = s.host;
    else
      break;
  return { x: e, y: i };
}
class En {
  constructor() {
    this.anchorNode = null, this.anchorOffset = 0, this.focusNode = null, this.focusOffset = 0;
  }
  eq(t) {
    return this.anchorNode == t.anchorNode && this.anchorOffset == t.anchorOffset && this.focusNode == t.focusNode && this.focusOffset == t.focusOffset;
  }
  setRange(t) {
    let { anchorNode: e, focusNode: i } = t;
    this.set(e, Math.min(t.anchorOffset, e ? Q(e) : 0), i, Math.min(t.focusOffset, i ? Q(i) : 0));
  }
  set(t, e, i, s) {
    this.anchorNode = t, this.anchorOffset = e, this.focusNode = i, this.focusOffset = s;
  }
}
let mt = null;
function ps(n) {
  if (n.setActive)
    return n.setActive();
  if (mt)
    return n.focus(mt);
  let t = [];
  for (let e = n; e && (t.push(e, e.scrollTop, e.scrollLeft), e != e.ownerDocument); e = e.parentNode)
    ;
  if (n.focus(mt == null ? {
    get preventScroll() {
      return mt = { preventScroll: !0 }, !0;
    }
  } : void 0), !mt) {
    mt = !1;
    for (let e = 0; e < t.length; ) {
      let i = t[e++], s = t[e++], o = t[e++];
      i.scrollTop != s && (i.scrollTop = s), i.scrollLeft != o && (i.scrollLeft = o);
    }
  }
}
let mi;
function ut(n, t, e = t) {
  let i = mi || (mi = document.createRange());
  return i.setEnd(n, e), i.setStart(n, t), i;
}
function St(n, t, e, i) {
  let s = { key: t, code: t, keyCode: e, which: e, cancelable: !0 };
  i && ({ altKey: s.altKey, ctrlKey: s.ctrlKey, shiftKey: s.shiftKey, metaKey: s.metaKey } = i);
  let o = new KeyboardEvent("keydown", s);
  o.synthetic = !0, n.dispatchEvent(o);
  let r = new KeyboardEvent("keyup", s);
  return r.synthetic = !0, n.dispatchEvent(r), o.defaultPrevented || r.defaultPrevented;
}
function Bn(n) {
  for (; n; ) {
    if (n && (n.nodeType == 9 || n.nodeType == 11 && n.host))
      return n;
    n = n.assignedSlot || n.parentNode;
  }
  return null;
}
function gs(n) {
  for (; n.attributes.length; )
    n.removeAttributeNode(n.attributes[0]);
}
function Ln(n, t) {
  let e = t.focusNode, i = t.focusOffset;
  if (!e || t.anchorNode != e || t.anchorOffset != i)
    return !1;
  for (i = Math.min(i, Q(e)); ; )
    if (i) {
      if (e.nodeType != 1)
        return !1;
      let s = e.childNodes[i - 1];
      s.contentEditable == "false" ? i-- : (e = s, i = Q(e));
    } else {
      if (e == n)
        return !0;
      i = dt(e), e = e.parentNode;
    }
}
function ms(n) {
  return n.scrollTop > Math.max(1, n.scrollHeight - n.clientHeight - 4);
}
function bs(n, t) {
  for (let e = n, i = t; ; ) {
    if (e.nodeType == 3 && i > 0)
      return { node: e, offset: i };
    if (e.nodeType == 1 && i > 0) {
      if (e.contentEditable == "false")
        return null;
      e = e.childNodes[i - 1], i = Q(e);
    } else if (e.parentNode && !pe(e))
      i = dt(e), e = e.parentNode;
    else
      return null;
  }
}
function ys(n, t) {
  for (let e = n, i = t; ; ) {
    if (e.nodeType == 3 && i < e.nodeValue.length)
      return { node: e, offset: i };
    if (e.nodeType == 1 && i < e.childNodes.length) {
      if (e.contentEditable == "false")
        return null;
      e = e.childNodes[i], i = 0;
    } else if (e.parentNode && !pe(e))
      i = dt(e) + 1, e = e.parentNode;
    else
      return null;
  }
}
class N {
  constructor(t, e, i = !0) {
    this.node = t, this.offset = e, this.precise = i;
  }
  static before(t, e) {
    return new N(t.parentNode, dt(t), e);
  }
  static after(t, e) {
    return new N(t.parentNode, dt(t) + 1, e);
  }
}
const si = [];
class O {
  constructor() {
    this.parent = null, this.dom = null, this.flags = 2;
  }
  get overrideDOMText() {
    return null;
  }
  get posAtStart() {
    return this.parent ? this.parent.posBefore(this) : 0;
  }
  get posAtEnd() {
    return this.posAtStart + this.length;
  }
  posBefore(t) {
    let e = this.posAtStart;
    for (let i of this.children) {
      if (i == t)
        return e;
      e += i.length + i.breakAfter;
    }
    throw new RangeError("Invalid child in posBefore");
  }
  posAfter(t) {
    return this.posBefore(t) + t.length;
  }
  sync(t, e) {
    if (this.flags & 2) {
      let i = this.dom, s = null, o;
      for (let r of this.children) {
        if (r.flags & 7) {
          if (!r.dom && (o = s ? s.nextSibling : i.firstChild)) {
            let l = O.get(o);
            (!l || !l.parent && l.canReuseDOM(r)) && r.reuseDOM(o);
          }
          r.sync(t, e), r.flags &= -8;
        }
        if (o = s ? s.nextSibling : i.firstChild, e && !e.written && e.node == i && o != r.dom && (e.written = !0), r.dom.parentNode == i)
          for (; o && o != r.dom; )
            o = bi(o);
        else
          i.insertBefore(r.dom, o);
        s = r.dom;
      }
      for (o = s ? s.nextSibling : i.firstChild, o && e && e.node == i && (e.written = !0); o; )
        o = bi(o);
    } else if (this.flags & 1)
      for (let i of this.children)
        i.flags & 7 && (i.sync(t, e), i.flags &= -8);
  }
  reuseDOM(t) {
  }
  localPosFromDOM(t, e) {
    let i;
    if (t == this.dom)
      i = this.dom.childNodes[e];
    else {
      let s = Q(t) == 0 ? 0 : e == 0 ? -1 : 1;
      for (; ; ) {
        let o = t.parentNode;
        if (o == this.dom)
          break;
        s == 0 && o.firstChild != o.lastChild && (t == o.firstChild ? s = -1 : s = 1), t = o;
      }
      s < 0 ? i = t : i = t.nextSibling;
    }
    if (i == this.dom.firstChild)
      return 0;
    for (; i && !O.get(i); )
      i = i.nextSibling;
    if (!i)
      return this.length;
    for (let s = 0, o = 0; ; s++) {
      let r = this.children[s];
      if (r.dom == i)
        return o;
      o += r.length + r.breakAfter;
    }
  }
  domBoundsAround(t, e, i = 0) {
    let s = -1, o = -1, r = -1, l = -1;
    for (let h = 0, f = i, a = i; h < this.children.length; h++) {
      let c = this.children[h], d = f + c.length;
      if (f < t && d > e)
        return c.domBoundsAround(t, e, f);
      if (d >= t && s == -1 && (s = h, o = f), f > e && c.dom.parentNode == this.dom) {
        r = h, l = a;
        break;
      }
      a = d, f = d + c.breakAfter;
    }
    return {
      from: o,
      to: l < 0 ? i + this.length : l,
      startDOM: (s ? this.children[s - 1].dom.nextSibling : null) || this.dom.firstChild,
      endDOM: r < this.children.length && r >= 0 ? this.children[r].dom : null
    };
  }
  markDirty(t = !1) {
    this.flags |= 2, this.markParentsDirty(t);
  }
  markParentsDirty(t) {
    for (let e = this.parent; e; e = e.parent) {
      if (t && (e.flags |= 2), e.flags & 1)
        return;
      e.flags |= 1, t = !1;
    }
  }
  setParent(t) {
    this.parent != t && (this.parent = t, this.flags & 7 && this.markParentsDirty(!0));
  }
  setDOM(t) {
    this.dom != t && (this.dom && (this.dom.cmView = null), this.dom = t, t.cmView = this);
  }
  get rootView() {
    for (let t = this; ; ) {
      let e = t.parent;
      if (!e)
        return t;
      t = e;
    }
  }
  replaceChildren(t, e, i = si) {
    this.markDirty();
    for (let s = t; s < e; s++) {
      let o = this.children[s];
      o.parent == this && i.indexOf(o) < 0 && o.destroy();
    }
    i.length < 250 ? this.children.splice(t, e - t, ...i) : this.children = [].concat(this.children.slice(0, t), i, this.children.slice(e));
    for (let s = 0; s < i.length; s++)
      i[s].setParent(this);
  }
  ignoreMutation(t) {
    return !1;
  }
  ignoreEvent(t) {
    return !1;
  }
  childCursor(t = this.length) {
    return new ws(this.children, t, this.children.length);
  }
  childPos(t, e = 1) {
    return this.childCursor().findPos(t, e);
  }
  toString() {
    let t = this.constructor.name.replace("View", "");
    return t + (this.children.length ? "(" + this.children.join() + ")" : this.length ? "[" + (t == "Text" ? this.text : this.length) + "]" : "") + (this.breakAfter ? "#" : "");
  }
  static get(t) {
    return t.cmView;
  }
  get isEditable() {
    return !0;
  }
  get isWidget() {
    return !1;
  }
  get isHidden() {
    return !1;
  }
  merge(t, e, i, s, o, r) {
    return !1;
  }
  become(t) {
    return !1;
  }
  canReuseDOM(t) {
    return t.constructor == this.constructor && !((this.flags | t.flags) & 8);
  }
  // When this is a zero-length view with a side, this should return a
  // number <= 0 to indicate it is before its position, or a
  // number > 0 when after its position.
  getSide() {
    return 0;
  }
  destroy() {
    for (let t of this.children)
      t.parent == this && t.destroy();
    this.parent = null;
  }
}
O.prototype.breakAfter = 0;
function bi(n) {
  let t = n.nextSibling;
  return n.parentNode.removeChild(n), t;
}
class ws {
  constructor(t, e, i) {
    this.children = t, this.pos = e, this.i = i, this.off = 0;
  }
  findPos(t, e = 1) {
    for (; ; ) {
      if (t > this.pos || t == this.pos && (e > 0 || this.i == 0 || this.children[this.i - 1].breakAfter))
        return this.off = t - this.pos, this;
      let i = this.children[--this.i];
      this.pos -= i.length + i.breakAfter;
    }
  }
}
function xs(n, t, e, i, s, o, r, l, h) {
  let { children: f } = n, a = f.length ? f[t] : null, c = o.length ? o[o.length - 1] : null, d = c ? c.breakAfter : r;
  if (!(t == i && a && !r && !d && o.length < 2 && a.merge(e, s, o.length ? c : null, e == 0, l, h))) {
    if (i < f.length) {
      let u = f[i];
      u && (s < u.length || u.breakAfter && (c != null && c.breakAfter)) ? (t == i && (u = u.split(s), s = 0), !d && c && u.merge(0, s, c, !0, 0, h) ? o[o.length - 1] = u : ((s || u.children.length && !u.children[0].length) && u.merge(0, s, null, !1, 0, h), o.push(u))) : u != null && u.breakAfter && (c ? c.breakAfter = 1 : r = 1), i++;
    }
    for (a && (a.breakAfter = r, e > 0 && (!r && o.length && a.merge(e, a.length, o[0], !1, l, 0) ? a.breakAfter = o.shift().breakAfter : (e < a.length || a.children.length && a.children[a.children.length - 1].length == 0) && a.merge(e, a.length, null, !1, l, 0), t++)); t < i && o.length; )
      if (f[i - 1].become(o[o.length - 1]))
        i--, o.pop(), h = o.length ? 0 : l;
      else if (f[t].become(o[0]))
        t++, o.shift(), l = o.length ? 0 : h;
      else
        break;
    !o.length && t && i < f.length && !f[t - 1].breakAfter && f[i].merge(0, 0, f[t - 1], !1, l, h) && t--, (t < i || o.length) && n.replaceChildren(t, i, o);
  }
}
function Ss(n, t, e, i, s, o) {
  let r = n.childCursor(), { i: l, off: h } = r.findPos(e, 1), { i: f, off: a } = r.findPos(t, -1), c = t - e;
  for (let d of i)
    c += d.length;
  n.length += c, xs(n, f, a, l, h, i, 0, s, o);
}
let I = typeof navigator < "u" ? navigator : { userAgent: "", vendor: "", platform: "" }, He = typeof document < "u" ? document : { documentElement: { style: {} } };
const Ne = /* @__PURE__ */ /Edge\/(\d+)/.exec(I.userAgent), vs = /* @__PURE__ */ /MSIE \d/.test(I.userAgent), Ve = /* @__PURE__ */ /Trident\/(?:[7-9]|\d{2,})\..*rv:(\d+)/.exec(I.userAgent), xe = !!(vs || Ve || Ne), yi = !xe && /* @__PURE__ */ /gecko\/(\d+)/i.test(I.userAgent), Ce = !xe && /* @__PURE__ */ /Chrome\/(\d+)/.exec(I.userAgent), Pn = "webkitFontSmoothing" in He.documentElement.style, Cs = !xe && /* @__PURE__ */ /Apple Computer/.test(I.vendor), wi = Cs && (/* @__PURE__ */ /Mobile\/\w+/.test(I.userAgent) || I.maxTouchPoints > 2);
var y = {
  mac: wi || /* @__PURE__ */ /Mac/.test(I.platform),
  windows: /* @__PURE__ */ /Win/.test(I.platform),
  linux: /* @__PURE__ */ /Linux|X11/.test(I.platform),
  ie: xe,
  ie_version: vs ? He.documentMode || 6 : Ve ? +Ve[1] : Ne ? +Ne[1] : 0,
  gecko: yi,
  gecko_version: yi ? +(/* @__PURE__ */ /Firefox\/(\d+)/.exec(I.userAgent) || [0, 0])[1] : 0,
  chrome: !!Ce,
  chrome_version: Ce ? +Ce[1] : 0,
  ios: wi,
  android: /* @__PURE__ */ /Android\b/.test(I.userAgent),
  safari: Cs,
  webkit_version: Pn ? +(/* @__PURE__ */ /\bAppleWebKit\/(\d+)/.exec(I.userAgent) || [0, 0])[1] : 0,
  tabSize: He.documentElement.style.tabSize != null ? "tab-size" : "-moz-tab-size"
};
const Hn = 256;
class _ extends O {
  constructor(t) {
    super(), this.text = t;
  }
  get length() {
    return this.text.length;
  }
  createDOM(t) {
    this.setDOM(t || document.createTextNode(this.text));
  }
  sync(t, e) {
    this.dom || this.createDOM(), this.dom.nodeValue != this.text && (e && e.node == this.dom && (e.written = !0), this.dom.nodeValue = this.text);
  }
  reuseDOM(t) {
    t.nodeType == 3 && this.createDOM(t);
  }
  merge(t, e, i) {
    return this.flags & 8 || i && (!(i instanceof _) || this.length - (e - t) + i.length > Hn || i.flags & 8) ? !1 : (this.text = this.text.slice(0, t) + (i ? i.text : "") + this.text.slice(e), this.markDirty(), !0);
  }
  split(t) {
    let e = new _(this.text.slice(t));
    return this.text = this.text.slice(0, t), this.markDirty(), e.flags |= this.flags & 8, e;
  }
  localPosFromDOM(t, e) {
    return t == this.dom ? e : e ? this.text.length : 0;
  }
  domAtPos(t) {
    return new N(this.dom, t);
  }
  domBoundsAround(t, e, i) {
    return { from: i, to: i + this.length, startDOM: this.dom, endDOM: this.dom.nextSibling };
  }
  coordsAt(t, e) {
    return Nn(this.dom, t, e);
  }
}
class it extends O {
  constructor(t, e = [], i = 0) {
    super(), this.mark = t, this.children = e, this.length = i;
    for (let s of e)
      s.setParent(this);
  }
  setAttrs(t) {
    if (gs(t), this.mark.class && (t.className = this.mark.class), this.mark.attrs)
      for (let e in this.mark.attrs)
        t.setAttribute(e, this.mark.attrs[e]);
    return t;
  }
  canReuseDOM(t) {
    return super.canReuseDOM(t) && !((this.flags | t.flags) & 8);
  }
  reuseDOM(t) {
    t.nodeName == this.mark.tagName.toUpperCase() && (this.setDOM(t), this.flags |= 6);
  }
  sync(t, e) {
    this.dom ? this.flags & 4 && this.setAttrs(this.dom) : this.setDOM(this.setAttrs(document.createElement(this.mark.tagName))), super.sync(t, e);
  }
  merge(t, e, i, s, o, r) {
    return i && (!(i instanceof it && i.mark.eq(this.mark)) || t && o <= 0 || e < this.length && r <= 0) ? !1 : (Ss(this, t, e, i ? i.children.slice() : [], o - 1, r - 1), this.markDirty(), !0);
  }
  split(t) {
    let e = [], i = 0, s = -1, o = 0;
    for (let l of this.children) {
      let h = i + l.length;
      h > t && e.push(i < t ? l.split(t - i) : l), s < 0 && i >= t && (s = o), i = h, o++;
    }
    let r = this.length - t;
    return this.length = t, s > -1 && (this.children.length = s, this.markDirty()), new it(this.mark, e, r);
  }
  domAtPos(t) {
    return Ms(this, t);
  }
  coordsAt(t, e) {
    return As(this, t, e);
  }
}
function Nn(n, t, e) {
  let i = n.nodeValue.length;
  t > i && (t = i);
  let s = t, o = t, r = 0;
  t == 0 && e < 0 || t == i && e >= 0 ? y.chrome || y.gecko || (t ? (s--, r = 1) : o < i && (o++, r = -1)) : e < 0 ? s-- : o < i && o++;
  let l = ut(n, s, o).getClientRects();
  if (!l.length)
    return null;
  let h = l[(r ? r < 0 : e >= 0) ? 0 : l.length - 1];
  return y.safari && !r && h.width == 0 && (h = Array.prototype.find.call(l, (f) => f.width) || h), r ? qt(h, r < 0) : h || null;
}
class rt extends O {
  static create(t, e, i) {
    return new rt(t, e, i);
  }
  constructor(t, e, i) {
    super(), this.widget = t, this.length = e, this.side = i, this.prevWidget = null;
  }
  split(t) {
    let e = rt.create(this.widget, this.length - t, this.side);
    return this.length -= t, e;
  }
  sync(t) {
    (!this.dom || !this.widget.updateDOM(this.dom, t)) && (this.dom && this.prevWidget && this.prevWidget.destroy(this.dom), this.prevWidget = null, this.setDOM(this.widget.toDOM(t)), this.widget.editable || (this.dom.contentEditable = "false"));
  }
  getSide() {
    return this.side;
  }
  merge(t, e, i, s, o, r) {
    return i && (!(i instanceof rt) || !this.widget.compare(i.widget) || t > 0 && o <= 0 || e < this.length && r <= 0) ? !1 : (this.length = t + (i ? i.length : 0) + (this.length - e), !0);
  }
  become(t) {
    return t instanceof rt && t.side == this.side && this.widget.constructor == t.widget.constructor ? (this.widget.compare(t.widget) || this.markDirty(!0), this.dom && !this.prevWidget && (this.prevWidget = this.widget), this.widget = t.widget, this.length = t.length, !0) : !1;
  }
  ignoreMutation() {
    return !0;
  }
  ignoreEvent(t) {
    return this.widget.ignoreEvent(t);
  }
  get overrideDOMText() {
    if (this.length == 0)
      return q.empty;
    let t = this;
    for (; t.parent; )
      t = t.parent;
    let { view: e } = t, i = e && e.state.doc, s = this.posAtStart;
    return i ? i.slice(s, s + this.length) : q.empty;
  }
  domAtPos(t) {
    return (this.length ? t == 0 : this.side > 0) ? N.before(this.dom) : N.after(this.dom, t == this.length);
  }
  domBoundsAround() {
    return null;
  }
  coordsAt(t, e) {
    let i = this.widget.coordsAt(this.dom, t, e);
    if (i)
      return i;
    let s = this.dom.getClientRects(), o = null;
    if (!s.length)
      return null;
    let r = this.side ? this.side < 0 : t > 0;
    for (let l = r ? s.length - 1 : 0; o = s[l], !(t > 0 ? l == 0 : l == s.length - 1 || o.top < o.bottom); l += r ? -1 : 1)
      ;
    return qt(o, !r);
  }
  get isEditable() {
    return !1;
  }
  get isWidget() {
    return !0;
  }
  get isHidden() {
    return this.widget.isHidden;
  }
  destroy() {
    super.destroy(), this.dom && this.widget.destroy(this.dom);
  }
}
class kt extends O {
  constructor(t) {
    super(), this.side = t;
  }
  get length() {
    return 0;
  }
  merge() {
    return !1;
  }
  become(t) {
    return t instanceof kt && t.side == this.side;
  }
  split() {
    return new kt(this.side);
  }
  sync() {
    if (!this.dom) {
      let t = document.createElement("img");
      t.className = "cm-widgetBuffer", t.setAttribute("aria-hidden", "true"), this.setDOM(t);
    }
  }
  getSide() {
    return this.side;
  }
  domAtPos(t) {
    return this.side > 0 ? N.before(this.dom) : N.after(this.dom);
  }
  localPosFromDOM() {
    return 0;
  }
  domBoundsAround() {
    return null;
  }
  coordsAt(t) {
    return this.dom.getBoundingClientRect();
  }
  get overrideDOMText() {
    return q.empty;
  }
  get isHidden() {
    return !0;
  }
}
_.prototype.children = rt.prototype.children = kt.prototype.children = si;
function Ms(n, t) {
  let e = n.dom, { children: i } = n, s = 0;
  for (let o = 0; s < i.length; s++) {
    let r = i[s], l = o + r.length;
    if (!(l == o && r.getSide() <= 0)) {
      if (t > o && t < l && r.dom.parentNode == e)
        return r.domAtPos(t - o);
      if (t <= o)
        break;
      o = l;
    }
  }
  for (let o = s; o > 0; o--) {
    let r = i[o - 1];
    if (r.dom.parentNode == e)
      return r.domAtPos(r.length);
  }
  for (let o = s; o < i.length; o++) {
    let r = i[o];
    if (r.dom.parentNode == e)
      return r.domAtPos(0);
  }
  return new N(e, 0);
}
function ks(n, t, e) {
  let i, { children: s } = n;
  e > 0 && t instanceof it && s.length && (i = s[s.length - 1]) instanceof it && i.mark.eq(t.mark) ? ks(i, t.children[0], e - 1) : (s.push(t), t.setParent(n)), n.length += t.length;
}
function As(n, t, e) {
  let i = null, s = -1, o = null, r = -1;
  function l(f, a) {
    for (let c = 0, d = 0; c < f.children.length && d <= a; c++) {
      let u = f.children[c], p = d + u.length;
      p >= a && (u.children.length ? l(u, a - d) : (!o || o.isHidden && (e > 0 || Fn(o, u))) && (p > a || d == p && u.getSide() > 0) ? (o = u, r = a - d) : (d < a || d == p && u.getSide() < 0 && !u.isHidden) && (i = u, s = a - d)), d = p;
    }
  }
  l(n, t);
  let h = (e < 0 ? i : o) || i || o;
  return h ? h.coordsAt(Math.max(0, h == i ? s : r), e) : Vn(n);
}
function Vn(n) {
  let t = n.dom.lastChild;
  if (!t)
    return n.dom.getBoundingClientRect();
  let e = Mt(t);
  return e[e.length - 1] || null;
}
function Fn(n, t) {
  let e = n.coordsAt(0, 1), i = t.coordsAt(0, 1);
  return e && i && i.top < e.bottom;
}
function Fe(n, t) {
  for (let e in n)
    e == "class" && t.class ? t.class += " " + n.class : e == "style" && t.style ? t.style += ";" + n.style : t[e] = n[e];
  return t;
}
const xi = /* @__PURE__ */ Object.create(null);
function ge(n, t, e) {
  if (n == t)
    return !0;
  n || (n = xi), t || (t = xi);
  let i = Object.keys(n), s = Object.keys(t);
  if (i.length - (e && i.indexOf(e) > -1 ? 1 : 0) != s.length - (e && s.indexOf(e) > -1 ? 1 : 0))
    return !1;
  for (let o of i)
    if (o != e && (s.indexOf(o) == -1 || n[o] !== t[o]))
      return !1;
  return !0;
}
function We(n, t, e) {
  let i = !1;
  if (t)
    for (let s in t)
      e && s in e || (i = !0, s == "style" ? n.style.cssText = "" : n.removeAttribute(s));
  if (e)
    for (let s in e)
      t && t[s] == e[s] || (i = !0, s == "style" ? n.style.cssText = e[s] : n.setAttribute(s, e[s]));
  return i;
}
function Wn(n) {
  let t = /* @__PURE__ */ Object.create(null);
  for (let e = 0; e < n.attributes.length; e++) {
    let i = n.attributes[e];
    t[i.name] = i.value;
  }
  return t;
}
class Tt {
  /**
  Compare this instance to another instance of the same type.
  (TypeScript can't express this, but only instances of the same
  specific class will be passed to this method.) This is used to
  avoid redrawing widgets when they are replaced by a new
  decoration of the same type. The default implementation just
  returns `false`, which will cause new instances of the widget to
  always be redrawn.
  */
  eq(t) {
    return !1;
  }
  /**
  Update a DOM element created by a widget of the same type (but
  different, non-`eq` content) to reflect this widget. May return
  true to indicate that it could update, false to indicate it
  couldn't (in which case the widget will be redrawn). The default
  implementation just returns false.
  */
  updateDOM(t, e) {
    return !1;
  }
  /**
  @internal
  */
  compare(t) {
    return this == t || this.constructor == t.constructor && this.eq(t);
  }
  /**
  The estimated height this widget will have, to be used when
  estimating the height of content that hasn't been drawn. May
  return -1 to indicate you don't know. The default implementation
  returns -1.
  */
  get estimatedHeight() {
    return -1;
  }
  /**
  For inline widgets that are displayed inline (as opposed to
  `inline-block`) and introduce line breaks (through `<br>` tags
  or textual newlines), this must indicate the amount of line
  breaks they introduce. Defaults to 0.
  */
  get lineBreaks() {
    return 0;
  }
  /**
  Can be used to configure which kinds of events inside the widget
  should be ignored by the editor. The default is to ignore all
  events.
  */
  ignoreEvent(t) {
    return !0;
  }
  /**
  Override the way screen coordinates for positions at/in the
  widget are found. `pos` will be the offset into the widget, and
  `side` the side of the position that is being queried—less than
  zero for before, greater than zero for after, and zero for
  directly at that position.
  */
  coordsAt(t, e, i) {
    return null;
  }
  /**
  @internal
  */
  get isHidden() {
    return !1;
  }
  /**
  @internal
  */
  get editable() {
    return !1;
  }
  /**
  This is called when the an instance of the widget is removed
  from the editor view.
  */
  destroy(t) {
  }
}
var F = /* @__PURE__ */ function(n) {
  return n[n.Text = 0] = "Text", n[n.WidgetBefore = 1] = "WidgetBefore", n[n.WidgetAfter = 2] = "WidgetAfter", n[n.WidgetRange = 3] = "WidgetRange", n;
}(F || (F = {}));
class E extends as {
  constructor(t, e, i, s) {
    super(), this.startSide = t, this.endSide = e, this.widget = i, this.spec = s;
  }
  /**
  @internal
  */
  get heightRelevant() {
    return !1;
  }
  /**
  Create a mark decoration, which influences the styling of the
  content in its range. Nested mark decorations will cause nested
  DOM elements to be created. Nesting order is determined by
  precedence of the [facet](https://codemirror.net/6/docs/ref/#view.EditorView^decorations), with
  the higher-precedence decorations creating the inner DOM nodes.
  Such elements are split on line boundaries and on the boundaries
  of lower-precedence decorations.
  */
  static mark(t) {
    return new Yt(t);
  }
  /**
  Create a widget decoration, which displays a DOM element at the
  given position.
  */
  static widget(t) {
    let e = Math.max(-1e4, Math.min(1e4, t.side || 0)), i = !!t.block;
    return e += i && !t.inlineOrder ? e > 0 ? 3e8 : -4e8 : e > 0 ? 1e8 : -1e8, new ht(t, e, e, i, t.widget || null, !1);
  }
  /**
  Create a replace decoration which replaces the given range with
  a widget, or simply hides it.
  */
  static replace(t) {
    let e = !!t.block, i, s;
    if (t.isBlockGap)
      i = -5e8, s = 4e8;
    else {
      let { start: o, end: r } = Ds(t, e);
      i = (o ? e ? -3e8 : -1 : 5e8) - 1, s = (r ? e ? 2e8 : 1 : -6e8) + 1;
    }
    return new ht(t, i, s, e, t.widget || null, !0);
  }
  /**
  Create a line decoration, which can add DOM attributes to the
  line starting at the given position.
  */
  static line(t) {
    return new jt(t);
  }
  /**
  Build a [`DecorationSet`](https://codemirror.net/6/docs/ref/#view.DecorationSet) from the given
  decorated range or ranges. If the ranges aren't already sorted,
  pass `true` for `sort` to make the library sort them for you.
  */
  static set(t, e = !1) {
    return V.of(t, e);
  }
  /**
  @internal
  */
  hasHeight() {
    return this.widget ? this.widget.estimatedHeight > -1 : !1;
  }
}
E.none = V.empty;
class Yt extends E {
  constructor(t) {
    let { start: e, end: i } = Ds(t);
    super(e ? -1 : 5e8, i ? 1 : -6e8, null, t), this.tagName = t.tagName || "span", this.class = t.class || "", this.attrs = t.attributes || null;
  }
  eq(t) {
    var e, i;
    return this == t || t instanceof Yt && this.tagName == t.tagName && (this.class || ((e = this.attrs) === null || e === void 0 ? void 0 : e.class)) == (t.class || ((i = t.attrs) === null || i === void 0 ? void 0 : i.class)) && ge(this.attrs, t.attrs, "class");
  }
  range(t, e = t) {
    if (t >= e)
      throw new RangeError("Mark decorations may not be empty");
    return super.range(t, e);
  }
}
Yt.prototype.point = !1;
class jt extends E {
  constructor(t) {
    super(-2e8, -2e8, null, t);
  }
  eq(t) {
    return t instanceof jt && this.spec.class == t.spec.class && ge(this.spec.attributes, t.spec.attributes);
  }
  range(t, e = t) {
    if (e != t)
      throw new RangeError("Line decoration ranges must be zero-length");
    return super.range(t, e);
  }
}
jt.prototype.mapMode = xt.TrackBefore;
jt.prototype.point = !0;
class ht extends E {
  constructor(t, e, i, s, o, r) {
    super(e, i, o, t), this.block = s, this.isReplace = r, this.mapMode = s ? e <= 0 ? xt.TrackBefore : xt.TrackAfter : xt.TrackDel;
  }
  // Only relevant when this.block == true
  get type() {
    return this.startSide != this.endSide ? F.WidgetRange : this.startSide <= 0 ? F.WidgetBefore : F.WidgetAfter;
  }
  get heightRelevant() {
    return this.block || !!this.widget && (this.widget.estimatedHeight >= 5 || this.widget.lineBreaks > 0);
  }
  eq(t) {
    return t instanceof ht && zn(this.widget, t.widget) && this.block == t.block && this.startSide == t.startSide && this.endSide == t.endSide;
  }
  range(t, e = t) {
    if (this.isReplace && (t > e || t == e && this.startSide > 0 && this.endSide <= 0))
      throw new RangeError("Invalid range for replacement decoration");
    if (!this.isReplace && e != t)
      throw new RangeError("Widget decorations can only have zero-length ranges");
    return super.range(t, e);
  }
}
ht.prototype.point = !0;
function Ds(n, t = !1) {
  let { inclusiveStart: e, inclusiveEnd: i } = n;
  return e == null && (e = n.inclusive), i == null && (i = n.inclusive), { start: e ?? t, end: i ?? t };
}
function zn(n, t) {
  return n == t || !!(n && t && n.compare(t));
}
function he(n, t, e, i = 0) {
  let s = e.length - 1;
  s >= 0 && e[s] + i >= n ? e[s] = Math.max(e[s], t) : e.push(n, t);
}
class L extends O {
  constructor() {
    super(...arguments), this.children = [], this.length = 0, this.prevAttrs = void 0, this.attrs = null, this.breakAfter = 0;
  }
  // Consumes source
  merge(t, e, i, s, o, r) {
    if (i) {
      if (!(i instanceof L))
        return !1;
      this.dom || i.transferDOM(this);
    }
    return s && this.setDeco(i ? i.attrs : null), Ss(this, t, e, i ? i.children.slice() : [], o, r), !0;
  }
  split(t) {
    let e = new L();
    if (e.breakAfter = this.breakAfter, this.length == 0)
      return e;
    let { i, off: s } = this.childPos(t);
    s && (e.append(this.children[i].split(s), 0), this.children[i].merge(s, this.children[i].length, null, !1, 0, 0), i++);
    for (let o = i; o < this.children.length; o++)
      e.append(this.children[o], 0);
    for (; i > 0 && this.children[i - 1].length == 0; )
      this.children[--i].destroy();
    return this.children.length = i, this.markDirty(), this.length = t, e;
  }
  transferDOM(t) {
    this.dom && (this.markDirty(), t.setDOM(this.dom), t.prevAttrs = this.prevAttrs === void 0 ? this.attrs : this.prevAttrs, this.prevAttrs = void 0, this.dom = null);
  }
  setDeco(t) {
    ge(this.attrs, t) || (this.dom && (this.prevAttrs = this.attrs, this.markDirty()), this.attrs = t);
  }
  append(t, e) {
    ks(this, t, e);
  }
  // Only called when building a line view in ContentBuilder
  addLineDeco(t) {
    let e = t.spec.attributes, i = t.spec.class;
    e && (this.attrs = Fe(e, this.attrs || {})), i && (this.attrs = Fe({ class: i }, this.attrs || {}));
  }
  domAtPos(t) {
    return Ms(this, t);
  }
  reuseDOM(t) {
    t.nodeName == "DIV" && (this.setDOM(t), this.flags |= 6);
  }
  sync(t, e) {
    var i;
    this.dom ? this.flags & 4 && (gs(this.dom), this.dom.className = "cm-line", this.prevAttrs = this.attrs ? null : void 0) : (this.setDOM(document.createElement("div")), this.dom.className = "cm-line", this.prevAttrs = this.attrs ? null : void 0), this.prevAttrs !== void 0 && (We(this.dom, this.prevAttrs, this.attrs), this.dom.classList.add("cm-line"), this.prevAttrs = void 0), super.sync(t, e);
    let s = this.dom.lastChild;
    for (; s && O.get(s) instanceof it; )
      s = s.lastChild;
    if (!s || !this.length || s.nodeName != "BR" && ((i = O.get(s)) === null || i === void 0 ? void 0 : i.isEditable) == !1 && (!y.ios || !this.children.some((o) => o instanceof _))) {
      let o = document.createElement("BR");
      o.cmIgnore = !0, this.dom.appendChild(o);
    }
  }
  measureTextSize() {
    if (this.children.length == 0 || this.length > 20)
      return null;
    let t = 0, e;
    for (let i of this.children) {
      if (!(i instanceof _) || /[^ -~]/.test(i.text))
        return null;
      let s = Mt(i.dom);
      if (s.length != 1)
        return null;
      t += s[0].width, e = s[0].height;
    }
    return t ? {
      lineHeight: this.dom.getBoundingClientRect().height,
      charWidth: t / this.length,
      textHeight: e
    } : null;
  }
  coordsAt(t, e) {
    let i = As(this, t, e);
    if (!this.children.length && i && this.parent) {
      let { heightOracle: s } = this.parent.view.viewState, o = i.bottom - i.top;
      if (Math.abs(o - s.lineHeight) < 2 && s.textHeight < o) {
        let r = (o - s.textHeight) / 2;
        return { top: i.top + r, bottom: i.bottom - r, left: i.left, right: i.left };
      }
    }
    return i;
  }
  become(t) {
    return t instanceof L && this.children.length == 0 && t.children.length == 0 && ge(this.attrs, t.attrs) && this.breakAfter == t.breakAfter;
  }
  covers() {
    return !0;
  }
  static find(t, e) {
    for (let i = 0, s = 0; i < t.children.length; i++) {
      let o = t.children[i], r = s + o.length;
      if (r >= e) {
        if (o instanceof L)
          return o;
        if (r > e)
          break;
      }
      s = r + o.breakAfter;
    }
    return null;
  }
}
class et extends O {
  constructor(t, e, i) {
    super(), this.widget = t, this.length = e, this.deco = i, this.breakAfter = 0, this.prevWidget = null;
  }
  merge(t, e, i, s, o, r) {
    return i && (!(i instanceof et) || !this.widget.compare(i.widget) || t > 0 && o <= 0 || e < this.length && r <= 0) ? !1 : (this.length = t + (i ? i.length : 0) + (this.length - e), !0);
  }
  domAtPos(t) {
    return t == 0 ? N.before(this.dom) : N.after(this.dom, t == this.length);
  }
  split(t) {
    let e = this.length - t;
    this.length = t;
    let i = new et(this.widget, e, this.deco);
    return i.breakAfter = this.breakAfter, i;
  }
  get children() {
    return si;
  }
  sync(t) {
    (!this.dom || !this.widget.updateDOM(this.dom, t)) && (this.dom && this.prevWidget && this.prevWidget.destroy(this.dom), this.prevWidget = null, this.setDOM(this.widget.toDOM(t)), this.widget.editable || (this.dom.contentEditable = "false"));
  }
  get overrideDOMText() {
    return this.parent ? this.parent.view.state.doc.slice(this.posAtStart, this.posAtEnd) : q.empty;
  }
  domBoundsAround() {
    return null;
  }
  become(t) {
    return t instanceof et && t.widget.constructor == this.widget.constructor ? (t.widget.compare(this.widget) || this.markDirty(!0), this.dom && !this.prevWidget && (this.prevWidget = this.widget), this.widget = t.widget, this.length = t.length, this.deco = t.deco, this.breakAfter = t.breakAfter, !0) : !1;
  }
  ignoreMutation() {
    return !0;
  }
  ignoreEvent(t) {
    return this.widget.ignoreEvent(t);
  }
  get isEditable() {
    return !1;
  }
  get isWidget() {
    return !0;
  }
  coordsAt(t, e) {
    let i = this.widget.coordsAt(this.dom, t, e);
    return i || (this.widget instanceof ze ? null : qt(this.dom.getBoundingClientRect(), this.length ? t == 0 : e <= 0));
  }
  destroy() {
    super.destroy(), this.dom && this.widget.destroy(this.dom);
  }
  covers(t) {
    let { startSide: e, endSide: i } = this.deco;
    return e == i ? !1 : t < 0 ? e < 0 : i > 0;
  }
}
class ze extends Tt {
  constructor(t) {
    super(), this.height = t;
  }
  toDOM() {
    let t = document.createElement("div");
    return t.className = "cm-gap", this.updateDOM(t), t;
  }
  eq(t) {
    return t.height == this.height;
  }
  updateDOM(t) {
    return t.style.height = this.height + "px", !0;
  }
  get editable() {
    return !0;
  }
  get estimatedHeight() {
    return this.height;
  }
  ignoreEvent() {
    return !1;
  }
}
class Ft {
  constructor(t, e, i, s) {
    this.doc = t, this.pos = e, this.end = i, this.disallowBlockEffectsFor = s, this.content = [], this.curLine = null, this.breakAtStart = 0, this.pendingBuffer = 0, this.bufferMarks = [], this.atCursorPos = !0, this.openStart = -1, this.openEnd = -1, this.text = "", this.textOff = 0, this.cursor = t.iter(), this.skip = e;
  }
  posCovered() {
    if (this.content.length == 0)
      return !this.breakAtStart && this.doc.lineAt(this.pos).from != this.pos;
    let t = this.content[this.content.length - 1];
    return !(t.breakAfter || t instanceof et && t.deco.endSide < 0);
  }
  getLine() {
    return this.curLine || (this.content.push(this.curLine = new L()), this.atCursorPos = !0), this.curLine;
  }
  flushBuffer(t = this.bufferMarks) {
    this.pendingBuffer && (this.curLine.append(Ut(new kt(-1), t), t.length), this.pendingBuffer = 0);
  }
  addBlockWidget(t) {
    this.flushBuffer(), this.curLine = null, this.content.push(t);
  }
  finish(t) {
    this.pendingBuffer && t <= this.bufferMarks.length ? this.flushBuffer() : this.pendingBuffer = 0, !this.posCovered() && !(t && this.content.length && this.content[this.content.length - 1] instanceof et) && this.getLine();
  }
  buildText(t, e, i) {
    for (; t > 0; ) {
      if (this.textOff == this.text.length) {
        let { value: o, lineBreak: r, done: l } = this.cursor.next(this.skip);
        if (this.skip = 0, l)
          throw new Error("Ran out of text content when drawing inline views");
        if (r) {
          this.posCovered() || this.getLine(), this.content.length ? this.content[this.content.length - 1].breakAfter = 1 : this.breakAtStart = 1, this.flushBuffer(), this.curLine = null, this.atCursorPos = !0, t--;
          continue;
        } else
          this.text = o, this.textOff = 0;
      }
      let s = Math.min(
        this.text.length - this.textOff,
        t,
        512
        /* T.Chunk */
      );
      this.flushBuffer(e.slice(e.length - i)), this.getLine().append(Ut(new _(this.text.slice(this.textOff, this.textOff + s)), e), i), this.atCursorPos = !0, this.textOff += s, t -= s, i = 0;
    }
  }
  span(t, e, i, s) {
    this.buildText(e - t, i, s), this.pos = e, this.openStart < 0 && (this.openStart = s);
  }
  point(t, e, i, s, o, r) {
    if (this.disallowBlockEffectsFor[r] && i instanceof ht) {
      if (i.block)
        throw new RangeError("Block decorations may not be specified via plugins");
      if (e > this.doc.lineAt(this.pos).to)
        throw new RangeError("Decorations that replace line breaks may not be specified via plugins");
    }
    let l = e - t;
    if (i instanceof ht)
      if (i.block)
        i.startSide > 0 && !this.posCovered() && this.getLine(), this.addBlockWidget(new et(i.widget || At.block, l, i));
      else {
        let h = rt.create(i.widget || At.inline, l, l ? 0 : i.startSide), f = this.atCursorPos && !h.isEditable && o <= s.length && (t < e || i.startSide > 0), a = !h.isEditable && (t < e || o > s.length || i.startSide <= 0), c = this.getLine();
        this.pendingBuffer == 2 && !f && !h.isEditable && (this.pendingBuffer = 0), this.flushBuffer(s), f && (c.append(Ut(new kt(1), s), o), o = s.length + Math.max(0, o - s.length)), c.append(Ut(h, s), o), this.atCursorPos = a, this.pendingBuffer = a ? t < e || o > s.length ? 1 : 2 : 0, this.pendingBuffer && (this.bufferMarks = s.slice());
      }
    else this.doc.lineAt(this.pos).from == this.pos && this.getLine().addLineDeco(i);
    l && (this.textOff + l <= this.text.length ? this.textOff += l : (this.skip += l - (this.text.length - this.textOff), this.text = "", this.textOff = 0), this.pos = e), this.openStart < 0 && (this.openStart = o);
  }
  static build(t, e, i, s, o) {
    let r = new Ft(t, e, i, o);
    return r.openEnd = V.spans(s, e, i, r), r.openStart < 0 && (r.openStart = r.openEnd), r.finish(r.openEnd), r;
  }
}
function Ut(n, t) {
  for (let e of t)
    n = new it(e, [n], n.length);
  return n;
}
class At extends Tt {
  constructor(t) {
    super(), this.tag = t;
  }
  eq(t) {
    return t.tag == this.tag;
  }
  toDOM() {
    return document.createElement(this.tag);
  }
  updateDOM(t) {
    return t.nodeName.toLowerCase() == this.tag;
  }
  get isHidden() {
    return !0;
  }
}
At.inline = /* @__PURE__ */ new At("span");
At.block = /* @__PURE__ */ new At("div");
var B = /* @__PURE__ */ function(n) {
  return n[n.LTR = 0] = "LTR", n[n.RTL = 1] = "RTL", n;
}(B || (B = {}));
const pt = B.LTR, ni = B.RTL;
function Os(n) {
  let t = [];
  for (let e = 0; e < n.length; e++)
    t.push(1 << +n[e]);
  return t;
}
const In = /* @__PURE__ */ Os("88888888888888888888888888888888888666888888787833333333337888888000000000000000000000000008888880000000000000000000000000088888888888888888888888888888888888887866668888088888663380888308888800000000000000000000000800000000000000000000000000000008"), Kn = /* @__PURE__ */ Os("4444448826627288999999999992222222222222222222222222222222222222222222222229999999999999999999994444444444644222822222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222999999949999999229989999223333333333"), Ie = /* @__PURE__ */ Object.create(null), G = [];
for (let n of ["()", "[]", "{}"]) {
  let t = /* @__PURE__ */ n.charCodeAt(0), e = /* @__PURE__ */ n.charCodeAt(1);
  Ie[t] = e, Ie[e] = -t;
}
function Ts(n) {
  return n <= 247 ? In[n] : 1424 <= n && n <= 1524 ? 2 : 1536 <= n && n <= 1785 ? Kn[n - 1536] : 1774 <= n && n <= 2220 ? 4 : 8192 <= n && n <= 8204 ? 256 : 64336 <= n && n <= 65023 ? 4 : 1;
}
const qn = /[\u0590-\u05f4\u0600-\u06ff\u0700-\u08ac\ufb50-\ufdff]/;
class lt {
  /**
  The direction of this span.
  */
  get dir() {
    return this.level % 2 ? ni : pt;
  }
  /**
  @internal
  */
  constructor(t, e, i) {
    this.from = t, this.to = e, this.level = i;
  }
  /**
  @internal
  */
  side(t, e) {
    return this.dir == e == t ? this.to : this.from;
  }
  /**
  @internal
  */
  forward(t, e) {
    return t == (this.dir == e);
  }
  /**
  @internal
  */
  static find(t, e, i, s) {
    let o = -1;
    for (let r = 0; r < t.length; r++) {
      let l = t[r];
      if (l.from <= e && l.to >= e) {
        if (l.level == i)
          return r;
        (o < 0 || (s != 0 ? s < 0 ? l.from < e : l.to > e : t[o].level > l.level)) && (o = r);
      }
    }
    if (o < 0)
      throw new RangeError("Index out of range");
    return o;
  }
}
function Rs(n, t) {
  if (n.length != t.length)
    return !1;
  for (let e = 0; e < n.length; e++) {
    let i = n[e], s = t[e];
    if (i.from != s.from || i.to != s.to || i.direction != s.direction || !Rs(i.inner, s.inner))
      return !1;
  }
  return !0;
}
const D = [];
function Yn(n, t, e, i, s) {
  for (let o = 0; o <= i.length; o++) {
    let r = o ? i[o - 1].to : t, l = o < i.length ? i[o].from : e, h = o ? 256 : s;
    for (let f = r, a = h, c = h; f < l; f++) {
      let d = Ts(n.charCodeAt(f));
      d == 512 ? d = a : d == 8 && c == 4 && (d = 16), D[f] = d == 4 ? 2 : d, d & 7 && (c = d), a = d;
    }
    for (let f = r, a = h, c = h; f < l; f++) {
      let d = D[f];
      if (d == 128)
        f < l - 1 && a == D[f + 1] && a & 24 ? d = D[f] = a : D[f] = 256;
      else if (d == 64) {
        let u = f + 1;
        for (; u < l && D[u] == 64; )
          u++;
        let p = f && a == 8 || u < e && D[u] == 8 ? c == 1 ? 1 : 8 : 256;
        for (let g = f; g < u; g++)
          D[g] = p;
        f = u - 1;
      } else d == 8 && c == 1 && (D[f] = 1);
      a = d, d & 7 && (c = d);
    }
  }
}
function jn(n, t, e, i, s) {
  let o = s == 1 ? 2 : 1;
  for (let r = 0, l = 0, h = 0; r <= i.length; r++) {
    let f = r ? i[r - 1].to : t, a = r < i.length ? i[r].from : e;
    for (let c = f, d, u, p; c < a; c++)
      if (u = Ie[d = n.charCodeAt(c)])
        if (u < 0) {
          for (let g = l - 3; g >= 0; g -= 3)
            if (G[g + 1] == -u) {
              let m = G[g + 2], b = m & 2 ? s : m & 4 ? m & 1 ? o : s : 0;
              b && (D[c] = D[G[g]] = b), l = g;
              break;
            }
        } else {
          if (G.length == 189)
            break;
          G[l++] = c, G[l++] = d, G[l++] = h;
        }
      else if ((p = D[c]) == 2 || p == 1) {
        let g = p == s;
        h = g ? 0 : 1;
        for (let m = l - 3; m >= 0; m -= 3) {
          let b = G[m + 2];
          if (b & 2)
            break;
          if (g)
            G[m + 2] |= 2;
          else {
            if (b & 4)
              break;
            G[m + 2] |= 4;
          }
        }
      }
  }
}
function _n(n, t, e, i) {
  for (let s = 0, o = i; s <= e.length; s++) {
    let r = s ? e[s - 1].to : n, l = s < e.length ? e[s].from : t;
    for (let h = r; h < l; ) {
      let f = D[h];
      if (f == 256) {
        let a = h + 1;
        for (; ; )
          if (a == l) {
            if (s == e.length)
              break;
            a = e[s++].to, l = s < e.length ? e[s].from : t;
          } else if (D[a] == 256)
            a++;
          else
            break;
        let c = o == 1, d = (a < t ? D[a] : i) == 1, u = c == d ? c ? 1 : 2 : i;
        for (let p = a, g = s, m = g ? e[g - 1].to : n; p > h; )
          p == m && (p = e[--g].from, m = g ? e[g - 1].to : n), D[--p] = u;
        h = a;
      } else
        o = f, h++;
    }
  }
}
function Ke(n, t, e, i, s, o, r) {
  let l = i % 2 ? 2 : 1;
  if (i % 2 == s % 2)
    for (let h = t, f = 0; h < e; ) {
      let a = !0, c = !1;
      if (f == o.length || h < o[f].from) {
        let g = D[h];
        g != l && (a = !1, c = g == 16);
      }
      let d = !a && l == 1 ? [] : null, u = a ? i : i + 1, p = h;
      t: for (; ; )
        if (f < o.length && p == o[f].from) {
          if (c)
            break t;
          let g = o[f];
          if (!a)
            for (let m = g.to, b = f + 1; ; ) {
              if (m == e)
                break t;
              if (b < o.length && o[b].from == m)
                m = o[b++].to;
              else {
                if (D[m] == l)
                  break t;
                break;
              }
            }
          if (f++, d)
            d.push(g);
          else {
            g.from > h && r.push(new lt(h, g.from, u));
            let m = g.direction == pt != !(u % 2);
            qe(n, m ? i + 1 : i, s, g.inner, g.from, g.to, r), h = g.to;
          }
          p = g.to;
        } else {
          if (p == e || (a ? D[p] != l : D[p] == l))
            break;
          p++;
        }
      d ? Ke(n, h, p, i + 1, s, d, r) : h < p && r.push(new lt(h, p, u)), h = p;
    }
  else
    for (let h = e, f = o.length; h > t; ) {
      let a = !0, c = !1;
      if (!f || h > o[f - 1].to) {
        let g = D[h - 1];
        g != l && (a = !1, c = g == 16);
      }
      let d = !a && l == 1 ? [] : null, u = a ? i : i + 1, p = h;
      t: for (; ; )
        if (f && p == o[f - 1].to) {
          if (c)
            break t;
          let g = o[--f];
          if (!a)
            for (let m = g.from, b = f; ; ) {
              if (m == t)
                break t;
              if (b && o[b - 1].to == m)
                m = o[--b].from;
              else {
                if (D[m - 1] == l)
                  break t;
                break;
              }
            }
          if (d)
            d.push(g);
          else {
            g.to < h && r.push(new lt(g.to, h, u));
            let m = g.direction == pt != !(u % 2);
            qe(n, m ? i + 1 : i, s, g.inner, g.from, g.to, r), h = g.from;
          }
          p = g.from;
        } else {
          if (p == t || (a ? D[p - 1] != l : D[p - 1] == l))
            break;
          p--;
        }
      d ? Ke(n, p, h, i + 1, s, d, r) : p < h && r.push(new lt(p, h, u)), h = p;
    }
}
function qe(n, t, e, i, s, o, r) {
  let l = t % 2 ? 2 : 1;
  Yn(n, s, o, i, l), jn(n, s, o, i, l), _n(s, o, i, l), Ke(n, s, o, t, e, i, r);
}
function Xn(n, t, e) {
  if (!n)
    return [new lt(0, 0, t == ni ? 1 : 0)];
  if (t == pt && !e.length && !qn.test(n))
    return Es(n.length);
  if (e.length)
    for (; n.length > D.length; )
      D[D.length] = 256;
  let i = [], s = t == pt ? 0 : 1;
  return qe(n, s, s, e, 0, n.length, i), i;
}
function Es(n) {
  return [new lt(0, n, 0)];
}
let Bs = "";
function Gn(n, t, e, i, s) {
  var o;
  let r = i.head - n.from, l = lt.find(t, r, (o = i.bidiLevel) !== null && o !== void 0 ? o : -1, i.assoc), h = t[l], f = h.side(s, e);
  if (r == f) {
    let d = l += s ? 1 : -1;
    if (d < 0 || d >= t.length)
      return null;
    h = t[l = d], r = h.side(!s, e), f = h.side(s, e);
  }
  let a = bt(n.text, r, h.forward(s, e));
  (a < h.from || a > h.to) && (a = f), Bs = n.text.slice(Math.min(r, a), Math.max(r, a));
  let c = l == (s ? t.length - 1 : 0) ? null : t[l + (s ? 1 : -1)];
  return c && a == f && c.level + (s ? 0 : 1) < h.level ? k.cursor(c.side(!s, e) + n.from, c.forward(s, e) ? 1 : -1, c.level) : k.cursor(a + n.from, h.forward(s, e) ? -1 : 1, h.level);
}
function $n(n, t, e) {
  for (let i = t; i < e; i++) {
    let s = Ts(n.charCodeAt(i));
    if (s == 1)
      return pt;
    if (s == 2 || s == 4)
      return ni;
  }
  return pt;
}
const Ls = /* @__PURE__ */ M.define(), Ps = /* @__PURE__ */ M.define(), Hs = /* @__PURE__ */ M.define(), Ns = /* @__PURE__ */ M.define(), Ye = /* @__PURE__ */ M.define(), Vs = /* @__PURE__ */ M.define(), Fs = /* @__PURE__ */ M.define(), oi = /* @__PURE__ */ M.define(), ri = /* @__PURE__ */ M.define(), Ws = /* @__PURE__ */ M.define({
  combine: (n) => n.some((t) => t)
}), zs = /* @__PURE__ */ M.define({
  combine: (n) => n.some((t) => t)
}), Is = /* @__PURE__ */ M.define();
class vt {
  constructor(t, e = "nearest", i = "nearest", s = 5, o = 5, r = !1) {
    this.range = t, this.y = e, this.x = i, this.yMargin = s, this.xMargin = o, this.isSnapshot = r;
  }
  map(t) {
    return t.empty ? this : new vt(this.range.map(t), this.y, this.x, this.yMargin, this.xMargin, this.isSnapshot);
  }
  clip(t) {
    return this.range.to <= t.doc.length ? this : new vt(k.cursor(t.doc.length), this.y, this.x, this.yMargin, this.xMargin, this.isSnapshot);
  }
}
const Qt = /* @__PURE__ */ Ot.define({ map: (n, t) => n.map(t) }), Ks = /* @__PURE__ */ Ot.define();
function U(n, t, e) {
  let i = n.facet(Ns);
  i.length ? i[0](t) : window.onerror && window.onerror(String(t), e, void 0, void 0, t) || (e ? console.error(e + ":", t) : console.error(t));
}
const tt = /* @__PURE__ */ M.define({ combine: (n) => n.length ? n[0] : !0 });
let Un = 0;
const yt = /* @__PURE__ */ M.define({
  combine(n) {
    return n.filter((t, e) => {
      for (let i = 0; i < e; i++)
        if (n[i].plugin == t.plugin)
          return !1;
      return !0;
    });
  }
});
class z {
  constructor(t, e, i, s, o) {
    this.id = t, this.create = e, this.domEventHandlers = i, this.domEventObservers = s, this.baseExtensions = o(this), this.extension = this.baseExtensions.concat(yt.of({ plugin: this, arg: void 0 }));
  }
  /**
  Create an extension for this plugin with the given argument.
  */
  of(t) {
    return this.baseExtensions.concat(yt.of({ plugin: this, arg: t }));
  }
  /**
  Define a plugin from a constructor function that creates the
  plugin's value, given an editor view.
  */
  static define(t, e) {
    const { eventHandlers: i, eventObservers: s, provide: o, decorations: r } = e || {};
    return new z(Un++, t, i, s, (l) => {
      let h = [];
      return r && h.push(It.of((f) => {
        let a = f.plugin(l);
        return a ? r(a) : E.none;
      })), o && h.push(o(l)), h;
    });
  }
  /**
  Create a plugin for a class whose constructor takes a single
  editor view as argument.
  */
  static fromClass(t, e) {
    return z.define((i, s) => new t(i, s), e);
  }
}
class Me {
  constructor(t) {
    this.spec = t, this.mustUpdate = null, this.value = null;
  }
  get plugin() {
    return this.spec && this.spec.plugin;
  }
  update(t) {
    if (this.value) {
      if (this.mustUpdate) {
        let e = this.mustUpdate;
        if (this.mustUpdate = null, this.value.update)
          try {
            this.value.update(e);
          } catch (i) {
            if (U(e.state, i, "CodeMirror plugin crashed"), this.value.destroy)
              try {
                this.value.destroy();
              } catch {
              }
            this.deactivate();
          }
      }
    } else if (this.spec)
      try {
        this.value = this.spec.plugin.create(t, this.spec.arg);
      } catch (e) {
        U(t.state, e, "CodeMirror plugin crashed"), this.deactivate();
      }
    return this;
  }
  destroy(t) {
    var e;
    if (!((e = this.value) === null || e === void 0) && e.destroy)
      try {
        this.value.destroy();
      } catch (i) {
        U(t.state, i, "CodeMirror plugin crashed");
      }
  }
  deactivate() {
    this.spec = this.value = null;
  }
}
const qs = /* @__PURE__ */ M.define(), li = /* @__PURE__ */ M.define(), It = /* @__PURE__ */ M.define(), Ys = /* @__PURE__ */ M.define(), hi = /* @__PURE__ */ M.define(), js = /* @__PURE__ */ M.define();
function Si(n, t) {
  let e = n.state.facet(js);
  if (!e.length)
    return e;
  let i = e.map((o) => o instanceof Function ? o(n) : o), s = [];
  return V.spans(i, t.from, t.to, {
    point() {
    },
    span(o, r, l, h) {
      let f = o - t.from, a = r - t.from, c = s;
      for (let d = l.length - 1; d >= 0; d--, h--) {
        let u = l[d].spec.bidiIsolate, p;
        if (u == null && (u = $n(t.text, f, a)), h > 0 && c.length && (p = c[c.length - 1]).to == f && p.direction == u)
          p.to = a, c = p.inner;
        else {
          let g = { from: f, to: a, direction: u, inner: [] };
          c.push(g), c = g.inner;
        }
      }
    }
  }), s;
}
const _s = /* @__PURE__ */ M.define();
function ai(n) {
  let t = 0, e = 0, i = 0, s = 0;
  for (let o of n.state.facet(_s)) {
    let r = o(n);
    r && (r.left != null && (t = Math.max(t, r.left)), r.right != null && (e = Math.max(e, r.right)), r.top != null && (i = Math.max(i, r.top)), r.bottom != null && (s = Math.max(s, r.bottom)));
  }
  return { left: t, right: e, top: i, bottom: s };
}
const Bt = /* @__PURE__ */ M.define();
class Y {
  constructor(t, e, i, s) {
    this.fromA = t, this.toA = e, this.fromB = i, this.toB = s;
  }
  join(t) {
    return new Y(Math.min(this.fromA, t.fromA), Math.max(this.toA, t.toA), Math.min(this.fromB, t.fromB), Math.max(this.toB, t.toB));
  }
  addToSet(t) {
    let e = t.length, i = this;
    for (; e > 0; e--) {
      let s = t[e - 1];
      if (!(s.fromA > i.toA)) {
        if (s.toA < i.fromA)
          break;
        i = i.join(s), t.splice(e - 1, 1);
      }
    }
    return t.splice(e, 0, i), t;
  }
  static extendWithRanges(t, e) {
    if (e.length == 0)
      return t;
    let i = [];
    for (let s = 0, o = 0, r = 0, l = 0; ; s++) {
      let h = s == t.length ? null : t[s], f = r - l, a = h ? h.fromB : 1e9;
      for (; o < e.length && e[o] < a; ) {
        let c = e[o], d = e[o + 1], u = Math.max(l, c), p = Math.min(a, d);
        if (u <= p && new Y(u + f, p + f, u, p).addToSet(i), d > a)
          break;
        o += 2;
      }
      if (!h)
        return i;
      new Y(h.fromA, h.toA, h.fromB, h.toB).addToSet(i), r = h.toA, l = h.toB;
    }
  }
}
class me {
  constructor(t, e, i) {
    this.view = t, this.state = e, this.transactions = i, this.flags = 0, this.startState = t.state, this.changes = hs.empty(this.startState.doc.length);
    for (let o of i)
      this.changes = this.changes.compose(o.changes);
    let s = [];
    this.changes.iterChangedRanges((o, r, l, h) => s.push(new Y(o, r, l, h))), this.changedRanges = s;
  }
  /**
  @internal
  */
  static create(t, e, i) {
    return new me(t, e, i);
  }
  /**
  Tells you whether the [viewport](https://codemirror.net/6/docs/ref/#view.EditorView.viewport) or
  [visible ranges](https://codemirror.net/6/docs/ref/#view.EditorView.visibleRanges) changed in this
  update.
  */
  get viewportChanged() {
    return (this.flags & 4) > 0;
  }
  /**
  Returns true when
  [`viewportChanged`](https://codemirror.net/6/docs/ref/#view.ViewUpdate.viewportChanged) is true
  and the viewport change is not just the result of mapping it in
  response to document changes.
  */
  get viewportMoved() {
    return (this.flags & 8) > 0;
  }
  /**
  Indicates whether the height of a block element in the editor
  changed in this update.
  */
  get heightChanged() {
    return (this.flags & 2) > 0;
  }
  /**
  Returns true when the document was modified or the size of the
  editor, or elements within the editor, changed.
  */
  get geometryChanged() {
    return this.docChanged || (this.flags & 18) > 0;
  }
  /**
  True when this update indicates a focus change.
  */
  get focusChanged() {
    return (this.flags & 1) > 0;
  }
  /**
  Whether the document changed in this update.
  */
  get docChanged() {
    return !this.changes.empty;
  }
  /**
  Whether the selection was explicitly set in this update.
  */
  get selectionSet() {
    return this.transactions.some((t) => t.selection);
  }
  /**
  @internal
  */
  get empty() {
    return this.flags == 0 && this.transactions.length == 0;
  }
}
class vi extends O {
  get length() {
    return this.view.state.doc.length;
  }
  constructor(t) {
    super(), this.view = t, this.decorations = [], this.dynamicDecorationMap = [!1], this.domChanged = null, this.hasComposition = null, this.markedForComposition = /* @__PURE__ */ new Set(), this.editContextFormatting = E.none, this.lastCompositionAfterCursor = !1, this.minWidth = 0, this.minWidthFrom = 0, this.minWidthTo = 0, this.impreciseAnchor = null, this.impreciseHead = null, this.forceSelection = !1, this.lastUpdate = Date.now(), this.setDOM(t.contentDOM), this.children = [new L()], this.children[0].setParent(this), this.updateDeco(), this.updateInner([new Y(0, 0, 0, t.state.doc.length)], 0, null);
  }
  // Update the document view to a given state.
  update(t) {
    var e;
    let i = t.changedRanges;
    this.minWidth > 0 && i.length && (i.every(({ fromA: f, toA: a }) => a < this.minWidthFrom || f > this.minWidthTo) ? (this.minWidthFrom = t.changes.mapPos(this.minWidthFrom, 1), this.minWidthTo = t.changes.mapPos(this.minWidthTo, 1)) : this.minWidth = this.minWidthFrom = this.minWidthTo = 0), this.updateEditContextFormatting(t);
    let s = -1;
    this.view.inputState.composing >= 0 && !this.view.observer.editContext && (!((e = this.domChanged) === null || e === void 0) && e.newSel ? s = this.domChanged.newSel.head : !so(t.changes, this.hasComposition) && !t.selectionSet && (s = t.state.selection.main.head));
    let o = s > -1 ? Jn(this.view, t.changes, s) : null;
    if (this.domChanged = null, this.hasComposition) {
      this.markedForComposition.clear();
      let { from: f, to: a } = this.hasComposition;
      i = new Y(f, a, t.changes.mapPos(f, -1), t.changes.mapPos(a, 1)).addToSet(i.slice());
    }
    this.hasComposition = o ? { from: o.range.fromB, to: o.range.toB } : null, (y.ie || y.chrome) && !o && t && t.state.doc.lines != t.startState.doc.lines && (this.forceSelection = !0);
    let r = this.decorations, l = this.updateDeco(), h = eo(r, l, t.changes);
    return i = Y.extendWithRanges(i, h), !(this.flags & 7) && i.length == 0 ? !1 : (this.updateInner(i, t.startState.doc.length, o), t.transactions.length && (this.lastUpdate = Date.now()), !0);
  }
  // Used by update and the constructor do perform the actual DOM
  // update
  updateInner(t, e, i) {
    this.view.viewState.mustMeasureContent = !0, this.updateChildren(t, e, i);
    let { observer: s } = this.view;
    s.ignore(() => {
      this.dom.style.height = this.view.viewState.contentHeight / this.view.scaleY + "px", this.dom.style.flexBasis = this.minWidth ? this.minWidth + "px" : "";
      let r = y.chrome || y.ios ? { node: s.selectionRange.focusNode, written: !1 } : void 0;
      this.sync(this.view, r), this.flags &= -8, r && (r.written || s.selectionRange.focusNode != r.node) && (this.forceSelection = !0), this.dom.style.height = "";
    }), this.markedForComposition.forEach(
      (r) => r.flags &= -9
      /* ViewFlag.Composition */
    );
    let o = [];
    if (this.view.viewport.from || this.view.viewport.to < this.view.state.doc.length)
      for (let r of this.children)
        r instanceof et && r.widget instanceof ze && o.push(r.dom);
    s.updateGaps(o);
  }
  updateChildren(t, e, i) {
    let s = i ? i.range.addToSet(t.slice()) : t, o = this.childCursor(e);
    for (let r = s.length - 1; ; r--) {
      let l = r >= 0 ? s[r] : null;
      if (!l)
        break;
      let { fromA: h, toA: f, fromB: a, toB: c } = l, d, u, p, g;
      if (i && i.range.fromB < c && i.range.toB > a) {
        let x = Ft.build(this.view.state.doc, a, i.range.fromB, this.decorations, this.dynamicDecorationMap), v = Ft.build(this.view.state.doc, i.range.toB, c, this.decorations, this.dynamicDecorationMap);
        u = x.breakAtStart, p = x.openStart, g = v.openEnd;
        let C = this.compositionView(i);
        v.breakAtStart ? C.breakAfter = 1 : v.content.length && C.merge(C.length, C.length, v.content[0], !1, v.openStart, 0) && (C.breakAfter = v.content[0].breakAfter, v.content.shift()), x.content.length && C.merge(0, 0, x.content[x.content.length - 1], !0, 0, x.openEnd) && x.content.pop(), d = x.content.concat(C).concat(v.content);
      } else
        ({ content: d, breakAtStart: u, openStart: p, openEnd: g } = Ft.build(this.view.state.doc, a, c, this.decorations, this.dynamicDecorationMap));
      let { i: m, off: b } = o.findPos(f, 1), { i: w, off: S } = o.findPos(h, -1);
      xs(this, w, S, m, b, d, u, p, g);
    }
    i && this.fixCompositionDOM(i);
  }
  updateEditContextFormatting(t) {
    this.editContextFormatting = this.editContextFormatting.map(t.changes);
    for (let e of t.transactions)
      for (let i of e.effects)
        i.is(Ks) && (this.editContextFormatting = i.value);
  }
  compositionView(t) {
    let e = new _(t.text.nodeValue);
    e.flags |= 8;
    for (let { deco: s } of t.marks)
      e = new it(s, [e], e.length);
    let i = new L();
    return i.append(e, 0), i;
  }
  fixCompositionDOM(t) {
    let e = (o, r) => {
      r.flags |= 8 | (r.children.some(
        (h) => h.flags & 7
        /* ViewFlag.Dirty */
      ) ? 1 : 0), this.markedForComposition.add(r);
      let l = O.get(o);
      l && l != r && (l.dom = null), r.setDOM(o);
    }, i = this.childPos(t.range.fromB, 1), s = this.children[i.i];
    e(t.line, s);
    for (let o = t.marks.length - 1; o >= -1; o--)
      i = s.childPos(i.off, 1), s = s.children[i.i], e(o >= 0 ? t.marks[o].node : t.text, s);
  }
  // Sync the DOM selection to this.state.selection
  updateSelection(t = !1, e = !1) {
    (t || !this.view.observer.selectionRange.focusNode) && this.view.observer.readSelectionRange();
    let i = this.view.root.activeElement, s = i == this.dom, o = !s && !(this.view.state.facet(tt) || this.dom.tabIndex > -1) && le(this.dom, this.view.observer.selectionRange) && !(i && this.dom.contains(i));
    if (!(s || e || o))
      return;
    let r = this.forceSelection;
    this.forceSelection = !1;
    let l = this.view.state.selection.main, h = this.moveToLine(this.domAtPos(l.anchor)), f = l.empty ? h : this.moveToLine(this.domAtPos(l.head));
    if (y.gecko && l.empty && !this.hasComposition && Qn(h)) {
      let c = document.createTextNode("");
      this.view.observer.ignore(() => h.node.insertBefore(c, h.node.childNodes[h.offset] || null)), h = f = new N(c, 0), r = !0;
    }
    let a = this.view.observer.selectionRange;
    (r || !a.focusNode || (!Vt(h.node, h.offset, a.anchorNode, a.anchorOffset) || !Vt(f.node, f.offset, a.focusNode, a.focusOffset)) && !this.suppressWidgetCursorChange(a, l)) && (this.view.observer.ignore(() => {
      y.android && y.chrome && this.dom.contains(a.focusNode) && io(a.focusNode, this.dom) && (this.dom.blur(), this.dom.focus({ preventScroll: !0 }));
      let c = zt(this.view.root);
      if (c) if (l.empty) {
        if (y.gecko) {
          let d = Zn(h.node, h.offset);
          if (d && d != 3) {
            let u = (d == 1 ? bs : ys)(h.node, h.offset);
            u && (h = new N(u.node, u.offset));
          }
        }
        c.collapse(h.node, h.offset), l.bidiLevel != null && c.caretBidiLevel !== void 0 && (c.caretBidiLevel = l.bidiLevel);
      } else if (c.extend) {
        c.collapse(h.node, h.offset);
        try {
          c.extend(f.node, f.offset);
        } catch {
        }
      } else {
        let d = document.createRange();
        l.anchor > l.head && ([h, f] = [f, h]), d.setEnd(f.node, f.offset), d.setStart(h.node, h.offset), c.removeAllRanges(), c.addRange(d);
      }
      o && this.view.root.activeElement == this.dom && (this.dom.blur(), i && i.focus());
    }), this.view.observer.setSelectionRange(h, f)), this.impreciseAnchor = h.precise ? null : new N(a.anchorNode, a.anchorOffset), this.impreciseHead = f.precise ? null : new N(a.focusNode, a.focusOffset);
  }
  // If a zero-length widget is inserted next to the cursor during
  // composition, avoid moving it across it and disrupting the
  // composition.
  suppressWidgetCursorChange(t, e) {
    return this.hasComposition && e.empty && Vt(t.focusNode, t.focusOffset, t.anchorNode, t.anchorOffset) && this.posFromDOM(t.focusNode, t.focusOffset) == e.head;
  }
  enforceCursorAssoc() {
    if (this.hasComposition)
      return;
    let { view: t } = this, e = t.state.selection.main, i = zt(t.root), { anchorNode: s, anchorOffset: o } = t.observer.selectionRange;
    if (!i || !e.empty || !e.assoc || !i.modify)
      return;
    let r = L.find(this, e.head);
    if (!r)
      return;
    let l = r.posAtStart;
    if (e.head == l || e.head == l + r.length)
      return;
    let h = this.coordsAt(e.head, -1), f = this.coordsAt(e.head, 1);
    if (!h || !f || h.bottom > f.top)
      return;
    let a = this.domAtPos(e.head + e.assoc);
    i.collapse(a.node, a.offset), i.modify("move", e.assoc < 0 ? "forward" : "backward", "lineboundary"), t.observer.readSelectionRange();
    let c = t.observer.selectionRange;
    t.docView.posFromDOM(c.anchorNode, c.anchorOffset) != e.from && i.collapse(s, o);
  }
  // If a position is in/near a block widget, move it to a nearby text
  // line, since we don't want the cursor inside a block widget.
  moveToLine(t) {
    let e = this.dom, i;
    if (t.node != e)
      return t;
    for (let s = t.offset; !i && s < e.childNodes.length; s++) {
      let o = O.get(e.childNodes[s]);
      o instanceof L && (i = o.domAtPos(0));
    }
    for (let s = t.offset - 1; !i && s >= 0; s--) {
      let o = O.get(e.childNodes[s]);
      o instanceof L && (i = o.domAtPos(o.length));
    }
    return i ? new N(i.node, i.offset, !0) : t;
  }
  nearest(t) {
    for (let e = t; e; ) {
      let i = O.get(e);
      if (i && i.rootView == this)
        return i;
      e = e.parentNode;
    }
    return null;
  }
  posFromDOM(t, e) {
    let i = this.nearest(t);
    if (!i)
      throw new RangeError("Trying to find position for a DOM position outside of the document");
    return i.localPosFromDOM(t, e) + i.posAtStart;
  }
  domAtPos(t) {
    let { i: e, off: i } = this.childCursor().findPos(t, -1);
    for (; e < this.children.length - 1; ) {
      let s = this.children[e];
      if (i < s.length || s instanceof L)
        break;
      e++, i = 0;
    }
    return this.children[e].domAtPos(i);
  }
  coordsAt(t, e) {
    let i = null, s = 0;
    for (let o = this.length, r = this.children.length - 1; r >= 0; r--) {
      let l = this.children[r], h = o - l.breakAfter, f = h - l.length;
      if (h < t)
        break;
      if (f <= t && (f < t || l.covers(-1)) && (h > t || l.covers(1)) && (!i || l instanceof L && !(i instanceof L && e >= 0)))
        i = l, s = f;
      else if (i && f == t && h == t && l instanceof et && Math.abs(e) < 2) {
        if (l.deco.startSide < 0)
          break;
        r && (i = null);
      }
      o = f;
    }
    return i ? i.coordsAt(t - s, e) : null;
  }
  coordsForChar(t) {
    let { i: e, off: i } = this.childPos(t, 1), s = this.children[e];
    if (!(s instanceof L))
      return null;
    for (; s.children.length; ) {
      let { i: l, off: h } = s.childPos(i, 1);
      for (; ; l++) {
        if (l == s.children.length)
          return null;
        if ((s = s.children[l]).length)
          break;
      }
      i = h;
    }
    if (!(s instanceof _))
      return null;
    let o = bt(s.text, i);
    if (o == i)
      return null;
    let r = ut(s.dom, i, o).getClientRects();
    for (let l = 0; l < r.length; l++) {
      let h = r[l];
      if (l == r.length - 1 || h.top < h.bottom && h.left < h.right)
        return h;
    }
    return null;
  }
  measureVisibleLineHeights(t) {
    let e = [], { from: i, to: s } = t, o = this.view.contentDOM.clientWidth, r = o > Math.max(this.view.scrollDOM.clientWidth, this.minWidth) + 1, l = -1, h = this.view.textDirection == B.LTR;
    for (let f = 0, a = 0; a < this.children.length; a++) {
      let c = this.children[a], d = f + c.length;
      if (d > s)
        break;
      if (f >= i) {
        let u = c.dom.getBoundingClientRect();
        if (e.push(u.height), r) {
          let p = c.dom.lastChild, g = p ? Mt(p) : [];
          if (g.length) {
            let m = g[g.length - 1], b = h ? m.right - u.left : u.right - m.left;
            b > l && (l = b, this.minWidth = o, this.minWidthFrom = f, this.minWidthTo = d);
          }
        }
      }
      f = d + c.breakAfter;
    }
    return e;
  }
  textDirectionAt(t) {
    let { i: e } = this.childPos(t, 1);
    return getComputedStyle(this.children[e].dom).direction == "rtl" ? B.RTL : B.LTR;
  }
  measureTextSize() {
    for (let o of this.children)
      if (o instanceof L) {
        let r = o.measureTextSize();
        if (r)
          return r;
      }
    let t = document.createElement("div"), e, i, s;
    return t.className = "cm-line", t.style.width = "99999px", t.style.position = "absolute", t.textContent = "abc def ghi jkl mno pqr stu", this.view.observer.ignore(() => {
      this.dom.appendChild(t);
      let o = Mt(t.firstChild)[0];
      e = t.getBoundingClientRect().height, i = o ? o.width / 27 : 7, s = o ? o.height : e, t.remove();
    }), { lineHeight: e, charWidth: i, textHeight: s };
  }
  childCursor(t = this.length) {
    let e = this.children.length;
    return e && (t -= this.children[--e].length), new ws(this.children, t, e);
  }
  computeBlockGapDeco() {
    let t = [], e = this.view.viewState;
    for (let i = 0, s = 0; ; s++) {
      let o = s == e.viewports.length ? null : e.viewports[s], r = o ? o.from - 1 : this.length;
      if (r > i) {
        let l = (e.lineBlockAt(r).bottom - e.lineBlockAt(i).top) / this.view.scaleY;
        t.push(E.replace({
          widget: new ze(l),
          block: !0,
          inclusive: !0,
          isBlockGap: !0
        }).range(i, r));
      }
      if (!o)
        break;
      i = o.to + 1;
    }
    return E.set(t);
  }
  updateDeco() {
    let t = 1, e = this.view.state.facet(It).map((o) => (this.dynamicDecorationMap[t++] = typeof o == "function") ? o(this.view) : o), i = !1, s = this.view.state.facet(Ys).map((o, r) => {
      let l = typeof o == "function";
      return l && (i = !0), l ? o(this.view) : o;
    });
    for (s.length && (this.dynamicDecorationMap[t++] = i, e.push(V.join(s))), this.decorations = [
      this.editContextFormatting,
      ...e,
      this.computeBlockGapDeco(),
      this.view.viewState.lineGapDeco
    ]; t < this.decorations.length; )
      this.dynamicDecorationMap[t++] = !1;
    return this.decorations;
  }
  scrollIntoView(t) {
    if (t.isSnapshot) {
      let f = this.view.viewState.lineBlockAt(t.range.head);
      this.view.scrollDOM.scrollTop = f.top - t.yMargin, this.view.scrollDOM.scrollLeft = t.xMargin;
      return;
    }
    for (let f of this.view.state.facet(Is))
      try {
        if (f(this.view, t.range, t))
          return !0;
      } catch (a) {
        U(this.view.state, a, "scroll handler");
      }
    let { range: e } = t, i = this.coordsAt(e.head, e.empty ? e.assoc : e.head > e.anchor ? -1 : 1), s;
    if (!i)
      return;
    !e.empty && (s = this.coordsAt(e.anchor, e.anchor > e.head ? -1 : 1)) && (i = {
      left: Math.min(i.left, s.left),
      top: Math.min(i.top, s.top),
      right: Math.max(i.right, s.right),
      bottom: Math.max(i.bottom, s.bottom)
    });
    let o = ai(this.view), r = {
      left: i.left - o.left,
      top: i.top - o.top,
      right: i.right + o.right,
      bottom: i.bottom + o.bottom
    }, { offsetWidth: l, offsetHeight: h } = this.view.scrollDOM;
    Tn(this.view.scrollDOM, r, e.head < e.anchor ? -1 : 1, t.x, t.y, Math.max(Math.min(t.xMargin, l), -l), Math.max(Math.min(t.yMargin, h), -h), this.view.textDirection == B.LTR);
  }
}
function Qn(n) {
  return n.node.nodeType == 1 && n.node.firstChild && (n.offset == 0 || n.node.childNodes[n.offset - 1].contentEditable == "false") && (n.offset == n.node.childNodes.length || n.node.childNodes[n.offset].contentEditable == "false");
}
function Xs(n, t) {
  let e = n.observer.selectionRange;
  if (!e.focusNode)
    return null;
  let i = bs(e.focusNode, e.focusOffset), s = ys(e.focusNode, e.focusOffset), o = i || s;
  if (s && i && s.node != i.node) {
    let l = O.get(s.node);
    if (!l || l instanceof _ && l.text != s.node.nodeValue)
      o = s;
    else if (n.docView.lastCompositionAfterCursor) {
      let h = O.get(i.node);
      !h || h instanceof _ && h.text != i.node.nodeValue || (o = s);
    }
  }
  if (n.docView.lastCompositionAfterCursor = o != i, !o)
    return null;
  let r = t - o.offset;
  return { from: r, to: r + o.node.nodeValue.length, node: o.node };
}
function Jn(n, t, e) {
  let i = Xs(n, e);
  if (!i)
    return null;
  let { node: s, from: o, to: r } = i, l = s.nodeValue;
  if (/[\n\r]/.test(l) || n.state.doc.sliceString(i.from, i.to) != l)
    return null;
  let h = t.invertedDesc, f = new Y(h.mapPos(o), h.mapPos(r), o, r), a = [];
  for (let c = s.parentNode; ; c = c.parentNode) {
    let d = O.get(c);
    if (d instanceof it)
      a.push({ node: c, deco: d.mark });
    else {
      if (d instanceof L || c.nodeName == "DIV" && c.parentNode == n.contentDOM)
        return { range: f, text: s, marks: a, line: c };
      if (c != n.contentDOM)
        a.push({ node: c, deco: new Yt({
          inclusive: !0,
          attributes: Wn(c),
          tagName: c.tagName.toLowerCase()
        }) });
      else
        return null;
    }
  }
}
function Zn(n, t) {
  return n.nodeType != 1 ? 0 : (t && n.childNodes[t - 1].contentEditable == "false" ? 1 : 0) | (t < n.childNodes.length && n.childNodes[t].contentEditable == "false" ? 2 : 0);
}
let to = class {
  constructor() {
    this.changes = [];
  }
  compareRange(t, e) {
    he(t, e, this.changes);
  }
  comparePoint(t, e) {
    he(t, e, this.changes);
  }
  boundChange(t) {
    he(t, t, this.changes);
  }
};
function eo(n, t, e) {
  let i = new to();
  return V.compare(n, t, e, i), i.changes;
}
function io(n, t) {
  for (let e = n; e && e != t; e = e.assignedSlot || e.parentNode)
    if (e.nodeType == 1 && e.contentEditable == "false")
      return !0;
  return !1;
}
function so(n, t) {
  let e = !1;
  return t && n.iterChangedRanges((i, s) => {
    i < t.to && s > t.from && (e = !0);
  }), e;
}
function no(n, t, e = 1) {
  let i = n.charCategorizer(t), s = n.doc.lineAt(t), o = t - s.from;
  if (s.length == 0)
    return k.cursor(t);
  o == 0 ? e = 1 : o == s.length && (e = -1);
  let r = o, l = o;
  e < 0 ? r = bt(s.text, o, !1) : l = bt(s.text, o);
  let h = i(s.text.slice(r, l));
  for (; r > 0; ) {
    let f = bt(s.text, r, !1);
    if (i(s.text.slice(f, r)) != h)
      break;
    r = f;
  }
  for (; l < s.length; ) {
    let f = bt(s.text, l);
    if (i(s.text.slice(l, f)) != h)
      break;
    l = f;
  }
  return k.range(r + s.from, l + s.from);
}
function oo(n, t) {
  return t.left > n ? t.left - n : Math.max(0, n - t.right);
}
function ro(n, t) {
  return t.top > n ? t.top - n : Math.max(0, n - t.bottom);
}
function ke(n, t) {
  return n.top < t.bottom - 1 && n.bottom > t.top + 1;
}
function Ci(n, t) {
  return t < n.top ? { top: t, left: n.left, right: n.right, bottom: n.bottom } : n;
}
function Mi(n, t) {
  return t > n.bottom ? { top: n.top, left: n.left, right: n.right, bottom: t } : n;
}
function je(n, t, e) {
  let i, s, o, r, l = !1, h, f, a, c;
  for (let p = n.firstChild; p; p = p.nextSibling) {
    let g = Mt(p);
    for (let m = 0; m < g.length; m++) {
      let b = g[m];
      s && ke(s, b) && (b = Ci(Mi(b, s.bottom), s.top));
      let w = oo(t, b), S = ro(e, b);
      if (w == 0 && S == 0)
        return p.nodeType == 3 ? ki(p, t, e) : je(p, t, e);
      (!i || r > S || r == S && o > w) && (i = p, s = b, o = w, r = S, l = w ? t < b.left ? m > 0 : m < g.length - 1 : !0), w == 0 ? e > b.bottom && (!a || a.bottom < b.bottom) ? (h = p, a = b) : e < b.top && (!c || c.top > b.top) && (f = p, c = b) : a && ke(a, b) ? a = Mi(a, b.bottom) : c && ke(c, b) && (c = Ci(c, b.top));
    }
  }
  if (a && a.bottom >= e ? (i = h, s = a) : c && c.top <= e && (i = f, s = c), !i)
    return { node: n, offset: 0 };
  let d = Math.max(s.left, Math.min(s.right, t));
  if (i.nodeType == 3)
    return ki(i, d, e);
  if (l && i.contentEditable != "false")
    return je(i, d, e);
  let u = Array.prototype.indexOf.call(n.childNodes, i) + (t >= (s.left + s.right) / 2 ? 1 : 0);
  return { node: n, offset: u };
}
function ki(n, t, e) {
  let i = n.nodeValue.length, s = -1, o = 1e9, r = 0;
  for (let l = 0; l < i; l++) {
    let h = ut(n, l, l + 1).getClientRects();
    for (let f = 0; f < h.length; f++) {
      let a = h[f];
      if (a.top == a.bottom)
        continue;
      r || (r = t - a.left);
      let c = (a.top > e ? a.top - e : e - a.bottom) - 1;
      if (a.left - 1 <= t && a.right + 1 >= t && c < o) {
        let d = t >= (a.left + a.right) / 2, u = d;
        if ((y.chrome || y.gecko) && ut(n, l).getBoundingClientRect().left == a.right && (u = !d), c <= 0)
          return { node: n, offset: l + (u ? 1 : 0) };
        s = l + (u ? 1 : 0), o = c;
      }
    }
  }
  return { node: n, offset: s > -1 ? s : r > 0 ? n.nodeValue.length : 0 };
}
function Gs(n, t, e, i = -1) {
  var s, o;
  let r = n.contentDOM.getBoundingClientRect(), l = r.top + n.viewState.paddingTop, h, { docHeight: f } = n.viewState, { x: a, y: c } = t, d = c - l;
  if (d < 0)
    return 0;
  if (d > f)
    return n.state.doc.length;
  for (let x = n.viewState.heightOracle.textHeight / 2, v = !1; h = n.elementAtHeight(d), h.type != F.Text; )
    for (; d = i > 0 ? h.bottom + x : h.top - x, !(d >= 0 && d <= f); ) {
      if (v)
        return e ? null : 0;
      v = !0, i = -i;
    }
  c = l + d;
  let u = h.from;
  if (u < n.viewport.from)
    return n.viewport.from == 0 ? 0 : e ? null : Ai(n, r, h, a, c);
  if (u > n.viewport.to)
    return n.viewport.to == n.state.doc.length ? n.state.doc.length : e ? null : Ai(n, r, h, a, c);
  let p = n.dom.ownerDocument, g = n.root.elementFromPoint ? n.root : p, m = g.elementFromPoint(a, c);
  m && !n.contentDOM.contains(m) && (m = null), m || (a = Math.max(r.left + 1, Math.min(r.right - 1, a)), m = g.elementFromPoint(a, c), m && !n.contentDOM.contains(m) && (m = null));
  let b, w = -1;
  if (m && ((s = n.docView.nearest(m)) === null || s === void 0 ? void 0 : s.isEditable) != !1) {
    if (p.caretPositionFromPoint) {
      let x = p.caretPositionFromPoint(a, c);
      x && ({ offsetNode: b, offset: w } = x);
    } else if (p.caretRangeFromPoint) {
      let x = p.caretRangeFromPoint(a, c);
      x && ({ startContainer: b, startOffset: w } = x, (!n.contentDOM.contains(b) || y.safari && lo(b, w, a) || y.chrome && ho(b, w, a)) && (b = void 0));
    }
    b && (w = Math.min(Q(b), w));
  }
  if (!b || !n.docView.dom.contains(b)) {
    let x = L.find(n.docView, u);
    if (!x)
      return d > h.top + h.height / 2 ? h.to : h.from;
    ({ node: b, offset: w } = je(x.dom, a, c));
  }
  let S = n.docView.nearest(b);
  if (!S)
    return null;
  if (S.isWidget && ((o = S.dom) === null || o === void 0 ? void 0 : o.nodeType) == 1) {
    let x = S.dom.getBoundingClientRect();
    return t.y < x.top || t.y <= x.bottom && t.x <= (x.left + x.right) / 2 ? S.posAtStart : S.posAtEnd;
  } else
    return S.localPosFromDOM(b, w) + S.posAtStart;
}
function Ai(n, t, e, i, s) {
  let o = Math.round((i - t.left) * n.defaultCharacterWidth);
  if (n.lineWrapping && e.height > n.defaultLineHeight * 1.5) {
    let l = n.viewState.heightOracle.textHeight, h = Math.floor((s - e.top - (n.defaultLineHeight - l) * 0.5) / l);
    o += h * n.viewState.heightOracle.lineLength;
  }
  let r = n.state.sliceDoc(e.from, e.to);
  return e.from + Le(r, o, n.state.tabSize);
}
function lo(n, t, e) {
  let i, s = n;
  if (n.nodeType != 3 || t != (i = n.nodeValue.length))
    return !1;
  for (; ; ) {
    let o = s.nextSibling;
    if (o) {
      if (o.nodeName == "BR")
        break;
      return !1;
    } else {
      let r = s.parentNode;
      if (!r || r.nodeName == "DIV")
        break;
      s = r;
    }
  }
  return ut(n, i - 1, i).getBoundingClientRect().right > e;
}
function ho(n, t, e) {
  if (t != 0)
    return !1;
  for (let s = n; ; ) {
    let o = s.parentNode;
    if (!o || o.nodeType != 1 || o.firstChild != s)
      return !1;
    if (o.classList.contains("cm-line"))
      break;
    s = o;
  }
  let i = n.nodeType == 1 ? n.getBoundingClientRect() : ut(n, 0, Math.max(n.nodeValue.length, 1)).getBoundingClientRect();
  return e - i.left > 5;
}
function _e(n, t, e) {
  let i = n.lineBlockAt(t);
  if (Array.isArray(i.type)) {
    let s;
    for (let o of i.type) {
      if (o.from > t)
        break;
      if (!(o.to < t)) {
        if (o.from < t && o.to > t)
          return o;
        (!s || o.type == F.Text && (s.type != o.type || (e < 0 ? o.from < t : o.to > t))) && (s = o);
      }
    }
    return s || i;
  }
  return i;
}
function ao(n, t, e, i) {
  let s = _e(n, t.head, t.assoc || -1), o = !i || s.type != F.Text || !(n.lineWrapping || s.widgetLineBreaks) ? null : n.coordsAtPos(t.assoc < 0 && t.head > s.from ? t.head - 1 : t.head);
  if (o) {
    let r = n.dom.getBoundingClientRect(), l = n.textDirectionAt(s.from), h = n.posAtCoords({
      x: e == (l == B.LTR) ? r.right - 1 : r.left + 1,
      y: (o.top + o.bottom) / 2
    });
    if (h != null)
      return k.cursor(h, e ? -1 : 1);
  }
  return k.cursor(e ? s.to : s.from, e ? -1 : 1);
}
function Di(n, t, e, i) {
  let s = n.state.doc.lineAt(t.head), o = n.bidiSpans(s), r = n.textDirectionAt(s.from);
  for (let l = t, h = null; ; ) {
    let f = Gn(s, o, r, l, e), a = Bs;
    if (!f) {
      if (s.number == (e ? n.state.doc.lines : 1))
        return l;
      a = `
`, s = n.state.doc.line(s.number + (e ? 1 : -1)), o = n.bidiSpans(s), f = n.visualLineSide(s, !e);
    }
    if (h) {
      if (!h(a))
        return l;
    } else {
      if (!i)
        return f;
      h = i(a);
    }
    l = f;
  }
}
function fo(n, t, e) {
  let i = n.state.charCategorizer(t), s = i(e);
  return (o) => {
    let r = i(o);
    return s == Mn.Space && (s = r), s == r;
  };
}
function co(n, t, e, i) {
  let s = t.head, o = e ? 1 : -1;
  if (s == (e ? n.state.doc.length : 0))
    return k.cursor(s, t.assoc);
  let r = t.goalColumn, l, h = n.contentDOM.getBoundingClientRect(), f = n.coordsAtPos(s, t.assoc || -1), a = n.documentTop;
  if (f)
    r == null && (r = f.left - h.left), l = o < 0 ? f.top : f.bottom;
  else {
    let u = n.viewState.lineBlockAt(s);
    r == null && (r = Math.min(h.right - h.left, n.defaultCharacterWidth * (s - u.from))), l = (o < 0 ? u.top : u.bottom) + a;
  }
  let c = h.left + r, d = i ?? n.viewState.heightOracle.textHeight >> 1;
  for (let u = 0; ; u += 10) {
    let p = l + (d + u) * o, g = Gs(n, { x: c, y: p }, !1, o);
    if (p < h.top || p > h.bottom || (o < 0 ? g < s : g > s)) {
      let m = n.docView.coordsForChar(g), b = !m || p < m.top ? -1 : 1;
      return k.cursor(g, b, void 0, r);
    }
  }
}
function ae(n, t, e) {
  for (; ; ) {
    let i = 0;
    for (let s of n)
      s.between(t - 1, t + 1, (o, r, l) => {
        if (t > o && t < r) {
          let h = i || e || (t - o < r - t ? -1 : 1);
          t = h < 0 ? o : r, i = h;
        }
      });
    if (!i)
      return t;
  }
}
function Ae(n, t, e) {
  let i = ae(n.state.facet(hi).map((s) => s(n)), e.from, t.head > e.from ? -1 : 1);
  return i == e.from ? e : k.cursor(i, i < e.from ? 1 : -1);
}
const Lt = "￿";
class uo {
  constructor(t, e) {
    this.points = t, this.text = "", this.lineSeparator = e.facet(Nt.lineSeparator);
  }
  append(t) {
    this.text += t;
  }
  lineBreak() {
    this.text += Lt;
  }
  readRange(t, e) {
    if (!t)
      return this;
    let i = t.parentNode;
    for (let s = t; ; ) {
      this.findPointBefore(i, s);
      let o = this.text.length;
      this.readNode(s);
      let r = s.nextSibling;
      if (r == e)
        break;
      let l = O.get(s), h = O.get(r);
      (l && h ? l.breakAfter : (l ? l.breakAfter : pe(s)) || pe(r) && (s.nodeName != "BR" || s.cmIgnore) && this.text.length > o) && this.lineBreak(), s = r;
    }
    return this.findPointBefore(i, e), this;
  }
  readTextNode(t) {
    let e = t.nodeValue;
    for (let i of this.points)
      i.node == t && (i.pos = this.text.length + Math.min(i.offset, e.length));
    for (let i = 0, s = this.lineSeparator ? null : /\r\n?|\n/g; ; ) {
      let o = -1, r = 1, l;
      if (this.lineSeparator ? (o = e.indexOf(this.lineSeparator, i), r = this.lineSeparator.length) : (l = s.exec(e)) && (o = l.index, r = l[0].length), this.append(e.slice(i, o < 0 ? e.length : o)), o < 0)
        break;
      if (this.lineBreak(), r > 1)
        for (let h of this.points)
          h.node == t && h.pos > this.text.length && (h.pos -= r - 1);
      i = o + r;
    }
  }
  readNode(t) {
    if (t.cmIgnore)
      return;
    let e = O.get(t), i = e && e.overrideDOMText;
    if (i != null) {
      this.findPointInside(t, i.length);
      for (let s = i.iter(); !s.next().done; )
        s.lineBreak ? this.lineBreak() : this.append(s.value);
    } else t.nodeType == 3 ? this.readTextNode(t) : t.nodeName == "BR" ? t.nextSibling && this.lineBreak() : t.nodeType == 1 && this.readRange(t.firstChild, null);
  }
  findPointBefore(t, e) {
    for (let i of this.points)
      i.node == t && t.childNodes[i.offset] == e && (i.pos = this.text.length);
  }
  findPointInside(t, e) {
    for (let i of this.points)
      (t.nodeType == 3 ? i.node == t : t.contains(i.node)) && (i.pos = this.text.length + (po(t, i.node, i.offset) ? e : 0));
  }
}
function po(n, t, e) {
  for (; ; ) {
    if (!t || e < Q(t))
      return !1;
    if (t == n)
      return !0;
    e = dt(t) + 1, t = t.parentNode;
  }
}
class Oi {
  constructor(t, e) {
    this.node = t, this.offset = e, this.pos = -1;
  }
}
class go {
  constructor(t, e, i, s) {
    this.typeOver = s, this.bounds = null, this.text = "", this.domChanged = e > -1;
    let { impreciseHead: o, impreciseAnchor: r } = t.docView;
    if (t.state.readOnly && e > -1)
      this.newSel = null;
    else if (e > -1 && (this.bounds = t.docView.domBoundsAround(e, i, 0))) {
      let l = o || r ? [] : yo(t), h = new uo(l, t.state);
      h.readRange(this.bounds.startDOM, this.bounds.endDOM), this.text = h.text, this.newSel = wo(l, this.bounds.from);
    } else {
      let l = t.observer.selectionRange, h = o && o.node == l.focusNode && o.offset == l.focusOffset || !Pe(t.contentDOM, l.focusNode) ? t.state.selection.main.head : t.docView.posFromDOM(l.focusNode, l.focusOffset), f = r && r.node == l.anchorNode && r.offset == l.anchorOffset || !Pe(t.contentDOM, l.anchorNode) ? t.state.selection.main.anchor : t.docView.posFromDOM(l.anchorNode, l.anchorOffset), a = t.viewport;
      if ((y.ios || y.chrome) && t.state.selection.main.empty && h != f && (a.from > 0 || a.to < t.state.doc.length)) {
        let c = Math.min(h, f), d = Math.max(h, f), u = a.from - c, p = a.to - d;
        (u == 0 || u == 1 || c == 0) && (p == 0 || p == -1 || d == t.state.doc.length) && (h = 0, f = t.state.doc.length);
      }
      this.newSel = k.single(f, h);
    }
  }
}
function $s(n, t) {
  let e, { newSel: i } = t, s = n.state.selection.main, o = n.inputState.lastKeyTime > Date.now() - 100 ? n.inputState.lastKeyCode : -1;
  if (t.bounds) {
    let { from: r, to: l } = t.bounds, h = s.from, f = null;
    (o === 8 || y.android && t.text.length < l - r) && (h = s.to, f = "end");
    let a = bo(n.state.doc.sliceString(r, l, Lt), t.text, h - r, f);
    a && (y.chrome && o == 13 && a.toB == a.from + 2 && t.text.slice(a.from, a.toB) == Lt + Lt && a.toB--, e = {
      from: r + a.from,
      to: r + a.toA,
      insert: q.of(t.text.slice(a.from, a.toB).split(Lt))
    });
  } else i && (!n.hasFocus && n.state.facet(tt) || i.main.eq(s)) && (i = null);
  if (!e && !i)
    return !1;
  if (!e && t.typeOver && !s.empty && i && i.main.empty ? e = { from: s.from, to: s.to, insert: n.state.doc.slice(s.from, s.to) } : (y.mac || y.android) && e && e.from == e.to && e.from == s.head - 1 && /^\. ?$/.test(e.insert.toString()) && n.contentDOM.getAttribute("autocorrect") == "off" ? (i && e.insert.length == 2 && (i = k.single(i.main.anchor - 1, i.main.head - 1)), e = { from: e.from, to: e.to, insert: q.of([e.insert.toString().replace(".", " ")]) }) : e && e.from >= s.from && e.to <= s.to && (e.from != s.from || e.to != s.to) && s.to - s.from - (e.to - e.from) <= 4 ? e = {
    from: s.from,
    to: s.to,
    insert: n.state.doc.slice(s.from, e.from).append(e.insert).append(n.state.doc.slice(e.to, s.to))
  } : y.chrome && e && e.from == e.to && e.from == s.head && e.insert.toString() == `
 ` && n.lineWrapping && (i && (i = k.single(i.main.anchor - 1, i.main.head - 1)), e = { from: s.from, to: s.to, insert: q.of([" "]) }), e)
    return fi(n, e, i, o);
  if (i && !i.main.eq(s)) {
    let r = !1, l = "select";
    return n.inputState.lastSelectionTime > Date.now() - 50 && (n.inputState.lastSelectionOrigin == "select" && (r = !0), l = n.inputState.lastSelectionOrigin), n.dispatch({ selection: i, scrollIntoView: r, userEvent: l }), !0;
  } else
    return !1;
}
function fi(n, t, e, i = -1) {
  if (y.ios && n.inputState.flushIOSKey(t))
    return !0;
  let s = n.state.selection.main;
  if (y.android && (t.to == s.to && // GBoard will sometimes remove a space it just inserted
  // after a completion when you press enter
  (t.from == s.from || t.from == s.from - 1 && n.state.sliceDoc(t.from, s.from) == " ") && t.insert.length == 1 && t.insert.lines == 2 && St(n.contentDOM, "Enter", 13) || (t.from == s.from - 1 && t.to == s.to && t.insert.length == 0 || i == 8 && t.insert.length < t.to - t.from && t.to > s.head) && St(n.contentDOM, "Backspace", 8) || t.from == s.from && t.to == s.to + 1 && t.insert.length == 0 && St(n.contentDOM, "Delete", 46)))
    return !0;
  let o = t.insert.toString();
  n.inputState.composing >= 0 && n.inputState.composing++;
  let r, l = () => r || (r = mo(n, t, e));
  return n.state.facet(Vs).some((h) => h(n, t.from, t.to, o, l)) || n.dispatch(l()), !0;
}
function mo(n, t, e) {
  let i, s = n.state, o = s.selection.main;
  if (t.from >= o.from && t.to <= o.to && t.to - t.from >= (o.to - o.from) / 3 && (!e || e.main.empty && e.main.from == t.from + t.insert.length) && n.inputState.composing < 0) {
    let l = o.from < t.from ? s.sliceDoc(o.from, t.from) : "", h = o.to > t.to ? s.sliceDoc(t.to, o.to) : "";
    i = s.replaceSelection(n.state.toText(l + t.insert.sliceString(0, void 0, n.state.lineBreak) + h));
  } else {
    let l = s.changes(t), h = e && e.main.to <= l.newLength ? e.main : void 0;
    if (s.selection.ranges.length > 1 && n.inputState.composing >= 0 && t.to <= o.to && t.to >= o.to - 10) {
      let f = n.state.sliceDoc(t.from, t.to), a, c = e && Xs(n, e.main.head);
      if (c) {
        let p = t.insert.length - (t.to - t.from);
        a = { from: c.from, to: c.to - p };
      } else
        a = n.state.doc.lineAt(o.head);
      let d = o.to - t.to, u = o.to - o.from;
      i = s.changeByRange((p) => {
        if (p.from == o.from && p.to == o.to)
          return { changes: l, range: h || p.map(l) };
        let g = p.to - d, m = g - f.length;
        if (p.to - p.from != u || n.state.sliceDoc(m, g) != f || // Unfortunately, there's no way to make multiple
        // changes in the same node work without aborting
        // composition, so cursors in the composition range are
        // ignored.
        p.to >= a.from && p.from <= a.to)
          return { range: p };
        let b = s.changes({ from: m, to: g, insert: t.insert }), w = p.to - o.to;
        return {
          changes: b,
          range: h ? k.range(Math.max(0, h.anchor + w), Math.max(0, h.head + w)) : p.map(b)
        };
      });
    } else
      i = {
        changes: l,
        selection: h && s.selection.replaceRange(h)
      };
  }
  let r = "input.type";
  return (n.composing || n.inputState.compositionPendingChange && n.inputState.compositionEndedAt > Date.now() - 50) && (n.inputState.compositionPendingChange = !1, r += ".compose", n.inputState.compositionFirstChange && (r += ".start", n.inputState.compositionFirstChange = !1)), s.update(i, { userEvent: r, scrollIntoView: !0 });
}
function bo(n, t, e, i) {
  let s = Math.min(n.length, t.length), o = 0;
  for (; o < s && n.charCodeAt(o) == t.charCodeAt(o); )
    o++;
  if (o == s && n.length == t.length)
    return null;
  let r = n.length, l = t.length;
  for (; r > 0 && l > 0 && n.charCodeAt(r - 1) == t.charCodeAt(l - 1); )
    r--, l--;
  if (i == "end") {
    let h = Math.max(0, o - Math.min(r, l));
    e -= r + h - o;
  }
  if (r < o && n.length < t.length) {
    let h = e <= o && e >= r ? o - e : 0;
    o -= h, l = o + (l - r), r = o;
  } else if (l < o) {
    let h = e <= o && e >= l ? o - e : 0;
    o -= h, r = o + (r - l), l = o;
  }
  return { from: o, toA: r, toB: l };
}
function yo(n) {
  let t = [];
  if (n.root.activeElement != n.contentDOM)
    return t;
  let { anchorNode: e, anchorOffset: i, focusNode: s, focusOffset: o } = n.observer.selectionRange;
  return e && (t.push(new Oi(e, i)), (s != e || o != i) && t.push(new Oi(s, o))), t;
}
function wo(n, t) {
  if (n.length == 0)
    return null;
  let e = n[0].pos, i = n.length == 2 ? n[1].pos : e;
  return e > -1 && i > -1 ? k.single(e + t, i + t) : null;
}
class xo {
  setSelectionOrigin(t) {
    this.lastSelectionOrigin = t, this.lastSelectionTime = Date.now();
  }
  constructor(t) {
    this.view = t, this.lastKeyCode = 0, this.lastKeyTime = 0, this.lastTouchTime = 0, this.lastFocusTime = 0, this.lastScrollTop = 0, this.lastScrollLeft = 0, this.pendingIOSKey = void 0, this.tabFocusMode = -1, this.lastSelectionOrigin = null, this.lastSelectionTime = 0, this.lastContextMenu = 0, this.scrollHandlers = [], this.handlers = /* @__PURE__ */ Object.create(null), this.composing = -1, this.compositionFirstChange = null, this.compositionEndedAt = 0, this.compositionPendingKey = !1, this.compositionPendingChange = !1, this.mouseSelection = null, this.draggedContent = null, this.handleEvent = this.handleEvent.bind(this), this.notifiedFocused = t.hasFocus, y.safari && t.contentDOM.addEventListener("input", () => null), y.gecko && No(t.contentDOM.ownerDocument);
  }
  handleEvent(t) {
    !Oo(this.view, t) || this.ignoreDuringComposition(t) || t.type == "keydown" && this.keydown(t) || (this.view.updateState != 0 ? Promise.resolve().then(() => this.runHandlers(t.type, t)) : this.runHandlers(t.type, t));
  }
  runHandlers(t, e) {
    let i = this.handlers[t];
    if (i) {
      for (let s of i.observers)
        s(this.view, e);
      for (let s of i.handlers) {
        if (e.defaultPrevented)
          break;
        if (s(this.view, e)) {
          e.preventDefault();
          break;
        }
      }
    }
  }
  ensureHandlers(t) {
    let e = So(t), i = this.handlers, s = this.view.contentDOM;
    for (let o in e)
      if (o != "scroll") {
        let r = !e[o].handlers.length, l = i[o];
        l && r != !l.handlers.length && (s.removeEventListener(o, this.handleEvent), l = null), l || s.addEventListener(o, this.handleEvent, { passive: r });
      }
    for (let o in i)
      o != "scroll" && !e[o] && s.removeEventListener(o, this.handleEvent);
    this.handlers = e;
  }
  keydown(t) {
    if (this.lastKeyCode = t.keyCode, this.lastKeyTime = Date.now(), t.keyCode == 9 && this.tabFocusMode > -1 && (!this.tabFocusMode || Date.now() <= this.tabFocusMode))
      return !0;
    if (this.tabFocusMode > 0 && t.keyCode != 27 && Qs.indexOf(t.keyCode) < 0 && (this.tabFocusMode = -1), y.android && y.chrome && !t.synthetic && (t.keyCode == 13 || t.keyCode == 8))
      return this.view.observer.delayAndroidKey(t.key, t.keyCode), !0;
    let e;
    return y.ios && !t.synthetic && !t.altKey && !t.metaKey && ((e = Us.find((i) => i.keyCode == t.keyCode)) && !t.ctrlKey || vo.indexOf(t.key) > -1 && t.ctrlKey && !t.shiftKey) ? (this.pendingIOSKey = e || t, setTimeout(() => this.flushIOSKey(), 250), !0) : (t.keyCode != 229 && this.view.observer.forceFlush(), !1);
  }
  flushIOSKey(t) {
    let e = this.pendingIOSKey;
    return !e || e.key == "Enter" && t && t.from < t.to && /^\S+$/.test(t.insert.toString()) ? !1 : (this.pendingIOSKey = void 0, St(this.view.contentDOM, e.key, e.keyCode, e instanceof KeyboardEvent ? e : void 0));
  }
  ignoreDuringComposition(t) {
    return /^key/.test(t.type) ? this.composing > 0 ? !0 : y.safari && !y.ios && this.compositionPendingKey && Date.now() - this.compositionEndedAt < 100 ? (this.compositionPendingKey = !1, !0) : !1 : !1;
  }
  startMouseSelection(t) {
    this.mouseSelection && this.mouseSelection.destroy(), this.mouseSelection = t;
  }
  update(t) {
    this.view.observer.update(t), this.mouseSelection && this.mouseSelection.update(t), this.draggedContent && t.docChanged && (this.draggedContent = this.draggedContent.map(t.changes)), t.transactions.length && (this.lastKeyCode = this.lastSelectionTime = 0);
  }
  destroy() {
    this.mouseSelection && this.mouseSelection.destroy();
  }
}
function Ti(n, t) {
  return (e, i) => {
    try {
      return t.call(n, i, e);
    } catch (s) {
      U(e.state, s);
    }
  };
}
function So(n) {
  let t = /* @__PURE__ */ Object.create(null);
  function e(i) {
    return t[i] || (t[i] = { observers: [], handlers: [] });
  }
  for (let i of n) {
    let s = i.spec, o = s && s.plugin.domEventHandlers, r = s && s.plugin.domEventObservers;
    if (o)
      for (let l in o) {
        let h = o[l];
        h && e(l).handlers.push(Ti(i.value, h));
      }
    if (r)
      for (let l in r) {
        let h = r[l];
        h && e(l).observers.push(Ti(i.value, h));
      }
  }
  for (let i in X)
    e(i).handlers.push(X[i]);
  for (let i in j)
    e(i).observers.push(j[i]);
  return t;
}
const Us = [
  { key: "Backspace", keyCode: 8, inputType: "deleteContentBackward" },
  { key: "Enter", keyCode: 13, inputType: "insertParagraph" },
  { key: "Enter", keyCode: 13, inputType: "insertLineBreak" },
  { key: "Delete", keyCode: 46, inputType: "deleteContentForward" }
], vo = "dthko", Qs = [16, 17, 18, 20, 91, 92, 224, 225], Jt = 6;
function Zt(n) {
  return Math.max(0, n) * 0.7 + 8;
}
function Co(n, t) {
  return Math.max(Math.abs(n.clientX - t.clientX), Math.abs(n.clientY - t.clientY));
}
class Mo {
  constructor(t, e, i, s) {
    this.view = t, this.startEvent = e, this.style = i, this.mustSelect = s, this.scrollSpeed = { x: 0, y: 0 }, this.scrolling = -1, this.lastEvent = e, this.scrollParents = Rn(t.contentDOM), this.atoms = t.state.facet(hi).map((r) => r(t));
    let o = t.contentDOM.ownerDocument;
    o.addEventListener("mousemove", this.move = this.move.bind(this)), o.addEventListener("mouseup", this.up = this.up.bind(this)), this.extend = e.shiftKey, this.multiple = t.state.facet(Nt.allowMultipleSelections) && ko(t, e), this.dragging = Do(t, e) && tn(e) == 1 ? null : !1;
  }
  start(t) {
    this.dragging === !1 && this.select(t);
  }
  move(t) {
    if (t.buttons == 0)
      return this.destroy();
    if (this.dragging || this.dragging == null && Co(this.startEvent, t) < 10)
      return;
    this.select(this.lastEvent = t);
    let e = 0, i = 0, s = 0, o = 0, r = this.view.win.innerWidth, l = this.view.win.innerHeight;
    this.scrollParents.x && ({ left: s, right: r } = this.scrollParents.x.getBoundingClientRect()), this.scrollParents.y && ({ top: o, bottom: l } = this.scrollParents.y.getBoundingClientRect());
    let h = ai(this.view);
    t.clientX - h.left <= s + Jt ? e = -Zt(s - t.clientX) : t.clientX + h.right >= r - Jt && (e = Zt(t.clientX - r)), t.clientY - h.top <= o + Jt ? i = -Zt(o - t.clientY) : t.clientY + h.bottom >= l - Jt && (i = Zt(t.clientY - l)), this.setScrollSpeed(e, i);
  }
  up(t) {
    this.dragging == null && this.select(this.lastEvent), this.dragging || t.preventDefault(), this.destroy();
  }
  destroy() {
    this.setScrollSpeed(0, 0);
    let t = this.view.contentDOM.ownerDocument;
    t.removeEventListener("mousemove", this.move), t.removeEventListener("mouseup", this.up), this.view.inputState.mouseSelection = this.view.inputState.draggedContent = null;
  }
  setScrollSpeed(t, e) {
    this.scrollSpeed = { x: t, y: e }, t || e ? this.scrolling < 0 && (this.scrolling = setInterval(() => this.scroll(), 50)) : this.scrolling > -1 && (clearInterval(this.scrolling), this.scrolling = -1);
  }
  scroll() {
    let { x: t, y: e } = this.scrollSpeed;
    t && this.scrollParents.x && (this.scrollParents.x.scrollLeft += t, t = 0), e && this.scrollParents.y && (this.scrollParents.y.scrollTop += e, e = 0), (t || e) && this.view.win.scrollBy(t, e), this.dragging === !1 && this.select(this.lastEvent);
  }
  skipAtoms(t) {
    let e = null;
    for (let i = 0; i < t.ranges.length; i++) {
      let s = t.ranges[i], o = null;
      if (s.empty) {
        let r = ae(this.atoms, s.from, 0);
        r != s.from && (o = k.cursor(r, -1));
      } else {
        let r = ae(this.atoms, s.from, -1), l = ae(this.atoms, s.to, 1);
        (r != s.from || l != s.to) && (o = k.range(s.from == s.anchor ? r : l, s.from == s.head ? r : l));
      }
      o && (e || (e = t.ranges.slice()), e[i] = o);
    }
    return e ? k.create(e, t.mainIndex) : t;
  }
  select(t) {
    let { view: e } = this, i = this.skipAtoms(this.style.get(t, this.extend, this.multiple));
    (this.mustSelect || !i.eq(e.state.selection, this.dragging === !1)) && this.view.dispatch({
      selection: i,
      userEvent: "select.pointer"
    }), this.mustSelect = !1;
  }
  update(t) {
    t.transactions.some((e) => e.isUserEvent("input.type")) ? this.destroy() : this.style.update(t) && setTimeout(() => this.select(this.lastEvent), 20);
  }
}
function ko(n, t) {
  let e = n.state.facet(Ls);
  return e.length ? e[0](t) : y.mac ? t.metaKey : t.ctrlKey;
}
function Ao(n, t) {
  let e = n.state.facet(Ps);
  return e.length ? e[0](t) : y.mac ? !t.altKey : !t.ctrlKey;
}
function Do(n, t) {
  let { main: e } = n.state.selection;
  if (e.empty)
    return !1;
  let i = zt(n.root);
  if (!i || i.rangeCount == 0)
    return !0;
  let s = i.getRangeAt(0).getClientRects();
  for (let o = 0; o < s.length; o++) {
    let r = s[o];
    if (r.left <= t.clientX && r.right >= t.clientX && r.top <= t.clientY && r.bottom >= t.clientY)
      return !0;
  }
  return !1;
}
function Oo(n, t) {
  if (!t.bubbles)
    return !0;
  if (t.defaultPrevented)
    return !1;
  for (let e = t.target, i; e != n.contentDOM; e = e.parentNode)
    if (!e || e.nodeType == 11 || (i = O.get(e)) && i.ignoreEvent(t))
      return !1;
  return !0;
}
const X = /* @__PURE__ */ Object.create(null), j = /* @__PURE__ */ Object.create(null), Js = y.ie && y.ie_version < 15 || y.ios && y.webkit_version < 604;
function To(n) {
  let t = n.dom.parentNode;
  if (!t)
    return;
  let e = t.appendChild(document.createElement("textarea"));
  e.style.cssText = "position: fixed; left: -10000px; top: 10px", e.focus(), setTimeout(() => {
    n.focus(), e.remove(), Zs(n, e.value);
  }, 50);
}
function Se(n, t, e) {
  for (let i of n.facet(t))
    e = i(e, n);
  return e;
}
function Zs(n, t) {
  t = Se(n.state, oi, t);
  let { state: e } = n, i, s = 1, o = e.toText(t), r = o.lines == e.selection.ranges.length;
  if (Xe != null && e.selection.ranges.every((h) => h.empty) && Xe == o.toString()) {
    let h = -1;
    i = e.changeByRange((f) => {
      let a = e.doc.lineAt(f.from);
      if (a.from == h)
        return { range: f };
      h = a.from;
      let c = e.toText((r ? o.line(s++).text : t) + e.lineBreak);
      return {
        changes: { from: a.from, insert: c },
        range: k.cursor(f.from + c.length)
      };
    });
  } else r ? i = e.changeByRange((h) => {
    let f = o.line(s++);
    return {
      changes: { from: h.from, to: h.to, insert: f.text },
      range: k.cursor(h.from + f.length)
    };
  }) : i = e.replaceSelection(o);
  n.dispatch(i, {
    userEvent: "input.paste",
    scrollIntoView: !0
  });
}
j.scroll = (n) => {
  n.inputState.lastScrollTop = n.scrollDOM.scrollTop, n.inputState.lastScrollLeft = n.scrollDOM.scrollLeft;
};
X.keydown = (n, t) => (n.inputState.setSelectionOrigin("select"), t.keyCode == 27 && n.inputState.tabFocusMode != 0 && (n.inputState.tabFocusMode = Date.now() + 2e3), !1);
j.touchstart = (n, t) => {
  n.inputState.lastTouchTime = Date.now(), n.inputState.setSelectionOrigin("select.pointer");
};
j.touchmove = (n) => {
  n.inputState.setSelectionOrigin("select.pointer");
};
X.mousedown = (n, t) => {
  if (n.observer.flush(), n.inputState.lastTouchTime > Date.now() - 2e3)
    return !1;
  let e = null;
  for (let i of n.state.facet(Hs))
    if (e = i(n, t), e)
      break;
  if (!e && t.button == 0 && (e = Bo(n, t)), e) {
    let i = !n.hasFocus;
    n.inputState.startMouseSelection(new Mo(n, t, e, i)), i && n.observer.ignore(() => {
      ps(n.contentDOM);
      let o = n.root.activeElement;
      o && !o.contains(n.contentDOM) && o.blur();
    });
    let s = n.inputState.mouseSelection;
    if (s)
      return s.start(t), s.dragging === !1;
  }
  return !1;
};
function Ri(n, t, e, i) {
  if (i == 1)
    return k.cursor(t, e);
  if (i == 2)
    return no(n.state, t, e);
  {
    let s = L.find(n.docView, t), o = n.state.doc.lineAt(s ? s.posAtEnd : t), r = s ? s.posAtStart : o.from, l = s ? s.posAtEnd : o.to;
    return l < n.state.doc.length && l == o.to && l++, k.range(r, l);
  }
}
let Ei = (n, t, e) => t >= e.top && t <= e.bottom && n >= e.left && n <= e.right;
function Ro(n, t, e, i) {
  let s = L.find(n.docView, t);
  if (!s)
    return 1;
  let o = t - s.posAtStart;
  if (o == 0)
    return 1;
  if (o == s.length)
    return -1;
  let r = s.coordsAt(o, -1);
  if (r && Ei(e, i, r))
    return -1;
  let l = s.coordsAt(o, 1);
  return l && Ei(e, i, l) ? 1 : r && r.bottom >= i ? -1 : 1;
}
function Bi(n, t) {
  let e = n.posAtCoords({ x: t.clientX, y: t.clientY }, !1);
  return { pos: e, bias: Ro(n, e, t.clientX, t.clientY) };
}
const Eo = y.ie && y.ie_version <= 11;
let Li = null, Pi = 0, Hi = 0;
function tn(n) {
  if (!Eo)
    return n.detail;
  let t = Li, e = Hi;
  return Li = n, Hi = Date.now(), Pi = !t || e > Date.now() - 400 && Math.abs(t.clientX - n.clientX) < 2 && Math.abs(t.clientY - n.clientY) < 2 ? (Pi + 1) % 3 : 1;
}
function Bo(n, t) {
  let e = Bi(n, t), i = tn(t), s = n.state.selection;
  return {
    update(o) {
      o.docChanged && (e.pos = o.changes.mapPos(e.pos), s = s.map(o.changes));
    },
    get(o, r, l) {
      let h = Bi(n, o), f, a = Ri(n, h.pos, h.bias, i);
      if (e.pos != h.pos && !r) {
        let c = Ri(n, e.pos, e.bias, i), d = Math.min(c.from, a.from), u = Math.max(c.to, a.to);
        a = d < a.from ? k.range(d, u) : k.range(u, d);
      }
      return r ? s.replaceRange(s.main.extend(a.from, a.to)) : l && i == 1 && s.ranges.length > 1 && (f = Lo(s, h.pos)) ? f : l ? s.addRange(a) : k.create([a]);
    }
  };
}
function Lo(n, t) {
  for (let e = 0; e < n.ranges.length; e++) {
    let { from: i, to: s } = n.ranges[e];
    if (i <= t && s >= t)
      return k.create(n.ranges.slice(0, e).concat(n.ranges.slice(e + 1)), n.mainIndex == e ? 0 : n.mainIndex - (n.mainIndex > e ? 1 : 0));
  }
  return null;
}
X.dragstart = (n, t) => {
  let { selection: { main: e } } = n.state;
  if (t.target.draggable) {
    let s = n.docView.nearest(t.target);
    if (s && s.isWidget) {
      let o = s.posAtStart, r = o + s.length;
      (o >= e.to || r <= e.from) && (e = k.range(o, r));
    }
  }
  let { inputState: i } = n;
  return i.mouseSelection && (i.mouseSelection.dragging = !0), i.draggedContent = e, t.dataTransfer && (t.dataTransfer.setData("Text", Se(n.state, ri, n.state.sliceDoc(e.from, e.to))), t.dataTransfer.effectAllowed = "copyMove"), !1;
};
X.dragend = (n) => (n.inputState.draggedContent = null, !1);
function Ni(n, t, e, i) {
  if (e = Se(n.state, oi, e), !e)
    return;
  let s = n.posAtCoords({ x: t.clientX, y: t.clientY }, !1), { draggedContent: o } = n.inputState, r = i && o && Ao(n, t) ? { from: o.from, to: o.to } : null, l = { from: s, insert: e }, h = n.state.changes(r ? [r, l] : l);
  n.focus(), n.dispatch({
    changes: h,
    selection: { anchor: h.mapPos(s, -1), head: h.mapPos(s, 1) },
    userEvent: r ? "move.drop" : "input.drop"
  }), n.inputState.draggedContent = null;
}
X.drop = (n, t) => {
  if (!t.dataTransfer)
    return !1;
  if (n.state.readOnly)
    return !0;
  let e = t.dataTransfer.files;
  if (e && e.length) {
    let i = Array(e.length), s = 0, o = () => {
      ++s == e.length && Ni(n, t, i.filter((r) => r != null).join(n.state.lineBreak), !1);
    };
    for (let r = 0; r < e.length; r++) {
      let l = new FileReader();
      l.onerror = o, l.onload = () => {
        /[\x00-\x08\x0e-\x1f]{2}/.test(l.result) || (i[r] = l.result), o();
      }, l.readAsText(e[r]);
    }
    return !0;
  } else {
    let i = t.dataTransfer.getData("Text");
    if (i)
      return Ni(n, t, i, !0), !0;
  }
  return !1;
};
X.paste = (n, t) => {
  if (n.state.readOnly)
    return !0;
  n.observer.flush();
  let e = Js ? null : t.clipboardData;
  return e ? (Zs(n, e.getData("text/plain") || e.getData("text/uri-list")), !0) : (To(n), !1);
};
function Po(n, t) {
  let e = n.dom.parentNode;
  if (!e)
    return;
  let i = e.appendChild(document.createElement("textarea"));
  i.style.cssText = "position: fixed; left: -10000px; top: 10px", i.value = t, i.focus(), i.selectionEnd = t.length, i.selectionStart = 0, setTimeout(() => {
    i.remove(), n.focus();
  }, 50);
}
function Ho(n) {
  let t = [], e = [], i = !1;
  for (let s of n.selection.ranges)
    s.empty || (t.push(n.sliceDoc(s.from, s.to)), e.push(s));
  if (!t.length) {
    let s = -1;
    for (let { from: o } of n.selection.ranges) {
      let r = n.doc.lineAt(o);
      r.number > s && (t.push(r.text), e.push({ from: r.from, to: Math.min(n.doc.length, r.to + 1) })), s = r.number;
    }
    i = !0;
  }
  return { text: Se(n, ri, t.join(n.lineBreak)), ranges: e, linewise: i };
}
let Xe = null;
X.copy = X.cut = (n, t) => {
  let { text: e, ranges: i, linewise: s } = Ho(n.state);
  if (!e && !s)
    return !1;
  Xe = s ? e : null, t.type == "cut" && !n.state.readOnly && n.dispatch({
    changes: i,
    scrollIntoView: !0,
    userEvent: "delete.cut"
  });
  let o = Js ? null : t.clipboardData;
  return o ? (o.clearData(), o.setData("text/plain", e), !0) : (Po(n, e), !1);
};
const en = /* @__PURE__ */ Sn.define();
function sn(n, t) {
  let e = [];
  for (let i of n.facet(Fs)) {
    let s = i(n, t);
    s && e.push(s);
  }
  return e.length ? n.update({ effects: e, annotations: en.of(!0) }) : null;
}
function nn(n) {
  setTimeout(() => {
    let t = n.hasFocus;
    if (t != n.inputState.notifiedFocused) {
      let e = sn(n.state, t);
      e ? n.dispatch(e) : n.update([]);
    }
  }, 10);
}
j.focus = (n) => {
  n.inputState.lastFocusTime = Date.now(), !n.scrollDOM.scrollTop && (n.inputState.lastScrollTop || n.inputState.lastScrollLeft) && (n.scrollDOM.scrollTop = n.inputState.lastScrollTop, n.scrollDOM.scrollLeft = n.inputState.lastScrollLeft), nn(n);
};
j.blur = (n) => {
  n.observer.clearSelectionRange(), nn(n);
};
j.compositionstart = j.compositionupdate = (n) => {
  n.observer.editContext || (n.inputState.compositionFirstChange == null && (n.inputState.compositionFirstChange = !0), n.inputState.composing < 0 && (n.inputState.composing = 0));
};
j.compositionend = (n) => {
  n.observer.editContext || (n.inputState.composing = -1, n.inputState.compositionEndedAt = Date.now(), n.inputState.compositionPendingKey = !0, n.inputState.compositionPendingChange = n.observer.pendingRecords().length > 0, n.inputState.compositionFirstChange = null, y.chrome && y.android ? n.observer.flushSoon() : n.inputState.compositionPendingChange ? Promise.resolve().then(() => n.observer.flush()) : setTimeout(() => {
    n.inputState.composing < 0 && n.docView.hasComposition && n.update([]);
  }, 50));
};
j.contextmenu = (n) => {
  n.inputState.lastContextMenu = Date.now();
};
X.beforeinput = (n, t) => {
  var e, i;
  if (t.inputType == "insertReplacementText" && n.observer.editContext) {
    let o = (e = t.dataTransfer) === null || e === void 0 ? void 0 : e.getData("text/plain"), r = t.getTargetRanges();
    if (o && r.length) {
      let l = r[0], h = n.posAtDOM(l.startContainer, l.startOffset), f = n.posAtDOM(l.endContainer, l.endOffset);
      return fi(n, { from: h, to: f, insert: n.state.toText(o) }, null), !0;
    }
  }
  let s;
  if (y.chrome && y.android && (s = Us.find((o) => o.inputType == t.inputType)) && (n.observer.delayAndroidKey(s.key, s.keyCode), s.key == "Backspace" || s.key == "Delete")) {
    let o = ((i = window.visualViewport) === null || i === void 0 ? void 0 : i.height) || 0;
    setTimeout(() => {
      var r;
      (((r = window.visualViewport) === null || r === void 0 ? void 0 : r.height) || 0) > o + 10 && n.hasFocus && (n.contentDOM.blur(), n.focus());
    }, 100);
  }
  return y.ios && t.inputType == "deleteContentForward" && n.observer.flushSoon(), y.safari && t.inputType == "insertText" && n.inputState.composing >= 0 && setTimeout(() => j.compositionend(n, t), 20), !1;
};
const Vi = /* @__PURE__ */ new Set();
function No(n) {
  Vi.has(n) || (Vi.add(n), n.addEventListener("copy", () => {
  }), n.addEventListener("cut", () => {
  }));
}
const Fi = ["pre-wrap", "normal", "pre-line", "break-spaces"];
let Dt = !1;
function Wi() {
  Dt = !1;
}
class Vo {
  constructor(t) {
    this.lineWrapping = t, this.doc = q.empty, this.heightSamples = {}, this.lineHeight = 14, this.charWidth = 7, this.textHeight = 14, this.lineLength = 30;
  }
  heightForGap(t, e) {
    let i = this.doc.lineAt(e).number - this.doc.lineAt(t).number + 1;
    return this.lineWrapping && (i += Math.max(0, Math.ceil((e - t - i * this.lineLength * 0.5) / this.lineLength))), this.lineHeight * i;
  }
  heightForLine(t) {
    return this.lineWrapping ? (1 + Math.max(0, Math.ceil((t - this.lineLength) / Math.max(1, this.lineLength - 5)))) * this.lineHeight : this.lineHeight;
  }
  setDoc(t) {
    return this.doc = t, this;
  }
  mustRefreshForWrapping(t) {
    return Fi.indexOf(t) > -1 != this.lineWrapping;
  }
  mustRefreshForHeights(t) {
    let e = !1;
    for (let i = 0; i < t.length; i++) {
      let s = t[i];
      s < 0 ? i++ : this.heightSamples[Math.floor(s * 10)] || (e = !0, this.heightSamples[Math.floor(s * 10)] = !0);
    }
    return e;
  }
  refresh(t, e, i, s, o, r) {
    let l = Fi.indexOf(t) > -1, h = Math.round(e) != Math.round(this.lineHeight) || this.lineWrapping != l;
    if (this.lineWrapping = l, this.lineHeight = e, this.charWidth = i, this.textHeight = s, this.lineLength = o, h) {
      this.heightSamples = {};
      for (let f = 0; f < r.length; f++) {
        let a = r[f];
        a < 0 ? f++ : this.heightSamples[Math.floor(a * 10)] = !0;
      }
    }
    return h;
  }
}
class Fo {
  constructor(t, e) {
    this.from = t, this.heights = e, this.index = 0;
  }
  get more() {
    return this.index < this.heights.length;
  }
}
class $ {
  /**
  @internal
  */
  constructor(t, e, i, s, o) {
    this.from = t, this.length = e, this.top = i, this.height = s, this._content = o;
  }
  /**
  The type of element this is. When querying lines, this may be
  an array of all the blocks that make up the line.
  */
  get type() {
    return typeof this._content == "number" ? F.Text : Array.isArray(this._content) ? this._content : this._content.type;
  }
  /**
  The end of the element as a document position.
  */
  get to() {
    return this.from + this.length;
  }
  /**
  The bottom position of the element.
  */
  get bottom() {
    return this.top + this.height;
  }
  /**
  If this is a widget block, this will return the widget
  associated with it.
  */
  get widget() {
    return this._content instanceof ht ? this._content.widget : null;
  }
  /**
  If this is a textblock, this holds the number of line breaks
  that appear in widgets inside the block.
  */
  get widgetLineBreaks() {
    return typeof this._content == "number" ? this._content : 0;
  }
  /**
  @internal
  */
  join(t) {
    let e = (Array.isArray(this._content) ? this._content : [this]).concat(Array.isArray(t._content) ? t._content : [t]);
    return new $(this.from, this.length + t.length, this.top, this.height + t.height, e);
  }
}
var T = /* @__PURE__ */ function(n) {
  return n[n.ByPos = 0] = "ByPos", n[n.ByHeight = 1] = "ByHeight", n[n.ByPosNoHeight = 2] = "ByPosNoHeight", n;
}(T || (T = {}));
const fe = 1e-3;
class W {
  constructor(t, e, i = 2) {
    this.length = t, this.height = e, this.flags = i;
  }
  get outdated() {
    return (this.flags & 2) > 0;
  }
  set outdated(t) {
    this.flags = (t ? 2 : 0) | this.flags & -3;
  }
  setHeight(t) {
    this.height != t && (Math.abs(this.height - t) > fe && (Dt = !0), this.height = t);
  }
  // Base case is to replace a leaf node, which simply builds a tree
  // from the new nodes and returns that (HeightMapBranch and
  // HeightMapGap override this to actually use from/to)
  replace(t, e, i) {
    return W.of(i);
  }
  // Again, these are base cases, and are overridden for branch and gap nodes.
  decomposeLeft(t, e) {
    e.push(this);
  }
  decomposeRight(t, e) {
    e.push(this);
  }
  applyChanges(t, e, i, s) {
    let o = this, r = i.doc;
    for (let l = s.length - 1; l >= 0; l--) {
      let { fromA: h, toA: f, fromB: a, toB: c } = s[l], d = o.lineAt(h, T.ByPosNoHeight, i.setDoc(e), 0, 0), u = d.to >= f ? d : o.lineAt(f, T.ByPosNoHeight, i, 0, 0);
      for (c += u.to - f, f = u.to; l > 0 && d.from <= s[l - 1].toA; )
        h = s[l - 1].fromA, a = s[l - 1].fromB, l--, h < d.from && (d = o.lineAt(h, T.ByPosNoHeight, i, 0, 0));
      a += d.from - h, h = d.from;
      let p = ci.build(i.setDoc(r), t, a, c);
      o = be(o, o.replace(h, f, p));
    }
    return o.updateHeight(i, 0);
  }
  static empty() {
    return new K(0, 0);
  }
  // nodes uses null values to indicate the position of line breaks.
  // There are never line breaks at the start or end of the array, or
  // two line breaks next to each other, and the array isn't allowed
  // to be empty (same restrictions as return value from the builder).
  static of(t) {
    if (t.length == 1)
      return t[0];
    let e = 0, i = t.length, s = 0, o = 0;
    for (; ; )
      if (e == i)
        if (s > o * 2) {
          let l = t[e - 1];
          l.break ? t.splice(--e, 1, l.left, null, l.right) : t.splice(--e, 1, l.left, l.right), i += 1 + l.break, s -= l.size;
        } else if (o > s * 2) {
          let l = t[i];
          l.break ? t.splice(i, 1, l.left, null, l.right) : t.splice(i, 1, l.left, l.right), i += 2 + l.break, o -= l.size;
        } else
          break;
      else if (s < o) {
        let l = t[e++];
        l && (s += l.size);
      } else {
        let l = t[--i];
        l && (o += l.size);
      }
    let r = 0;
    return t[e - 1] == null ? (r = 1, e--) : t[e] == null && (r = 1, i++), new Wo(W.of(t.slice(0, e)), r, W.of(t.slice(i)));
  }
}
function be(n, t) {
  return n == t ? n : (n.constructor != t.constructor && (Dt = !0), t);
}
W.prototype.size = 1;
class on extends W {
  constructor(t, e, i) {
    super(t, e), this.deco = i;
  }
  blockAt(t, e, i, s) {
    return new $(s, this.length, i, this.height, this.deco || 0);
  }
  lineAt(t, e, i, s, o) {
    return this.blockAt(0, i, s, o);
  }
  forEachLine(t, e, i, s, o, r) {
    t <= o + this.length && e >= o && r(this.blockAt(0, i, s, o));
  }
  updateHeight(t, e = 0, i = !1, s) {
    return s && s.from <= e && s.more && this.setHeight(s.heights[s.index++]), this.outdated = !1, this;
  }
  toString() {
    return `block(${this.length})`;
  }
}
class K extends on {
  constructor(t, e) {
    super(t, e, null), this.collapsed = 0, this.widgetHeight = 0, this.breaks = 0;
  }
  blockAt(t, e, i, s) {
    return new $(s, this.length, i, this.height, this.breaks);
  }
  replace(t, e, i) {
    let s = i[0];
    return i.length == 1 && (s instanceof K || s instanceof H && s.flags & 4) && Math.abs(this.length - s.length) < 10 ? (s instanceof H ? s = new K(s.length, this.height) : s.height = this.height, this.outdated || (s.outdated = !1), s) : W.of(i);
  }
  updateHeight(t, e = 0, i = !1, s) {
    return s && s.from <= e && s.more ? this.setHeight(s.heights[s.index++]) : (i || this.outdated) && this.setHeight(Math.max(this.widgetHeight, t.heightForLine(this.length - this.collapsed)) + this.breaks * t.lineHeight), this.outdated = !1, this;
  }
  toString() {
    return `line(${this.length}${this.collapsed ? -this.collapsed : ""}${this.widgetHeight ? ":" + this.widgetHeight : ""})`;
  }
}
class H extends W {
  constructor(t) {
    super(t, 0);
  }
  heightMetrics(t, e) {
    let i = t.doc.lineAt(e).number, s = t.doc.lineAt(e + this.length).number, o = s - i + 1, r, l = 0;
    if (t.lineWrapping) {
      let h = Math.min(this.height, t.lineHeight * o);
      r = h / o, this.length > o + 1 && (l = (this.height - h) / (this.length - o - 1));
    } else
      r = this.height / o;
    return { firstLine: i, lastLine: s, perLine: r, perChar: l };
  }
  blockAt(t, e, i, s) {
    let { firstLine: o, lastLine: r, perLine: l, perChar: h } = this.heightMetrics(e, s);
    if (e.lineWrapping) {
      let f = s + (t < e.lineHeight ? 0 : Math.round(Math.max(0, Math.min(1, (t - i) / this.height)) * this.length)), a = e.doc.lineAt(f), c = l + a.length * h, d = Math.max(i, t - c / 2);
      return new $(a.from, a.length, d, c, 0);
    } else {
      let f = Math.max(0, Math.min(r - o, Math.floor((t - i) / l))), { from: a, length: c } = e.doc.line(o + f);
      return new $(a, c, i + l * f, l, 0);
    }
  }
  lineAt(t, e, i, s, o) {
    if (e == T.ByHeight)
      return this.blockAt(t, i, s, o);
    if (e == T.ByPosNoHeight) {
      let { from: u, to: p } = i.doc.lineAt(t);
      return new $(u, p - u, 0, 0, 0);
    }
    let { firstLine: r, perLine: l, perChar: h } = this.heightMetrics(i, o), f = i.doc.lineAt(t), a = l + f.length * h, c = f.number - r, d = s + l * c + h * (f.from - o - c);
    return new $(f.from, f.length, Math.max(s, Math.min(d, s + this.height - a)), a, 0);
  }
  forEachLine(t, e, i, s, o, r) {
    t = Math.max(t, o), e = Math.min(e, o + this.length);
    let { firstLine: l, perLine: h, perChar: f } = this.heightMetrics(i, o);
    for (let a = t, c = s; a <= e; ) {
      let d = i.doc.lineAt(a);
      if (a == t) {
        let p = d.number - l;
        c += h * p + f * (t - o - p);
      }
      let u = h + f * d.length;
      r(new $(d.from, d.length, c, u, 0)), c += u, a = d.to + 1;
    }
  }
  replace(t, e, i) {
    let s = this.length - e;
    if (s > 0) {
      let o = i[i.length - 1];
      o instanceof H ? i[i.length - 1] = new H(o.length + s) : i.push(null, new H(s - 1));
    }
    if (t > 0) {
      let o = i[0];
      o instanceof H ? i[0] = new H(t + o.length) : i.unshift(new H(t - 1), null);
    }
    return W.of(i);
  }
  decomposeLeft(t, e) {
    e.push(new H(t - 1), null);
  }
  decomposeRight(t, e) {
    e.push(null, new H(this.length - t - 1));
  }
  updateHeight(t, e = 0, i = !1, s) {
    let o = e + this.length;
    if (s && s.from <= e + this.length && s.more) {
      let r = [], l = Math.max(e, s.from), h = -1;
      for (s.from > e && r.push(new H(s.from - e - 1).updateHeight(t, e)); l <= o && s.more; ) {
        let a = t.doc.lineAt(l).length;
        r.length && r.push(null);
        let c = s.heights[s.index++];
        h == -1 ? h = c : Math.abs(c - h) >= fe && (h = -2);
        let d = new K(a, c);
        d.outdated = !1, r.push(d), l += a + 1;
      }
      l <= o && r.push(null, new H(o - l).updateHeight(t, l));
      let f = W.of(r);
      return (h < 0 || Math.abs(f.height - this.height) >= fe || Math.abs(h - this.heightMetrics(t, e).perLine) >= fe) && (Dt = !0), be(this, f);
    } else (i || this.outdated) && (this.setHeight(t.heightForGap(e, e + this.length)), this.outdated = !1);
    return this;
  }
  toString() {
    return `gap(${this.length})`;
  }
}
class Wo extends W {
  constructor(t, e, i) {
    super(t.length + e + i.length, t.height + i.height, e | (t.outdated || i.outdated ? 2 : 0)), this.left = t, this.right = i, this.size = t.size + i.size;
  }
  get break() {
    return this.flags & 1;
  }
  blockAt(t, e, i, s) {
    let o = i + this.left.height;
    return t < o ? this.left.blockAt(t, e, i, s) : this.right.blockAt(t, e, o, s + this.left.length + this.break);
  }
  lineAt(t, e, i, s, o) {
    let r = s + this.left.height, l = o + this.left.length + this.break, h = e == T.ByHeight ? t < r : t < l, f = h ? this.left.lineAt(t, e, i, s, o) : this.right.lineAt(t, e, i, r, l);
    if (this.break || (h ? f.to < l : f.from > l))
      return f;
    let a = e == T.ByPosNoHeight ? T.ByPosNoHeight : T.ByPos;
    return h ? f.join(this.right.lineAt(l, a, i, r, l)) : this.left.lineAt(l, a, i, s, o).join(f);
  }
  forEachLine(t, e, i, s, o, r) {
    let l = s + this.left.height, h = o + this.left.length + this.break;
    if (this.break)
      t < h && this.left.forEachLine(t, e, i, s, o, r), e >= h && this.right.forEachLine(t, e, i, l, h, r);
    else {
      let f = this.lineAt(h, T.ByPos, i, s, o);
      t < f.from && this.left.forEachLine(t, f.from - 1, i, s, o, r), f.to >= t && f.from <= e && r(f), e > f.to && this.right.forEachLine(f.to + 1, e, i, l, h, r);
    }
  }
  replace(t, e, i) {
    let s = this.left.length + this.break;
    if (e < s)
      return this.balanced(this.left.replace(t, e, i), this.right);
    if (t > this.left.length)
      return this.balanced(this.left, this.right.replace(t - s, e - s, i));
    let o = [];
    t > 0 && this.decomposeLeft(t, o);
    let r = o.length;
    for (let l of i)
      o.push(l);
    if (t > 0 && zi(o, r - 1), e < this.length) {
      let l = o.length;
      this.decomposeRight(e, o), zi(o, l);
    }
    return W.of(o);
  }
  decomposeLeft(t, e) {
    let i = this.left.length;
    if (t <= i)
      return this.left.decomposeLeft(t, e);
    e.push(this.left), this.break && (i++, t >= i && e.push(null)), t > i && this.right.decomposeLeft(t - i, e);
  }
  decomposeRight(t, e) {
    let i = this.left.length, s = i + this.break;
    if (t >= s)
      return this.right.decomposeRight(t - s, e);
    t < i && this.left.decomposeRight(t, e), this.break && t < s && e.push(null), e.push(this.right);
  }
  balanced(t, e) {
    return t.size > 2 * e.size || e.size > 2 * t.size ? W.of(this.break ? [t, null, e] : [t, e]) : (this.left = be(this.left, t), this.right = be(this.right, e), this.setHeight(t.height + e.height), this.outdated = t.outdated || e.outdated, this.size = t.size + e.size, this.length = t.length + this.break + e.length, this);
  }
  updateHeight(t, e = 0, i = !1, s) {
    let { left: o, right: r } = this, l = e + o.length + this.break, h = null;
    return s && s.from <= e + o.length && s.more ? h = o = o.updateHeight(t, e, i, s) : o.updateHeight(t, e, i), s && s.from <= l + r.length && s.more ? h = r = r.updateHeight(t, l, i, s) : r.updateHeight(t, l, i), h ? this.balanced(o, r) : (this.height = this.left.height + this.right.height, this.outdated = !1, this);
  }
  toString() {
    return this.left + (this.break ? " " : "-") + this.right;
  }
}
function zi(n, t) {
  let e, i;
  n[t] == null && (e = n[t - 1]) instanceof H && (i = n[t + 1]) instanceof H && n.splice(t - 1, 3, new H(e.length + 1 + i.length));
}
const zo = 5;
class ci {
  constructor(t, e) {
    this.pos = t, this.oracle = e, this.nodes = [], this.lineStart = -1, this.lineEnd = -1, this.covering = null, this.writtenTo = t;
  }
  get isCovered() {
    return this.covering && this.nodes[this.nodes.length - 1] == this.covering;
  }
  span(t, e) {
    if (this.lineStart > -1) {
      let i = Math.min(e, this.lineEnd), s = this.nodes[this.nodes.length - 1];
      s instanceof K ? s.length += i - this.pos : (i > this.pos || !this.isCovered) && this.nodes.push(new K(i - this.pos, -1)), this.writtenTo = i, e > i && (this.nodes.push(null), this.writtenTo++, this.lineStart = -1);
    }
    this.pos = e;
  }
  point(t, e, i) {
    if (t < e || i.heightRelevant) {
      let s = i.widget ? i.widget.estimatedHeight : 0, o = i.widget ? i.widget.lineBreaks : 0;
      s < 0 && (s = this.oracle.lineHeight);
      let r = e - t;
      i.block ? this.addBlock(new on(r, s, i)) : (r || o || s >= zo) && this.addLineDeco(s, o, r);
    } else e > t && this.span(t, e);
    this.lineEnd > -1 && this.lineEnd < this.pos && (this.lineEnd = this.oracle.doc.lineAt(this.pos).to);
  }
  enterLine() {
    if (this.lineStart > -1)
      return;
    let { from: t, to: e } = this.oracle.doc.lineAt(this.pos);
    this.lineStart = t, this.lineEnd = e, this.writtenTo < t && ((this.writtenTo < t - 1 || this.nodes[this.nodes.length - 1] == null) && this.nodes.push(this.blankContent(this.writtenTo, t - 1)), this.nodes.push(null)), this.pos > t && this.nodes.push(new K(this.pos - t, -1)), this.writtenTo = this.pos;
  }
  blankContent(t, e) {
    let i = new H(e - t);
    return this.oracle.doc.lineAt(t).to == e && (i.flags |= 4), i;
  }
  ensureLine() {
    this.enterLine();
    let t = this.nodes.length ? this.nodes[this.nodes.length - 1] : null;
    if (t instanceof K)
      return t;
    let e = new K(0, -1);
    return this.nodes.push(e), e;
  }
  addBlock(t) {
    this.enterLine();
    let e = t.deco;
    e && e.startSide > 0 && !this.isCovered && this.ensureLine(), this.nodes.push(t), this.writtenTo = this.pos = this.pos + t.length, e && e.endSide > 0 && (this.covering = t);
  }
  addLineDeco(t, e, i) {
    let s = this.ensureLine();
    s.length += i, s.collapsed += i, s.widgetHeight = Math.max(s.widgetHeight, t), s.breaks += e, this.writtenTo = this.pos = this.pos + i;
  }
  finish(t) {
    let e = this.nodes.length == 0 ? null : this.nodes[this.nodes.length - 1];
    this.lineStart > -1 && !(e instanceof K) && !this.isCovered ? this.nodes.push(new K(0, -1)) : (this.writtenTo < this.pos || e == null) && this.nodes.push(this.blankContent(this.writtenTo, this.pos));
    let i = t;
    for (let s of this.nodes)
      s instanceof K && s.updateHeight(this.oracle, i), i += s ? s.length : 1;
    return this.nodes;
  }
  // Always called with a region that on both sides either stretches
  // to a line break or the end of the document.
  // The returned array uses null to indicate line breaks, but never
  // starts or ends in a line break, or has multiple line breaks next
  // to each other.
  static build(t, e, i, s) {
    let o = new ci(i, t);
    return V.spans(e, i, s, o, 0), o.finish(i);
  }
}
function Io(n, t, e) {
  let i = new Ko();
  return V.compare(n, t, e, i, 0), i.changes;
}
class Ko {
  constructor() {
    this.changes = [];
  }
  compareRange() {
  }
  comparePoint(t, e, i, s) {
    (t < e || i && i.heightRelevant || s && s.heightRelevant) && he(t, e, this.changes, 5);
  }
}
function qo(n, t) {
  let e = n.getBoundingClientRect(), i = n.ownerDocument, s = i.defaultView || window, o = Math.max(0, e.left), r = Math.min(s.innerWidth, e.right), l = Math.max(0, e.top), h = Math.min(s.innerHeight, e.bottom);
  for (let f = n.parentNode; f && f != i.body; )
    if (f.nodeType == 1) {
      let a = f, c = window.getComputedStyle(a);
      if ((a.scrollHeight > a.clientHeight || a.scrollWidth > a.clientWidth) && c.overflow != "visible") {
        let d = a.getBoundingClientRect();
        o = Math.max(o, d.left), r = Math.min(r, d.right), l = Math.max(l, d.top), h = Math.min(f == n.parentNode ? s.innerHeight : h, d.bottom);
      }
      f = c.position == "absolute" || c.position == "fixed" ? a.offsetParent : a.parentNode;
    } else if (f.nodeType == 11)
      f = f.host;
    else
      break;
  return {
    left: o - e.left,
    right: Math.max(o, r) - e.left,
    top: l - (e.top + t),
    bottom: Math.max(l, h) - (e.top + t)
  };
}
function Yo(n) {
  let t = n.getBoundingClientRect(), e = n.ownerDocument.defaultView || window;
  return t.left < e.innerWidth && t.right > 0 && t.top < e.innerHeight && t.bottom > 0;
}
function jo(n, t) {
  let e = n.getBoundingClientRect();
  return {
    left: 0,
    right: e.right - e.left,
    top: t,
    bottom: e.bottom - (e.top + t)
  };
}
class De {
  constructor(t, e, i, s) {
    this.from = t, this.to = e, this.size = i, this.displaySize = s;
  }
  static same(t, e) {
    if (t.length != e.length)
      return !1;
    for (let i = 0; i < t.length; i++) {
      let s = t[i], o = e[i];
      if (s.from != o.from || s.to != o.to || s.size != o.size)
        return !1;
    }
    return !0;
  }
  draw(t, e) {
    return E.replace({
      widget: new _o(this.displaySize * (e ? t.scaleY : t.scaleX), e)
    }).range(this.from, this.to);
  }
}
class _o extends Tt {
  constructor(t, e) {
    super(), this.size = t, this.vertical = e;
  }
  eq(t) {
    return t.size == this.size && t.vertical == this.vertical;
  }
  toDOM() {
    let t = document.createElement("div");
    return this.vertical ? t.style.height = this.size + "px" : (t.style.width = this.size + "px", t.style.height = "2px", t.style.display = "inline-block"), t;
  }
  get estimatedHeight() {
    return this.vertical ? this.size : -1;
  }
}
class Ii {
  constructor(t) {
    this.state = t, this.pixelViewport = { left: 0, right: window.innerWidth, top: 0, bottom: 0 }, this.inView = !0, this.paddingTop = 0, this.paddingBottom = 0, this.contentDOMWidth = 0, this.contentDOMHeight = 0, this.editorHeight = 0, this.editorWidth = 0, this.scrollTop = 0, this.scrolledToBottom = !1, this.scaleX = 1, this.scaleY = 1, this.scrollAnchorPos = 0, this.scrollAnchorHeight = -1, this.scaler = Ki, this.scrollTarget = null, this.printing = !1, this.mustMeasureContent = !0, this.defaultTextDirection = B.LTR, this.visibleRanges = [], this.mustEnforceCursorAssoc = !1;
    let e = t.facet(li).some((i) => typeof i != "function" && i.class == "cm-lineWrapping");
    this.heightOracle = new Vo(e), this.stateDeco = t.facet(It).filter((i) => typeof i != "function"), this.heightMap = W.empty().applyChanges(this.stateDeco, q.empty, this.heightOracle.setDoc(t.doc), [new Y(0, 0, 0, t.doc.length)]);
    for (let i = 0; i < 2 && (this.viewport = this.getViewport(0, null), !!this.updateForViewport()); i++)
      ;
    this.updateViewportLines(), this.lineGaps = this.ensureLineGaps([]), this.lineGapDeco = E.set(this.lineGaps.map((i) => i.draw(this, !1))), this.computeVisibleRanges();
  }
  updateForViewport() {
    let t = [this.viewport], { main: e } = this.state.selection;
    for (let i = 0; i <= 1; i++) {
      let s = i ? e.head : e.anchor;
      if (!t.some(({ from: o, to: r }) => s >= o && s <= r)) {
        let { from: o, to: r } = this.lineBlockAt(s);
        t.push(new te(o, r));
      }
    }
    return this.viewports = t.sort((i, s) => i.from - s.from), this.updateScaler();
  }
  updateScaler() {
    let t = this.scaler;
    return this.scaler = this.heightMap.height <= 7e6 ? Ki : new di(this.heightOracle, this.heightMap, this.viewports), t.eq(this.scaler) ? 0 : 2;
  }
  updateViewportLines() {
    this.viewportLines = [], this.heightMap.forEachLine(this.viewport.from, this.viewport.to, this.heightOracle.setDoc(this.state.doc), 0, 0, (t) => {
      this.viewportLines.push(Pt(t, this.scaler));
    });
  }
  update(t, e = null) {
    this.state = t.state;
    let i = this.stateDeco;
    this.stateDeco = this.state.facet(It).filter((a) => typeof a != "function");
    let s = t.changedRanges, o = Y.extendWithRanges(s, Io(i, this.stateDeco, t ? t.changes : hs.empty(this.state.doc.length))), r = this.heightMap.height, l = this.scrolledToBottom ? null : this.scrollAnchorAt(this.scrollTop);
    Wi(), this.heightMap = this.heightMap.applyChanges(this.stateDeco, t.startState.doc, this.heightOracle.setDoc(this.state.doc), o), (this.heightMap.height != r || Dt) && (t.flags |= 2), l ? (this.scrollAnchorPos = t.changes.mapPos(l.from, -1), this.scrollAnchorHeight = l.top) : (this.scrollAnchorPos = -1, this.scrollAnchorHeight = r);
    let h = o.length ? this.mapViewport(this.viewport, t.changes) : this.viewport;
    (e && (e.range.head < h.from || e.range.head > h.to) || !this.viewportIsAppropriate(h)) && (h = this.getViewport(0, e));
    let f = h.from != this.viewport.from || h.to != this.viewport.to;
    this.viewport = h, t.flags |= this.updateForViewport(), (f || !t.changes.empty || t.flags & 2) && this.updateViewportLines(), (this.lineGaps.length || this.viewport.to - this.viewport.from > 4e3) && this.updateLineGaps(this.ensureLineGaps(this.mapLineGaps(this.lineGaps, t.changes))), t.flags |= this.computeVisibleRanges(t.changes), e && (this.scrollTarget = e), !this.mustEnforceCursorAssoc && t.selectionSet && t.view.lineWrapping && t.state.selection.main.empty && t.state.selection.main.assoc && !t.state.facet(zs) && (this.mustEnforceCursorAssoc = !0);
  }
  measure(t) {
    let e = t.contentDOM, i = window.getComputedStyle(e), s = this.heightOracle, o = i.whiteSpace;
    this.defaultTextDirection = i.direction == "rtl" ? B.RTL : B.LTR;
    let r = this.heightOracle.mustRefreshForWrapping(o), l = e.getBoundingClientRect(), h = r || this.mustMeasureContent || this.contentDOMHeight != l.height;
    this.contentDOMHeight = l.height, this.mustMeasureContent = !1;
    let f = 0, a = 0;
    if (l.width && l.height) {
      let { scaleX: x, scaleY: v } = us(e, l);
      (x > 5e-3 && Math.abs(this.scaleX - x) > 5e-3 || v > 5e-3 && Math.abs(this.scaleY - v) > 5e-3) && (this.scaleX = x, this.scaleY = v, f |= 16, r = h = !0);
    }
    let c = (parseInt(i.paddingTop) || 0) * this.scaleY, d = (parseInt(i.paddingBottom) || 0) * this.scaleY;
    (this.paddingTop != c || this.paddingBottom != d) && (this.paddingTop = c, this.paddingBottom = d, f |= 18), this.editorWidth != t.scrollDOM.clientWidth && (s.lineWrapping && (h = !0), this.editorWidth = t.scrollDOM.clientWidth, f |= 16);
    let u = t.scrollDOM.scrollTop * this.scaleY;
    this.scrollTop != u && (this.scrollAnchorHeight = -1, this.scrollTop = u), this.scrolledToBottom = ms(t.scrollDOM);
    let p = (this.printing ? jo : qo)(e, this.paddingTop), g = p.top - this.pixelViewport.top, m = p.bottom - this.pixelViewport.bottom;
    this.pixelViewport = p;
    let b = this.pixelViewport.bottom > this.pixelViewport.top && this.pixelViewport.right > this.pixelViewport.left;
    if (b != this.inView && (this.inView = b, b && (h = !0)), !this.inView && !this.scrollTarget && !Yo(t.dom))
      return 0;
    let w = l.width;
    if ((this.contentDOMWidth != w || this.editorHeight != t.scrollDOM.clientHeight) && (this.contentDOMWidth = l.width, this.editorHeight = t.scrollDOM.clientHeight, f |= 16), h) {
      let x = t.docView.measureVisibleLineHeights(this.viewport);
      if (s.mustRefreshForHeights(x) && (r = !0), r || s.lineWrapping && Math.abs(w - this.contentDOMWidth) > s.charWidth) {
        let { lineHeight: v, charWidth: C, textHeight: R } = t.docView.measureTextSize();
        r = v > 0 && s.refresh(o, v, C, R, Math.max(5, w / C), x), r && (t.docView.minWidth = 0, f |= 16);
      }
      g > 0 && m > 0 ? a = Math.max(g, m) : g < 0 && m < 0 && (a = Math.min(g, m)), Wi();
      for (let v of this.viewports) {
        let C = v.from == this.viewport.from ? x : t.docView.measureVisibleLineHeights(v);
        this.heightMap = (r ? W.empty().applyChanges(this.stateDeco, q.empty, this.heightOracle, [new Y(0, 0, 0, t.state.doc.length)]) : this.heightMap).updateHeight(s, 0, r, new Fo(v.from, C));
      }
      Dt && (f |= 2);
    }
    let S = !this.viewportIsAppropriate(this.viewport, a) || this.scrollTarget && (this.scrollTarget.range.head < this.viewport.from || this.scrollTarget.range.head > this.viewport.to);
    return S && (f & 2 && (f |= this.updateScaler()), this.viewport = this.getViewport(a, this.scrollTarget), f |= this.updateForViewport()), (f & 2 || S) && this.updateViewportLines(), (this.lineGaps.length || this.viewport.to - this.viewport.from > 4e3) && this.updateLineGaps(this.ensureLineGaps(r ? [] : this.lineGaps, t)), f |= this.computeVisibleRanges(), this.mustEnforceCursorAssoc && (this.mustEnforceCursorAssoc = !1, t.docView.enforceCursorAssoc()), f;
  }
  get visibleTop() {
    return this.scaler.fromDOM(this.pixelViewport.top);
  }
  get visibleBottom() {
    return this.scaler.fromDOM(this.pixelViewport.bottom);
  }
  getViewport(t, e) {
    let i = 0.5 - Math.max(-0.5, Math.min(0.5, t / 1e3 / 2)), s = this.heightMap, o = this.heightOracle, { visibleTop: r, visibleBottom: l } = this, h = new te(s.lineAt(r - i * 1e3, T.ByHeight, o, 0, 0).from, s.lineAt(l + (1 - i) * 1e3, T.ByHeight, o, 0, 0).to);
    if (e) {
      let { head: f } = e.range;
      if (f < h.from || f > h.to) {
        let a = Math.min(this.editorHeight, this.pixelViewport.bottom - this.pixelViewport.top), c = s.lineAt(f, T.ByPos, o, 0, 0), d;
        e.y == "center" ? d = (c.top + c.bottom) / 2 - a / 2 : e.y == "start" || e.y == "nearest" && f < h.from ? d = c.top : d = c.bottom - a, h = new te(s.lineAt(d - 1e3 / 2, T.ByHeight, o, 0, 0).from, s.lineAt(d + a + 1e3 / 2, T.ByHeight, o, 0, 0).to);
      }
    }
    return h;
  }
  mapViewport(t, e) {
    let i = e.mapPos(t.from, -1), s = e.mapPos(t.to, 1);
    return new te(this.heightMap.lineAt(i, T.ByPos, this.heightOracle, 0, 0).from, this.heightMap.lineAt(s, T.ByPos, this.heightOracle, 0, 0).to);
  }
  // Checks if a given viewport covers the visible part of the
  // document and not too much beyond that.
  viewportIsAppropriate({ from: t, to: e }, i = 0) {
    if (!this.inView)
      return !0;
    let { top: s } = this.heightMap.lineAt(t, T.ByPos, this.heightOracle, 0, 0), { bottom: o } = this.heightMap.lineAt(e, T.ByPos, this.heightOracle, 0, 0), { visibleTop: r, visibleBottom: l } = this;
    return (t == 0 || s <= r - Math.max(10, Math.min(
      -i,
      250
      /* VP.MaxCoverMargin */
    ))) && (e == this.state.doc.length || o >= l + Math.max(10, Math.min(
      i,
      250
      /* VP.MaxCoverMargin */
    ))) && s > r - 2 * 1e3 && o < l + 2 * 1e3;
  }
  mapLineGaps(t, e) {
    if (!t.length || e.empty)
      return t;
    let i = [];
    for (let s of t)
      e.touchesRange(s.from, s.to) || i.push(new De(e.mapPos(s.from), e.mapPos(s.to), s.size, s.displaySize));
    return i;
  }
  // Computes positions in the viewport where the start or end of a
  // line should be hidden, trying to reuse existing line gaps when
  // appropriate to avoid unneccesary redraws.
  // Uses crude character-counting for the positioning and sizing,
  // since actual DOM coordinates aren't always available and
  // predictable. Relies on generous margins (see LG.Margin) to hide
  // the artifacts this might produce from the user.
  ensureLineGaps(t, e) {
    let i = this.heightOracle.lineWrapping, s = i ? 1e4 : 2e3, o = s >> 1, r = s << 1;
    if (this.defaultTextDirection != B.LTR && !i)
      return [];
    let l = [], h = (a, c, d, u) => {
      if (c - a < o)
        return;
      let p = this.state.selection.main, g = [p.from];
      p.empty || g.push(p.to);
      for (let b of g)
        if (b > a && b < c) {
          h(a, b - 10, d, u), h(b + 10, c, d, u);
          return;
        }
      let m = Go(t, (b) => b.from >= d.from && b.to <= d.to && Math.abs(b.from - a) < o && Math.abs(b.to - c) < o && !g.some((w) => b.from < w && b.to > w));
      if (!m) {
        if (c < d.to && e && i && e.visibleRanges.some((S) => S.from <= c && S.to >= c)) {
          let S = e.moveToLineBoundary(k.cursor(c), !1, !0).head;
          S > a && (c = S);
        }
        let b = this.gapSize(d, a, c, u), w = i || b < 2e6 ? b : 2e6;
        m = new De(a, c, b, w);
      }
      l.push(m);
    }, f = (a) => {
      if (a.length < r || a.type != F.Text)
        return;
      let c = Xo(a.from, a.to, this.stateDeco);
      if (c.total < r)
        return;
      let d = this.scrollTarget ? this.scrollTarget.range.head : null, u, p;
      if (i) {
        let g = s / this.heightOracle.lineLength * this.heightOracle.lineHeight, m, b;
        if (d != null) {
          let w = ie(c, d), S = ((this.visibleBottom - this.visibleTop) / 2 + g) / a.height;
          m = w - S, b = w + S;
        } else
          m = (this.visibleTop - a.top - g) / a.height, b = (this.visibleBottom - a.top + g) / a.height;
        u = ee(c, m), p = ee(c, b);
      } else {
        let g = c.total * this.heightOracle.charWidth, m = s * this.heightOracle.charWidth, b = 0;
        if (g > 2e6)
          for (let C of t)
            C.from >= a.from && C.from < a.to && C.size != C.displaySize && C.from * this.heightOracle.charWidth + b < this.pixelViewport.left && (b = C.size - C.displaySize);
        let w = this.pixelViewport.left + b, S = this.pixelViewport.right + b, x, v;
        if (d != null) {
          let C = ie(c, d), R = ((S - w) / 2 + m) / g;
          x = C - R, v = C + R;
        } else
          x = (w - m) / g, v = (S + m) / g;
        u = ee(c, x), p = ee(c, v);
      }
      u > a.from && h(a.from, u, a, c), p < a.to && h(p, a.to, a, c);
    };
    for (let a of this.viewportLines)
      Array.isArray(a.type) ? a.type.forEach(f) : f(a);
    return l;
  }
  gapSize(t, e, i, s) {
    let o = ie(s, i) - ie(s, e);
    return this.heightOracle.lineWrapping ? t.height * o : s.total * this.heightOracle.charWidth * o;
  }
  updateLineGaps(t) {
    De.same(t, this.lineGaps) || (this.lineGaps = t, this.lineGapDeco = E.set(t.map((e) => e.draw(this, this.heightOracle.lineWrapping))));
  }
  computeVisibleRanges(t) {
    let e = this.stateDeco;
    this.lineGaps.length && (e = e.concat(this.lineGapDeco));
    let i = [];
    V.spans(e, this.viewport.from, this.viewport.to, {
      span(o, r) {
        i.push({ from: o, to: r });
      },
      point() {
      }
    }, 20);
    let s = 0;
    if (i.length != this.visibleRanges.length)
      s = 12;
    else
      for (let o = 0; o < i.length && !(s & 8); o++) {
        let r = this.visibleRanges[o], l = i[o];
        (r.from != l.from || r.to != l.to) && (s |= 4, t && t.mapPos(r.from, -1) == l.from && t.mapPos(r.to, 1) == l.to || (s |= 8));
      }
    return this.visibleRanges = i, s;
  }
  lineBlockAt(t) {
    return t >= this.viewport.from && t <= this.viewport.to && this.viewportLines.find((e) => e.from <= t && e.to >= t) || Pt(this.heightMap.lineAt(t, T.ByPos, this.heightOracle, 0, 0), this.scaler);
  }
  lineBlockAtHeight(t) {
    return t >= this.viewportLines[0].top && t <= this.viewportLines[this.viewportLines.length - 1].bottom && this.viewportLines.find((e) => e.top <= t && e.bottom >= t) || Pt(this.heightMap.lineAt(this.scaler.fromDOM(t), T.ByHeight, this.heightOracle, 0, 0), this.scaler);
  }
  scrollAnchorAt(t) {
    let e = this.lineBlockAtHeight(t + 8);
    return e.from >= this.viewport.from || this.viewportLines[0].top - t > 200 ? e : this.viewportLines[0];
  }
  elementAtHeight(t) {
    return Pt(this.heightMap.blockAt(this.scaler.fromDOM(t), this.heightOracle, 0, 0), this.scaler);
  }
  get docHeight() {
    return this.scaler.toDOM(this.heightMap.height);
  }
  get contentHeight() {
    return this.docHeight + this.paddingTop + this.paddingBottom;
  }
}
class te {
  constructor(t, e) {
    this.from = t, this.to = e;
  }
}
function Xo(n, t, e) {
  let i = [], s = n, o = 0;
  return V.spans(e, n, t, {
    span() {
    },
    point(r, l) {
      r > s && (i.push({ from: s, to: r }), o += r - s), s = l;
    }
  }, 20), s < t && (i.push({ from: s, to: t }), o += t - s), { total: o, ranges: i };
}
function ee({ total: n, ranges: t }, e) {
  if (e <= 0)
    return t[0].from;
  if (e >= 1)
    return t[t.length - 1].to;
  let i = Math.floor(n * e);
  for (let s = 0; ; s++) {
    let { from: o, to: r } = t[s], l = r - o;
    if (i <= l)
      return o + i;
    i -= l;
  }
}
function ie(n, t) {
  let e = 0;
  for (let { from: i, to: s } of n.ranges) {
    if (t <= s) {
      e += t - i;
      break;
    }
    e += s - i;
  }
  return e / n.total;
}
function Go(n, t) {
  for (let e of n)
    if (t(e))
      return e;
}
const Ki = {
  toDOM(n) {
    return n;
  },
  fromDOM(n) {
    return n;
  },
  scale: 1,
  eq(n) {
    return n == this;
  }
};
class di {
  constructor(t, e, i) {
    let s = 0, o = 0, r = 0;
    this.viewports = i.map(({ from: l, to: h }) => {
      let f = e.lineAt(l, T.ByPos, t, 0, 0).top, a = e.lineAt(h, T.ByPos, t, 0, 0).bottom;
      return s += a - f, { from: l, to: h, top: f, bottom: a, domTop: 0, domBottom: 0 };
    }), this.scale = (7e6 - s) / (e.height - s);
    for (let l of this.viewports)
      l.domTop = r + (l.top - o) * this.scale, r = l.domBottom = l.domTop + (l.bottom - l.top), o = l.bottom;
  }
  toDOM(t) {
    for (let e = 0, i = 0, s = 0; ; e++) {
      let o = e < this.viewports.length ? this.viewports[e] : null;
      if (!o || t < o.top)
        return s + (t - i) * this.scale;
      if (t <= o.bottom)
        return o.domTop + (t - o.top);
      i = o.bottom, s = o.domBottom;
    }
  }
  fromDOM(t) {
    for (let e = 0, i = 0, s = 0; ; e++) {
      let o = e < this.viewports.length ? this.viewports[e] : null;
      if (!o || t < o.domTop)
        return i + (t - s) / this.scale;
      if (t <= o.domBottom)
        return o.top + (t - o.domTop);
      i = o.bottom, s = o.domBottom;
    }
  }
  eq(t) {
    return t instanceof di ? this.scale == t.scale && this.viewports.length == t.viewports.length && this.viewports.every((e, i) => e.from == t.viewports[i].from && e.to == t.viewports[i].to) : !1;
  }
}
function Pt(n, t) {
  if (t.scale == 1)
    return n;
  let e = t.toDOM(n.top), i = t.toDOM(n.bottom);
  return new $(n.from, n.length, e, i - e, Array.isArray(n._content) ? n._content.map((s) => Pt(s, t)) : n._content);
}
const se = /* @__PURE__ */ M.define({ combine: (n) => n.join(" ") }), Ge = /* @__PURE__ */ M.define({ combine: (n) => n.indexOf(!0) > -1 }), $e = /* @__PURE__ */ Ct.newName(), rn = /* @__PURE__ */ Ct.newName(), ln = /* @__PURE__ */ Ct.newName(), hn = { "&light": "." + rn, "&dark": "." + ln };
function Ue(n, t, e) {
  return new Ct(t, {
    finish(i) {
      return /&/.test(i) ? i.replace(/&\w*/, (s) => {
        if (s == "&")
          return n;
        if (!e || !e[s])
          throw new RangeError(`Unsupported selector: ${s}`);
        return e[s];
      }) : n + " " + i;
    }
  });
}
const $o = /* @__PURE__ */ Ue("." + $e, {
  "&": {
    position: "relative !important",
    boxSizing: "border-box",
    "&.cm-focused": {
      // Provide a simple default outline to make sure a focused
      // editor is visually distinct. Can't leave the default behavior
      // because that will apply to the content element, which is
      // inside the scrollable container and doesn't include the
      // gutters. We also can't use an 'auto' outline, since those
      // are, for some reason, drawn behind the element content, which
      // will cause things like the active line background to cover
      // the outline (#297).
      outline: "1px dotted #212121"
    },
    display: "flex !important",
    flexDirection: "column"
  },
  ".cm-scroller": {
    display: "flex !important",
    alignItems: "flex-start !important",
    fontFamily: "monospace",
    lineHeight: 1.4,
    height: "100%",
    overflowX: "auto",
    position: "relative",
    zIndex: 0,
    overflowAnchor: "none"
  },
  ".cm-content": {
    margin: 0,
    flexGrow: 2,
    flexShrink: 0,
    display: "block",
    whiteSpace: "pre",
    wordWrap: "normal",
    // https://github.com/codemirror/dev/issues/456
    boxSizing: "border-box",
    minHeight: "100%",
    padding: "4px 0",
    outline: "none",
    "&[contenteditable=true]": {
      WebkitUserModify: "read-write-plaintext-only"
    }
  },
  ".cm-lineWrapping": {
    whiteSpace_fallback: "pre-wrap",
    // For IE
    whiteSpace: "break-spaces",
    wordBreak: "break-word",
    // For Safari, which doesn't support overflow-wrap: anywhere
    overflowWrap: "anywhere",
    flexShrink: 1
  },
  "&light .cm-content": { caretColor: "black" },
  "&dark .cm-content": { caretColor: "white" },
  ".cm-line": {
    display: "block",
    padding: "0 2px 0 6px"
  },
  ".cm-layer": {
    position: "absolute",
    left: 0,
    top: 0,
    contain: "size style",
    "& > *": {
      position: "absolute"
    }
  },
  "&light .cm-selectionBackground": {
    background: "#d9d9d9"
  },
  "&dark .cm-selectionBackground": {
    background: "#222"
  },
  "&light.cm-focused > .cm-scroller > .cm-selectionLayer .cm-selectionBackground": {
    background: "#d7d4f0"
  },
  "&dark.cm-focused > .cm-scroller > .cm-selectionLayer .cm-selectionBackground": {
    background: "#233"
  },
  ".cm-cursorLayer": {
    pointerEvents: "none"
  },
  "&.cm-focused > .cm-scroller > .cm-cursorLayer": {
    animation: "steps(1) cm-blink 1.2s infinite"
  },
  // Two animations defined so that we can switch between them to
  // restart the animation without forcing another style
  // recomputation.
  "@keyframes cm-blink": { "0%": {}, "50%": { opacity: 0 }, "100%": {} },
  "@keyframes cm-blink2": { "0%": {}, "50%": { opacity: 0 }, "100%": {} },
  ".cm-cursor, .cm-dropCursor": {
    borderLeft: "1.2px solid black",
    marginLeft: "-0.6px",
    pointerEvents: "none"
  },
  ".cm-cursor": {
    display: "none"
  },
  "&dark .cm-cursor": {
    borderLeftColor: "#ddd"
  },
  ".cm-dropCursor": {
    position: "absolute"
  },
  "&.cm-focused > .cm-scroller > .cm-cursorLayer .cm-cursor": {
    display: "block"
  },
  ".cm-iso": {
    unicodeBidi: "isolate"
  },
  ".cm-announced": {
    position: "fixed",
    top: "-10000px"
  },
  "@media print": {
    ".cm-announced": { display: "none" }
  },
  "&light .cm-activeLine": { backgroundColor: "#cceeff44" },
  "&dark .cm-activeLine": { backgroundColor: "#99eeff33" },
  "&light .cm-specialChar": { color: "red" },
  "&dark .cm-specialChar": { color: "#f78" },
  ".cm-gutters": {
    flexShrink: 0,
    display: "flex",
    height: "100%",
    boxSizing: "border-box",
    zIndex: 200
  },
  ".cm-gutters-before": { insetInlineStart: 0 },
  ".cm-gutters-after": { insetInlineEnd: 0 },
  "&light .cm-gutters": {
    backgroundColor: "#f5f5f5",
    color: "#6c6c6c",
    border: "0px solid #ddd",
    "&.cm-gutters-before": { borderRightWidth: "1px" },
    "&.cm-gutters-after": { borderLeftWidth: "1px" }
  },
  "&dark .cm-gutters": {
    backgroundColor: "#333338",
    color: "#ccc"
  },
  ".cm-gutter": {
    display: "flex !important",
    // Necessary -- prevents margin collapsing
    flexDirection: "column",
    flexShrink: 0,
    boxSizing: "border-box",
    minHeight: "100%",
    overflow: "hidden"
  },
  ".cm-gutterElement": {
    boxSizing: "border-box"
  },
  ".cm-lineNumbers .cm-gutterElement": {
    padding: "0 3px 0 5px",
    minWidth: "20px",
    textAlign: "right",
    whiteSpace: "nowrap"
  },
  "&light .cm-activeLineGutter": {
    backgroundColor: "#e2f2ff"
  },
  "&dark .cm-activeLineGutter": {
    backgroundColor: "#222227"
  },
  ".cm-panels": {
    boxSizing: "border-box",
    position: "sticky",
    left: 0,
    right: 0,
    zIndex: 300
  },
  "&light .cm-panels": {
    backgroundColor: "#f5f5f5",
    color: "black"
  },
  "&light .cm-panels-top": {
    borderBottom: "1px solid #ddd"
  },
  "&light .cm-panels-bottom": {
    borderTop: "1px solid #ddd"
  },
  "&dark .cm-panels": {
    backgroundColor: "#333338",
    color: "white"
  },
  ".cm-dialog": {
    padding: "2px 19px 4px 6px",
    position: "relative",
    "& label": { fontSize: "80%" }
  },
  ".cm-dialog-close": {
    position: "absolute",
    top: "3px",
    right: "4px",
    backgroundColor: "inherit",
    border: "none",
    font: "inherit",
    fontSize: "14px",
    padding: "0"
  },
  ".cm-tab": {
    display: "inline-block",
    overflow: "hidden",
    verticalAlign: "bottom"
  },
  ".cm-widgetBuffer": {
    verticalAlign: "text-top",
    height: "1em",
    width: 0,
    display: "inline"
  },
  ".cm-placeholder": {
    color: "#888",
    display: "inline-block",
    verticalAlign: "top",
    userSelect: "none"
  },
  ".cm-highlightSpace": {
    backgroundImage: "radial-gradient(circle at 50% 55%, #aaa 20%, transparent 5%)",
    backgroundPosition: "center"
  },
  ".cm-highlightTab": {
    backgroundImage: `url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="200" height="20"><path stroke="%23888" stroke-width="1" fill="none" d="M1 10H196L190 5M190 15L196 10M197 4L197 16"/></svg>')`,
    backgroundSize: "auto 100%",
    backgroundPosition: "right 90%",
    backgroundRepeat: "no-repeat"
  },
  ".cm-trailingSpace": {
    backgroundColor: "#ff332255"
  },
  ".cm-button": {
    verticalAlign: "middle",
    color: "inherit",
    fontSize: "70%",
    padding: ".2em 1em",
    borderRadius: "1px"
  },
  "&light .cm-button": {
    backgroundImage: "linear-gradient(#eff1f5, #d9d9df)",
    border: "1px solid #888",
    "&:active": {
      backgroundImage: "linear-gradient(#b4b4b4, #d0d3d6)"
    }
  },
  "&dark .cm-button": {
    backgroundImage: "linear-gradient(#393939, #111)",
    border: "1px solid #888",
    "&:active": {
      backgroundImage: "linear-gradient(#111, #333)"
    }
  },
  ".cm-textfield": {
    verticalAlign: "middle",
    color: "inherit",
    fontSize: "70%",
    border: "1px solid silver",
    padding: ".2em .5em"
  },
  "&light .cm-textfield": {
    backgroundColor: "white"
  },
  "&dark .cm-textfield": {
    border: "1px solid #555",
    backgroundColor: "inherit"
  }
}, hn), Uo = {
  childList: !0,
  characterData: !0,
  subtree: !0,
  attributes: !0,
  characterDataOldValue: !0
}, Oe = y.ie && y.ie_version <= 11;
class Qo {
  constructor(t) {
    this.view = t, this.active = !1, this.editContext = null, this.selectionRange = new En(), this.selectionChanged = !1, this.delayedFlush = -1, this.resizeTimeout = -1, this.queue = [], this.delayedAndroidKey = null, this.flushingAndroidKey = -1, this.lastChange = 0, this.scrollTargets = [], this.intersection = null, this.resizeScroll = null, this.intersecting = !1, this.gapIntersection = null, this.gaps = [], this.printQuery = null, this.parentCheck = -1, this.dom = t.contentDOM, this.observer = new MutationObserver((e) => {
      for (let i of e)
        this.queue.push(i);
      (y.ie && y.ie_version <= 11 || y.ios && t.composing) && e.some((i) => i.type == "childList" && i.removedNodes.length || i.type == "characterData" && i.oldValue.length > i.target.nodeValue.length) ? this.flushSoon() : this.flush();
    }), window.EditContext && y.android && t.constructor.EDIT_CONTEXT !== !1 && // Chrome <126 doesn't support inverted selections in edit context (#1392)
    !(y.chrome && y.chrome_version < 126) && (this.editContext = new Zo(t), t.state.facet(tt) && (t.contentDOM.editContext = this.editContext.editContext)), Oe && (this.onCharData = (e) => {
      this.queue.push({
        target: e.target,
        type: "characterData",
        oldValue: e.prevValue
      }), this.flushSoon();
    }), this.onSelectionChange = this.onSelectionChange.bind(this), this.onResize = this.onResize.bind(this), this.onPrint = this.onPrint.bind(this), this.onScroll = this.onScroll.bind(this), window.matchMedia && (this.printQuery = window.matchMedia("print")), typeof ResizeObserver == "function" && (this.resizeScroll = new ResizeObserver(() => {
      var e;
      ((e = this.view.docView) === null || e === void 0 ? void 0 : e.lastUpdate) < Date.now() - 75 && this.onResize();
    }), this.resizeScroll.observe(t.scrollDOM)), this.addWindowListeners(this.win = t.win), this.start(), typeof IntersectionObserver == "function" && (this.intersection = new IntersectionObserver((e) => {
      this.parentCheck < 0 && (this.parentCheck = setTimeout(this.listenForScroll.bind(this), 1e3)), e.length > 0 && e[e.length - 1].intersectionRatio > 0 != this.intersecting && (this.intersecting = !this.intersecting, this.intersecting != this.view.inView && this.onScrollChanged(document.createEvent("Event")));
    }, { threshold: [0, 1e-3] }), this.intersection.observe(this.dom), this.gapIntersection = new IntersectionObserver((e) => {
      e.length > 0 && e[e.length - 1].intersectionRatio > 0 && this.onScrollChanged(document.createEvent("Event"));
    }, {})), this.listenForScroll(), this.readSelectionRange();
  }
  onScrollChanged(t) {
    this.view.inputState.runHandlers("scroll", t), this.intersecting && this.view.measure();
  }
  onScroll(t) {
    this.intersecting && this.flush(!1), this.editContext && this.view.requestMeasure(this.editContext.measureReq), this.onScrollChanged(t);
  }
  onResize() {
    this.resizeTimeout < 0 && (this.resizeTimeout = setTimeout(() => {
      this.resizeTimeout = -1, this.view.requestMeasure();
    }, 50));
  }
  onPrint(t) {
    (t.type == "change" || !t.type) && !t.matches || (this.view.viewState.printing = !0, this.view.measure(), setTimeout(() => {
      this.view.viewState.printing = !1, this.view.requestMeasure();
    }, 500));
  }
  updateGaps(t) {
    if (this.gapIntersection && (t.length != this.gaps.length || this.gaps.some((e, i) => e != t[i]))) {
      this.gapIntersection.disconnect();
      for (let e of t)
        this.gapIntersection.observe(e);
      this.gaps = t;
    }
  }
  onSelectionChange(t) {
    let e = this.selectionChanged;
    if (!this.readSelectionRange() || this.delayedAndroidKey)
      return;
    let { view: i } = this, s = this.selectionRange;
    if (i.state.facet(tt) ? i.root.activeElement != this.dom : !le(this.dom, s))
      return;
    let o = s.anchorNode && i.docView.nearest(s.anchorNode);
    if (o && o.ignoreEvent(t)) {
      e || (this.selectionChanged = !1);
      return;
    }
    (y.ie && y.ie_version <= 11 || y.android && y.chrome) && !i.state.selection.main.empty && // (Selection.isCollapsed isn't reliable on IE)
    s.focusNode && Vt(s.focusNode, s.focusOffset, s.anchorNode, s.anchorOffset) ? this.flushSoon() : this.flush(!1);
  }
  readSelectionRange() {
    let { view: t } = this, e = zt(t.root);
    if (!e)
      return !1;
    let i = y.safari && t.root.nodeType == 11 && t.root.activeElement == this.dom && Jo(this.view, e) || e;
    if (!i || this.selectionRange.eq(i))
      return !1;
    let s = le(this.dom, i);
    return s && !this.selectionChanged && t.inputState.lastFocusTime > Date.now() - 200 && t.inputState.lastTouchTime < Date.now() - 300 && Ln(this.dom, i) ? (this.view.inputState.lastFocusTime = 0, t.docView.updateSelection(), !1) : (this.selectionRange.setRange(i), s && (this.selectionChanged = !0), !0);
  }
  setSelectionRange(t, e) {
    this.selectionRange.set(t.node, t.offset, e.node, e.offset), this.selectionChanged = !1;
  }
  clearSelectionRange() {
    this.selectionRange.set(null, 0, null, 0);
  }
  listenForScroll() {
    this.parentCheck = -1;
    let t = 0, e = null;
    for (let i = this.dom; i; )
      if (i.nodeType == 1)
        !e && t < this.scrollTargets.length && this.scrollTargets[t] == i ? t++ : e || (e = this.scrollTargets.slice(0, t)), e && e.push(i), i = i.assignedSlot || i.parentNode;
      else if (i.nodeType == 11)
        i = i.host;
      else
        break;
    if (t < this.scrollTargets.length && !e && (e = this.scrollTargets.slice(0, t)), e) {
      for (let i of this.scrollTargets)
        i.removeEventListener("scroll", this.onScroll);
      for (let i of this.scrollTargets = e)
        i.addEventListener("scroll", this.onScroll);
    }
  }
  ignore(t) {
    if (!this.active)
      return t();
    try {
      return this.stop(), t();
    } finally {
      this.start(), this.clear();
    }
  }
  start() {
    this.active || (this.observer.observe(this.dom, Uo), Oe && this.dom.addEventListener("DOMCharacterDataModified", this.onCharData), this.active = !0);
  }
  stop() {
    this.active && (this.active = !1, this.observer.disconnect(), Oe && this.dom.removeEventListener("DOMCharacterDataModified", this.onCharData));
  }
  // Throw away any pending changes
  clear() {
    this.processRecords(), this.queue.length = 0, this.selectionChanged = !1;
  }
  // Chrome Android, especially in combination with GBoard, not only
  // doesn't reliably fire regular key events, but also often
  // surrounds the effect of enter or backspace with a bunch of
  // composition events that, when interrupted, cause text duplication
  // or other kinds of corruption. This hack makes the editor back off
  // from handling DOM changes for a moment when such a key is
  // detected (via beforeinput or keydown), and then tries to flush
  // them or, if that has no effect, dispatches the given key.
  delayAndroidKey(t, e) {
    var i;
    if (!this.delayedAndroidKey) {
      let s = () => {
        let o = this.delayedAndroidKey;
        o && (this.clearDelayedAndroidKey(), this.view.inputState.lastKeyCode = o.keyCode, this.view.inputState.lastKeyTime = Date.now(), !this.flush() && o.force && St(this.dom, o.key, o.keyCode));
      };
      this.flushingAndroidKey = this.view.win.requestAnimationFrame(s);
    }
    (!this.delayedAndroidKey || t == "Enter") && (this.delayedAndroidKey = {
      key: t,
      keyCode: e,
      // Only run the key handler when no changes are detected if
      // this isn't coming right after another change, in which case
      // it is probably part of a weird chain of updates, and should
      // be ignored if it returns the DOM to its previous state.
      force: this.lastChange < Date.now() - 50 || !!(!((i = this.delayedAndroidKey) === null || i === void 0) && i.force)
    });
  }
  clearDelayedAndroidKey() {
    this.win.cancelAnimationFrame(this.flushingAndroidKey), this.delayedAndroidKey = null, this.flushingAndroidKey = -1;
  }
  flushSoon() {
    this.delayedFlush < 0 && (this.delayedFlush = this.view.win.requestAnimationFrame(() => {
      this.delayedFlush = -1, this.flush();
    }));
  }
  forceFlush() {
    this.delayedFlush >= 0 && (this.view.win.cancelAnimationFrame(this.delayedFlush), this.delayedFlush = -1), this.flush();
  }
  pendingRecords() {
    for (let t of this.observer.takeRecords())
      this.queue.push(t);
    return this.queue;
  }
  processRecords() {
    let t = this.pendingRecords();
    t.length && (this.queue = []);
    let e = -1, i = -1, s = !1;
    for (let o of t) {
      let r = this.readMutation(o);
      r && (r.typeOver && (s = !0), e == -1 ? { from: e, to: i } = r : (e = Math.min(r.from, e), i = Math.max(r.to, i)));
    }
    return { from: e, to: i, typeOver: s };
  }
  readChange() {
    let { from: t, to: e, typeOver: i } = this.processRecords(), s = this.selectionChanged && le(this.dom, this.selectionRange);
    if (t < 0 && !s)
      return null;
    t > -1 && (this.lastChange = Date.now()), this.view.inputState.lastFocusTime = 0, this.selectionChanged = !1;
    let o = new go(this.view, t, e, i);
    return this.view.docView.domChanged = { newSel: o.newSel ? o.newSel.main : null }, o;
  }
  // Apply pending changes, if any
  flush(t = !0) {
    if (this.delayedFlush >= 0 || this.delayedAndroidKey)
      return !1;
    t && this.readSelectionRange();
    let e = this.readChange();
    if (!e)
      return this.view.requestMeasure(), !1;
    let i = this.view.state, s = $s(this.view, e);
    return this.view.state == i && (e.domChanged || e.newSel && !e.newSel.main.eq(this.view.state.selection.main)) && this.view.update([]), s;
  }
  readMutation(t) {
    let e = this.view.docView.nearest(t.target);
    if (!e || e.ignoreMutation(t))
      return null;
    if (e.markDirty(t.type == "attributes"), t.type == "attributes" && (e.flags |= 4), t.type == "childList") {
      let i = qi(e, t.previousSibling || t.target.previousSibling, -1), s = qi(e, t.nextSibling || t.target.nextSibling, 1);
      return {
        from: i ? e.posAfter(i) : e.posAtStart,
        to: s ? e.posBefore(s) : e.posAtEnd,
        typeOver: !1
      };
    } else return t.type == "characterData" ? { from: e.posAtStart, to: e.posAtEnd, typeOver: t.target.nodeValue == t.oldValue } : null;
  }
  setWindow(t) {
    t != this.win && (this.removeWindowListeners(this.win), this.win = t, this.addWindowListeners(this.win));
  }
  addWindowListeners(t) {
    t.addEventListener("resize", this.onResize), this.printQuery ? this.printQuery.addEventListener ? this.printQuery.addEventListener("change", this.onPrint) : this.printQuery.addListener(this.onPrint) : t.addEventListener("beforeprint", this.onPrint), t.addEventListener("scroll", this.onScroll), t.document.addEventListener("selectionchange", this.onSelectionChange);
  }
  removeWindowListeners(t) {
    t.removeEventListener("scroll", this.onScroll), t.removeEventListener("resize", this.onResize), this.printQuery ? this.printQuery.removeEventListener ? this.printQuery.removeEventListener("change", this.onPrint) : this.printQuery.removeListener(this.onPrint) : t.removeEventListener("beforeprint", this.onPrint), t.document.removeEventListener("selectionchange", this.onSelectionChange);
  }
  update(t) {
    this.editContext && (this.editContext.update(t), t.startState.facet(tt) != t.state.facet(tt) && (t.view.contentDOM.editContext = t.state.facet(tt) ? this.editContext.editContext : null));
  }
  destroy() {
    var t, e, i;
    this.stop(), (t = this.intersection) === null || t === void 0 || t.disconnect(), (e = this.gapIntersection) === null || e === void 0 || e.disconnect(), (i = this.resizeScroll) === null || i === void 0 || i.disconnect();
    for (let s of this.scrollTargets)
      s.removeEventListener("scroll", this.onScroll);
    this.removeWindowListeners(this.win), clearTimeout(this.parentCheck), clearTimeout(this.resizeTimeout), this.win.cancelAnimationFrame(this.delayedFlush), this.win.cancelAnimationFrame(this.flushingAndroidKey), this.editContext && (this.view.contentDOM.editContext = null, this.editContext.destroy());
  }
}
function qi(n, t, e) {
  for (; t; ) {
    let i = O.get(t);
    if (i && i.parent == n)
      return i;
    let s = t.parentNode;
    t = s != n.dom ? s : e > 0 ? t.nextSibling : t.previousSibling;
  }
  return null;
}
function Yi(n, t) {
  let e = t.startContainer, i = t.startOffset, s = t.endContainer, o = t.endOffset, r = n.docView.domAtPos(n.state.selection.main.anchor);
  return Vt(r.node, r.offset, s, o) && ([e, i, s, o] = [s, o, e, i]), { anchorNode: e, anchorOffset: i, focusNode: s, focusOffset: o };
}
function Jo(n, t) {
  if (t.getComposedRanges) {
    let s = t.getComposedRanges(n.root)[0];
    if (s)
      return Yi(n, s);
  }
  let e = null;
  function i(s) {
    s.preventDefault(), s.stopImmediatePropagation(), e = s.getTargetRanges()[0];
  }
  return n.contentDOM.addEventListener("beforeinput", i, !0), n.dom.ownerDocument.execCommand("indent"), n.contentDOM.removeEventListener("beforeinput", i, !0), e ? Yi(n, e) : null;
}
class Zo {
  constructor(t) {
    this.from = 0, this.to = 0, this.pendingContextChange = null, this.handlers = /* @__PURE__ */ Object.create(null), this.composing = null, this.resetRange(t.state);
    let e = this.editContext = new window.EditContext({
      text: t.state.doc.sliceString(this.from, this.to),
      selectionStart: this.toContextPos(Math.max(this.from, Math.min(this.to, t.state.selection.main.anchor))),
      selectionEnd: this.toContextPos(t.state.selection.main.head)
    });
    this.handlers.textupdate = (i) => {
      let s = t.state.selection.main, { anchor: o, head: r } = s, l = this.toEditorPos(i.updateRangeStart), h = this.toEditorPos(i.updateRangeEnd);
      t.inputState.composing >= 0 && !this.composing && (this.composing = { contextBase: i.updateRangeStart, editorBase: l, drifted: !1 });
      let f = { from: l, to: h, insert: q.of(i.text.split(`
`)) };
      if (f.from == this.from && o < this.from ? f.from = o : f.to == this.to && o > this.to && (f.to = o), f.from == f.to && !f.insert.length) {
        let a = k.single(this.toEditorPos(i.selectionStart), this.toEditorPos(i.selectionEnd));
        a.main.eq(s) || t.dispatch({ selection: a, userEvent: "select" });
        return;
      }
      if ((y.mac || y.android) && f.from == r - 1 && /^\. ?$/.test(i.text) && t.contentDOM.getAttribute("autocorrect") == "off" && (f = { from: l, to: h, insert: q.of([i.text.replace(".", " ")]) }), this.pendingContextChange = f, !t.state.readOnly) {
        let a = this.to - this.from + (f.to - f.from + f.insert.length);
        fi(t, f, k.single(this.toEditorPos(i.selectionStart, a), this.toEditorPos(i.selectionEnd, a)));
      }
      this.pendingContextChange && (this.revertPending(t.state), this.setSelection(t.state));
    }, this.handlers.characterboundsupdate = (i) => {
      let s = [], o = null;
      for (let r = this.toEditorPos(i.rangeStart), l = this.toEditorPos(i.rangeEnd); r < l; r++) {
        let h = t.coordsForChar(r);
        o = h && new DOMRect(h.left, h.top, h.right - h.left, h.bottom - h.top) || o || new DOMRect(), s.push(o);
      }
      e.updateCharacterBounds(i.rangeStart, s);
    }, this.handlers.textformatupdate = (i) => {
      let s = [];
      for (let o of i.getTextFormats()) {
        let r = o.underlineStyle, l = o.underlineThickness;
        if (r != "None" && l != "None") {
          let h = this.toEditorPos(o.rangeStart), f = this.toEditorPos(o.rangeEnd);
          if (h < f) {
            let a = `text-decoration: underline ${r == "Dashed" ? "dashed " : r == "Squiggle" ? "wavy " : ""}${l == "Thin" ? 1 : 2}px`;
            s.push(E.mark({ attributes: { style: a } }).range(h, f));
          }
        }
      }
      t.dispatch({ effects: Ks.of(E.set(s)) });
    }, this.handlers.compositionstart = () => {
      t.inputState.composing < 0 && (t.inputState.composing = 0, t.inputState.compositionFirstChange = !0);
    }, this.handlers.compositionend = () => {
      if (t.inputState.composing = -1, t.inputState.compositionFirstChange = null, this.composing) {
        let { drifted: i } = this.composing;
        this.composing = null, i && this.reset(t.state);
      }
    };
    for (let i in this.handlers)
      e.addEventListener(i, this.handlers[i]);
    this.measureReq = { read: (i) => {
      this.editContext.updateControlBounds(i.contentDOM.getBoundingClientRect());
      let s = zt(i.root);
      s && s.rangeCount && this.editContext.updateSelectionBounds(s.getRangeAt(0).getBoundingClientRect());
    } };
  }
  applyEdits(t) {
    let e = 0, i = !1, s = this.pendingContextChange;
    return t.changes.iterChanges((o, r, l, h, f) => {
      if (i)
        return;
      let a = f.length - (r - o);
      if (s && r >= s.to)
        if (s.from == o && s.to == r && s.insert.eq(f)) {
          s = this.pendingContextChange = null, e += a, this.to += a;
          return;
        } else
          s = null, this.revertPending(t.state);
      if (o += e, r += e, r <= this.from)
        this.from += a, this.to += a;
      else if (o < this.to) {
        if (o < this.from || r > this.to || this.to - this.from + f.length > 3e4) {
          i = !0;
          return;
        }
        this.editContext.updateText(this.toContextPos(o), this.toContextPos(r), f.toString()), this.to += a;
      }
      e += a;
    }), s && !i && this.revertPending(t.state), !i;
  }
  update(t) {
    let e = this.pendingContextChange, i = t.startState.selection.main;
    this.composing && (this.composing.drifted || !t.changes.touchesRange(i.from, i.to) && t.transactions.some((s) => !s.isUserEvent("input.type") && s.changes.touchesRange(this.from, this.to))) ? (this.composing.drifted = !0, this.composing.editorBase = t.changes.mapPos(this.composing.editorBase)) : !this.applyEdits(t) || !this.rangeIsValid(t.state) ? (this.pendingContextChange = null, this.reset(t.state)) : (t.docChanged || t.selectionSet || e) && this.setSelection(t.state), (t.geometryChanged || t.docChanged || t.selectionSet) && t.view.requestMeasure(this.measureReq);
  }
  resetRange(t) {
    let { head: e } = t.selection.main;
    this.from = Math.max(
      0,
      e - 1e4
      /* CxVp.Margin */
    ), this.to = Math.min(
      t.doc.length,
      e + 1e4
      /* CxVp.Margin */
    );
  }
  reset(t) {
    this.resetRange(t), this.editContext.updateText(0, this.editContext.text.length, t.doc.sliceString(this.from, this.to)), this.setSelection(t);
  }
  revertPending(t) {
    let e = this.pendingContextChange;
    this.pendingContextChange = null, this.editContext.updateText(this.toContextPos(e.from), this.toContextPos(e.from + e.insert.length), t.doc.sliceString(e.from, e.to));
  }
  setSelection(t) {
    let { main: e } = t.selection, i = this.toContextPos(Math.max(this.from, Math.min(this.to, e.anchor))), s = this.toContextPos(e.head);
    (this.editContext.selectionStart != i || this.editContext.selectionEnd != s) && this.editContext.updateSelection(i, s);
  }
  rangeIsValid(t) {
    let { head: e } = t.selection.main;
    return !(this.from > 0 && e - this.from < 500 || this.to < t.doc.length && this.to - e < 500 || this.to - this.from > 1e4 * 3);
  }
  toEditorPos(t, e = this.to - this.from) {
    t = Math.min(t, e);
    let i = this.composing;
    return i && i.drifted ? i.editorBase + (t - i.contextBase) : t + this.from;
  }
  toContextPos(t) {
    let e = this.composing;
    return e && e.drifted ? e.contextBase + (t - e.editorBase) : t - this.from;
  }
  destroy() {
    for (let t in this.handlers)
      this.editContext.removeEventListener(t, this.handlers[t]);
  }
}
class A {
  /**
  The current editor state.
  */
  get state() {
    return this.viewState.state;
  }
  /**
  To be able to display large documents without consuming too much
  memory or overloading the browser, CodeMirror only draws the
  code that is visible (plus a margin around it) to the DOM. This
  property tells you the extent of the current drawn viewport, in
  document positions.
  */
  get viewport() {
    return this.viewState.viewport;
  }
  /**
  When there are, for example, large collapsed ranges in the
  viewport, its size can be a lot bigger than the actual visible
  content. Thus, if you are doing something like styling the
  content in the viewport, it is preferable to only do so for
  these ranges, which are the subset of the viewport that is
  actually drawn.
  */
  get visibleRanges() {
    return this.viewState.visibleRanges;
  }
  /**
  Returns false when the editor is entirely scrolled out of view
  or otherwise hidden.
  */
  get inView() {
    return this.viewState.inView;
  }
  /**
  Indicates whether the user is currently composing text via
  [IME](https://en.wikipedia.org/wiki/Input_method), and at least
  one change has been made in the current composition.
  */
  get composing() {
    return !!this.inputState && this.inputState.composing > 0;
  }
  /**
  Indicates whether the user is currently in composing state. Note
  that on some platforms, like Android, this will be the case a
  lot, since just putting the cursor on a word starts a
  composition there.
  */
  get compositionStarted() {
    return !!this.inputState && this.inputState.composing >= 0;
  }
  /**
  The document or shadow root that the view lives in.
  */
  get root() {
    return this._root;
  }
  /**
  @internal
  */
  get win() {
    return this.dom.ownerDocument.defaultView || window;
  }
  /**
  Construct a new view. You'll want to either provide a `parent`
  option, or put `view.dom` into your document after creating a
  view, so that the user can see the editor.
  */
  constructor(t = {}) {
    var e;
    this.plugins = [], this.pluginMap = /* @__PURE__ */ new Map(), this.editorAttrs = {}, this.contentAttrs = {}, this.bidiCache = [], this.destroyed = !1, this.updateState = 2, this.measureScheduled = -1, this.measureRequests = [], this.contentDOM = document.createElement("div"), this.scrollDOM = document.createElement("div"), this.scrollDOM.tabIndex = -1, this.scrollDOM.className = "cm-scroller", this.scrollDOM.appendChild(this.contentDOM), this.announceDOM = document.createElement("div"), this.announceDOM.className = "cm-announced", this.announceDOM.setAttribute("aria-live", "polite"), this.dom = document.createElement("div"), this.dom.appendChild(this.announceDOM), this.dom.appendChild(this.scrollDOM), t.parent && t.parent.appendChild(this.dom);
    let { dispatch: i } = t;
    this.dispatchTransactions = t.dispatchTransactions || i && ((s) => s.forEach((o) => i(o, this))) || ((s) => this.update(s)), this.dispatch = this.dispatch.bind(this), this._root = t.root || Bn(t.parent) || document, this.viewState = new Ii(t.state || Nt.create(t)), t.scrollTo && t.scrollTo.is(Qt) && (this.viewState.scrollTarget = t.scrollTo.value.clip(this.viewState.state)), this.plugins = this.state.facet(yt).map((s) => new Me(s));
    for (let s of this.plugins)
      s.update(this);
    this.observer = new Qo(this), this.inputState = new xo(this), this.inputState.ensureHandlers(this.plugins), this.docView = new vi(this), this.mountStyles(), this.updateAttrs(), this.updateState = 0, this.requestMeasure(), !((e = document.fonts) === null || e === void 0) && e.ready && document.fonts.ready.then(() => this.requestMeasure());
  }
  dispatch(...t) {
    let e = t.length == 1 && t[0] instanceof xn ? t : t.length == 1 && Array.isArray(t[0]) ? t[0] : [this.state.update(...t)];
    this.dispatchTransactions(e, this);
  }
  /**
  Update the view for the given array of transactions. This will
  update the visible document and selection to match the state
  produced by the transactions, and notify view plugins of the
  change. You should usually call
  [`dispatch`](https://codemirror.net/6/docs/ref/#view.EditorView.dispatch) instead, which uses this
  as a primitive.
  */
  update(t) {
    if (this.updateState != 0)
      throw new Error("Calls to EditorView.update are not allowed while an update is in progress");
    let e = !1, i = !1, s, o = this.state;
    for (let d of t) {
      if (d.startState != o)
        throw new RangeError("Trying to update state with a transaction that doesn't start from the previous state.");
      o = d.state;
    }
    if (this.destroyed) {
      this.viewState.state = o;
      return;
    }
    let r = this.hasFocus, l = 0, h = null;
    t.some((d) => d.annotation(en)) ? (this.inputState.notifiedFocused = r, l = 1) : r != this.inputState.notifiedFocused && (this.inputState.notifiedFocused = r, h = sn(o, r), h || (l = 1));
    let f = this.observer.delayedAndroidKey, a = null;
    if (f ? (this.observer.clearDelayedAndroidKey(), a = this.observer.readChange(), (a && !this.state.doc.eq(o.doc) || !this.state.selection.eq(o.selection)) && (a = null)) : this.observer.clear(), o.facet(Nt.phrases) != this.state.facet(Nt.phrases))
      return this.setState(o);
    s = me.create(this, o, t), s.flags |= l;
    let c = this.viewState.scrollTarget;
    try {
      this.updateState = 2;
      for (let d of t) {
        if (c && (c = c.map(d.changes)), d.scrollIntoView) {
          let { main: u } = d.state.selection;
          c = new vt(u.empty ? u : k.cursor(u.head, u.head > u.anchor ? -1 : 1));
        }
        for (let u of d.effects)
          u.is(Qt) && (c = u.value.clip(this.state));
      }
      this.viewState.update(s, c), this.bidiCache = ye.update(this.bidiCache, s.changes), s.empty || (this.updatePlugins(s), this.inputState.update(s)), e = this.docView.update(s), this.state.facet(Bt) != this.styleModules && this.mountStyles(), i = this.updateAttrs(), this.showAnnouncements(t), this.docView.updateSelection(e, t.some((d) => d.isUserEvent("select.pointer")));
    } finally {
      this.updateState = 0;
    }
    if (s.startState.facet(se) != s.state.facet(se) && (this.viewState.mustMeasureContent = !0), (e || i || c || this.viewState.mustEnforceCursorAssoc || this.viewState.mustMeasureContent) && this.requestMeasure(), e && this.docViewUpdate(), !s.empty)
      for (let d of this.state.facet(Ye))
        try {
          d(s);
        } catch (u) {
          U(this.state, u, "update listener");
        }
    (h || a) && Promise.resolve().then(() => {
      h && this.state == h.startState && this.dispatch(h), a && !$s(this, a) && f.force && St(this.contentDOM, f.key, f.keyCode);
    });
  }
  /**
  Reset the view to the given state. (This will cause the entire
  document to be redrawn and all view plugins to be reinitialized,
  so you should probably only use it when the new state isn't
  derived from the old state. Otherwise, use
  [`dispatch`](https://codemirror.net/6/docs/ref/#view.EditorView.dispatch) instead.)
  */
  setState(t) {
    if (this.updateState != 0)
      throw new Error("Calls to EditorView.setState are not allowed while an update is in progress");
    if (this.destroyed) {
      this.viewState.state = t;
      return;
    }
    this.updateState = 2;
    let e = this.hasFocus;
    try {
      for (let i of this.plugins)
        i.destroy(this);
      this.viewState = new Ii(t), this.plugins = t.facet(yt).map((i) => new Me(i)), this.pluginMap.clear();
      for (let i of this.plugins)
        i.update(this);
      this.docView.destroy(), this.docView = new vi(this), this.inputState.ensureHandlers(this.plugins), this.mountStyles(), this.updateAttrs(), this.bidiCache = [];
    } finally {
      this.updateState = 0;
    }
    e && this.focus(), this.requestMeasure();
  }
  updatePlugins(t) {
    let e = t.startState.facet(yt), i = t.state.facet(yt);
    if (e != i) {
      let s = [];
      for (let o of i) {
        let r = e.indexOf(o);
        if (r < 0)
          s.push(new Me(o));
        else {
          let l = this.plugins[r];
          l.mustUpdate = t, s.push(l);
        }
      }
      for (let o of this.plugins)
        o.mustUpdate != t && o.destroy(this);
      this.plugins = s, this.pluginMap.clear();
    } else
      for (let s of this.plugins)
        s.mustUpdate = t;
    for (let s = 0; s < this.plugins.length; s++)
      this.plugins[s].update(this);
    e != i && this.inputState.ensureHandlers(this.plugins);
  }
  docViewUpdate() {
    for (let t of this.plugins) {
      let e = t.value;
      if (e && e.docViewUpdate)
        try {
          e.docViewUpdate(this);
        } catch (i) {
          U(this.state, i, "doc view update listener");
        }
    }
  }
  /**
  @internal
  */
  measure(t = !0) {
    if (this.destroyed)
      return;
    if (this.measureScheduled > -1 && this.win.cancelAnimationFrame(this.measureScheduled), this.observer.delayedAndroidKey) {
      this.measureScheduled = -1, this.requestMeasure();
      return;
    }
    this.measureScheduled = 0, t && this.observer.forceFlush();
    let e = null, i = this.scrollDOM, s = i.scrollTop * this.scaleY, { scrollAnchorPos: o, scrollAnchorHeight: r } = this.viewState;
    Math.abs(s - this.viewState.scrollTop) > 1 && (r = -1), this.viewState.scrollAnchorHeight = -1;
    try {
      for (let l = 0; ; l++) {
        if (r < 0)
          if (ms(i))
            o = -1, r = this.viewState.heightMap.height;
          else {
            let u = this.viewState.scrollAnchorAt(s);
            o = u.from, r = u.top;
          }
        this.updateState = 1;
        let h = this.viewState.measure(this);
        if (!h && !this.measureRequests.length && this.viewState.scrollTarget == null)
          break;
        if (l > 5) {
          console.warn(this.measureRequests.length ? "Measure loop restarted more than 5 times" : "Viewport failed to stabilize");
          break;
        }
        let f = [];
        h & 4 || ([this.measureRequests, f] = [f, this.measureRequests]);
        let a = f.map((u) => {
          try {
            return u.read(this);
          } catch (p) {
            return U(this.state, p), ji;
          }
        }), c = me.create(this, this.state, []), d = !1;
        c.flags |= h, e ? e.flags |= h : e = c, this.updateState = 2, c.empty || (this.updatePlugins(c), this.inputState.update(c), this.updateAttrs(), d = this.docView.update(c), d && this.docViewUpdate());
        for (let u = 0; u < f.length; u++)
          if (a[u] != ji)
            try {
              let p = f[u];
              p.write && p.write(a[u], this);
            } catch (p) {
              U(this.state, p);
            }
        if (d && this.docView.updateSelection(!0), !c.viewportChanged && this.measureRequests.length == 0) {
          if (this.viewState.editorHeight)
            if (this.viewState.scrollTarget) {
              this.docView.scrollIntoView(this.viewState.scrollTarget), this.viewState.scrollTarget = null, r = -1;
              continue;
            } else {
              let p = (o < 0 ? this.viewState.heightMap.height : this.viewState.lineBlockAt(o).top) - r;
              if (p > 1 || p < -1) {
                s = s + p, i.scrollTop = s / this.scaleY, r = -1;
                continue;
              }
            }
          break;
        }
      }
    } finally {
      this.updateState = 0, this.measureScheduled = -1;
    }
    if (e && !e.empty)
      for (let l of this.state.facet(Ye))
        l(e);
  }
  /**
  Get the CSS classes for the currently active editor themes.
  */
  get themeClasses() {
    return $e + " " + (this.state.facet(Ge) ? ln : rn) + " " + this.state.facet(se);
  }
  updateAttrs() {
    let t = _i(this, qs, {
      class: "cm-editor" + (this.hasFocus ? " cm-focused " : " ") + this.themeClasses
    }), e = {
      spellcheck: "false",
      autocorrect: "off",
      autocapitalize: "off",
      writingsuggestions: "false",
      translate: "no",
      contenteditable: this.state.facet(tt) ? "true" : "false",
      class: "cm-content",
      style: `${y.tabSize}: ${this.state.tabSize}`,
      role: "textbox",
      "aria-multiline": "true"
    };
    this.state.readOnly && (e["aria-readonly"] = "true"), _i(this, li, e);
    let i = this.observer.ignore(() => {
      let s = We(this.contentDOM, this.contentAttrs, e), o = We(this.dom, this.editorAttrs, t);
      return s || o;
    });
    return this.editorAttrs = t, this.contentAttrs = e, i;
  }
  showAnnouncements(t) {
    let e = !0;
    for (let i of t)
      for (let s of i.effects)
        if (s.is(A.announce)) {
          e && (this.announceDOM.textContent = ""), e = !1;
          let o = this.announceDOM.appendChild(document.createElement("div"));
          o.textContent = s.value;
        }
  }
  mountStyles() {
    this.styleModules = this.state.facet(Bt);
    let t = this.state.facet(A.cspNonce);
    Ct.mount(this.root, this.styleModules.concat($o).reverse(), t ? { nonce: t } : void 0);
  }
  readMeasured() {
    if (this.updateState == 2)
      throw new Error("Reading the editor layout isn't allowed during an update");
    this.updateState == 0 && this.measureScheduled > -1 && this.measure(!1);
  }
  /**
  Schedule a layout measurement, optionally providing callbacks to
  do custom DOM measuring followed by a DOM write phase. Using
  this is preferable reading DOM layout directly from, for
  example, an event handler, because it'll make sure measuring and
  drawing done by other components is synchronized, avoiding
  unnecessary DOM layout computations.
  */
  requestMeasure(t) {
    if (this.measureScheduled < 0 && (this.measureScheduled = this.win.requestAnimationFrame(() => this.measure())), t) {
      if (this.measureRequests.indexOf(t) > -1)
        return;
      if (t.key != null) {
        for (let e = 0; e < this.measureRequests.length; e++)
          if (this.measureRequests[e].key === t.key) {
            this.measureRequests[e] = t;
            return;
          }
      }
      this.measureRequests.push(t);
    }
  }
  /**
  Get the value of a specific plugin, if present. Note that
  plugins that crash can be dropped from a view, so even when you
  know you registered a given plugin, it is recommended to check
  the return value of this method.
  */
  plugin(t) {
    let e = this.pluginMap.get(t);
    return (e === void 0 || e && e.plugin != t) && this.pluginMap.set(t, e = this.plugins.find((i) => i.plugin == t) || null), e && e.update(this).value;
  }
  /**
  The top position of the document, in screen coordinates. This
  may be negative when the editor is scrolled down. Points
  directly to the top of the first line, not above the padding.
  */
  get documentTop() {
    return this.contentDOM.getBoundingClientRect().top + this.viewState.paddingTop;
  }
  /**
  Reports the padding above and below the document.
  */
  get documentPadding() {
    return { top: this.viewState.paddingTop, bottom: this.viewState.paddingBottom };
  }
  /**
  If the editor is transformed with CSS, this provides the scale
  along the X axis. Otherwise, it will just be 1. Note that
  transforms other than translation and scaling are not supported.
  */
  get scaleX() {
    return this.viewState.scaleX;
  }
  /**
  Provide the CSS transformed scale along the Y axis.
  */
  get scaleY() {
    return this.viewState.scaleY;
  }
  /**
  Find the text line or block widget at the given vertical
  position (which is interpreted as relative to the [top of the
  document](https://codemirror.net/6/docs/ref/#view.EditorView.documentTop)).
  */
  elementAtHeight(t) {
    return this.readMeasured(), this.viewState.elementAtHeight(t);
  }
  /**
  Find the line block (see
  [`lineBlockAt`](https://codemirror.net/6/docs/ref/#view.EditorView.lineBlockAt) at the given
  height, again interpreted relative to the [top of the
  document](https://codemirror.net/6/docs/ref/#view.EditorView.documentTop).
  */
  lineBlockAtHeight(t) {
    return this.readMeasured(), this.viewState.lineBlockAtHeight(t);
  }
  /**
  Get the extent and vertical position of all [line
  blocks](https://codemirror.net/6/docs/ref/#view.EditorView.lineBlockAt) in the viewport. Positions
  are relative to the [top of the
  document](https://codemirror.net/6/docs/ref/#view.EditorView.documentTop);
  */
  get viewportLineBlocks() {
    return this.viewState.viewportLines;
  }
  /**
  Find the line block around the given document position. A line
  block is a range delimited on both sides by either a
  non-[hidden](https://codemirror.net/6/docs/ref/#view.Decoration^replace) line break, or the
  start/end of the document. It will usually just hold a line of
  text, but may be broken into multiple textblocks by block
  widgets.
  */
  lineBlockAt(t) {
    return this.viewState.lineBlockAt(t);
  }
  /**
  The editor's total content height.
  */
  get contentHeight() {
    return this.viewState.contentHeight;
  }
  /**
  Move a cursor position by [grapheme
  cluster](https://codemirror.net/6/docs/ref/#state.findClusterBreak). `forward` determines whether
  the motion is away from the line start, or towards it. In
  bidirectional text, the line is traversed in visual order, using
  the editor's [text direction](https://codemirror.net/6/docs/ref/#view.EditorView.textDirection).
  When the start position was the last one on the line, the
  returned position will be across the line break. If there is no
  further line, the original position is returned.
  
  By default, this method moves over a single cluster. The
  optional `by` argument can be used to move across more. It will
  be called with the first cluster as argument, and should return
  a predicate that determines, for each subsequent cluster,
  whether it should also be moved over.
  */
  moveByChar(t, e, i) {
    return Ae(this, t, Di(this, t, e, i));
  }
  /**
  Move a cursor position across the next group of either
  [letters](https://codemirror.net/6/docs/ref/#state.EditorState.charCategorizer) or non-letter
  non-whitespace characters.
  */
  moveByGroup(t, e) {
    return Ae(this, t, Di(this, t, e, (i) => fo(this, t.head, i)));
  }
  /**
  Get the cursor position visually at the start or end of a line.
  Note that this may differ from the _logical_ position at its
  start or end (which is simply at `line.from`/`line.to`) if text
  at the start or end goes against the line's base text direction.
  */
  visualLineSide(t, e) {
    let i = this.bidiSpans(t), s = this.textDirectionAt(t.from), o = i[e ? i.length - 1 : 0];
    return k.cursor(o.side(e, s) + t.from, o.forward(!e, s) ? 1 : -1);
  }
  /**
  Move to the next line boundary in the given direction. If
  `includeWrap` is true, line wrapping is on, and there is a
  further wrap point on the current line, the wrap point will be
  returned. Otherwise this function will return the start or end
  of the line.
  */
  moveToLineBoundary(t, e, i = !0) {
    return ao(this, t, e, i);
  }
  /**
  Move a cursor position vertically. When `distance` isn't given,
  it defaults to moving to the next line (including wrapped
  lines). Otherwise, `distance` should provide a positive distance
  in pixels.
  
  When `start` has a
  [`goalColumn`](https://codemirror.net/6/docs/ref/#state.SelectionRange.goalColumn), the vertical
  motion will use that as a target horizontal position. Otherwise,
  the cursor's own horizontal position is used. The returned
  cursor will have its goal column set to whichever column was
  used.
  */
  moveVertically(t, e, i) {
    return Ae(this, t, co(this, t, e, i));
  }
  /**
  Find the DOM parent node and offset (child offset if `node` is
  an element, character offset when it is a text node) at the
  given document position.
  
  Note that for positions that aren't currently in
  `visibleRanges`, the resulting DOM position isn't necessarily
  meaningful (it may just point before or after a placeholder
  element).
  */
  domAtPos(t) {
    return this.docView.domAtPos(t);
  }
  /**
  Find the document position at the given DOM node. Can be useful
  for associating positions with DOM events. Will raise an error
  when `node` isn't part of the editor content.
  */
  posAtDOM(t, e = 0) {
    return this.docView.posFromDOM(t, e);
  }
  posAtCoords(t, e = !0) {
    return this.readMeasured(), Gs(this, t, e);
  }
  /**
  Get the screen coordinates at the given document position.
  `side` determines whether the coordinates are based on the
  element before (-1) or after (1) the position (if no element is
  available on the given side, the method will transparently use
  another strategy to get reasonable coordinates).
  */
  coordsAtPos(t, e = 1) {
    this.readMeasured();
    let i = this.docView.coordsAt(t, e);
    if (!i || i.left == i.right)
      return i;
    let s = this.state.doc.lineAt(t), o = this.bidiSpans(s), r = o[lt.find(o, t - s.from, -1, e)];
    return qt(i, r.dir == B.LTR == e > 0);
  }
  /**
  Return the rectangle around a given character. If `pos` does not
  point in front of a character that is in the viewport and
  rendered (i.e. not replaced, not a line break), this will return
  null. For space characters that are a line wrap point, this will
  return the position before the line break.
  */
  coordsForChar(t) {
    return this.readMeasured(), this.docView.coordsForChar(t);
  }
  /**
  The default width of a character in the editor. May not
  accurately reflect the width of all characters (given variable
  width fonts or styling of invididual ranges).
  */
  get defaultCharacterWidth() {
    return this.viewState.heightOracle.charWidth;
  }
  /**
  The default height of a line in the editor. May not be accurate
  for all lines.
  */
  get defaultLineHeight() {
    return this.viewState.heightOracle.lineHeight;
  }
  /**
  The text direction
  ([`direction`](https://developer.mozilla.org/en-US/docs/Web/CSS/direction)
  CSS property) of the editor's content element.
  */
  get textDirection() {
    return this.viewState.defaultTextDirection;
  }
  /**
  Find the text direction of the block at the given position, as
  assigned by CSS. If
  [`perLineTextDirection`](https://codemirror.net/6/docs/ref/#view.EditorView^perLineTextDirection)
  isn't enabled, or the given position is outside of the viewport,
  this will always return the same as
  [`textDirection`](https://codemirror.net/6/docs/ref/#view.EditorView.textDirection). Note that
  this may trigger a DOM layout.
  */
  textDirectionAt(t) {
    return !this.state.facet(Ws) || t < this.viewport.from || t > this.viewport.to ? this.textDirection : (this.readMeasured(), this.docView.textDirectionAt(t));
  }
  /**
  Whether this editor [wraps lines](https://codemirror.net/6/docs/ref/#view.EditorView.lineWrapping)
  (as determined by the
  [`white-space`](https://developer.mozilla.org/en-US/docs/Web/CSS/white-space)
  CSS property of its content element).
  */
  get lineWrapping() {
    return this.viewState.heightOracle.lineWrapping;
  }
  /**
  Returns the bidirectional text structure of the given line
  (which should be in the current document) as an array of span
  objects. The order of these spans matches the [text
  direction](https://codemirror.net/6/docs/ref/#view.EditorView.textDirection)—if that is
  left-to-right, the leftmost spans come first, otherwise the
  rightmost spans come first.
  */
  bidiSpans(t) {
    if (t.length > tr)
      return Es(t.length);
    let e = this.textDirectionAt(t.from), i;
    for (let o of this.bidiCache)
      if (o.from == t.from && o.dir == e && (o.fresh || Rs(o.isolates, i = Si(this, t))))
        return o.order;
    i || (i = Si(this, t));
    let s = Xn(t.text, e, i);
    return this.bidiCache.push(new ye(t.from, t.to, e, i, !0, s)), s;
  }
  /**
  Check whether the editor has focus.
  */
  get hasFocus() {
    var t;
    return (this.dom.ownerDocument.hasFocus() || y.safari && ((t = this.inputState) === null || t === void 0 ? void 0 : t.lastContextMenu) > Date.now() - 3e4) && this.root.activeElement == this.contentDOM;
  }
  /**
  Put focus on the editor.
  */
  focus() {
    this.observer.ignore(() => {
      ps(this.contentDOM), this.docView.updateSelection();
    });
  }
  /**
  Update the [root](https://codemirror.net/6/docs/ref/##view.EditorViewConfig.root) in which the editor lives. This is only
  necessary when moving the editor's existing DOM to a new window or shadow root.
  */
  setRoot(t) {
    this._root != t && (this._root = t, this.observer.setWindow((t.nodeType == 9 ? t : t.ownerDocument).defaultView || window), this.mountStyles());
  }
  /**
  Clean up this editor view, removing its element from the
  document, unregistering event handlers, and notifying
  plugins. The view instance can no longer be used after
  calling this.
  */
  destroy() {
    this.root.activeElement == this.contentDOM && this.contentDOM.blur();
    for (let t of this.plugins)
      t.destroy(this);
    this.plugins = [], this.inputState.destroy(), this.docView.destroy(), this.dom.remove(), this.observer.destroy(), this.measureScheduled > -1 && this.win.cancelAnimationFrame(this.measureScheduled), this.destroyed = !0;
  }
  /**
  Returns an effect that can be
  [added](https://codemirror.net/6/docs/ref/#state.TransactionSpec.effects) to a transaction to
  cause it to scroll the given position or range into view.
  */
  static scrollIntoView(t, e = {}) {
    return Qt.of(new vt(typeof t == "number" ? k.cursor(t) : t, e.y, e.x, e.yMargin, e.xMargin));
  }
  /**
  Return an effect that resets the editor to its current (at the
  time this method was called) scroll position. Note that this
  only affects the editor's own scrollable element, not parents.
  See also
  [`EditorViewConfig.scrollTo`](https://codemirror.net/6/docs/ref/#view.EditorViewConfig.scrollTo).
  
  The effect should be used with a document identical to the one
  it was created for. Failing to do so is not an error, but may
  not scroll to the expected position. You can
  [map](https://codemirror.net/6/docs/ref/#state.StateEffect.map) the effect to account for changes.
  */
  scrollSnapshot() {
    let { scrollTop: t, scrollLeft: e } = this.scrollDOM, i = this.viewState.scrollAnchorAt(t);
    return Qt.of(new vt(k.cursor(i.from), "start", "start", i.top - t, e, !0));
  }
  /**
  Enable or disable tab-focus mode, which disables key bindings
  for Tab and Shift-Tab, letting the browser's default
  focus-changing behavior go through instead. This is useful to
  prevent trapping keyboard users in your editor.
  
  Without argument, this toggles the mode. With a boolean, it
  enables (true) or disables it (false). Given a number, it
  temporarily enables the mode until that number of milliseconds
  have passed or another non-Tab key is pressed.
  */
  setTabFocusMode(t) {
    t == null ? this.inputState.tabFocusMode = this.inputState.tabFocusMode < 0 ? 0 : -1 : typeof t == "boolean" ? this.inputState.tabFocusMode = t ? 0 : -1 : this.inputState.tabFocusMode != 0 && (this.inputState.tabFocusMode = Date.now() + t);
  }
  /**
  Returns an extension that can be used to add DOM event handlers.
  The value should be an object mapping event names to handler
  functions. For any given event, such functions are ordered by
  extension precedence, and the first handler to return true will
  be assumed to have handled that event, and no other handlers or
  built-in behavior will be activated for it. These are registered
  on the [content element](https://codemirror.net/6/docs/ref/#view.EditorView.contentDOM), except
  for `scroll` handlers, which will be called any time the
  editor's [scroll element](https://codemirror.net/6/docs/ref/#view.EditorView.scrollDOM) or one of
  its parent nodes is scrolled.
  */
  static domEventHandlers(t) {
    return z.define(() => ({}), { eventHandlers: t });
  }
  /**
  Create an extension that registers DOM event observers. Contrary
  to event [handlers](https://codemirror.net/6/docs/ref/#view.EditorView^domEventHandlers),
  observers can't be prevented from running by a higher-precedence
  handler returning true. They also don't prevent other handlers
  and observers from running when they return true, and should not
  call `preventDefault`.
  */
  static domEventObservers(t) {
    return z.define(() => ({}), { eventObservers: t });
  }
  /**
  Create a theme extension. The first argument can be a
  [`style-mod`](https://github.com/marijnh/style-mod#documentation)
  style spec providing the styles for the theme. These will be
  prefixed with a generated class for the style.
  
  Because the selectors will be prefixed with a scope class, rule
  that directly match the editor's [wrapper
  element](https://codemirror.net/6/docs/ref/#view.EditorView.dom)—to which the scope class will be
  added—need to be explicitly differentiated by adding an `&` to
  the selector for that element—for example
  `&.cm-focused`.
  
  When `dark` is set to true, the theme will be marked as dark,
  which will cause the `&dark` rules from [base
  themes](https://codemirror.net/6/docs/ref/#view.EditorView^baseTheme) to be used (as opposed to
  `&light` when a light theme is active).
  */
  static theme(t, e) {
    let i = Ct.newName(), s = [se.of(i), Bt.of(Ue(`.${i}`, t))];
    return e && e.dark && s.push(Ge.of(!0)), s;
  }
  /**
  Create an extension that adds styles to the base theme. Like
  with [`theme`](https://codemirror.net/6/docs/ref/#view.EditorView^theme), use `&` to indicate the
  place of the editor wrapper element when directly targeting
  that. You can also use `&dark` or `&light` instead to only
  target editors with a dark or light theme.
  */
  static baseTheme(t) {
    return ei.lowest(Bt.of(Ue("." + $e, t, hn)));
  }
  /**
  Retrieve an editor view instance from the view's DOM
  representation.
  */
  static findFromDOM(t) {
    var e;
    let i = t.querySelector(".cm-content"), s = i && O.get(i) || O.get(t);
    return ((e = s == null ? void 0 : s.rootView) === null || e === void 0 ? void 0 : e.view) || null;
  }
}
A.styleModule = Bt;
A.inputHandler = Vs;
A.clipboardInputFilter = oi;
A.clipboardOutputFilter = ri;
A.scrollHandler = Is;
A.focusChangeEffect = Fs;
A.perLineTextDirection = Ws;
A.exceptionSink = Ns;
A.updateListener = Ye;
A.editable = tt;
A.mouseSelectionStyle = Hs;
A.dragMovesSelection = Ps;
A.clickAddsSelectionRange = Ls;
A.decorations = It;
A.outerDecorations = Ys;
A.atomicRanges = hi;
A.bidiIsolatedRanges = js;
A.scrollMargins = _s;
A.darkTheme = Ge;
A.cspNonce = /* @__PURE__ */ M.define({ combine: (n) => n.length ? n[0] : "" });
A.contentAttributes = li;
A.editorAttributes = qs;
A.lineWrapping = /* @__PURE__ */ A.contentAttributes.of({ class: "cm-lineWrapping" });
A.announce = /* @__PURE__ */ Ot.define();
const tr = 4096, ji = {};
class ye {
  constructor(t, e, i, s, o, r) {
    this.from = t, this.to = e, this.dir = i, this.isolates = s, this.fresh = o, this.order = r;
  }
  static update(t, e) {
    if (e.empty && !t.some((o) => o.fresh))
      return t;
    let i = [], s = t.length ? t[t.length - 1].dir : B.LTR;
    for (let o = Math.max(0, t.length - 10); o < t.length; o++) {
      let r = t[o];
      r.dir == s && !e.touchesRange(r.from, r.to) && i.push(new ye(e.mapPos(r.from, 1), e.mapPos(r.to, -1), r.dir, r.isolates, !1, r.order));
    }
    return i;
  }
}
function _i(n, t, e) {
  for (let i = n.state.facet(t), s = i.length - 1; s >= 0; s--) {
    let o = i[s], r = typeof o == "function" ? o(n) : o;
    r && Fe(r, e);
  }
  return e;
}
const er = y.mac ? "mac" : y.windows ? "win" : y.linux ? "linux" : "key";
function ir(n, t) {
  const e = n.split(/-(?!$)/);
  let i = e[e.length - 1];
  i == "Space" && (i = " ");
  let s, o, r, l;
  for (let h = 0; h < e.length - 1; ++h) {
    const f = e[h];
    if (/^(cmd|meta|m)$/i.test(f))
      l = !0;
    else if (/^a(lt)?$/i.test(f))
      s = !0;
    else if (/^(c|ctrl|control)$/i.test(f))
      o = !0;
    else if (/^s(hift)?$/i.test(f))
      r = !0;
    else if (/^mod$/i.test(f))
      t == "mac" ? l = !0 : o = !0;
    else
      throw new Error("Unrecognized modifier name: " + f);
  }
  return s && (i = "Alt-" + i), o && (i = "Ctrl-" + i), l && (i = "Meta-" + i), r && (i = "Shift-" + i), i;
}
function ne(n, t, e) {
  return t.altKey && (n = "Alt-" + n), t.ctrlKey && (n = "Ctrl-" + n), t.metaKey && (n = "Meta-" + n), e !== !1 && t.shiftKey && (n = "Shift-" + n), n;
}
const sr = /* @__PURE__ */ ei.default(/* @__PURE__ */ A.domEventHandlers({
  keydown(n, t) {
    return fn(an(t.state), n, t, "editor");
  }
})), nr = /* @__PURE__ */ M.define({ enables: sr }), Xi = /* @__PURE__ */ new WeakMap();
function an(n) {
  let t = n.facet(nr), e = Xi.get(t);
  return e || Xi.set(t, e = rr(t.reduce((i, s) => i.concat(s), []))), e;
}
function tl(n, t, e) {
  return fn(an(n.state), t, n, e);
}
let ot = null;
const or = 4e3;
function rr(n, t = er) {
  let e = /* @__PURE__ */ Object.create(null), i = /* @__PURE__ */ Object.create(null), s = (r, l) => {
    let h = i[r];
    if (h == null)
      i[r] = l;
    else if (h != l)
      throw new Error("Key binding " + r + " is used both as a regular binding and as a multi-stroke prefix");
  }, o = (r, l, h, f, a) => {
    var c, d;
    let u = e[r] || (e[r] = /* @__PURE__ */ Object.create(null)), p = l.split(/ (?!$)/).map((b) => ir(b, t));
    for (let b = 1; b < p.length; b++) {
      let w = p.slice(0, b).join(" ");
      s(w, !0), u[w] || (u[w] = {
        preventDefault: !0,
        stopPropagation: !1,
        run: [(S) => {
          let x = ot = { view: S, prefix: w, scope: r };
          return setTimeout(() => {
            ot == x && (ot = null);
          }, or), !0;
        }]
      });
    }
    let g = p.join(" ");
    s(g, !1);
    let m = u[g] || (u[g] = {
      preventDefault: !1,
      stopPropagation: !1,
      run: ((d = (c = u._any) === null || c === void 0 ? void 0 : c.run) === null || d === void 0 ? void 0 : d.slice()) || []
    });
    h && m.run.push(h), f && (m.preventDefault = !0), a && (m.stopPropagation = !0);
  };
  for (let r of n) {
    let l = r.scope ? r.scope.split(" ") : ["editor"];
    if (r.any)
      for (let f of l) {
        let a = e[f] || (e[f] = /* @__PURE__ */ Object.create(null));
        a._any || (a._any = { preventDefault: !1, stopPropagation: !1, run: [] });
        let { any: c } = r;
        for (let d in a)
          a[d].run.push((u) => c(u, Qe));
      }
    let h = r[t] || r.key;
    if (h)
      for (let f of l)
        o(f, h, r.run, r.preventDefault, r.stopPropagation), r.shift && o(f, "Shift-" + h, r.shift, r.preventDefault, r.stopPropagation);
  }
  return e;
}
let Qe = null;
function fn(n, t, e, i) {
  Qe = t;
  let s = kn(t), o = cs(s, 0), r = vn(o) == s.length && s != " ", l = "", h = !1, f = !1, a = !1;
  ot && ot.view == e && ot.scope == i && (l = ot.prefix + " ", Qs.indexOf(t.keyCode) < 0 && (f = !0, ot = null));
  let c = /* @__PURE__ */ new Set(), d = (m) => {
    if (m) {
      for (let b of m.run)
        if (!c.has(b) && (c.add(b), b(e)))
          return m.stopPropagation && (a = !0), !0;
      m.preventDefault && (m.stopPropagation && (a = !0), f = !0);
    }
    return !1;
  }, u = n[i], p, g;
  return u && (d(u[l + ne(s, t, !r)]) ? h = !0 : r && (t.altKey || t.metaKey || t.ctrlKey) && // Ctrl-Alt may be used for AltGr on Windows
  !(y.windows && t.ctrlKey && t.altKey) && // Alt-combinations on macOS tend to be typed characters
  !(y.mac && t.altKey && !t.ctrlKey) && (p = An[t.keyCode]) && p != s ? (d(u[l + ne(p, t, !0)]) || t.shiftKey && (g = Dn[t.keyCode]) != s && g != p && d(u[l + ne(g, t, !1)])) && (h = !0) : r && t.shiftKey && d(u[l + ne(s, t, !0)]) && (h = !0), !h && d(u._any) && (h = !0)), f && (h = !0), h && a && t.stopPropagation(), Qe = null, h;
}
class _t {
  /**
  Create a marker with the given class and dimensions. If `width`
  is null, the DOM element will get no width style.
  */
  constructor(t, e, i, s, o) {
    this.className = t, this.left = e, this.top = i, this.width = s, this.height = o;
  }
  draw() {
    let t = document.createElement("div");
    return t.className = this.className, this.adjust(t), t;
  }
  update(t, e) {
    return e.className != this.className ? !1 : (this.adjust(t), !0);
  }
  adjust(t) {
    t.style.left = this.left + "px", t.style.top = this.top + "px", this.width != null && (t.style.width = this.width + "px"), t.style.height = this.height + "px";
  }
  eq(t) {
    return this.left == t.left && this.top == t.top && this.width == t.width && this.height == t.height && this.className == t.className;
  }
  /**
  Create a set of rectangles for the given selection range,
  assigning them theclass`className`. Will create a single
  rectangle for empty ranges, and a set of selection-style
  rectangles covering the range's content (in a bidi-aware
  way) for non-empty ones.
  */
  static forRange(t, e, i) {
    if (i.empty) {
      let s = t.coordsAtPos(i.head, i.assoc || 1);
      if (!s)
        return [];
      let o = cn(t);
      return [new _t(e, s.left - o.left, s.top - o.top, null, s.bottom - s.top)];
    } else
      return lr(t, e, i);
  }
}
function cn(n) {
  let t = n.scrollDOM.getBoundingClientRect();
  return { left: (n.textDirection == B.LTR ? t.left : t.right - n.scrollDOM.clientWidth * n.scaleX) - n.scrollDOM.scrollLeft * n.scaleX, top: t.top - n.scrollDOM.scrollTop * n.scaleY };
}
function Gi(n, t, e, i) {
  let s = n.coordsAtPos(t, e * 2);
  if (!s)
    return i;
  let o = n.dom.getBoundingClientRect(), r = (s.top + s.bottom) / 2, l = n.posAtCoords({ x: o.left + 1, y: r }), h = n.posAtCoords({ x: o.right - 1, y: r });
  return l == null || h == null ? i : { from: Math.max(i.from, Math.min(l, h)), to: Math.min(i.to, Math.max(l, h)) };
}
function lr(n, t, e) {
  if (e.to <= n.viewport.from || e.from >= n.viewport.to)
    return [];
  let i = Math.max(e.from, n.viewport.from), s = Math.min(e.to, n.viewport.to), o = n.textDirection == B.LTR, r = n.contentDOM, l = r.getBoundingClientRect(), h = cn(n), f = r.querySelector(".cm-line"), a = f && window.getComputedStyle(f), c = l.left + (a ? parseInt(a.paddingLeft) + Math.min(0, parseInt(a.textIndent)) : 0), d = l.right - (a ? parseInt(a.paddingRight) : 0), u = _e(n, i, 1), p = _e(n, s, -1), g = u.type == F.Text ? u : null, m = p.type == F.Text ? p : null;
  if (g && (n.lineWrapping || u.widgetLineBreaks) && (g = Gi(n, i, 1, g)), m && (n.lineWrapping || p.widgetLineBreaks) && (m = Gi(n, s, -1, m)), g && m && g.from == m.from && g.to == m.to)
    return w(S(e.from, e.to, g));
  {
    let v = g ? S(e.from, null, g) : x(u, !1), C = m ? S(null, e.to, m) : x(p, !0), R = [];
    return (g || u).to < (m || p).from - (g && m ? 1 : 0) || u.widgetLineBreaks > 1 && v.bottom + n.defaultLineHeight / 2 < C.top ? R.push(b(c, v.bottom, d, C.top)) : v.bottom < C.top && n.elementAtHeight((v.bottom + C.top) / 2).type == F.Text && (v.bottom = C.top = (v.bottom + C.top) / 2), w(v).concat(R).concat(w(C));
  }
  function b(v, C, R, P) {
    return new _t(t, v - h.left, C - h.top, R - v, P - C);
  }
  function w({ top: v, bottom: C, horizontal: R }) {
    let P = [];
    for (let st = 0; st < R.length; st += 2)
      P.push(b(R[st], v, R[st + 1], C));
    return P;
  }
  function S(v, C, R) {
    let P = 1e9, st = -1e9, Xt = [];
    function pi(ft, nt, gt, ct, Rt) {
      let J = n.coordsAtPos(ft, ft == R.to ? -2 : 2), Z = n.coordsAtPos(gt, gt == R.from ? 2 : -2);
      !J || !Z || (P = Math.min(J.top, Z.top, P), st = Math.max(J.bottom, Z.bottom, st), Rt == B.LTR ? Xt.push(o && nt ? c : J.left, o && ct ? d : Z.right) : Xt.push(!o && ct ? c : Z.left, !o && nt ? d : J.right));
    }
    let Gt = v ?? R.from, $t = C ?? R.to;
    for (let ft of n.visibleRanges)
      if (ft.to > Gt && ft.from < $t)
        for (let nt = Math.max(ft.from, Gt), gt = Math.min(ft.to, $t); ; ) {
          let ct = n.state.doc.lineAt(nt);
          for (let Rt of n.bidiSpans(ct)) {
            let J = Rt.from + ct.from, Z = Rt.to + ct.from;
            if (J >= gt)
              break;
            Z > nt && pi(Math.max(J, nt), v == null && J <= Gt, Math.min(Z, gt), C == null && Z >= $t, Rt.dir);
          }
          if (nt = ct.to + 1, nt >= gt)
            break;
        }
    return Xt.length == 0 && pi(Gt, v == null, $t, C == null, n.textDirection), { top: P, bottom: st, horizontal: Xt };
  }
  function x(v, C) {
    let R = l.top + (C ? v.top : v.bottom);
    return { top: R, bottom: R, horizontal: [] };
  }
}
function hr(n, t) {
  return n.constructor == t.constructor && n.eq(t);
}
class ar {
  constructor(t, e) {
    this.view = t, this.layer = e, this.drawn = [], this.scaleX = 1, this.scaleY = 1, this.measureReq = { read: this.measure.bind(this), write: this.draw.bind(this) }, this.dom = t.scrollDOM.appendChild(document.createElement("div")), this.dom.classList.add("cm-layer"), e.above && this.dom.classList.add("cm-layer-above"), e.class && this.dom.classList.add(e.class), this.scale(), this.dom.setAttribute("aria-hidden", "true"), this.setOrder(t.state), t.requestMeasure(this.measureReq), e.mount && e.mount(this.dom, t);
  }
  update(t) {
    t.startState.facet(ce) != t.state.facet(ce) && this.setOrder(t.state), (this.layer.update(t, this.dom) || t.geometryChanged) && (this.scale(), t.view.requestMeasure(this.measureReq));
  }
  docViewUpdate(t) {
    this.layer.updateOnDocViewUpdate !== !1 && t.requestMeasure(this.measureReq);
  }
  setOrder(t) {
    let e = 0, i = t.facet(ce);
    for (; e < i.length && i[e] != this.layer; )
      e++;
    this.dom.style.zIndex = String((this.layer.above ? 150 : -1) - e);
  }
  measure() {
    return this.layer.markers(this.view);
  }
  scale() {
    let { scaleX: t, scaleY: e } = this.view;
    (t != this.scaleX || e != this.scaleY) && (this.scaleX = t, this.scaleY = e, this.dom.style.transform = `scale(${1 / t}, ${1 / e})`);
  }
  draw(t) {
    if (t.length != this.drawn.length || t.some((e, i) => !hr(e, this.drawn[i]))) {
      let e = this.dom.firstChild, i = 0;
      for (let s of t)
        s.update && e && s.constructor && this.drawn[i].constructor && s.update(e, this.drawn[i]) ? (e = e.nextSibling, i++) : this.dom.insertBefore(s.draw(), e);
      for (; e; ) {
        let s = e.nextSibling;
        e.remove(), e = s;
      }
      this.drawn = t;
    }
  }
  destroy() {
    this.layer.destroy && this.layer.destroy(this.dom, this.view), this.dom.remove();
  }
}
const ce = /* @__PURE__ */ M.define();
function dn(n) {
  return [
    z.define((t) => new ar(t, n)),
    ce.of(n)
  ];
}
const Kt = /* @__PURE__ */ M.define({
  combine(n) {
    return ii(n, {
      cursorBlinkRate: 1200,
      drawRangeCursor: !0
    }, {
      cursorBlinkRate: (t, e) => Math.min(t, e),
      drawRangeCursor: (t, e) => t || e
    });
  }
});
function el(n = {}) {
  return [
    Kt.of(n),
    fr,
    cr,
    dr,
    zs.of(!0)
  ];
}
function un(n) {
  return n.startState.facet(Kt) != n.state.facet(Kt);
}
const fr = /* @__PURE__ */ dn({
  above: !0,
  markers(n) {
    let { state: t } = n, e = t.facet(Kt), i = [];
    for (let s of t.selection.ranges) {
      let o = s == t.selection.main;
      if (s.empty || e.drawRangeCursor) {
        let r = o ? "cm-cursor cm-cursor-primary" : "cm-cursor cm-cursor-secondary", l = s.empty ? s : k.cursor(s.head, s.head > s.anchor ? -1 : 1);
        for (let h of _t.forRange(n, r, l))
          i.push(h);
      }
    }
    return i;
  },
  update(n, t) {
    n.transactions.some((i) => i.selection) && (t.style.animationName = t.style.animationName == "cm-blink" ? "cm-blink2" : "cm-blink");
    let e = un(n);
    return e && $i(n.state, t), n.docChanged || n.selectionSet || e;
  },
  mount(n, t) {
    $i(t.state, n);
  },
  class: "cm-cursorLayer"
});
function $i(n, t) {
  t.style.animationDuration = n.facet(Kt).cursorBlinkRate + "ms";
}
const cr = /* @__PURE__ */ dn({
  above: !1,
  markers(n) {
    return n.state.selection.ranges.map((t) => t.empty ? [] : _t.forRange(n, "cm-selectionBackground", t)).reduce((t, e) => t.concat(e));
  },
  update(n, t) {
    return n.docChanged || n.selectionSet || n.viewportChanged || un(n);
  },
  class: "cm-selectionLayer"
}), dr = /* @__PURE__ */ ei.highest(/* @__PURE__ */ A.theme({
  ".cm-line": {
    "& ::selection, &::selection": { backgroundColor: "transparent !important" },
    caretColor: "transparent !important"
  },
  ".cm-content": {
    caretColor: "transparent !important",
    "& :focus": {
      caretColor: "initial !important",
      "&::selection, & ::selection": {
        backgroundColor: "Highlight !important"
      }
    }
  }
})), pn = /* @__PURE__ */ Ot.define({
  map(n, t) {
    return n == null ? null : t.mapPos(n);
  }
}), Ht = /* @__PURE__ */ fs.define({
  create() {
    return null;
  },
  update(n, t) {
    return n != null && (n = t.changes.mapPos(n)), t.effects.reduce((e, i) => i.is(pn) ? i.value : e, n);
  }
}), ur = /* @__PURE__ */ z.fromClass(class {
  constructor(n) {
    this.view = n, this.cursor = null, this.measureReq = { read: this.readPos.bind(this), write: this.drawCursor.bind(this) };
  }
  update(n) {
    var t;
    let e = n.state.field(Ht);
    e == null ? this.cursor != null && ((t = this.cursor) === null || t === void 0 || t.remove(), this.cursor = null) : (this.cursor || (this.cursor = this.view.scrollDOM.appendChild(document.createElement("div")), this.cursor.className = "cm-dropCursor"), (n.startState.field(Ht) != e || n.docChanged || n.geometryChanged) && this.view.requestMeasure(this.measureReq));
  }
  readPos() {
    let { view: n } = this, t = n.state.field(Ht), e = t != null && n.coordsAtPos(t);
    if (!e)
      return null;
    let i = n.scrollDOM.getBoundingClientRect();
    return {
      left: e.left - i.left + n.scrollDOM.scrollLeft * n.scaleX,
      top: e.top - i.top + n.scrollDOM.scrollTop * n.scaleY,
      height: e.bottom - e.top
    };
  }
  drawCursor(n) {
    if (this.cursor) {
      let { scaleX: t, scaleY: e } = this.view;
      n ? (this.cursor.style.left = n.left / t + "px", this.cursor.style.top = n.top / e + "px", this.cursor.style.height = n.height / e + "px") : this.cursor.style.left = "-100000px";
    }
  }
  destroy() {
    this.cursor && this.cursor.remove();
  }
  setDropPos(n) {
    this.view.state.field(Ht) != n && this.view.dispatch({ effects: pn.of(n) });
  }
}, {
  eventObservers: {
    dragover(n) {
      this.setDropPos(this.view.posAtCoords({ x: n.clientX, y: n.clientY }));
    },
    dragleave(n) {
      (n.target == this.view.contentDOM || !this.view.contentDOM.contains(n.relatedTarget)) && this.setDropPos(null);
    },
    dragend() {
      this.setDropPos(null);
    },
    drop() {
      this.setDropPos(null);
    }
  }
});
function il() {
  return [Ht, ur];
}
function Ui(n, t, e, i, s) {
  t.lastIndex = 0;
  for (let o = n.iterRange(e, i), r = e, l; !o.next().done; r += o.value.length)
    if (!o.lineBreak)
      for (; l = t.exec(o.value); )
        s(r + l.index, l);
}
function pr(n, t) {
  let e = n.visibleRanges;
  if (e.length == 1 && e[0].from == n.viewport.from && e[0].to == n.viewport.to)
    return e;
  let i = [];
  for (let { from: s, to: o } of e)
    s = Math.max(n.state.doc.lineAt(s).from, s - t), o = Math.min(n.state.doc.lineAt(o).to, o + t), i.length && i[i.length - 1].to >= s ? i[i.length - 1].to = o : i.push({ from: s, to: o });
  return i;
}
class gr {
  /**
  Create a decorator.
  */
  constructor(t) {
    const { regexp: e, decoration: i, decorate: s, boundary: o, maxLength: r = 1e3 } = t;
    if (!e.global)
      throw new RangeError("The regular expression given to MatchDecorator should have its 'g' flag set");
    if (this.regexp = e, s)
      this.addMatch = (l, h, f, a) => s(a, f, f + l[0].length, l, h);
    else if (typeof i == "function")
      this.addMatch = (l, h, f, a) => {
        let c = i(l, h, f);
        c && a(f, f + l[0].length, c);
      };
    else if (i)
      this.addMatch = (l, h, f, a) => a(f, f + l[0].length, i);
    else
      throw new RangeError("Either 'decorate' or 'decoration' should be provided to MatchDecorator");
    this.boundary = o, this.maxLength = r;
  }
  /**
  Compute the full set of decorations for matches in the given
  view's viewport. You'll want to call this when initializing your
  plugin.
  */
  createDeco(t) {
    let e = new Cn(), i = e.add.bind(e);
    for (let { from: s, to: o } of pr(t, this.maxLength))
      Ui(t.state.doc, this.regexp, s, o, (r, l) => this.addMatch(l, t, r, i));
    return e.finish();
  }
  /**
  Update a set of decorations for a view update. `deco` _must_ be
  the set of decorations produced by _this_ `MatchDecorator` for
  the view state before the update.
  */
  updateDeco(t, e) {
    let i = 1e9, s = -1;
    return t.docChanged && t.changes.iterChanges((o, r, l, h) => {
      h >= t.view.viewport.from && l <= t.view.viewport.to && (i = Math.min(l, i), s = Math.max(h, s));
    }), t.viewportMoved || s - i > 1e3 ? this.createDeco(t.view) : s > -1 ? this.updateRange(t.view, e.map(t.changes), i, s) : e;
  }
  updateRange(t, e, i, s) {
    for (let o of t.visibleRanges) {
      let r = Math.max(o.from, i), l = Math.min(o.to, s);
      if (l >= r) {
        let h = t.state.doc.lineAt(r), f = h.to < l ? t.state.doc.lineAt(l) : h, a = Math.max(o.from, h.from), c = Math.min(o.to, f.to);
        if (this.boundary) {
          for (; r > h.from; r--)
            if (this.boundary.test(h.text[r - 1 - h.from])) {
              a = r;
              break;
            }
          for (; l < f.to; l++)
            if (this.boundary.test(f.text[l - f.from])) {
              c = l;
              break;
            }
        }
        let d = [], u, p = (g, m, b) => d.push(b.range(g, m));
        if (h == f)
          for (this.regexp.lastIndex = a - h.from; (u = this.regexp.exec(h.text)) && u.index < c - h.from; )
            this.addMatch(u, t, u.index + h.from, p);
        else
          Ui(t.state.doc, this.regexp, a, c, (g, m) => this.addMatch(m, t, g, p));
        e = e.update({ filterFrom: a, filterTo: c, filter: (g, m) => g < a || m > c, add: d });
      }
    }
    return e;
  }
}
const Je = /x/.unicode != null ? "gu" : "g", mr = /* @__PURE__ */ new RegExp(`[\0-\b
--Ÿ­؜​‎‏\u2028\u2029‭‮⁦⁧⁩\uFEFF-]`, Je), br = {
  0: "null",
  7: "bell",
  8: "backspace",
  10: "newline",
  11: "vertical tab",
  13: "carriage return",
  27: "escape",
  8203: "zero width space",
  8204: "zero width non-joiner",
  8205: "zero width joiner",
  8206: "left-to-right mark",
  8207: "right-to-left mark",
  8232: "line separator",
  8237: "left-to-right override",
  8238: "right-to-left override",
  8294: "left-to-right isolate",
  8295: "right-to-left isolate",
  8297: "pop directional isolate",
  8233: "paragraph separator",
  65279: "zero width no-break space",
  65532: "object replacement"
};
let Te = null;
function yr() {
  var n;
  if (Te == null && typeof document < "u" && document.body) {
    let t = document.body.style;
    Te = ((n = t.tabSize) !== null && n !== void 0 ? n : t.MozTabSize) != null;
  }
  return Te || !1;
}
const de = /* @__PURE__ */ M.define({
  combine(n) {
    let t = ii(n, {
      render: null,
      specialChars: mr,
      addSpecialChars: null
    });
    return (t.replaceTabs = !yr()) && (t.specialChars = new RegExp("	|" + t.specialChars.source, Je)), t.addSpecialChars && (t.specialChars = new RegExp(t.specialChars.source + "|" + t.addSpecialChars.source, Je)), t;
  }
});
function sl(n = {}) {
  return [de.of(n), wr()];
}
let Qi = null;
function wr() {
  return Qi || (Qi = z.fromClass(class {
    constructor(n) {
      this.view = n, this.decorations = E.none, this.decorationCache = /* @__PURE__ */ Object.create(null), this.decorator = this.makeDecorator(n.state.facet(de)), this.decorations = this.decorator.createDeco(n);
    }
    makeDecorator(n) {
      return new gr({
        regexp: n.specialChars,
        decoration: (t, e, i) => {
          let { doc: s } = e.state, o = cs(t[0], 0);
          if (o == 9) {
            let r = s.lineAt(i), l = e.state.tabSize, h = ds(r.text, l, i - r.from);
            return E.replace({
              widget: new Cr((l - h % l) * this.view.defaultCharacterWidth / this.view.scaleX)
            });
          }
          return this.decorationCache[o] || (this.decorationCache[o] = E.replace({ widget: new vr(n, o) }));
        },
        boundary: n.replaceTabs ? void 0 : /[^]/
      });
    }
    update(n) {
      let t = n.state.facet(de);
      n.startState.facet(de) != t ? (this.decorator = this.makeDecorator(t), this.decorations = this.decorator.createDeco(n.view)) : this.decorations = this.decorator.updateDeco(n, this.decorations);
    }
  }, {
    decorations: (n) => n.decorations
  }));
}
const xr = "•";
function Sr(n) {
  return n >= 32 ? xr : n == 10 ? "␤" : String.fromCharCode(9216 + n);
}
class vr extends Tt {
  constructor(t, e) {
    super(), this.options = t, this.code = e;
  }
  eq(t) {
    return t.code == this.code;
  }
  toDOM(t) {
    let e = Sr(this.code), i = t.state.phrase("Control character") + " " + (br[this.code] || "0x" + this.code.toString(16)), s = this.options.render && this.options.render(this.code, i, e);
    if (s)
      return s;
    let o = document.createElement("span");
    return o.textContent = e, o.title = i, o.setAttribute("aria-label", i), o.className = "cm-specialChar", o;
  }
  ignoreEvent() {
    return !1;
  }
}
class Cr extends Tt {
  constructor(t) {
    super(), this.width = t;
  }
  eq(t) {
    return t.width == this.width;
  }
  toDOM() {
    let t = document.createElement("span");
    return t.textContent = "	", t.className = "cm-tab", t.style.width = this.width + "px", t;
  }
  ignoreEvent() {
    return !1;
  }
}
function nl() {
  return kr;
}
const Mr = /* @__PURE__ */ E.line({ class: "cm-activeLine" }), kr = /* @__PURE__ */ z.fromClass(class {
  constructor(n) {
    this.decorations = this.getDeco(n);
  }
  update(n) {
    (n.docChanged || n.selectionSet) && (this.decorations = this.getDeco(n.view));
  }
  getDeco(n) {
    let t = -1, e = [];
    for (let i of n.state.selection.ranges) {
      let s = n.lineBlockAt(i.head);
      s.from > t && (e.push(Mr.range(s.from)), t = s.from);
    }
    return E.set(e);
  }
}, {
  decorations: (n) => n.decorations
});
class Ar extends Tt {
  constructor(t) {
    super(), this.content = t;
  }
  toDOM(t) {
    let e = document.createElement("span");
    return e.className = "cm-placeholder", e.style.pointerEvents = "none", e.appendChild(typeof this.content == "string" ? document.createTextNode(this.content) : typeof this.content == "function" ? this.content(t) : this.content.cloneNode(!0)), e.setAttribute("aria-hidden", "true"), e;
  }
  coordsAt(t) {
    let e = t.firstChild ? Mt(t.firstChild) : [];
    if (!e.length)
      return null;
    let i = window.getComputedStyle(t.parentNode), s = qt(e[0], i.direction != "rtl"), o = parseInt(i.lineHeight);
    return s.bottom - s.top > o * 1.5 ? { left: s.left, right: s.right, top: s.top, bottom: s.top + o } : s;
  }
  ignoreEvent() {
    return !1;
  }
}
function ol(n) {
  let t = z.fromClass(class {
    constructor(e) {
      this.view = e, this.placeholder = n ? E.set([E.widget({ widget: new Ar(n), side: 1 }).range(0)]) : E.none;
    }
    get decorations() {
      return this.view.state.doc.length ? E.none : this.placeholder;
    }
  }, { decorations: (e) => e.decorations });
  return typeof n == "string" ? [
    t,
    A.contentAttributes.of({ "aria-placeholder": n })
  ] : t;
}
const Ze = 2e3;
function Dr(n, t, e) {
  let i = Math.min(t.line, e.line), s = Math.max(t.line, e.line), o = [];
  if (t.off > Ze || e.off > Ze || t.col < 0 || e.col < 0) {
    let r = Math.min(t.off, e.off), l = Math.max(t.off, e.off);
    for (let h = i; h <= s; h++) {
      let f = n.doc.line(h);
      f.length <= l && o.push(k.range(f.from + r, f.to + l));
    }
  } else {
    let r = Math.min(t.col, e.col), l = Math.max(t.col, e.col);
    for (let h = i; h <= s; h++) {
      let f = n.doc.line(h), a = Le(f.text, r, n.tabSize, !0);
      if (a < 0)
        o.push(k.cursor(f.to));
      else {
        let c = Le(f.text, l, n.tabSize);
        o.push(k.range(f.from + a, f.from + c));
      }
    }
  }
  return o;
}
function Or(n, t) {
  let e = n.coordsAtPos(n.viewport.from);
  return e ? Math.round(Math.abs((e.left - t) / n.defaultCharacterWidth)) : -1;
}
function Ji(n, t) {
  let e = n.posAtCoords({ x: t.clientX, y: t.clientY }, !1), i = n.state.doc.lineAt(e), s = e - i.from, o = s > Ze ? -1 : s == i.length ? Or(n, t.clientX) : ds(i.text, n.state.tabSize, e - i.from);
  return { line: i.number, col: o, off: s };
}
function Tr(n, t) {
  let e = Ji(n, t), i = n.state.selection;
  return e ? {
    update(s) {
      if (s.docChanged) {
        let o = s.changes.mapPos(s.startState.doc.line(e.line).from), r = s.state.doc.lineAt(o);
        e = { line: r.number, col: e.col, off: Math.min(e.off, r.length) }, i = i.map(s.changes);
      }
    },
    get(s, o, r) {
      let l = Ji(n, s);
      if (!l)
        return i;
      let h = Dr(n.state, e, l);
      return h.length ? r ? k.create(h.concat(i.ranges)) : k.create(h) : i;
    }
  } : null;
}
function rl(n) {
  let t = (e) => e.altKey && e.button == 0;
  return A.mouseSelectionStyle.of((e, i) => t(i) ? Tr(e, i) : null);
}
const Rr = {
  Alt: [18, (n) => !!n.altKey],
  Control: [17, (n) => !!n.ctrlKey],
  Shift: [16, (n) => !!n.shiftKey],
  Meta: [91, (n) => !!n.metaKey]
}, Er = { style: "cursor: crosshair" };
function ll(n = {}) {
  let [t, e] = Rr[n.key || "Alt"], i = z.fromClass(class {
    constructor(s) {
      this.view = s, this.isDown = !1;
    }
    set(s) {
      this.isDown != s && (this.isDown = s, this.view.update([]));
    }
  }, {
    eventObservers: {
      keydown(s) {
        this.set(s.keyCode == t || e(s));
      },
      keyup(s) {
        (s.keyCode == t || !e(s)) && this.set(!1);
      },
      mousemove(s) {
        this.set(e(s));
      }
    }
  });
  return [
    i,
    A.contentAttributes.of((s) => {
      var o;
      return !((o = s.plugin(i)) === null || o === void 0) && o.isDown ? Er : null;
    })
  ];
}
const Et = "-10000px";
class gn {
  constructor(t, e, i, s) {
    this.facet = e, this.createTooltipView = i, this.removeTooltipView = s, this.input = t.state.facet(e), this.tooltips = this.input.filter((r) => r);
    let o = null;
    this.tooltipViews = this.tooltips.map((r) => o = i(r, o));
  }
  update(t, e) {
    var i;
    let s = t.state.facet(this.facet), o = s.filter((h) => h);
    if (s === this.input) {
      for (let h of this.tooltipViews)
        h.update && h.update(t);
      return !1;
    }
    let r = [], l = e ? [] : null;
    for (let h = 0; h < o.length; h++) {
      let f = o[h], a = -1;
      if (f) {
        for (let c = 0; c < this.tooltips.length; c++) {
          let d = this.tooltips[c];
          d && d.create == f.create && (a = c);
        }
        if (a < 0)
          r[h] = this.createTooltipView(f, h ? r[h - 1] : null), l && (l[h] = !!f.above);
        else {
          let c = r[h] = this.tooltipViews[a];
          l && (l[h] = e[a]), c.update && c.update(t);
        }
      }
    }
    for (let h of this.tooltipViews)
      r.indexOf(h) < 0 && (this.removeTooltipView(h), (i = h.destroy) === null || i === void 0 || i.call(h));
    return e && (l.forEach((h, f) => e[f] = h), e.length = l.length), this.input = s, this.tooltips = o, this.tooltipViews = r, !0;
  }
}
function Br(n) {
  let t = n.dom.ownerDocument.documentElement;
  return { top: 0, left: 0, bottom: t.clientHeight, right: t.clientWidth };
}
const Re = /* @__PURE__ */ M.define({
  combine: (n) => {
    var t, e, i;
    return {
      position: y.ios ? "absolute" : ((t = n.find((s) => s.position)) === null || t === void 0 ? void 0 : t.position) || "fixed",
      parent: ((e = n.find((s) => s.parent)) === null || e === void 0 ? void 0 : e.parent) || null,
      tooltipSpace: ((i = n.find((s) => s.tooltipSpace)) === null || i === void 0 ? void 0 : i.tooltipSpace) || Br
    };
  }
}), Zi = /* @__PURE__ */ new WeakMap(), ui = /* @__PURE__ */ z.fromClass(class {
  constructor(n) {
    this.view = n, this.above = [], this.inView = !0, this.madeAbsolute = !1, this.lastTransaction = 0, this.measureTimeout = -1;
    let t = n.state.facet(Re);
    this.position = t.position, this.parent = t.parent, this.classes = n.themeClasses, this.createContainer(), this.measureReq = { read: this.readMeasure.bind(this), write: this.writeMeasure.bind(this), key: this }, this.resizeObserver = typeof ResizeObserver == "function" ? new ResizeObserver(() => this.measureSoon()) : null, this.manager = new gn(n, mn, (e, i) => this.createTooltip(e, i), (e) => {
      this.resizeObserver && this.resizeObserver.unobserve(e.dom), e.dom.remove();
    }), this.above = this.manager.tooltips.map((e) => !!e.above), this.intersectionObserver = typeof IntersectionObserver == "function" ? new IntersectionObserver((e) => {
      Date.now() > this.lastTransaction - 50 && e.length > 0 && e[e.length - 1].intersectionRatio < 1 && this.measureSoon();
    }, { threshold: [1] }) : null, this.observeIntersection(), n.win.addEventListener("resize", this.measureSoon = this.measureSoon.bind(this)), this.maybeMeasure();
  }
  createContainer() {
    this.parent ? (this.container = document.createElement("div"), this.container.style.position = "relative", this.container.className = this.view.themeClasses, this.parent.appendChild(this.container)) : this.container = this.view.dom;
  }
  observeIntersection() {
    if (this.intersectionObserver) {
      this.intersectionObserver.disconnect();
      for (let n of this.manager.tooltipViews)
        this.intersectionObserver.observe(n.dom);
    }
  }
  measureSoon() {
    this.measureTimeout < 0 && (this.measureTimeout = setTimeout(() => {
      this.measureTimeout = -1, this.maybeMeasure();
    }, 50));
  }
  update(n) {
    n.transactions.length && (this.lastTransaction = Date.now());
    let t = this.manager.update(n, this.above);
    t && this.observeIntersection();
    let e = t || n.geometryChanged, i = n.state.facet(Re);
    if (i.position != this.position && !this.madeAbsolute) {
      this.position = i.position;
      for (let s of this.manager.tooltipViews)
        s.dom.style.position = this.position;
      e = !0;
    }
    if (i.parent != this.parent) {
      this.parent && this.container.remove(), this.parent = i.parent, this.createContainer();
      for (let s of this.manager.tooltipViews)
        this.container.appendChild(s.dom);
      e = !0;
    } else this.parent && this.view.themeClasses != this.classes && (this.classes = this.container.className = this.view.themeClasses);
    e && this.maybeMeasure();
  }
  createTooltip(n, t) {
    let e = n.create(this.view), i = t ? t.dom : null;
    if (e.dom.classList.add("cm-tooltip"), n.arrow && !e.dom.querySelector(".cm-tooltip > .cm-tooltip-arrow")) {
      let s = document.createElement("div");
      s.className = "cm-tooltip-arrow", e.dom.appendChild(s);
    }
    return e.dom.style.position = this.position, e.dom.style.top = Et, e.dom.style.left = "0px", this.container.insertBefore(e.dom, i), e.mount && e.mount(this.view), this.resizeObserver && this.resizeObserver.observe(e.dom), e;
  }
  destroy() {
    var n, t, e;
    this.view.win.removeEventListener("resize", this.measureSoon);
    for (let i of this.manager.tooltipViews)
      i.dom.remove(), (n = i.destroy) === null || n === void 0 || n.call(i);
    this.parent && this.container.remove(), (t = this.resizeObserver) === null || t === void 0 || t.disconnect(), (e = this.intersectionObserver) === null || e === void 0 || e.disconnect(), clearTimeout(this.measureTimeout);
  }
  readMeasure() {
    let n = 1, t = 1, e = !1;
    if (this.position == "fixed" && this.manager.tooltipViews.length) {
      let { dom: o } = this.manager.tooltipViews[0];
      if (y.gecko)
        e = o.offsetParent != this.container.ownerDocument.body;
      else if (o.style.top == Et && o.style.left == "0px") {
        let r = o.getBoundingClientRect();
        e = Math.abs(r.top + 1e4) > 1 || Math.abs(r.left) > 1;
      }
    }
    if (e || this.position == "absolute")
      if (this.parent) {
        let o = this.parent.getBoundingClientRect();
        o.width && o.height && (n = o.width / this.parent.offsetWidth, t = o.height / this.parent.offsetHeight);
      } else
        ({ scaleX: n, scaleY: t } = this.view.viewState);
    let i = this.view.scrollDOM.getBoundingClientRect(), s = ai(this.view);
    return {
      visible: {
        left: i.left + s.left,
        top: i.top + s.top,
        right: i.right - s.right,
        bottom: i.bottom - s.bottom
      },
      parent: this.parent ? this.container.getBoundingClientRect() : this.view.dom.getBoundingClientRect(),
      pos: this.manager.tooltips.map((o, r) => {
        let l = this.manager.tooltipViews[r];
        return l.getCoords ? l.getCoords(o.pos) : this.view.coordsAtPos(o.pos);
      }),
      size: this.manager.tooltipViews.map(({ dom: o }) => o.getBoundingClientRect()),
      space: this.view.state.facet(Re).tooltipSpace(this.view),
      scaleX: n,
      scaleY: t,
      makeAbsolute: e
    };
  }
  writeMeasure(n) {
    var t;
    if (n.makeAbsolute) {
      this.madeAbsolute = !0, this.position = "absolute";
      for (let l of this.manager.tooltipViews)
        l.dom.style.position = "absolute";
    }
    let { visible: e, space: i, scaleX: s, scaleY: o } = n, r = [];
    for (let l = 0; l < this.manager.tooltips.length; l++) {
      let h = this.manager.tooltips[l], f = this.manager.tooltipViews[l], { dom: a } = f, c = n.pos[l], d = n.size[l];
      if (!c || h.clip !== !1 && (c.bottom <= Math.max(e.top, i.top) || c.top >= Math.min(e.bottom, i.bottom) || c.right < Math.max(e.left, i.left) - 0.1 || c.left > Math.min(e.right, i.right) + 0.1)) {
        a.style.top = Et;
        continue;
      }
      let u = h.arrow ? f.dom.querySelector(".cm-tooltip-arrow") : null, p = u ? 7 : 0, g = d.right - d.left, m = (t = Zi.get(f)) !== null && t !== void 0 ? t : d.bottom - d.top, b = f.offset || Pr, w = this.view.textDirection == B.LTR, S = d.width > i.right - i.left ? w ? i.left : i.right - d.width : w ? Math.max(i.left, Math.min(c.left - (u ? 14 : 0) + b.x, i.right - g)) : Math.min(Math.max(i.left, c.left - g + (u ? 14 : 0) - b.x), i.right - g), x = this.above[l];
      !h.strictSide && (x ? c.top - m - p - b.y < i.top : c.bottom + m + p + b.y > i.bottom) && x == i.bottom - c.bottom > c.top - i.top && (x = this.above[l] = !x);
      let v = (x ? c.top - i.top : i.bottom - c.bottom) - p;
      if (v < m && f.resize !== !1) {
        if (v < this.view.defaultLineHeight) {
          a.style.top = Et;
          continue;
        }
        Zi.set(f, m), a.style.height = (m = v) / o + "px";
      } else a.style.height && (a.style.height = "");
      let C = x ? c.top - m - p - b.y : c.bottom + p + b.y, R = S + g;
      if (f.overlap !== !0)
        for (let P of r)
          P.left < R && P.right > S && P.top < C + m && P.bottom > C && (C = x ? P.top - m - 2 - p : P.bottom + p + 2);
      if (this.position == "absolute" ? (a.style.top = (C - n.parent.top) / o + "px", ts(a, (S - n.parent.left) / s)) : (a.style.top = C / o + "px", ts(a, S / s)), u) {
        let P = c.left + (w ? b.x : -b.x) - (S + 14 - 7);
        u.style.left = P / s + "px";
      }
      f.overlap !== !0 && r.push({ left: S, top: C, right: R, bottom: C + m }), a.classList.toggle("cm-tooltip-above", x), a.classList.toggle("cm-tooltip-below", !x), f.positioned && f.positioned(n.space);
    }
  }
  maybeMeasure() {
    if (this.manager.tooltips.length && (this.view.inView && this.view.requestMeasure(this.measureReq), this.inView != this.view.inView && (this.inView = this.view.inView, !this.inView)))
      for (let n of this.manager.tooltipViews)
        n.dom.style.top = Et;
  }
}, {
  eventObservers: {
    scroll() {
      this.maybeMeasure();
    }
  }
});
function ts(n, t) {
  let e = parseInt(n.style.left, 10);
  (isNaN(e) || Math.abs(t - e) > 1) && (n.style.left = t + "px");
}
const Lr = /* @__PURE__ */ A.baseTheme({
  ".cm-tooltip": {
    zIndex: 500,
    boxSizing: "border-box"
  },
  "&light .cm-tooltip": {
    border: "1px solid #bbb",
    backgroundColor: "#f5f5f5"
  },
  "&light .cm-tooltip-section:not(:first-child)": {
    borderTop: "1px solid #bbb"
  },
  "&dark .cm-tooltip": {
    backgroundColor: "#333338",
    color: "white"
  },
  ".cm-tooltip-arrow": {
    height: "7px",
    width: `${7 * 2}px`,
    position: "absolute",
    zIndex: -1,
    overflow: "hidden",
    "&:before, &:after": {
      content: "''",
      position: "absolute",
      width: 0,
      height: 0,
      borderLeft: "7px solid transparent",
      borderRight: "7px solid transparent"
    },
    ".cm-tooltip-above &": {
      bottom: "-7px",
      "&:before": {
        borderTop: "7px solid #bbb"
      },
      "&:after": {
        borderTop: "7px solid #f5f5f5",
        bottom: "1px"
      }
    },
    ".cm-tooltip-below &": {
      top: "-7px",
      "&:before": {
        borderBottom: "7px solid #bbb"
      },
      "&:after": {
        borderBottom: "7px solid #f5f5f5",
        top: "1px"
      }
    }
  },
  "&dark .cm-tooltip .cm-tooltip-arrow": {
    "&:before": {
      borderTopColor: "#333338",
      borderBottomColor: "#333338"
    },
    "&:after": {
      borderTopColor: "transparent",
      borderBottomColor: "transparent"
    }
  }
}), Pr = { x: 0, y: 0 }, mn = /* @__PURE__ */ M.define({
  enables: [ui, Lr]
}), we = /* @__PURE__ */ M.define({
  combine: (n) => n.reduce((t, e) => t.concat(e), [])
});
class ve {
  // Needs to be static so that host tooltip instances always match
  static create(t) {
    return new ve(t);
  }
  constructor(t) {
    this.view = t, this.mounted = !1, this.dom = document.createElement("div"), this.dom.classList.add("cm-tooltip-hover"), this.manager = new gn(t, we, (e, i) => this.createHostedView(e, i), (e) => e.dom.remove());
  }
  createHostedView(t, e) {
    let i = t.create(this.view);
    return i.dom.classList.add("cm-tooltip-section"), this.dom.insertBefore(i.dom, e ? e.dom.nextSibling : this.dom.firstChild), this.mounted && i.mount && i.mount(this.view), i;
  }
  mount(t) {
    for (let e of this.manager.tooltipViews)
      e.mount && e.mount(t);
    this.mounted = !0;
  }
  positioned(t) {
    for (let e of this.manager.tooltipViews)
      e.positioned && e.positioned(t);
  }
  update(t) {
    this.manager.update(t);
  }
  destroy() {
    var t;
    for (let e of this.manager.tooltipViews)
      (t = e.destroy) === null || t === void 0 || t.call(e);
  }
  passProp(t) {
    let e;
    for (let i of this.manager.tooltipViews) {
      let s = i[t];
      if (s !== void 0) {
        if (e === void 0)
          e = s;
        else if (e !== s)
          return;
      }
    }
    return e;
  }
  get offset() {
    return this.passProp("offset");
  }
  get getCoords() {
    return this.passProp("getCoords");
  }
  get overlap() {
    return this.passProp("overlap");
  }
  get resize() {
    return this.passProp("resize");
  }
}
const Hr = /* @__PURE__ */ mn.compute([we], (n) => {
  let t = n.facet(we);
  return t.length === 0 ? null : {
    pos: Math.min(...t.map((e) => e.pos)),
    end: Math.max(...t.map((e) => {
      var i;
      return (i = e.end) !== null && i !== void 0 ? i : e.pos;
    })),
    create: ve.create,
    above: t[0].above,
    arrow: t.some((e) => e.arrow)
  };
});
class Nr {
  constructor(t, e, i, s, o) {
    this.view = t, this.source = e, this.field = i, this.setHover = s, this.hoverTime = o, this.hoverTimeout = -1, this.restartTimeout = -1, this.pending = null, this.lastMove = { x: 0, y: 0, target: t.dom, time: 0 }, this.checkHover = this.checkHover.bind(this), t.dom.addEventListener("mouseleave", this.mouseleave = this.mouseleave.bind(this)), t.dom.addEventListener("mousemove", this.mousemove = this.mousemove.bind(this));
  }
  update() {
    this.pending && (this.pending = null, clearTimeout(this.restartTimeout), this.restartTimeout = setTimeout(() => this.startHover(), 20));
  }
  get active() {
    return this.view.state.field(this.field);
  }
  checkHover() {
    if (this.hoverTimeout = -1, this.active.length)
      return;
    let t = Date.now() - this.lastMove.time;
    t < this.hoverTime ? this.hoverTimeout = setTimeout(this.checkHover, this.hoverTime - t) : this.startHover();
  }
  startHover() {
    clearTimeout(this.restartTimeout);
    let { view: t, lastMove: e } = this, i = t.docView.nearest(e.target);
    if (!i)
      return;
    let s, o = 1;
    if (i instanceof rt)
      s = i.posAtStart;
    else {
      if (s = t.posAtCoords(e), s == null)
        return;
      let l = t.coordsAtPos(s);
      if (!l || e.y < l.top || e.y > l.bottom || e.x < l.left - t.defaultCharacterWidth || e.x > l.right + t.defaultCharacterWidth)
        return;
      let h = t.bidiSpans(t.state.doc.lineAt(s)).find((a) => a.from <= s && a.to >= s), f = h && h.dir == B.RTL ? -1 : 1;
      o = e.x < l.left ? -f : f;
    }
    let r = this.source(t, s, o);
    if (r != null && r.then) {
      let l = this.pending = { pos: s };
      r.then((h) => {
        this.pending == l && (this.pending = null, h && !(Array.isArray(h) && !h.length) && t.dispatch({ effects: this.setHover.of(Array.isArray(h) ? h : [h]) }));
      }, (h) => U(t.state, h, "hover tooltip"));
    } else r && !(Array.isArray(r) && !r.length) && t.dispatch({ effects: this.setHover.of(Array.isArray(r) ? r : [r]) });
  }
  get tooltip() {
    let t = this.view.plugin(ui), e = t ? t.manager.tooltips.findIndex((i) => i.create == ve.create) : -1;
    return e > -1 ? t.manager.tooltipViews[e] : null;
  }
  mousemove(t) {
    var e, i;
    this.lastMove = { x: t.clientX, y: t.clientY, target: t.target, time: Date.now() }, this.hoverTimeout < 0 && (this.hoverTimeout = setTimeout(this.checkHover, this.hoverTime));
    let { active: s, tooltip: o } = this;
    if (s.length && o && !Vr(o.dom, t) || this.pending) {
      let { pos: r } = s[0] || this.pending, l = (i = (e = s[0]) === null || e === void 0 ? void 0 : e.end) !== null && i !== void 0 ? i : r;
      (r == l ? this.view.posAtCoords(this.lastMove) != r : !Fr(this.view, r, l, t.clientX, t.clientY)) && (this.view.dispatch({ effects: this.setHover.of([]) }), this.pending = null);
    }
  }
  mouseleave(t) {
    clearTimeout(this.hoverTimeout), this.hoverTimeout = -1;
    let { active: e } = this;
    if (e.length) {
      let { tooltip: i } = this;
      i && i.dom.contains(t.relatedTarget) ? this.watchTooltipLeave(i.dom) : this.view.dispatch({ effects: this.setHover.of([]) });
    }
  }
  watchTooltipLeave(t) {
    let e = (i) => {
      t.removeEventListener("mouseleave", e), this.active.length && !this.view.dom.contains(i.relatedTarget) && this.view.dispatch({ effects: this.setHover.of([]) });
    };
    t.addEventListener("mouseleave", e);
  }
  destroy() {
    clearTimeout(this.hoverTimeout), this.view.dom.removeEventListener("mouseleave", this.mouseleave), this.view.dom.removeEventListener("mousemove", this.mousemove);
  }
}
const oe = 4;
function Vr(n, t) {
  let { left: e, right: i, top: s, bottom: o } = n.getBoundingClientRect(), r;
  if (r = n.querySelector(".cm-tooltip-arrow")) {
    let l = r.getBoundingClientRect();
    s = Math.min(l.top, s), o = Math.max(l.bottom, o);
  }
  return t.clientX >= e - oe && t.clientX <= i + oe && t.clientY >= s - oe && t.clientY <= o + oe;
}
function Fr(n, t, e, i, s, o) {
  let r = n.scrollDOM.getBoundingClientRect(), l = n.documentTop + n.documentPadding.top + n.contentHeight;
  if (r.left > i || r.right < i || r.top > s || Math.min(r.bottom, l) < s)
    return !1;
  let h = n.posAtCoords({ x: i, y: s }, !1);
  return h >= t && h <= e;
}
function hl(n, t = {}) {
  let e = Ot.define(), i = fs.define({
    create() {
      return [];
    },
    update(s, o) {
      if (s.length && (t.hideOnChange && (o.docChanged || o.selection) ? s = [] : t.hideOn && (s = s.filter((r) => !t.hideOn(o, r))), o.docChanged)) {
        let r = [];
        for (let l of s) {
          let h = o.changes.mapPos(l.pos, -1, xt.TrackDel);
          if (h != null) {
            let f = Object.assign(/* @__PURE__ */ Object.create(null), l);
            f.pos = h, f.end != null && (f.end = o.changes.mapPos(f.end)), r.push(f);
          }
        }
        s = r;
      }
      for (let r of o.effects)
        r.is(e) && (s = r.value), r.is(Wr) && (s = []);
      return s;
    },
    provide: (s) => we.from(s)
  });
  return {
    active: i,
    extension: [
      i,
      z.define((s) => new Nr(
        s,
        n,
        i,
        e,
        t.hoverTime || 300
        /* Hover.Time */
      )),
      Hr
    ]
  };
}
function al(n, t) {
  let e = n.plugin(ui);
  if (!e)
    return null;
  let i = e.manager.tooltips.indexOf(t);
  return i < 0 ? null : e.manager.tooltipViews[i];
}
const Wr = /* @__PURE__ */ Ot.define(), es = /* @__PURE__ */ M.define({
  combine(n) {
    let t, e;
    for (let i of n)
      t = t || i.topContainer, e = e || i.bottomContainer;
    return { topContainer: t, bottomContainer: e };
  }
});
function fl(n, t) {
  let e = n.plugin(bn), i = e ? e.specs.indexOf(t) : -1;
  return i > -1 ? e.panels[i] : null;
}
const bn = /* @__PURE__ */ z.fromClass(class {
  constructor(n) {
    this.input = n.state.facet(ss), this.specs = this.input.filter((e) => e), this.panels = this.specs.map((e) => e(n));
    let t = n.state.facet(es);
    this.top = new re(n, !0, t.topContainer), this.bottom = new re(n, !1, t.bottomContainer), this.top.sync(this.panels.filter((e) => e.top)), this.bottom.sync(this.panels.filter((e) => !e.top));
    for (let e of this.panels)
      e.dom.classList.add("cm-panel"), e.mount && e.mount();
  }
  update(n) {
    let t = n.state.facet(es);
    this.top.container != t.topContainer && (this.top.sync([]), this.top = new re(n.view, !0, t.topContainer)), this.bottom.container != t.bottomContainer && (this.bottom.sync([]), this.bottom = new re(n.view, !1, t.bottomContainer)), this.top.syncClasses(), this.bottom.syncClasses();
    let e = n.state.facet(ss);
    if (e != this.input) {
      let i = e.filter((h) => h), s = [], o = [], r = [], l = [];
      for (let h of i) {
        let f = this.specs.indexOf(h), a;
        f < 0 ? (a = h(n.view), l.push(a)) : (a = this.panels[f], a.update && a.update(n)), s.push(a), (a.top ? o : r).push(a);
      }
      this.specs = i, this.panels = s, this.top.sync(o), this.bottom.sync(r);
      for (let h of l)
        h.dom.classList.add("cm-panel"), h.mount && h.mount();
    } else
      for (let i of this.panels)
        i.update && i.update(n);
  }
  destroy() {
    this.top.sync([]), this.bottom.sync([]);
  }
}, {
  provide: (n) => A.scrollMargins.of((t) => {
    let e = t.plugin(n);
    return e && { top: e.top.scrollMargin(), bottom: e.bottom.scrollMargin() };
  })
});
class re {
  constructor(t, e, i) {
    this.view = t, this.top = e, this.container = i, this.dom = void 0, this.classes = "", this.panels = [], this.syncClasses();
  }
  sync(t) {
    for (let e of this.panels)
      e.destroy && t.indexOf(e) < 0 && e.destroy();
    this.panels = t, this.syncDOM();
  }
  syncDOM() {
    if (this.panels.length == 0) {
      this.dom && (this.dom.remove(), this.dom = void 0);
      return;
    }
    if (!this.dom) {
      this.dom = document.createElement("div"), this.dom.className = this.top ? "cm-panels cm-panels-top" : "cm-panels cm-panels-bottom", this.dom.style[this.top ? "top" : "bottom"] = "0";
      let e = this.container || this.view.dom;
      e.insertBefore(this.dom, this.top ? e.firstChild : null);
    }
    let t = this.dom.firstChild;
    for (let e of this.panels)
      if (e.dom.parentNode == this.dom) {
        for (; t != e.dom; )
          t = is(t);
        t = t.nextSibling;
      } else
        this.dom.insertBefore(e.dom, t);
    for (; t; )
      t = is(t);
  }
  scrollMargin() {
    return !this.dom || this.container ? 0 : Math.max(0, this.top ? this.dom.getBoundingClientRect().bottom - Math.max(0, this.view.scrollDOM.getBoundingClientRect().top) : Math.min(innerHeight, this.view.scrollDOM.getBoundingClientRect().bottom) - this.dom.getBoundingClientRect().top);
  }
  syncClasses() {
    if (!(!this.container || this.classes == this.view.themeClasses)) {
      for (let t of this.classes.split(" "))
        t && this.container.classList.remove(t);
      for (let t of (this.classes = this.view.themeClasses).split(" "))
        t && this.container.classList.add(t);
    }
  }
}
function is(n) {
  let t = n.nextSibling;
  return n.remove(), t;
}
const ss = /* @__PURE__ */ M.define({
  enables: bn
});
class at extends as {
  /**
  @internal
  */
  compare(t) {
    return this == t || this.constructor == t.constructor && this.eq(t);
  }
  /**
  Compare this marker to another marker of the same type.
  */
  eq(t) {
    return !1;
  }
  /**
  Called if the marker has a `toDOM` method and its representation
  was removed from a gutter.
  */
  destroy(t) {
  }
}
at.prototype.elementClass = "";
at.prototype.toDOM = void 0;
at.prototype.mapMode = xt.TrackBefore;
at.prototype.startSide = at.prototype.endSide = -1;
at.prototype.point = !0;
const ue = /* @__PURE__ */ M.define(), zr = /* @__PURE__ */ M.define(), Ir = {
  class: "",
  renderEmptyElements: !1,
  elementStyle: "",
  markers: () => V.empty,
  lineMarker: () => null,
  widgetMarker: () => null,
  lineMarkerChange: null,
  initialSpacer: null,
  updateSpacer: null,
  domEventHandlers: {},
  side: "before"
}, Wt = /* @__PURE__ */ M.define();
function cl(n) {
  return [yn(), Wt.of({ ...Ir, ...n })];
}
const ns = /* @__PURE__ */ M.define({
  combine: (n) => n.some((t) => t)
});
function yn(n) {
  return [
    Kr
  ];
}
const Kr = /* @__PURE__ */ z.fromClass(class {
  constructor(n) {
    this.view = n, this.domAfter = null, this.prevViewport = n.viewport, this.dom = document.createElement("div"), this.dom.className = "cm-gutters cm-gutters-before", this.dom.setAttribute("aria-hidden", "true"), this.dom.style.minHeight = this.view.contentHeight / this.view.scaleY + "px", this.gutters = n.state.facet(Wt).map((t) => new rs(n, t)), this.fixed = !n.state.facet(ns);
    for (let t of this.gutters)
      t.config.side == "after" ? this.getDOMAfter().appendChild(t.dom) : this.dom.appendChild(t.dom);
    this.fixed && (this.dom.style.position = "sticky"), this.syncGutters(!1), n.scrollDOM.insertBefore(this.dom, n.contentDOM);
  }
  getDOMAfter() {
    return this.domAfter || (this.domAfter = document.createElement("div"), this.domAfter.className = "cm-gutters cm-gutters-after", this.domAfter.setAttribute("aria-hidden", "true"), this.domAfter.style.minHeight = this.view.contentHeight / this.view.scaleY + "px", this.domAfter.style.position = this.fixed ? "sticky" : "", this.view.scrollDOM.appendChild(this.domAfter)), this.domAfter;
  }
  update(n) {
    if (this.updateGutters(n)) {
      let t = this.prevViewport, e = n.view.viewport, i = Math.min(t.to, e.to) - Math.max(t.from, e.from);
      this.syncGutters(i < (e.to - e.from) * 0.8);
    }
    if (n.geometryChanged) {
      let t = this.view.contentHeight / this.view.scaleY + "px";
      this.dom.style.minHeight = t, this.domAfter && (this.domAfter.style.minHeight = t);
    }
    this.view.state.facet(ns) != !this.fixed && (this.fixed = !this.fixed, this.dom.style.position = this.fixed ? "sticky" : "", this.domAfter && (this.domAfter.style.position = this.fixed ? "sticky" : "")), this.prevViewport = n.view.viewport;
  }
  syncGutters(n) {
    let t = this.dom.nextSibling;
    n && (this.dom.remove(), this.domAfter && this.domAfter.remove());
    let e = V.iter(this.view.state.facet(ue), this.view.viewport.from), i = [], s = this.gutters.map((o) => new qr(o, this.view.viewport, -this.view.documentPadding.top));
    for (let o of this.view.viewportLineBlocks)
      if (i.length && (i = []), Array.isArray(o.type)) {
        let r = !0;
        for (let l of o.type)
          if (l.type == F.Text && r) {
            ti(e, i, l.from);
            for (let h of s)
              h.line(this.view, l, i);
            r = !1;
          } else if (l.widget)
            for (let h of s)
              h.widget(this.view, l);
      } else if (o.type == F.Text) {
        ti(e, i, o.from);
        for (let r of s)
          r.line(this.view, o, i);
      } else if (o.widget)
        for (let r of s)
          r.widget(this.view, o);
    for (let o of s)
      o.finish();
    n && (this.view.scrollDOM.insertBefore(this.dom, t), this.domAfter && this.view.scrollDOM.appendChild(this.domAfter));
  }
  updateGutters(n) {
    let t = n.startState.facet(Wt), e = n.state.facet(Wt), i = n.docChanged || n.heightChanged || n.viewportChanged || !V.eq(n.startState.facet(ue), n.state.facet(ue), n.view.viewport.from, n.view.viewport.to);
    if (t == e)
      for (let s of this.gutters)
        s.update(n) && (i = !0);
    else {
      i = !0;
      let s = [];
      for (let o of e) {
        let r = t.indexOf(o);
        r < 0 ? s.push(new rs(this.view, o)) : (this.gutters[r].update(n), s.push(this.gutters[r]));
      }
      for (let o of this.gutters)
        o.dom.remove(), s.indexOf(o) < 0 && o.destroy();
      for (let o of s)
        o.config.side == "after" ? this.getDOMAfter().appendChild(o.dom) : this.dom.appendChild(o.dom);
      this.gutters = s;
    }
    return i;
  }
  destroy() {
    for (let n of this.gutters)
      n.destroy();
    this.dom.remove(), this.domAfter && this.domAfter.remove();
  }
}, {
  provide: (n) => A.scrollMargins.of((t) => {
    let e = t.plugin(n);
    if (!e || e.gutters.length == 0 || !e.fixed)
      return null;
    let i = e.dom.offsetWidth * t.scaleX, s = e.domAfter ? e.domAfter.offsetWidth * t.scaleX : 0;
    return t.textDirection == B.LTR ? { left: i, right: s } : { right: i, left: s };
  })
});
function os(n) {
  return Array.isArray(n) ? n : [n];
}
function ti(n, t, e) {
  for (; n.value && n.from <= e; )
    n.from == e && t.push(n.value), n.next();
}
class qr {
  constructor(t, e, i) {
    this.gutter = t, this.height = i, this.i = 0, this.cursor = V.iter(t.markers, e.from);
  }
  addElement(t, e, i) {
    let { gutter: s } = this, o = (e.top - this.height) / t.scaleY, r = e.height / t.scaleY;
    if (this.i == s.elements.length) {
      let l = new wn(t, r, o, i);
      s.elements.push(l), s.dom.appendChild(l.dom);
    } else
      s.elements[this.i].update(t, r, o, i);
    this.height = e.bottom, this.i++;
  }
  line(t, e, i) {
    let s = [];
    ti(this.cursor, s, e.from), i.length && (s = s.concat(i));
    let o = this.gutter.config.lineMarker(t, e, s);
    o && s.unshift(o);
    let r = this.gutter;
    s.length == 0 && !r.config.renderEmptyElements || this.addElement(t, e, s);
  }
  widget(t, e) {
    let i = this.gutter.config.widgetMarker(t, e.widget, e), s = i ? [i] : null;
    for (let o of t.state.facet(zr)) {
      let r = o(t, e.widget, e);
      r && (s || (s = [])).push(r);
    }
    s && this.addElement(t, e, s);
  }
  finish() {
    let t = this.gutter;
    for (; t.elements.length > this.i; ) {
      let e = t.elements.pop();
      t.dom.removeChild(e.dom), e.destroy();
    }
  }
}
class rs {
  constructor(t, e) {
    this.view = t, this.config = e, this.elements = [], this.spacer = null, this.dom = document.createElement("div"), this.dom.className = "cm-gutter" + (this.config.class ? " " + this.config.class : "");
    for (let i in e.domEventHandlers)
      this.dom.addEventListener(i, (s) => {
        let o = s.target, r;
        if (o != this.dom && this.dom.contains(o)) {
          for (; o.parentNode != this.dom; )
            o = o.parentNode;
          let h = o.getBoundingClientRect();
          r = (h.top + h.bottom) / 2;
        } else
          r = s.clientY;
        let l = t.lineBlockAtHeight(r - t.documentTop);
        e.domEventHandlers[i](t, l, s) && s.preventDefault();
      });
    this.markers = os(e.markers(t)), e.initialSpacer && (this.spacer = new wn(t, 0, 0, [e.initialSpacer(t)]), this.dom.appendChild(this.spacer.dom), this.spacer.dom.style.cssText += "visibility: hidden; pointer-events: none");
  }
  update(t) {
    let e = this.markers;
    if (this.markers = os(this.config.markers(t.view)), this.spacer && this.config.updateSpacer) {
      let s = this.config.updateSpacer(this.spacer.markers[0], t);
      s != this.spacer.markers[0] && this.spacer.update(t.view, 0, 0, [s]);
    }
    let i = t.view.viewport;
    return !V.eq(this.markers, e, i.from, i.to) || (this.config.lineMarkerChange ? this.config.lineMarkerChange(t) : !1);
  }
  destroy() {
    for (let t of this.elements)
      t.destroy();
  }
}
class wn {
  constructor(t, e, i, s) {
    this.height = -1, this.above = 0, this.markers = [], this.dom = document.createElement("div"), this.dom.className = "cm-gutterElement", this.update(t, e, i, s);
  }
  update(t, e, i, s) {
    this.height != e && (this.height = e, this.dom.style.height = e + "px"), this.above != i && (this.dom.style.marginTop = (this.above = i) ? i + "px" : ""), Yr(this.markers, s) || this.setMarkers(t, s);
  }
  setMarkers(t, e) {
    let i = "cm-gutterElement", s = this.dom.firstChild;
    for (let o = 0, r = 0; ; ) {
      let l = r, h = o < e.length ? e[o++] : null, f = !1;
      if (h) {
        let a = h.elementClass;
        a && (i += " " + a);
        for (let c = r; c < this.markers.length; c++)
          if (this.markers[c].compare(h)) {
            l = c, f = !0;
            break;
          }
      } else
        l = this.markers.length;
      for (; r < l; ) {
        let a = this.markers[r++];
        if (a.toDOM) {
          a.destroy(s);
          let c = s.nextSibling;
          s.remove(), s = c;
        }
      }
      if (!h)
        break;
      h.toDOM && (f ? s = s.nextSibling : this.dom.insertBefore(h.toDOM(t), s)), f && r++;
    }
    this.dom.className = i, this.markers = e;
  }
  destroy() {
    this.setMarkers(null, []);
  }
}
function Yr(n, t) {
  if (n.length != t.length)
    return !1;
  for (let e = 0; e < n.length; e++)
    if (!n[e].compare(t[e]))
      return !1;
  return !0;
}
const jr = /* @__PURE__ */ M.define(), _r = /* @__PURE__ */ M.define(), wt = /* @__PURE__ */ M.define({
  combine(n) {
    return ii(n, { formatNumber: String, domEventHandlers: {} }, {
      domEventHandlers(t, e) {
        let i = Object.assign({}, t);
        for (let s in e) {
          let o = i[s], r = e[s];
          i[s] = o ? (l, h, f) => o(l, h, f) || r(l, h, f) : r;
        }
        return i;
      }
    });
  }
});
class Ee extends at {
  constructor(t) {
    super(), this.number = t;
  }
  eq(t) {
    return this.number == t.number;
  }
  toDOM() {
    return document.createTextNode(this.number);
  }
}
function Be(n, t) {
  return n.state.facet(wt).formatNumber(t, n.state);
}
const Xr = /* @__PURE__ */ Wt.compute([wt], (n) => ({
  class: "cm-lineNumbers",
  renderEmptyElements: !1,
  markers(t) {
    return t.state.facet(jr);
  },
  lineMarker(t, e, i) {
    return i.some((s) => s.toDOM) ? null : new Ee(Be(t, t.state.doc.lineAt(e.from).number));
  },
  widgetMarker: (t, e, i) => {
    for (let s of t.state.facet(_r)) {
      let o = s(t, e, i);
      if (o)
        return o;
    }
    return null;
  },
  lineMarkerChange: (t) => t.startState.facet(wt) != t.state.facet(wt),
  initialSpacer(t) {
    return new Ee(Be(t, ls(t.state.doc.lines)));
  },
  updateSpacer(t, e) {
    let i = Be(e.view, ls(e.view.state.doc.lines));
    return i == t.number ? t : new Ee(i);
  },
  domEventHandlers: n.facet(wt).domEventHandlers,
  side: "before"
}));
function dl(n = {}) {
  return [
    wt.of(n),
    yn(),
    Xr
  ];
}
function ls(n) {
  let t = 9;
  for (; t < n; )
    t = t * 10 + 9;
  return t;
}
const Gr = /* @__PURE__ */ new class extends at {
  constructor() {
    super(...arguments), this.elementClass = "cm-activeLineGutter";
  }
}(), $r = /* @__PURE__ */ ue.compute(["selection"], (n) => {
  let t = [], e = -1;
  for (let i of n.selection.ranges) {
    let s = n.doc.lineAt(i.head).from;
    s > e && (e = s, t.push(Gr.range(s)));
  }
  return V.of(t);
});
function ul() {
  return $r;
}
export {
  lt as BidiSpan,
  $ as BlockInfo,
  F as BlockType,
  E as Decoration,
  B as Direction,
  A as EditorView,
  at as GutterMarker,
  gr as MatchDecorator,
  _t as RectangleMarker,
  z as ViewPlugin,
  me as ViewUpdate,
  Tt as WidgetType,
  ll as crosshairCursor,
  el as drawSelection,
  il as dropCursor,
  fl as getPanel,
  al as getTooltip,
  cl as gutter,
  ue as gutterLineClass,
  zr as gutterWidgetClass,
  yn as gutters,
  nl as highlightActiveLine,
  ul as highlightActiveLineGutter,
  sl as highlightSpecialChars,
  hl as hoverTooltip,
  nr as keymap,
  dn as layer,
  jr as lineNumberMarkers,
  _r as lineNumberWidgetMarker,
  dl as lineNumbers,
  U as logException,
  ol as placeholder,
  rl as rectangularSelection,
  tl as runScopeHandlers,
  ss as showPanel,
  mn as showTooltip
};
//# sourceMappingURL=index.mjs.map