ckeditor5-livewire 1.1.0 → 1.1.2

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/index.mjs CHANGED
@@ -213,7 +213,7 @@ class H {
213
213
  this.items.size === 1 && t !== null && this.register(null, e);
214
214
  }
215
215
  }
216
- function D(i, t) {
216
+ function O(i, t) {
217
217
  let e = null;
218
218
  return (...r) => {
219
219
  e && clearTimeout(e), e = setTimeout(() => {
@@ -232,7 +232,7 @@ function S(i, t) {
232
232
  const e = Object.entries(i).map(([r, a]) => [r, t(a, r)]);
233
233
  return Object.fromEntries(e);
234
234
  }
235
- function v(i, t) {
235
+ function P(i, t) {
236
236
  if (i === t)
237
237
  return !0;
238
238
  const e = Object.keys(i), r = Object.keys(t);
@@ -294,7 +294,7 @@ function J(i) {
294
294
  function Q(i) {
295
295
  return i.model.document.getRootNames().reduce((e, r) => (e[r] = i.getData({ rootName: r }), e), /* @__PURE__ */ Object.create({}));
296
296
  }
297
- function P(i) {
297
+ function v(i) {
298
298
  return ["inline", "classic", "balloon", "decoupled"].includes(i);
299
299
  }
300
300
  function Z(i) {
@@ -379,7 +379,7 @@ class $ {
379
379
  return this.plugins.has(t);
380
380
  }
381
381
  }
382
- async function V(i) {
382
+ async function F(i) {
383
383
  const t = await import("ckeditor5");
384
384
  let e = null;
385
385
  const r = i.map(async (a) => {
@@ -405,7 +405,7 @@ async function V(i) {
405
405
  hasPremium: !!e
406
406
  };
407
407
  }
408
- async function q(i, t) {
408
+ async function V(i, t) {
409
409
  const e = [i.ui, i.content];
410
410
  return await Promise.all(
411
411
  [
@@ -723,12 +723,12 @@ async function et(i, t) {
723
723
  return console.error(`Failed to load translation for ${i}/${t}:`, e), null;
724
724
  }
725
725
  }
726
- function N(i) {
726
+ function q(i) {
727
727
  return S(i, (t) => ({
728
728
  dictionary: t
729
729
  }));
730
730
  }
731
- function L(i) {
731
+ function M(i) {
732
732
  return window.Livewire.all().filter(({ name: t, canonical: e }) => t === "ckeditor5-editable" && e.editorId === i).reduce((t, { canonical: e, el: r }) => ({
733
733
  ...t,
734
734
  [e.rootName]: {
@@ -737,38 +737,38 @@ function L(i) {
737
737
  }
738
738
  }), /* @__PURE__ */ Object.create({}));
739
739
  }
740
- function x(i, t) {
740
+ function T(i, t) {
741
741
  if (t === "decoupled") {
742
- const { element: r } = F(i);
742
+ const { element: r } = N(i);
743
743
  return r;
744
744
  }
745
- if (P(t))
745
+ if (v(t))
746
746
  return document.getElementById(`${i}_editor`);
747
- const e = L(i);
747
+ const e = M(i);
748
748
  return S(e, ({ element: r }) => r);
749
749
  }
750
750
  function A(i, t) {
751
751
  if (t === "decoupled") {
752
- const { content: a } = F(i);
752
+ const { content: a } = N(i);
753
753
  if (typeof a == "string")
754
754
  return {
755
755
  main: a
756
756
  };
757
757
  }
758
- const e = L(i), r = S(e, ({ content: a }) => a);
758
+ const e = M(i), r = S(e, ({ content: a }) => a);
759
759
  return G(r, (a) => typeof a == "string");
760
760
  }
761
- function F(i) {
762
- const t = L(i).main;
761
+ function N(i) {
762
+ const t = M(i).main;
763
763
  if (!t)
764
764
  throw new Error(`No "main" editable found for editor with ID "${i}".`);
765
765
  return t;
766
766
  }
767
- function T(i) {
767
+ function x(i) {
768
768
  if (!i || typeof i != "object")
769
769
  return i;
770
770
  if (Array.isArray(i))
771
- return i.map((r) => T(r));
771
+ return i.map((r) => x(r));
772
772
  const t = i;
773
773
  if (t.$element && typeof t.$element == "string") {
774
774
  const r = document.querySelector(t.$element);
@@ -776,7 +776,7 @@ function T(i) {
776
776
  }
777
777
  const e = /* @__PURE__ */ Object.create(null);
778
778
  for (const [r, a] of Object.entries(i))
779
- e[r] = T(a);
779
+ e[r] = x(a);
780
780
  return e;
781
781
  }
782
782
  function rt(i, t) {
@@ -785,12 +785,12 @@ function rt(i, t) {
785
785
  r.setStyle("height", `${t}px`, e.view.document.getRoot());
786
786
  });
787
787
  }
788
- const O = Symbol.for("elixir-editor-watchdog");
788
+ const D = Symbol.for("elixir-editor-watchdog");
789
789
  async function it(i) {
790
790
  const { EditorWatchdog: t } = await import("ckeditor5"), e = new t(i);
791
791
  return e.setCreator(async (...r) => {
792
792
  const a = await i.create(...r);
793
- return a[O] = e, a;
793
+ return a[D] = e, a;
794
794
  }), {
795
795
  watchdog: e,
796
796
  Constructor: {
@@ -799,7 +799,7 @@ async function it(i) {
799
799
  };
800
800
  }
801
801
  function at(i) {
802
- return O in i ? i[O] : null;
802
+ return D in i ? i[D] : null;
803
803
  }
804
804
  class y extends H {
805
805
  static the = new y();
@@ -813,9 +813,9 @@ class nt extends C {
813
813
  * Mounts the context component.
814
814
  */
815
815
  async mounted() {
816
- const { contextId: t, language: e, context: r } = this.canonical, { customTranslations: a, watchdogConfig: n, config: { plugins: s, ...o } } = r, { loadedPlugins: c, hasPremium: u } = await V(s ?? []), h = [
817
- ...await q(e, u),
818
- N(a || {})
816
+ const { contextId: t, language: e, context: r } = this.canonical, { customTranslations: a, watchdogConfig: n, config: { plugins: s, ...o } } = r, { loadedPlugins: c, hasPremium: u } = await F(s ?? []), h = [
817
+ ...await V(e, u),
818
+ q(a || {})
819
819
  ].filter((d) => !j(d));
820
820
  this.contextPromise = (async () => {
821
821
  const { ContextWatchdog: d, Context: g } = await import("ckeditor5"), p = new d(g, {
@@ -885,7 +885,7 @@ class st extends C {
885
885
  const d = s.getData({ rootName: e });
886
886
  n && (n.value = d), this.$wire.set("content", d);
887
887
  };
888
- return s.model.document.on("change:data", D(a, f)), f(), s;
888
+ return s.model.document.on("change:data", O(a, f)), f(), s;
889
889
  });
890
890
  }
891
891
  /**
@@ -926,17 +926,32 @@ async function ot({
926
926
  * Initializes the plugin.
927
927
  */
928
928
  init() {
929
- this.setupTypingContentPush(), this.setupFocusableEventPush(), this.setupContentServerSync(), this.setupLivewireEventListeners();
929
+ this.setupTypingContentPush(), this.setupFocusableEventPush(), this.setupContentServerSync();
930
930
  }
931
931
  /**
932
932
  * Setups the content sync from Livewire to the editor.
933
933
  */
934
934
  setupContentServerSync() {
935
- this.editor.on("afterCommitSynced", () => {
936
- if (!Z(t.element))
935
+ const { editor: a } = this, { model: n, ui: { focusTracker: s } } = a;
936
+ let o = null;
937
+ a.on("afterCommitSynced", () => {
938
+ const { content: c } = t.canonical, u = this.getEditorRootsValues();
939
+ if (Z(t.element)) {
940
+ if (s.isFocused) {
941
+ P(c, u) || (o = c);
942
+ return;
943
+ }
944
+ P(c, u) || a.setData(c);
945
+ }
946
+ }), Livewire.on("set-editor-content", ({ editorId: c, content: u }) => {
947
+ if (c !== t.canonical.editorId)
937
948
  return;
938
- const { content: a } = t.canonical, n = this.getEditorRootsValues();
939
- v(a, n) || this.editor.setData(a);
949
+ const l = this.getEditorRootsValues();
950
+ P(l, u) || a.setData(u);
951
+ }), n.document.on("change:data", () => {
952
+ o = null;
953
+ }), s.on("change:isFocused", () => {
954
+ !s.isFocused && o !== null && (a.setData(o), o = null);
940
955
  });
941
956
  }
942
957
  /**
@@ -945,12 +960,12 @@ async function ot({
945
960
  setupTypingContentPush() {
946
961
  const { model: a } = this.editor, { $wire: n } = t, s = () => {
947
962
  const o = this.getEditorRootsValues();
948
- v(o, t.canonical.content) || (n.set("content", o), n.dispatch("editor-content-changed", {
963
+ P(o, t.canonical.content) || (n.set("content", o), n.dispatch("editor-content-changed", {
949
964
  editorId: t.canonical.editorId,
950
965
  content: o
951
966
  }));
952
967
  };
953
- a.document.on("change:data", D(i, s)), this.editor.once("ready", s);
968
+ a.document.on("change:data", O(i, s)), this.editor.once("ready", s);
954
969
  }
955
970
  /**
956
971
  * Setups the event push for the editor.
@@ -958,7 +973,7 @@ async function ot({
958
973
  setupFocusableEventPush() {
959
974
  const { ui: a } = this.editor, { $wire: n } = t, s = () => {
960
975
  const o = this.getEditorRootsValues();
961
- n.set("focused", a.focusTracker.isFocused), v(o, t.canonical.content) || n.set("content", o);
976
+ n.set("focused", a.focusTracker.isFocused), P(o, t.canonical.content) || n.set("content", o);
962
977
  };
963
978
  a.focusTracker.on("change:isFocused", s);
964
979
  }
@@ -968,17 +983,6 @@ async function ot({
968
983
  getEditorRootsValues() {
969
984
  return Q(this.editor);
970
985
  }
971
- /**
972
- * Setups Livewire event listeners for external content updates.
973
- */
974
- setupLivewireEventListeners() {
975
- Livewire.on("set-editor-content", ({ editorId: a, content: n }) => {
976
- if (a !== t.canonical.editorId)
977
- return;
978
- const s = this.getEditorRootsValues();
979
- v(s, n) || this.editor.setData(n);
980
- });
981
- }
982
986
  };
983
987
  }
984
988
  async function ct(i) {
@@ -1003,7 +1007,7 @@ async function ct(i) {
1003
1007
  */
1004
1008
  afterInit() {
1005
1009
  const { editor: r } = this, n = r.sourceElement.id.replace(/_editor$/, "");
1006
- this.input = document.getElementById(`${n}_input`), this.input && (r.model.document.on("change:data", D(i, () => this.sync())), r.once("ready", this.sync), this.form = this.input.closest("form"), this.form?.addEventListener("submit", this.sync));
1010
+ this.input = document.getElementById(`${n}_input`), this.input && (r.model.document.on("change:data", O(i, () => this.sync())), r.once("ready", this.sync), this.form = this.input.closest("form"), this.form?.addEventListener("submit", this.sync));
1007
1011
  }
1008
1012
  /**
1009
1013
  * Synchronizes the editor's content with the input field.
@@ -1091,7 +1095,7 @@ class ut extends C {
1091
1095
  this.editorPromise = Promise.resolve(k), w.the.register(e, k);
1092
1096
  });
1093
1097
  }
1094
- const { loadedPlugins: E, hasPremium: U } = await V(f);
1098
+ const { loadedPlugins: E, hasPremium: U } = await F(f);
1095
1099
  E.push(
1096
1100
  await ot(
1097
1101
  {
@@ -1099,35 +1103,35 @@ class ut extends C {
1099
1103
  component: this
1100
1104
  }
1101
1105
  )
1102
- ), P(l) && E.push(
1106
+ ), v(l) && E.push(
1103
1107
  await ct(n)
1104
1108
  );
1105
- const M = [
1106
- ...await q(s, U),
1107
- N(u || {})
1109
+ const z = [
1110
+ ...await V(s, U),
1111
+ q(u || {})
1108
1112
  ].filter((m) => !j(m));
1109
1113
  let b = {
1110
1114
  ...c,
1111
1115
  ...A(e, l)
1112
1116
  };
1113
- P(l) && (b = b.main || "");
1114
- const z = await (async () => {
1115
- let m = x(e, l);
1117
+ v(l) && (b = b.main || "");
1118
+ const R = await (async () => {
1119
+ let m = T(e, l);
1116
1120
  if (mt(m, l)) {
1117
- const R = Object.keys(b);
1118
- B(m, R) || (m = await lt(e, l, R), b = {
1121
+ const L = Object.keys(b);
1122
+ B(m, L) || (m = await lt(e, l, L), b = {
1119
1123
  ...c,
1120
1124
  ...A(e, l)
1121
1125
  });
1122
1126
  }
1123
1127
  const k = {
1124
- ...T(d),
1128
+ ...x(d),
1125
1129
  initialData: b,
1126
1130
  licenseKey: h,
1127
1131
  plugins: E,
1128
1132
  language: s,
1129
- ...M.length && {
1130
- translations: M
1133
+ ...z.length && {
1134
+ translations: z
1131
1135
  }
1132
1136
  };
1133
1137
  return !p || !(m instanceof HTMLElement) ? g.create(m, k) : (await X({
@@ -1137,7 +1141,7 @@ class ut extends C {
1137
1141
  config: k
1138
1142
  })).editor;
1139
1143
  })();
1140
- return P(l) && a && rt(z, a), z;
1144
+ return v(l) && a && rt(R, a), R;
1141
1145
  }
1142
1146
  }
1143
1147
  function B(i, t) {
@@ -1146,7 +1150,7 @@ function B(i, t) {
1146
1150
  async function lt(i, t, e) {
1147
1151
  return await Y(
1148
1152
  () => {
1149
- const r = x(i, t);
1153
+ const r = T(i, t);
1150
1154
  if (!B(r, e))
1151
1155
  throw new Error(
1152
1156
  `It looks like not all required root elements are present yet.
@@ -1157,10 +1161,10 @@ Missing roots: ${e.filter((a) => !r[a]).join(", ")}.`
1157
1161
  return !0;
1158
1162
  },
1159
1163
  { timeOutAfter: 2e3, retryAfter: 100 }
1160
- ), x(i, t);
1164
+ ), T(i, t);
1161
1165
  }
1162
1166
  function mt(i, t) {
1163
- return !P(t) && typeof i == "object" && !(i instanceof HTMLElement);
1167
+ return !v(t) && typeof i == "object" && !(i instanceof HTMLElement);
1164
1168
  }
1165
1169
  class dt extends C {
1166
1170
  /**