@estiva-app/ui 0.83.0 → 0.85.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/CommentBox.d.ts +12 -3
- package/dist/CommentBox.d.ts.map +1 -1
- package/dist/commentBoxFrame.d.ts +93 -0
- package/dist/commentBoxFrame.d.ts.map +1 -0
- package/dist/editor.d.ts +0 -1
- package/dist/editor.d.ts.map +1 -1
- package/dist/editor.js +445 -337
- package/dist/editor.js.map +4 -4
- package/dist/eslint/index.js +594 -559
- package/dist/eslint/index.js.map +4 -4
- package/dist/eslint/no-hand-rolled-behaviour.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/links.d.ts.map +1 -1
- package/dist/looks.d.ts +1 -1
- package/package.json +1 -1
- package/registry.json +16 -128
- package/src/CommentBox.mdx +15 -4
- package/src/CommentBox.stories.tsx +78 -2
- package/src/CommentBox.test.tsx +185 -3
- package/src/CommentBox.tsx +241 -76
- package/src/{ComposeBox.tsx → commentBoxFrame.tsx} +76 -25
- package/src/editor.ts +1 -1
- package/src/eslint/no-hand-rolled-behaviour.test.ts +27 -0
- package/src/eslint/no-hand-rolled-behaviour.ts +44 -0
- package/src/links.ts +4 -1
- package/src/looks.ts +1 -1
- package/src/registry/app.test.ts +3 -4
- package/src/registry/registry.test.ts +5 -5
- package/stories/Choosing.mdx +0 -1
- package/dist/ComposeBox.d.ts +0 -57
- package/dist/ComposeBox.d.ts.map +0 -1
- package/src/ComposeBox.mdx +0 -58
- package/src/ComposeBox.stories.tsx +0 -81
package/src/CommentBox.tsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Fragment, useCallback, useEffect, useRef, useState, type ComponentType, type DragEvent, type ReactNode } from 'react'
|
|
2
|
-
import { Extension, type AnyExtension } from '@tiptap/core'
|
|
3
|
-
import {
|
|
1
|
+
import { Fragment, useCallback, useEffect, useLayoutEffect, useRef, useState, type ComponentType, type DragEvent, type ReactNode } from 'react'
|
|
2
|
+
import { Extension, type AnyExtension, type EditorOptions } from '@tiptap/core'
|
|
3
|
+
import { Editor, type JSONContent } from '@tiptap/react'
|
|
4
4
|
import { IconLock, IconPaperclip, IconSquareRounded } from '@tabler/icons-react'
|
|
5
5
|
import { AttachmentCard } from './AttachmentCard'
|
|
6
6
|
import { ReferenceNode, type ReferenceViewProps } from './BlockNodes'
|
|
@@ -8,7 +8,7 @@ import { Button } from './Button'
|
|
|
8
8
|
import { Card } from './Card'
|
|
9
9
|
import { CardUnder } from './CardUnder'
|
|
10
10
|
import { cn } from './cn'
|
|
11
|
-
import {
|
|
11
|
+
import { BoxFrame, ComposeKeys, COMPOSE_EDITOR_CLASSES, isEmptyDoc as isEmpty, SendBox } from './commentBoxFrame'
|
|
12
12
|
import {
|
|
13
13
|
CaptionedReference,
|
|
14
14
|
PersonMention,
|
|
@@ -27,7 +27,6 @@ import { FieldLine } from './Field'
|
|
|
27
27
|
import { FilePicker } from './FilePicker'
|
|
28
28
|
import { Kbd } from './Kbd'
|
|
29
29
|
import { BoxKit, PasteOverWords } from './links'
|
|
30
|
-
import { WRITING_BOX_CLASSES } from './looks'
|
|
31
30
|
import { PastedImages } from './PastedImages'
|
|
32
31
|
import { KeptSelection } from './SelectionToolbar'
|
|
33
32
|
import { isSuggestionOpen } from './suggestionPopup'
|
|
@@ -36,7 +35,7 @@ import { ToolbarButton } from './Toolbar'
|
|
|
36
35
|
/**
|
|
37
36
|
* The one box every app writes a comment, a reply and a chat line in, from the
|
|
38
37
|
* approved prototype (`concept/comments-everywhere`, Miky 17 October): the
|
|
39
|
-
*
|
|
38
|
+
* box's one frame (`commentBoxFrame`) with everything each app built around it — `@`,
|
|
40
39
|
* `!@`, `[` and `/`, the keys, drafts, files, the edit mode, sending and a
|
|
41
40
|
* refused send. An app hands over only its people, its `[` sections, its `/`
|
|
42
41
|
* Insert rows and a send that says whether the relay accepted it.
|
|
@@ -163,6 +162,10 @@ export interface CommentBoxProps {
|
|
|
163
162
|
onSent?: (body: JSONContent, files: BoxFile[], under: BoxSentUnder) => void
|
|
164
163
|
/** Called when a send shown at once was refused, so the app takes it back. */
|
|
165
164
|
onRefused?: () => void
|
|
165
|
+
/** The bar down the text's left edge shows while this holds, besides while an urgent mention is in the box: the app's own marks (a highlight). */
|
|
166
|
+
marked?: (editor: Editor) => boolean
|
|
167
|
+
/** Called with the box's editor once it is drawn, and with `null` as it goes: an app's launcher inserts into the box last used. */
|
|
168
|
+
onEditor?: (editor: Editor | null) => void
|
|
166
169
|
className?: string
|
|
167
170
|
}
|
|
168
171
|
|
|
@@ -183,8 +186,8 @@ export const commentBoxRows: Readonly<Record<'file' | 'mention' | 'urgent' | 're
|
|
|
183
186
|
reference: { key: 'insert-reference', row: { label: 'Reference', shortcut: '[' }, keywords: ['[', 'Reference'], run: typeTrigger('[') },
|
|
184
187
|
}
|
|
185
188
|
|
|
186
|
-
/** Ctrl/Cmd+Enter sends as Enter does; Escape cancels an edit (an open list takes Escape first). */
|
|
187
|
-
const BoxKeys = Extension.create<{ onEscape: () => boolean }>({
|
|
189
|
+
/** Ctrl/Cmd+Enter sends as Enter does; Escape cancels an edit (an open list, or the selection strip, takes Escape first). */
|
|
190
|
+
const BoxKeys = Extension.create<{ onEscape: (editor: Editor) => boolean }>({
|
|
188
191
|
name: 'commentBoxKeys',
|
|
189
192
|
addOptions() {
|
|
190
193
|
return { onEscape: () => false }
|
|
@@ -193,7 +196,7 @@ const BoxKeys = Extension.create<{ onEscape: () => boolean }>({
|
|
|
193
196
|
return {
|
|
194
197
|
'Mod-Enter': ({ editor }) => (editor.storage as unknown as { composeKeys?: { send: (() => boolean) | null } }).composeKeys?.send?.() ?? false,
|
|
195
198
|
// Open now, with no grace: the first Escape closes a list, the next is a deliberate cancel (suggestionPopup).
|
|
196
|
-
Escape: () => (isSuggestionOpen() ? false : this.options.onEscape()),
|
|
199
|
+
Escape: ({ editor }) => (isSuggestionOpen() ? false : this.options.onEscape(editor as unknown as Editor)),
|
|
197
200
|
}
|
|
198
201
|
},
|
|
199
202
|
})
|
|
@@ -205,6 +208,176 @@ const stillNamed = (removed: ReadonlySet<string>, names: readonly string[]): Rea
|
|
|
205
208
|
const SQUARE = <IconSquareRounded size={16} stroke={1.5} className="text-text-secondary" />
|
|
206
209
|
const SMALL_SQUARE = <IconSquareRounded size={14} stroke={1.5} />
|
|
207
210
|
|
|
211
|
+
/** What the box's editor reads as it runs: the box holding it now, swapped in when a box takes a spare. */
|
|
212
|
+
interface BoxLive {
|
|
213
|
+
edit?: CommentBoxEdit
|
|
214
|
+
people: readonly MentionPerson[]
|
|
215
|
+
references?: CommentBoxProps['references']
|
|
216
|
+
referencesChanged?: CommentBoxProps['referencesChanged']
|
|
217
|
+
referenceIcon?: CommentBoxProps['referenceIcon']
|
|
218
|
+
/** Takes pasted pictures; false when the box takes no files. */
|
|
219
|
+
attach: (files: File[]) => boolean
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** One editor's link to the box holding it: the live props, and the `/` sections the box refills every render. */
|
|
223
|
+
interface BoxHold {
|
|
224
|
+
current: BoxLive
|
|
225
|
+
sections: SlashSection[]
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** What an app adds to every box's editor: a spare is built only for these. */
|
|
229
|
+
interface BoxApp {
|
|
230
|
+
extensions: readonly AnyExtension[]
|
|
231
|
+
referenceView: ComponentType<ReferenceViewProps> | null
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const NO_EXTENSIONS: readonly AnyExtension[] = []
|
|
235
|
+
const sameApp = (a: BoxApp, b: BoxApp) =>
|
|
236
|
+
a.referenceView === b.referenceView && a.extensions.length === b.extensions.length && a.extensions.every((one, i) => one === b.extensions[i])
|
|
237
|
+
|
|
238
|
+
function boxExtensions(hold: BoxHold, app: BoxApp): AnyExtension[] {
|
|
239
|
+
const iconFor = (uri: string | null) => hold.current.referenceIcon?.(uri)
|
|
240
|
+
return [
|
|
241
|
+
BoxKit.configure({ link: { openOnClick: false } }),
|
|
242
|
+
PasteOverWords,
|
|
243
|
+
KeptSelection,
|
|
244
|
+
ComposeKeys,
|
|
245
|
+
BoxKeys.configure({
|
|
246
|
+
onEscape: (ed) => {
|
|
247
|
+
const editing = hold.current.edit
|
|
248
|
+
if (!editing) return false
|
|
249
|
+
// Words selected: the strip is over them, and the first Escape closes it, leaving the caret at their end.
|
|
250
|
+
// The next is the deliberate cancel, as with an open list (Katerina, 21 Oct: the edit box gets the strip).
|
|
251
|
+
const { selection } = ed.state
|
|
252
|
+
if (!selection.empty) {
|
|
253
|
+
ed.commands.setTextSelection(selection.to)
|
|
254
|
+
return true
|
|
255
|
+
}
|
|
256
|
+
editing.onCancel()
|
|
257
|
+
return true
|
|
258
|
+
},
|
|
259
|
+
}),
|
|
260
|
+
PersonMention.configure({ people: () => hold.current.people }),
|
|
261
|
+
// An edit makes nobody urgent (CON-24): `!@` opens no list there, as `/` offers no Urgent.
|
|
262
|
+
UrgentPersonMention.extend({
|
|
263
|
+
addOptions() {
|
|
264
|
+
const parent = this.parent!()
|
|
265
|
+
return { ...parent, suggestion: { ...parent.suggestion, allow: () => !hold.current.edit } }
|
|
266
|
+
},
|
|
267
|
+
}).configure({ people: () => hold.current.people }),
|
|
268
|
+
CaptionedReference.configure({ icon: SMALL_SQUARE, iconFor }),
|
|
269
|
+
ReferenceTrigger.configure({
|
|
270
|
+
items: () => [],
|
|
271
|
+
sections: (query) => hold.current.references?.(query) ?? [],
|
|
272
|
+
subscribe: (listener) => hold.current.referencesChanged?.(listener) ?? (() => {}),
|
|
273
|
+
ariaLabel: 'References',
|
|
274
|
+
sectionLabel: 'References',
|
|
275
|
+
icon: SQUARE,
|
|
276
|
+
iconFor,
|
|
277
|
+
}),
|
|
278
|
+
SlashCommands.configure({ sections: hold.sections }),
|
|
279
|
+
ReferenceNode.configure({ view: app.referenceView }),
|
|
280
|
+
PastedImages.configure({ onImages: (files: File[]) => hold.current.attach(files) }),
|
|
281
|
+
...app.extensions,
|
|
282
|
+
]
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
interface Held {
|
|
286
|
+
editor: Editor
|
|
287
|
+
hold: BoxHold
|
|
288
|
+
destroyAt?: ReturnType<typeof setTimeout>
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function buildBox(app: BoxApp, options: Partial<EditorOptions>): Held {
|
|
292
|
+
const hold: BoxHold = { current: { people: [], attach: () => false }, sections: [] }
|
|
293
|
+
return { editor: new Editor({ ...options, extensions: boxExtensions(hold, app) }), hold }
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/*
|
|
297
|
+
A box's editor, built ahead, as Peek's composer has been since 4ef41d41:
|
|
298
|
+
building one (schema, commands, view) is the longest single step in drawing
|
|
299
|
+
a box. One is built while the page is idle for the app `keepCommentBoxWarm`
|
|
300
|
+
names, and the next empty box with those extensions takes it and builds the
|
|
301
|
+
next; any other box builds its own on the spot.
|
|
302
|
+
*/
|
|
303
|
+
let warmApp: BoxApp | null = null
|
|
304
|
+
let spare: Held | null = null
|
|
305
|
+
let sparing = false
|
|
306
|
+
|
|
307
|
+
function scheduleSpare() {
|
|
308
|
+
if (!warmApp || spare || sparing || typeof window === 'undefined' || !window.requestIdleCallback) return
|
|
309
|
+
sparing = true
|
|
310
|
+
window.requestIdleCallback(() => {
|
|
311
|
+
sparing = false
|
|
312
|
+
// No autofocus: it would fire on the detached spare. The box that takes it focuses it.
|
|
313
|
+
if (warmApp && !spare) spare = buildBox(warmApp, { autofocus: false })
|
|
314
|
+
})
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** Keeps an editor built ahead for the next empty box given these `extensions` and `referenceView`, so it opens at once. */
|
|
318
|
+
export function keepCommentBoxWarm(app: { extensions?: readonly AnyExtension[]; referenceView?: ComponentType<ReferenceViewProps> } = {}): void {
|
|
319
|
+
const next = { extensions: app.extensions ?? NO_EXTENSIONS, referenceView: app.referenceView ?? null }
|
|
320
|
+
if (warmApp && sameApp(warmApp, next)) return
|
|
321
|
+
warmApp = next
|
|
322
|
+
spare?.editor.destroy()
|
|
323
|
+
spare = null
|
|
324
|
+
scheduleSpare()
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/** `useEditor` with no deps, taking a spare when one fits: built once, given the latest `editorProps`, destroyed when the box goes. */
|
|
328
|
+
function useBoxEditor(app: BoxApp, content: JSONContent | undefined, options: Pick<EditorOptions, 'editable' | 'editorProps'>): Held {
|
|
329
|
+
const args = useRef({ app, content, options })
|
|
330
|
+
args.current = { app, content, options }
|
|
331
|
+
const [held, setHeld] = useState(() => {
|
|
332
|
+
const fits = !content && spare && !spare.editor.isDestroyed && warmApp && sameApp(warmApp, app)
|
|
333
|
+
const taken = fits ? spare : null
|
|
334
|
+
if (taken) {
|
|
335
|
+
spare = null
|
|
336
|
+
taken.editor.setOptions(options)
|
|
337
|
+
}
|
|
338
|
+
const one = taken ?? buildBox(app, { ...options, content })
|
|
339
|
+
// A render thrown away never mounts: what it built goes with it. Later than React lets a transition run (5 s).
|
|
340
|
+
one.destroyAt = setTimeout(() => one.editor.destroy(), 10_000)
|
|
341
|
+
scheduleSpare()
|
|
342
|
+
return one
|
|
343
|
+
})
|
|
344
|
+
// Mounted, it is this box's until it unmounts — and not remounted at once, as in StrictMode.
|
|
345
|
+
useLayoutEffect(() => {
|
|
346
|
+
clearTimeout(held.destroyAt)
|
|
347
|
+
/*
|
|
348
|
+
Shown again after React kept its state but ran this cleanup (a Suspense boundary
|
|
349
|
+
that suspended again, an <Activity> that hid it): the editor went, so build one
|
|
350
|
+
from what the box would open with now — its draft holds what was typed.
|
|
351
|
+
*/
|
|
352
|
+
if (held.editor.isDestroyed) {
|
|
353
|
+
const { app: now, content: opening, options: current } = args.current
|
|
354
|
+
setHeld(buildBox(now, { ...current, content: opening }))
|
|
355
|
+
return
|
|
356
|
+
}
|
|
357
|
+
return () => {
|
|
358
|
+
held.destroyAt = setTimeout(() => held.editor.destroy(), 1)
|
|
359
|
+
}
|
|
360
|
+
}, [held])
|
|
361
|
+
// The attributes are what changes after it is built: the label.
|
|
362
|
+
const props = useRef(options.editorProps)
|
|
363
|
+
props.current = options.editorProps
|
|
364
|
+
const attributes = JSON.stringify(options.editorProps.attributes ?? {})
|
|
365
|
+
useEffect(() => {
|
|
366
|
+
if (!held.editor.isDestroyed) held.editor.setOptions({ editorProps: props.current })
|
|
367
|
+
}, [held, attributes])
|
|
368
|
+
return held
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/** Whether the document holds an urgent mention: the box draws its bar while one is in it. */
|
|
372
|
+
const holdsUrgent = (editor: Editor): boolean => {
|
|
373
|
+
let found = false
|
|
374
|
+
editor.state.doc.descendants((node) => {
|
|
375
|
+
if (node.type.name === 'urgentMention') found = true
|
|
376
|
+
return !found
|
|
377
|
+
})
|
|
378
|
+
return found
|
|
379
|
+
}
|
|
380
|
+
|
|
208
381
|
let fileCount = 0
|
|
209
382
|
|
|
210
383
|
export function CommentBox({
|
|
@@ -225,10 +398,12 @@ export function CommentBox({
|
|
|
225
398
|
accept,
|
|
226
399
|
closed,
|
|
227
400
|
referenceView,
|
|
228
|
-
extensions =
|
|
401
|
+
extensions = NO_EXTENSIONS,
|
|
229
402
|
autoFocus,
|
|
230
403
|
onSent,
|
|
231
404
|
onRefused,
|
|
405
|
+
marked,
|
|
406
|
+
onEditor,
|
|
232
407
|
className,
|
|
233
408
|
}: CommentBoxProps) {
|
|
234
409
|
const [files, setFiles] = useState<BoxFile[]>(edit?.files ?? [])
|
|
@@ -239,9 +414,9 @@ export function CommentBox({
|
|
|
239
414
|
const [removed, setRemoved] = useState<ReadonlySet<string>>(() => new Set())
|
|
240
415
|
const picker = useRef<HTMLInputElement>(null)
|
|
241
416
|
const root = useRef<HTMLDivElement>(null)
|
|
242
|
-
// The latest props, for
|
|
243
|
-
const latest = useRef({ edit,
|
|
244
|
-
latest.current = { edit,
|
|
417
|
+
// The latest props, for callbacks made once.
|
|
418
|
+
const latest = useRef({ edit, upload, draft, under })
|
|
419
|
+
latest.current = { edit, upload, draft, under }
|
|
245
420
|
const save = useRef<() => boolean>(() => false)
|
|
246
421
|
|
|
247
422
|
const attachFiles = useCallback((picked: File[]): boolean => {
|
|
@@ -264,53 +439,36 @@ export function CommentBox({
|
|
|
264
439
|
|
|
265
440
|
// An edit cannot add a file or make a mention urgent, as in every app before the box; no upload, no File.
|
|
266
441
|
const rows = insert.filter((c) => !((edit || !upload) && c.key === commentBoxRows.file.key) && !(edit && c.key === commentBoxRows.urgent.key))
|
|
267
|
-
|
|
268
|
-
// comes or goes with `upload`, `insert` or `code` after the box opened is offered as it is now.
|
|
269
|
-
const sections = useRef<SlashSection[]>([]).current
|
|
270
|
-
sections.splice(0, sections.length, formatSection(code), ...(rows.length > 0 ? [{ label: 'Insert', commands: rows }] : []))
|
|
271
|
-
const iconFor = useCallback((uri: string | null) => latest.current.referenceIcon?.(uri), [])
|
|
272
|
-
|
|
273
|
-
const editor = useEditor({
|
|
274
|
-
extensions: [
|
|
275
|
-
BoxKit.configure({ link: { openOnClick: false } }),
|
|
276
|
-
PasteOverWords,
|
|
277
|
-
KeptSelection,
|
|
278
|
-
ComposeKeys,
|
|
279
|
-
BoxKeys.configure({
|
|
280
|
-
onEscape: () => {
|
|
281
|
-
const editing = latest.current.edit
|
|
282
|
-
if (!editing) return false
|
|
283
|
-
editing.onCancel()
|
|
284
|
-
return true
|
|
285
|
-
},
|
|
286
|
-
}),
|
|
287
|
-
PersonMention.configure({ people: () => latest.current.people }),
|
|
288
|
-
// An edit makes nobody urgent (CON-24): `!@` opens no list there, as `/` offers no Urgent.
|
|
289
|
-
UrgentPersonMention.extend({
|
|
290
|
-
addOptions() {
|
|
291
|
-
const parent = this.parent!()
|
|
292
|
-
return { ...parent, suggestion: { ...parent.suggestion, allow: () => !latest.current.edit } }
|
|
293
|
-
},
|
|
294
|
-
}).configure({ people: () => latest.current.people }),
|
|
295
|
-
CaptionedReference.configure({ icon: SMALL_SQUARE, iconFor }),
|
|
296
|
-
ReferenceTrigger.configure({
|
|
297
|
-
items: () => [],
|
|
298
|
-
sections: (query) => latest.current.references?.(query) ?? [],
|
|
299
|
-
subscribe: (listener) => latest.current.referencesChanged?.(listener) ?? (() => {}),
|
|
300
|
-
ariaLabel: 'References',
|
|
301
|
-
sectionLabel: 'References',
|
|
302
|
-
icon: SQUARE,
|
|
303
|
-
iconFor,
|
|
304
|
-
}),
|
|
305
|
-
SlashCommands.configure({ sections }),
|
|
306
|
-
ReferenceNode.configure({ view: referenceView ?? null }),
|
|
307
|
-
PastedImages.configure({ onImages: attachFiles }),
|
|
308
|
-
...extensions,
|
|
309
|
-
],
|
|
310
|
-
content: edit?.body ?? draft?.read(),
|
|
442
|
+
const { editor, hold } = useBoxEditor({ extensions, referenceView: referenceView ?? null }, edit?.body ?? draft?.read(), {
|
|
311
443
|
editable: !closed,
|
|
312
444
|
editorProps: { attributes: { class: COMPOSE_EDITOR_CLASSES, role: 'textbox', 'aria-multiline': 'true', 'aria-label': label } },
|
|
313
445
|
})
|
|
446
|
+
// The editor's callbacks are made once, with the editor: they read this box's latest props through its hold.
|
|
447
|
+
hold.current = { edit, people, references, referencesChanged, referenceIcon, attach: attachFiles }
|
|
448
|
+
// `/` reads its sections each time it opens, from this one array: refilled every render, so a row that
|
|
449
|
+
// comes or goes with `upload`, `insert` or `code` after the box opened is offered as it is now.
|
|
450
|
+
hold.sections.splice(0, hold.sections.length, formatSection(code), ...(rows.length > 0 ? [{ label: 'Insert', commands: rows }] : []))
|
|
451
|
+
|
|
452
|
+
// The bar down the text's left edge, while an urgent mention or the app's own mark is in the box.
|
|
453
|
+
const [marking, setMarking] = useState(false)
|
|
454
|
+
const markedBy = useRef(marked)
|
|
455
|
+
markedBy.current = marked
|
|
456
|
+
// Before paint: an edit or a draft opening with an urgent mention draws its bar in its first frame.
|
|
457
|
+
useLayoutEffect(() => {
|
|
458
|
+
const read = () => setMarking(holdsUrgent(editor) || !!markedBy.current?.(editor))
|
|
459
|
+
read()
|
|
460
|
+
editor.on('update', read)
|
|
461
|
+
return () => {
|
|
462
|
+
editor.off('update', read)
|
|
463
|
+
}
|
|
464
|
+
}, [editor])
|
|
465
|
+
|
|
466
|
+
const handOut = useRef(onEditor)
|
|
467
|
+
handOut.current = onEditor
|
|
468
|
+
useEffect(() => {
|
|
469
|
+
handOut.current?.(editor)
|
|
470
|
+
return () => handOut.current?.(null)
|
|
471
|
+
}, [editor])
|
|
314
472
|
|
|
315
473
|
// Drafts: every change is kept, and an empty box is no draft.
|
|
316
474
|
useEffect(() => {
|
|
@@ -390,7 +548,7 @@ export function CommentBox({
|
|
|
390
548
|
setRefused(null)
|
|
391
549
|
const answered = (answer: Promise<SendAnswer>) => answer.catch((): SendAnswer => ({ accepted: false, reason: 'try again in a moment' }))
|
|
392
550
|
if (showsAtOnce) {
|
|
393
|
-
// Shown at once: the box empties now (
|
|
551
|
+
// Shown at once: the box empties now (the send box clears it); a refusal puts the words back above anything written since,
|
|
394
552
|
// and what ✕ took off with them.
|
|
395
553
|
const took = removed
|
|
396
554
|
setFiles((all) => all.filter((f) => f.state !== 'ready'))
|
|
@@ -447,7 +605,7 @@ export function CommentBox({
|
|
|
447
605
|
return true
|
|
448
606
|
}
|
|
449
607
|
|
|
450
|
-
// Enter and Ctrl/Cmd+Enter save an edit; the send box wires them
|
|
608
|
+
// Enter and Ctrl/Cmd+Enter save an edit; the send box wires them itself.
|
|
451
609
|
useEffect(() => {
|
|
452
610
|
if (!editor || !edit) return
|
|
453
611
|
const storage = (editor.storage as unknown as { composeKeys?: { send: (() => boolean) | null } }).composeKeys
|
|
@@ -507,23 +665,29 @@ export function CommentBox({
|
|
|
507
665
|
if (edit) {
|
|
508
666
|
return (
|
|
509
667
|
<div className={cn('flex flex-col gap-1', className)}>
|
|
510
|
-
<
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
668
|
+
<BoxFrame
|
|
669
|
+
editor={editor}
|
|
670
|
+
marked={marking}
|
|
671
|
+
// The same strip as the send box: writing and editing work the same way (Katerina, 21 Oct).
|
|
672
|
+
selection={{ link: true }}
|
|
673
|
+
attachments={fileCards}
|
|
674
|
+
className={cn(sending && '[&_.ProseMirror]:text-text-muted')}
|
|
675
|
+
footer={
|
|
676
|
+
<div className="flex items-center justify-between gap-2">
|
|
677
|
+
<span className="flex items-center gap-1 text-caption text-text-muted">
|
|
678
|
+
<Kbd>Esc</Kbd> to cancel · <Kbd>Enter</Kbd> to save
|
|
679
|
+
</span>
|
|
680
|
+
<div className="flex items-center gap-2">
|
|
681
|
+
<Button variant="outlined" size="small" onClick={edit.onCancel}>
|
|
682
|
+
Cancel
|
|
683
|
+
</Button>
|
|
684
|
+
<Button variant="primary" size="small" disabled={uploading || sending} onClick={() => save.current()}>
|
|
685
|
+
Save
|
|
686
|
+
</Button>
|
|
687
|
+
</div>
|
|
524
688
|
</div>
|
|
525
|
-
|
|
526
|
-
|
|
689
|
+
}
|
|
690
|
+
/>
|
|
527
691
|
{underBox}
|
|
528
692
|
{sending && <FieldLine>Saving…</FieldLine>}
|
|
529
693
|
{refusedLine}
|
|
@@ -540,7 +704,7 @@ export function CommentBox({
|
|
|
540
704
|
|
|
541
705
|
return (
|
|
542
706
|
<div ref={root} className={cn('flex flex-col gap-1', className)} onDragOver={upload ? (e) => e.preventDefault() : undefined} onDrop={onDrop}>
|
|
543
|
-
<
|
|
707
|
+
<SendBox
|
|
544
708
|
editor={editor}
|
|
545
709
|
onSend={go}
|
|
546
710
|
placeholder={placeholder}
|
|
@@ -553,6 +717,7 @@ export function CommentBox({
|
|
|
553
717
|
}
|
|
554
718
|
attachments={fileCards}
|
|
555
719
|
ready={ready}
|
|
720
|
+
marked={marking}
|
|
556
721
|
holding={uploading || failed > 0 || sending}
|
|
557
722
|
selection={{ link: true }}
|
|
558
723
|
className={cn(sending && '[&_[data-composer-editor]]:text-text-muted')}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// @estiva-escape(component-has-a-page, component-has-a-story): CommentBox's own frame, not offered to apps — CommentBox's page and stories show it (Katerina, 21 Oct: one box)
|
|
1
2
|
import { useCallback, useEffect, useState, type ReactNode } from 'react'
|
|
2
3
|
import { Extension } from '@tiptap/core'
|
|
3
4
|
import { EditorContent, type Editor } from '@tiptap/react'
|
|
@@ -6,21 +7,37 @@ import { IconButton } from './IconButton'
|
|
|
6
7
|
import { Toolbar } from './Toolbar'
|
|
7
8
|
import { cn } from './cn'
|
|
8
9
|
import { richTextClassName } from './RichText'
|
|
10
|
+
import { ScrollArea } from './ScrollArea'
|
|
9
11
|
import { WRITING_BOX_CLASSES } from './looks'
|
|
10
12
|
import { SelectionToolbar, type SelectionToolbarProps } from './SelectionToolbar'
|
|
11
13
|
import { isSuggestionActive } from './suggestionPopup'
|
|
12
14
|
|
|
13
15
|
/**
|
|
14
|
-
* The
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
16
|
+
* The frame `CommentBox` writes in, from Katerina's Leaf prototype (9
|
|
17
|
+
* October): the text, scrolling in a `ScrollArea` past half the window, the
|
|
18
|
+
* bar down its left while it is marked, what is attached, and a footer. The
|
|
19
|
+
* send box and the edit box are this one frame with different footers, so a
|
|
20
|
+
* change to the box is made once (Katerina, 21 Oct: the scrollbar had to be
|
|
21
|
+
* fixed in two copies). It was `ComposeBox`, a part of its own until 0.85.0;
|
|
22
|
+
* no app used it, and two box names invited the wrong pick.
|
|
19
23
|
*/
|
|
20
24
|
|
|
21
|
-
/** The
|
|
25
|
+
/** The classes the box's editor wears: rich text with no outline. It never scrolls itself; the box does, in a `ScrollArea`. */
|
|
22
26
|
export const COMPOSE_EDITOR_CLASSES = richTextClassName('default', 'outline-none w-full bg-transparent min-h-[20px] [&>p+p]:-mt-1')
|
|
23
27
|
|
|
28
|
+
/**
|
|
29
|
+
* A long draft stops at half the window and scrolls inside the box, so its
|
|
30
|
+
* first line can be reached (PEE-59). The package's `ScrollArea` scrolls it,
|
|
31
|
+
* not the browser: its bar, and one that shows while there is more (Katerina,
|
|
32
|
+
* 21 Oct: the browser's bar, and none at all in some browsers).
|
|
33
|
+
*
|
|
34
|
+
* The region reaches into the box's right padding (the box's `p-3`) and its
|
|
35
|
+
* content gives it back, so the text ends where it did and the bar sits 3px
|
|
36
|
+
* from the box's edge, as every other box's bar does (Katerina, 21 Oct: the gap
|
|
37
|
+
* on the right; measured 15px before, 3px after).
|
|
38
|
+
*/
|
|
39
|
+
export const COMPOSE_SCROLL_CLASSES = { area: '-mr-3', viewport: 'max-h-[50vh]', content: 'pr-3' }
|
|
40
|
+
|
|
24
41
|
type ComposeKeysStorage = { send: (() => boolean) | null }
|
|
25
42
|
|
|
26
43
|
/**
|
|
@@ -71,8 +88,45 @@ export function isEmptyDoc(editor: Editor): boolean {
|
|
|
71
88
|
return empty
|
|
72
89
|
}
|
|
73
90
|
|
|
74
|
-
export interface
|
|
75
|
-
/** The
|
|
91
|
+
export interface BoxFrameProps {
|
|
92
|
+
/** The box's editor, with `ComposeKeys` in its extensions and `COMPOSE_EDITOR_CLASSES` on it. `null` while it loads. */
|
|
93
|
+
editor: Editor | null
|
|
94
|
+
/** The bar down the text's left: an urgent mention or a highlight, as Peek draws it. */
|
|
95
|
+
marked?: boolean
|
|
96
|
+
/** The selection strip over the text, with these buttons. Absent, no strip (the edit box). */
|
|
97
|
+
selection?: Pick<SelectionToolbarProps, 'marks' | 'link' | 'actions'>
|
|
98
|
+
/** Said while the text is empty: words, and a `Kbd` where a key helps. */
|
|
99
|
+
placeholder?: ReactNode
|
|
100
|
+
empty?: boolean
|
|
101
|
+
/** What is attached, under the text: `AttachmentCard pending`s. */
|
|
102
|
+
attachments?: ReactNode
|
|
103
|
+
/** The row under it all: the tools and Send, or the keys and Cancel/Save. */
|
|
104
|
+
footer: ReactNode
|
|
105
|
+
/** On the box itself. */
|
|
106
|
+
className?: string
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The one frame both boxes draw: the bordered box, its text in a `ScrollArea`, what is attached, the footer. */
|
|
110
|
+
export function BoxFrame({ editor, marked = false, selection, placeholder, empty = false, attachments, footer, className }: BoxFrameProps) {
|
|
111
|
+
return (
|
|
112
|
+
<div className={cn(WRITING_BOX_CLASSES, 'focus-within:border-border-strong transition-colors', className)}>
|
|
113
|
+
<div data-composer-editor data-marked={marked || undefined} className={cn('relative min-h-[20px] transition-all', marked && 'border-l-4 border-border-strong pl-2')}>
|
|
114
|
+
<ScrollArea className={COMPOSE_SCROLL_CLASSES.area} viewportClassName={COMPOSE_SCROLL_CLASSES.viewport} contentClassName={COMPOSE_SCROLL_CLASSES.content}>
|
|
115
|
+
<EditorContent editor={editor} />
|
|
116
|
+
</ScrollArea>
|
|
117
|
+
{selection && <SelectionToolbar editor={editor} {...selection} />}
|
|
118
|
+
{empty && placeholder != null && (
|
|
119
|
+
<div className="pointer-events-none absolute inset-0 flex flex-wrap items-center gap-1 text-body-2 text-text-muted">{placeholder}</div>
|
|
120
|
+
)}
|
|
121
|
+
</div>
|
|
122
|
+
{attachments && <div className="flex flex-wrap gap-2">{attachments}</div>}
|
|
123
|
+
{footer}
|
|
124
|
+
</div>
|
|
125
|
+
)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export interface SendBoxProps {
|
|
129
|
+
/** The box's editor. `null` while it loads. */
|
|
76
130
|
editor: Editor | null
|
|
77
131
|
/**
|
|
78
132
|
* Send what is written. The box empties afterwards — unless this returns
|
|
@@ -97,11 +151,10 @@ export interface ComposeBoxProps {
|
|
|
97
151
|
}
|
|
98
152
|
|
|
99
153
|
/**
|
|
100
|
-
* The box
|
|
101
|
-
*
|
|
102
|
-
* button.
|
|
154
|
+
* The send box: the frame with the selection strip over the text, the tools
|
|
155
|
+
* and Send under it. Enter sends (`ComposeKeys`), and so does the button.
|
|
103
156
|
*/
|
|
104
|
-
export function
|
|
157
|
+
export function SendBox({ editor, onSend, placeholder, tools, attachments, ready = false, holding = false, marked = false, selection, className }: SendBoxProps) {
|
|
105
158
|
const [empty, setEmpty] = useState(true)
|
|
106
159
|
const canSend = (!empty || ready) && !holding
|
|
107
160
|
|
|
@@ -136,18 +189,15 @@ export function ComposeBox({ editor, onSend, placeholder, tools, attachments, re
|
|
|
136
189
|
|
|
137
190
|
return (
|
|
138
191
|
<div className={cn('relative', className)}>
|
|
139
|
-
<
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
{attachments && <div className="flex flex-wrap gap-2">{attachments}</div>}
|
|
149
|
-
|
|
150
|
-
{/* The tools are one strip — one Tab stop, arrows along it; Send, the answer rather than a tool, keeps its own. */}
|
|
192
|
+
<BoxFrame
|
|
193
|
+
editor={editor}
|
|
194
|
+
marked={marked}
|
|
195
|
+
selection={selection ?? {}}
|
|
196
|
+
placeholder={placeholder}
|
|
197
|
+
empty={empty}
|
|
198
|
+
attachments={attachments}
|
|
199
|
+
footer={
|
|
200
|
+
// The tools are one strip — one Tab stop, arrows along it; Send, the answer rather than a tool, keeps its own.
|
|
151
201
|
<div className="flex items-center justify-between">
|
|
152
202
|
{tools ? (
|
|
153
203
|
<Toolbar aria-label="Compose tools" surface={false} className="flex items-center gap-1">
|
|
@@ -178,7 +228,8 @@ export function ComposeBox({ editor, onSend, placeholder, tools, attachments, re
|
|
|
178
228
|
</IconButton>
|
|
179
229
|
</div>
|
|
180
230
|
</div>
|
|
181
|
-
|
|
231
|
+
}
|
|
232
|
+
/>
|
|
182
233
|
</div>
|
|
183
234
|
)
|
|
184
235
|
}
|
package/src/editor.ts
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* `Toolbar`, `MenuItem`, `SuggestionMenu` — is the main entry's own, not a copy.
|
|
9
9
|
*/
|
|
10
10
|
export { KeptSelection, SelectionToolbar, keepSelection, releaseSelection, normalizeHref, type MarkId, type SelectionAction, type SelectionToolbarProps } from './SelectionToolbar'
|
|
11
|
-
|
|
11
|
+
|
|
12
12
|
export {
|
|
13
13
|
CommentBox,
|
|
14
14
|
commentBoxRows,
|
|
@@ -39,6 +39,13 @@ tester.run('no-hand-rolled-behaviour', noHandRolledBehaviour, {
|
|
|
39
39
|
name: 'a global listener kept with its reason',
|
|
40
40
|
code: 'export function probe() {\n // @estiva-escape(no-hand-rolled-behaviour): the frame measures the window, which no floating part owns\n window.addEventListener("resize", () => {})\n}\n',
|
|
41
41
|
},
|
|
42
|
+
{ name: "the package's ScrollArea, its cap on the viewport", code: 'export const Probe = () => <ScrollArea viewportClassName="max-h-[50vh]" />\n' },
|
|
43
|
+
{ name: 'a box that clips, which draws no bar', code: 'export const Probe = () => <div className="overflow-hidden" />\n' },
|
|
44
|
+
{ name: 'the class named in a comment', code: '// a hand-made overflow-y-auto box never scrolled\nexport const Probe = () => null\n' },
|
|
45
|
+
{
|
|
46
|
+
name: 'a scroll class kept with its reason',
|
|
47
|
+
code: "// @estiva-escape(no-hand-rolled-behaviour): a probe that keeps the browser bar on purpose\nexport const PROBE_CLASSES = 'overflow-y-auto'\n",
|
|
48
|
+
},
|
|
42
49
|
],
|
|
43
50
|
invalid: [
|
|
44
51
|
{
|
|
@@ -66,5 +73,25 @@ tester.run('no-hand-rolled-behaviour', noHandRolledBehaviour, {
|
|
|
66
73
|
code: 'export function probe() {\n document.addEventListener("keydown", () => {})\n}\n',
|
|
67
74
|
errors: [{ messageId: 'listener', data: { event: 'keydown', target: 'document' } }],
|
|
68
75
|
},
|
|
76
|
+
{
|
|
77
|
+
name: "the comment box's long draft, as the review of 0.83 found it",
|
|
78
|
+
code: "export const COMPOSE_EDITOR_CLASSES = 'outline-none max-h-[50vh] overflow-y-auto'\n",
|
|
79
|
+
errors: [{ messageId: 'scroll', data: { what: 'overflow-y-auto' } }],
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
name: 'a scroll class in a className, behind a variant',
|
|
83
|
+
code: 'export const Probe = () => <div className="[&_pre]:overflow-x-auto" />\n',
|
|
84
|
+
errors: [{ messageId: 'scroll', data: { what: '[&_pre]:overflow-x-auto' } }],
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
name: 'a scroll class in a template',
|
|
88
|
+
code: 'export const probe = (h: string) => `max-h-${h} overflow-auto`\n',
|
|
89
|
+
errors: [{ messageId: 'scroll', data: { what: 'overflow-auto' } }],
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
name: 'a style that scrolls',
|
|
93
|
+
code: "export const Probe = () => <div style={{ overflowY: 'scroll' }} />\n",
|
|
94
|
+
errors: [{ messageId: 'scroll', data: { what: "overflowY: 'scroll'" } }],
|
|
95
|
+
},
|
|
69
96
|
],
|
|
70
97
|
})
|