sanity-plugin-hotspot-array 0.0.8 → 0.1.0-v3-studio.1

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.
@@ -1,189 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _client = _interopRequireDefault(require("part:@sanity/base/client"));
9
-
10
- var _formBuilder = require("part:@sanity/form-builder");
11
-
12
- var _assetUtils = require("@sanity/asset-utils");
13
-
14
- var _FormBuilderInput = require("@sanity/form-builder/lib/FormBuilderInput");
15
-
16
- var _PatchEvent = require("@sanity/form-builder/PatchEvent");
17
-
18
- var _imageUrl = _interopRequireDefault(require("@sanity/image-url"));
19
-
20
- var _ui = require("@sanity/ui");
21
-
22
- var _content = require("@sanity/util/content");
23
-
24
- var _get = _interopRequireDefault(require("lodash/get"));
25
-
26
- var _react = _interopRequireWildcard(require("react"));
27
-
28
- var _web = require("@react-hookz/web");
29
-
30
- var _Feedback = _interopRequireDefault(require("./Feedback"));
31
-
32
- var _Spot = _interopRequireDefault(require("./Spot"));
33
-
34
- var _useUnsetInputComponent = require("./useUnsetInputComponent");
35
-
36
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
37
-
38
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
39
-
40
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
41
-
42
- function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
43
-
44
- function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
45
-
46
- 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."); }
47
-
48
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
49
-
50
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
51
-
52
- function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
53
-
54
- function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
55
-
56
- var imageStyle = {
57
- width: "100%",
58
- height: "auto"
59
- };
60
- var VALID_ROOT_PATHS = ['document', 'parent'];
61
-
62
- var HotspotArray = /*#__PURE__*/_react.default.forwardRef((props, ref) => {
63
- var _type$options, _type$options2, _type$options3;
64
-
65
- var type = props.type,
66
- value = props.value,
67
- onChange = props.onChange,
68
- document = props.document;
69
-
70
- var _ref = type !== null && type !== void 0 ? type : {},
71
- options = _ref.options; // Attempt prevention of infinite loop in <FormBuilderInput />
72
- // Re-renders can still occur if this Component is used again in a nested field
73
-
74
-
75
- var typeWithoutInputComponent = (0, _useUnsetInputComponent.useUnsetInputComponent)(type, type === null || type === void 0 ? void 0 : type.inputComponent);
76
- var imageHotspotPathRoot = VALID_ROOT_PATHS.includes(options === null || options === void 0 ? void 0 : options.imageHotspotPathRoot) ? props[options.imageHotspotPathRoot] : document;
77
- /**
78
- * Finding the image from the imageHotspotPathRoot (defaults to document),
79
- * using the path from the hotspot's `options` field
80
- *
81
- * when changes in imageHotspotPathRoot (e.g. document) occur,
82
- * check if there are any changes to the hotspotImage and update the reference
83
- */
84
-
85
- var hotspotImage = _react.default.useMemo(() => {
86
- return (0, _get.default)(imageHotspotPathRoot, options === null || options === void 0 ? void 0 : options.hotspotImagePath);
87
- }, [imageHotspotPathRoot]);
88
-
89
- var displayImage = _react.default.useMemo(() => {
90
- var _hotspotImage$asset;
91
-
92
- var builder = (0, _imageUrl.default)(_client.default).dataset(_client.default.config().dataset);
93
-
94
- var urlFor = source => builder.image(source);
95
-
96
- if (hotspotImage !== null && hotspotImage !== void 0 && (_hotspotImage$asset = hotspotImage.asset) !== null && _hotspotImage$asset !== void 0 && _hotspotImage$asset._ref) {
97
- var _getImageDimensions = (0, _assetUtils.getImageDimensions)(hotspotImage.asset._ref),
98
- aspectRatio = _getImageDimensions.aspectRatio;
99
-
100
- var width = 1200;
101
- var height = Math.round(width / aspectRatio);
102
- var url = urlFor(hotspotImage).width(width).url();
103
- return {
104
- width,
105
- height,
106
- url
107
- };
108
- }
109
-
110
- return null;
111
- }, [hotspotImage]);
112
-
113
- var handleHotspotImageClick = _react.default.useCallback(event => {
114
- var nativeEvent = event.nativeEvent; // Calculate the x/y percentage of the click position
115
-
116
- var x = Number((nativeEvent.offsetX * 100 / nativeEvent.srcElement.width).toFixed(2));
117
- var y = Number((nativeEvent.offsetY * 100 / nativeEvent.srcElement.height).toFixed(2));
118
- var description = "New Hotspot at ".concat(x, "% x ").concat(y, "%");
119
- var newRow = {
120
- _key: (0, _content.randomKey)(12),
121
- _type: "spot",
122
- x,
123
- y
124
- };
125
-
126
- if (options !== null && options !== void 0 && options.hotspotDescriptionPath) {
127
- newRow[options.hotspotDescriptionPath] = description;
128
- }
129
-
130
- onChange(_PatchEvent.PatchEvent.from((0, _PatchEvent.setIfMissing)([]), (0, _PatchEvent.insert)([newRow], 'after', [-1])));
131
- }, []);
132
-
133
- var handleHotspotMove = _react.default.useCallback((key, x, y) => {
134
- onChange(_PatchEvent.PatchEvent.from( // Set the `x` value of this array key item
135
- (0, _PatchEvent.set)(x, [{
136
- _key: key
137
- }, 'x']), // Set the `y` value of this array key item
138
- (0, _PatchEvent.set)(y, [{
139
- _key: key
140
- }, 'y'])));
141
- }, [value]);
142
-
143
- var hotspotImageRef = _react.default.useRef(null);
144
-
145
- var _useState = (0, _react.useState)(),
146
- _useState2 = _slicedToArray(_useState, 2),
147
- imageRect = _useState2[0],
148
- setImageRect = _useState2[1];
149
-
150
- var updateImageRectCallback = (0, _web.useDebouncedCallback)(e => setImageRect(e.contentRect), [setImageRect], 200);
151
- (0, _web.useResizeObserver)(hotspotImageRef, updateImageRectCallback);
152
- return /*#__PURE__*/_react.default.createElement(_ui.Stack, {
153
- space: [2, 2, 3]
154
- }, displayImage !== null && displayImage !== void 0 && displayImage.url ? /*#__PURE__*/_react.default.createElement("div", {
155
- style: {
156
- position: "relative"
157
- }
158
- }, imageRect && (value === null || value === void 0 ? void 0 : value.length) > 0 && value.map((spot, index) => /*#__PURE__*/_react.default.createElement(_Spot.default, {
159
- index: index,
160
- key: spot._key,
161
- spot: spot,
162
- bounds: imageRect,
163
- update: handleHotspotMove,
164
- hotspotDescriptionPath: options === null || options === void 0 ? void 0 : options.hotspotDescriptionPath,
165
- tooltip: options === null || options === void 0 ? void 0 : options.hotspotTooltip
166
- })), /*#__PURE__*/_react.default.createElement(_ui.Card, {
167
- __unstable_checkered: true,
168
- shadow: 1
169
- }, /*#__PURE__*/_react.default.createElement(_ui.Flex, {
170
- align: "center",
171
- justify: "center"
172
- }, /*#__PURE__*/_react.default.createElement("img", {
173
- ref: hotspotImageRef,
174
- src: displayImage.url,
175
- width: displayImage.width,
176
- height: displayImage.height,
177
- alt: "",
178
- style: imageStyle,
179
- onClick: handleHotspotImageClick
180
- })))) : /*#__PURE__*/_react.default.createElement(_Feedback.default, null, type !== null && type !== void 0 && (_type$options = type.options) !== null && _type$options !== void 0 && _type$options.hotspotImagePath ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, "No Hotspot image found at path ", /*#__PURE__*/_react.default.createElement("code", null, type === null || type === void 0 ? void 0 : (_type$options2 = type.options) === null || _type$options2 === void 0 ? void 0 : _type$options2.hotspotImagePath)) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, "Define a path in this field using to the image field in this document at", ' ', /*#__PURE__*/_react.default.createElement("code", null, "options.hotspotImagePath"))), (type === null || type === void 0 ? void 0 : (_type$options3 = type.options) === null || _type$options3 === void 0 ? void 0 : _type$options3.imageHotspotPathRoot) && !VALID_ROOT_PATHS.includes(type.options.imageHotspotPathRoot) && /*#__PURE__*/_react.default.createElement(_Feedback.default, null, "The supplied imageHotspotPathRoot \"", type.options.imageHotspotPathRoot, "\" is not valid, falling back to \"document\". Available values are \"", VALID_ROOT_PATHS.join(', '), "\"."), /*#__PURE__*/_react.default.createElement(_FormBuilderInput.FormBuilderInput, _extends({}, props, {
181
- type: typeWithoutInputComponent,
182
- ref: ref
183
- })));
184
- });
185
-
186
- var _default = (0, _formBuilder.withDocument)(HotspotArray);
187
-
188
- exports.default = _default;
189
- //# sourceMappingURL=HotspotArray.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"HotspotArray.js","names":["imageStyle","width","height","VALID_ROOT_PATHS","HotspotArray","React","forwardRef","props","ref","type","value","onChange","document","options","typeWithoutInputComponent","useUnsetInputComponent","inputComponent","imageHotspotPathRoot","includes","hotspotImage","useMemo","get","hotspotImagePath","displayImage","builder","imageUrlBuilder","sanityClient","dataset","config","urlFor","source","image","asset","_ref","getImageDimensions","aspectRatio","Math","round","url","handleHotspotImageClick","useCallback","event","nativeEvent","x","Number","offsetX","srcElement","toFixed","y","offsetY","description","newRow","_key","randomKey","_type","hotspotDescriptionPath","PatchEvent","from","setIfMissing","insert","handleHotspotMove","key","set","hotspotImageRef","useRef","useState","imageRect","setImageRect","updateImageRectCallback","useDebouncedCallback","e","contentRect","useResizeObserver","position","length","map","spot","index","hotspotTooltip","join","withDocument"],"sources":["../src/HotspotArray.tsx"],"sourcesContent":["/* eslint-disable react/display-name */\n\n// @ts-ignore\nimport sanityClient from 'part:@sanity/base/client'\n// @ts-ignore\nimport {withDocument} from 'part:@sanity/form-builder'\n\nimport {getImageDimensions} from '@sanity/asset-utils'\nimport {FormBuilderInput} from '@sanity/form-builder/lib/FormBuilderInput'\nimport {insert, PatchEvent, set, setIfMissing} from '@sanity/form-builder/PatchEvent'\nimport imageUrlBuilder from '@sanity/image-url'\nimport {Card, Flex, Stack} from '@sanity/ui'\nimport {randomKey} from '@sanity/util/content'\nimport get from 'lodash/get'\nimport React, {useState} from 'react'\n\nimport {IUseResizeObserverCallback, useDebouncedCallback, useResizeObserver} from '@react-hookz/web'\nimport Feedback from './Feedback'\nimport Spot from './Spot'\nimport {useUnsetInputComponent} from './useUnsetInputComponent'\n\nconst imageStyle = {width: `100%`, height: `auto`}\n\nconst VALID_ROOT_PATHS = ['document', 'parent']\n\nexport type FnHotspotMove = (key: string, x: number, y: number) => void\n\nexport type TSpot = {\n _key: string\n _type: `spot`\n x: number\n y: number\n} & {[key: string]: unknown}\n\nconst HotspotArray = React.forwardRef((props: any, ref) => {\n const {type, value, onChange, document} = props\n const {options} = type ?? {}\n\n // Attempt prevention of infinite loop in <FormBuilderInput />\n // Re-renders can still occur if this Component is used again in a nested field\n const typeWithoutInputComponent = useUnsetInputComponent(type, type?.inputComponent)\n const imageHotspotPathRoot = VALID_ROOT_PATHS.includes(options?.imageHotspotPathRoot)\n ? props[options.imageHotspotPathRoot]\n : document\n\n /**\n * Finding the image from the imageHotspotPathRoot (defaults to document),\n * using the path from the hotspot's `options` field\n *\n * when changes in imageHotspotPathRoot (e.g. document) occur,\n * check if there are any changes to the hotspotImage and update the reference\n */\n const hotspotImage = React.useMemo(() => {\n return get(imageHotspotPathRoot, options?.hotspotImagePath)\n }, [imageHotspotPathRoot])\n\n const displayImage = React.useMemo(() => {\n const builder = imageUrlBuilder(sanityClient).dataset(sanityClient.config().dataset)\n const urlFor = (source) => builder.image(source)\n\n if (hotspotImage?.asset?._ref) {\n const {aspectRatio} = getImageDimensions(hotspotImage.asset._ref)\n const width = 1200\n const height = Math.round(width / aspectRatio)\n const url = urlFor(hotspotImage).width(width).url()\n\n return {width, height, url}\n }\n\n return null\n }, [hotspotImage])\n\n const handleHotspotImageClick = React.useCallback((event) => {\n const {nativeEvent} = event\n\n // Calculate the x/y percentage of the click position\n const x = Number(((nativeEvent.offsetX * 100) / nativeEvent.srcElement.width).toFixed(2))\n const y = Number(((nativeEvent.offsetY * 100) / nativeEvent.srcElement.height).toFixed(2))\n const description = `New Hotspot at ${x}% x ${y}%`\n\n const newRow: TSpot = {\n _key: randomKey(12),\n _type: `spot`,\n x,\n y,\n }\n\n if (options?.hotspotDescriptionPath) {\n newRow[options.hotspotDescriptionPath] = description\n }\n\n onChange(PatchEvent.from(setIfMissing([]), insert([newRow], 'after', [-1])))\n }, [])\n\n const handleHotspotMove: FnHotspotMove = React.useCallback(\n (key, x, y) => {\n onChange(\n PatchEvent.from(\n // Set the `x` value of this array key item\n set(x, [{_key: key}, 'x']),\n // Set the `y` value of this array key item\n set(y, [{_key: key}, 'y'])\n )\n )\n },\n [value]\n )\n\n const hotspotImageRef = React.useRef<HTMLImageElement | null>(null)\n\n const [imageRect, setImageRect] = useState<DOMRectReadOnly>()\n const updateImageRectCallback = useDebouncedCallback(\n ((e) => setImageRect(e.contentRect)) as IUseResizeObserverCallback,\n [setImageRect],\n 200\n )\n useResizeObserver(hotspotImageRef, updateImageRectCallback)\n\n return (\n <Stack space={[2, 2, 3]}>\n {displayImage?.url ? (\n <div style={{position: `relative`}}>\n {imageRect &&\n value?.length > 0 &&\n value.map((spot, index) => (\n <Spot\n index={index}\n key={spot._key}\n spot={spot}\n bounds={imageRect}\n update={handleHotspotMove}\n hotspotDescriptionPath={options?.hotspotDescriptionPath}\n tooltip={options?.hotspotTooltip}\n />\n ))}\n\n <Card __unstable_checkered shadow={1}>\n <Flex align=\"center\" justify=\"center\">\n <img\n ref={hotspotImageRef}\n src={displayImage.url}\n width={displayImage.width}\n height={displayImage.height}\n alt=\"\"\n style={imageStyle}\n onClick={handleHotspotImageClick}\n />\n </Flex>\n </Card>\n </div>\n ) : (\n <Feedback>\n {type?.options?.hotspotImagePath ? (\n <>\n No Hotspot image found at path <code>{type?.options?.hotspotImagePath}</code>\n </>\n ) : (\n <>\n Define a path in this field using to the image field in this document at{' '}\n <code>options.hotspotImagePath</code>\n </>\n )}\n </Feedback>\n )}\n {type?.options?.imageHotspotPathRoot &&\n !VALID_ROOT_PATHS.includes(type.options.imageHotspotPathRoot) && (\n <Feedback>\n The supplied imageHotspotPathRoot \"{type.options.imageHotspotPathRoot}\" is not valid,\n falling back to \"document\". Available values are \"{VALID_ROOT_PATHS.join(', ')}\".\n </Feedback>\n )}\n <FormBuilderInput {...props} type={typeWithoutInputComponent} ref={ref} />\n </Stack>\n )\n})\n\nexport default withDocument(HotspotArray)\n"],"mappings":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IAAI,SAAJ,IAAAA,IAAI,WAAJ,qBAAAA,IAAI,CAAEI,OAAN,wDAAeS,gBAAf,gBACC,4GACiC,2CAAOb,IAAP,aAAOA,IAAP,yCAAOA,IAAI,CAAEI,OAAb,mDAAO,eAAeS,gBAAtB,CADjC,CADD,gBAKC,wIAC2E,GAD3E,eAEE,sEAFF,CANJ,CAhCJ,EA6CG,CAAAb,IAAI,SAAJ,IAAAA,IAAI,WAAJ,8BAAAA,IAAI,CAAEI,OAAN,kEAAeI,oBAAf,KACC,CAACd,gBAAgB,CAACe,QAAjB,CAA0BT,IAAI,CAACI,OAAL,CAAaI,oBAAvC,CADF,iBAEG,6BAAC,iBAAD,gDACsCR,IAAI,CAACI,OAAL,CAAaI,oBADnD,4EAEqDd,gBAAgB,CAAC4E,IAAjB,CAAsB,IAAtB,CAFrD,QA/CN,eAoDE,6BAAC,kCAAD,eAAsBxE,KAAtB;IAA6B,IAAI,EAAEO,yBAAnC;IAA8D,GAAG,EAAEN;EAAnE,GApDF,CADF;AAwDD,CA5IoB,CAArB;;eA8Ie,IAAAwE,yBAAA,EAAa5E,YAAb,C"}
package/lib/Spot.js DELETED
@@ -1,181 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = Spot;
7
-
8
- var _ui = require("@sanity/ui");
9
-
10
- var _framerMotion = require("framer-motion");
11
-
12
- var _get = _interopRequireDefault(require("lodash/get"));
13
-
14
- var _react = _interopRequireWildcard(require("react"));
15
-
16
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
17
-
18
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
19
-
20
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
21
-
22
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
23
-
24
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
25
-
26
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
27
-
28
- function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
29
-
30
- 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."); }
31
-
32
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
33
-
34
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
35
-
36
- function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
37
-
38
- function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
39
-
40
- var dragStyle = {
41
- width: '1.4rem',
42
- height: '1.4rem',
43
- position: 'absolute',
44
- boxSizing: 'border-box',
45
- top: 0,
46
- left: 0,
47
- margin: '-0.7rem 0 0 -0.7rem',
48
- cursor: 'pointer',
49
- display: 'flex',
50
- justifyContent: 'center',
51
- alignItems: 'center',
52
- borderRadius: '50%',
53
- background: '#000',
54
- color: 'white'
55
- };
56
- var dragStyleWhileDrag = {
57
- background: 'rgba(0, 0, 0, 0.1)',
58
- border: '1px solid #fff',
59
- cursor: 'none'
60
- };
61
- var dragStyleWhileHover = {
62
- background: 'rgba(0, 0, 0, 0.1)',
63
- border: '1px solid #fff'
64
- };
65
- var dotStyle = {
66
- position: 'absolute',
67
- left: '50%',
68
- top: '50%',
69
- height: '0.2rem',
70
- width: '0.2rem',
71
- margin: '-0.1rem 0 0 -0.1rem',
72
- background: '#fff',
73
- visibility: 'hidden',
74
- borderRadius: '50%',
75
- // make sure pointer events only run on the parent
76
- pointerEvents: 'none'
77
- };
78
- var dotStyleWhileActive = {
79
- visibility: 'visible'
80
- };
81
- var labelStyle = {
82
- color: 'white',
83
- fontSize: '0.7rem',
84
- fontWeight: 600,
85
- lineHeight: '1'
86
- };
87
- var labelStyleWhileActive = {
88
- visibility: 'hidden'
89
- };
90
-
91
- var round = num => Math.round(num * 100) / 100;
92
-
93
- function Spot(_ref) {
94
- var spot = _ref.spot,
95
- bounds = _ref.bounds,
96
- update = _ref.update,
97
- hotspotDescriptionPath = _ref.hotspotDescriptionPath,
98
- tooltip = _ref.tooltip,
99
- index = _ref.index;
100
-
101
- var _React$useState = _react.default.useState(false),
102
- _React$useState2 = _slicedToArray(_React$useState, 2),
103
- isDragging = _React$useState2[0],
104
- setIsDragging = _React$useState2[1];
105
-
106
- var _React$useState3 = _react.default.useState(false),
107
- _React$useState4 = _slicedToArray(_React$useState3, 2),
108
- isHovering = _React$useState4[0],
109
- setIsHovering = _React$useState4[1]; // x/y are stored as % but need to be converted to px
110
-
111
-
112
- var x = (0, _framerMotion.useMotionValue)(round(bounds.width * (spot.x / 100)));
113
- var y = (0, _framerMotion.useMotionValue)(round(bounds.height * (spot.y / 100)));
114
- /**
115
- * update x/y if the bounds change when resizing the window
116
- */
117
-
118
- (0, _react.useEffect)(() => {
119
- x.set(round(bounds.width * (spot.x / 100)));
120
- y.set(round(bounds.height * (spot.y / 100)));
121
- }, [bounds]);
122
-
123
- var handleDragEnd = _react.default.useCallback(() => {
124
- setIsDragging(false); // get current values for x/y in px
125
-
126
- var currentX = x.get();
127
- var currentY = y.get(); // Which we need to convert back to `%` to patch the document
128
-
129
- var newX = round(currentX * 100 / bounds.width);
130
- var newY = round(currentY * 100 / bounds.height); // Don't go below 0 or above 100
131
-
132
- var safeX = Math.max(0, Math.min(100, newX));
133
- var safeY = Math.max(0, Math.min(100, newY));
134
- update(spot._key, safeX, safeY);
135
- }, [spot]);
136
-
137
- var handleDragStart = _react.default.useCallback(() => setIsDragging(true), []);
138
-
139
- var handleHoverStart = _react.default.useCallback(() => setIsHovering(true), []);
140
-
141
- var handleHoverEnd = _react.default.useCallback(() => setIsHovering(false), []);
142
-
143
- if (!x || !y) {
144
- return null;
145
- }
146
-
147
- return /*#__PURE__*/_react.default.createElement(_ui.Tooltip, {
148
- key: spot._key,
149
- disabled: isDragging,
150
- portal: true,
151
- content: tooltip && typeof tooltip === 'function' ? /*#__PURE__*/_react.default.createElement(tooltip, {
152
- spot
153
- }) : /*#__PURE__*/_react.default.createElement(_ui.Box, {
154
- padding: 2,
155
- style: {
156
- maxWidth: 200,
157
- pointerEvents: "none"
158
- }
159
- }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
160
- textOverflow: "ellipsis"
161
- }, hotspotDescriptionPath ? (0, _get.default)(spot, hotspotDescriptionPath) : "".concat(spot.x, "% x ").concat(spot.y, "%")))
162
- }, /*#__PURE__*/_react.default.createElement(_framerMotion.motion.div, {
163
- drag: true,
164
- dragConstraints: bounds,
165
- dragElastic: 0,
166
- dragMomentum: false,
167
- onDragEnd: handleDragEnd,
168
- onDragStart: handleDragStart,
169
- onHoverStart: handleHoverStart,
170
- onHoverEnd: handleHoverEnd,
171
- style: _objectSpread(_objectSpread(_objectSpread({}, dragStyle), {}, {
172
- x,
173
- y
174
- }, isDragging && _objectSpread({}, dragStyleWhileDrag)), isHovering && _objectSpread({}, dragStyleWhileHover))
175
- }, /*#__PURE__*/_react.default.createElement(_ui.Box, {
176
- style: _objectSpread(_objectSpread({}, dotStyle), (isDragging || isHovering) && _objectSpread({}, dotStyleWhileActive))
177
- }), /*#__PURE__*/_react.default.createElement("div", {
178
- style: _objectSpread(_objectSpread({}, labelStyle), (isDragging || isHovering) && _objectSpread({}, labelStyleWhileActive))
179
- }, index + 1)));
180
- }
181
- //# sourceMappingURL=Spot.js.map
package/lib/Spot.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"Spot.js","names":["dragStyle","width","height","position","boxSizing","top","left","margin","cursor","display","justifyContent","alignItems","borderRadius","background","color","dragStyleWhileDrag","border","dragStyleWhileHover","dotStyle","visibility","pointerEvents","dotStyleWhileActive","labelStyle","fontSize","fontWeight","lineHeight","labelStyleWhileActive","round","num","Math","Spot","spot","bounds","update","hotspotDescriptionPath","tooltip","index","React","useState","isDragging","setIsDragging","isHovering","setIsHovering","x","useMotionValue","y","useEffect","set","handleDragEnd","useCallback","currentX","get","currentY","newX","newY","safeX","max","min","safeY","_key","handleDragStart","handleHoverStart","handleHoverEnd","createElement","maxWidth"],"sources":["../src/Spot.tsx"],"sourcesContent":["import {Box, Text, Tooltip} from '@sanity/ui'\nimport {motion, useMotionValue} from 'framer-motion'\nimport get from 'lodash/get'\nimport React, {CSSProperties, ReactElement, useEffect} from 'react'\nimport {FnHotspotMove, TSpot} from './HotspotArray'\n\nconst dragStyle: CSSProperties = {\n width: '1.4rem',\n height: '1.4rem',\n position: 'absolute',\n boxSizing: 'border-box',\n top: 0,\n left: 0,\n margin: '-0.7rem 0 0 -0.7rem',\n cursor: 'pointer',\n display: 'flex',\n justifyContent: 'center',\n alignItems: 'center',\n borderRadius: '50%',\n background: '#000',\n color: 'white',\n}\n\nconst dragStyleWhileDrag: CSSProperties = {\n background: 'rgba(0, 0, 0, 0.1)',\n border: '1px solid #fff',\n cursor: 'none',\n}\n\nconst dragStyleWhileHover: CSSProperties = {\n background: 'rgba(0, 0, 0, 0.1)',\n border: '1px solid #fff',\n}\n\nconst dotStyle: CSSProperties = {\n position: 'absolute',\n left: '50%',\n top: '50%',\n height: '0.2rem',\n width: '0.2rem',\n margin: '-0.1rem 0 0 -0.1rem',\n background: '#fff',\n visibility: 'hidden',\n borderRadius: '50%',\n // make sure pointer events only run on the parent\n pointerEvents: 'none',\n}\n\nconst dotStyleWhileActive: CSSProperties = {\n visibility: 'visible',\n}\n\nconst labelStyle: CSSProperties = {\n color: 'white',\n fontSize: '0.7rem',\n fontWeight: 600,\n lineHeight: '1',\n}\n\nconst labelStyleWhileActive: CSSProperties = {\n visibility: 'hidden',\n}\n\nconst round = (num) => Math.round(num * 100) / 100\n\ninterface IHotspot {\n spot: TSpot\n bounds: DOMRectReadOnly\n update: FnHotspotMove\n hotspotDescriptionPath?: string\n tooltip?: ReactElement\n index: number\n}\n\nexport default function Spot({\n spot,\n bounds,\n update,\n hotspotDescriptionPath,\n tooltip,\n index,\n}: IHotspot) {\n const [isDragging, setIsDragging] = React.useState(false)\n const [isHovering, setIsHovering] = React.useState(false)\n\n // x/y are stored as % but need to be converted to px\n const x = useMotionValue(round(bounds.width * (spot.x / 100)))\n const y = useMotionValue(round(bounds.height * (spot.y / 100)))\n\n /**\n * update x/y if the bounds change when resizing the window\n */\n useEffect(() => {\n x.set(round(bounds.width * (spot.x / 100)))\n y.set(round(bounds.height * (spot.y / 100)))\n }, [bounds])\n\n const handleDragEnd = React.useCallback(() => {\n setIsDragging(false)\n\n // get current values for x/y in px\n const currentX = x.get()\n const currentY = y.get()\n\n // Which we need to convert back to `%` to patch the document\n const newX = round((currentX * 100) / bounds.width)\n const newY = round((currentY * 100) / bounds.height)\n\n // Don't go below 0 or above 100\n const safeX = Math.max(0, Math.min(100, newX))\n const safeY = Math.max(0, Math.min(100, newY))\n\n update(spot._key, safeX, safeY)\n }, [spot])\n const handleDragStart = React.useCallback(() => setIsDragging(true), [])\n\n const handleHoverStart = React.useCallback(() => setIsHovering(true), [])\n const handleHoverEnd = React.useCallback(() => setIsHovering(false), [])\n\n if (!x || !y) {\n return null\n }\n\n return (\n <Tooltip\n key={spot._key}\n disabled={isDragging}\n portal\n content={\n tooltip && typeof tooltip === 'function' ? (\n React.createElement(tooltip, {spot})\n ) : (\n <Box padding={2} style={{maxWidth: 200, pointerEvents: `none`}}>\n <Text textOverflow=\"ellipsis\">\n {hotspotDescriptionPath\n ? (get(spot, hotspotDescriptionPath) as string)\n : `${spot.x}% x ${spot.y}%`}\n </Text>\n </Box>\n )\n }\n >\n <motion.div\n drag\n dragConstraints={bounds}\n dragElastic={0}\n dragMomentum={false}\n onDragEnd={handleDragEnd}\n onDragStart={handleDragStart}\n onHoverStart={handleHoverStart}\n onHoverEnd={handleHoverEnd}\n style={{\n ...dragStyle,\n x,\n y,\n ...(isDragging && {...dragStyleWhileDrag}),\n ...(isHovering && {...dragStyleWhileHover}),\n }}\n >\n {/* Dot */}\n <Box\n style={{\n ...dotStyle,\n ...((isDragging || isHovering) && {...dotStyleWhileActive}),\n }}\n />\n {/* Label */}\n <div\n style={{\n ...labelStyle,\n ...((isDragging || isHovering) && {...labelStyleWhileActive}),\n }}\n >\n {index + 1}\n </div>\n </motion.div>\n </Tooltip>\n )\n}\n"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AACA;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,IAAMA,SAAwB,GAAG;EAC/BC,KAAK,EAAE,QADwB;EAE/BC,MAAM,EAAE,QAFuB;EAG/BC,QAAQ,EAAE,UAHqB;EAI/BC,SAAS,EAAE,YAJoB;EAK/BC,GAAG,EAAE,CAL0B;EAM/BC,IAAI,EAAE,CANyB;EAO/BC,MAAM,EAAE,qBAPuB;EAQ/BC,MAAM,EAAE,SARuB;EAS/BC,OAAO,EAAE,MATsB;EAU/BC,cAAc,EAAE,QAVe;EAW/BC,UAAU,EAAE,QAXmB;EAY/BC,YAAY,EAAE,KAZiB;EAa/BC,UAAU,EAAE,MAbmB;EAc/BC,KAAK,EAAE;AAdwB,CAAjC;AAiBA,IAAMC,kBAAiC,GAAG;EACxCF,UAAU,EAAE,oBAD4B;EAExCG,MAAM,EAAE,gBAFgC;EAGxCR,MAAM,EAAE;AAHgC,CAA1C;AAMA,IAAMS,mBAAkC,GAAG;EACzCJ,UAAU,EAAE,oBAD6B;EAEzCG,MAAM,EAAE;AAFiC,CAA3C;AAKA,IAAME,QAAuB,GAAG;EAC9Bf,QAAQ,EAAE,UADoB;EAE9BG,IAAI,EAAE,KAFwB;EAG9BD,GAAG,EAAE,KAHyB;EAI9BH,MAAM,EAAE,QAJsB;EAK9BD,KAAK,EAAE,QALuB;EA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E,KAAK,kCACAC,QADA,GAEC,CAACqB,UAAU,IAAIE,UAAf,uBAAkCpB,mBAAlC,CAFD;EADP,EAlBF,eAyBE;IACE,KAAK,kCACAC,UADA,GAEC,CAACiB,UAAU,IAAIE,UAAf,uBAAkCf,qBAAlC,CAFD;EADP,GAMGU,KAAK,GAAG,CANX,CAzBF,CAlBF,CADF;AAuDD"}
@@ -1,32 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.useUnsetInputComponent = useUnsetInputComponent;
7
-
8
- var _react = _interopRequireDefault(require("react"));
9
-
10
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
-
12
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
13
-
14
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
15
-
16
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
17
-
18
- function useUnsetInputComponent(type, component) {
19
- return _react.default.useMemo(() => unsetInputComponent(type, component), [type, component]);
20
- }
21
-
22
- function unsetInputComponent(type, component) {
23
- var t = _objectSpread(_objectSpread({}, type), {}, {
24
- inputComponent: type.inputComponent === component ? undefined : type.inputComponent
25
- });
26
-
27
- var typeOfType = t.type ? unsetInputComponent(t.type, component) : undefined;
28
- return _objectSpread(_objectSpread({}, t), {}, {
29
- type: typeOfType
30
- });
31
- }
32
- //# sourceMappingURL=useUnsetInputComponent.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useUnsetInputComponent.js","names":["useUnsetInputComponent","type","component","React","useMemo","unsetInputComponent","t","inputComponent","undefined","typeOfType"],"sources":["../src/useUnsetInputComponent.ts"],"sourcesContent":["import React from 'react'\n\nexport function useUnsetInputComponent(type, component) {\n return React.useMemo(() => unsetInputComponent(type, component), [type, component])\n}\n\nfunction unsetInputComponent(type, component) {\n const t = {\n ...type,\n inputComponent: type.inputComponent === component ? undefined : type.inputComponent,\n }\n const typeOfType = t.type ? unsetInputComponent(t.type, component) : undefined\n return {\n ...t,\n type: typeOfType,\n }\n}\n"],"mappings":";;;;;;;AAAA;;;;;;;;;;AAEO,SAASA,sBAAT,CAAgCC,IAAhC,EAAsCC,SAAtC,EAAiD;EACtD,OAAOC,cAAA,CAAMC,OAAN,CAAc,MAAMC,mBAAmB,CAACJ,IAAD,EAAOC,SAAP,CAAvC,EAA0D,CAACD,IAAD,EAAOC,SAAP,CAA1D,CAAP;AACD;;AAED,SAASG,mBAAT,CAA6BJ,IAA7B,EAAmCC,SAAnC,EAA8C;EAC5C,IAAMI,CAAC,mCACFL,IADE;IAELM,cAAc,EAAEN,IAAI,CAACM,cAAL,KAAwBL,SAAxB,GAAoCM,SAApC,GAAgDP,IAAI,CAACM;EAFhE,EAAP;;EAIA,IAAME,UAAU,GAAGH,CAAC,CAACL,IAAF,GAASI,mBAAmB,CAACC,CAAC,CAACL,IAAH,EAASC,SAAT,CAA5B,GAAkDM,SAArE;EACA,uCACKF,CADL;IAEEL,IAAI,EAAEQ;EAFR;AAID"}
@@ -1,177 +0,0 @@
1
- /* eslint-disable react/display-name */
2
-
3
- // @ts-ignore
4
- import sanityClient from 'part:@sanity/base/client'
5
- // @ts-ignore
6
- import {withDocument} from 'part:@sanity/form-builder'
7
-
8
- import {getImageDimensions} from '@sanity/asset-utils'
9
- import {FormBuilderInput} from '@sanity/form-builder/lib/FormBuilderInput'
10
- import {insert, PatchEvent, set, setIfMissing} from '@sanity/form-builder/PatchEvent'
11
- import imageUrlBuilder from '@sanity/image-url'
12
- import {Card, Flex, Stack} from '@sanity/ui'
13
- import {randomKey} from '@sanity/util/content'
14
- import get from 'lodash/get'
15
- import React, {useState} from 'react'
16
-
17
- import {IUseResizeObserverCallback, useDebouncedCallback, useResizeObserver} from '@react-hookz/web'
18
- import Feedback from './Feedback'
19
- import Spot from './Spot'
20
- import {useUnsetInputComponent} from './useUnsetInputComponent'
21
-
22
- const imageStyle = {width: `100%`, height: `auto`}
23
-
24
- const VALID_ROOT_PATHS = ['document', 'parent']
25
-
26
- export type FnHotspotMove = (key: string, x: number, y: number) => void
27
-
28
- export type TSpot = {
29
- _key: string
30
- _type: `spot`
31
- x: number
32
- y: number
33
- } & {[key: string]: unknown}
34
-
35
- const HotspotArray = React.forwardRef((props: any, ref) => {
36
- const {type, value, onChange, document} = props
37
- const {options} = type ?? {}
38
-
39
- // Attempt prevention of infinite loop in <FormBuilderInput />
40
- // Re-renders can still occur if this Component is used again in a nested field
41
- const typeWithoutInputComponent = useUnsetInputComponent(type, type?.inputComponent)
42
- const imageHotspotPathRoot = VALID_ROOT_PATHS.includes(options?.imageHotspotPathRoot)
43
- ? props[options.imageHotspotPathRoot]
44
- : document
45
-
46
- /**
47
- * Finding the image from the imageHotspotPathRoot (defaults to document),
48
- * using the path from the hotspot's `options` field
49
- *
50
- * when changes in imageHotspotPathRoot (e.g. document) occur,
51
- * check if there are any changes to the hotspotImage and update the reference
52
- */
53
- const hotspotImage = React.useMemo(() => {
54
- return get(imageHotspotPathRoot, options?.hotspotImagePath)
55
- }, [imageHotspotPathRoot])
56
-
57
- const displayImage = React.useMemo(() => {
58
- const builder = imageUrlBuilder(sanityClient).dataset(sanityClient.config().dataset)
59
- const urlFor = (source) => builder.image(source)
60
-
61
- if (hotspotImage?.asset?._ref) {
62
- const {aspectRatio} = getImageDimensions(hotspotImage.asset._ref)
63
- const width = 1200
64
- const height = Math.round(width / aspectRatio)
65
- const url = urlFor(hotspotImage).width(width).url()
66
-
67
- return {width, height, url}
68
- }
69
-
70
- return null
71
- }, [hotspotImage])
72
-
73
- const handleHotspotImageClick = React.useCallback((event) => {
74
- const {nativeEvent} = event
75
-
76
- // Calculate the x/y percentage of the click position
77
- const x = Number(((nativeEvent.offsetX * 100) / nativeEvent.srcElement.width).toFixed(2))
78
- const y = Number(((nativeEvent.offsetY * 100) / nativeEvent.srcElement.height).toFixed(2))
79
- const description = `New Hotspot at ${x}% x ${y}%`
80
-
81
- const newRow: TSpot = {
82
- _key: randomKey(12),
83
- _type: `spot`,
84
- x,
85
- y,
86
- }
87
-
88
- if (options?.hotspotDescriptionPath) {
89
- newRow[options.hotspotDescriptionPath] = description
90
- }
91
-
92
- onChange(PatchEvent.from(setIfMissing([]), insert([newRow], 'after', [-1])))
93
- }, [])
94
-
95
- const handleHotspotMove: FnHotspotMove = React.useCallback(
96
- (key, x, y) => {
97
- onChange(
98
- PatchEvent.from(
99
- // Set the `x` value of this array key item
100
- set(x, [{_key: key}, 'x']),
101
- // Set the `y` value of this array key item
102
- set(y, [{_key: key}, 'y'])
103
- )
104
- )
105
- },
106
- [value]
107
- )
108
-
109
- const hotspotImageRef = React.useRef<HTMLImageElement | null>(null)
110
-
111
- const [imageRect, setImageRect] = useState<DOMRectReadOnly>()
112
- const updateImageRectCallback = useDebouncedCallback(
113
- ((e) => setImageRect(e.contentRect)) as IUseResizeObserverCallback,
114
- [setImageRect],
115
- 200
116
- )
117
- useResizeObserver(hotspotImageRef, updateImageRectCallback)
118
-
119
- return (
120
- <Stack space={[2, 2, 3]}>
121
- {displayImage?.url ? (
122
- <div style={{position: `relative`}}>
123
- {imageRect &&
124
- value?.length > 0 &&
125
- value.map((spot, index) => (
126
- <Spot
127
- index={index}
128
- key={spot._key}
129
- spot={spot}
130
- bounds={imageRect}
131
- update={handleHotspotMove}
132
- hotspotDescriptionPath={options?.hotspotDescriptionPath}
133
- tooltip={options?.hotspotTooltip}
134
- />
135
- ))}
136
-
137
- <Card __unstable_checkered shadow={1}>
138
- <Flex align="center" justify="center">
139
- <img
140
- ref={hotspotImageRef}
141
- src={displayImage.url}
142
- width={displayImage.width}
143
- height={displayImage.height}
144
- alt=""
145
- style={imageStyle}
146
- onClick={handleHotspotImageClick}
147
- />
148
- </Flex>
149
- </Card>
150
- </div>
151
- ) : (
152
- <Feedback>
153
- {type?.options?.hotspotImagePath ? (
154
- <>
155
- No Hotspot image found at path <code>{type?.options?.hotspotImagePath}</code>
156
- </>
157
- ) : (
158
- <>
159
- Define a path in this field using to the image field in this document at{' '}
160
- <code>options.hotspotImagePath</code>
161
- </>
162
- )}
163
- </Feedback>
164
- )}
165
- {type?.options?.imageHotspotPathRoot &&
166
- !VALID_ROOT_PATHS.includes(type.options.imageHotspotPathRoot) && (
167
- <Feedback>
168
- The supplied imageHotspotPathRoot "{type.options.imageHotspotPathRoot}" is not valid,
169
- falling back to "document". Available values are "{VALID_ROOT_PATHS.join(', ')}".
170
- </Feedback>
171
- )}
172
- <FormBuilderInput {...props} type={typeWithoutInputComponent} ref={ref} />
173
- </Stack>
174
- )
175
- })
176
-
177
- export default withDocument(HotspotArray)