mindcache 3.3.2 → 3.4.0

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
@@ -520,14 +520,30 @@ var MindCache = class {
520
520
  break;
521
521
  }
522
522
  }
523
+ } else {
524
+ let current = event.target;
525
+ while (current && current.parent) {
526
+ if (current.parent.parent === this.rootMap) {
527
+ for (const [key, val] of this.rootMap) {
528
+ if (val === current.parent) {
529
+ keysAffected.add(key);
530
+ break;
531
+ }
532
+ }
533
+ break;
534
+ }
535
+ current = current.parent;
536
+ }
523
537
  }
524
538
  });
525
539
  keysAffected.forEach((key) => {
526
540
  const entryMap = this.rootMap.get(key);
527
541
  if (entryMap) {
528
542
  const value = entryMap.get("value");
543
+ const attrs = entryMap.get("attributes");
544
+ const resolvedValue = attrs?.type === "document" && value instanceof Y.Text ? value.toString() : value;
529
545
  if (this.listeners[key]) {
530
- this.listeners[key].forEach((l) => l(value));
546
+ this.listeners[key].forEach((l) => l(resolvedValue));
531
547
  }
532
548
  } else {
533
549
  if (this.listeners[key]) {
@@ -657,12 +673,19 @@ var MindCache = class {
657
673
  if (event.target === this.rootMap) {
658
674
  const mapEvent = event;
659
675
  mapEvent.keysChanged.forEach((key) => keysAffected.add(key));
660
- } else if (event.target.parent === this.rootMap) {
661
- for (const [key, val] of this.rootMap) {
662
- if (val === event.target) {
663
- keysAffected.add(key);
676
+ } else {
677
+ let current = event.target;
678
+ while (current && current.parent) {
679
+ if (current.parent === this.rootMap) {
680
+ for (const [key, val] of this.rootMap) {
681
+ if (val === current) {
682
+ keysAffected.add(key);
683
+ break;
684
+ }
685
+ }
664
686
  break;
665
687
  }
688
+ current = current.parent;
666
689
  }
667
690
  }
668
691
  });
@@ -1059,11 +1082,15 @@ var MindCache = class {
1059
1082
  const existingAttrs = existingEntry.get("attributes");
1060
1083
  if (existingAttrs?.type === "document") {
1061
1084
  if (typeof value === "string") {
1062
- this.replace_document_text(key, value);
1085
+ this._replaceDocumentText(key, value);
1063
1086
  }
1064
1087
  return;
1065
1088
  }
1066
1089
  }
1090
+ if (!existingEntry && attributes?.type === "document") {
1091
+ this.set_document(key, typeof value === "string" ? value : "", attributes);
1092
+ return;
1093
+ }
1067
1094
  let entryMap = this.rootMap.get(key);
1068
1095
  const isNewEntry = !entryMap;
1069
1096
  if (isNewEntry) {
@@ -1804,14 +1831,6 @@ var MindCache = class {
1804
1831
  }
1805
1832
  return void 0;
1806
1833
  }
1807
- /**
1808
- * Get plain text content of a document key.
1809
- * For collaborative editing, use get_document() and bind to an editor.
1810
- */
1811
- get_document_text(key) {
1812
- const yText = this.get_document(key);
1813
- return yText?.toString();
1814
- }
1815
1834
  /**
1816
1835
  * Insert text at a position in a document key.
1817
1836
  */
@@ -1831,15 +1850,11 @@ var MindCache = class {
1831
1850
  }
1832
1851
  }
1833
1852
  /**
1834
- * Replace all text in a document key.
1853
+ * Replace all text in a document key (private - use set_value for public API).
1835
1854
  * Uses diff-based updates when changes are < diffThreshold (default 80%).
1836
1855
  * This preserves concurrent edits and provides better undo granularity.
1837
- *
1838
- * @param key - The document key
1839
- * @param newText - The new text content
1840
- * @param diffThreshold - Percentage (0-1) of change above which full replace is used (default: 0.8)
1841
1856
  */
1842
- replace_document_text(key, newText, diffThreshold = 0.8) {
1857
+ _replaceDocumentText(key, newText, diffThreshold = 0.8) {
1843
1858
  const yText = this.get_document(key);
1844
1859
  if (!yText) {
1845
1860
  return;
@@ -1945,7 +1960,7 @@ var MindCache = class {
1945
1960
  },
1946
1961
  execute: async ({ value }) => {
1947
1962
  if (isDocument) {
1948
- this.replace_document_text(key, value);
1963
+ this._replaceDocumentText(key, value);
1949
1964
  } else {
1950
1965
  this.set_value(key, value);
1951
1966
  }
@@ -2101,7 +2116,7 @@ var MindCache = class {
2101
2116
  switch (action) {
2102
2117
  case "write":
2103
2118
  if (isDocument) {
2104
- this.replace_document_text(key, value);
2119
+ this._replaceDocumentText(key, value);
2105
2120
  } else {
2106
2121
  this.set_value(key, value);
2107
2122
  }