sanity-plugin-workflow 1.0.0-beta.7 → 1.0.0-beta.9

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/lib/index.esm.js CHANGED
@@ -687,9 +687,9 @@ function useWorkflowDocuments(schemaTypes) {
687
687
  state: newStateId,
688
688
  orderRank: newOrder
689
689
  }).commit().then(res => {
690
- var _a;
690
+ var _a, _b;
691
691
  toast.push({
692
- title: "Moved to \"".concat((_a = newState == null ? void 0 : newState.title) != null ? _a : newStateId, "\""),
692
+ title: newState.id === document._metadata.state ? "Reordered in \"".concat((_a = newState == null ? void 0 : newState.title) != null ? _a : newStateId, "\"") : "Moved to \"".concat((_b = newState == null ? void 0 : newState.title) != null ? _b : newStateId, "\""),
693
693
  status: "success"
694
694
  });
695
695
  return res;
@@ -1059,7 +1059,8 @@ function DocumentCard(props) {
1059
1059
  children: [/* @__PURE__ */jsx(Box, {
1060
1060
  flex: 1,
1061
1061
  children: /* @__PURE__ */jsx(Preview, {
1062
- layout: "block",
1062
+ layout: "default",
1063
+ skipVisibilityCheck: true,
1063
1064
  value: item,
1064
1065
  schemaType: schema.get(item._type)
1065
1066
  })
@@ -1113,6 +1114,21 @@ function DocumentCard(props) {
1113
1114
  })]
1114
1115
  });
1115
1116
  }
1117
+ function getStyle(draggableStyle, virtualItem) {
1118
+ let transform = "translateY(".concat(virtualItem.start, "px)");
1119
+ if (draggableStyle && draggableStyle.transform) {
1120
+ const draggableTransformY = parseInt(draggableStyle.transform.split(",")[1].split("px")[0], 10);
1121
+ transform = "translateY(".concat(virtualItem.start + draggableTransformY, "px)");
1122
+ }
1123
+ return {
1124
+ position: "absolute",
1125
+ top: 0,
1126
+ left: 0,
1127
+ width: "100%",
1128
+ height: "".concat(virtualItem.size, "px"),
1129
+ transform
1130
+ };
1131
+ }
1116
1132
  function DocumentList(props) {
1117
1133
  const {
1118
1134
  data = [],
@@ -1131,17 +1147,20 @@ function DocumentList(props) {
1131
1147
  return data.length ? filterItemsAndSort(data, state.id, selectedUserIds, selectedSchemaTypes) : [];
1132
1148
  }, [data, selectedSchemaTypes, selectedUserIds, state.id]);
1133
1149
  const parentRef = useRef(null);
1134
- const rowVirtualizer = useVirtualizer({
1135
- count: data.length,
1150
+ const virtualizer = useVirtualizer({
1151
+ count: dataFiltered.length,
1136
1152
  getScrollElement: () => parentRef.current,
1137
1153
  getItemKey: index => {
1138
1154
  var _a, _b, _c;
1139
1155
  return (_c = (_b = (_a = dataFiltered[index]) == null ? void 0 : _a._metadata) == null ? void 0 : _b.documentId) != null ? _c : index;
1140
1156
  },
1141
- estimateSize: () => 113,
1142
- overscan: 10
1157
+ estimateSize: () => 115,
1158
+ overscan: 7,
1159
+ measureElement: element => {
1160
+ return element.getBoundingClientRect().height || 115;
1161
+ }
1143
1162
  });
1144
- if (!data.length) {
1163
+ if (!data.length || !dataFiltered.length) {
1145
1164
  return null;
1146
1165
  }
1147
1166
  return /* @__PURE__ */jsx("div", {
@@ -1149,44 +1168,53 @@ function DocumentList(props) {
1149
1168
  style: {
1150
1169
  height: "100%",
1151
1170
  overflow: "auto",
1152
- paddingTop: 1,
1153
1171
  // Smooths scrollbar behaviour
1154
1172
  overflowAnchor: "none",
1155
- scrollBehavior: "auto"
1173
+ scrollBehavior: "auto",
1174
+ paddingTop: 1
1156
1175
  },
1157
- children: rowVirtualizer.getVirtualItems().map(virtualItem => {
1158
- var _a;
1159
- const item = dataFiltered[virtualItem.index];
1160
- const {
1161
- documentId,
1162
- assignees
1163
- } = (_a = item == null ? void 0 : item._metadata) != null ? _a : {};
1164
- if (!documentId) {
1165
- return null;
1166
- }
1167
- const isInvalid = invalidDocumentIds.includes(documentId);
1168
- const meInAssignees = (user == null ? void 0 : user.id) ? assignees == null ? void 0 : assignees.includes(user.id) : false;
1169
- const isDragDisabled = patchingIds.includes(documentId) || !userRoleCanDrop || isInvalid || !(state.requireAssignment ? state.requireAssignment && meInAssignees : true);
1170
- return /* @__PURE__ */jsx(Draggable, {
1171
- draggableId: documentId,
1172
- index: virtualItem.index,
1173
- isDragDisabled,
1174
- children: (draggableProvided, draggableSnapshot) => /* @__PURE__ */jsx("div", {
1175
- ref: draggableProvided.innerRef,
1176
- ...draggableProvided.draggableProps,
1177
- ...draggableProvided.dragHandleProps,
1178
- children: /* @__PURE__ */jsx(DocumentCard, {
1179
- userRoleCanDrop,
1180
- isDragDisabled,
1181
- isPatching: patchingIds.includes(documentId),
1182
- isDragging: draggableSnapshot.isDragging,
1183
- item,
1184
- toggleInvalidDocumentId,
1185
- userList,
1186
- states
1176
+ children: /* @__PURE__ */jsx("div", {
1177
+ style: {
1178
+ height: "".concat(virtualizer.getTotalSize(), "px"),
1179
+ width: "100%",
1180
+ position: "relative"
1181
+ },
1182
+ children: virtualizer.getVirtualItems().map(virtualItem => {
1183
+ var _a;
1184
+ const item = dataFiltered[virtualItem.index];
1185
+ const {
1186
+ documentId,
1187
+ assignees
1188
+ } = (_a = item == null ? void 0 : item._metadata) != null ? _a : {};
1189
+ const isInvalid = invalidDocumentIds.includes(documentId);
1190
+ const meInAssignees = (user == null ? void 0 : user.id) ? assignees == null ? void 0 : assignees.includes(user.id) : false;
1191
+ const isDragDisabled = patchingIds.includes(documentId) || !userRoleCanDrop || isInvalid || !(state.requireAssignment ? state.requireAssignment && meInAssignees : true);
1192
+ return /* @__PURE__ */jsx(Draggable, {
1193
+ draggableId: documentId,
1194
+ index: virtualItem.index,
1195
+ isDragDisabled,
1196
+ children: (draggableProvided, draggableSnapshot) => /* @__PURE__ */jsx("div", {
1197
+ ref: draggableProvided.innerRef,
1198
+ ...draggableProvided.draggableProps,
1199
+ ...draggableProvided.dragHandleProps,
1200
+ style: getStyle(draggableProvided.draggableProps.style, virtualItem),
1201
+ children: /* @__PURE__ */jsx("div", {
1202
+ ref: virtualizer.measureElement,
1203
+ "data-index": virtualItem.index,
1204
+ children: /* @__PURE__ */jsx(DocumentCard, {
1205
+ userRoleCanDrop,
1206
+ isDragDisabled,
1207
+ isPatching: patchingIds.includes(documentId),
1208
+ isDragging: draggableSnapshot.isDragging,
1209
+ item,
1210
+ toggleInvalidDocumentId,
1211
+ userList,
1212
+ states
1213
+ })
1214
+ })
1187
1215
  })
1188
- })
1189
- }, documentId);
1216
+ }, virtualItem.key);
1217
+ })
1190
1218
  })
1191
1219
  });
1192
1220
  }
@@ -1390,6 +1418,40 @@ function StateTitle(props) {
1390
1418
  })
1391
1419
  });
1392
1420
  }
1421
+ function generateMiddleValue(ranks) {
1422
+ if (!ranks.some(rank => !rank)) {
1423
+ return ranks;
1424
+ }
1425
+ const firstUndefined = ranks.findIndex(rank => !rank);
1426
+ const firstDefinedAfter = ranks.findIndex((rank, index) => rank && index > firstUndefined);
1427
+ const firstDefinedBefore = ranks.findLastIndex((rank, index) => rank && index < firstUndefined);
1428
+ if (firstDefinedAfter === -1 || firstDefinedBefore === -1) {
1429
+ throw new Error("Unable to generate middle value between indexes ".concat(firstDefinedBefore, " and ").concat(firstDefinedAfter));
1430
+ }
1431
+ const beforeRank = ranks[firstDefinedBefore];
1432
+ const afterRank = ranks[firstDefinedAfter];
1433
+ if (!beforeRank || typeof beforeRank === "undefined" || !afterRank || typeof afterRank === "undefined") {
1434
+ throw new Error("Unable to generate middle value between indexes ".concat(firstDefinedBefore, " and ").concat(firstDefinedAfter));
1435
+ }
1436
+ const between = beforeRank.between(afterRank);
1437
+ const middle = Math.floor((firstDefinedAfter + firstDefinedBefore) / 2);
1438
+ if (ranks[middle]) {
1439
+ throw new Error("Should not have overwritten value at index ".concat(middle));
1440
+ }
1441
+ ranks[middle] = between;
1442
+ return ranks;
1443
+ }
1444
+ function generateMultipleOrderRanks(count, start, end) {
1445
+ let ranks = [...Array(count)];
1446
+ const rankStart = start != null ? start : LexoRank.min().genNext().genNext();
1447
+ const rankEnd = end != null ? end : LexoRank.max().genPrev().genPrev();
1448
+ ranks[0] = rankStart;
1449
+ ranks[count - 1] = rankEnd;
1450
+ for (let i = 0; i < count; i++) {
1451
+ ranks = generateMiddleValue(ranks);
1452
+ }
1453
+ return ranks.sort((a, b) => a.toString().localeCompare(b.toString()));
1454
+ }
1393
1455
  const StyledFloatingCard = styled(Card)(() => css(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n position: fixed;\n bottom: 0;\n left: 0;\n z-index: 1000;\n "]))));
1394
1456
  function FloatingCard(_ref3) {
1395
1457
  let {
@@ -1439,7 +1501,7 @@ function Verify(props) {
1439
1501
  const stateExists = states.find(s => s.id === state);
1440
1502
  return !stateExists && documentId ? [...acc, documentId] : acc;
1441
1503
  }, []) : [];
1442
- const documentsWithInvalidUserIds = (data == null ? void 0 : data.length) ? data.reduce((acc, cur) => {
1504
+ const documentsWithInvalidUserIds = (data == null ? void 0 : data.length) && (userList == null ? void 0 : userList.length) ? data.reduce((acc, cur) => {
1443
1505
  var _a;
1444
1506
  const {
1445
1507
  documentId,
@@ -1522,16 +1584,34 @@ function Verify(props) {
1522
1584
  var _a;
1523
1585
  return (_a = d._metadata) == null ? void 0 : _a.orderRank;
1524
1586
  });
1525
- const minLexo = firstOrder && data.length !== ids.length ? LexoRank.parse(firstOrder) : LexoRank.min();
1526
- const maxLexo = secondOrder && data.length !== ids.length ? LexoRank.parse(secondOrder) : LexoRank.max();
1527
- let newLexo = minLexo.between(maxLexo);
1528
- const lastLexo = maxLexo;
1587
+ const minLexo = firstOrder ? LexoRank.parse(firstOrder) : void 0;
1588
+ const maxLexo = secondOrder ? LexoRank.parse(secondOrder) : void 0;
1589
+ const ranks = generateMultipleOrderRanks(ids.length, minLexo, maxLexo);
1590
+ const tx = client.transaction();
1591
+ for (let index = 0; index < ids.length; index += 1) {
1592
+ tx.patch("workflow-metadata.".concat(ids[index]), {
1593
+ set: {
1594
+ orderRank: ranks[index].toString()
1595
+ }
1596
+ });
1597
+ }
1598
+ await tx.commit();
1599
+ toast.push({
1600
+ title: "Added order to ".concat(ids.length === 1 ? "1 Document" : "".concat(ids.length, " Documents")),
1601
+ status: "success"
1602
+ });
1603
+ }, [data, client, toast]);
1604
+ const resetOrderOfAllDocuments = React.useCallback(async ids => {
1605
+ toast.push({
1606
+ title: "Adding ordering...",
1607
+ status: "info"
1608
+ });
1609
+ const ranks = generateMultipleOrderRanks(ids.length);
1529
1610
  const tx = client.transaction();
1530
1611
  for (let index = 0; index < ids.length; index += 1) {
1531
- newLexo = newLexo.between(lastLexo);
1532
1612
  tx.patch("workflow-metadata.".concat(ids[index]), {
1533
1613
  set: {
1534
- orderRank: newLexo.toString()
1614
+ orderRank: ranks[index].toString()
1535
1615
  }
1536
1616
  });
1537
1617
  }
@@ -1562,24 +1642,39 @@ function Verify(props) {
1562
1642
  return /* @__PURE__ */jsxs(FloatingCard, {
1563
1643
  children: [documentsWithoutValidMetadataIds.length > 0 ? /* @__PURE__ */jsx(Button, {
1564
1644
  tone: "caution",
1645
+ mode: "ghost",
1565
1646
  onClick: () => correctDocuments(documentsWithoutValidMetadataIds),
1566
1647
  text: documentsWithoutValidMetadataIds.length === 1 ? "Correct 1 Document State" : "Correct ".concat(documentsWithoutValidMetadataIds.length, " Document States")
1567
1648
  }) : null, documentsWithInvalidUserIds.length > 0 ? /* @__PURE__ */jsx(Button, {
1568
1649
  tone: "caution",
1650
+ mode: "ghost",
1569
1651
  onClick: () => removeUsersFromDocuments(documentsWithInvalidUserIds),
1570
1652
  text: documentsWithInvalidUserIds.length === 1 ? "Remove Invalid Users from 1 Document" : "Remove Invalid Users from ".concat(documentsWithInvalidUserIds.length, " Documents")
1571
1653
  }) : null, documentsWithoutOrderIds.length > 0 ? /* @__PURE__ */jsx(Button, {
1572
1654
  tone: "caution",
1655
+ mode: "ghost",
1573
1656
  onClick: () => addOrderToDocuments(documentsWithoutOrderIds),
1574
1657
  text: documentsWithoutOrderIds.length === 1 ? "Set Order for 1 Document" : "Set Order for ".concat(documentsWithoutOrderIds.length, " Documents")
1575
- }) : null, documentsWithDuplicatedOrderIds.length > 0 ? /* @__PURE__ */jsx(Button, {
1576
- tone: "caution",
1577
- onClick: () => addOrderToDocuments(documentsWithDuplicatedOrderIds),
1578
- text: documentsWithDuplicatedOrderIds.length === 1 ? "Set Unique Order for 1 Document" : "Set Unique Order for ".concat(documentsWithDuplicatedOrderIds.length, " Documents")
1658
+ }) : null, documentsWithDuplicatedOrderIds.length > 0 ? /* @__PURE__ */jsxs(Fragment, {
1659
+ children: [/* @__PURE__ */jsx(Button, {
1660
+ tone: "caution",
1661
+ mode: "ghost",
1662
+ onClick: () => addOrderToDocuments(documentsWithDuplicatedOrderIds),
1663
+ text: documentsWithDuplicatedOrderIds.length === 1 ? "Set Unique Order for 1 Document" : "Set Unique Order for ".concat(documentsWithDuplicatedOrderIds.length, " Documents")
1664
+ }), /* @__PURE__ */jsx(Button, {
1665
+ tone: "caution",
1666
+ mode: "ghost",
1667
+ onClick: () => resetOrderOfAllDocuments(data.map(doc => {
1668
+ var _a;
1669
+ return String((_a = doc._metadata) == null ? void 0 : _a.documentId);
1670
+ })),
1671
+ text: data.length === 1 ? "Reset Order for 1 Document" : "Reset Order for all ".concat(data.length, " Documents")
1672
+ })]
1579
1673
  }) : null, orphanedMetadataDocumentIds.length > 0 ? /* @__PURE__ */jsx(Button, {
1580
1674
  text: "Cleanup orphaned metadata",
1581
1675
  onClick: handleOrphans,
1582
- tone: "caution"
1676
+ tone: "caution",
1677
+ mode: "ghost"
1583
1678
  }) : null]
1584
1679
  });
1585
1680
  }
@@ -1846,7 +1941,6 @@ function WorkflowTool(props) {
1846
1941
  ref: provided.innerRef,
1847
1942
  tone: snapshot.isDraggingOver ? "primary" : defaultCardTone,
1848
1943
  height: "fill",
1849
- paddingTop: 1,
1850
1944
  children: [loading ? /* @__PURE__ */jsx(Flex, {
1851
1945
  padding: 5,
1852
1946
  align: "center",