Skip to content

Commit 852bfca

Browse files
authoredMar 26, 2024··
fix(tasks): update description input to create blocks edx-1206 (#6126)
* fix(tasks): update description input to create blocks * fix(comments): rename renderNormalBlock to defaultRenderBlock
1 parent 971ba9f commit 852bfca

File tree

11 files changed

+94
-29
lines changed

11 files changed

+94
-29
lines changed
 

‎packages/sanity/src/structure/comments/src/components/pte/comment-input/CommentInput.tsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
import {type EditorChange, keyGenerator, PortableTextEditor} from '@sanity/portable-text-editor'
1+
import {
2+
type EditorChange,
3+
keyGenerator,
4+
PortableTextEditor,
5+
type RenderBlockFunction,
6+
} from '@sanity/portable-text-editor'
27
import {type CurrentUser, type PortableTextBlock} from '@sanity/types'
38
import {type AvatarSize, focusFirstDescendant, focusLastDescendant, Stack} from '@sanity/ui'
49
import type * as React from 'react'
510
import {forwardRef, useCallback, useImperativeHandle, useMemo, useRef, useState} from 'react'
611
import {type UserListWithPermissionsHookValue} from 'sanity'
712

813
import {editorSchemaType} from '../config'
14+
import {renderBlock as defaultRenderBlock} from '../render'
915
import {CommentInputDiscardDialog} from './CommentInputDiscardDialog'
1016
import {CommentInputInner} from './CommentInputInner'
1117
import {CommentInputProvider} from './CommentInputProvider'
@@ -34,6 +40,7 @@ export interface CommentInputProps {
3440
onSubmit?: () => void
3541
placeholder?: React.ReactNode
3642
readOnly?: boolean
43+
renderBlock?: RenderBlockFunction
3744
value: PortableTextBlock[] | null
3845
withAvatar?: boolean
3946
avatarSize?: AvatarSize
@@ -75,6 +82,7 @@ export const CommentInput = forwardRef<CommentInputHandle, CommentInputProps>(
7582
onSubmit,
7683
placeholder,
7784
readOnly,
85+
renderBlock = defaultRenderBlock,
7886
value = EMPTY_ARRAY,
7987
withAvatar = true,
8088
} = props
@@ -231,6 +239,7 @@ export const CommentInput = forwardRef<CommentInputHandle, CommentInputProps>(
231239
onKeyDown={onKeyDown}
232240
onSubmit={onSubmit && handleSubmit}
233241
placeholder={placeholder}
242+
renderBlock={renderBlock}
234243
withAvatar={withAvatar}
235244
/>
236245
</Stack>

‎packages/sanity/src/structure/comments/src/components/pte/comment-input/CommentInputInner.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {type RenderBlockFunction} from '@sanity/portable-text-editor'
12
import {type CurrentUser} from '@sanity/types'
23
import {type AvatarSize, Box, Card, Flex, MenuDivider, Stack} from '@sanity/ui'
34
// eslint-disable-next-line camelcase
@@ -107,6 +108,7 @@ interface CommentInputInnerProps {
107108
onKeyDown?: (e: React.KeyboardEvent<Element>) => void
108109
onSubmit?: () => void
109110
placeholder?: React.ReactNode
111+
renderBlock: RenderBlockFunction
110112
withAvatar?: boolean
111113
}
112114

@@ -120,6 +122,7 @@ export function CommentInputInner(props: CommentInputInnerProps) {
120122
onKeyDown,
121123
onSubmit,
122124
placeholder,
125+
renderBlock,
123126
withAvatar,
124127
} = props
125128

@@ -164,6 +167,7 @@ export function CommentInputInner(props: CommentInputInnerProps) {
164167
onKeyDown={onKeyDown}
165168
onSubmit={onSubmit}
166169
placeholder={placeholder}
170+
renderBlock={renderBlock}
167171
/>
168172
</EditableWrap>
169173

‎packages/sanity/src/structure/comments/src/components/pte/comment-input/Editable.tsx

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
type EditorSelection,
33
PortableTextEditable,
4+
type RenderBlockFunction,
45
usePortableTextEditorSelection,
56
} from '@sanity/portable-text-editor'
67
import {isPortableTextSpan, isPortableTextTextBlock} from '@sanity/types'
@@ -15,7 +16,7 @@ import styled, {css} from 'styled-components'
1516
import {Popover, type PopoverProps} from '../../../../../../ui-components'
1617
import {commentsLocaleNamespace} from '../../../../i18n'
1718
import {MentionsMenu, type MentionsMenuHandle} from '../../mentions'
18-
import {renderBlock, renderChild} from '../render'
19+
import {renderChild} from '../render'
1920
import {useCommentInput} from './useCommentInput'
2021
import {useCursorElement} from './useCursorElement'
2122

@@ -65,6 +66,7 @@ interface EditableProps {
6566
onKeyDown?: (e: React.KeyboardEvent<Element>) => void
6667
onSubmit?: () => void
6768
placeholder?: React.ReactNode
69+
renderBlock: RenderBlockFunction
6870
}
6971

7072
export interface EditableHandle {
@@ -75,11 +77,12 @@ export function Editable(props: EditableProps) {
7577
const {t} = useTranslation(commentsLocaleNamespace)
7678
const {
7779
focusLock,
78-
placeholder = t('compose.create-comment-placeholder'),
7980
onFocus,
8081
onBlur,
8182
onKeyDown,
8283
onSubmit,
84+
placeholder = t('compose.create-comment-placeholder'),
85+
renderBlock,
8386
} = props
8487
const [popoverElement, setPopoverElement] = useState<HTMLDivElement | null>(null)
8588
const rootElementRef = useRef<HTMLDivElement | null>(null)
@@ -126,19 +129,21 @@ export function Editable(props: EditableProps) {
126129
if (event.shiftKey) {
127130
break
128131
}
129-
// Enter is being used both to select something from the mentionsMenu
130-
// or to submit the comment. Prevent the default behavior.
131-
event.preventDefault()
132-
event.stopPropagation()
133132

134133
// If the mention menu is open close it, but don't submit.
135134
if (mentionsMenuOpen) {
135+
// Enter is being used both to select something from the mentionsMenu, prevent the default behavior.
136+
event.preventDefault()
137+
event.stopPropagation()
136138
closeMentions()
137139
break
138140
}
139141

140142
// Submit the comment if eligible for submission
141143
if (onSubmit && canSubmit) {
144+
// Enter is being used to submit the comment, prevent the default behavior.
145+
event.preventDefault()
146+
event.stopPropagation()
142147
onSubmit()
143148
}
144149
break

‎packages/sanity/src/tasks/src/tasks/components/form/fields/DescriptionInput.tsx renamed to ‎packages/sanity/src/tasks/src/tasks/components/form/fields/descriptionInput/DescriptionInput.tsx

+24-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// eslint-disable-next-line camelcase
22
import {getTheme_v2} from '@sanity/ui/theme'
3-
import {useCallback, useEffect, useRef, useState} from 'react'
3+
import {startTransition, useCallback, useEffect, useState} from 'react'
44
import {
55
type ArrayFieldProps,
66
type PortableTextBlock,
@@ -10,10 +10,11 @@ import {
1010
} from 'sanity'
1111
import styled, {css} from 'styled-components'
1212

13-
import {CommentInput} from '../../../../../../structure/comments'
14-
import {tasksLocaleNamespace} from '../../../../../i18n'
15-
import {useMentionUser} from '../../../context'
16-
import {type FormMode} from '../../../types'
13+
import {CommentInput} from '../../../../../../../structure/comments'
14+
import {tasksLocaleNamespace} from '../../../../../../i18n'
15+
import {useMentionUser} from '../../../../context'
16+
import {type FormMode} from '../../../../types'
17+
import {renderBlock} from './render'
1718

1819
const DescriptionInputRoot = styled.div<{$mode: FormMode; $minHeight: number}>((props) => {
1920
const theme = getTheme_v2(props.theme)
@@ -43,39 +44,42 @@ export function DescriptionInput(props: ArrayFieldProps & {mode: FormMode}) {
4344
inputProps: {onChange},
4445
} = props
4546
const value = _propValue as PortableTextBlock[] | undefined
46-
4747
const currentUser = useCurrentUser()
4848
const {mentionOptions} = useMentionUser()
4949

5050
const handleChange = useCallback((next: PortableTextBlock[]) => onChange(set(next)), [onChange])
5151

52-
const rootRef = useRef<HTMLDivElement | null>(null)
52+
const [rootRef, setRootRef] = useState<HTMLDivElement | null>(null)
5353
const [textBoxScrollHeight, setTextBoxScrollHeight] = useState<number>(200)
5454
const setTextboxHeight = useCallback((ref: HTMLDivElement) => {
5555
const textBox = ref.querySelector('[role="textbox"]')
5656
if (!textBox) return
57+
5758
const height = textBox.scrollHeight
5859
setTextBoxScrollHeight(height)
5960
}, [])
6061

61-
const setRootRef = useCallback(
62-
(ref: HTMLDivElement) => {
63-
if (!ref) return
64-
setTextboxHeight(ref)
65-
rootRef.current = ref
66-
},
67-
[setTextboxHeight],
68-
)
62+
const handleSetRootRef = useCallback((ref: HTMLDivElement) => {
63+
if (!ref) return
64+
startTransition(() => {
65+
setRootRef(ref)
66+
})
67+
}, [])
68+
6969
const {t} = useTranslation(tasksLocaleNamespace)
7070

7171
useEffect(() => {
72-
if (!rootRef.current) return
73-
setTextboxHeight(rootRef.current)
74-
}, [value, setTextboxHeight])
72+
if (!rootRef) return
73+
setTextboxHeight(rootRef)
74+
}, [value, setTextboxHeight, rootRef])
7575

7676
if (!currentUser) return null
7777
return (
78-
<DescriptionInputRoot $mode={mode} ref={setRootRef} $minHeight={textBoxScrollHeight || 200}>
78+
<DescriptionInputRoot
79+
$mode={mode}
80+
ref={handleSetRootRef}
81+
$minHeight={textBoxScrollHeight || 200}
82+
>
7983
<CommentInput
8084
expandOnFocus={false}
8185
currentUser={currentUser}
@@ -86,6 +90,7 @@ export function DescriptionInput(props: ArrayFieldProps & {mode: FormMode}) {
8690
placeholder={t('form.input.description.placeholder')}
8791
// eslint-disable-next-line react/jsx-no-bind
8892
onDiscardConfirm={() => null}
93+
renderBlock={renderBlock}
8994
/>
9095
</DescriptionInputRoot>
9196
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {Box, Text} from '@sanity/ui'
2+
import type * as React from 'react'
3+
import styled from 'styled-components'
4+
5+
const NormalText = styled(Text)`
6+
word-break: break-word;
7+
`
8+
9+
interface NormalBlockProps {
10+
children: React.ReactNode
11+
}
12+
13+
export function DescriptionInputBlock(props: NormalBlockProps) {
14+
const {children} = props
15+
16+
return (
17+
<Box paddingTop={2} paddingBottom={3}>
18+
<NormalText size={1}>{children}</NormalText>
19+
</Box>
20+
)
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './DescriptionInputBlock'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './DescriptionInput'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './renderBlock'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {type RenderBlockFunction} from '@sanity/portable-text-editor'
2+
3+
import {DescriptionInputBlock} from '../blocks'
4+
5+
export const renderBlock: RenderBlockFunction = (blockProps) => {
6+
const {children} = blockProps
7+
8+
return <DescriptionInputBlock>{children}</DescriptionInputBlock>
9+
}

‎packages/sanity/src/tasks/src/tasks/components/form/fields/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export * from './assignee'
22
export * from './DateEditFormField'
3-
export * from './DescriptionInput'
3+
export * from './descriptionInput'
44
export * from './FieldWrapper'
55
export * from './StatusSelector'
66
export * from './TargetField'

‎packages/sanity/src/tasks/src/tasks/components/form/tasksFormBuilder/TasksFormBuilder.tsx

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Box, rem} from '@sanity/ui'
22
// eslint-disable-next-line camelcase
33
import {getTheme_v2} from '@sanity/ui/theme'
4+
import {motion, type Variants} from 'framer-motion'
45
import {useEffect, useMemo} from 'react'
56
import {
67
type CurrentUser,
@@ -20,7 +21,15 @@ import {TasksAddonWorkspaceProvider} from '../addonWorkspace/TasksAddOnWorkspace
2021
import {getTargetValue} from '../utils'
2122
import {useTasksFormBuilder} from './useTasksFormBuilder'
2223

23-
const FormBuilderRoot = styled.div((props) => {
24+
const VARIANTS: Variants = {
25+
hidden: {opacity: 0},
26+
visible: {
27+
opacity: 1,
28+
transition: {duration: 0.2, delay: 0.2},
29+
},
30+
}
31+
32+
const FormBuilderRoot = styled(motion.div)((props) => {
2433
const theme = getTheme_v2(props.theme)
2534

2635
return `
@@ -67,7 +76,7 @@ const TasksFormBuilderInner = ({
6776
{formBuilderProps.loading ? (
6877
<LoadingBlock showText />
6978
) : (
70-
<FormBuilderRoot id="wrapper">
79+
<FormBuilderRoot id="wrapper" initial="hidden" animate="visible" variants={VARIANTS}>
7180
<FormBuilder {...formBuilderProps} />
7281
</FormBuilderRoot>
7382
)}

0 commit comments

Comments
 (0)
Please sign in to comment.