Skip to content

Commit

Permalink
refactor(core/form/inputs): use child presence hook for PT-input nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
skogsmaskin committed May 30, 2023
1 parent f4a113f commit 145c573
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {useMemberValidation} from '../hooks/useMemberValidation'
import {usePortableTextMarkers} from '../hooks/usePortableTextMarkers'
import {usePortableTextMemberItem} from '../hooks/usePortableTextMembers'
import {debugRender} from '../debugRender'
import {useChildPresence} from '../../../studio/contexts/Presence'
import {EMPTY_ARRAY} from '../../../../util'
import {AnnotationToolbarPopover} from './AnnotationToolbarPopover'
import {Root, TooltipBox} from './Annotation.styles'
Expand Down Expand Up @@ -111,7 +112,11 @@ export function Annotation(props: AnnotationProps) {
[Markers, markers, renderCustomMarkers, text, validation]
)

const presence = memberItem?.node.presence || EMPTY_ARRAY
const presence = useChildPresence(path, true)
const rootPresence = useMemo(
() => presence.filter((p) => isEqual(p.path, path)),
[path, presence]
)

const isOpen = Boolean(memberItem?.member.open)
const input = memberItem?.input
Expand All @@ -133,7 +138,7 @@ export function Annotation(props: AnnotationProps) {
open: isOpen,
parentSchemaType: editor.schemaTypes.block,
path: nodePath,
presence,
presence: rootPresence,
readOnly: Boolean(readOnly),
renderDefault: DefaultAnnotationComponent,
schemaType,
Expand All @@ -156,9 +161,9 @@ export function Annotation(props: AnnotationProps) {
onOpen,
onPathFocus,
onRemove,
presence,
readOnly,
referenceElement,
rootPresence,
schemaType,
selected,
text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import React, {
useRef,
useState,
} from 'react'
import {isEqual} from '@sanity/util/paths'
import {PatchArg} from '../../../patch'
import {BlockProps, RenderCustomMarkers, RenderPreviewCallback} from '../../../types'
import {RenderBlockActionsCallback} from '../types'
Expand All @@ -27,6 +28,7 @@ import {usePortableTextMemberItem} from '../hooks/usePortableTextMembers'
import {pathToString} from '../../../../field'
import {debugRender} from '../debugRender'
import {EMPTY_ARRAY} from '../../../../util'
import {useChildPresence} from '../../../studio/contexts/Presence'
import {
Root,
ChangeIndicatorWrapper,
Expand Down Expand Up @@ -160,7 +162,11 @@ export function BlockObject(props: BlockObjectProps) {
const parentSchemaType = editor.schemaTypes.portableText
const hasMarkers = Boolean(markers.length > 0)

const presence = memberItem?.node.presence || EMPTY_ARRAY
const presence = useChildPresence(path, true)
const rootPresence = useMemo(
() => presence.filter((p) => isEqual(p.path, path)),
[path, presence]
)

// Tooltip indicating validation errors, warnings, info and markers
const tooltipEnabled = hasError || hasWarning || hasInfo || hasMarkers
Expand Down Expand Up @@ -199,7 +205,7 @@ export function BlockObject(props: BlockObjectProps) {
open: isOpen,
parentSchemaType,
path: nodePath,
presence,
presence: rootPresence,
readOnly: Boolean(readOnly),
renderDefault: DefaultBlockObjectComponent,
renderPreview,
Expand All @@ -221,7 +227,7 @@ export function BlockObject(props: BlockObjectProps) {
isOpen,
parentSchemaType,
nodePath,
presence,
rootPresence,
readOnly,
renderPreview,
schemaType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import {
import {ObjectSchemaType, Path, PortableTextBlock, PortableTextChild} from '@sanity/types'
import React, {useCallback, useEffect, useMemo, useState} from 'react'
import {Tooltip} from '@sanity/ui'
import {isEqual} from '@sanity/util/paths'
import {BlockProps, RenderCustomMarkers, RenderPreviewCallback} from '../../../types'
import {useFormBuilder} from '../../../useFormBuilder'
import {usePortableTextMarkers} from '../hooks/usePortableTextMarkers'
import {useMemberValidation} from '../hooks/useMemberValidation'
import {usePortableTextMemberItem} from '../hooks/usePortableTextMembers'
import {pathToString} from '../../../../field/paths'
import {EMPTY_ARRAY} from '../../../../util'
import {useChildPresence} from '../../../studio/contexts/Presence'
import {InlineObjectToolbarPopover} from './InlineObjectToolbarPopover'
import {ObjectEditModal} from './modals/ObjectEditModal'
import {PreviewSpan, Root, TooltipBox} from './InlineObject.styles'
Expand Down Expand Up @@ -89,7 +91,11 @@ export const InlineObject = (props: InlineObjectProps) => {
const nodePath = memberItem?.node.path || EMPTY_ARRAY
const referenceElement = memberItem?.elementRef?.current

const presence = memberItem?.node.presence || EMPTY_ARRAY
const presence = useChildPresence(path, true)
const rootPresence = useMemo(
() => presence.filter((p) => isEqual(p.path, path)),
[path, presence]
)

const componentProps: BlockProps | undefined = useMemo(
() => ({
Expand All @@ -106,7 +112,7 @@ export const InlineObject = (props: InlineObjectProps) => {
member: memberItem?.member,
parentSchemaType,
path: nodePath,
presence,
presence: rootPresence,
readOnly: Boolean(readOnly),
renderDefault: DefaultInlineObjectComponent,
renderPreview,
Expand All @@ -128,10 +134,10 @@ export const InlineObject = (props: InlineObjectProps) => {
onPathFocus,
onRemove,
parentSchemaType,
presence,
readOnly,
referenceElement,
renderPreview,
rootPresence,
schemaType,
selected,
validation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
PortableTextEditor,
usePortableTextEditor,
} from '@sanity/portable-text-editor'
import {isEqual} from '@sanity/util/paths'
import {BlockProps, RenderCustomMarkers, RenderPreviewCallback} from '../../../types'
import {PatchArg} from '../../../patch'
import {useFormBuilder} from '../../../useFormBuilder'
Expand All @@ -18,6 +19,7 @@ import {usePortableTextMemberItem} from '../hooks/usePortableTextMembers'
import {pathToString} from '../../../../field'
import {debugRender} from '../debugRender'
import {EMPTY_ARRAY} from '../../../../util'
import {useChildPresence} from '../../../studio/contexts/Presence'
import {TEXT_STYLE_PADDING} from './constants'
import {
BlockActionsInner,
Expand Down Expand Up @@ -79,6 +81,17 @@ export function TextBlock(props: TextBlockProps) {
const memberItem = usePortableTextMemberItem(pathToString(path))
const editor = usePortableTextEditor()

const presence = useChildPresence(path, true)
// Include all presence paths pointing either directly to a block, or directly to a block child
// (which is where the user most of the time would have the presence in a text block)
const textPresence = useMemo(() => {
return presence.filter(
(p) =>
isEqual(p.path, path) ||
(p.path.slice(-3)[1] === 'children' && p.path.length - path.length === 2)
)
}, [path, presence])

const handleChangeIndicatorMouseEnter = useCallback(() => setReviewChangesHovered(true), [])
const handleChangeIndicatorMouseLeave = useCallback(() => setReviewChangesHovered(false), [])

Expand Down Expand Up @@ -147,7 +160,6 @@ export function TextBlock(props: TextBlockProps) {

const isOpen = Boolean(memberItem?.member.open)
const parentSchemaType = editor.schemaTypes.portableText
const presence = memberItem?.node.presence || EMPTY_ARRAY

const CustomComponent = schemaType.components?.block as ComponentType<BlockProps> | undefined
const componentProps: BlockProps = useMemo(
Expand All @@ -164,7 +176,7 @@ export function TextBlock(props: TextBlockProps) {
open: isOpen,
parentSchemaType,
path: memberItem?.node.path || EMPTY_ARRAY,
presence,
presence: textPresence,
readOnly: Boolean(readOnly),
renderDefault: DefaultComponent,
renderPreview,
Expand All @@ -184,12 +196,12 @@ export function TextBlock(props: TextBlockProps) {
onPathFocus,
onRemove,
parentSchemaType,
presence,
readOnly,
renderPreview,
schemaType,
selected,
text,
textPresence,
validation,
value,
]
Expand Down

2 comments on commit 145c573

@vercel
Copy link

@vercel vercel bot commented on 145c573 May 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

performance-studio – ./

performance-studio-git-next.sanity.build
performance-studio.sanity.build

@vercel
Copy link

@vercel vercel bot commented on 145c573 May 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

test-studio – ./

test-studio-git-next.sanity.build
test-studio.sanity.build

Please sign in to comment.