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.js CHANGED
@@ -700,9 +700,9 @@ function useWorkflowDocuments(schemaTypes) {
700
700
  state: newStateId,
701
701
  orderRank: newOrder
702
702
  }).commit().then(res => {
703
- var _a;
703
+ var _a, _b;
704
704
  toast.push({
705
- title: "Moved to \"".concat((_a = newState == null ? void 0 : newState.title) != null ? _a : newStateId, "\""),
705
+ 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, "\""),
706
706
  status: "success"
707
707
  });
708
708
  return res;
@@ -1072,7 +1072,8 @@ function DocumentCard(props) {
1072
1072
  children: [/* @__PURE__ */jsxRuntime.jsx(ui.Box, {
1073
1073
  flex: 1,
1074
1074
  children: /* @__PURE__ */jsxRuntime.jsx(sanity.Preview, {
1075
- layout: "block",
1075
+ layout: "default",
1076
+ skipVisibilityCheck: true,
1076
1077
  value: item,
1077
1078
  schemaType: schema.get(item._type)
1078
1079
  })
@@ -1126,6 +1127,21 @@ function DocumentCard(props) {
1126
1127
  })]
1127
1128
  });
1128
1129
  }
1130
+ function getStyle(draggableStyle, virtualItem) {
1131
+ let transform = "translateY(".concat(virtualItem.start, "px)");
1132
+ if (draggableStyle && draggableStyle.transform) {
1133
+ const draggableTransformY = parseInt(draggableStyle.transform.split(",")[1].split("px")[0], 10);
1134
+ transform = "translateY(".concat(virtualItem.start + draggableTransformY, "px)");
1135
+ }
1136
+ return {
1137
+ position: "absolute",
1138
+ top: 0,
1139
+ left: 0,
1140
+ width: "100%",
1141
+ height: "".concat(virtualItem.size, "px"),
1142
+ transform
1143
+ };
1144
+ }
1129
1145
  function DocumentList(props) {
1130
1146
  const {
1131
1147
  data = [],
@@ -1144,17 +1160,20 @@ function DocumentList(props) {
1144
1160
  return data.length ? filterItemsAndSort(data, state.id, selectedUserIds, selectedSchemaTypes) : [];
1145
1161
  }, [data, selectedSchemaTypes, selectedUserIds, state.id]);
1146
1162
  const parentRef = React.useRef(null);
1147
- const rowVirtualizer = reactVirtual.useVirtualizer({
1148
- count: data.length,
1163
+ const virtualizer = reactVirtual.useVirtualizer({
1164
+ count: dataFiltered.length,
1149
1165
  getScrollElement: () => parentRef.current,
1150
1166
  getItemKey: index => {
1151
1167
  var _a, _b, _c;
1152
1168
  return (_c = (_b = (_a = dataFiltered[index]) == null ? void 0 : _a._metadata) == null ? void 0 : _b.documentId) != null ? _c : index;
1153
1169
  },
1154
- estimateSize: () => 113,
1155
- overscan: 10
1170
+ estimateSize: () => 115,
1171
+ overscan: 7,
1172
+ measureElement: element => {
1173
+ return element.getBoundingClientRect().height || 115;
1174
+ }
1156
1175
  });
1157
- if (!data.length) {
1176
+ if (!data.length || !dataFiltered.length) {
1158
1177
  return null;
1159
1178
  }
1160
1179
  return /* @__PURE__ */jsxRuntime.jsx("div", {
@@ -1162,44 +1181,53 @@ function DocumentList(props) {
1162
1181
  style: {
1163
1182
  height: "100%",
1164
1183
  overflow: "auto",
1165
- paddingTop: 1,
1166
1184
  // Smooths scrollbar behaviour
1167
1185
  overflowAnchor: "none",
1168
- scrollBehavior: "auto"
1186
+ scrollBehavior: "auto",
1187
+ paddingTop: 1
1169
1188
  },
1170
- children: rowVirtualizer.getVirtualItems().map(virtualItem => {
1171
- var _a;
1172
- const item = dataFiltered[virtualItem.index];
1173
- const {
1174
- documentId,
1175
- assignees
1176
- } = (_a = item == null ? void 0 : item._metadata) != null ? _a : {};
1177
- if (!documentId) {
1178
- return null;
1179
- }
1180
- const isInvalid = invalidDocumentIds.includes(documentId);
1181
- const meInAssignees = (user == null ? void 0 : user.id) ? assignees == null ? void 0 : assignees.includes(user.id) : false;
1182
- const isDragDisabled = patchingIds.includes(documentId) || !userRoleCanDrop || isInvalid || !(state.requireAssignment ? state.requireAssignment && meInAssignees : true);
1183
- return /* @__PURE__ */jsxRuntime.jsx(dnd.Draggable, {
1184
- draggableId: documentId,
1185
- index: virtualItem.index,
1186
- isDragDisabled,
1187
- children: (draggableProvided, draggableSnapshot) => /* @__PURE__ */jsxRuntime.jsx("div", {
1188
- ref: draggableProvided.innerRef,
1189
- ...draggableProvided.draggableProps,
1190
- ...draggableProvided.dragHandleProps,
1191
- children: /* @__PURE__ */jsxRuntime.jsx(DocumentCard, {
1192
- userRoleCanDrop,
1193
- isDragDisabled,
1194
- isPatching: patchingIds.includes(documentId),
1195
- isDragging: draggableSnapshot.isDragging,
1196
- item,
1197
- toggleInvalidDocumentId,
1198
- userList,
1199
- states
1189
+ children: /* @__PURE__ */jsxRuntime.jsx("div", {
1190
+ style: {
1191
+ height: "".concat(virtualizer.getTotalSize(), "px"),
1192
+ width: "100%",
1193
+ position: "relative"
1194
+ },
1195
+ children: virtualizer.getVirtualItems().map(virtualItem => {
1196
+ var _a;
1197
+ const item = dataFiltered[virtualItem.index];
1198
+ const {
1199
+ documentId,
1200
+ assignees
1201
+ } = (_a = item == null ? void 0 : item._metadata) != null ? _a : {};
1202
+ const isInvalid = invalidDocumentIds.includes(documentId);
1203
+ const meInAssignees = (user == null ? void 0 : user.id) ? assignees == null ? void 0 : assignees.includes(user.id) : false;
1204
+ const isDragDisabled = patchingIds.includes(documentId) || !userRoleCanDrop || isInvalid || !(state.requireAssignment ? state.requireAssignment && meInAssignees : true);
1205
+ return /* @__PURE__ */jsxRuntime.jsx(dnd.Draggable, {
1206
+ draggableId: documentId,
1207
+ index: virtualItem.index,
1208
+ isDragDisabled,
1209
+ children: (draggableProvided, draggableSnapshot) => /* @__PURE__ */jsxRuntime.jsx("div", {
1210
+ ref: draggableProvided.innerRef,
1211
+ ...draggableProvided.draggableProps,
1212
+ ...draggableProvided.dragHandleProps,
1213
+ style: getStyle(draggableProvided.draggableProps.style, virtualItem),
1214
+ children: /* @__PURE__ */jsxRuntime.jsx("div", {
1215
+ ref: virtualizer.measureElement,
1216
+ "data-index": virtualItem.index,
1217
+ children: /* @__PURE__ */jsxRuntime.jsx(DocumentCard, {
1218
+ userRoleCanDrop,
1219
+ isDragDisabled,
1220
+ isPatching: patchingIds.includes(documentId),
1221
+ isDragging: draggableSnapshot.isDragging,
1222
+ item,
1223
+ toggleInvalidDocumentId,
1224
+ userList,
1225
+ states
1226
+ })
1227
+ })
1200
1228
  })
1201
- })
1202
- }, documentId);
1229
+ }, virtualItem.key);
1230
+ })
1203
1231
  })
1204
1232
  });
1205
1233
  }
@@ -1403,6 +1431,40 @@ function StateTitle(props) {
1403
1431
  })
1404
1432
  });
1405
1433
  }
1434
+ function generateMiddleValue(ranks) {
1435
+ if (!ranks.some(rank => !rank)) {
1436
+ return ranks;
1437
+ }
1438
+ const firstUndefined = ranks.findIndex(rank => !rank);
1439
+ const firstDefinedAfter = ranks.findIndex((rank, index) => rank && index > firstUndefined);
1440
+ const firstDefinedBefore = ranks.findLastIndex((rank, index) => rank && index < firstUndefined);
1441
+ if (firstDefinedAfter === -1 || firstDefinedBefore === -1) {
1442
+ throw new Error("Unable to generate middle value between indexes ".concat(firstDefinedBefore, " and ").concat(firstDefinedAfter));
1443
+ }
1444
+ const beforeRank = ranks[firstDefinedBefore];
1445
+ const afterRank = ranks[firstDefinedAfter];
1446
+ if (!beforeRank || typeof beforeRank === "undefined" || !afterRank || typeof afterRank === "undefined") {
1447
+ throw new Error("Unable to generate middle value between indexes ".concat(firstDefinedBefore, " and ").concat(firstDefinedAfter));
1448
+ }
1449
+ const between = beforeRank.between(afterRank);
1450
+ const middle = Math.floor((firstDefinedAfter + firstDefinedBefore) / 2);
1451
+ if (ranks[middle]) {
1452
+ throw new Error("Should not have overwritten value at index ".concat(middle));
1453
+ }
1454
+ ranks[middle] = between;
1455
+ return ranks;
1456
+ }
1457
+ function generateMultipleOrderRanks(count, start, end) {
1458
+ let ranks = [...Array(count)];
1459
+ const rankStart = start != null ? start : lexorank.LexoRank.min().genNext().genNext();
1460
+ const rankEnd = end != null ? end : lexorank.LexoRank.max().genPrev().genPrev();
1461
+ ranks[0] = rankStart;
1462
+ ranks[count - 1] = rankEnd;
1463
+ for (let i = 0; i < count; i++) {
1464
+ ranks = generateMiddleValue(ranks);
1465
+ }
1466
+ return ranks.sort((a, b) => a.toString().localeCompare(b.toString()));
1467
+ }
1406
1468
  const StyledFloatingCard = styled__default.default(ui.Card)(() => styled.css(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n position: fixed;\n bottom: 0;\n left: 0;\n z-index: 1000;\n "]))));
1407
1469
  function FloatingCard(_ref3) {
1408
1470
  let {
@@ -1452,7 +1514,7 @@ function Verify(props) {
1452
1514
  const stateExists = states.find(s => s.id === state);
1453
1515
  return !stateExists && documentId ? [...acc, documentId] : acc;
1454
1516
  }, []) : [];
1455
- const documentsWithInvalidUserIds = (data == null ? void 0 : data.length) ? data.reduce((acc, cur) => {
1517
+ const documentsWithInvalidUserIds = (data == null ? void 0 : data.length) && (userList == null ? void 0 : userList.length) ? data.reduce((acc, cur) => {
1456
1518
  var _a;
1457
1519
  const {
1458
1520
  documentId,
@@ -1535,16 +1597,34 @@ function Verify(props) {
1535
1597
  var _a;
1536
1598
  return (_a = d._metadata) == null ? void 0 : _a.orderRank;
1537
1599
  });
1538
- const minLexo = firstOrder && data.length !== ids.length ? lexorank.LexoRank.parse(firstOrder) : lexorank.LexoRank.min();
1539
- const maxLexo = secondOrder && data.length !== ids.length ? lexorank.LexoRank.parse(secondOrder) : lexorank.LexoRank.max();
1540
- let newLexo = minLexo.between(maxLexo);
1541
- const lastLexo = maxLexo;
1600
+ const minLexo = firstOrder ? lexorank.LexoRank.parse(firstOrder) : void 0;
1601
+ const maxLexo = secondOrder ? lexorank.LexoRank.parse(secondOrder) : void 0;
1602
+ const ranks = generateMultipleOrderRanks(ids.length, minLexo, maxLexo);
1603
+ const tx = client.transaction();
1604
+ for (let index = 0; index < ids.length; index += 1) {
1605
+ tx.patch("workflow-metadata.".concat(ids[index]), {
1606
+ set: {
1607
+ orderRank: ranks[index].toString()
1608
+ }
1609
+ });
1610
+ }
1611
+ await tx.commit();
1612
+ toast.push({
1613
+ title: "Added order to ".concat(ids.length === 1 ? "1 Document" : "".concat(ids.length, " Documents")),
1614
+ status: "success"
1615
+ });
1616
+ }, [data, client, toast]);
1617
+ const resetOrderOfAllDocuments = React__default.default.useCallback(async ids => {
1618
+ toast.push({
1619
+ title: "Adding ordering...",
1620
+ status: "info"
1621
+ });
1622
+ const ranks = generateMultipleOrderRanks(ids.length);
1542
1623
  const tx = client.transaction();
1543
1624
  for (let index = 0; index < ids.length; index += 1) {
1544
- newLexo = newLexo.between(lastLexo);
1545
1625
  tx.patch("workflow-metadata.".concat(ids[index]), {
1546
1626
  set: {
1547
- orderRank: newLexo.toString()
1627
+ orderRank: ranks[index].toString()
1548
1628
  }
1549
1629
  });
1550
1630
  }
@@ -1575,24 +1655,39 @@ function Verify(props) {
1575
1655
  return /* @__PURE__ */jsxRuntime.jsxs(FloatingCard, {
1576
1656
  children: [documentsWithoutValidMetadataIds.length > 0 ? /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
1577
1657
  tone: "caution",
1658
+ mode: "ghost",
1578
1659
  onClick: () => correctDocuments(documentsWithoutValidMetadataIds),
1579
1660
  text: documentsWithoutValidMetadataIds.length === 1 ? "Correct 1 Document State" : "Correct ".concat(documentsWithoutValidMetadataIds.length, " Document States")
1580
1661
  }) : null, documentsWithInvalidUserIds.length > 0 ? /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
1581
1662
  tone: "caution",
1663
+ mode: "ghost",
1582
1664
  onClick: () => removeUsersFromDocuments(documentsWithInvalidUserIds),
1583
1665
  text: documentsWithInvalidUserIds.length === 1 ? "Remove Invalid Users from 1 Document" : "Remove Invalid Users from ".concat(documentsWithInvalidUserIds.length, " Documents")
1584
1666
  }) : null, documentsWithoutOrderIds.length > 0 ? /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
1585
1667
  tone: "caution",
1668
+ mode: "ghost",
1586
1669
  onClick: () => addOrderToDocuments(documentsWithoutOrderIds),
1587
1670
  text: documentsWithoutOrderIds.length === 1 ? "Set Order for 1 Document" : "Set Order for ".concat(documentsWithoutOrderIds.length, " Documents")
1588
- }) : null, documentsWithDuplicatedOrderIds.length > 0 ? /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
1589
- tone: "caution",
1590
- onClick: () => addOrderToDocuments(documentsWithDuplicatedOrderIds),
1591
- text: documentsWithDuplicatedOrderIds.length === 1 ? "Set Unique Order for 1 Document" : "Set Unique Order for ".concat(documentsWithDuplicatedOrderIds.length, " Documents")
1671
+ }) : null, documentsWithDuplicatedOrderIds.length > 0 ? /* @__PURE__ */jsxRuntime.jsxs(jsxRuntime.Fragment, {
1672
+ children: [/* @__PURE__ */jsxRuntime.jsx(ui.Button, {
1673
+ tone: "caution",
1674
+ mode: "ghost",
1675
+ onClick: () => addOrderToDocuments(documentsWithDuplicatedOrderIds),
1676
+ text: documentsWithDuplicatedOrderIds.length === 1 ? "Set Unique Order for 1 Document" : "Set Unique Order for ".concat(documentsWithDuplicatedOrderIds.length, " Documents")
1677
+ }), /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
1678
+ tone: "caution",
1679
+ mode: "ghost",
1680
+ onClick: () => resetOrderOfAllDocuments(data.map(doc => {
1681
+ var _a;
1682
+ return String((_a = doc._metadata) == null ? void 0 : _a.documentId);
1683
+ })),
1684
+ text: data.length === 1 ? "Reset Order for 1 Document" : "Reset Order for all ".concat(data.length, " Documents")
1685
+ })]
1592
1686
  }) : null, orphanedMetadataDocumentIds.length > 0 ? /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
1593
1687
  text: "Cleanup orphaned metadata",
1594
1688
  onClick: handleOrphans,
1595
- tone: "caution"
1689
+ tone: "caution",
1690
+ mode: "ghost"
1596
1691
  }) : null]
1597
1692
  });
1598
1693
  }
@@ -1859,7 +1954,6 @@ function WorkflowTool(props) {
1859
1954
  ref: provided.innerRef,
1860
1955
  tone: snapshot.isDraggingOver ? "primary" : defaultCardTone,
1861
1956
  height: "fill",
1862
- paddingTop: 1,
1863
1957
  children: [loading ? /* @__PURE__ */jsxRuntime.jsx(ui.Flex, {
1864
1958
  padding: 5,
1865
1959
  align: "center",