@theoremjs/react 0.3.5-next.1024.g444d79c → 0.3.5-next.1025.g5523d8f
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -3
- package/package.json +3 -3
- package/ui/composer-refs.js +222 -22
package/README.md
CHANGED
|
@@ -252,14 +252,22 @@ import { refToken, TheoremChat } from '@theoremjs/react/ui';
|
|
|
252
252
|
| `renderRef` | Draws a ref, as a chip in the composer and in the person's message. Without it, the chip is a badge and the message shows as text. |
|
|
253
253
|
| `refText` | Writes a ref into the message's text. Default: the id. Give the server the same one. |
|
|
254
254
|
|
|
255
|
-
To put a ref in the field from your own button, hold an `inputRef` and call `inputRef.current.insertToken(refToken({ kind, id }))`. Only a chip that `refToken` made is a ref. Typed text is never read as one. A click on a chip can leave the caret inside it. `insertToken` and `insertText` on your `inputRef` then
|
|
255
|
+
To put a ref in the field from your own button, hold an `inputRef` and call `inputRef.current.insertToken(refToken({ kind, id }))`. Only a chip that `refToken` made is a ref. Typed text is never read as one. A click on a chip can leave the caret inside it. Typing, pasting, and `insertToken` and `insertText` on your `inputRef` then land just after that chip, never inside it.
|
|
256
256
|
|
|
257
|
-
The draft keeps its refs, so a composer drawn again from a saved draft shows its chips again. This holds for a stashed or queued message that you restore.
|
|
257
|
+
The draft keeps its refs, so a composer drawn again from a saved draft shows its chips again. This holds for a stashed or queued message that you restore.
|
|
258
|
+
|
|
259
|
+
Chips also stay in these two cases:
|
|
260
|
+
|
|
261
|
+
- The up arrow brings back a sent message with its chips. The down arrow brings back your draft with its chips.
|
|
262
|
+
- Text that you copy or cut in a composer, and paste in a composer on the same page, keeps its chips. A paste from anywhere else is plain text.
|
|
263
|
+
|
|
264
|
+
Your own input must keep the markup of Astryx's tokens (`data-astryx-token`) and its handle.
|
|
258
265
|
|
|
259
266
|
Limits:
|
|
260
267
|
|
|
261
268
|
- A call sends a typed line as text. Its refs reach the model as that text.
|
|
262
|
-
- The up
|
|
269
|
+
- The up arrow brings back the messages that the field itself keeps: those you send with Enter. It matches them by their words: if two have the same words, both come back with the later one's refs.
|
|
270
|
+
- A paste in another tab or another app is plain text.
|
|
263
271
|
|
|
264
272
|
### Tool media
|
|
265
273
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theoremjs/react",
|
|
3
|
-
"version": "0.3.5-next.
|
|
3
|
+
"version": "0.3.5-next.1025.g5523d8f",
|
|
4
4
|
"description": "React bindings for Theorem: headless hooks + transport, an Astryx chat UI, and a Web-standard server handler.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -60,8 +60,8 @@
|
|
|
60
60
|
"@stylexjs/stylex": "0.19.0",
|
|
61
61
|
"react": "^19.0.0",
|
|
62
62
|
"react-dom": "^19.0.0",
|
|
63
|
-
"@theoremjs/agents": "^0.3.5-next.
|
|
64
|
-
"@theoremjs/web": "^0.3.5-next.
|
|
63
|
+
"@theoremjs/agents": "^0.3.5-next.1025.g5523d8f",
|
|
64
|
+
"@theoremjs/web": "^0.3.5-next.1025.g5523d8f"
|
|
65
65
|
},
|
|
66
66
|
"peerDependenciesMeta": {
|
|
67
67
|
"@astryxdesign/core": {
|
package/ui/composer-refs.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { Badge } from '@astryxdesign/core/Badge';
|
|
3
|
-
import { hasRef, refIdText, textOfParts, } from '@theoremjs/agents/interface';
|
|
3
|
+
import { hasRef, messageParts, refIdText, textOfParts, } from '@theoremjs/agents/interface';
|
|
4
4
|
import { createContext, useCallback, useContext, useEffect, useLayoutEffect, useRef, } from 'react';
|
|
5
5
|
// invariant: This file is the one that reads the input's own markup, which a host's input keeps.
|
|
6
6
|
const TOKEN = '[data-astryx-token]';
|
|
@@ -39,7 +39,7 @@ function addWords(parts, text) {
|
|
|
39
39
|
else if (text)
|
|
40
40
|
parts.push({ type: 'text', text });
|
|
41
41
|
}
|
|
42
|
-
function readElement(element, parts) {
|
|
42
|
+
function readElement(element, parts, range) {
|
|
43
43
|
if (element.matches(TOKEN)) {
|
|
44
44
|
const ref = tokenRefs.get(element);
|
|
45
45
|
if (ref)
|
|
@@ -50,26 +50,34 @@ function readElement(element, parts) {
|
|
|
50
50
|
else if (element.tagName === 'BR')
|
|
51
51
|
addWords(parts, '\n');
|
|
52
52
|
else
|
|
53
|
-
readInto(element, parts);
|
|
53
|
+
readInto(element, parts, range);
|
|
54
54
|
}
|
|
55
|
-
|
|
55
|
+
/** The text of `node`, cut to what `range` selects of it. */
|
|
56
|
+
function textIn(node, range) {
|
|
57
|
+
const text = node.textContent ?? '';
|
|
58
|
+
const from = range?.startContainer === node ? range.startOffset : 0;
|
|
59
|
+
const to = range?.endContainer === node ? range.endOffset : text.length;
|
|
60
|
+
return text.slice(from, to);
|
|
61
|
+
}
|
|
62
|
+
/** Reads `node` into `parts`; with a `range`, only what it selects. */
|
|
63
|
+
function readInto(node, parts, range) {
|
|
56
64
|
for (const child of node.childNodes) {
|
|
65
|
+
if (range && !range.intersectsNode(child))
|
|
66
|
+
continue;
|
|
57
67
|
if (child.nodeType === 3)
|
|
58
|
-
addWords(parts, child
|
|
68
|
+
addWords(parts, textIn(child, range));
|
|
59
69
|
else if (child.nodeType === 1)
|
|
60
|
-
readElement(child, parts);
|
|
70
|
+
readElement(child, parts, range);
|
|
61
71
|
}
|
|
62
72
|
}
|
|
63
73
|
/** The editor's words and refs, as the input's own value reads them; undefined with no ref. */
|
|
64
|
-
function readEditorParts(editor) {
|
|
74
|
+
function readEditorParts(editor, range) {
|
|
65
75
|
const parts = [];
|
|
66
|
-
readInto(editor, parts);
|
|
76
|
+
readInto(editor, parts, range);
|
|
67
77
|
return hasRef(parts) ? parts : undefined;
|
|
68
78
|
}
|
|
69
|
-
/**
|
|
70
|
-
function
|
|
71
|
-
const focused = editor.ownerDocument.activeElement === editor;
|
|
72
|
-
editor.textContent = '';
|
|
79
|
+
/** Puts `parts` in at the caret through the input's handle, each ref a chip again. */
|
|
80
|
+
function insertParts(editor, handle, parts, options) {
|
|
73
81
|
for (const part of parts) {
|
|
74
82
|
if (part.type === 'text') {
|
|
75
83
|
handle.insertText(part.text);
|
|
@@ -80,9 +88,15 @@ function writeEditorParts(editor, handle, parts, options) {
|
|
|
80
88
|
if (!token)
|
|
81
89
|
continue;
|
|
82
90
|
tokenRefs.set(token, part);
|
|
83
|
-
// why: The input adds a space after each token; the
|
|
91
|
+
// why: The input adds a space after each token; the parts' own words already hold theirs.
|
|
84
92
|
token.nextSibling?.remove();
|
|
85
93
|
}
|
|
94
|
+
}
|
|
95
|
+
/** Fills an empty editor with `parts`, leaving focus where it was. */
|
|
96
|
+
function writeEditorParts(editor, handle, parts, options) {
|
|
97
|
+
const focused = editor.ownerDocument.activeElement === editor;
|
|
98
|
+
editor.textContent = '';
|
|
99
|
+
insertParts(editor, handle, parts, options);
|
|
86
100
|
if (!focused)
|
|
87
101
|
editor.blur();
|
|
88
102
|
}
|
|
@@ -134,6 +148,172 @@ function outsideChips(input, editor) {
|
|
|
134
148
|
getValue: () => input.getValue(),
|
|
135
149
|
};
|
|
136
150
|
}
|
|
151
|
+
/** The editor's selection, when it has one. */
|
|
152
|
+
function selectionIn(editor) {
|
|
153
|
+
const selection = editor.ownerDocument.defaultView?.getSelection();
|
|
154
|
+
if (!selection?.rangeCount)
|
|
155
|
+
return undefined;
|
|
156
|
+
const range = selection.getRangeAt(0);
|
|
157
|
+
return editor.contains(range.commonAncestorContainer) ? range : undefined;
|
|
158
|
+
}
|
|
159
|
+
function selectAll(editor) {
|
|
160
|
+
const selection = editor.ownerDocument.defaultView?.getSelection();
|
|
161
|
+
const range = editor.ownerDocument.createRange();
|
|
162
|
+
range.selectNodeContents(editor);
|
|
163
|
+
selection?.removeAllRanges();
|
|
164
|
+
selection?.addRange(range);
|
|
165
|
+
}
|
|
166
|
+
// invariant: The clipboard carries only a key; the refs stay in memory, so a paste can make no ref
|
|
167
|
+
// that was not copied from a composer on this page.
|
|
168
|
+
const CLIP = 'application/x-theorem-parts';
|
|
169
|
+
let clipped;
|
|
170
|
+
/** What the editor has selected, as a range holding any chip it touches whole. */
|
|
171
|
+
function wholeChips(editor) {
|
|
172
|
+
const selected = selectionIn(editor);
|
|
173
|
+
if (!selected || selected.collapsed)
|
|
174
|
+
return undefined;
|
|
175
|
+
const range = selected.cloneRange();
|
|
176
|
+
const start = chipAround(editor, range.startContainer);
|
|
177
|
+
const end = chipAround(editor, range.endContainer);
|
|
178
|
+
if (start)
|
|
179
|
+
range.setStartBefore(start);
|
|
180
|
+
if (end)
|
|
181
|
+
range.setEndAfter(end);
|
|
182
|
+
return range;
|
|
183
|
+
}
|
|
184
|
+
/** Copies a selection that holds a chip; any other is left to the browser. */
|
|
185
|
+
function copyChips(editor, event, refText) {
|
|
186
|
+
const range = wholeChips(editor);
|
|
187
|
+
const parts = range && readEditorParts(editor, range);
|
|
188
|
+
if (!range || !parts || !event.clipboardData)
|
|
189
|
+
return;
|
|
190
|
+
// why: Not `randomUUID`, which a page served over plain HTTP lacks.
|
|
191
|
+
clipped = { key: `${Date.now()}-${Math.random()}`, parts };
|
|
192
|
+
event.clipboardData.setData('text/plain', textOfParts(parts, refText));
|
|
193
|
+
event.clipboardData.setData(CLIP, clipped.key);
|
|
194
|
+
event.preventDefault();
|
|
195
|
+
event.stopPropagation();
|
|
196
|
+
if (event.type !== 'cut' || editor.getAttribute('contenteditable') === 'false')
|
|
197
|
+
return;
|
|
198
|
+
range.deleteContents();
|
|
199
|
+
editor.dispatchEvent(new Event('input', { bubbles: true }));
|
|
200
|
+
}
|
|
201
|
+
/** The parts a paste brings from a composer; undefined for anything from elsewhere. */
|
|
202
|
+
function pastedParts(event) {
|
|
203
|
+
const key = event.clipboardData?.getData(CLIP);
|
|
204
|
+
return key && key === clipped?.key ? clipped.parts : undefined;
|
|
205
|
+
}
|
|
206
|
+
/** Pastes a composer's copy as chips; an outside paste goes on to the input, past any chip. */
|
|
207
|
+
function pasteChips(editor, event, field) {
|
|
208
|
+
leaveChips(editor);
|
|
209
|
+
const parts = pastedParts(event);
|
|
210
|
+
const handle = field.handle();
|
|
211
|
+
if (!parts || !handle)
|
|
212
|
+
return;
|
|
213
|
+
event.preventDefault();
|
|
214
|
+
event.stopPropagation();
|
|
215
|
+
if (editor.getAttribute('contenteditable') === 'false')
|
|
216
|
+
return;
|
|
217
|
+
insertParts(editor, handle, parts, field.options());
|
|
218
|
+
field.read();
|
|
219
|
+
}
|
|
220
|
+
/** Typing with the caret in a chip goes after it, never inside. */
|
|
221
|
+
function typePastChips(editor, event, field) {
|
|
222
|
+
if (event.isComposing || !event.inputType.startsWith('insert'))
|
|
223
|
+
return;
|
|
224
|
+
const at = selectionIn(editor)?.startContainer;
|
|
225
|
+
if (!at || !chipAround(editor, at))
|
|
226
|
+
return;
|
|
227
|
+
if (event.inputType !== 'insertText' || !event.data) {
|
|
228
|
+
leaveChips(editor);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
event.preventDefault();
|
|
232
|
+
event.stopPropagation();
|
|
233
|
+
field.handle()?.insertText(event.data);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Moves the caret out of a chip before a key writes: a chip takes no input, so the browser would
|
|
237
|
+
* drop the character.
|
|
238
|
+
*/
|
|
239
|
+
function keyPastChips(editor, event) {
|
|
240
|
+
const writes = event.key.length === 1 || event.key === 'Process' || event.key === 'Unidentified';
|
|
241
|
+
if (writes && !event.ctrlKey && !event.metaKey)
|
|
242
|
+
leaveChips(editor);
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Hears the field's own events before the input does. On the document, so a field drawn again
|
|
246
|
+
* needs no new listeners.
|
|
247
|
+
*/
|
|
248
|
+
function listenToField(field) {
|
|
249
|
+
const heard = [
|
|
250
|
+
['copy', (editor, e) => copyChips(editor, e, field.options().refText)],
|
|
251
|
+
['cut', (editor, e) => copyChips(editor, e, field.options().refText)],
|
|
252
|
+
['paste', (editor, e) => pasteChips(editor, e, field)],
|
|
253
|
+
['beforeinput', (editor, e) => typePastChips(editor, e, field)],
|
|
254
|
+
[
|
|
255
|
+
'keydown',
|
|
256
|
+
(editor, e) => {
|
|
257
|
+
const event = e;
|
|
258
|
+
keyPastChips(editor, event);
|
|
259
|
+
field.key(event.key, event.shiftKey);
|
|
260
|
+
},
|
|
261
|
+
],
|
|
262
|
+
['keyup', () => field.released()],
|
|
263
|
+
];
|
|
264
|
+
const page = field.editor()?.ownerDocument ?? document;
|
|
265
|
+
const added = heard.map(([type, act]) => {
|
|
266
|
+
const listener = (event) => {
|
|
267
|
+
const editor = field.editor();
|
|
268
|
+
if (editor && event.composedPath().includes(editor))
|
|
269
|
+
act(editor, event);
|
|
270
|
+
};
|
|
271
|
+
page.addEventListener(type, listener, true);
|
|
272
|
+
return () => page.removeEventListener(type, listener, true);
|
|
273
|
+
});
|
|
274
|
+
return () => {
|
|
275
|
+
for (const remove of added)
|
|
276
|
+
remove();
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* The refs of messages sent from the field, by their words: the input's ↑ history keeps words
|
|
281
|
+
* alone. Two sent with the same words give back the later one's refs.
|
|
282
|
+
*/
|
|
283
|
+
function useRecall(editor, words) {
|
|
284
|
+
const kept = useRef(new Map());
|
|
285
|
+
const recalling = useRef(false);
|
|
286
|
+
const reselect = useRef(false);
|
|
287
|
+
return useRef({
|
|
288
|
+
key(key, shift) {
|
|
289
|
+
recalling.current = key === 'ArrowUp' || key === 'ArrowDown';
|
|
290
|
+
const sends = key === 'Enter' && !shift;
|
|
291
|
+
const field = editor();
|
|
292
|
+
if (!field || !(sends || recalling.current))
|
|
293
|
+
return;
|
|
294
|
+
const parts = messageParts(readEditorParts(field));
|
|
295
|
+
if (parts)
|
|
296
|
+
kept.current.set(words().trim(), parts);
|
|
297
|
+
else if (sends)
|
|
298
|
+
kept.current.delete(words().trim());
|
|
299
|
+
},
|
|
300
|
+
released() {
|
|
301
|
+
recalling.current = false;
|
|
302
|
+
},
|
|
303
|
+
/** The refs of `text`, when an arrow key has just brought it back. */
|
|
304
|
+
recalled(text) {
|
|
305
|
+
const parts = recalling.current ? kept.current.get(text.trim()) : undefined;
|
|
306
|
+
reselect.current = Boolean(parts);
|
|
307
|
+
return parts;
|
|
308
|
+
},
|
|
309
|
+
/** Selects a recalled message again, so the next ↑ steps on as the input's own recall does. */
|
|
310
|
+
written(field) {
|
|
311
|
+
if (reselect.current)
|
|
312
|
+
selectAll(field);
|
|
313
|
+
reselect.current = false;
|
|
314
|
+
},
|
|
315
|
+
}).current;
|
|
316
|
+
}
|
|
137
317
|
/**
|
|
138
318
|
* Keeps a draft's refs with its input: reads them as the person edits, and makes their chips again
|
|
139
319
|
* when the input is drawn from a saved draft, which it would otherwise show as plain text.
|
|
@@ -144,27 +324,46 @@ export function useEditorRefs(args) {
|
|
|
144
324
|
const handle = useRef(null);
|
|
145
325
|
const writing = useRef(false);
|
|
146
326
|
const drawnAs = useRef(args.drawn);
|
|
327
|
+
const editor = useCallback(() => editorIn(latest.current.rootRef.current), []);
|
|
147
328
|
const { inputRef } = args;
|
|
148
329
|
const handleRef = useCallback((input) => {
|
|
149
330
|
// why: An insert with the caret in a chip, as after a click on it, would nest a chip in it.
|
|
150
|
-
const node = input && outsideChips(input,
|
|
331
|
+
const node = input && outsideChips(input, editor);
|
|
151
332
|
handle.current = node;
|
|
152
333
|
if (typeof inputRef === 'function')
|
|
153
334
|
inputRef(node);
|
|
154
335
|
else if (inputRef)
|
|
155
336
|
inputRef.current = node;
|
|
156
|
-
}, [inputRef]);
|
|
337
|
+
}, [inputRef, editor]);
|
|
157
338
|
const report = useCallback(() => {
|
|
158
|
-
const
|
|
159
|
-
if (
|
|
160
|
-
latest.current.onParts?.(readEditorParts(
|
|
161
|
-
}, []);
|
|
339
|
+
const field = editor();
|
|
340
|
+
if (field && !writing.current)
|
|
341
|
+
latest.current.onParts?.(readEditorParts(field));
|
|
342
|
+
}, [editor]);
|
|
343
|
+
const recall = useRecall(editor, () => handle.current?.getValue() ?? '');
|
|
162
344
|
const onChange = useCallback((text) => {
|
|
163
345
|
if (writing.current)
|
|
164
346
|
return;
|
|
165
347
|
latest.current.onChange(text);
|
|
166
|
-
|
|
167
|
-
|
|
348
|
+
const parts = recall.recalled(text);
|
|
349
|
+
if (parts)
|
|
350
|
+
latest.current.onParts?.(parts);
|
|
351
|
+
else
|
|
352
|
+
report();
|
|
353
|
+
}, [report, recall]);
|
|
354
|
+
useEffect(() => listenToField({
|
|
355
|
+
editor,
|
|
356
|
+
handle: () => handle.current,
|
|
357
|
+
options: () => latest.current,
|
|
358
|
+
read: () => {
|
|
359
|
+
const text = handle.current?.getValue();
|
|
360
|
+
if (text !== undefined && text !== latest.current.value)
|
|
361
|
+
latest.current.onChange(text);
|
|
362
|
+
report();
|
|
363
|
+
},
|
|
364
|
+
key: recall.key,
|
|
365
|
+
released: recall.released,
|
|
366
|
+
}), [editor, report, recall]);
|
|
168
367
|
const { value, parts, drawn } = args;
|
|
169
368
|
useEffect(() => {
|
|
170
369
|
const editor = editorIn(latest.current.rootRef.current);
|
|
@@ -189,7 +388,8 @@ export function useEditorRefs(args) {
|
|
|
189
388
|
if (written !== value)
|
|
190
389
|
latest.current.onChange(written);
|
|
191
390
|
report();
|
|
192
|
-
|
|
391
|
+
recall.written(editor);
|
|
392
|
+
}, [value, parts, drawn, report, recall]);
|
|
193
393
|
return {
|
|
194
394
|
handleRef,
|
|
195
395
|
handle,
|