@sanity/visual-editing
Version:
[](https://npm-stat.com/charts.html?package=@sanity/visual-editing) [](https://
1 lines • 12.1 kB
Source Map (JSON)
{"version":3,"file":"index.cjs","sources":["../../src/overlay-components/components/UnionInsertMenuOverlay.tsx","../../src/overlay-components/defineOverlayComponents.ts","../../src/overlay-components/defineOverlayComponent.ts"],"sourcesContent":["import type {SchemaNode, SchemaUnionNode} from '@repo/visual-editing-helpers'\nimport {AddIcon} from '@sanity/icons'\nimport type {SchemaType} from '@sanity/types'\nimport {Button, Flex} from '@sanity/ui'\nimport {\n useCallback,\n useRef,\n useState,\n type FunctionComponent,\n type HTMLProps,\n type MouseEvent,\n} from 'react'\nimport {styled} from 'styled-components'\nimport {useDocuments} from '../../react/useDocuments'\nimport type {ElementNode, OverlayComponent} from '../../types'\nimport {getArrayInsertPatches} from '../../util/mutations'\nimport {InsertMenuPopover} from './InsertMenu'\n\nconst AddButton = styled(Button)`\n position: relative;\n transform: var(--add-button-position);\n\n --add-button-position: translateY(0);\n [data-position='top'] & {\n --add-button-position: translateY(-50%);\n }\n [data-position='right'] & {\n --add-button-position: translateX(50%);\n }\n [data-position='bottom'] & {\n --add-button-position: translateY(50%);\n }\n [data-position='left'] & {\n --add-button-position: translateX(-50%);\n }\n`\nconst HoverAreaRoot = styled(Flex)`\n pointer-events: all;\n height: var(--hover-area-height);\n width: var(--hover-area-width);\n\n --hover-area-height: 100%;\n --hover-area-width: 100%;\n &[data-position='top'],\n &[data-position='bottom'] {\n --hover-area-height: 48px;\n }\n &[data-position='right'],\n &[data-position='left'] {\n --hover-area-width: 48px;\n }\n`\n\nconst HoverArea: FunctionComponent<{\n element: ElementNode\n hoverAreaExtent: HTMLProps<HTMLDivElement>['height' | 'width']\n node: SchemaUnionNode\n onAddUnion: (insertPosition: 'before' | 'after', name: string) => void\n position: 'top' | 'right' | 'bottom' | 'left'\n}> = (props) => {\n const {element, hoverAreaExtent, node, onAddUnion, position} = props\n const [showButton, setShowButton] = useState(false)\n const onEnter = useCallback(() => {\n setShowButton(true)\n }, [])\n const onLeave = useCallback(() => {\n setShowButton(false)\n }, [])\n const ref = useRef<HTMLDivElement | null>(null)\n\n // This function clones and dispatches MouseEvents so that they can be handled\n // by the underlying element. This is useful because we want to handle hover\n // events on the overlay element to display the add button, but let the\n // underlying element handle click events, drag and drop, etc.\n const relayEventToElement = useCallback(\n (event: MouseEvent<HTMLDivElement>) => {\n if (event.target === ref.current) {\n const newEvent = new MouseEvent(event.type, {\n ...event.nativeEvent,\n bubbles: true,\n cancelable: true,\n })\n element.dispatchEvent(newEvent)\n }\n },\n [element],\n )\n\n // The element that the popover containing the InsertMenu will be positioned\n // relative to (in this case, the AddButton).\n const [popoverReferenceElement, setPopoverReferenceElement] = useState<HTMLElement | null>(null)\n\n const [menuVisible, setMenuVisible] = useState(false)\n\n const dismissPortal = useCallback(() => {\n setMenuVisible(false)\n setShowButton(false)\n }, [])\n\n const onSelect = useCallback(\n (schemaType: SchemaType) => {\n setMenuVisible(false)\n const insertPosition = position === 'top' || position === 'left' ? 'before' : 'after'\n onAddUnion(insertPosition, schemaType.name)\n },\n [onAddUnion, position],\n )\n\n const align = position === 'top' ? 'flex-start' : position === 'bottom' ? 'flex-end' : 'center'\n const justify = position === 'left' ? 'flex-start' : position === 'right' ? 'flex-end' : 'center'\n const blockDirection = position === 'top' || position === 'bottom' ? 'height' : 'width'\n\n return (\n <HoverAreaRoot\n align={align}\n data-position={position}\n data-sanity-overlay-element\n justify={justify}\n onClick={relayEventToElement}\n onContextMenu={relayEventToElement}\n onMouseDown={relayEventToElement}\n onMouseEnter={onEnter}\n onMouseLeave={onLeave}\n onMouseUp={relayEventToElement}\n ref={ref}\n style={{\n [blockDirection]: hoverAreaExtent,\n }}\n >\n {(showButton || menuVisible) && (\n <AddButton\n ref={setPopoverReferenceElement}\n icon={AddIcon}\n mode={'ghost'}\n onClick={() => setMenuVisible((visible) => !visible)}\n radius={'full'}\n selected={menuVisible}\n />\n )}\n {menuVisible && popoverReferenceElement && (\n <InsertMenuPopover\n node={node}\n onDismiss={dismissPortal}\n referenceElement={popoverReferenceElement}\n onSelect={onSelect}\n />\n )}\n </HoverAreaRoot>\n )\n}\n\nexport const UnionInsertMenuOverlay: OverlayComponent<\n {\n direction?: 'horizontal' | 'vertical'\n hoverAreaExtent?: HTMLProps<HTMLDivElement>['height' | 'width']\n },\n SchemaUnionNode<SchemaNode>\n> = (props) => {\n const {direction = 'vertical', element, hoverAreaExtent, node, parent} = props\n\n const {getDocument} = useDocuments()\n\n const onAddUnion = useCallback(\n (insertPosition: 'before' | 'after', name: string) => {\n const doc = getDocument(node.id)\n const patches = getArrayInsertPatches(node, name, insertPosition)\n doc.patch(patches)\n },\n [getDocument, node],\n )\n\n if (!parent) return null\n\n return (\n <Flex\n height=\"fill\"\n width=\"fill\"\n direction={direction === 'horizontal' ? 'row' : 'column'}\n justify=\"space-between\"\n >\n <HoverArea\n element={element}\n hoverAreaExtent={hoverAreaExtent}\n node={parent}\n onAddUnion={onAddUnion}\n position={direction === 'horizontal' ? 'left' : 'top'}\n />\n <HoverArea\n element={element}\n hoverAreaExtent={hoverAreaExtent}\n node={parent}\n onAddUnion={onAddUnion}\n position={direction === 'horizontal' ? 'right' : 'bottom'}\n />\n </Flex>\n )\n}\n\nexport default UnionInsertMenuOverlay\n","import type {OverlayComponent, OverlayComponentResolver} from '../types'\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function defineOverlayComponents<T extends OverlayComponent>(\n resolver: OverlayComponentResolver<T>,\n): typeof resolver {\n return resolver\n}\n","import type {ComponentProps} from 'react'\nimport type {OverlayComponent, OverlayComponentProps} from '../types'\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function defineOverlayComponent<T extends OverlayComponent<Record<string, unknown>, any>>(\n component: T,\n props?: Omit<ComponentProps<T>, keyof OverlayComponentProps>,\n): {component: T; props: typeof props} {\n return {\n component: component,\n props: props,\n }\n}\n"],"names":["Object","defineProperty","exports","value","PointerEvents","require","jsxRuntime","reactCompilerRuntime","React","mutations","AddButton","styled","Button","HoverAreaRoot","Flex","HoverArea","props","$","_c","element","hoverAreaExtent","node","onAddUnion","position","showButton","setShowButton","useState","t0","Symbol","for","onEnter","t1","onLeave","ref","useRef","t2","event","target","current","newEvent","MouseEvent","type","nativeEvent","bubbles","cancelable","dispatchEvent","relayEventToElement","popoverReferenceElement","setPopoverReferenceElement","menuVisible","setMenuVisible","t3","dismissPortal","t4","schemaType","name","onSelect","align","justify","blockDirection","t5","t6","t7","t8","AddIcon","mode","onClick","_temp","radius","jsx","InsertMenuPopover","jsxs","style","visible","P","UnionInsertMenuOverlay","direction","parent","undefined","getDocument","useDocuments","insertPosition","doc","id","patches","getArrayInsertPatches","patch","height","width","defineOverlayComponent","component","defineOverlayComponents","resolver"],"mappings":"aAkBAA,OAAAC,eAAAC,QAAA,aAAA,CAAAC,OAAA,IAAA,IAAAC,EAAAC,QAAA,oCAAAC,EAAAD,QAAA,qBAAAE,EAAAF,QAAA,0BAAAG,EAAAH,QAAA,SAAAI,EAAAJ,QAAA,gCAAA,MAAMK,EAAYC,IAAOC,IAAM;;;;;;;;;;;;;;;;;EAkBzBC,EAAgBF,IAAOG,IAAI;;;;;;;;;;;;;;;EAiB3BC,EAMDC,IAAAC,MAAAA,EAAAC,IAAA,KACHC,QAAAA,EAAAA,gBAAAC,EAAAA,KAAAC,EAAAA,WAAAC,EAAAA,SAAAC,GAA+DP,GAC/DQ,EAAAC,GAAoCC,EAAAA,aAAeC,IAAAA,EAAAV,EAAA,KAAAW,OAAAC,IAAA,8BACvBF,EAAAA,KAC1BF,KAAkB,EACnBR,KAAAU,GAAAA,EAAAV,EAAA,GAFD,MAAAa,EAAgBH,EAEVI,IAAAA,EAAAd,EAAA,KAAAW,OAAAC,IAAA,8BACsBE,EAAAA,KAC1BN,KAAmB,EACpBR,KAAAc,GAAAA,EAAAd,EAAA,GAFD,MAAAe,EAAgBD,EAGhBE,EAAYC,EAAAA,OAAA,MAAmCC,IAAAA,EA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