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.
@@ -698,6 +698,38 @@ function normalizeRealtimeItem(item) {
698
698
  normalized.childrenSvg = "";
699
699
  return normalized;
700
700
  }
701
+ function reorderYItemsToMatchLocalItems(yItems, items) {
702
+ const currentItems = readVectorItems(yItems);
703
+ const currentItemsById = /* @__PURE__ */ new Map();
704
+ for (const currentItem of currentItems) {
705
+ const id = getItemId(currentItem);
706
+ if (id) currentItemsById.set(id, currentItem);
707
+ }
708
+ const orderedItems = [];
709
+ const orderedIds = /* @__PURE__ */ new Set();
710
+ for (const item of items) {
711
+ const id = getItemId(item);
712
+ if (!id) continue;
713
+ const currentItem = currentItemsById.get(id);
714
+ if (!currentItem) continue;
715
+ orderedItems.push(currentItem);
716
+ orderedIds.add(id);
717
+ }
718
+ for (const currentItem of currentItems) {
719
+ const id = getItemId(currentItem);
720
+ if (!id || !orderedIds.has(id)) orderedItems.push(currentItem);
721
+ }
722
+ const alreadyOrdered = currentItems.length === orderedItems.length && currentItems.every((currentItem, index) => {
723
+ const nextItem = orderedItems[index];
724
+ return nextItem ? getItemId(currentItem) === getItemId(nextItem) : false;
725
+ });
726
+ if (alreadyOrdered) return;
727
+ if (yItems.length > 0) yItems.delete(0, yItems.length);
728
+ yItems.insert(
729
+ 0,
730
+ orderedItems.map((item) => vectorItemToYMap(item))
731
+ );
732
+ }
701
733
  function applyLocalItemsToYDoc(board, options) {
702
734
  const { items, origin } = options;
703
735
  const addedIds = [];
@@ -741,6 +773,7 @@ function applyLocalItemsToYDoc(board, options) {
741
773
  board.yItems.push([yMap]);
742
774
  addedIds.push(id);
743
775
  }
776
+ reorderYItemsToMatchLocalItems(board.yItems, items);
744
777
  }, origin);
745
778
  return { addedIds, removedIds };
746
779
  }