@remit/ui 0.0.213 → 0.0.214

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remit/ui",
3
- "version": "0.0.213",
3
+ "version": "0.0.214",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "src"
@@ -10,7 +10,11 @@ import { PlainTextEditor } from "./plain-text-editor.js";
10
10
  import { markdownToHtml } from "./rich-text-document.js";
11
11
  import { RichTextEditor } from "./rich-text-editor.js";
12
12
  import type { SpellcheckOptions } from "./rich-text-spellcheck.js";
13
- import type { ComposeCaret, RichTextValue } from "./rich-text-value.js";
13
+ import type {
14
+ ComposeCaret,
15
+ ComposeInsertion,
16
+ RichTextValue,
17
+ } from "./rich-text-value.js";
14
18
  import { useComposeLanguage } from "./use-compose-language.js";
15
19
 
16
20
  export interface ConversionFailure {
@@ -69,6 +73,8 @@ export interface ComposeBodyProps {
69
73
  * the browser's.
70
74
  */
71
75
  spellcheck?: SpellcheckOptions;
76
+ insertion?: ComposeInsertion;
77
+ onInserted?: (version: number) => void;
72
78
  }
73
79
 
74
80
  /**
@@ -91,6 +97,8 @@ export const ComposeBody = ({
91
97
  initialLanguage,
92
98
  onLanguageChange,
93
99
  spellcheck,
100
+ insertion,
101
+ onInserted,
94
102
  }: ComposeBodyProps) => {
95
103
  const [richHtml, setRichHtml] = useState(initialHtml);
96
104
  const [richGeneration, setRichGeneration] = useState(0);
@@ -195,6 +203,8 @@ export const ComposeBody = ({
195
203
  initialCaret={focusSwitchedSurface ? "end" : initialCaret}
196
204
  lang={language}
197
205
  trailing={trailing}
206
+ insertion={insertion}
207
+ onInserted={onInserted}
198
208
  />
199
209
  ) : (
200
210
  <RichTextEditor
@@ -206,6 +216,8 @@ export const ComposeBody = ({
206
216
  lang={language}
207
217
  trailing={trailing}
208
218
  spellcheck={spellcheck}
219
+ insertion={insertion}
220
+ onInserted={onInserted}
209
221
  />
210
222
  )}
211
223
  <ConfirmDialog
@@ -1,4 +1,4 @@
1
- import { CalendarDays, Copy } from "lucide-react";
1
+ import { CalendarDays, Reply } from "lucide-react";
2
2
  import { type ReactNode, useState } from "react";
3
3
  import { cn } from "../lib/cn.js";
4
4
  import { Button } from "./button.js";
@@ -82,10 +82,6 @@ export interface CalendarProseIntel {
82
82
  slots: CalendarSlotPick[];
83
83
  /** Start clocks already ticked into the reply. */
84
84
  picked: readonly string[];
85
- /** What became of the last copy of the picked times. */
86
- copy?: "idle" | "copied" | "failed";
87
- /** The picked times as text, offered to select by hand when copying failed. */
88
- copyText?: string;
89
85
  }
90
86
 
