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