/
Annotation.tsx
201 lines (180 loc) · 6.18 KB
/
Annotation.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
199
200
201
import {hues} from '@sanity/color'
import {
BlockAnnotationRenderProps,
PortableTextEditor,
usePortableTextEditor,
} from '@sanity/portable-text-editor'
import {Path} from '@sanity/types'
import {Box, Theme, ThemeColorToneKey, Tooltip} from '@sanity/ui'
import React, {ComponentType, SyntheticEvent, useCallback, useMemo, useRef, useState} from 'react'
import styled, {css} from 'styled-components'
import {pathToString} from '../../../../field'
import {BlockAnnotationProps, RenderCustomMarkers} from '../../../types'
import {DefaultMarkers} from '../_legacyDefaultParts/Markers'
import {useFormBuilder} from '../../../useFormBuilder'
import {useMemberValidation} from '../hooks/useMemberValidation'
import {usePortableTextMarkers} from '../hooks/usePortableTextMarkers'
import {usePortableTextMemberItem} from '../hooks/usePortableTextMembers'
import {AnnotationToolbarPopover} from './AnnotationToolbarPopover'
interface AnnotationProps {
renderProps: BlockAnnotationRenderProps
onItemOpen: (path: Path) => void
onItemClose: () => void
readOnly?: boolean
renderCustomMarkers?: RenderCustomMarkers
scrollElement: HTMLElement | null
}
const Root = styled.span<{$toneKey?: Exclude<ThemeColorToneKey, 'transparent'>}>(
(props: {$toneKey?: Exclude<ThemeColorToneKey, 'transparent'>; theme: Theme}) => {
const {$toneKey = 'default', theme} = props
return css`
text-decoration: none;
display: inline;
background-color: ${theme.sanity.color.selectable?.[$toneKey].enabled.bg};
border-bottom: 1px dashed ${theme.sanity.color.selectable?.[$toneKey].enabled.fg};
color: ${theme.sanity.color.selectable?.[$toneKey].enabled.fg};
&[data-link] {
border-bottom: 1px solid ${theme.sanity.color.selectable?.[$toneKey].enabled.fg};
}
&[data-custom-markers] {
background-color: ${theme.sanity.color.dark ? hues.purple[950].hex : hues.purple[50].hex};
}
&[data-warning] {
background-color: ${theme.sanity.color.muted.caution.hovered.bg};
}
&[data-error] {
background-color: ${theme.sanity.color.muted.critical.hovered.bg};
}
`
}
)
const TooltipBox = styled(Box).attrs({forwardedAs: 'span'})`
max-width: 250px;
`
export const Annotation = function Annotation(props: AnnotationProps) {
const {onItemOpen, onItemClose, renderCustomMarkers, scrollElement, renderProps, readOnly} = props
const {children, type, value, path, selected, focused} = renderProps
const {Markers = DefaultMarkers} = useFormBuilder().__internal.components
const annotationRef = useRef<HTMLElement>(null)
const editor = usePortableTextEditor()
const markDefPath = useMemo(
() => [path[0]].concat(['markDefs', {_key: value._key}]),
[path, value._key]
)
const [textElement, setTextElement] = useState<HTMLSpanElement | null>(null)
const memberItem = usePortableTextMemberItem(pathToString(markDefPath))
const {validation, hasError, hasWarning} = useMemberValidation(memberItem?.node)
const markers = usePortableTextMarkers(path)
const CustomComponent = type.components?.annotation as ComponentType<BlockAnnotationProps>
const text = useMemo(
() => (
<span ref={setTextElement} data-annotation="">
{children}
</span>
),
[children]
)
const openItem = useCallback(() => {
if (memberItem) {
onItemOpen(memberItem.node.path)
}
}, [memberItem, onItemOpen])
const markersToolTip = useMemo(
() =>
validation.length > 0 || markers.length > 0 ? (
<Tooltip
placement="bottom"
portal="default"
content={
<TooltipBox padding={2}>
<Markers
markers={markers}
renderCustomMarkers={renderCustomMarkers}
validation={validation}
/>
</TooltipBox>
}
>
<span>{text}</span>
</Tooltip>
) : undefined,
[Markers, markers, renderCustomMarkers, text, validation]
)
const handleEditClick = useCallback(
(event: SyntheticEvent): void => {
PortableTextEditor.blur(editor)
event.preventDefault()
event.stopPropagation()
openItem()
},
[editor, openItem]
)
const handleRemoveClick = useCallback(
(event: React.MouseEvent<HTMLButtonElement>): void => {
event.preventDefault()
event.stopPropagation()
PortableTextEditor.removeAnnotation(editor, type)
PortableTextEditor.focus(editor)
},
[editor, type]
)
const isLink = type.name === 'link'
const toneKey = useMemo(() => {
if (hasError) {
return 'critical'
}
if (hasWarning) {
return 'caution'
}
if (isLink) {
return 'primary'
}
return 'default'
}, [isLink, hasError, hasWarning])
const hasCustomMarkers = markers.length > 0
const defaultRendered = (
<Root
$toneKey={toneKey}
ref={annotationRef}
data-link={isLink ? '' : undefined}
data-error={hasError ? '' : undefined}
data-warning={hasWarning ? '' : undefined}
data-custom-markers={hasCustomMarkers ? '' : undefined}
onClick={readOnly ? openItem : undefined}
>
<span ref={memberItem?.elementRef}>{markersToolTip || text}</span>
{!readOnly && (
<AnnotationToolbarPopover
textElement={textElement || undefined}
annotationElement={annotationRef.current || undefined}
scrollElement={scrollElement || undefined}
onEdit={handleEditClick}
onDelete={handleRemoveClick}
title={type?.title || type.name}
/>
)}
</Root>
)
return CustomComponent && memberItem ? (
<CustomComponent
focused={focused}
open={memberItem.member.open}
onOpen={openItem}
onClose={onItemClose}
// eslint-disable-next-line react/jsx-no-bind
onRemove={() => {
PortableTextEditor.removeAnnotation(editor, type)
PortableTextEditor.focus(editor)
}}
path={memberItem?.node.path || path}
// eslint-disable-next-line react/jsx-no-bind
renderDefault={() => defaultRendered}
selected={selected}
value={value}
>
<span ref={memberItem?.elementRef}>{text}</span>
</CustomComponent>
) : (
defaultRendered
)
}