91
87
  export interface IntelligenceCalendarData {
@@ -123,7 +119,7 @@ export interface IntelligenceCalendarActions {
123
119
  onRemoveInvite?: () => void;
124
120
  onOpenNewerInvite?: () => void;
125
121
  onToggleSlot: (slot: CalendarSlotPick) => void;
126
- onCopySlots?: () => void;
122
+ onReplyWithSlots?: () => void;
127
123
  onAddSuggestion: (suggestionId: string, timeZone: string) => void;
128
124
  onReviewSuggestion?: (suggestionId: string, timeZone: string) => void;
129
125
  onDismissSuggestion: (suggestionId: string) => void;
@@ -277,41 +273,20 @@ export function IntelligenceCalendar({
277
273
  touch={touch}
278
274
  scroll
279
275
  />
280
- {actions.onCopySlots ? (
276
+ {actions.onReplyWithSlots ? (
281
277
  <div className="mt-2 flex flex-col gap-1.5">
282
278
  <Button
283
279
  variant="secondary"
284
280
  size={touch ? "md" : "sm"}
285
- icon={<Copy className="size-3.5" />}
286
- onClick={actions.onCopySlots}
281
+ icon={<Reply className="size-3.5" />}
282
+ onClick={actions.onReplyWithSlots}
287
283
  disabled={picked.size === 0}
288
284
  className={cn("self-start", touch && "min-h-11")}
289
285
  >
290
- Copy picked times
286
+ Reply with these times
291
287
  </Button>
292
- {prose.copy === "copied" && (
293
- <p role="status" className="text-2xs text-positive">
294
- Copied. Paste them into your reply.
295
- </p>
296
- )}
297
- {prose.copy === "failed" && (
298
- <div role="alert" className="flex flex-col gap-1">
299
- <p className="text-2xs text-danger">
300
- This page can't copy for you. Select the times below and
301
- copy them yourself.
302
- </p>
303
- <textarea
304
- readOnly
305
- aria-label="Picked times"
306
- value={prose.copyText ?? ""}
307
- onFocus={(event) => event.currentTarget.select()}
308
- rows={2}
309
- className="w-full resize-none rounded-md border border-line bg-surface p-1.5 text-xs text-fg"
310
- />
311
- </div>
312
- )}
313
288
  <p className="text-2xs text-fg-subtle">
314
- Picked slots are copied as plain text. Nothing is booked.
289
+ Picked slots go into the reply as plain text. Nothing is booked.
315
290
  </p>
316
291
  </div>
317
292
  ) : (
@@ -54,7 +54,6 @@ function PanelDemo({
54
54
  hideCloseButton,
55
55
  touch,
56
56
  className,
57
- copyFails = false,
58
57
  }: {
59
58
  sender: IntelligenceData;
60
59
  calendar: IntelligenceCalendarData;
@@ -62,8 +61,6 @@ function PanelDemo({
62
61
  hideCloseButton?: boolean;
63
62
  touch?: boolean;
64
63
  className?: string;
65
- /** The page cannot reach the clipboard, as on plain http. */
66
- copyFails?: boolean;
67
64
  }) {
68
65
  const [tab, setTab] = useState<IntelligenceTabId>(initialTab);
69
66
  const [rsvp, setRsvp] = useState<RsvpState>(
@@ -72,9 +69,6 @@ function PanelDemo({
72
69
  const [removed, setRemoved] = useState(false);
73
70
  const [offering, setOffering] = useState(calendar.prose !== undefined);
74
71
  const [picked, setPicked] = useState<string[]>([]);
75
- const [copy, setCopy] = useState<"idle" | "copied" | "failed">(
76
- calendar.prose?.copy ?? "idle",
77
- );
78
72
  const [dropped, setDropped] = useState<string[]>([]);
79
73
  const [selectedEventId, setSelectedEventId] = useState("");
80
74
  const [lastAction, setLastAction] = useState("");
@@ -83,8 +77,6 @@ function PanelDemo({
83
77
  ? {
84
78
  ...(calendar.prose ?? thursdayProse),
85
79
  picked,
86
- copy,
87
- copyText: `${(calendar.prose ?? thursdayProse).dayLabel}: ${picked.join(", ")}`,
88
80
  }
89
81
  : undefined;
90
82
 
@@ -123,14 +115,16 @@ function PanelDemo({
123
115
  onOfferOtherTimes: () => setOffering(true),
124
116
  onRemoveInvite: () => setRemoved(true),
125
117
  onToggleSlot: (slot) => {
126
- setCopy("idle");
127
118
  setPicked((prev) =>
128
119
  prev.includes(slot.startTime)
129
120
  ? prev.filter((start) => start !== slot.startTime)
130
121
  : [...prev, slot.startTime],
131
122
  );
132
123
  },
133
- onCopySlots: () => setCopy(copyFails ? "failed" : "copied"),
124
+ onReplyWithSlots: () =>
125
+ setLastAction(
126
+ `opened a reply holding ${(calendar.prose ?? thursdayProse).dayLabel}: ${picked.join(", ")}`,
127
+ ),
134
128
  onAddSuggestion: (id, timeZone) => {
135
129
  setDropped((prev) => [...prev, id]);
136
130
  setLastAction(
@@ -235,7 +229,6 @@ function rail(
235
229
  sender: IntelligenceData,
236
230
  calendar: IntelligenceCalendarData,
237
231
  initialTab: IntelligenceTabId = "calendar",
238
- copyFails = false,
239
232
  ): Story {
240
233
  return {
241
234
  render: () => (
@@ -244,7 +237,6 @@ function rail(
244
237
  sender={sender}
245
238
  calendar={calendar}
246
239
  initialTab={initialTab}
247
- copyFails={copyFails}
248
240
  className="h-full"
249
241
  />
250
242
  </RailHost>
@@ -367,18 +359,6 @@ export const ReadFailure = rail(PLAIN, organiserSender, {
367
359
  reportHref: REPORT,
368
360
  });
369
361
 
370
- /**
371
- * Picked times on a page that cannot reach the clipboard, as on plain http.
372
- * The failure is stated and the times are there to select by hand.
373
- */
374
- export const CopyFailed = rail(
375
- THURSDAY,
376
- organiserSender,
377
- proseTimeThread,
378
- "calendar",
379
- true,
380
- );
381
-
382
362
  /**
383
363
  * The same invitation an hour later. Nothing is booked over it, and the panel
384
364
  * says that too — silence there would read as "not checked", which is the same
@@ -10,7 +10,7 @@ import {
10
10
  import { Banner } from "./banner.js";
11
11
  import { isWritingElsewhere } from "./editor-focus.js";
12
12
  import { htmlToMarkdown } from "./rich-text-document.js";
13
- import type { ComposeCaret } from "./rich-text-value.js";
13
+ import type { ComposeCaret, ComposeInsertion } from "./rich-text-value.js";
14
14
 
15
15
  export interface PlainTextEditorProps {
16
16
  value: string;
@@ -21,6 +21,8 @@ export interface PlainTextEditorProps {
21
21
  * does not take focus.
22
22
  */
23
23
  initialCaret?: ComposeCaret;
24
+ insertion?: ComposeInsertion;
25
+ onInserted?: (version: number) => void;
24
26
  placeholder?: string;
25
27
  ariaLabel?: string;
26
28
  /** Pinned to the right of the toolbar strip. The mode toggle rides here. */
@@ -70,11 +72,27 @@ export const PlainTextEditor = ({
70
72
  ariaLabel = "Message body",
71
73
  trailing,
72
74
  lang,
75
+ insertion,
76
+ onInserted,
73
77
  }: PlainTextEditorProps) => {
74
78
  const textareaRef = useRef<HTMLTextAreaElement>(null);
75
79
  const pendingCaret = useRef<number | null>(null);
76
80
  const plainRequested = useRef(false);
77
81
  const [emptyPaste, setEmptyPaste] = useState(false);
82
+ const appliedInsertion = useRef<number | undefined>(undefined);
83
+
84
+ useEffect(() => {
85
+ if (!insertion || insertion.version === appliedInsertion.current) return;
86
+ appliedInsertion.current = insertion.version;
87
+ const textarea = textareaRef.current;
88
+ if (!textarea) return;
89
+ textarea.focus();
90
+ const spliced = insertAtCaret(textarea, insertion.text);
91
+ onInserted?.(insertion.version);
92
+ if (!spliced) return;
93
+ pendingCaret.current = spliced.caret;
94
+ onChange(spliced.value);
95
+ }, [insertion, onChange, onInserted]);
78
96
 
79
97
  // Grows to its content rather than scrolling inside itself: one scroller in
80
98
  // the compose body keeps the caret in view for free, which a nested one is
@@ -47,7 +47,11 @@ import {
47
47
  SUGGESTION_LIMIT,
48
48
  } from "./rich-text-spellcheck-words.js";
49
49
  import { RichTextToolbar } from "./rich-text-toolbar.js";
50
- import type { ComposeCaret, RichTextValue } from "./rich-text-value.js";
50
+ import type {
51
+ ComposeCaret,
52
+ ComposeInsertion,
53
+ RichTextValue,
54
+ } from "./rich-text-value.js";
51
55
 
52
56
  export interface RichTextEditorProps {
53
57
  /**
@@ -81,6 +85,8 @@ export interface RichTextEditorProps {
81
85
  * `lang`, which is also when the browser keeps checking.
82
86
  */
83
87
  spellcheck?: SpellcheckOptions;
88
+ insertion?: ComposeInsertion;
89
+ onInserted?: (version: number) => void;
84
90
  }
85
91
 
86
92
  /**
@@ -906,6 +912,32 @@ const AutoFocus = ({ caret }: { caret?: ComposeCaret }) => {
906
912
  return null;
907
913
  };
908
914
 
915
+ const InsertionPlugin = ({
916
+ insertion,
917
+ onInserted,
918
+ }: {
919
+ insertion: ComposeInsertion | undefined;
920
+ onInserted: ((version: number) => void) | undefined;
921
+ }) => {
922
+ const [editor] = useLexicalComposerContext();
923
+ const applied = useRef<number | undefined>(undefined);
924
+
925
+ useEffect(() => {
926
+ if (!insertion || insertion.version === applied.current) return;
927
+ applied.current = insertion.version;
928
+ editor.update(() => {
929
+ const selection = $getSelection();
930
+ const target = $isRangeSelection(selection)
931
+ ? selection
932
+ : $getRoot().selectEnd();
933
+ target.insertRawText(insertion.text);
934
+ });
935
+ onInserted?.(insertion.version);
936
+ }, [editor, insertion, onInserted]);
937
+
938
+ return null;
939
+ };
940
+
909
941
  const seedDocument =
910
942
  (html: string) =>
911
943
  (editor: LexicalEditor): void => {
@@ -925,6 +957,8 @@ export const RichTextEditor = ({
925
957
  trailing,
926
958
  lang,
927
959
  spellcheck,
960
+ insertion,
961
+ onInserted,
928
962
  }: RichTextEditorProps) => {
929
963
  const [checkedHere, setCheckedHere] = useState(false);
930
964
  const bodyRef = useRef<HTMLDivElement>(null);
@@ -997,6 +1031,7 @@ export const RichTextEditor = ({
997
1031
  <TablePlugin />
998
1032
  <PastePlugin />
999
1033
  <AutoFocus caret={initialCaret} />
1034
+ <InsertionPlugin insertion={insertion} onInserted={onInserted} />
1000
1035
  {onChange && <ChangePlugin onChange={onChange} />}
1001
1036
  </LexicalComposer>
1002
1037
  );
@@ -23,6 +23,11 @@ export interface RichTextValue {
23
23
  */
24
24
  export type ComposeCaret = "start" | "end";
25
25
 
26
+ export interface ComposeInsertion {
27
+ version: number;
28
+ text: string;
29
+ }
30
+
26
31
  export const EMPTY_RICH_TEXT: RichTextValue = {
27
32
  html: "",
28
33
  text: "",
package/src/rich-text.ts CHANGED
@@ -59,6 +59,7 @@ export {
59
59
  normaliseWord,
60
60
  SUGGESTION_LIMIT,
61
61
  } from "./components/rich-text-spellcheck-words.js";
62
+ export type { ComposeInsertion } from "./components/rich-text-value.js";
62
63
  export {
63
64
  type ComposeCaret,
64
65
  EMPTY_RICH_TEXT,