@estiva-app/ui 0.82.1 → 0.84.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/dist/BlockNodes.d.ts +16 -5
- package/dist/BlockNodes.d.ts.map +1 -1
- package/dist/CommentBox.d.ts +11 -2
- package/dist/CommentBox.d.ts.map +1 -1
- package/dist/ComposeBox.d.ts +17 -0
- package/dist/ComposeBox.d.ts.map +1 -1
- package/dist/DocumentEditor.d.ts +5 -4
- package/dist/DocumentEditor.d.ts.map +1 -1
- package/dist/LinkPlaces.d.ts +51 -0
- package/dist/LinkPlaces.d.ts.map +1 -0
- package/dist/ReferenceMenu.d.ts.map +1 -1
- package/dist/editor.d.ts +1 -0
- package/dist/editor.d.ts.map +1 -1
- package/dist/editor.js +392 -178
- package/dist/editor.js.map +4 -4
- package/dist/linked.examples.d.ts.map +1 -1
- package/dist/links.d.ts.map +1 -1
- package/package.json +1 -1
- package/registry.json +75 -12
- package/src/BlockNodes.tsx +36 -9
- package/src/CommentBox.mdx +11 -1
- package/src/CommentBox.stories.tsx +75 -2
- package/src/CommentBox.test.tsx +170 -3
- package/src/CommentBox.tsx +211 -53
- package/src/ComposeBox.tsx +19 -2
- package/src/DocumentEditor.mdx +7 -2
- package/src/DocumentEditor.stories.tsx +101 -15
- package/src/DocumentEditor.test.tsx +49 -0
- package/src/DocumentEditor.tsx +17 -10
- package/src/LinkPlaces.mdx +59 -0
- package/src/LinkPlaces.stories.tsx +60 -0
- package/src/LinkPlaces.test.ts +134 -0
- package/src/LinkPlaces.ts +169 -0
- package/src/ReferenceCards.mdx +4 -0
- package/src/ReferenceMenu.mdx +32 -24
- package/src/ReferenceMenu.stories.tsx +15 -16
- package/src/ReferenceMenu.test.tsx +161 -57
- package/src/ReferenceMenu.tsx +48 -44
- package/src/editor.ts +1 -0
- package/src/linked.examples.ts +4 -3
- package/src/links.ts +4 -1
- package/src/pages.test.ts +2 -2
- package/src/registry/registry.test.ts +6 -6
- package/stories/Choosing.mdx +3 -2
|
@@ -4,14 +4,14 @@ import StarterKit from '@tiptap/starter-kit'
|
|
|
4
4
|
import { BlockId, ReferenceNode, type ReferenceViewProps } from './BlockNodes'
|
|
5
5
|
import { Card } from './Card'
|
|
6
6
|
import { InlineChip } from './InlineChip'
|
|
7
|
-
import {
|
|
7
|
+
import { LinkPlaces } from './LinkPlaces'
|
|
8
8
|
import { ReferenceMenu, ReferenceMenuView } from './ReferenceMenu'
|
|
9
9
|
import { richTextClassName } from './RichText'
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
* Click
|
|
13
|
-
* or
|
|
14
|
-
*
|
|
12
|
+
* Click a link: its menu opens under it — Open, and alone on its line Show as
|
|
13
|
+
* Row or Card. The link is a placeholder naming where it sits: the real chip,
|
|
14
|
+
* row and card are the app's.
|
|
15
15
|
*/
|
|
16
16
|
const meta = {
|
|
17
17
|
title: 'Primitives/ReferenceMenu',
|
|
@@ -21,39 +21,38 @@ const meta = {
|
|
|
21
21
|
export default meta
|
|
22
22
|
type Story = StoryObj
|
|
23
23
|
|
|
24
|
-
function Placeholder({
|
|
25
|
-
return <InlineChip>Item
|
|
24
|
+
function Placeholder({ place = 'chip' }: ReferenceViewProps) {
|
|
25
|
+
if (place === 'chip') return <InlineChip>Item one</InlineChip>
|
|
26
|
+
return (
|
|
27
|
+
<Card fill="inset" className={place === 'card' ? 'flex h-24 items-start p-3' : 'flex h-9 items-center px-3'}>
|
|
28
|
+
<span className="text-body-2 text-text-secondary">Item two, its {place}</span>
|
|
29
|
+
</Card>
|
|
30
|
+
)
|
|
26
31
|
}
|
|
27
32
|
|
|
28
33
|
function Box() {
|
|
29
34
|
const editor = useEditor({
|
|
30
|
-
extensions: [StarterKit.configure({ link: false }), BlockId, ReferenceNode.configure({ view: Placeholder }),
|
|
35
|
+
extensions: [StarterKit.configure({ link: false }), BlockId, ReferenceNode.configure({ view: Placeholder }), LinkPlaces, ReferenceMenu],
|
|
31
36
|
content: {
|
|
32
37
|
type: 'doc',
|
|
33
38
|
content: [
|
|
34
39
|
{
|
|
35
40
|
type: 'paragraph',
|
|
36
41
|
attrs: { blockId: 'p1' },
|
|
37
|
-
content: [
|
|
38
|
-
{ type: 'text', text: 'See ' },
|
|
39
|
-
{ type: 'reference', attrs: { uri: 'nostr:naddr1one' } },
|
|
40
|
-
{ type: 'text', text: ' for the details.' },
|
|
41
|
-
],
|
|
42
|
+
content: [{ type: 'text', text: 'See ' }, { type: 'reference', attrs: { uri: 'nostr:naddr1one' } }, { type: 'text', text: ' for the details.' }],
|
|
42
43
|
},
|
|
44
|
+
{ type: 'paragraph', attrs: { blockId: 'p2' }, content: [{ type: 'reference', attrs: { uri: 'nostr:naddr1two' } }] },
|
|
43
45
|
],
|
|
44
46
|
},
|
|
45
47
|
editorProps: { attributes: { class: richTextClassName('default', 'outline-none'), 'aria-label': 'Text' } },
|
|
46
48
|
})
|
|
47
|
-
const slots = useReferenceCards(editor)
|
|
48
|
-
const cards = slots.flatMap((slot) => slot.cards.map((card) => `${card.layout} card for ${card.uri}`))
|
|
49
49
|
return (
|
|
50
50
|
<Card fill="surface" className="flex w-[520px] flex-col gap-2 p-3">
|
|
51
51
|
<EditorContent editor={editor} />
|
|
52
52
|
<ReferenceMenuView editor={editor} />
|
|
53
|
-
<p className="text-caption text-text-secondary">{cards.length ? cards.join(', ') : 'No card'}</p>
|
|
54
53
|
</Card>
|
|
55
54
|
)
|
|
56
55
|
}
|
|
57
56
|
|
|
58
|
-
/** A
|
|
57
|
+
/** A link in a sentence, whose menu has Open only, and one alone on its line: choose Card, then Row. */
|
|
59
58
|
export const InAnEditor: Story = { render: () => <Box /> }
|
|
@@ -1,27 +1,34 @@
|
|
|
1
1
|
// @vitest-environment jsdom
|
|
2
2
|
/**
|
|
3
|
-
* A
|
|
4
|
-
*
|
|
5
|
-
*
|
|
3
|
+
* A link's menu while writing a document (the record
|
|
4
|
+
* a-lone-links-chip-menu-offers-row-or-card): a click on a link opens Open, and
|
|
5
|
+
* alone on its line Show as Row or Card, instead of following it; a lone-line
|
|
6
|
+
* link just pasted or picked opens the menu by itself, without taking the
|
|
7
|
+
* caret, and typing on closes it with no card.
|
|
6
8
|
*/
|
|
7
9
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
10
|
+
import { createPortal } from 'react-dom'
|
|
8
11
|
import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
|
|
9
12
|
import { Editor } from '@tiptap/core'
|
|
10
13
|
import { NodeSelection, TextSelection } from '@tiptap/pm/state'
|
|
11
14
|
import { EditorContent, useEditor } from '@tiptap/react'
|
|
12
15
|
import StarterKit from '@tiptap/starter-kit'
|
|
16
|
+
import { encodeNaddr, encodeNpub } from '@estiva-app/protocol'
|
|
13
17
|
import { BlockId, ReferenceNode, type ReferenceViewProps } from './BlockNodes'
|
|
14
18
|
import { LinkChips } from './LinkChips'
|
|
15
|
-
import {
|
|
19
|
+
import { LinkPlaces } from './LinkPlaces'
|
|
16
20
|
import { ReferenceMenu, ReferenceMenuView, isReferenceMenuOpen, referenceMenuClick } from './ReferenceMenu'
|
|
17
21
|
|
|
18
22
|
const URL_ = 'https://app.example/i/one'
|
|
19
23
|
const V = 'nostr:naddr1example'
|
|
24
|
+
const FOLDER = `nostr:${encodeNaddr({ kind: 39000, pubkey: 'a'.repeat(64), identifier: 'place-one', relays: [] })}`
|
|
25
|
+
const PERSON = `nostr:${encodeNpub('b'.repeat(64))}`
|
|
20
26
|
|
|
21
27
|
type Json = { type: string; text?: string; attrs?: Record<string, unknown>; content?: Json[] }
|
|
22
28
|
const inline = (ed: Editor) => (ed.getJSON() as Json).content?.[0].content ?? []
|
|
23
29
|
const chipOf = (ed: Editor) => inline(ed).find((n) => n.type === 'reference')
|
|
24
30
|
const para = (...content: unknown[]): Record<string, unknown> => ({ type: 'doc', content: [{ type: 'paragraph', attrs: { blockId: 'p1' }, content }] })
|
|
31
|
+
const ref = (uri: string, embed?: string) => ({ type: 'reference', attrs: { uri, ...(embed ? { embed } : {}) } })
|
|
25
32
|
const tick = () => new Promise((resolve) => setTimeout(resolve, 0))
|
|
26
33
|
// jsdom has no ClipboardEvent; `pasteText` takes the paste's event, so a plain one stands in.
|
|
27
34
|
const paste = (ed: Editor, text: string) => ed.view.pasteText(text, new Event('paste') as ClipboardEvent)
|
|
@@ -45,7 +52,7 @@ const make = (content: Record<string, unknown>, known: Map<string, string>, answ
|
|
|
45
52
|
StarterKit.configure({ link: false }),
|
|
46
53
|
BlockId,
|
|
47
54
|
ReferenceNode,
|
|
48
|
-
|
|
55
|
+
LinkPlaces,
|
|
49
56
|
ReferenceMenu,
|
|
50
57
|
LinkChips.configure({
|
|
51
58
|
referenceOf: (url) => known.get(url),
|
|
@@ -67,23 +74,44 @@ const make = (content: Record<string, unknown>, known: Map<string, string>, answ
|
|
|
67
74
|
const toEnd = (ed: Editor) => ed.view.dispatch(ed.state.tr.setSelection(TextSelection.create(ed.state.doc, ed.state.doc.content.size - 1)))
|
|
68
75
|
|
|
69
76
|
describe('ReferenceMenu', () => {
|
|
70
|
-
it('opens by itself on a pasted
|
|
71
|
-
const ed = make(para(
|
|
77
|
+
it('opens by itself on a URL pasted on an empty line that becomes its chip, and typing on closes it with no card', async () => {
|
|
78
|
+
const ed = make(para(), new Map([[URL_, V]]))
|
|
72
79
|
await tick()
|
|
73
80
|
toEnd(ed)
|
|
74
81
|
paste(ed, URL_)
|
|
75
82
|
expect(chipOf(ed)?.attrs).toMatchObject({ uri: V, href: URL_ })
|
|
76
83
|
expect(isReferenceMenuOpen(ed.state)).toBe(true)
|
|
77
|
-
// The caret stayed in the text, after the
|
|
84
|
+
// The caret stayed in the text, after the link.
|
|
78
85
|
expect(ed.state.selection.empty).toBe(true)
|
|
79
86
|
ed.view.dispatch(ed.state.tr.insertText(' and more'))
|
|
80
87
|
expect(isReferenceMenuOpen(ed.state)).toBe(false)
|
|
81
88
|
expect(chipOf(ed)?.attrs?.embed).toBeNull()
|
|
82
89
|
})
|
|
83
90
|
|
|
91
|
+
it('does not open by itself on a link pasted or picked into a sentence, where it would offer Open alone', async () => {
|
|
92
|
+
const ed = make(para({ type: 'text', text: 'See ' }), new Map([[URL_, V]]))
|
|
93
|
+
await tick()
|
|
94
|
+
toEnd(ed)
|
|
95
|
+
paste(ed, URL_)
|
|
96
|
+
expect(chipOf(ed)?.attrs).toMatchObject({ uri: V, href: URL_ })
|
|
97
|
+
expect(isReferenceMenuOpen(ed.state)).toBe(false)
|
|
98
|
+
ed.commands.insertContent([ref(V), { type: 'text', text: ' ' }])
|
|
99
|
+
expect(isReferenceMenuOpen(ed.state)).toBe(false)
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
it('does not open by itself on a person or a Folder picked on an empty line, which stay chips', () => {
|
|
103
|
+
for (const uri of [PERSON, FOLDER]) {
|
|
104
|
+
const ed = make(para(), new Map())
|
|
105
|
+
toEnd(ed)
|
|
106
|
+
ed.commands.insertContent([ref(uri), { type: 'text', text: ' ' }])
|
|
107
|
+
expect(isReferenceMenuOpen(ed.state)).toBe(false)
|
|
108
|
+
ed.destroy()
|
|
109
|
+
}
|
|
110
|
+
})
|
|
111
|
+
|
|
84
112
|
it('opens when a pasted URL\'s answer comes back with the caret still after it, not once the person typed on', async () => {
|
|
85
113
|
const answers: (() => void)[] = []
|
|
86
|
-
const ed = make(para(
|
|
114
|
+
const ed = make(para(), new Map(), answers)
|
|
87
115
|
await tick()
|
|
88
116
|
toEnd(ed)
|
|
89
117
|
paste(ed, URL_)
|
|
@@ -96,7 +124,7 @@ describe('ReferenceMenu', () => {
|
|
|
96
124
|
|
|
97
125
|
const later: (() => void)[] = []
|
|
98
126
|
const other = 'https://app.example/i/two'
|
|
99
|
-
const ed2 = make(para(
|
|
127
|
+
const ed2 = make(para(), new Map(), later)
|
|
100
128
|
await tick()
|
|
101
129
|
toEnd(ed2)
|
|
102
130
|
paste(ed2, other)
|
|
@@ -113,13 +141,13 @@ describe('ReferenceMenu', () => {
|
|
|
113
141
|
const doc = {
|
|
114
142
|
type: 'doc',
|
|
115
143
|
content: [
|
|
116
|
-
{ type: 'paragraph', attrs: { blockId: 'p1' }, content: [
|
|
144
|
+
{ type: 'paragraph', attrs: { blockId: 'p1' }, content: [ref(V)] },
|
|
117
145
|
{ type: 'paragraph', attrs: { blockId: 'p2' }, content: [{ type: 'text', text: other }] },
|
|
118
146
|
],
|
|
119
147
|
}
|
|
120
148
|
const ed = make(doc, new Map(), answers)
|
|
121
149
|
await tick()
|
|
122
|
-
expect(referenceMenuClick(ed,
|
|
150
|
+
expect(referenceMenuClick(ed, 1)).toBe(true)
|
|
123
151
|
answers[0]()
|
|
124
152
|
await tick()
|
|
125
153
|
expect((ed.getJSON() as Json).content?.[1].content?.[0].type).toBe('reference')
|
|
@@ -128,31 +156,30 @@ describe('ReferenceMenu', () => {
|
|
|
128
156
|
expect(isReferenceMenuOpen(ed.state)).toBe(false)
|
|
129
157
|
})
|
|
130
158
|
|
|
131
|
-
it('opens on a chip pasted right before an identical one', () => {
|
|
132
|
-
const ed = make(para({ type: 'text', text: 'a ' }, { type: 'reference', attrs: { uri: V } }), new Map())
|
|
133
|
-
ed.view.dispatch(ed.state.tr.setSelection(TextSelection.create(ed.state.doc, 3)))
|
|
134
|
-
ed.view.dispatch(ed.state.tr.insert(3, ed.schema.nodes.reference.create({ uri: V })).setMeta('uiEvent', 'paste'))
|
|
135
|
-
expect(ed.state.selection.from).toBe(4)
|
|
136
|
-
expect(isReferenceMenuOpen(ed.state)).toBe(true)
|
|
137
|
-
})
|
|
138
|
-
|
|
139
159
|
it('leaves a key with a modifier to the editor', async () => {
|
|
140
|
-
const ed = make(para(
|
|
160
|
+
const ed = make(para(), new Map([[URL_, V]]))
|
|
141
161
|
await tick()
|
|
142
162
|
toEnd(ed)
|
|
143
163
|
paste(ed, URL_)
|
|
164
|
+
expect(isReferenceMenuOpen(ed.state)).toBe(true)
|
|
144
165
|
const shifted = new KeyboardEvent('keydown', { key: 'ArrowDown', shiftKey: true, bubbles: true, cancelable: true })
|
|
145
166
|
ed.view.dom.dispatchEvent(shifted)
|
|
146
167
|
expect(shifted.defaultPrevented).toBe(false)
|
|
147
168
|
})
|
|
148
169
|
|
|
149
|
-
it('offers no Open for a
|
|
150
|
-
// No view: the
|
|
151
|
-
const ed = make(para(
|
|
170
|
+
it('offers no Open for a link with nothing to open, and still Show as', () => {
|
|
171
|
+
// No view: the link draws no control of its own, and it has no URL.
|
|
172
|
+
const ed = make(para(ref(V)), new Map())
|
|
152
173
|
render(<ReferenceMenuView editor={ed} />)
|
|
153
|
-
act(() => void referenceMenuClick(ed,
|
|
174
|
+
act(() => void referenceMenuClick(ed, 1))
|
|
154
175
|
expect(screen.queryByRole('menuitem', { name: 'Open' })).toBeNull()
|
|
155
|
-
expect(screen.getByRole('menuitemradio', { name: '
|
|
176
|
+
expect(screen.getByRole('menuitemradio', { name: 'Card' })).toBeDefined()
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
it('has no menu for a link in a sentence with nothing to open', () => {
|
|
180
|
+
const ed = make(para({ type: 'text', text: 'See ' }, ref(V)), new Map())
|
|
181
|
+
expect(referenceMenuClick(ed, 5)).toBe(false)
|
|
182
|
+
expect(isReferenceMenuOpen(ed.state)).toBe(false)
|
|
156
183
|
})
|
|
157
184
|
|
|
158
185
|
it('forgets a paste edited inside: each keystroke in it is not an ask', async () => {
|
|
@@ -171,33 +198,33 @@ describe('ReferenceMenu', () => {
|
|
|
171
198
|
expect(answers).toHaveLength(1)
|
|
172
199
|
})
|
|
173
200
|
|
|
174
|
-
it('opens on a
|
|
175
|
-
const ed = make(para(
|
|
201
|
+
it('opens on a link picked from a list on an empty line, which puts a space after it', () => {
|
|
202
|
+
const ed = make(para(), new Map())
|
|
176
203
|
toEnd(ed)
|
|
177
|
-
ed.commands.insertContent([
|
|
204
|
+
ed.commands.insertContent([ref(V), { type: 'text', text: ' ' }])
|
|
178
205
|
expect(isReferenceMenuOpen(ed.state)).toBe(true)
|
|
179
206
|
})
|
|
180
207
|
|
|
181
208
|
it('does not open on a URL typed by hand, an undo, or a document loaded over the editor', async () => {
|
|
182
|
-
const ed = make(para(
|
|
209
|
+
const ed = make(para(), new Map([[URL_, V]]))
|
|
183
210
|
await tick()
|
|
184
211
|
toEnd(ed)
|
|
185
212
|
for (const ch of URL_) ed.view.dispatch(ed.state.tr.insertText(ch))
|
|
186
213
|
ed.view.dispatch(ed.state.tr.insertText(' '))
|
|
187
214
|
expect(chipOf(ed)?.attrs).toMatchObject({ uri: V })
|
|
188
215
|
expect(isReferenceMenuOpen(ed.state)).toBe(false)
|
|
189
|
-
ed.commands.setContent(para(
|
|
216
|
+
ed.commands.setContent(para(ref(V)))
|
|
190
217
|
expect(isReferenceMenuOpen(ed.state)).toBe(false)
|
|
191
218
|
})
|
|
192
219
|
|
|
193
220
|
it('keeps Enter for a new line after a paste until an arrow highlights a row; then Enter chooses it', async () => {
|
|
194
|
-
const ed = make(para(
|
|
221
|
+
const ed = make(para(), new Map([[URL_, V]]))
|
|
195
222
|
await tick()
|
|
196
223
|
toEnd(ed)
|
|
197
224
|
paste(ed, URL_)
|
|
198
225
|
expect(isReferenceMenuOpen(ed.state)).toBe(true)
|
|
199
|
-
// ↓ to Open, ↓ to
|
|
200
|
-
for (let i = 0; i <
|
|
226
|
+
// ↓ to Open, ↓ to Row, ↓ to Card.
|
|
227
|
+
for (let i = 0; i < 3; i++) expect(press(ed, 'ArrowDown')).toBe(true)
|
|
201
228
|
expect(press(ed, 'Enter')).toBe(true)
|
|
202
229
|
expect(chipOf(ed)?.attrs?.embed).toBe('detailed')
|
|
203
230
|
expect(isReferenceMenuOpen(ed.state)).toBe(false)
|
|
@@ -205,36 +232,38 @@ describe('ReferenceMenu', () => {
|
|
|
205
232
|
expect((ed.getJSON() as Json).content).toHaveLength(1)
|
|
206
233
|
})
|
|
207
234
|
|
|
208
|
-
it('opens from the keyboard with Enter on a selected
|
|
209
|
-
const ed = make(para(
|
|
210
|
-
ed.view.dispatch(ed.state.tr.setSelection(NodeSelection.create(ed.state.doc,
|
|
235
|
+
it('opens from the keyboard with Enter on a selected link, and Escape closes it without writing', () => {
|
|
236
|
+
const ed = make(para(ref(V, 'compact')), new Map())
|
|
237
|
+
ed.view.dispatch(ed.state.tr.setSelection(NodeSelection.create(ed.state.doc, 1)))
|
|
211
238
|
expect(press(ed, 'Enter')).toBe(true)
|
|
212
239
|
expect(isReferenceMenuOpen(ed.state)).toBe(true)
|
|
213
|
-
// The
|
|
240
|
+
// The link is still there: Enter did not replace it with a line break.
|
|
214
241
|
expect(chipOf(ed)).toBeDefined()
|
|
215
|
-
//
|
|
242
|
+
// Row is highlighted; ↑ wraps to Card.
|
|
216
243
|
press(ed, 'ArrowUp')
|
|
217
244
|
expect(press(ed, 'Escape')).toBe(true)
|
|
218
245
|
expect(isReferenceMenuOpen(ed.state)).toBe(false)
|
|
246
|
+
// An older writer's compact is kept as written until Row or Card is chosen.
|
|
219
247
|
expect(chipOf(ed)?.attrs?.embed).toBe('compact')
|
|
220
248
|
})
|
|
221
249
|
|
|
222
250
|
it('opens on a click only while editing', () => {
|
|
223
|
-
const ed = make(para(
|
|
251
|
+
const ed = make(para(ref(V)), new Map())
|
|
224
252
|
ed.setEditable(false)
|
|
225
|
-
expect(referenceMenuClick(ed,
|
|
253
|
+
expect(referenceMenuClick(ed, 1)).toBe(false)
|
|
226
254
|
ed.setEditable(true)
|
|
227
|
-
expect(referenceMenuClick(ed,
|
|
255
|
+
expect(referenceMenuClick(ed, 1)).toBe(true)
|
|
228
256
|
expect(isReferenceMenuOpen(ed.state)).toBe(true)
|
|
229
|
-
// The caret right after the
|
|
230
|
-
expect(ed.state.selection.empty && ed.state.selection.from).toBe(
|
|
257
|
+
// The caret right after the link, not the link selected: no selection toolbar beside the menu, and typing does not replace it.
|
|
258
|
+
expect(ed.state.selection.empty && ed.state.selection.from).toBe(2)
|
|
231
259
|
})
|
|
232
260
|
})
|
|
233
261
|
|
|
234
262
|
describe('ReferenceMenuView', () => {
|
|
235
|
-
|
|
263
|
+
/** The app's drawing: a link, named by its place, that follows on a click. */
|
|
264
|
+
function Linked({ uri, place = 'chip' }: ReferenceViewProps) {
|
|
236
265
|
return (
|
|
237
|
-
<a href={`/o/${uri}`} onClick={(event) => (followed(uri), event.preventDefault())}>
|
|
266
|
+
<a href={`/o/${uri}`} data-place={place} onClick={(event) => (followed(uri), event.preventDefault())}>
|
|
238
267
|
Item
|
|
239
268
|
</a>
|
|
240
269
|
)
|
|
@@ -243,7 +272,7 @@ describe('ReferenceMenuView', () => {
|
|
|
243
272
|
|
|
244
273
|
function Harness({ content }: { content: Record<string, unknown> }) {
|
|
245
274
|
const e = useEditor({
|
|
246
|
-
extensions: [StarterKit.configure({ link: false }), BlockId, ReferenceNode.configure({ view: Linked }),
|
|
275
|
+
extensions: [StarterKit.configure({ link: false }), BlockId, ReferenceNode.configure({ view: Linked }), LinkPlaces, ReferenceMenu],
|
|
247
276
|
content,
|
|
248
277
|
})
|
|
249
278
|
editor = e ?? undefined
|
|
@@ -255,31 +284,106 @@ describe('ReferenceMenuView', () => {
|
|
|
255
284
|
)
|
|
256
285
|
}
|
|
257
286
|
|
|
258
|
-
it('takes a click on
|
|
287
|
+
it('takes a click on a link in a sentence for its menu, which offers Open only, and Open hands the click to the link', async () => {
|
|
259
288
|
followed.mockClear()
|
|
260
|
-
render(<Harness content={para({ type: 'text', text: 'See ' },
|
|
289
|
+
render(<Harness content={para({ type: 'text', text: 'See ' }, ref(V))} />)
|
|
261
290
|
const chip = await screen.findByRole('link', { name: 'Item' })
|
|
262
291
|
fireEvent.click(chip)
|
|
263
292
|
expect(followed).not.toHaveBeenCalled()
|
|
264
293
|
await screen.findByRole('menu', { name: 'Link' })
|
|
294
|
+
expect(screen.queryByRole('group', { name: 'Show as' })).toBeNull()
|
|
295
|
+
expect(screen.queryAllByRole('menuitemradio')).toHaveLength(0)
|
|
265
296
|
fireEvent.mouseDown(screen.getByRole('menuitem', { name: 'Open' }))
|
|
266
297
|
expect(followed).toHaveBeenCalledWith(V)
|
|
267
298
|
await waitFor(() => expect(screen.queryByRole('menu', { name: 'Link' })).toBeNull())
|
|
268
299
|
})
|
|
269
300
|
|
|
270
|
-
it('
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
301
|
+
it('offers Open only on a person or a Folder alone on its line, which stay chips', async () => {
|
|
302
|
+
for (const uri of [PERSON, FOLDER]) {
|
|
303
|
+
render(<Harness content={para(ref(uri))} />)
|
|
304
|
+
const link = await screen.findByRole('link', { name: 'Item' })
|
|
305
|
+
expect(link.getAttribute('data-place')).toBe('chip')
|
|
306
|
+
fireEvent.click(link)
|
|
307
|
+
await screen.findByRole('menuitem', { name: 'Open' })
|
|
308
|
+
expect(screen.queryAllByRole('menuitemradio')).toHaveLength(0)
|
|
309
|
+
cleanup()
|
|
310
|
+
editor?.destroy()
|
|
311
|
+
}
|
|
312
|
+
})
|
|
313
|
+
|
|
314
|
+
it('draws a lone link as its row with Row checked; Card puts its card in the row\'s place, and Row puts the row back', async () => {
|
|
315
|
+
render(<Harness content={para(ref(V))} />)
|
|
316
|
+
const link = await screen.findByRole('link', { name: 'Item' })
|
|
317
|
+
expect(link.getAttribute('data-place')).toBe('row')
|
|
318
|
+
fireEvent.click(link)
|
|
319
|
+
expect((await screen.findByRole('group', { name: 'Show as' })).textContent).toContain('Row')
|
|
320
|
+
expect(screen.getByRole('menuitemradio', { name: 'Row' }).getAttribute('aria-checked')).toBe('true')
|
|
321
|
+
fireEvent.mouseDown(screen.getByRole('menuitemradio', { name: 'Card' }))
|
|
276
322
|
expect(chipOf(editor!)?.attrs?.embed).toBe('detailed')
|
|
323
|
+
await waitFor(() => expect(screen.getByRole('link', { name: 'Item' }).getAttribute('data-place')).toBe('card'))
|
|
277
324
|
await waitFor(() => expect(screen.queryByRole('menu', { name: 'Link' })).toBeNull())
|
|
325
|
+
|
|
326
|
+
fireEvent.click(screen.getByRole('link', { name: 'Item' }))
|
|
327
|
+
expect((await screen.findByRole('menuitemradio', { name: 'Card' })).getAttribute('aria-checked')).toBe('true')
|
|
328
|
+
fireEvent.mouseDown(screen.getByRole('menuitemradio', { name: 'Row' }))
|
|
329
|
+
// Row writes no `embed` at all, not `none`.
|
|
330
|
+
expect(chipOf(editor!)?.attrs?.embed).toBeNull()
|
|
331
|
+
await waitFor(() => expect(screen.getByRole('link', { name: 'Item' }).getAttribute('data-place')).toBe('row'))
|
|
332
|
+
})
|
|
333
|
+
|
|
334
|
+
it('lets a click on a card\'s own control reach it while editing, instead of opening the menu', async () => {
|
|
335
|
+
const changed = vi.fn()
|
|
336
|
+
function WithControl({ place = 'chip' }: ReferenceViewProps) {
|
|
337
|
+
return place === 'card' ? (
|
|
338
|
+
<>
|
|
339
|
+
<button type="button" onClick={changed}>
|
|
340
|
+
Status
|
|
341
|
+
</button>
|
|
342
|
+
{/* A picker's list, portalled out as a Select's is: its click travels React's tree through the link. */}
|
|
343
|
+
{createPortal(
|
|
344
|
+
<div role="option" aria-selected={false} onClick={changed}>
|
|
345
|
+
Done
|
|
346
|
+
</div>,
|
|
347
|
+
document.body,
|
|
348
|
+
)}
|
|
349
|
+
</>
|
|
350
|
+
) : (
|
|
351
|
+
<a href="/o/item">Item</a>
|
|
352
|
+
)
|
|
353
|
+
}
|
|
354
|
+
function CardHarness() {
|
|
355
|
+
const e = useEditor({ extensions: [StarterKit.configure({ link: false }), BlockId, ReferenceNode.configure({ view: WithControl }), LinkPlaces, ReferenceMenu], content: para(ref(V, 'detailed')) })
|
|
356
|
+
editor = e ?? undefined
|
|
357
|
+
return (
|
|
358
|
+
<>
|
|
359
|
+
<EditorContent editor={e} />
|
|
360
|
+
<ReferenceMenuView editor={e} />
|
|
361
|
+
</>
|
|
362
|
+
)
|
|
363
|
+
}
|
|
364
|
+
render(<CardHarness />)
|
|
365
|
+
fireEvent.click(await screen.findByRole('button', { name: 'Status' }))
|
|
366
|
+
expect(changed).toHaveBeenCalledTimes(1)
|
|
367
|
+
expect(isReferenceMenuOpen(editor!.state)).toBe(false)
|
|
368
|
+
fireEvent.click(screen.getByRole('option', { name: 'Done' }))
|
|
369
|
+
expect(changed).toHaveBeenCalledTimes(2)
|
|
370
|
+
expect(isReferenceMenuOpen(editor!.state)).toBe(false)
|
|
371
|
+
})
|
|
372
|
+
|
|
373
|
+
it('offers Open at once on a link picked on an empty line, before the app has drawn it', async () => {
|
|
374
|
+
render(<Harness content={para()} />)
|
|
375
|
+
await screen.findByRole('textbox')
|
|
376
|
+
act(() => {
|
|
377
|
+
editor!.commands.focus('end')
|
|
378
|
+
editor!.commands.insertContent([ref(V), { type: 'text', text: ' ' }])
|
|
379
|
+
})
|
|
380
|
+
expect((await screen.findAllByRole('menuitem', { name: 'Open' })).length).toBe(1)
|
|
381
|
+
expect(screen.getByRole('menuitemradio', { name: 'Row' }).getAttribute('aria-checked')).toBe('true')
|
|
278
382
|
})
|
|
279
383
|
|
|
280
|
-
it('lets a click follow the
|
|
384
|
+
it('lets a click follow the link when the editor is not editable', async () => {
|
|
281
385
|
followed.mockClear()
|
|
282
|
-
render(<Harness content={para(
|
|
386
|
+
render(<Harness content={para(ref(V))} />)
|
|
283
387
|
const chip = await screen.findByRole('link', { name: 'Item' })
|
|
284
388
|
act(() => editor!.setEditable(false))
|
|
285
389
|
fireEvent.click(chip)
|
package/src/ReferenceMenu.tsx
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* What a
|
|
3
|
-
*
|
|
2
|
+
* What a link in a document offers its writer: Open, and — alone on its line —
|
|
3
|
+
* Show as Row or Card (SPEC §13.3, "A reference's card"). Card puts its card in
|
|
4
|
+
* the row's place (`embed` `detailed`); Row puts the row back (no `embed`).
|
|
5
|
+
* Nothing writes `compact`. A chip in a sentence, and a person or a Folder alone
|
|
6
|
+
* on its line (which stay chips), offer Open only (`linkPlaceAt`).
|
|
4
7
|
*
|
|
5
|
-
* **Where it opens
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
8
|
+
* **Where it opens**: a click on a link while editing opens the menu instead
|
|
9
|
+
* of following it, and a lone-line link just put in — pasted, picked with `[`,
|
|
10
|
+
* or a pasted URL becoming its chip (`LinkChips`) — opens it by itself, so the
|
|
11
|
+
* card can be chosen at once. That menu takes no focus: the person is still
|
|
12
|
+
* writing, and typing on closes it with no card. Enter on a selected link opens
|
|
13
|
+
* it from the keyboard.
|
|
11
14
|
*
|
|
12
15
|
* **Keys while it is open**, handed over by the editor as the `/` menu's are:
|
|
13
16
|
* ↓ and ↑ move through the rows (nothing is highlighted until they do, so
|
|
@@ -20,12 +23,7 @@
|
|
|
20
23
|
*
|
|
21
24
|
* Two parts: the extension `ReferenceMenu` holds which chip the menu is open
|
|
22
25
|
* on, and `ReferenceMenuView` draws it beside `EditorContent`. Pair them with
|
|
23
|
-
* `ReferenceNode` and `
|
|
24
|
-
*
|
|
25
|
-
* Not yet what the record below says: it replaced the Card sizes with Show as
|
|
26
|
-
* Row or Card for a lone link, and Open only for a chip in a sentence. The
|
|
27
|
-
* shared editor's chip menu is changed to it next (WBD-4); until then this
|
|
28
|
-
* menu is the 14 October one.
|
|
26
|
+
* `ReferenceNode` and `LinkPlaces`.
|
|
29
27
|
*
|
|
30
28
|
* why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/a-lone-links-chip-menu-offers-row-or-card.md
|
|
31
29
|
*/
|
|
@@ -35,11 +33,12 @@ import { useEditorState } from '@tiptap/react'
|
|
|
35
33
|
import { NodeSelection, Plugin, PluginKey, TextSelection, type EditorState, type Transaction } from '@tiptap/pm/state'
|
|
36
34
|
import { ReplaceStep } from '@tiptap/pm/transform'
|
|
37
35
|
import type { EditorView } from '@tiptap/pm/view'
|
|
38
|
-
import { IconCheck } from '@tabler/icons-react'
|
|
36
|
+
import { IconCheck, IconExternalLink } from '@tabler/icons-react'
|
|
39
37
|
import { CHIPS_MADE } from './LinkChips'
|
|
38
|
+
import { linkPlaceAt } from './LinkPlaces'
|
|
40
39
|
import { MenuItem, MenuSection, MenuSeparator } from './Menu'
|
|
41
40
|
import { Popover } from './Popover'
|
|
42
|
-
import { setReferenceCardAt
|
|
41
|
+
import { setReferenceCardAt } from './ReferenceCards'
|
|
43
42
|
|
|
44
43
|
interface MenuState {
|
|
45
44
|
/** The chip's position. */
|
|
@@ -50,14 +49,16 @@ interface MenuState {
|
|
|
50
49
|
|
|
51
50
|
const key = new PluginKey<MenuState | null>('referenceMenu')
|
|
52
51
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
52
|
+
/** How a lone-line link can show. */
|
|
53
|
+
type ShowAs = 'row' | 'card'
|
|
54
|
+
|
|
55
|
+
const SHOW_AS: { as: ShowAs; label: string }[] = [
|
|
56
|
+
{ as: 'row', label: 'Row' },
|
|
57
|
+
{ as: 'card', label: 'Card' },
|
|
57
58
|
]
|
|
58
59
|
|
|
59
|
-
/** A row: Open, or a
|
|
60
|
-
type Row = 'open' |
|
|
60
|
+
/** A row of the menu: Open, or a way to show the link. */
|
|
61
|
+
type Row = 'open' | ShowAs
|
|
61
62
|
|
|
62
63
|
/** The chip's own control to click, or null. */
|
|
63
64
|
function chipTarget(view: EditorView, pos: number): HTMLElement | null {
|
|
@@ -72,12 +73,15 @@ function webHref(view: EditorView, pos: number): string {
|
|
|
72
73
|
}
|
|
73
74
|
|
|
74
75
|
/**
|
|
75
|
-
* The menu's rows: Open when there is something to open — the
|
|
76
|
-
* control, or its URL — and
|
|
76
|
+
* The menu's rows: Open when there is something to open — the link's own
|
|
77
|
+
* control, or its URL — and Row and Card when it is alone on its line and is
|
|
78
|
+
* neither a person nor a Folder (`linkPlaceAt`).
|
|
77
79
|
*/
|
|
78
80
|
function rowsOf(view: EditorView, pos: number): Row[] {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
+
// A link the app draws has its own control even before React has drawn it, as right after a `[` pick.
|
|
82
|
+
const drawn = !!view.state.doc.nodeAt(pos)?.attrs.uri && !!view.someProp('nodeViews', (views) => views?.reference)
|
|
83
|
+
const open: Row[] = drawn || chipTarget(view, pos) || webHref(view, pos) ? ['open'] : []
|
|
84
|
+
return linkPlaceAt(view.state.doc, pos) === 'chip' ? open : [...open, ...SHOW_AS.map((show) => show.as)]
|
|
81
85
|
}
|
|
82
86
|
|
|
83
87
|
/** A chip node, or null. */
|
|
@@ -114,6 +118,8 @@ function justPlaced(tr: Transaction, state: EditorState): number | null {
|
|
|
114
118
|
// The same chip as before the step — a key pressed after it, a card set on it — is not new. One put in just before
|
|
115
119
|
// an identical chip pushes that chip along, so it no longer maps onto `pos` (code review).
|
|
116
120
|
if (was && was.type.name === 'reference' && was.attrs.uri === before.attrs.uri && tr.mapping.map(old, 1) === pos) return null
|
|
121
|
+
// Only a link alone on its line has a card to choose; in a sentence the menu would offer Open alone.
|
|
122
|
+
if (linkPlaceAt(state.doc, pos) === 'chip') return null
|
|
117
123
|
return pos
|
|
118
124
|
}
|
|
119
125
|
|
|
@@ -144,7 +150,7 @@ function openChip(view: EditorView, pos: number): void {
|
|
|
144
150
|
function choose(editor: Editor, pos: number, row: Row | undefined): void {
|
|
145
151
|
dispatchMenu(editor.view, null)
|
|
146
152
|
if (row === 'open') openChip(editor.view, pos)
|
|
147
|
-
else if (row) setReferenceCardAt(editor, pos, row)
|
|
153
|
+
else if (row) setReferenceCardAt(editor, pos, row === 'card' ? 'detailed' : 'none')
|
|
148
154
|
}
|
|
149
155
|
|
|
150
156
|
/** Whether the menu is open, for an editor's own Escape: while it is, Escape closes it rather than leaving. */
|
|
@@ -248,12 +254,11 @@ export const ReferenceMenu = Extension.create({
|
|
|
248
254
|
},
|
|
249
255
|
})
|
|
250
256
|
|
|
251
|
-
/** What the panel draws: the
|
|
257
|
+
/** What the panel draws: the link the menu is open on, and how it shows. */
|
|
252
258
|
interface Shown {
|
|
253
259
|
pos: number
|
|
254
260
|
active: number
|
|
255
|
-
|
|
256
|
-
layout: ReferenceCardLayout
|
|
261
|
+
shownAs: ShowAs
|
|
257
262
|
}
|
|
258
263
|
|
|
259
264
|
export interface ReferenceMenuViewProps {
|
|
@@ -268,12 +273,9 @@ export function ReferenceMenuView({ editor }: ReferenceMenuViewProps) {
|
|
|
268
273
|
if (!ed || ed.isDestroyed) return null
|
|
269
274
|
const open = key.getState(ed.state)
|
|
270
275
|
if (!open) return null
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
// An unknown layout draws compact (§13.3), so it reads as compact here.
|
|
275
|
-
const layout: ReferenceCardLayout = embed === 'detailed' ? 'detailed' : typeof embed === 'string' && embed !== '' && embed !== 'none' ? 'compact' : 'none'
|
|
276
|
-
return { pos: open.pos, active: open.active, named: !!node.attrs.uri, layout }
|
|
276
|
+
if (!chipAt(ed.state, open.pos)) return null
|
|
277
|
+
// `compact` and a value this version does not know draw the row (§13.3), so they read as Row here.
|
|
278
|
+
return { pos: open.pos, active: open.active, shownAs: linkPlaceAt(ed.state.doc, open.pos) === 'card' ? 'card' : 'row' }
|
|
277
279
|
},
|
|
278
280
|
})
|
|
279
281
|
// A press anywhere in the panel keeps focus in the text, as a row's does: the editor saves and ends its menus when it loses focus.
|
|
@@ -290,6 +292,7 @@ export function ReferenceMenuView({ editor }: ReferenceMenuViewProps) {
|
|
|
290
292
|
const anchor = dom instanceof HTMLElement ? dom : null
|
|
291
293
|
const rows = live && shown ? rowsOf(live.view, shown.pos) : []
|
|
292
294
|
const opens = rows[0] === 'open'
|
|
295
|
+
const showAs = rows.includes('card')
|
|
293
296
|
const run = (row: Row) => {
|
|
294
297
|
if (editor && shown) choose(editor, shown.pos, row)
|
|
295
298
|
}
|
|
@@ -315,6 +318,7 @@ export function ReferenceMenuView({ editor }: ReferenceMenuViewProps) {
|
|
|
315
318
|
role="menuitem"
|
|
316
319
|
tabIndex={-1}
|
|
317
320
|
label="Open"
|
|
321
|
+
leading={<IconExternalLink size={16} />}
|
|
318
322
|
selected={shown.active === 0}
|
|
319
323
|
onMouseDown={(event) => {
|
|
320
324
|
event.preventDefault()
|
|
@@ -322,23 +326,23 @@ export function ReferenceMenuView({ editor }: ReferenceMenuViewProps) {
|
|
|
322
326
|
}}
|
|
323
327
|
/>
|
|
324
328
|
)}
|
|
325
|
-
{
|
|
329
|
+
{showAs && (
|
|
326
330
|
<>
|
|
327
331
|
{opens && <MenuSeparator />}
|
|
328
|
-
<div role="group" aria-label="
|
|
329
|
-
<MenuSection label="
|
|
330
|
-
{
|
|
332
|
+
<div role="group" aria-label="Show as">
|
|
333
|
+
<MenuSection label="Show as">
|
|
334
|
+
{SHOW_AS.map(({ as, label }, i) => (
|
|
331
335
|
<MenuItem
|
|
332
|
-
key={
|
|
336
|
+
key={as}
|
|
333
337
|
role="menuitemradio"
|
|
334
|
-
aria-checked={shown.
|
|
338
|
+
aria-checked={shown.shownAs === as}
|
|
335
339
|
tabIndex={-1}
|
|
336
340
|
label={label}
|
|
337
341
|
selected={shown.active === i + (opens ? 1 : 0)}
|
|
338
|
-
trailing={shown.
|
|
342
|
+
trailing={shown.shownAs === as ? <IconCheck size={16} aria-hidden /> : undefined}
|
|
339
343
|
onMouseDown={(event) => {
|
|
340
344
|
event.preventDefault()
|
|
341
|
-
run(
|
|
345
|
+
run(as)
|
|
342
346
|
}}
|
|
343
347
|
/>
|
|
344
348
|
))}
|