@kerebron/legacy-compat 0.7.7 → 0.7.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/kerebron.js CHANGED
@@ -10064,7 +10064,9 @@ class CommandManager {
10064
10064
  return () => this.createCan();
10065
10065
  }
10066
10066
  createChain(e, A = !0) {
10067
- const { commandFactories: n, editor: r } = this, o = this.editor.state, { view: s } = r, a = [], l = !!e, g = e || o.tr, c = createChainableState(g, o), I = () => {
10067
+ const { commandFactories: n, editor: r } = this, o = this.editor.state, { view: s } = r, a = [], l = !!e, g = e || o.tr;
10068
+ g.setMeta("isCommand", !0);
10069
+ const c = createChainableState(g, o), I = () => {
10068
10070
  }, d = {
10069
10071
  ...Object.fromEntries(
10070
10072
  Object.entries(n).map(([h, u]) => [h, (...E) => {
@@ -34389,6 +34391,75 @@ class ExtensionTables extends Extension {
34389
34391
  ];
34390
34392
  }
34391
34393
  }
34394
+ const CSS_PREFIX = "kb-autocomplete";
34395
+ class DefaultRenderer extends EventTarget {
34396
+ constructor(e) {
34397
+ super(), this.editor = e, this.items = [], this.pos = -1, this.command = () => {
34398
+ }, this.keyDownHandler = (A) => {
34399
+ this.onKeyDown({ event: A }) && (A.stopPropagation(), A.preventDefault());
34400
+ };
34401
+ }
34402
+ onUpdate(e) {
34403
+ this.command = e.command, this.items.splice(0, this.items.length, ...e.items), this.props = e, this.refresh();
34404
+ }
34405
+ destroy() {
34406
+ document.body.removeEventListener("keydown", this.keyDownHandler, {
34407
+ capture: !0
34408
+ }), this.wrapper && (this.wrapper.parentNode?.removeChild(this.wrapper), this.wrapper = void 0, this.dispatchEvent(new Event("close"))), this.pos = -1;
34409
+ }
34410
+ onKeyDown(e) {
34411
+ if (!this.wrapper || this.items.length === 0)
34412
+ return !1;
34413
+ if (e.event.key === "Escape" && this.wrapper)
34414
+ return this.wrapper.parentNode?.removeChild(this.wrapper), this.wrapper = void 0, !0;
34415
+ if (e.event.key === "ArrowUp" && this.pos > -1)
34416
+ return this.pos = this.pos - 1, this.refresh(), !0;
34417
+ if (e.event.key === "ArrowDown" && this.pos < this.items.length - 1)
34418
+ return this.pos++, this.refresh(), !0;
34419
+ if (e.event.key === "Enter" && this.pos > -1 && this.pos < this.items.length) {
34420
+ const A = this.items[this.pos];
34421
+ return this.items.splice(0, this.items.length), this.destroy(), this.command(A), !0;
34422
+ }
34423
+ return !1;
34424
+ }
34425
+ createListItem(e, A) {
34426
+ const n = document.createElement("li");
34427
+ return A === this.pos && n.classList.add("active"), n.innerText = "" + e, n.style.cursor = "pointer", n.addEventListener("click", () => {
34428
+ this.destroy(), this.command(e);
34429
+ }), n;
34430
+ }
34431
+ refresh() {
34432
+ if (this.wrapper || (this.wrapper = document.createElement("dialog"), this.wrapper.classList.add(CSS_PREFIX + "__wrapper"), document.body.appendChild(this.wrapper), this.list = document.createElement("ul"), this.wrapper.appendChild(this.list)), document.body.removeEventListener("keydown", this.keyDownHandler, {
34433
+ capture: !0
34434
+ }), !this.list)
34435
+ return;
34436
+ this.list.innerHTML = "";
34437
+ for (let n = 0; n < this.items.length; n++) {
34438
+ const r = this.items[n];
34439
+ this.list.appendChild(this.createListItem(r, n));
34440
+ }
34441
+ this.items.length > 0 && document.body.addEventListener("keydown", this.keyDownHandler, {
34442
+ capture: !0
34443
+ });
34444
+ let e = !1;
34445
+ this.items.length === 0 || (e = !0);
34446
+ const A = this.props?.clientRect?.();
34447
+ if (A?.height) {
34448
+ const n = A.left, r = A.bottom;
34449
+ this.wrapper.style.left = n + "px", this.wrapper.style.top = r + "px";
34450
+ } else
34451
+ e = !1;
34452
+ if (e) {
34453
+ if (!this.wrapper.open) {
34454
+ const n = document.activeElement, r = n instanceof HTMLElement ? n : null;
34455
+ this.wrapper.show(), requestAnimationFrame(() => {
34456
+ r && r.isConnected && r.focus();
34457
+ });
34458
+ }
34459
+ } else
34460
+ this.wrapper.open && this.wrapper.close();
34461
+ }
34462
+ }
34392
34463
  function escapeForRegEx(t) {
34393
34464
  return t.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
34394
34465
  }
@@ -34403,10 +34474,10 @@ function createDefaultMatcher(t = {}) {
34403
34474
  "gm"
34404
34475
  ), d = o.nodeBefore?.isText && o.nodeBefore.text;
34405
34476
  if (!d)
34406
- return null;
34477
+ return;
34407
34478
  const h = o.pos - d.length, u = Array.from(d.matchAll(I)).pop();
34408
34479
  if (!u || u.input === void 0 || u.index === void 0)
34409
- return null;
34480
+ return;
34410
34481
  const B = u.input.slice(
34411
34482
  Math.max(0, u.index - 1),
34412
34483
  u.index
@@ -34414,196 +34485,222 @@ function createDefaultMatcher(t = {}) {
34414
34485
  `^[${n?.join("")}\0]?$`
34415
34486
  ).test(B);
34416
34487
  if (n !== null && !E)
34417
- return null;
34488
+ return;
34418
34489
  const f = h + u.index;
34419
34490
  let Q = f + u[0].length;
34420
- return s && l.test(d.slice(Q - 1, Q + 1)) && (u[0] += " ", Q += 1), f < o.pos && Q >= o.pos ? {
34421
- range: {
34422
- from: f,
34423
- to: Q
34424
- },
34425
- query: u[0],
34426
- text: u[0]
34427
- } : null;
34491
+ if (s && l.test(d.slice(Q - 1, Q + 1)) && (u[0] += " ", Q += 1), f < o.pos && Q >= o.pos)
34492
+ return {
34493
+ range: {
34494
+ from: f,
34495
+ to: Q
34496
+ },
34497
+ query: u[0]
34498
+ };
34428
34499
  };
34429
34500
  }
34430
- const CSS_PREFIX = "kb-autocomplete";
34431
- class DefaultRenderer extends EventTarget {
34432
- constructor(e) {
34433
- super(), this.editor = e, this.items = [], this.pos = -1, this.command = () => {
34434
- };
34435
- }
34436
- onStart(e) {
34437
- this.command = e.command, this.wrapper && this.wrapper.parentElement?.removeChild(this.wrapper);
34438
- const A = "root" in this.editor.view ? this.editor.view.root : document || document;
34439
- this.wrapper = document.createElement("ul"), this.wrapper.classList.add(CSS_PREFIX + "__wrapper"), A.appendChild(this.wrapper), this.items.splice(0, this.items.length, ...e.items), this.recreateList(e);
34440
- }
34441
- onUpdate(e) {
34442
- this.command = e.command, this.items.splice(0, this.items.length, ...e.items), this.recreateList(e);
34443
- }
34444
- onExit() {
34445
- this.wrapper && (this.wrapper.parentNode?.removeChild(this.wrapper), this.wrapper = void 0), this.dispatchEvent(new Event("close")), this.pos = -1;
34446
- }
34447
- onKeyDown(e) {
34448
- return !this.wrapper || this.items.length === 0 ? !1 : e.event.key === "Escape" && this.wrapper ? (this.wrapper.parentNode?.removeChild(this.wrapper), this.wrapper = void 0, !0) : e.event.key === "ArrowUp" ? (this.pos > -1 && (this.pos = this.pos - 1, this.recreateList()), !0) : e.event.key === "ArrowDown" ? (this.pos < this.items.length - 1 && (this.pos++, this.recreateList()), !0) : e.event.key === "Enter" && this.pos > -1 && this.pos < this.items.length ? (this.command(this.items[this.pos]), this.items.splice(0, this.items.length), this.onExit(), !0) : !1;
34449
- }
34450
- createListItem(e, A) {
34451
- const n = document.createElement("li");
34452
- return A === this.pos && n.classList.add("active"), n.innerText = "" + e, n.style.cursor = "pointer", n.addEventListener("click", () => {
34453
- this.command(e);
34454
- }), n;
34455
- }
34456
- recreateList(e) {
34457
- if (!this.wrapper)
34458
- return;
34459
- this.wrapper.innerHTML = "";
34460
- for (let n = 0; n < this.items.length; n++) {
34461
- const r = this.items[n];
34462
- this.wrapper.appendChild(this.createListItem(r, n));
34463
- }
34464
- this.items.length === 0 ? this.wrapper.style.display = "none" : this.wrapper.style.display = "";
34465
- const A = e?.clientRect?.();
34466
- if (A?.height) {
34467
- const n = A.left, r = A.bottom;
34468
- this.wrapper.style.left = n + "px", this.wrapper.style.top = r + "px";
34469
- }
34501
+ const AutocompletePluginKey = new PluginKey(
34502
+ "autocomplete"
34503
+ );
34504
+ function matchSource(t, e) {
34505
+ let A;
34506
+ const n = t.$anchor.parent;
34507
+ for (const r of e) {
34508
+ if (!r.matchers)
34509
+ continue;
34510
+ const o = r.matchers;
34511
+ if (!["code_blocxk"].includes(n.type.name))
34512
+ for (const s of o) {
34513
+ const a = s(t.$from);
34514
+ if (a)
34515
+ return A = {
34516
+ source: r,
34517
+ match: a
34518
+ }, A;
34519
+ }
34470
34520
  }
34471
34521
  }
34472
- const AutocompletePluginKey = new PluginKey("autocomplete");
34473
34522
  class AutocompletePlugin extends Plugin {
34474
34523
  constructor(e, A) {
34475
- let n;
34476
- const r = e.renderer || new DefaultRenderer(A);
34477
34524
  super({
34478
34525
  key: AutocompletePluginKey,
34526
+ state: {
34527
+ // Initialize the plugin's internal state.
34528
+ init() {
34529
+ return {
34530
+ autocompleteSources: [],
34531
+ manual: !1,
34532
+ active: void 0,
34533
+ composing: !1
34534
+ };
34535
+ },
34536
+ // Apply changes to the plugin state from a view transaction.
34537
+ apply(n, r, o, s) {
34538
+ const a = n.getMeta(
34539
+ AutocompletePluginKey
34540
+ ), l = { ...r };
34541
+ !a && n.isGeneric;
34542
+ const { editable: g, composing: c } = A.view, { selection: I } = n, d = () => {
34543
+ l.active && (l.active.renderer.destroy(), l.active = void 0), l.decorationId = void 0;
34544
+ }, h = (B) => {
34545
+ if (B && (!B.source.allow || B.source.allow({
34546
+ range: B.match.range,
34547
+ isActive: !!l.active
34548
+ }))) {
34549
+ const { source: E, match: f } = B;
34550
+ console.info("Trigger matcher autocomplete", f);
34551
+ let Q = l.active?.renderer;
34552
+ l.active?.source !== E && Q && (Q.destroy(), Q = void 0), Q || (Q = new DefaultRenderer(A), Q.addEventListener("close", () => {
34553
+ const m = A.state.tr.setMeta(AutocompletePluginKey, {
34554
+ deactivate: !0
34555
+ });
34556
+ console.info("Manual autocomplete deactivate"), A.view.dispatch(m);
34557
+ })), l.active = {
34558
+ renderer: Q,
34559
+ source: E,
34560
+ match: f
34561
+ };
34562
+ const p = `id_${Math.floor(Math.random() * 4294967295)}`;
34563
+ l.decorationId = l.decorationId || p;
34564
+ } else
34565
+ d();
34566
+ };
34567
+ if (a?.addAutocompleteSource) {
34568
+ const B = {
34569
+ ...a.addAutocompleteSource.autocompleteSource
34570
+ };
34571
+ return !B.matchers && B.triggerKeys?.length === 1 && (B.matchers = [
34572
+ createDefaultMatcher({ char: B.triggerKeys[0] })
34573
+ ]), l.autocompleteSources.push(
34574
+ B
34575
+ ), l;
34576
+ }
34577
+ if (a?.activate) {
34578
+ console.info("Trigger manual autocomplete");
34579
+ const B = matchSource(I, l.autocompleteSources);
34580
+ return h(B), l.manual = !0, l;
34581
+ }
34582
+ if (a?.deactivate)
34583
+ return console.info("Deactivate autocomplete"), d(), l;
34584
+ if (!g || !I.empty && !A.view.composing)
34585
+ return d(), l;
34586
+ if (l.active && !c && !l.composing && (I.from < l.active.match.range.from || I.from > l.active.match.range.to) && d(), l.composing = c, n.getMeta("isCommand"))
34587
+ return l;
34588
+ const u = matchSource(I, l.autocompleteSources);
34589
+ return h(u), l.manual = !1, l;
34590
+ }
34591
+ },
34479
34592
  view() {
34480
34593
  return {
34481
- update: async (o, s) => {
34482
- const a = this.key?.getState(s), l = this.key?.getState(o.state), g = a.active && l.active && a.range.from !== l.range.from, c = !a.active && l.active, I = a.active && !l.active, d = !c && !I && a.query !== l.query, h = c || g && d, u = d || g;
34483
- let B = I || g && d;
34484
- if (!h && !u && !B)
34594
+ update: async (n, r) => {
34595
+ const o = this.key?.getState(r), s = this.key?.getState(n.state), a = o.active && s.active && o.active.match.range.from !== s.active.match.range.from, l = !o.active?.match && s.active?.match, g = o.active?.match && !s.active?.match, c = !l && !g && o.active?.match.query !== s.active?.match.query;
34596
+ if (g && o.active) {
34597
+ o.active.renderer.destroy();
34485
34598
  return;
34486
- const E = B && !h ? a : l, f = o.dom.querySelector(
34487
- `[data-decoration-id="${E.decorationId}"]`
34488
- );
34489
- if (n = {
34490
- range: E.range,
34491
- query: E.query,
34492
- text: E.text,
34493
- items: [],
34494
- command: (Q) => e.onSelect ? e.onSelect(Q, E.range) : () => {
34495
- },
34496
- decorationNode: f,
34497
- // virtual node for popper.js or tippy.js
34498
- // this can be used for building popups without a DOM node
34499
- clientRect: f ? () => {
34500
- const { decorationId: Q } = this.key?.getState(A.state);
34501
- return o.dom.querySelector(
34502
- `[data-decoration-id="${Q}"]`
34503
- )?.getBoundingClientRect() || null;
34504
- } : null
34505
- }, h && r?.onBeforeStart?.(n), u && r?.onBeforeUpdate?.(n), (u || h) && e.getItems) {
34599
+ }
34600
+ if (s.active && (o.active?.match.query !== s.active.match.query || a)) {
34601
+ n.dom.querySelector(
34602
+ `[data-decoration-id="${s.decorationId}"]`
34603
+ );
34604
+ const B = s.active.source.onSelect, E = s.active.match.range, f = {
34605
+ match: s.active.match,
34606
+ items: [],
34607
+ command: (Q) => B(Q, E),
34608
+ // decorationNode,
34609
+ // virtual node for popper.js or tippy.js
34610
+ // this can be used for building popups without a DOM node
34611
+ clientRect: () => {
34612
+ const { decorationId: Q } = s;
34613
+ return n.dom.querySelector(
34614
+ `[data-decoration-id="${Q}"]`
34615
+ )?.getBoundingClientRect() || null;
34616
+ }
34617
+ };
34618
+ s.active.renderer.onUpdate(f);
34506
34619
  try {
34507
- const Q = { state: E, range: E.range, isActive: !0 };
34508
- n.items = await e.getItems(E.query, Q);
34620
+ const Q = {
34621
+ range: s.active.match.range,
34622
+ isActive: !!s.active
34623
+ }, p = await s.active.source.getItems(
34624
+ s.active.match.query,
34625
+ Q
34626
+ );
34627
+ s.active.renderer.onUpdate({ ...f, items: p });
34509
34628
  } catch (Q) {
34510
34629
  if (Q.isLSP)
34511
- n.items = [], console.error(
34630
+ console.error(
34512
34631
  "LSP error config.getItems()",
34513
34632
  Q.message,
34514
- e.getItems
34633
+ s.active.source.getItems
34515
34634
  );
34516
34635
  else
34517
34636
  throw Q;
34518
34637
  }
34519
- n.items.length === 0 && (B = !0);
34520
34638
  }
34521
- B && r?.onExit?.(n), u && r?.onUpdate?.(n), h && r?.onStart?.(n);
34522
34639
  },
34523
34640
  destroy: () => {
34524
- n && r?.onExit?.(n);
34641
+ const n = AutocompletePluginKey.getState(A.state);
34642
+ if (!n)
34643
+ return;
34644
+ const { active: r } = n;
34645
+ r && r.renderer.destroy();
34525
34646
  }
34526
34647
  };
34527
34648
  },
34528
- state: {
34529
- // Initialize the plugin's internal state.
34530
- init() {
34531
- return {
34532
- manual: !1,
34533
- active: !1,
34534
- range: {
34535
- from: 0,
34536
- to: 0
34537
- },
34538
- query: null,
34539
- text: null,
34540
- composing: !1
34541
- };
34542
- },
34543
- // Apply changes to the plugin state from a view transaction.
34544
- apply(o, s, a, l) {
34545
- const { composing: c } = A.view, { selection: I } = o, { empty: d, from: h } = I, u = { ...s }, B = o.getMeta(AutocompletePluginKey);
34546
- if (!B && !o.isGeneric)
34547
- return u;
34548
- if (B?.type === "deactivate")
34549
- return console.info("Deactivate autocomplete"), u.active = !1, u;
34550
- if (B?.type === "activate")
34551
- return console.info("Trigger manual autocomplete"), u.range = { from: I.from, to: I.to }, u.active = !0, u.manual = !0, u.query = null, u;
34552
- u.composing = c;
34553
- const E = I.$anchor.parent;
34554
- if (!["code_block"].includes(E?.type.name) && (d || A.view.composing)) {
34555
- (h < s.range.from || h > s.range.to) && !c && !s.composing && (u.active = !1);
34556
- const f = e.matchers || [createDefaultMatcher()];
34557
- let Q;
34558
- for (const m of f)
34559
- if (Q = m(I.$from), Q)
34560
- break;
34561
- const p = `id_${Math.floor(Math.random() * 4294967295)}`;
34562
- Q && (!e.allow || e.allow({
34563
- state: l,
34564
- range: Q.range,
34565
- isActive: s.active
34566
- })) ? (console.info("Trigger matcher autocomplete", Q), u.active = !0, u.decorationId = s.decorationId ? s.decorationId : p, u.range = Q.range, u.query = Q.query, u.text = Q.text) : u.active = !1;
34567
- } else
34568
- u.active = !1;
34569
- return u.manual = !1, u.active || (u.decorationId = null, u.range = { from: 0, to: 0 }, u.query = null, u.text = null), u;
34570
- }
34571
- },
34572
34649
  props: {
34573
34650
  // Call the keydown hook if suggestion is active.
34574
- handleKeyDown(o, s) {
34575
- const { active: a, range: l } = this.getState(o.state);
34576
- if (s.key === " " && s.ctrlKey) {
34577
- const g = o.state.tr.setMeta(AutocompletePluginKey, {
34578
- type: "activate"
34579
- });
34580
- return console.info("Manual autocomplete key"), o.dispatch(g), !0;
34651
+ handleKeyDown(n, r) {
34652
+ const o = this.getState(n.state), { autocompleteSources: s } = o;
34653
+ for (const a of s) {
34654
+ const l = [...a.triggerKeys || []];
34655
+ for (const g of l) {
34656
+ let c = g.toLowerCase();
34657
+ if (c.startsWith("ctrl+")) {
34658
+ if (!r.ctrlKey)
34659
+ continue;
34660
+ c = c.substring(5);
34661
+ }
34662
+ if (c === r.key) {
34663
+ const I = n.state.tr.setMeta(AutocompletePluginKey, {
34664
+ activate: !0
34665
+ });
34666
+ return console.info("Manual autocomplete key " + g), n.dispatch(I), !1;
34667
+ }
34668
+ }
34581
34669
  }
34582
- return a && r?.onKeyDown?.({ view: o, event: s, range: l }) || !1;
34670
+ return !1;
34583
34671
  },
34584
34672
  // Setup decorator on the currently active suggestion.
34585
- decorations(o) {
34586
- const { active: s, range: a, decorationId: l } = this.getState(o);
34587
- if (!s)
34673
+ decorations(n) {
34674
+ const { active: r, decorationId: o } = this.getState(n) || {};
34675
+ if (!r || !o)
34588
34676
  return null;
34589
- const g = document.createElement("span");
34590
- return g.className = e.decorationClass || "kb-autocomplete--decor", g.setAttribute("data-decoration-id", l), DecorationSet.create(o.doc, [
34591
- Decoration.widget(a.from, g)
34677
+ const s = document.createElement("span");
34678
+ return s.className = e.decorationClass || "kb-autocomplete--decor", s.setAttribute("data-decoration-id", o), DecorationSet.create(n.doc, [
34679
+ Decoration.widget(r.match.range.from, s, {
34680
+ class: s.className,
34681
+ decorationId: o,
34682
+ refresh: () => r.renderer.refresh()
34683
+ })
34592
34684
  ]);
34593
34685
  }
34594
34686
  }
34595
- }), r.addEventListener("close", () => {
34596
- const o = A.state.tr.setMeta(AutocompletePluginKey, {
34597
- type: "deactivate"
34598
- });
34599
- console.info("Manual autocomplete deactivate"), A.view.dispatch(o);
34600
34687
  });
34601
34688
  }
34602
34689
  }
34603
34690
  class ExtensionAutocomplete extends Extension {
34604
- constructor(e) {
34691
+ constructor(e = {}) {
34605
34692
  super(e), this.config = e, this.name = "autocomplete";
34606
34693
  }
34694
+ getCommandFactories() {
34695
+ return {
34696
+ addAutocompleteSource: (A) => (n, r) => {
34697
+ const o = n.tr;
34698
+ return o.setMeta(AutocompletePluginKey, {
34699
+ addAutocompleteSource: { autocompleteSource: A }
34700
+ }), r && r(o), !0;
34701
+ }
34702
+ };
34703
+ }
34607
34704
  getProseMirrorPlugins() {
34608
34705
  return [
34609
34706
  new AutocompletePlugin(this.config, this.editor)