@estiva-app/ui 0.98.0 → 0.99.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/AppProvider.d.ts +6 -3
- package/dist/AppProvider.d.ts.map +1 -1
- package/dist/ArchiveDialog.d.ts +35 -0
- package/dist/ArchiveDialog.d.ts.map +1 -0
- package/dist/AttachedCard.d.ts +13 -2
- package/dist/AttachedCard.d.ts.map +1 -1
- package/dist/Card.d.ts +6 -1
- package/dist/Card.d.ts.map +1 -1
- package/dist/ChipInput.d.ts +3 -1
- package/dist/ChipInput.d.ts.map +1 -1
- package/dist/CollapsibleSection.d.ts +27 -2
- package/dist/CollapsibleSection.d.ts.map +1 -1
- package/dist/CommentBox.d.ts +2 -2
- package/dist/CommentBox.d.ts.map +1 -1
- package/dist/ConfirmDialog.d.ts +15 -2
- package/dist/ConfirmDialog.d.ts.map +1 -1
- package/dist/ConversationCard.d.ts +6 -1
- package/dist/ConversationCard.d.ts.map +1 -1
- package/dist/ConversationLayout.d.ts +19 -4
- package/dist/ConversationLayout.d.ts.map +1 -1
- package/dist/ConversationPlace.d.ts +17 -0
- package/dist/ConversationPlace.d.ts.map +1 -0
- package/dist/DocumentEditor.d.ts +38 -0
- package/dist/DocumentEditor.d.ts.map +1 -1
- package/dist/DocumentField.d.ts +50 -0
- package/dist/DocumentField.d.ts.map +1 -0
- package/dist/EditableText.d.ts +38 -4
- package/dist/EditableText.d.ts.map +1 -1
- package/dist/EmptyState.d.ts +10 -5
- package/dist/EmptyState.d.ts.map +1 -1
- package/dist/FormDialog.d.ts +38 -0
- package/dist/FormDialog.d.ts.map +1 -0
- package/dist/InlineText.d.ts.map +1 -1
- package/dist/MoveDialog.d.ts +61 -0
- package/dist/MoveDialog.d.ts.map +1 -0
- package/dist/NavItem.d.ts +9 -2
- package/dist/NavItem.d.ts.map +1 -1
- package/dist/NavTree.d.ts +16 -3
- package/dist/NavTree.d.ts.map +1 -1
- package/dist/PinnedComment.d.ts +3 -1
- package/dist/PinnedComment.d.ts.map +1 -1
- package/dist/ReactionHorizon.d.ts +5 -1
- package/dist/ReactionHorizon.d.ts.map +1 -1
- package/dist/ResolveDialog.d.ts +3 -1
- package/dist/ResolveDialog.d.ts.map +1 -1
- package/dist/SelectionToolbar.d.ts +2 -2
- package/dist/Thread.d.ts +6 -3
- package/dist/Thread.d.ts.map +1 -1
- package/dist/commentBoxFrame.d.ts.map +1 -1
- package/dist/conversation.examples.d.ts +4 -1
- package/dist/conversation.examples.d.ts.map +1 -1
- package/dist/conversationScroll.d.ts +80 -0
- package/dist/conversationScroll.d.ts.map +1 -0
- package/dist/editor.d.ts +1 -0
- package/dist/editor.d.ts.map +1 -1
- package/dist/editor.js +228 -69
- package/dist/editor.js.map +4 -4
- package/dist/eslint/index.js +2 -2
- package/dist/eslint/index.js.map +2 -2
- package/dist/eslint/no-field-outside-a-form.d.ts +2 -2
- package/dist/eslint/no-field-outside-a-form.d.ts.map +1 -1
- package/dist/eslint/no-native-title.d.ts +1 -1
- package/dist/eslint/no-native-title.d.ts.map +1 -1
- package/dist/formBusy.d.ts +5 -2
- package/dist/formBusy.d.ts.map +1 -1
- package/dist/index.d.ts +14 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2210 -1458
- package/dist/index.js.map +4 -4
- package/dist/looks.d.ts +8 -0
- package/dist/looks.d.ts.map +1 -1
- package/dist/seenOnScreen.d.ts +27 -0
- package/dist/seenOnScreen.d.ts.map +1 -0
- package/dist/showInConversation.d.ts +31 -0
- package/dist/showInConversation.d.ts.map +1 -0
- package/dist/threadCard.d.ts +13 -0
- package/dist/threadCard.d.ts.map +1 -1
- package/package.json +1 -1
- package/registry.json +904 -139
- package/src/AppProvider.mdx +4 -0
- package/src/AppProvider.test.tsx +34 -2
- package/src/AppProvider.tsx +17 -6
- package/src/ArchiveDialog.mdx +70 -0
- package/src/ArchiveDialog.stories.tsx +64 -0
- package/src/ArchiveDialog.test.tsx +90 -0
- package/src/ArchiveDialog.tsx +70 -0
- package/src/AttachedCard.mdx +6 -0
- package/src/AttachedCard.stories.tsx +9 -1
- package/src/AttachedCard.test.tsx +111 -2
- package/src/AttachedCard.tsx +77 -7
- package/src/Card.mdx +20 -1
- package/src/Card.stories.tsx +20 -0
- package/src/Card.test.tsx +87 -1
- package/src/Card.tsx +51 -3
- package/src/ChipInput.mdx +7 -1
- package/src/ChipInput.stories.tsx +9 -0
- package/src/ChipInput.test.tsx +42 -0
- package/src/ChipInput.tsx +7 -3
- package/src/CollapsibleSection.mdx +20 -1
- package/src/CollapsibleSection.stories.tsx +19 -1
- package/src/CollapsibleSection.test.tsx +85 -2
- package/src/CollapsibleSection.tsx +54 -15
- package/src/CommentBox.mdx +2 -1
- package/src/CommentBox.test.tsx +81 -1
- package/src/CommentBox.tsx +32 -11
- package/src/ConfirmDialog.mdx +25 -3
- package/src/ConfirmDialog.stories.tsx +27 -0
- package/src/ConfirmDialog.test.tsx +87 -1
- package/src/ConfirmDialog.tsx +39 -8
- package/src/ConversationCard.mdx +4 -0
- package/src/ConversationCard.stories.tsx +11 -1
- package/src/ConversationCard.test.tsx +116 -0
- package/src/ConversationCard.tsx +53 -36
- package/src/ConversationLayout.mdx +30 -4
- package/src/ConversationLayout.scroll.test.tsx +411 -0
- package/src/ConversationLayout.stories.tsx +79 -1
- package/src/ConversationLayout.test.tsx +79 -0
- package/src/ConversationLayout.tsx +136 -15
- package/src/ConversationPlace.mdx +53 -0
- package/src/ConversationPlace.stories.tsx +48 -0
- package/src/ConversationPlace.test.tsx +51 -0
- package/src/ConversationPlace.tsx +30 -0
- package/src/DocumentEditor.mdx +2 -1
- package/src/DocumentEditor.tsx +118 -64
- package/src/DocumentField.mdx +83 -0
- package/src/DocumentField.stories.tsx +110 -0
- package/src/DocumentField.test.tsx +281 -0
- package/src/DocumentField.tsx +199 -0
- package/src/EditableText.buttons.test.tsx +318 -0
- package/src/EditableText.mdx +50 -2
- package/src/EditableText.stories.tsx +96 -2
- package/src/EditableText.tsx +189 -5
- package/src/EmptyState.mdx +33 -8
- package/src/EmptyState.stories.tsx +26 -1
- package/src/EmptyState.test.tsx +63 -2
- package/src/EmptyState.tsx +28 -6
- package/src/Form.mdx +3 -1
- package/src/FormDialog.mdx +78 -0
- package/src/FormDialog.stories.tsx +90 -0
- package/src/FormDialog.test.tsx +183 -0
- package/src/FormDialog.tsx +93 -0
- package/src/InlineText.mdx +1 -0
- package/src/InlineText.tsx +2 -1
- package/src/MoveDialog.mdx +74 -0
- package/src/MoveDialog.stories.tsx +121 -0
- package/src/MoveDialog.test.tsx +203 -0
- package/src/MoveDialog.tsx +183 -0
- package/src/NavItem.mdx +15 -0
- package/src/NavItem.stories.tsx +15 -0
- package/src/NavItem.test.tsx +64 -1
- package/src/NavItem.tsx +10 -2
- package/src/NavTree.mdx +26 -4
- package/src/NavTree.stories.tsx +59 -0
- package/src/NavTree.test.tsx +232 -2
- package/src/NavTree.tsx +93 -43
- package/src/PinnedComment.mdx +6 -1
- package/src/PinnedComment.stories.tsx +11 -1
- package/src/PinnedComment.tsx +15 -2
- package/src/ReactionHorizon.mdx +3 -0
- package/src/ReactionHorizon.stories.tsx +3 -0
- package/src/ReactionHorizon.test.tsx +55 -0
- package/src/ReactionHorizon.tsx +30 -11
- package/src/ReplyCard.mdx +3 -0
- package/src/ReplyCard.stories.tsx +19 -1
- package/src/ResolveDialog.mdx +2 -0
- package/src/ResolveDialog.test.tsx +21 -0
- package/src/ResolveDialog.tsx +11 -39
- package/src/SelectionToolbar.tsx +2 -2
- package/src/Textarea.mdx +1 -0
- package/src/Thread.mdx +18 -1
- package/src/Thread.stories.tsx +55 -3
- package/src/Thread.test.tsx +11 -0
- package/src/Thread.tsx +53 -11
- package/src/commentBoxFrame.tsx +2 -2
- package/src/conversation.examples.tsx +10 -2
- package/src/conversationScroll.ts +155 -0
- package/src/editor.ts +1 -0
- package/src/editorSelection.test.tsx +53 -0
- package/src/eslint/no-field-outside-a-form.test.ts +4 -0
- package/src/eslint/no-field-outside-a-form.ts +2 -2
- package/src/eslint/no-native-title.ts +1 -1
- package/src/formBusy.ts +5 -2
- package/src/index.ts +16 -6
- package/src/looks.ts +10 -0
- package/src/registry/registry.test.ts +12 -6
- package/src/seenOnScreen.test.tsx +182 -0
- package/src/seenOnScreen.ts +146 -0
- package/src/showInConversation.test.tsx +168 -0
- package/src/showInConversation.ts +93 -0
- package/src/threadCard.ts +16 -0
- package/stories/Choosing.mdx +7 -2
- package/tailwind-preset.js +7 -0
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
/**
|
|
3
|
+
* A form's field that holds a document (C12): what its page says it owns. Every
|
|
4
|
+
* edit goes back as it is typed, and nothing is saved on leaving: the form
|
|
5
|
+
* around it sends. A value from outside (a prefill) loads as the document it
|
|
6
|
+
* is, never handed back as an edit, and the field's own edits coming round
|
|
7
|
+
* late are never loaded over newer typing — Peek's `ProseField` tests (MAN-1,
|
|
8
|
+
* RIC-13), moved with it. Ctrl/Cmd+Enter is the form's. `[` and `@` are the
|
|
9
|
+
* kit's. The app's side is played by `@estiva-app/protocol`.
|
|
10
|
+
*/
|
|
11
|
+
import { act } from 'react'
|
|
12
|
+
import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'
|
|
13
|
+
import { cleanup, render, screen, within } from '@testing-library/react'
|
|
14
|
+
import type { Editor } from '@tiptap/core'
|
|
15
|
+
import type { JSONContent } from '@tiptap/react'
|
|
16
|
+
import { documentForEditing, encodeNpub, publishableFromEditor, toEditorDocument, type ContentFormat, type EditorDocument } from '@estiva-app/protocol'
|
|
17
|
+
import type { MentionPerson, ReferenceSection } from './ComposerTriggers'
|
|
18
|
+
import type { DocumentCodec } from './DocumentEditor'
|
|
19
|
+
import { DocumentField, type DocumentFieldProps } from './DocumentField'
|
|
20
|
+
import { Field } from './Field'
|
|
21
|
+
import { Form } from './Form'
|
|
22
|
+
|
|
23
|
+
const codec: DocumentCodec = {
|
|
24
|
+
read: (value, format) => toEditorDocument(documentForEditing(value, format as ContentFormat), { links: true }) as JSONContent,
|
|
25
|
+
write: (doc) => publishableFromEditor(doc as EditorDocument),
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const PEOPLE: MentionPerson[] = [{ id: 'one', name: 'Person one', pubkey: 'b'.repeat(64) }]
|
|
29
|
+
const mentions = { people: () => PEOPLE, uriOf: (person: MentionPerson) => (person.pubkey ? `nostr:${encodeNpub(person.pubkey)}` : null) }
|
|
30
|
+
const SECTIONS: ReferenceSection[] = [{ label: 'Group one', items: [{ id: 'item-one', label: 'Item one', snippet: '', uri: 'nostr:naddr1itemone' }] }]
|
|
31
|
+
const references = { sections: () => SECTIONS, ariaLabel: 'Items', sectionLabel: 'Items' }
|
|
32
|
+
|
|
33
|
+
/** A block document of paragraphs, as the protocol writes one. */
|
|
34
|
+
const doc = (...blocks: object[]) => JSON.stringify({ type: 'doc', content: blocks })
|
|
35
|
+
const para = (id: string, ...runs: object[]) => ({ type: 'paragraph', id, content: runs })
|
|
36
|
+
const run = (text: string, marks?: string[]) => ({ type: 'text', text, ...(marks ? { marks: marks.map((type) => ({ type })) } : {}) })
|
|
37
|
+
|
|
38
|
+
beforeAll(() => {
|
|
39
|
+
Element.prototype.scrollIntoView = vi.fn()
|
|
40
|
+
Element.prototype.getClientRects = () => [] as unknown as DOMRectList
|
|
41
|
+
Range.prototype.getClientRects = () => [] as unknown as DOMRectList
|
|
42
|
+
Range.prototype.getBoundingClientRect = () => new DOMRect()
|
|
43
|
+
;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
afterEach(() => {
|
|
47
|
+
cleanup()
|
|
48
|
+
vi.restoreAllMocks()
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
const box = () => screen.getByRole('textbox', { name: 'Description' }) as HTMLElement & { editor: Editor }
|
|
52
|
+
const editorOf = () => box().editor
|
|
53
|
+
|
|
54
|
+
function Described(props: Partial<DocumentFieldProps>) {
|
|
55
|
+
return <DocumentField value="" format="blocks" codec={codec} label="Description" onChange={() => {}} {...props} />
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function type(text: string) {
|
|
59
|
+
await act(async () => {
|
|
60
|
+
editorOf().view.focus()
|
|
61
|
+
editorOf().chain().focus('end').insertContent(text).run()
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The key codes a browser sends with these keys: the editor reads them, and takes every Enter and Escape as handled. */
|
|
66
|
+
const KEY_CODES: Record<string, number> = { Enter: 13, Escape: 27 }
|
|
67
|
+
|
|
68
|
+
/** A key pressed in the editor, as the browser sends it: to the editor's own element, with its key code, bubbling up. */
|
|
69
|
+
function key(init: KeyboardEventInit) {
|
|
70
|
+
const event = new KeyboardEvent('keydown', { bubbles: true, cancelable: true, keyCode: KEY_CODES[init.key ?? ''] ?? 0, ...init })
|
|
71
|
+
act(() => {
|
|
72
|
+
editorOf().view.dom.dispatchEvent(event)
|
|
73
|
+
})
|
|
74
|
+
return event
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const options = () => within(screen.getByRole('listbox')).getAllByRole('option').map((one) => one.textContent?.replace('↩ Enter', ''))
|
|
78
|
+
|
|
79
|
+
describe('every edit goes back as it is typed', () => {
|
|
80
|
+
it('hands back each edit with its format, while you are still in it', async () => {
|
|
81
|
+
const onChange = vi.fn<DocumentFieldProps['onChange']>()
|
|
82
|
+
render(<Described onChange={onChange} />)
|
|
83
|
+
await type('Hello')
|
|
84
|
+
expect(onChange).toHaveBeenCalled()
|
|
85
|
+
const [value, format] = onChange.mock.calls.at(-1)!
|
|
86
|
+
expect(format).toBe('estiva-blocks-1')
|
|
87
|
+
expect(codec.read(value, 'blocks').content?.[0]?.content?.[0]?.text).toBe('Hello')
|
|
88
|
+
expect(document.activeElement).toBe(box())
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it('leaving saves nothing: there is nothing to save, the form sends', async () => {
|
|
92
|
+
const onChange = vi.fn<DocumentFieldProps['onChange']>()
|
|
93
|
+
render(<Described onChange={onChange} />)
|
|
94
|
+
await type('Hello')
|
|
95
|
+
const edits = onChange.mock.calls.length
|
|
96
|
+
act(() => box().blur())
|
|
97
|
+
expect(onChange).toHaveBeenCalledTimes(edits)
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
it('emptied, it hands back nothing, so a required field is missing again', async () => {
|
|
101
|
+
const onChange = vi.fn<DocumentFieldProps['onChange']>()
|
|
102
|
+
render(<Described onChange={onChange} />)
|
|
103
|
+
await type('Hello')
|
|
104
|
+
act(() => {
|
|
105
|
+
editorOf().commands.clearContent(true)
|
|
106
|
+
})
|
|
107
|
+
expect(onChange).toHaveBeenLastCalledWith('', undefined)
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it('holding only spaces or blank lines, it hands back nothing, so a required field is missing (Peek’s ProseField)', async () => {
|
|
111
|
+
const onChange = vi.fn<DocumentFieldProps['onChange']>()
|
|
112
|
+
render(<Described onChange={onChange} />)
|
|
113
|
+
await type(' ')
|
|
114
|
+
expect(onChange).toHaveBeenLastCalledWith('', undefined)
|
|
115
|
+
act(() => {
|
|
116
|
+
editorOf().commands.setContent(
|
|
117
|
+
{ type: 'doc', content: [{ type: 'paragraph', content: [run(' ')] }, { type: 'paragraph' }, { type: 'paragraph', content: [run(' '), { type: 'hardBreak' }] }] },
|
|
118
|
+
{ emitUpdate: true },
|
|
119
|
+
)
|
|
120
|
+
})
|
|
121
|
+
expect(editorOf().state.doc.childCount).toBe(3)
|
|
122
|
+
expect(onChange).toHaveBeenLastCalledWith('', undefined)
|
|
123
|
+
// The spaces stay where they were typed: only what is handed back is empty.
|
|
124
|
+
expect(editorOf().getText()).toMatch(/^\s+$/)
|
|
125
|
+
await type('Hello')
|
|
126
|
+
expect(onChange.mock.lastCall?.[0]).not.toBe('')
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
it('a link alone among spaces is something: it is handed back', async () => {
|
|
130
|
+
const onChange = vi.fn<DocumentFieldProps['onChange']>()
|
|
131
|
+
render(<Described onChange={onChange} />)
|
|
132
|
+
act(() => {
|
|
133
|
+
editorOf().chain().focus('end').insertContent([{ type: 'text', text: ' ' }, { type: 'reference', attrs: { uri: 'nostr:naddr1itemone' } }, { type: 'text', text: ' ' }]).run()
|
|
134
|
+
})
|
|
135
|
+
expect(onChange.mock.lastCall?.[0]).not.toBe('')
|
|
136
|
+
})
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
describe('a value from outside', () => {
|
|
140
|
+
it('loads a prefill as formatted text, without handing it back as an edit', async () => {
|
|
141
|
+
const onChange = vi.fn<DocumentFieldProps['onChange']>()
|
|
142
|
+
const { rerender } = render(<Described onChange={onChange} />)
|
|
143
|
+
rerender(<Described onChange={onChange} value={doc(para('a1', run('The '), run('total', ['bold']), run(' is wrong')))} />)
|
|
144
|
+
expect(box().querySelector('strong')?.textContent).toBe('total')
|
|
145
|
+
expect(box().textContent).toBe('The total is wrong')
|
|
146
|
+
// A suggestion is not the person typing.
|
|
147
|
+
expect(onChange).not.toHaveBeenCalled()
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
it('does not reload an older edit of its own that arrives late', async () => {
|
|
151
|
+
/*
|
|
152
|
+
Production, RIC-13's probe: keys pressed back-to-back reached the editor
|
|
153
|
+
before the draft from the previous key came back, and that older value
|
|
154
|
+
was loaded over the newer text — the bold vanished. Two edits, then the
|
|
155
|
+
first comes round as `value`.
|
|
156
|
+
*/
|
|
157
|
+
const onChange = vi.fn<DocumentFieldProps['onChange']>()
|
|
158
|
+
const { rerender } = render(<Described onChange={onChange} />)
|
|
159
|
+
act(() => {
|
|
160
|
+
editorOf().commands.insertContent('abc')
|
|
161
|
+
})
|
|
162
|
+
act(() => {
|
|
163
|
+
editorOf().commands.selectAll()
|
|
164
|
+
editorOf().commands.toggleBold()
|
|
165
|
+
})
|
|
166
|
+
const [first, second] = onChange.mock.calls.map(([value]) => value)
|
|
167
|
+
expect(onChange).toHaveBeenCalledTimes(2)
|
|
168
|
+
|
|
169
|
+
rerender(<Described onChange={onChange} value={first} />)
|
|
170
|
+
expect(box().querySelector('strong')?.textContent).toBe('abc')
|
|
171
|
+
|
|
172
|
+
// The draft catches up, and a genuine outside value still loads after it.
|
|
173
|
+
rerender(<Described onChange={onChange} value={second} />)
|
|
174
|
+
rerender(<Described onChange={onChange} value={doc(para('b1', run('From the thread')))} />)
|
|
175
|
+
expect(box().textContent).toBe('From the thread')
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
it('keeps a quick Backspace when the text it undid comes round late', async () => {
|
|
179
|
+
// `a`, `ab`, `a`: the value repeats, so matching edits by value alone let the late `ab` back in and undid the Backspace.
|
|
180
|
+
const onChange = vi.fn<DocumentFieldProps['onChange']>()
|
|
181
|
+
const { rerender } = render(<Described onChange={onChange} />)
|
|
182
|
+
act(() => {
|
|
183
|
+
editorOf().commands.insertContent('a')
|
|
184
|
+
})
|
|
185
|
+
act(() => {
|
|
186
|
+
editorOf().commands.insertContent('b')
|
|
187
|
+
})
|
|
188
|
+
act(() => {
|
|
189
|
+
editorOf().commands.deleteRange({ from: 2, to: 3 })
|
|
190
|
+
})
|
|
191
|
+
const [a, ab, again] = onChange.mock.calls.map(([value]) => value)
|
|
192
|
+
expect(again).toBe(a)
|
|
193
|
+
|
|
194
|
+
rerender(<Described onChange={onChange} value={a} />)
|
|
195
|
+
rerender(<Described onChange={onChange} value={ab} />)
|
|
196
|
+
expect(editorOf().getText()).toBe('a')
|
|
197
|
+
rerender(<Described onChange={onChange} value={again} />)
|
|
198
|
+
expect(editorOf().getText()).toBe('a')
|
|
199
|
+
})
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
describe('in a form', () => {
|
|
203
|
+
it('Ctrl+Enter reaches the form around it, which sends; Shift+Enter breaks the line', async () => {
|
|
204
|
+
const onSubmit = vi.fn()
|
|
205
|
+
render(
|
|
206
|
+
<Form onSubmit={onSubmit}>
|
|
207
|
+
<Described />
|
|
208
|
+
</Form>,
|
|
209
|
+
)
|
|
210
|
+
await type('Hello')
|
|
211
|
+
const sent = key({ key: 'Enter', ctrlKey: true })
|
|
212
|
+
expect(onSubmit).toHaveBeenCalledTimes(1)
|
|
213
|
+
expect(sent.defaultPrevented).toBe(true)
|
|
214
|
+
let breaks = 0
|
|
215
|
+
key({ key: 'Enter', shiftKey: true })
|
|
216
|
+
editorOf().state.doc.descendants((node) => void (node.type.name === 'hardBreak' && breaks++))
|
|
217
|
+
expect(breaks).toBe(1)
|
|
218
|
+
expect(onSubmit).toHaveBeenCalledTimes(1)
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
it('Escape is the form’s: it reaches what is around the field', async () => {
|
|
222
|
+
const around = vi.fn()
|
|
223
|
+
render(
|
|
224
|
+
<div onKeyDown={(event) => around(event.key)}>
|
|
225
|
+
<Described />
|
|
226
|
+
</div>,
|
|
227
|
+
)
|
|
228
|
+
await type('Hello')
|
|
229
|
+
key({ key: 'Escape' })
|
|
230
|
+
expect(around).toHaveBeenCalledWith('Escape')
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
it('waits while its form sends, as the fields beside it do: nothing can be written in it until the answer comes', () => {
|
|
234
|
+
const onChange = vi.fn()
|
|
235
|
+
const { rerender } = render(
|
|
236
|
+
<Form busy onSubmit={() => {}}>
|
|
237
|
+
<Described onChange={onChange} />
|
|
238
|
+
</Form>,
|
|
239
|
+
)
|
|
240
|
+
expect(editorOf().isEditable).toBe(false)
|
|
241
|
+
expect(box().getAttribute('contenteditable')).toBe('false')
|
|
242
|
+
rerender(
|
|
243
|
+
<Form onSubmit={() => {}}>
|
|
244
|
+
<Described onChange={onChange} />
|
|
245
|
+
</Form>,
|
|
246
|
+
)
|
|
247
|
+
expect(editorOf().isEditable).toBe(true)
|
|
248
|
+
expect(box().getAttribute('contenteditable')).toBe('true')
|
|
249
|
+
// Going off and on again is not an edit.
|
|
250
|
+
expect(onChange).not.toHaveBeenCalled()
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
it('a required field says so to a screen reader', () => {
|
|
254
|
+
render(
|
|
255
|
+
<Field label="Description" required>
|
|
256
|
+
<Described />
|
|
257
|
+
</Field>,
|
|
258
|
+
)
|
|
259
|
+
expect(box().getAttribute('aria-required')).toBe('true')
|
|
260
|
+
})
|
|
261
|
+
})
|
|
262
|
+
|
|
263
|
+
describe('what it shows', () => {
|
|
264
|
+
it('its placeholder while it is empty, and not once something is written', async () => {
|
|
265
|
+
render(<Described placeholder="What happened?" />)
|
|
266
|
+
expect(screen.getByText('What happened?')).toBeTruthy()
|
|
267
|
+
await type('Hello')
|
|
268
|
+
expect(screen.queryByText('What happened?')).toBeNull()
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
it('[ opens the list the app supplies, and @ the people', async () => {
|
|
272
|
+
render(<Described references={references} mentions={mentions} />)
|
|
273
|
+
await type(' [')
|
|
274
|
+
expect(screen.getByRole('listbox', { name: 'Items' })).toBeTruthy()
|
|
275
|
+
act(() => {
|
|
276
|
+
editorOf().commands.clearContent()
|
|
277
|
+
})
|
|
278
|
+
await type(' @')
|
|
279
|
+
expect(options()[0]).toMatch(/Person one$/)
|
|
280
|
+
})
|
|
281
|
+
})
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { useEffect, useLayoutEffect, useRef, useState, type ComponentType, type MouseEvent, type ReactNode } from 'react'
|
|
2
|
+
import type { AnyExtension, Editor } from '@tiptap/core'
|
|
3
|
+
import { EditorContent, useEditor, useEditorState, type JSONContent } from '@tiptap/react'
|
|
4
|
+
import type { AttachmentViewProps, ReferenceViewProps } from './BlockNodes'
|
|
5
|
+
import { cn } from './cn'
|
|
6
|
+
import { documentExtensions, type DocumentCodec, type DocumentMentions, type DocumentReferences } from './DocumentEditor'
|
|
7
|
+
import { useFormBusy } from './formBusy'
|
|
8
|
+
import type { LinkChipsOptions } from './LinkChips'
|
|
9
|
+
import { EDITOR_SELECTION_CLASSES, FIELD_SHELL_CLASSES, FIELD_SHELL_FOCUS_CLASSES } from './looks'
|
|
10
|
+
import { ReferenceMenuView } from './ReferenceMenu'
|
|
11
|
+
import { richTextClassName } from './RichText'
|
|
12
|
+
import { SelectionToolbar } from './SelectionToolbar'
|
|
13
|
+
import { isSuggestionOpen } from './suggestionPopup'
|
|
14
|
+
|
|
15
|
+
export interface DocumentFieldProps {
|
|
16
|
+
/** What the field holds, in `format`; `''` when nothing is written. A value that is not its own edit coming back is loaded into it. */
|
|
17
|
+
value: string
|
|
18
|
+
/** The format `value` is in, handed to `codec.read`. */
|
|
19
|
+
format: string
|
|
20
|
+
/** How the app's text and the editor's document turn into each other: `DocumentEditor`'s. */
|
|
21
|
+
codec: DocumentCodec
|
|
22
|
+
/** Every edit, as it is typed: the text and its format, or `''` once it is emptied or holds only spaces and blank lines. Nothing is saved: the form around it sends. */
|
|
23
|
+
onChange: (value: string, contentFormat: string | undefined) => void
|
|
24
|
+
/** The editor's accessible name: the label of the `Field` around it. */
|
|
25
|
+
label: string
|
|
26
|
+
/** Muted words in the empty field. */
|
|
27
|
+
placeholder?: ReactNode
|
|
28
|
+
/** `[` and `/` → File. */
|
|
29
|
+
references?: DocumentReferences
|
|
30
|
+
/** `@` and `/` → Mention. */
|
|
31
|
+
mentions?: DocumentMentions
|
|
32
|
+
/** A pasted app URL drawn as the chip of what it names (`LinkChips`). */
|
|
33
|
+
linkChips?: LinkChipsOptions
|
|
34
|
+
/** Plain text pasted as the blocks it spells: the app's reader of markdown, as the editor's document. */
|
|
35
|
+
readMarkdown?: (text: string) => JSONContent | undefined
|
|
36
|
+
/** Draws a link: the app's own, which resolves what it names. */
|
|
37
|
+
referenceView?: ComponentType<ReferenceViewProps>
|
|
38
|
+
/** Draws a placed file: the app's own card. */
|
|
39
|
+
attachmentView?: ComponentType<AttachmentViewProps>
|
|
40
|
+
/** More of the app's own extensions. */
|
|
41
|
+
extensions?: readonly AnyExtension[]
|
|
42
|
+
/** Set by a required `Field` around it, for a screen reader. */
|
|
43
|
+
'aria-required'?: boolean | 'true' | 'false'
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const NONE: readonly AnyExtension[] = []
|
|
47
|
+
const nothing = () => {}
|
|
48
|
+
|
|
49
|
+
/** Nothing but spaces and blank lines: no word, and no link, card or picture (any part of it that is not text or a line break). */
|
|
50
|
+
const isBlank = (editor: Editor) => {
|
|
51
|
+
let blank = true
|
|
52
|
+
editor.state.doc.descendants((node) => {
|
|
53
|
+
if (node.isText ? /\S/.test(node.text ?? '') : node.isLeaf && node.type.name !== 'hardBreak') blank = false
|
|
54
|
+
return blank
|
|
55
|
+
})
|
|
56
|
+
return blank
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* A form's field that holds a document: a description written in the form that files it. The document editor's
|
|
61
|
+
* grammar (`DocumentEditor`'s blocks, marks, `[`, `@` and `/`) in a field's box, as a Textarea's beside it. It is the
|
|
62
|
+
* form's: every edit goes back as it is typed, nothing is saved on leaving, Ctrl/Cmd+Enter sends the form, and while
|
|
63
|
+
* the form sends nothing can be written in it.
|
|
64
|
+
*
|
|
65
|
+
* **Told apart by everything it has handed back, not only the last** (Peek's `ProseField`, RIC-13). Keys pressed faster
|
|
66
|
+
* than React renders reach the editor before the value from the previous one comes back. Compared with the last edit
|
|
67
|
+
* alone, that older value looked like one from outside and was loaded over the newer text. A value this field wrote is
|
|
68
|
+
* never an outside value, however late it arrives; each is dropped once the value has shown it, so the queue holds only
|
|
69
|
+
* the edits still in flight. A queue, not a set: `a`, `ab`, `a` (a key and its Backspace) repeats a value.
|
|
70
|
+
*/
|
|
71
|
+
export function DocumentField({
|
|
72
|
+
value,
|
|
73
|
+
format,
|
|
74
|
+
codec,
|
|
75
|
+
onChange,
|
|
76
|
+
label,
|
|
77
|
+
placeholder,
|
|
78
|
+
references,
|
|
79
|
+
mentions,
|
|
80
|
+
linkChips,
|
|
81
|
+
readMarkdown,
|
|
82
|
+
referenceView,
|
|
83
|
+
attachmentView,
|
|
84
|
+
extensions = NONE,
|
|
85
|
+
'aria-required': required,
|
|
86
|
+
}: DocumentFieldProps) {
|
|
87
|
+
// The latest props, for the editor's callbacks, which are made once.
|
|
88
|
+
const latest = useRef({ value, format, codec, onChange, references, mentions, readMarkdown })
|
|
89
|
+
useEffect(() => {
|
|
90
|
+
latest.current = { value, format, codec, onChange, references, mentions, readMarkdown }
|
|
91
|
+
})
|
|
92
|
+
// What this field has handed back and the value has not shown yet, oldest first.
|
|
93
|
+
const inFlight = useRef<string[]>([value])
|
|
94
|
+
// `''` too is the codec's to read, as the document editor reads it: a block typed into it then gets its id.
|
|
95
|
+
const read = (text: string) => latest.current.codec.read(text, latest.current.format)
|
|
96
|
+
// No upload in a field: `/` has no Upload, and a pasted picture stays the browser's.
|
|
97
|
+
const pickFile = useRef(nothing)
|
|
98
|
+
const attach = useRef(async () => {})
|
|
99
|
+
const [initial] = useState(() => read(value))
|
|
100
|
+
|
|
101
|
+
const editor = useEditor(
|
|
102
|
+
{
|
|
103
|
+
extensions: documentExtensions({
|
|
104
|
+
latest,
|
|
105
|
+
references,
|
|
106
|
+
mentions,
|
|
107
|
+
linkChips,
|
|
108
|
+
table: false,
|
|
109
|
+
upload: false,
|
|
110
|
+
readMarkdown: !!readMarkdown,
|
|
111
|
+
comments: false,
|
|
112
|
+
referenceView,
|
|
113
|
+
attachmentView,
|
|
114
|
+
pickFile,
|
|
115
|
+
attach,
|
|
116
|
+
extensions,
|
|
117
|
+
}),
|
|
118
|
+
// Made once: a value coming back must not rebuild the editor under the caret; see the effect below.
|
|
119
|
+
content: initial,
|
|
120
|
+
editorProps: {
|
|
121
|
+
attributes: {
|
|
122
|
+
'aria-label': label,
|
|
123
|
+
role: 'textbox',
|
|
124
|
+
'aria-multiline': 'true',
|
|
125
|
+
...(required !== undefined && required !== false ? { 'aria-required': 'true' } : {}),
|
|
126
|
+
// Three lines tall at rest, as a Textarea of three rows beside it. A row or a card is its whole line (`LinkPlaces`).
|
|
127
|
+
// eslint-disable-next-line token-spacing/no-restricted-classes -- @estiva-escape: three lines of the field's own text (140% of 14px each), the height of a Textarea of three rows
|
|
128
|
+
class: richTextClassName('default', cn('outline-none min-h-[4.2em] [&_[data-link-place]]:inline-block [&_[data-link-place]]:w-full [&_[data-link-place]]:align-top', EDITOR_SELECTION_CLASSES)),
|
|
129
|
+
},
|
|
130
|
+
/*
|
|
131
|
+
Ctrl/Cmd+Enter sends the form around it, as from any of its fields. The field asks the form itself: the
|
|
132
|
+
editor marks every Enter it is given as handled (a browser's Enter would otherwise change the text), and the
|
|
133
|
+
form leaves a handled key alone — measured in Chrome, the form never heard it. An open list keeps its keys.
|
|
134
|
+
*/
|
|
135
|
+
handleKeyDown: (view, event) => {
|
|
136
|
+
if (event.key !== 'Enter' || !(event.ctrlKey || event.metaKey) || event.altKey || event.shiftKey || isSuggestionOpen()) return false
|
|
137
|
+
const form = view.dom.closest('form')
|
|
138
|
+
if (!form) return false
|
|
139
|
+
form.requestSubmit()
|
|
140
|
+
return true
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
onUpdate: ({ editor: ed }) => {
|
|
144
|
+
// Emptied, or only spaces and blank lines, it holds nothing: `''`, so a required field is missing again (Peek's ProseField).
|
|
145
|
+
const next = isBlank(ed) ? { value: '' } : latest.current.codec.write(ed.getJSON())
|
|
146
|
+
inFlight.current.push(next.value)
|
|
147
|
+
latest.current.onChange(next.value, next.contentFormat)
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
[],
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
useEffect(() => {
|
|
154
|
+
if (!editor || editor.isDestroyed) return
|
|
155
|
+
const at = inFlight.current.indexOf(value)
|
|
156
|
+
if (at !== -1) {
|
|
157
|
+
// Its own edit coming round, possibly late. Values render in order, so nothing handed back before it can still
|
|
158
|
+
// arrive: drop those, keep the rest.
|
|
159
|
+
inFlight.current = inFlight.current.slice(at + 1)
|
|
160
|
+
return
|
|
161
|
+
}
|
|
162
|
+
inFlight.current = []
|
|
163
|
+
// `read` takes the latest codec and format, set by the effect above, which runs first.
|
|
164
|
+
editor.commands.setContent(read(value), { emitUpdate: false })
|
|
165
|
+
}, [editor, value])
|
|
166
|
+
|
|
167
|
+
/*
|
|
168
|
+
While its form sends it waits, as the fields beside it do. The form's disabled fieldset switches off every native
|
|
169
|
+
control in it, and a document's box is not one: it is told here, before the paint, with no edit handed back.
|
|
170
|
+
*/
|
|
171
|
+
const busy = useFormBusy()
|
|
172
|
+
useLayoutEffect(() => {
|
|
173
|
+
if (editor && !editor.isDestroyed && editor.isEditable === busy) editor.setEditable(!busy, false)
|
|
174
|
+
}, [editor, busy])
|
|
175
|
+
|
|
176
|
+
const empty = useEditorState({ editor, selector: ({ editor: ed }) => !ed || ed.isEmpty })
|
|
177
|
+
|
|
178
|
+
// A press on the box's padding puts the caret in, as a Textarea's does.
|
|
179
|
+
const into = (event: MouseEvent<HTMLDivElement>) => {
|
|
180
|
+
if (event.target !== event.currentTarget || !editor) return
|
|
181
|
+
event.preventDefault()
|
|
182
|
+
editor.commands.focus('end')
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return (
|
|
186
|
+
<>
|
|
187
|
+
<div className={cn(FIELD_SHELL_CLASSES, FIELD_SHELL_FOCUS_CLASSES, 'relative cursor-text px-3 py-2')} onMouseDown={into}>
|
|
188
|
+
<EditorContent editor={editor} />
|
|
189
|
+
{empty && placeholder && (
|
|
190
|
+
<span aria-hidden className="pointer-events-none absolute left-3 top-2 text-body-2 text-text-muted">
|
|
191
|
+
{placeholder}
|
|
192
|
+
</span>
|
|
193
|
+
)}
|
|
194
|
+
</div>
|
|
195
|
+
<SelectionToolbar editor={editor} marks={['bold', 'italic', 'underline', 'code']} link />
|
|
196
|
+
<ReferenceMenuView editor={editor} />
|
|
197
|
+
</>
|
|
198
|
+
)
|
|
199
|
+
}
|