canvu-react 0.4.78 → 0.4.80

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.
@@ -676,6 +676,38 @@ function normalizeRealtimeItem(item) {
676
676
  normalized.childrenSvg = "";
677
677
  return normalized;
678
678
  }
679
+ function reorderYItemsToMatchLocalItems(yItems, items) {
680
+ const currentItems = readVectorItems(yItems);
681
+ const currentItemsById = /* @__PURE__ */ new Map();
682
+ for (const currentItem of currentItems) {
683
+ const id = getItemId(currentItem);
684
+ if (id) currentItemsById.set(id, currentItem);
685
+ }
686
+ const orderedItems = [];
687
+ const orderedIds = /* @__PURE__ */ new Set();
688
+ for (const item of items) {
689
+ const id = getItemId(item);
690
+ if (!id) continue;
691
+ const currentItem = currentItemsById.get(id);
692
+ if (!currentItem) continue;
693
+ orderedItems.push(currentItem);
694
+ orderedIds.add(id);
695
+ }
696
+ for (const currentItem of currentItems) {
697
+ const id = getItemId(currentItem);
698
+ if (!id || !orderedIds.has(id)) orderedItems.push(currentItem);
699
+ }
700
+ const alreadyOrdered = currentItems.length === orderedItems.length && currentItems.every((currentItem, index) => {
701
+ const nextItem = orderedItems[index];
702
+ return nextItem ? getItemId(currentItem) === getItemId(nextItem) : false;
703
+ });
704
+ if (alreadyOrdered) return;
705
+ if (yItems.length > 0) yItems.delete(0, yItems.length);
706
+ yItems.insert(
707
+ 0,
708
+ orderedItems.map((item) => vectorItemToYMap(item))
709
+ );
710
+ }
679
711
  function applyLocalItemsToYDoc(board, options) {
680
712
  const { items, origin } = options;
681
713
  const addedIds = [];
@@ -719,6 +751,7 @@ function applyLocalItemsToYDoc(board, options) {
719
751
  board.yItems.push([yMap]);
720
752
  addedIds.push(id);
721
753
  }
754
+ reorderYItemsToMatchLocalItems(board.yItems, items);
722
755
  }, origin);
723
756
  return { addedIds, removedIds };
724
757
  }