@remit/ui 0.0.132 → 0.0.134
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 +1 -1
- package/src/components/confirm-dialog.stories.tsx +28 -0
- package/src/components/editor-focus.ts +78 -0
- package/src/components/plain-text-editor.mount.test.ts +27 -0
- package/src/components/plain-text-editor.tsx +2 -0
- package/src/components/rich-text-editor.mount.test.ts +94 -0
- package/src/components/rich-text-editor.tsx +2 -0
package/package.json
CHANGED
|
@@ -52,3 +52,31 @@ export const NonDestructive: Story = {
|
|
|
52
52
|
destructive: false,
|
|
53
53
|
},
|
|
54
54
|
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Deleting mail that already sits in Trash expunges it on the server, so the
|
|
58
|
+
* dialog asks that question instead of "move to Trash?" — the wording follows
|
|
59
|
+
* the consequence, never the button that opened it (#845).
|
|
60
|
+
*/
|
|
61
|
+
export const PermanentDelete: Story = {
|
|
62
|
+
args: {
|
|
63
|
+
title: "Permanently delete 12 messages?",
|
|
64
|
+
description: "They are erased from the mail server and cannot be restored.",
|
|
65
|
+
confirmLabel: "Delete permanently",
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The account's Trash appointment has not resolved yet, so which of the two
|
|
71
|
+
* dialogs above applies is not yet known. Rather than guess the reversible
|
|
72
|
+
* wording over what may be an expunge, the copy stays neutral and the confirm
|
|
73
|
+
* holds until the answer arrives.
|
|
74
|
+
*/
|
|
75
|
+
export const OutcomeUnknown: Story = {
|
|
76
|
+
args: {
|
|
77
|
+
title: "Delete 12 messages?",
|
|
78
|
+
description: "Checking where this account files deleted mail…",
|
|
79
|
+
confirmLabel: "Delete",
|
|
80
|
+
isBusy: true,
|
|
81
|
+
},
|
|
82
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether the reader is already typing somewhere else.
|
|
3
|
+
*
|
|
4
|
+
* Both writing surfaces open with the caret in the message, and both arrive on
|
|
5
|
+
* their own lazily-loaded chunk — so "on mount" is whenever that chunk lands.
|
|
6
|
+
* On a cold cache that is well after the composer opened and the reader moved
|
|
7
|
+
* on to the recipients, the subject or the search field, and claiming the caret
|
|
8
|
+
* then takes it out of a sentence in progress: the rest of what they type goes
|
|
9
|
+
* into the message body.
|
|
10
|
+
*
|
|
11
|
+
* Pressing a button to open the composer leaves focus on that button, which is
|
|
12
|
+
* not typing — so the ordinary open still lands the caret in the message.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The types an `<input>` accepts prose in. Everything else it can be — a
|
|
17
|
+
* checkbox, a radio, a button, a file or colour picker — is a control the
|
|
18
|
+
* reader clicks, not one they are mid-word in.
|
|
19
|
+
*/
|
|
20
|
+
const TEXT_INPUT_TYPES = new Set([
|
|
21
|
+
"text",
|
|
22
|
+
"search",
|
|
23
|
+
"email",
|
|
24
|
+
"url",
|
|
25
|
+
"tel",
|
|
26
|
+
"password",
|
|
27
|
+
"number",
|
|
28
|
+
"date",
|
|
29
|
+
"datetime-local",
|
|
30
|
+
"month",
|
|
31
|
+
"time",
|
|
32
|
+
"week",
|
|
33
|
+
]);
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Focus as the reader experiences it. `activeElement` stops at a shadow host,
|
|
37
|
+
* so a field inside a web component reports as the host; this walks in to the
|
|
38
|
+
* element actually holding the caret.
|
|
39
|
+
*/
|
|
40
|
+
const deepActiveElement = (scope: Document | ShadowRoot): Element | null => {
|
|
41
|
+
const active = scope.activeElement;
|
|
42
|
+
if (!active?.shadowRoot) return active;
|
|
43
|
+
return deepActiveElement(active.shadowRoot) ?? active;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Whether text goes into this element. Read off the tag and the `type`
|
|
48
|
+
* attribute rather than `instanceof`, which is per-realm and answers wrongly
|
|
49
|
+
* for anything reached through a frame.
|
|
50
|
+
*/
|
|
51
|
+
const takesTyping = (element: Element): boolean => {
|
|
52
|
+
const tag = element.tagName.toUpperCase();
|
|
53
|
+
// A focused frame is a document of its own — whatever is being typed in
|
|
54
|
+
// there is not ours to interrupt, and we cannot see it to ask.
|
|
55
|
+
if (tag === "IFRAME" || tag === "FRAME") return true;
|
|
56
|
+
if (tag === "TEXTAREA") return true;
|
|
57
|
+
// Typeahead: a reader part-way through selecting an option is choosing with
|
|
58
|
+
// the keyboard, and losing it drops them somewhere they did not pick.
|
|
59
|
+
if (tag === "SELECT") return true;
|
|
60
|
+
if (tag === "INPUT") {
|
|
61
|
+
const type = (element.getAttribute("type") ?? "text").toLowerCase();
|
|
62
|
+
return TEXT_INPUT_TYPES.has(type);
|
|
63
|
+
}
|
|
64
|
+
// The attribute as well as the property: `isContentEditable` is computed by
|
|
65
|
+
// the engine, and a document that never lays out does not answer for it.
|
|
66
|
+
const editable = element.getAttribute("contenteditable");
|
|
67
|
+
if (editable !== null && editable.toLowerCase() !== "false") return true;
|
|
68
|
+
return (element as Partial<HTMLElement>).isContentEditable === true;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export const isWritingElsewhere = (root: HTMLElement | null): boolean => {
|
|
72
|
+
const owner = root?.ownerDocument ?? globalThis.document;
|
|
73
|
+
if (!owner) return false;
|
|
74
|
+
const active = deepActiveElement(owner);
|
|
75
|
+
if (!active || active === root) return false;
|
|
76
|
+
if (root?.contains(active)) return false;
|
|
77
|
+
return takesTyping(active);
|
|
78
|
+
};
|
|
@@ -147,6 +147,33 @@ describe("PlainTextEditor", () => {
|
|
|
147
147
|
assert.equal(textarea.selectionStart, textarea.value.length);
|
|
148
148
|
});
|
|
149
149
|
|
|
150
|
+
/**
|
|
151
|
+
* The surface arrives in the same lazily-loaded chunk as the rich editor, so
|
|
152
|
+
* its mount is whenever that chunk lands rather than when the composer
|
|
153
|
+
* opened. Resuming a plain-text draft is the reachable path: the draft read
|
|
154
|
+
* remounts the body once it resolves, by which time the reader may well be
|
|
155
|
+
* typing in the search field.
|
|
156
|
+
*/
|
|
157
|
+
it("leaves the caret in a field the reader is typing in", async () => {
|
|
158
|
+
const elsewhere = document.createElement("input");
|
|
159
|
+
elsewhere.setAttribute("aria-label", "Search mail");
|
|
160
|
+
document.body.append(elsewhere);
|
|
161
|
+
elsewhere.focus();
|
|
162
|
+
|
|
163
|
+
await mount("", { initialCaret: "start" });
|
|
164
|
+
await act(async () => {
|
|
165
|
+
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
const holder = document.activeElement?.getAttribute("aria-label");
|
|
169
|
+
elsewhere.remove();
|
|
170
|
+
assert.equal(
|
|
171
|
+
holder,
|
|
172
|
+
"Search mail",
|
|
173
|
+
`the caret ended up on ${holder ?? "nothing"}`,
|
|
174
|
+
);
|
|
175
|
+
});
|
|
176
|
+
|
|
150
177
|
it("sends on Cmd+Enter", async () => {
|
|
151
178
|
let sent = 0;
|
|
152
179
|
await mount("Ready to go.", {
|
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
useState,
|
|
9
9
|
} from "react";
|
|
10
10
|
import { Banner } from "./banner.js";
|
|
11
|
+
import { isWritingElsewhere } from "./editor-focus.js";
|
|
11
12
|
import { htmlToMarkdown } from "./rich-text-document.js";
|
|
12
13
|
import type { ComposeCaret } from "./rich-text-value.js";
|
|
13
14
|
|
|
@@ -95,6 +96,7 @@ export const PlainTextEditor = ({
|
|
|
95
96
|
const textarea = textareaRef.current;
|
|
96
97
|
if (!textarea) return;
|
|
97
98
|
const timer = setTimeout(() => {
|
|
99
|
+
if (isWritingElsewhere(textarea)) return;
|
|
98
100
|
textarea.focus();
|
|
99
101
|
const at = initialCaret === "start" ? 0 : textarea.value.length;
|
|
100
102
|
textarea.setSelectionRange(at, at);
|
|
@@ -118,3 +118,97 @@ describe("RichTextEditor", () => {
|
|
|
118
118
|
assert.equal(field.value, "https://");
|
|
119
119
|
});
|
|
120
120
|
});
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* The writing surface is loaded on its own chunk, so it mounts whenever that
|
|
124
|
+
* chunk arrives rather than when the composer opened. A reader who pressed
|
|
125
|
+
* Compose and started typing in the search field, the recipients or the subject
|
|
126
|
+
* is mid-sentence by then, and the caret is theirs.
|
|
127
|
+
*/
|
|
128
|
+
describe("RichTextEditor opening on a caret", () => {
|
|
129
|
+
const mount = async (): Promise<void> => {
|
|
130
|
+
await act(async () => {
|
|
131
|
+
root = createRoot(container);
|
|
132
|
+
root.render(createElement(RichTextEditor, { initialCaret: "start" }));
|
|
133
|
+
});
|
|
134
|
+
// The caret is claimed off a timer, so let it run.
|
|
135
|
+
await act(async () => {
|
|
136
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
137
|
+
});
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
it("takes the caret when nothing else holds it", async () => {
|
|
141
|
+
await mount();
|
|
142
|
+
|
|
143
|
+
const editable = container.querySelector<HTMLElement>(
|
|
144
|
+
"[data-testid=compose-body]",
|
|
145
|
+
);
|
|
146
|
+
assert.ok(editable, "the editable surface is mounted");
|
|
147
|
+
assert.equal(document.activeElement === editable, true);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* One case per kind of thing that can hold focus when the chunk lands. The
|
|
152
|
+
* open is only allowed to lose to something the reader is typing in.
|
|
153
|
+
*/
|
|
154
|
+
const holders: readonly [string, () => HTMLElement, boolean][] = [
|
|
155
|
+
[
|
|
156
|
+
"a search field",
|
|
157
|
+
() => {
|
|
158
|
+
const field = document.createElement("input");
|
|
159
|
+
field.setAttribute("aria-label", "Search mail");
|
|
160
|
+
return field;
|
|
161
|
+
},
|
|
162
|
+
true,
|
|
163
|
+
],
|
|
164
|
+
[
|
|
165
|
+
"a subject field with no type attribute",
|
|
166
|
+
() => document.createElement("input"),
|
|
167
|
+
true,
|
|
168
|
+
],
|
|
169
|
+
["a plain textarea", () => document.createElement("textarea"), true],
|
|
170
|
+
[
|
|
171
|
+
"a select being typed through",
|
|
172
|
+
() => document.createElement("select"),
|
|
173
|
+
true,
|
|
174
|
+
],
|
|
175
|
+
[
|
|
176
|
+
"another contenteditable",
|
|
177
|
+
() => {
|
|
178
|
+
const surface = document.createElement("div");
|
|
179
|
+
surface.setAttribute("contenteditable", "true");
|
|
180
|
+
return surface;
|
|
181
|
+
},
|
|
182
|
+
true,
|
|
183
|
+
],
|
|
184
|
+
[
|
|
185
|
+
"the button that opened the composer",
|
|
186
|
+
() => document.createElement("button"),
|
|
187
|
+
false,
|
|
188
|
+
],
|
|
189
|
+
[
|
|
190
|
+
"a checkbox",
|
|
191
|
+
() => {
|
|
192
|
+
const box = document.createElement("input");
|
|
193
|
+
box.setAttribute("type", "checkbox");
|
|
194
|
+
return box;
|
|
195
|
+
},
|
|
196
|
+
false,
|
|
197
|
+
],
|
|
198
|
+
];
|
|
199
|
+
|
|
200
|
+
for (const [what, build, keepsIt] of holders) {
|
|
201
|
+
it(`${keepsIt ? "leaves the caret on" : "takes the caret from"} ${what}`, async () => {
|
|
202
|
+
const elsewhere = build();
|
|
203
|
+
elsewhere.setAttribute("data-holder", "");
|
|
204
|
+
document.body.append(elsewhere);
|
|
205
|
+
elsewhere.focus();
|
|
206
|
+
|
|
207
|
+
await mount();
|
|
208
|
+
|
|
209
|
+
const held = document.activeElement?.hasAttribute("data-holder") === true;
|
|
210
|
+
elsewhere.remove();
|
|
211
|
+
assert.equal(held, keepsIt);
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
});
|
|
@@ -30,6 +30,7 @@ import {
|
|
|
30
30
|
useRef,
|
|
31
31
|
useState,
|
|
32
32
|
} from "react";
|
|
33
|
+
import { isWritingElsewhere } from "./editor-focus.js";
|
|
33
34
|
import { RichTextCorrectionMenu } from "./rich-text-correction-menu.js";
|
|
34
35
|
import { $adoptHtml, $readRichText } from "./rich-text-document.js";
|
|
35
36
|
import { RICH_TEXT_NODES, richTextTheme } from "./rich-text-nodes.js";
|
|
@@ -886,6 +887,7 @@ const AutoFocus = ({ caret }: { caret?: ComposeCaret }) => {
|
|
|
886
887
|
// the caret is placed here rather than left to `focus`, which would open a
|
|
887
888
|
// new message below the signature instead of above it.
|
|
888
889
|
const timer = setTimeout(() => {
|
|
890
|
+
if (isWritingElsewhere(editor.getRootElement())) return;
|
|
889
891
|
editor.update(
|
|
890
892
|
() => {
|
|
891
893
|
const root = $getRoot();
|