@quintoandar-tokko/gridimagepicker 1.0.24 → 1.0.25

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/ImageItem.js CHANGED
@@ -48,10 +48,8 @@ var ImageItem = function ImageItem(_ref) {
48
48
  setNodeRef = _useSortable.setNodeRef,
49
49
  transform = _useSortable.transform,
50
50
  transition = _useSortable.transition;
51
- var _useState = (0, _react.useState)(true),
52
- _useState2 = _slicedToArray(_useState, 2),
53
- isEditedActive = _useState2[0],
54
- setIsEditedActive = _useState2[1];
51
+ var previousEditedSrc = (0, _react.useRef)(item.editedSrc);
52
+ var isEditedActive = item.isEditedActive;
55
53
  var displaySrc = isEditedActive && item.editedSrc ? item.editedSrc : item.src;
56
54
  var _useSize = (0, _useSize3["default"])(displaySrc, sizeFetcher),
57
55
  _useSize2 = _slicedToArray(_useSize, 3),
@@ -66,17 +64,17 @@ var ImageItem = function ImageItem(_ref) {
66
64
  isLoadingAspectRatio = _useAspectRatio2[3],
67
65
  isErrorAspectRatio = _useAspectRatio2[4];
68
66
  var wrapperRef = (0, _react.useRef)(null);
69
- var _useState3 = (0, _react.useState)({
67
+ var _useState = (0, _react.useState)({
70
68
  left: 0,
71
69
  top: 0
72
70
  }),
71
+ _useState2 = _slicedToArray(_useState, 2),
72
+ tooltipPosition = _useState2[0],
73
+ setTooltipPosition = _useState2[1];
74
+ var _useState3 = (0, _react.useState)(false),
73
75
  _useState4 = _slicedToArray(_useState3, 2),
74
- tooltipPosition = _useState4[0],
75
- setTooltipPosition = _useState4[1];
76
- var _useState5 = (0, _react.useState)(false),
77
- _useState6 = _slicedToArray(_useState5, 2),
78
- isTooltipShowable = _useState6[0],
79
- setIsTooltipShowable = _useState6[1];
76
+ isTooltipShowable = _useState4[0],
77
+ setIsTooltipShowable = _useState4[1];
80
78
  var isDraggingActive = status.isDraggingActive,
81
79
  isMaxSelectableReached = status.isMaxSelectableReached,
82
80
  itemsAreReady = status.itemsAreReady;
@@ -90,6 +88,15 @@ var ImageItem = function ImageItem(_ref) {
90
88
  var backgroundImage = item.loading || item.fetchError ? '' : "url(".concat(displaySrc, ")");
91
89
  var unclickable = !(0, _manageItem.isItemClickable)(item, isMaxSelectableReached, itemsAreReady);
92
90
  var disabled = (0, _manageItem.isDisabledCheckbox)(item, isMaxSelectableReached);
91
+
92
+ // On (re)edit, flip back to the edited version and notify; the ref skips mount/re-renders.
93
+ (0, _react.useEffect)(function () {
94
+ if (item.editedSrc === previousEditedSrc.current) return;
95
+ previousEditedSrc.current = item.editedSrc;
96
+ if (item.editedSrc) {
97
+ onSwitchChange === null || onSwitchChange === void 0 || onSwitchChange(item, true);
98
+ }
99
+ }, [item.editedSrc]);
93
100
  (0, _react.useEffect)(function () {
94
101
  var handleHideTooltip = function handleHideTooltip() {
95
102
  setIsTooltipShowable(false);
@@ -198,14 +205,13 @@ var ImageItem = function ImageItem(_ref) {
198
205
  className: "imageItemIconEdit"
199
206
  })), /*#__PURE__*/_react["default"].createElement(_box.Box, {
200
207
  className: "imageItemSwitch",
201
- "data-visible": item.isEdited && itemsAreReady && (!isError || !!item.editedSrc),
208
+ "data-visible": Boolean(item.editedSrc) && itemsAreReady,
202
209
  "data-cover": item.position === 1
203
210
  }, /*#__PURE__*/_react["default"].createElement(_box.Box, {
204
211
  className: "imageItemSwitchOption",
205
212
  "data-active": !isEditedActive,
206
213
  onClick: function onClick(e) {
207
214
  e.stopPropagation();
208
- setIsEditedActive(false);
209
215
  onSwitchChange === null || onSwitchChange === void 0 || onSwitchChange(item, false);
210
216
  }
211
217
  }, texts === null || texts === void 0 ? void 0 : texts.original), /*#__PURE__*/_react["default"].createElement(_box.Box, {
@@ -213,12 +219,11 @@ var ImageItem = function ImageItem(_ref) {
213
219
  "data-active": isEditedActive,
214
220
  onClick: function onClick(e) {
215
221
  e.stopPropagation();
216
- setIsEditedActive(true);
217
222
  onSwitchChange === null || onSwitchChange === void 0 || onSwitchChange(item, true);
218
223
  }
219
224
  }, texts === null || texts === void 0 ? void 0 : texts.edited))), /*#__PURE__*/_react["default"].createElement(_box.Box, {
220
225
  className: "imageItemTooltip",
221
- "data-showable": isTooltipShowable,
226
+ "data-showable": isTooltipShowable && Boolean(tooltipErrorText),
222
227
  __css: {
223
228
  left: tooltipPosition.left,
224
229
  top: tooltipPosition.top
@@ -10,13 +10,13 @@ var _sortable = require("@dnd-kit/sortable");
10
10
  var _manageItem = require("../utils/manageItem");
11
11
  var _manageListOfItems = require("../utils/manageListOfItems");
12
12
  var _excluded = ["id"];
13
+ function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
14
+ function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
13
15
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
14
16
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
15
17
  function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
16
18
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
17
19
  function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
18
- function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
19
- function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
20
20
  function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
21
21
  function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
22
22
  function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
@@ -29,7 +29,8 @@ function useGridImagePicker(_ref) {
29
29
  maxSizeInMB = _ref.maxSizeInMB,
30
30
  minAspectRatio = _ref.minAspectRatio,
31
31
  maxAspectRatio = _ref.maxAspectRatio,
32
- onChange = _ref.onChange;
32
+ onChange = _ref.onChange,
33
+ onSwitchChange = _ref.onSwitchChange;
33
34
  var _useState = (0, _react.useState)(false),
34
35
  _useState2 = _slicedToArray(_useState, 2),
35
36
  isDraggingActive = _useState2[0],
@@ -59,6 +60,25 @@ function useGridImagePicker(_ref) {
59
60
  setInitialUrlList(listOfImages);
60
61
  setItemNewUrl(null);
61
62
  }, [itemNewUrl]);
63
+
64
+ // Sync `editedSrc` from the source list, matched by positional `id` (robust to duplicate `src` and reordering).
65
+ (0, _react.useEffect)(function () {
66
+ setItems(function (prevItems) {
67
+ var hasChanges = false;
68
+ var nextItems = prevItems.map(function (item) {
69
+ var source = listOfImages[item.id - 1];
70
+ if (!source || source.editedSrc === item.editedSrc) return item;
71
+ hasChanges = true;
72
+ // No `editedSrc` means nothing to show as edited (the flip-back lives in ImageItem).
73
+ var isEditedActive = source.editedSrc ? item.isEditedActive : false;
74
+ return _objectSpread(_objectSpread({}, item), {}, {
75
+ editedSrc: source.editedSrc,
76
+ isEditedActive: isEditedActive
77
+ });
78
+ });
79
+ return hasChanges ? nextItems : prevItems;
80
+ });
81
+ }, [listOfImages]);
62
82
  var maxSelectable = Math.min(maxSelectablePreferenceByUser, items.filter(function (item) {
63
83
  return !item.sizeError && !item.aspectRatioError && !item.fetchError;
64
84
  }).length);
@@ -115,6 +135,18 @@ function useGridImagePicker(_ref) {
115
135
  });
116
136
  });
117
137
  };
138
+
139
+ // Single source of truth for the shown version: stored in `items` (for `onChange`) and reported via `onSwitchChange`.
140
+ var handleSwitchChange = function handleSwitchChange(targetItem, isEditedActive) {
141
+ setItems(function (prevItems) {
142
+ return prevItems.map(function (item) {
143
+ return item.id === targetItem.id ? _objectSpread(_objectSpread({}, item), {}, {
144
+ isEditedActive: isEditedActive
145
+ }) : item;
146
+ });
147
+ });
148
+ onSwitchChange === null || onSwitchChange === void 0 || onSwitchChange(targetItem, isEditedActive);
149
+ };
118
150
  var handleDragStart = function handleDragStart() {
119
151
  setIsDraggingActive(true);
120
152
  };
@@ -142,6 +174,7 @@ function useGridImagePicker(_ref) {
142
174
  methods: {
143
175
  handleClickItem: handleClickItem,
144
176
  handleUpdateItem: handleUpdateItem,
177
+ handleSwitchChange: handleSwitchChange,
145
178
  handleDeselectAll: handleDeselectAll,
146
179
  handleSelectAll: handleSelectAll,
147
180
  handleDragStart: handleDragStart,
package/lib/index.js CHANGED
@@ -16,15 +16,18 @@ function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default":
16
16
  * A grid-based image picker component with sortable and selectable images.
17
17
  *
18
18
  * @param {Object} props - The props for GridImagePicker.
19
- * @param {Array<{src: string, isEdited: boolean}>} props.listOfImages - Array of image objects to display in the grid.
19
+ * @param {Array<{src: string, editedSrc?: string}>} props.listOfImages - Array of image objects to display in the grid.
20
+ * An image is considered editable/edited when it has an `editedSrc`; that enables the Original/Editada switch.
20
21
  * @param {number} [props.maxSelectablePreferenceByUser=10] - Maximum number of images that can be selected by the user.
21
22
  * Default is 10. This value is a preference; if there are fewer images available than this number, it will adjust accordingly.
22
23
  * @param {number} [props.maxSizeInMB=8] - Maximum size in megabytes for each image. Default is 8 MB. Images exceeding this limit will be marked as an error.
23
24
  * @param {number} [props.minAspectRatio] - Minimum aspect ratio allowed for the images. Images that don't meet this ratio will be marked as an error.
24
25
  * @param {number} [props.maxAspectRatio] - Maximum aspect ratio allowed for the images. Images that exceed this ratio will be marked as an error.
25
26
  * @param {Function} [props.onChange] - Optional callback function invoked whenever the internal state of images changes.
26
- * Called with the updated list of items as `(items) => onChange(items)`.
27
- * @param {Function} [props.onSwitchChange] - Optional callback invoked when the user toggles the Original/Editada switch. Called with `(item, isEditedActive)`.
27
+ * Called with the updated list of items as `(items) => onChange(items)`. Each item exposes `isEditedActive`
28
+ * (whether the edited version is currently being shown); combine it with `editedSrc` to know which version is in use.
29
+ * @param {Function} [props.onSwitchChange] - Optional callback invoked when the Original/Editada switch changes, both on user
30
+ * toggle and on the automatic flip after an image is (re)edited. Called with `(item, isEditedActive)`.
28
31
  * @param {Function} [props.sizeFetcher] - Optional fetch function to get the size of the images. Default: (src) => fetch(src)
29
32
  * @param {Object} props.texts - Nested texts
30
33
  */
@@ -39,7 +42,7 @@ var GridImagePicker = exports.GridImagePicker = function GridImagePicker(_ref) {
39
42
  onChange = _ref.onChange,
40
43
  _ref$onEdit = _ref.onEdit,
41
44
  onEdit = _ref$onEdit === void 0 ? null : _ref$onEdit,
42
- _onSwitchChange = _ref.onSwitchChange,
45
+ onSwitchChange = _ref.onSwitchChange,
43
46
  texts = _ref.texts,
44
47
  sizeFetcher = _ref.sizeFetcher;
45
48
  var _useGridImagePicker = (0, _useGridImagePicker2["default"])({
@@ -48,7 +51,8 @@ var GridImagePicker = exports.GridImagePicker = function GridImagePicker(_ref) {
48
51
  maxSizeInMB: maxSizeInMB,
49
52
  minAspectRatio: minAspectRatio,
50
53
  maxAspectRatio: maxAspectRatio,
51
- onChange: onChange
54
+ onChange: onChange,
55
+ onSwitchChange: onSwitchChange
52
56
  }),
53
57
  attributes = _useGridImagePicker.attributes,
54
58
  methods = _useGridImagePicker.methods;
@@ -57,6 +61,7 @@ var GridImagePicker = exports.GridImagePicker = function GridImagePicker(_ref) {
57
61
  status = attributes.status;
58
62
  var handleClickItem = methods.handleClickItem,
59
63
  handleUpdateItem = methods.handleUpdateItem,
64
+ handleSwitchChange = methods.handleSwitchChange,
60
65
  handleDeselectAll = methods.handleDeselectAll,
61
66
  handleSelectAll = methods.handleSelectAll,
62
67
  handleDragStart = methods.handleDragStart,
@@ -100,9 +105,7 @@ var GridImagePicker = exports.GridImagePicker = function GridImagePicker(_ref) {
100
105
  config: config,
101
106
  handleClick: handleClickItem,
102
107
  onEdit: onEdit,
103
- onSwitchChange: function onSwitchChange(item) {
104
- return _onSwitchChange(item);
105
- },
108
+ onSwitchChange: handleSwitchChange,
106
109
  handleUpdateItem: handleUpdateItem,
107
110
  texts: texts,
108
111
  sizeFetcher: sizeFetcher
@@ -23,7 +23,8 @@ var getItemsInitialState = exports.getItemsInitialState = function getItemsIniti
23
23
  id: index + 1,
24
24
  src: image.src,
25
25
  editedSrc: image.editedSrc,
26
- isEdited: image.isEdited,
26
+ // Whether the edited version is shown; only meaningful when there's an `editedSrc`.
27
+ isEditedActive: Boolean(image.editedSrc),
27
28
  checked: false,
28
29
  position: 0,
29
30
  height: 0,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quintoandar-tokko/gridimagepicker",
3
- "version": "1.0.24",
3
+ "version": "1.0.25",
4
4
  "main": "lib/index.js",
5
5
  "module": "src/index.js",
6
6
  "files": [
@@ -23,5 +23,5 @@
23
23
  "publishConfig": {
24
24
  "access": "public"
25
25
  },
26
- "gitHead": "25f60fb626bac68e2d2599d2354dd5157d7371ab"
26
+ "gitHead": "ff1bd5286d3900060f981d2f8d81f1c074b4f22d"
27
27
  }