mindcache 3.3.2 → 3.4.1

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.
@@ -45,7 +45,7 @@ var init_IndexedDBAdapter = __esm({
45
45
  await this.initDB();
46
46
  await this.load();
47
47
  const listener = () => {
48
- if (this.mindcache && !this.mindcache.isRemoteUpdate()) {
48
+ if (this.mindcache) {
49
49
  this.scheduleSave();
50
50
  }
51
51
  };
@@ -449,7 +449,6 @@ var MindCache = class {
449
449
  version = "3.3.2";
450
450
  // Internal flag to prevent sync loops when receiving remote updates
451
451
  // (Less critical with Yjs but kept for API compat)
452
- _isRemoteUpdate = false;
453
452
  normalizeSystemTags(tags) {
454
453
  const normalized = [];
455
454
  let hasSystemPrompt = false;
@@ -519,14 +518,30 @@ var MindCache = class {
519
518
  break;
520
519
  }
521
520
  }
521
+ } else {
522
+ let current = event.target;
523
+ while (current && current.parent) {
524
+ if (current.parent.parent === this.rootMap) {
525
+ for (const [key, val] of this.rootMap) {
526
+ if (val === current.parent) {
527
+ keysAffected.add(key);
528
+ break;
529
+ }
530
+ }
531
+ break;
532
+ }
533
+ current = current.parent;
534
+ }
522
535
  }
523
536
  });
524
537
  keysAffected.forEach((key) => {
525
538
  const entryMap = this.rootMap.get(key);
526
539
  if (entryMap) {
527
540
  const value = entryMap.get("value");
541
+ const attrs = entryMap.get("attributes");
542
+ const resolvedValue = attrs?.type === "document" && value instanceof Y.Text ? value.toString() : value;
528
543
  if (this.listeners[key]) {
529
- this.listeners[key].forEach((l) => l(value));
544
+ this.listeners[key].forEach((l) => l(resolvedValue));
530
545
  }
531
546
  } else {
532
547
  if (this.listeners[key]) {
@@ -656,12 +671,19 @@ var MindCache = class {
656
671
  if (event.target === this.rootMap) {
657
672
  const mapEvent = event;
658
673
  mapEvent.keysChanged.forEach((key) => keysAffected.add(key));
659
- } else if (event.target.parent === this.rootMap) {
660
- for (const [key, val] of this.rootMap) {
661
- if (val === event.target) {
662
- keysAffected.add(key);
674
+ } else {
675
+ let current = event.target;
676
+ while (current && current.parent) {
677
+ if (current.parent === this.rootMap) {
678
+ for (const [key, val] of this.rootMap) {
679
+ if (val === current) {
680
+ keysAffected.add(key);
681
+ break;
682
+ }
683
+ }
663
684
  break;
664
685
  }
686
+ current = current.parent;
665
687
  }
666
688
  }
667
689
  });
@@ -848,10 +870,6 @@ var MindCache = class {
848
870
  this._idbProvider = null;
849
871
  }
850
872
  }
851
- // Legacy bridge
852
- isRemoteUpdate() {
853
- return false;
854
- }
855
873
  // Serialize state
856
874
  serialize() {
857
875
  const json = {};
@@ -1047,6 +1065,14 @@ var MindCache = class {
1047
1065
  mergedAttrs.systemTags = this.normalizeSystemTags(mergedAttrs.systemTags);
1048
1066
  }
1049
1067
  entryMap.set("attributes", mergedAttrs);
1068
+ const currentValue = entryMap.get("value");
1069
+ if (mergedAttrs.type === "document" && !(currentValue instanceof Y.Text)) {
1070
+ const strValue = typeof currentValue === "string" ? currentValue : String(currentValue ?? "");
1071
+ entryMap.set("value", new Y.Text(strValue));
1072
+ this.getUndoManager(key);
1073
+ } else if (mergedAttrs.type !== "document" && currentValue instanceof Y.Text) {
1074
+ entryMap.set("value", currentValue.toString());
1075
+ }
1050
1076
  });
1051
1077
  }
1052
1078
  set_value(key, value, attributes) {
@@ -1057,12 +1083,21 @@ var MindCache = class {
1057
1083
  if (existingEntry) {
1058
1084
  const existingAttrs = existingEntry.get("attributes");
1059
1085
  if (existingAttrs?.type === "document") {
1060
- if (typeof value === "string") {
1061
- this.replace_document_text(key, value);
1086
+ if (!attributes?.type || attributes.type === "document") {
1087
+ if (typeof value === "string") {
1088
+ this._replaceDocumentText(key, value);
1089
+ }
1090
+ if (attributes) {
1091
+ this.set_attributes(key, attributes);
1092
+ }
1093
+ return;
1062
1094
  }
1063
- return;
1064
1095
  }
1065
1096
  }
1097
+ if (!existingEntry && attributes?.type === "document") {
1098
+ this.set_document(key, typeof value === "string" ? value : "", attributes);
1099
+ return;
1100
+ }
1066
1101
  let entryMap = this.rootMap.get(key);
1067
1102
  const isNewEntry = !entryMap;
1068
1103
  if (isNewEntry) {
@@ -1088,7 +1123,13 @@ var MindCache = class {
1088
1123
  normalizedAttributes.systemTags.push("template");
1089
1124
  }
1090
1125
  }
1091
- entryMap.set("value", value);
1126
+ let valueToSet = value;
1127
+ if (normalizedAttributes.type === "document" && !(valueToSet instanceof Y.Text)) {
1128
+ valueToSet = new Y.Text(typeof value === "string" ? value : String(value ?? ""));
1129
+ } else if (normalizedAttributes.type !== "document" && valueToSet instanceof Y.Text) {
1130
+ valueToSet = valueToSet.toString();
1131
+ }
1132
+ entryMap.set("value", valueToSet);
1092
1133
  entryMap.set("attributes", normalizedAttributes);
1093
1134
  });
1094
1135
  }
@@ -1803,14 +1844,6 @@ var MindCache = class {
1803
1844
  }
1804
1845
  return void 0;
1805
1846
  }
1806
- /**
1807
- * Get plain text content of a document key.
1808
- * For collaborative editing, use get_document() and bind to an editor.
1809
- */
1810
- get_document_text(key) {
1811
- const yText = this.get_document(key);
1812
- return yText?.toString();
1813
- }
1814
1847
  /**
1815
1848
  * Insert text at a position in a document key.
1816
1849
  */
@@ -1830,15 +1863,11 @@ var MindCache = class {
1830
1863
  }
1831
1864
  }
1832
1865
  /**
1833
- * Replace all text in a document key.
1866
+ * Replace all text in a document key (private - use set_value for public API).
1834
1867
  * Uses diff-based updates when changes are < diffThreshold (default 80%).
1835
1868
  * This preserves concurrent edits and provides better undo granularity.
1836
- *
1837
- * @param key - The document key
1838
- * @param newText - The new text content
1839
- * @param diffThreshold - Percentage (0-1) of change above which full replace is used (default: 0.8)
1840
1869
  */
1841
- replace_document_text(key, newText, diffThreshold = 0.8) {
1870
+ _replaceDocumentText(key, newText, diffThreshold = 0.8) {
1842
1871
  const yText = this.get_document(key);
1843
1872
  if (!yText) {
1844
1873
  return;
@@ -1944,7 +1973,7 @@ var MindCache = class {
1944
1973
  },
1945
1974
  execute: async ({ value }) => {
1946
1975
  if (isDocument) {
1947
- this.replace_document_text(key, value);
1976
+ this._replaceDocumentText(key, value);
1948
1977
  } else {
1949
1978
  this.set_value(key, value);
1950
1979
  }
@@ -2100,7 +2129,7 @@ var MindCache = class {
2100
2129
  switch (action) {
2101
2130
  case "write":
2102
2131
  if (isDocument) {
2103
- this.replace_document_text(key, value);
2132
+ this._replaceDocumentText(key, value);
2104
2133
  } else {
2105
2134
  this.set_value(key, value);
2106
2135
  }