/
InlineObject.tsx
198 lines (178 loc) · 5.19 KB
/
InlineObject.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import {ChevronDownIcon} from '@sanity/icons'
import {
isKeySegment,
ObjectSchemaType,
Path,
PortableTextChild,
PortableTextObject,
} from '@sanity/types'
import {Card, Flex, Label, Popover, useClickOutside} from '@sanity/ui'
import {FOCUS_TERMINATOR, toString} from '@sanity/util/paths'
import React, {useCallback, useContext, useState, useEffect, useMemo} from 'react'
import styled from 'styled-components'
import {ChangeList, DiffContext, DiffTooltip, useDiffAnnotationColor} from '../../../../diff'
import {ObjectDiff} from '../../../../types'
import {isEmptyObject} from '../helpers'
import {ConnectorContext, useReportedValues} from '../../../../../changeIndicators'
import {Preview} from '../../../../../preview/components/Preview'
import {InlineBox, InlineText, PopoverContainer, PreviewContainer} from './styledComponents'
interface InlineObjectProps {
diff?: ObjectDiff
object: PortableTextObject
path: Path
schemaType?: ObjectSchemaType
}
const InlineObjectWrapper = styled(Card)`
&:not([hidden]) {
display: inline;
cursor: pointer;
white-space: nowrap;
align-items: center;
&[data-removed] {
text-decoration: line-through;
}
${InlineBox} {
display: inline-flex;
}
}
`
export function InlineObject({diff, object, schemaType, ...restProps}: InlineObjectProps) {
if (!schemaType) {
return (
<InlineObjectWrapper {...restProps} border radius={1}>
Unknown schema type: {object._type}
</InlineObjectWrapper>
)
}
if (diff) {
return (
<InlineObjectWithDiff {...restProps} diff={diff} object={object} schemaType={schemaType} />
)
}
return (
<InlineObjectWrapper>
<Preview schemaType={schemaType} value={object} layout="inline" />
</InlineObjectWrapper>
)
}
interface InlineObjectWithDiffProps {
diff: ObjectDiff
object: PortableTextChild
path: Path
schemaType: ObjectSchemaType
}
function InlineObjectWithDiff({
diff,
object,
path,
schemaType,
...restProps
}: InlineObjectWithDiffProps) {
const {path: fullPath} = useContext(DiffContext)
const {onSetFocus} = useContext(ConnectorContext)
const color = useDiffAnnotationColor(diff, [])
const style = useMemo(
() => (color ? {background: color.background, color: color.text} : {}),
[color]
)
const [open, setOpen] = useState(false)
const emptyObject = object && isEmptyObject(object)
const isRemoved = diff.action === 'removed'
const prefix = fullPath.slice(
0,
fullPath.findIndex((seg) => isKeySegment(seg) && seg._key === object._key)
)
const myPath = prefix.concat(path)
const myValue = `field-${toString(myPath)}`
const values = useReportedValues()
const isEditing = values.filter(([p]) => p.startsWith(myValue)).length > 0
const focusPath = fullPath.slice(0, -1).concat(path).concat([FOCUS_TERMINATOR])
useEffect(() => {
if (isEditing) {
setOpen(true)
onSetFocus(focusPath)
}
}, [isEditing])
const handleOpenPopup = useCallback(
(event: any) => {
event.stopPropagation()
setOpen(true)
if (!isRemoved) {
onSetFocus(focusPath)
return
}
event.preventDefault()
},
[focusPath]
)
const handleClose = useCallback(() => {
setOpen(false)
}, [])
const popoverContent = (
<DiffContext.Provider value={{path: myPath}}>
<PopoverContent
diff={diff}
emptyObject={emptyObject}
onClose={handleClose}
schemaType={schemaType}
/>
</DiffContext.Provider>
)
const annotation = (diff.action !== 'unchanged' && diff.annotation) || null
const annotations = annotation ? [annotation] : []
return (
<InlineObjectWrapper
{...restProps}
onClick={handleOpenPopup}
style={style}
data-removed={diff.action === 'removed' ? '' : undefined}
border
radius={2}
>
<Popover content={popoverContent} open={open} portal>
<PreviewContainer>
<DiffTooltip annotations={annotations} description={`${diff.action} inline object`}>
<InlineBox>
<Preview schemaType={schemaType} value={object} layout="inline" />
<Flex align="center" paddingX={1}>
<InlineText size={0}>
<ChevronDownIcon />
</InlineText>
</Flex>
</InlineBox>
</DiffTooltip>
</PreviewContainer>
</Popover>
</InlineObjectWrapper>
)
}
function PopoverContent({
diff,
emptyObject,
onClose,
schemaType,
}: {
diff: ObjectDiff
emptyObject: boolean
onClose: () => void
schemaType: ObjectSchemaType
}) {
const [popoverElement, setPopoverElement] = useState<HTMLDivElement | null>(null)
// const {isTopLayer} = useLayer()
const handleClickOutside = useCallback(() => {
// Popover doesn't close at all when using this condition
// if (!isTopLayer) return
onClose()
}, [onClose])
useClickOutside(handleClickOutside, [popoverElement])
return (
<PopoverContainer ref={setPopoverElement} padding={3}>
{emptyObject && (
<Label size={1} muted>
Empty {schemaType.title}
</Label>
)}
{!emptyObject && <ChangeList diff={diff} schemaType={schemaType} />}
</PopoverContainer>
)
}