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

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/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,YAJ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AK,OAAO,EAAE,CAAd;MAAiB,KAAK,EAAE;QAACiC,QAAQ,EAAE,GAAX;QAAgB5C,aAAa;MAA7B;IAAxB,gBACE,6BAAC,QAAD;MAAM,YAAY,EAAC;IAAnB,GACGc,sBAAsB,GAClB,IAAAiB,YAAA,EAAIpB,IAAJ,EAAUG,sBAAV,CADkB,aAEhBH,IAAI,CAACY,CAFW,iBAEHZ,IAAI,CAACc,CAFF,MADzB,CADF;EARN,gBAkBE,6BAAC,oBAAD,CAAQ,GAAR;IACE,IAAI,MADN;IAEE,eAAe,EAAEb,MAFnB;IAGE,WAAW,EAAE,CAHf;IAIE,YAAY,EAAE,KAJhB;IAKE,SAAS,EAAEgB,aALb;IAME,WAAW,EAAEY,eANf;IAOE,YAAY,EAAEC,gBAPhB;IAQE,UAAU,EAAEC,cARd;IASE,KAAK,gDACA9D,SADA;MAEH2C,CAFG;MAGHE;IAHG,GAICN,UAAU,sBAAQxB,kBAAR,CAJX,GAKC0B,UAAU,sBAAQxB,mBAAR,CALX;EATP,gBAkBE,6BAAC,OAAD;IACE,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)
@@ -1,17 +0,0 @@
1
- import React from 'react'
2
-
3
- export function useUnsetInputComponent(type, component) {
4
- return React.useMemo(() => unsetInputComponent(type, component), [type, component])
5
- }
6
-
7
- function unsetInputComponent(type, component) {
8
- const t = {
9
- ...type,
10
- inputComponent: type.inputComponent === component ? undefined : type.inputComponent,
11
- }
12
- const typeOfType = t.type ? unsetInputComponent(t.type, component) : undefined
13
- return {
14
- ...t,
15
- type: typeOfType,
16
- }
17
- }