UNPKG

@sanity/form-builder

Version:
87 lines (84 loc) 4.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useObjectEditData = useObjectEditData; var _portableTextEditor = require("@sanity/portable-text-editor"); var _types = require("@sanity/types"); var _react = require("react"); function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } 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."); } 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); } 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; } function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } /* eslint-disable complexity */ // This hook will gather the info we need to create a edit modal for some embedded object in the editor // based on the current focusPath details. function useObjectEditData(focusPath, refs) { var editor = (0, _portableTextEditor.usePortableTextEditor)(); var blockKey = focusPath && (0, _types.isKeySegment)(focusPath[0]) ? focusPath[0]._key : undefined; var isChild = blockKey && focusPath && focusPath[1] === 'children' && (0, _types.isKeyedObject)(focusPath[2]); var childKey = isChild && (0, _types.isKeyedObject)(focusPath[2]) ? focusPath[2]._key : undefined; var isAnnotation = blockKey && focusPath[1] === 'markDefs'; var markDefKey = isAnnotation && (0, _types.isKeyedObject)(focusPath[2]) ? focusPath[2]._key : undefined; var focusPathLength = focusPath.length; return (0, _react.useMemo)(() => { // Annotations if (markDefKey) { var _PortableTextEditor$f = _portableTextEditor.PortableTextEditor.findByPath(editor, [{ _key: blockKey }]), _PortableTextEditor$f2 = _slicedToArray(_PortableTextEditor$f, 1), node = _PortableTextEditor$f2[0]; var block = node ? node : undefined; if (markDefKey) { var span = block.children.find(child => Array.isArray(child.marks) && child.marks.includes(markDefKey)); if (span) { return { editorPath: [{ _key: blockKey }, 'children', { _key: span._key }], formBuilderPath: [{ _key: blockKey }, 'markDefs', { _key: markDefKey }], kind: 'annotation', editorHTMLElementRef: refs.child }; } } } // Inline object if (blockKey && childKey && focusPathLength > 3) { var path = [{ _key: blockKey }, 'children', { _key: childKey }]; return { editorPath: path, formBuilderPath: path, kind: 'inlineObject', editorHTMLElementRef: refs.child }; } // Block object if (blockKey && !childKey && focusPathLength > 1) { var _path = [{ _key: blockKey }]; return { editorPath: _path, formBuilderPath: _path, kind: 'blockObject', editorHTMLElementRef: refs.block }; } refs.child.current = null; refs.block.current = null; return null; }, [editor, blockKey, childKey, markDefKey, refs.block, refs.child, focusPathLength]); }