@zeyos/zx 5.4.0 → 5.6.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/CHANGELOG.md +136 -0
- package/README.md +23 -16
- package/dist/types/components/agent-thread/agent-thread.d.ts +583 -0
- package/dist/types/components/app-sidebar/app-sidebar.d.ts +33 -0
- package/dist/types/components/approval-card/approval-card.d.ts +596 -0
- package/dist/types/components/avatar/avatar.d.ts +91 -4
- package/dist/types/components/badge/badge.d.ts +5 -4
- package/dist/types/components/block-view/block-view.d.ts +205 -0
- package/dist/types/components/composer/composer.d.ts +736 -0
- package/dist/types/components/datebox/date-range-box.d.ts +15 -2
- package/dist/types/components/datebox/datebox.d.ts +37 -3
- package/dist/types/components/kbd/kbd.d.ts +41 -0
- package/dist/types/components/launcher/launcher.d.ts +4 -2
- package/dist/types/components/prose/prose.d.ts +69 -0
- package/dist/types/components/questionnaire/questionnaire.d.ts +0 -36
- package/dist/types/components/run-status/run-status.d.ts +393 -0
- package/dist/types/components/run-trace/run-trace.d.ts +624 -0
- package/dist/types/components/source-list/source-list.d.ts +177 -0
- package/dist/types/components/stat-tile/stat-tile.d.ts +20 -0
- package/dist/types/components/tag-picker/tag-picker.d.ts +11 -5
- package/dist/types/components/tree/tree.d.ts +44 -3
- package/dist/types/core/icons.d.ts +22 -1
- package/dist/types/core/messages.d.ts +135 -0
- package/dist/types/index.d.ts +12 -2
- package/dist/types/internal/agent-model.d.ts +587 -0
- package/dist/types/internal/app-rail.d.ts +10 -3
- package/dist/types/internal/highlight.d.ts +24 -0
- package/dist/types/internal/presence-loop.d.ts +148 -0
- package/dist/types/internal/row-actions.d.ts +151 -0
- package/dist/zx-zeyos.esm.js +4852 -4535
- package/dist/zx-zeyos.esm.min.js +4 -4
- package/dist/zx.css +3152 -434
- package/dist/zx.esm.js +13730 -6595
- package/dist/zx.esm.min.js +27 -8
- package/dist/zx.global.js +13730 -6595
- package/dist/zx.global.min.js +27 -8
- package/dist/zx.min.css +1 -1
- package/docs/api.json +1474 -205
- package/docs/llms.md +698 -39
- package/docs/llms.txt +12 -1
- package/package.json +1 -1
- package/src/components/account-menu/account-menu.js +2 -2
- package/src/components/agent-thread/agent-thread.css +417 -0
- package/src/components/agent-thread/agent-thread.js +1365 -0
- package/src/components/app-rail/app-rail.css +11 -11
- package/src/components/app-sidebar/app-sidebar.css +90 -17
- package/src/components/app-sidebar/app-sidebar.js +120 -12
- package/src/components/approval-card/approval-card.css +289 -0
- package/src/components/approval-card/approval-card.js +1377 -0
- package/src/components/avatar/avatar.css +56 -1
- package/src/components/avatar/avatar.js +148 -26
- package/src/components/badge/badge.css +33 -20
- package/src/components/badge/badge.js +5 -4
- package/src/components/block-view/block-view.css +300 -0
- package/src/components/block-view/block-view.js +523 -0
- package/src/components/breadcrumb/breadcrumb.css +28 -3
- package/src/components/breadcrumb/breadcrumb.js +2 -2
- package/src/components/button/button.css +24 -13
- package/src/components/button/button.js +20 -2
- package/src/components/calendar/calendar.css +14 -4
- package/src/components/card-view/card-view.js +2 -2
- package/src/components/chart/chart.js +20 -5
- package/src/components/check-button/check-button.css +11 -3
- package/src/components/check-button/check-button.js +3 -3
- package/src/components/composer/composer.css +403 -0
- package/src/components/composer/composer.js +1441 -0
- package/src/components/context-menu/context-menu.css +7 -1
- package/src/components/date-picker/date-picker.css +6 -0
- package/src/components/date-picker/date-picker.js +5 -5
- package/src/components/date-picker/date-range-picker.css +6 -0
- package/src/components/date-picker/date-range-picker.js +3 -3
- package/src/components/date-picker/month-picker.js +3 -3
- package/src/components/date-picker/time-picker.css +6 -0
- package/src/components/datebox/date-range-box.js +30 -6
- package/src/components/datebox/datebox.js +44 -7
- package/src/components/dialog/dialog.css +0 -1
- package/src/components/dialog/dialog.js +2 -1
- package/src/components/dock/dock.js +2 -2
- package/src/components/dropdown/dropdown.css +2 -1
- package/src/components/entity-ref/entity-ref.css +8 -1
- package/src/components/file-item/file-item.css +1 -1
- package/src/components/finder/finder.js +2 -2
- package/src/components/groupbox/groupbox.js +2 -2
- package/src/components/kanban-view/kanban-view.css +8 -4
- package/src/components/kanban-view/kanban-view.js +3 -3
- package/src/components/kbd/kbd.css +64 -0
- package/src/components/kbd/kbd.js +121 -0
- package/src/components/launcher/launcher.css +30 -25
- package/src/components/launcher/launcher.js +15 -21
- package/src/components/list-toolbar/list-toolbar.css +7 -1
- package/src/components/menu-button/menu-button.css +9 -2
- package/src/components/message/message.js +2 -2
- package/src/components/module-bar/module-bar.css +11 -1
- package/src/components/multi-value-editor/multi-value-editor.js +4 -4
- package/src/components/navigation-bar/navigation-bar.css +3 -2
- package/src/components/pagination/pagination.js +2 -2
- package/src/components/panel/panel.js +2 -2
- package/src/components/prose/prose.css +53 -0
- package/src/components/prose/prose.js +152 -0
- package/src/components/questionnaire/questionnaire.css +7 -17
- package/src/components/questionnaire/questionnaire.js +16 -1
- package/src/components/rating/rating.css +3 -1
- package/src/components/run-status/run-status.css +208 -0
- package/src/components/run-status/run-status.js +782 -0
- package/src/components/run-trace/run-trace.css +443 -0
- package/src/components/run-trace/run-trace.js +1209 -0
- package/src/components/search/search.js +2 -2
- package/src/components/select/glyphs.js +3 -3
- package/src/components/select/select.css +35 -7
- package/src/components/select/select.js +4 -4
- package/src/components/source-list/source-list.css +208 -0
- package/src/components/source-list/source-list.js +355 -0
- package/src/components/stat-tile/stat-tile.css +56 -2
- package/src/components/stat-tile/stat-tile.js +25 -2
- package/src/components/stepper/stepper.css +2 -1
- package/src/components/stepper/stepper.js +3 -3
- package/src/components/tabbox/tabbox.css +2 -2
- package/src/components/tabbox/tabbox.js +2 -2
- package/src/components/table/table.css +52 -16
- package/src/components/table/table.js +7 -5
- package/src/components/tag-picker/tag-picker.css +56 -43
- package/src/components/tag-picker/tag-picker.js +21 -16
- package/src/components/tree/tree.css +44 -0
- package/src/components/tree/tree.js +97 -9
- package/src/components/value-list/value-list.css +25 -4
- package/src/components/value-list/value-list.js +2 -1
- package/src/components/view/record-view.js +3 -3
- package/src/components/view-settings/view-settings.css +2 -1
- package/src/components/workspace-switcher/workspace-switcher.js +3 -3
- package/src/core/icons.js +34 -3
- package/src/core/messages.js +143 -0
- package/src/elements/define.js +1 -0
- package/src/index.js +12 -2
- package/src/internal/agent-model.js +488 -0
- package/src/internal/app-rail.js +5 -1
- package/src/internal/highlight.js +104 -0
- package/src/internal/presence-loop.js +343 -0
- package/src/internal/row-actions.js +203 -0
- package/styles/base.css +26 -1
- package/styles/prose.css +306 -0
- package/styles/tokens/dark.css +97 -120
- package/styles/tokens/global.css +24 -9
- package/styles/tokens/semantic.css +464 -184
- package/styles/tokens/themes.css +9 -0
- package/styles/zx.css +10 -0
|
@@ -0,0 +1,1441 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { Component } from '../../core/component.js';
|
|
3
|
+
import { h, restoreTarget, snapshotTarget } from '../../core/dom.js';
|
|
4
|
+
import { formatFileSize } from '../../core/format.js';
|
|
5
|
+
import { ICON_SIZE, icon } from '../../core/icons.js';
|
|
6
|
+
import { position } from '../../core/position.js';
|
|
7
|
+
import { storage } from '../../core/storage.js';
|
|
8
|
+
import { uid } from '../../core/util.js';
|
|
9
|
+
import { keyLabels } from '../kbd/kbd.js';
|
|
10
|
+
import { Launcher } from '../launcher/launcher.js';
|
|
11
|
+
|
|
12
|
+
/** @typedef {import('../launcher/launcher.js').LauncherItem} LauncherItem */
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Something the `@` trigger can mention: an agent, a person, a record.
|
|
16
|
+
* @typedef {Object} MentionItem
|
|
17
|
+
* @property {string|number} id Stable identifier.
|
|
18
|
+
* @property {string} label Visible name; picking the item writes `@<label> ` into the text.
|
|
19
|
+
* @property {string} [description] Secondary line in the suggestion list.
|
|
20
|
+
* @property {string} [icon] Icon name drawn before the label.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Where mentions come from.
|
|
25
|
+
* @typedef {Object} MentionSource
|
|
26
|
+
* @property {string} [trigger='@'] Character that starts a mention.
|
|
27
|
+
* @property {MentionItem[]|((query: string) => MentionItem[]|Promise<MentionItem[]>)} items The
|
|
28
|
+
* items, filtered by the composer as the query changes, or a function that answers a query itself.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Something the message is about, shown as a removable chip above the text and sent with it.
|
|
33
|
+
* @typedef {Object} ContextChip
|
|
34
|
+
* @property {string} id Stable identifier.
|
|
35
|
+
* @property {string} label Visible label.
|
|
36
|
+
* @property {string} [icon] Icon name drawn before the label.
|
|
37
|
+
* @property {unknown} [ref] Application data handed back in `send`; the composer never reads it.
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A further button in the composer's bar, after Attach and Commands: a search toggle, a model's
|
|
42
|
+
* parameters. The composer draws it and reports it; what it does is the application's.
|
|
43
|
+
* @typedef {Object} ComposerTool
|
|
44
|
+
* @property {string} id Stable identifier, handed back in `tool`.
|
|
45
|
+
* @property {string} label Accessible name and tooltip; the button shows only the icon.
|
|
46
|
+
* @property {string} icon Icon name: a built-in glyph or one the application registered.
|
|
47
|
+
* @property {boolean} [pressed] Makes the tool a toggle, drawn with `aria-pressed`. Leave it out for
|
|
48
|
+
* a plain button. The composer never changes it on its own: call `setToolPressed()`.
|
|
49
|
+
* @property {boolean} [disabled=false] Whether the tool takes no clicks.
|
|
50
|
+
* @property {boolean} [expanded] Makes the tool a disclosure, drawn with `aria-expanded`: a panel
|
|
51
|
+
* of settings it opens. Like `pressed`, the application sets it.
|
|
52
|
+
* @property {string} [controls] Id of what the tool opens, drawn as `aria-controls`.
|
|
53
|
+
*/
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @typedef {Object} ComposerToolDetail
|
|
57
|
+
* @property {string} id The tool's id.
|
|
58
|
+
* @property {ComposerTool} tool A copy of the tool as it stands, `pressed` included.
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* What `update()` changes in place. A key left out, or `undefined`, stays as it is.
|
|
63
|
+
* @typedef {Object} ComposerUpdate
|
|
64
|
+
* @property {string|null} [label] Accessible name; null resolves `composer.label` again.
|
|
65
|
+
* @property {string} [placeholder] Placeholder of the text; empty removes it.
|
|
66
|
+
* @property {string} [hint] Quiet text beside Send, as `setHint()`.
|
|
67
|
+
* @property {string} [note] Line under the composer, as `setNote()`.
|
|
68
|
+
* @property {'enter'|'mod+enter'} [submitKey] The key that sends.
|
|
69
|
+
* @property {string|null} [sendLabel] Visible text on Send; null or empty makes it icon-only.
|
|
70
|
+
* @property {boolean} [disabled] As `setDisabled()`.
|
|
71
|
+
* @property {boolean} [busy] As `setBusy()`.
|
|
72
|
+
*/
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @typedef {Object} ComposerSendDetail
|
|
76
|
+
* @property {string} text The message, trimmed.
|
|
77
|
+
* @property {File[]} files Attached files, in the order they were added.
|
|
78
|
+
* @property {MentionItem[]} mentions The picked mentions whose `@<label>` is still in the text.
|
|
79
|
+
* @property {ContextChip[]} context The context chips.
|
|
80
|
+
*/
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @typedef {Object} ComposerRemoveDetail
|
|
84
|
+
* @property {'file'|'context'} kind What was removed.
|
|
85
|
+
* @property {string} id The attachment's or chip's id.
|
|
86
|
+
* @property {File} [file] The removed file, for `kind: 'file'`.
|
|
87
|
+
* @property {ContextChip} [chip] The removed chip, for `kind: 'context'`.
|
|
88
|
+
*/
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @typedef {Object} ComposerOptions
|
|
92
|
+
* @property {string|null} [label=null] Accessible name of the text and of the group around it.
|
|
93
|
+
* Omitted resolves `composer.label`, English `Message`.
|
|
94
|
+
* @property {string} [placeholder=''] Placeholder of the text.
|
|
95
|
+
* @property {string} [value=''] Initial text. Without it, a saved draft is restored.
|
|
96
|
+
* @property {'enter'|'mod+enter'} [submitKey='enter'] `'enter'`: Enter sends and Shift+Enter breaks
|
|
97
|
+
* the line. `'mod+enter'`: Cmd+Enter (Ctrl+Enter elsewhere) sends and Enter breaks the line.
|
|
98
|
+
* @property {boolean} [busy=false] Whether an agent is working: Send becomes Stop.
|
|
99
|
+
* @property {boolean} [disabled=false] Whether the composer takes no input.
|
|
100
|
+
* @property {boolean} [attachments=false] Whether files can be attached: an Attach button, paste,
|
|
101
|
+
* and drop.
|
|
102
|
+
* @property {string} [accept=''] File types accepted, in the `<input type="file">` syntax
|
|
103
|
+
* (`'image/*,.pdf'`). Empty accepts every file.
|
|
104
|
+
* @property {number} [maxFiles=10] Most files one message carries.
|
|
105
|
+
* @property {MentionSource|null} [mentions=null] Enables `@` mentions.
|
|
106
|
+
* @property {LauncherItem[]|null} [commands=null] Commands opened by `/` on an empty text, in a
|
|
107
|
+
* Launcher; an item with `items` opens a page of further commands.
|
|
108
|
+
* @property {ContextChip[]} [context=[]] Initial context chips.
|
|
109
|
+
* @property {string} [hint=''] Quiet text in the bar beside Send.
|
|
110
|
+
* @property {string} [note=''] Quiet line under the composer.
|
|
111
|
+
* @property {string|null} [draftKey=null] Key the unsent text is kept under (through `storage()`),
|
|
112
|
+
* restored on create and cleared on send.
|
|
113
|
+
* @property {number} [minRows=1] Rows the text never shrinks below, even when empty. Capped at
|
|
114
|
+
* `maxRows`.
|
|
115
|
+
* @property {number} [maxRows=10] Rows the text grows to before it scrolls.
|
|
116
|
+
* @property {{attach?: string, commands?: string}} [icons={}] Glyph names for the Attach and Commands
|
|
117
|
+
* buttons (a paperclip, a book) where the application registered its own.
|
|
118
|
+
* @property {ComposerTool[]} [tools=[]] Further icon buttons in the bar, after Attach and Commands.
|
|
119
|
+
* An entry without `id`, `label` or `icon` is left out, as is a repeated id.
|
|
120
|
+
* @property {string|null} [sendLabel=null] Visible text beside the Send glyph (`'Start task'`), which
|
|
121
|
+
* is then also its accessible name; while busy the button reads `composer.stop`. Null keeps Send
|
|
122
|
+
* icon-only. In a composer narrower than 24rem the text is visually hidden and stays the name.
|
|
123
|
+
* @property {Record<string, string>|Record<string, Record<string, string>>} [msg] Localized messages.
|
|
124
|
+
* @property {(event: CustomEvent<ComposerSendDetail>) => void} [onsend] Preventable send listener.
|
|
125
|
+
* @property {(event: CustomEvent<Record<string, never>>) => void} [onstop] Stop listener.
|
|
126
|
+
* @property {(event: CustomEvent<{text: string}>) => void} [oninput] Text listener.
|
|
127
|
+
* @property {(event: CustomEvent<{files: File[]}>) => void} [onattach] Preventable attach listener.
|
|
128
|
+
* @property {(event: CustomEvent<ComposerRemoveDetail>) => void} [onremove] Chip removal listener.
|
|
129
|
+
* @property {(event: CustomEvent<{item: MentionItem}>) => void} [onmention] Mention listener.
|
|
130
|
+
* @property {(event: CustomEvent<{item: LauncherItem}>) => void} [oncommand] Command listener.
|
|
131
|
+
* @property {(event: CustomEvent<ComposerToolDetail>) => void} [ontool] Tool listener.
|
|
132
|
+
* @property {(event: CustomEvent<Record<string, never>>) => void} [onhistoryprevious] Listener for
|
|
133
|
+
* ArrowUp on an empty text.
|
|
134
|
+
*/
|
|
135
|
+
|
|
136
|
+
const MESSAGES = Object.freeze({
|
|
137
|
+
'composer.label': 'Message',
|
|
138
|
+
'composer.send': 'Send',
|
|
139
|
+
'composer.stop': 'Stop',
|
|
140
|
+
'composer.attach': 'Attach files',
|
|
141
|
+
'composer.remove': 'Remove %1',
|
|
142
|
+
'composer.attachments': 'Attachments',
|
|
143
|
+
'composer.context': 'Context',
|
|
144
|
+
'composer.suggestions': 'Suggestions',
|
|
145
|
+
'composer.match': '1 suggestion',
|
|
146
|
+
'composer.matches': '%1 suggestions',
|
|
147
|
+
'composer.commands': 'Commands',
|
|
148
|
+
'composer.commandsPlaceholder': 'Search commands',
|
|
149
|
+
'composer.drop': 'Drop files to attach'
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
/** Namespace the drafts are stored under. */
|
|
153
|
+
const DRAFT_NAMESPACE = 'composer';
|
|
154
|
+
|
|
155
|
+
/** Where a command copy given to the launcher keeps the application's own item. */
|
|
156
|
+
const COMMAND_SOURCE = Symbol('composer.command');
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Where a mention starts and what has been typed of it: the text after the trigger up to the caret,
|
|
160
|
+
* when the trigger opens a word and the query holds no whitespace.
|
|
161
|
+
* @param {string} text The whole text.
|
|
162
|
+
* @param {number} caret Caret offset.
|
|
163
|
+
* @param {string} [trigger='@'] Character that starts a mention.
|
|
164
|
+
* @returns {{start: number, query: string}|null}
|
|
165
|
+
*/
|
|
166
|
+
export function mentionQuery(text, caret, trigger = '@') {
|
|
167
|
+
const mark = String(trigger || '@');
|
|
168
|
+
const before = String(text ?? '').slice(0, Math.max(0, Number(caret) || 0));
|
|
169
|
+
const start = before.lastIndexOf(mark);
|
|
170
|
+
if (start < 0) return null;
|
|
171
|
+
if (start > 0 && !/\s/.test(before[start - 1])) return null;
|
|
172
|
+
const query = before.slice(start + mark.length);
|
|
173
|
+
if (/\s/.test(query)) return null;
|
|
174
|
+
return { start, query };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* The items that match a mention query, best first: a label that starts with it, then a word in
|
|
179
|
+
* the label that does, then a label or description that contains it. Case and accents are ignored;
|
|
180
|
+
* items that rank the same keep their order. An empty query keeps every item.
|
|
181
|
+
* @param {MentionItem[]} items
|
|
182
|
+
* @param {string} query
|
|
183
|
+
* @returns {MentionItem[]}
|
|
184
|
+
*/
|
|
185
|
+
export function filterMentions(items, query) {
|
|
186
|
+
const list = Array.isArray(items) ? items.filter(isMentionItem) : [];
|
|
187
|
+
const needle = fold(query);
|
|
188
|
+
if (!needle) return list;
|
|
189
|
+
return list
|
|
190
|
+
.map((item, order) => ({ item, order, rank: mentionRank(item, needle) }))
|
|
191
|
+
.filter((entry) => entry.rank < 4)
|
|
192
|
+
.sort((a, b) => a.rank - b.rank || a.order - b.order)
|
|
193
|
+
.map((entry) => entry.item);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* The picked mentions whose `@<label>` is still in the text, each once.
|
|
198
|
+
* @param {string} text
|
|
199
|
+
* @param {MentionItem[]} mentions Mentions in the order they were picked.
|
|
200
|
+
* @param {string} [trigger='@']
|
|
201
|
+
* @returns {MentionItem[]}
|
|
202
|
+
*/
|
|
203
|
+
export function mentionsInText(text, mentions, trigger = '@') {
|
|
204
|
+
const source = String(text ?? '');
|
|
205
|
+
const seen = new Set();
|
|
206
|
+
return (Array.isArray(mentions) ? mentions : []).filter((item) => {
|
|
207
|
+
if (!isMentionItem(item)) return false;
|
|
208
|
+
const key = String(item.id);
|
|
209
|
+
if (seen.has(key) || !source.includes(`${trigger}${item.label}`)) return false;
|
|
210
|
+
seen.add(key);
|
|
211
|
+
return true;
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Whether a key press sends. A press during an IME composition never does.
|
|
217
|
+
* @param {{key: string, shiftKey?: boolean, metaKey?: boolean, ctrlKey?: boolean, altKey?: boolean,
|
|
218
|
+
* isComposing?: boolean, keyCode?: number}} event Key event, or its fields.
|
|
219
|
+
* @param {'enter'|'mod+enter'} [submitKey='enter']
|
|
220
|
+
* @param {{apple?: boolean}} [platform] `apple`: whether Mod is Cmd (otherwise Ctrl). Read from the
|
|
221
|
+
* browser when omitted.
|
|
222
|
+
* @returns {boolean}
|
|
223
|
+
*/
|
|
224
|
+
export function isSubmitKey(event, submitKey = 'enter', { apple = isApplePlatform() } = {}) {
|
|
225
|
+
if (!event || event.key !== 'Enter') return false;
|
|
226
|
+
// 229 is the key code a browser reports for a key the input method is still processing.
|
|
227
|
+
if (event.isComposing || event.keyCode === 229) return false;
|
|
228
|
+
if (event.shiftKey || event.altKey) return false;
|
|
229
|
+
if (submitKey === 'mod+enter') {
|
|
230
|
+
return apple
|
|
231
|
+
? Boolean(event.metaKey) && !event.ctrlKey
|
|
232
|
+
: Boolean(event.ctrlKey) && !event.metaKey;
|
|
233
|
+
}
|
|
234
|
+
return true;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Whether a file matches an `accept` list in the `<input type="file">` syntax: extensions
|
|
239
|
+
* (`.pdf`), whole MIME types (`application/json`), and MIME groups (`image/*`).
|
|
240
|
+
* @param {{name?: string, type?: string}} file
|
|
241
|
+
* @param {string} [accept='']
|
|
242
|
+
* @returns {boolean}
|
|
243
|
+
*/
|
|
244
|
+
export function acceptsFile(file, accept = '') {
|
|
245
|
+
const tokens = String(accept ?? '').split(',').map((token) => token.trim().toLowerCase()).filter(Boolean);
|
|
246
|
+
if (!tokens.length) return true;
|
|
247
|
+
const name = String(file?.name ?? '').toLowerCase();
|
|
248
|
+
const type = String(file?.type ?? '').toLowerCase();
|
|
249
|
+
return tokens.some((token) => {
|
|
250
|
+
if (token.startsWith('.')) return name.endsWith(token);
|
|
251
|
+
if (token.endsWith('/*')) return type.startsWith(token.slice(0, -1));
|
|
252
|
+
return type === token;
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* The rows the text spans at least and at most: whole numbers of one or more, the least never above
|
|
258
|
+
* the most.
|
|
259
|
+
* @param {number} [minRows=1]
|
|
260
|
+
* @param {number} [maxRows=10]
|
|
261
|
+
* @returns {{min: number, max: number}}
|
|
262
|
+
*/
|
|
263
|
+
export function rowRange(minRows = 1, maxRows = 10) {
|
|
264
|
+
const max = Math.max(1, Math.floor(Number(maxRows) || 1));
|
|
265
|
+
const min = Math.min(max, Math.max(1, Math.floor(Number(minRows) || 1)));
|
|
266
|
+
return { min, max };
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* The height an auto-growing text area takes for its content: at least `minRows` lines, at most
|
|
271
|
+
* `maxRows`, scrolling beyond that.
|
|
272
|
+
* @param {number} scrollHeight The content height, padding included (`textarea.scrollHeight`).
|
|
273
|
+
* @param {{lineHeight: number, padding?: number, border?: number, minRows?: number, maxRows?: number}} metrics
|
|
274
|
+
* Line height and the block padding and border, in pixels.
|
|
275
|
+
* @returns {{height: number, scrolls: boolean}} The border-box height, and whether the content
|
|
276
|
+
* overflows it.
|
|
277
|
+
*/
|
|
278
|
+
export function autosizeHeight(scrollHeight, { lineHeight, padding = 0, border = 0, minRows = 1, maxRows = 10 }) {
|
|
279
|
+
const line = Number(lineHeight) > 0 ? Number(lineHeight) : 20;
|
|
280
|
+
const { min, max } = rowRange(minRows, maxRows);
|
|
281
|
+
const most = max * line + padding;
|
|
282
|
+
const content = Math.min(most, Math.max(min * line + padding, Number(scrollHeight) || 0));
|
|
283
|
+
return { height: Math.ceil(content + border), scrolls: Number(scrollHeight) > most + 1 };
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* The bar's extra tools as the composer draws them: entries without an `id`, a non-empty `label`
|
|
288
|
+
* or an `icon` are left out, a repeated id keeps its first entry, `id`, `label` and `icon` become
|
|
289
|
+
* strings, `disabled` a boolean, and `pressed` stays only when it is a boolean. Any other field the
|
|
290
|
+
* application put on a tool is kept, and handed back in `tool`.
|
|
291
|
+
* @param {unknown} tools
|
|
292
|
+
* @returns {ComposerTool[]}
|
|
293
|
+
*/
|
|
294
|
+
export function normalizeTools(tools) {
|
|
295
|
+
/** @type {ComposerTool[]} */
|
|
296
|
+
const list = [];
|
|
297
|
+
const seen = new Set();
|
|
298
|
+
for (const entry of Array.isArray(tools) ? tools : []) {
|
|
299
|
+
if (!entry || typeof entry !== 'object') continue;
|
|
300
|
+
const tool = /** @type {Partial<ComposerTool> & Record<string, unknown>} */ (entry);
|
|
301
|
+
const id = tool.id == null ? '' : String(tool.id);
|
|
302
|
+
const label = tool.label == null ? '' : String(tool.label).trim();
|
|
303
|
+
const glyph = tool.icon == null ? '' : String(tool.icon);
|
|
304
|
+
if (!id || !label || !glyph || seen.has(id)) continue;
|
|
305
|
+
seen.add(id);
|
|
306
|
+
const { pressed, ...rest } = tool;
|
|
307
|
+
/** @type {ComposerTool} */
|
|
308
|
+
const normalized = { ...rest, id, label, icon: glyph, disabled: Boolean(tool.disabled) };
|
|
309
|
+
if (typeof pressed === 'boolean') normalized.pressed = pressed;
|
|
310
|
+
list.push(normalized);
|
|
311
|
+
}
|
|
312
|
+
return list;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* A message box for talking to an agent: a text that grows with what is written, Send (Stop while
|
|
317
|
+
* the agent works), attachments, `@` mentions, `/` commands, and context chips. It keeps nothing
|
|
318
|
+
* but the unsent message: sending hands the text, files, mentions and context to the application,
|
|
319
|
+
* which owns the conversation.
|
|
320
|
+
*
|
|
321
|
+
* @fires Composer#send
|
|
322
|
+
* @fires Composer#stop
|
|
323
|
+
* @fires Composer#input
|
|
324
|
+
* @fires Composer#attach
|
|
325
|
+
* @fires Composer#remove
|
|
326
|
+
* @fires Composer#mention
|
|
327
|
+
* @fires Composer#command
|
|
328
|
+
* @fires Composer#tool
|
|
329
|
+
* @fires Composer#historyprevious
|
|
330
|
+
* @extends {Component<ComposerOptions>}
|
|
331
|
+
*/
|
|
332
|
+
export class Composer extends Component {
|
|
333
|
+
static cssName = 'composer';
|
|
334
|
+
|
|
335
|
+
/** @type {Readonly<ComposerOptions>} */
|
|
336
|
+
static defaults = {
|
|
337
|
+
label: null,
|
|
338
|
+
placeholder: '',
|
|
339
|
+
value: '',
|
|
340
|
+
submitKey: 'enter',
|
|
341
|
+
busy: false,
|
|
342
|
+
disabled: false,
|
|
343
|
+
attachments: false,
|
|
344
|
+
accept: '',
|
|
345
|
+
maxFiles: 10,
|
|
346
|
+
mentions: null,
|
|
347
|
+
commands: null,
|
|
348
|
+
context: [],
|
|
349
|
+
hint: '',
|
|
350
|
+
note: '',
|
|
351
|
+
draftKey: null,
|
|
352
|
+
minRows: 1,
|
|
353
|
+
maxRows: 10,
|
|
354
|
+
icons: {},
|
|
355
|
+
tools: [],
|
|
356
|
+
sendLabel: null,
|
|
357
|
+
msg: MESSAGES
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
/** @returns {HTMLElement} */
|
|
361
|
+
render() {
|
|
362
|
+
const created = !this.el;
|
|
363
|
+
const root = /** @type {HTMLElement} */ (this.el ?? h('div'));
|
|
364
|
+
this.el = root;
|
|
365
|
+
this._original = created ? null : snapshotTarget(root);
|
|
366
|
+
if (!created) root.replaceChildren();
|
|
367
|
+
|
|
368
|
+
const options = this.options;
|
|
369
|
+
/** @type {{id: string, file: File}[]} */
|
|
370
|
+
this._files = [];
|
|
371
|
+
/** @type {ContextChip[]} */
|
|
372
|
+
this._context = [];
|
|
373
|
+
/** @type {MentionItem[]} */
|
|
374
|
+
this._mentions = [];
|
|
375
|
+
/** @type {{start: number, end: number, items: MentionItem[], index: number}|null} */
|
|
376
|
+
this._mention = null;
|
|
377
|
+
this._mentionSequence = 0;
|
|
378
|
+
/** @type {import('../../core/position.js').PositionController|null} */
|
|
379
|
+
this._position = null;
|
|
380
|
+
/** @type {Launcher|null} */
|
|
381
|
+
this._launcher = null;
|
|
382
|
+
this._announced = '';
|
|
383
|
+
this._busy = Boolean(options.busy);
|
|
384
|
+
this._disabled = Boolean(options.disabled);
|
|
385
|
+
this._destroyed = false;
|
|
386
|
+
this._width = 0;
|
|
387
|
+
/** What the Send button last drew: mode, send key and label, so it redraws only on a change. */
|
|
388
|
+
this._sendDrawn = '';
|
|
389
|
+
this._mentionSource = normalizeMentionSource(options.mentions);
|
|
390
|
+
this._commands = Array.isArray(options.commands) && options.commands.length ? options.commands : null;
|
|
391
|
+
this._draft = options.draftKey ? storage(DRAFT_NAMESPACE) : null;
|
|
392
|
+
this._tools = normalizeTools(options.tools);
|
|
393
|
+
this._sendLabel = sendLabelText(options.sendLabel);
|
|
394
|
+
this._rows = rowRange(options.minRows, Number(options.maxRows) || 10);
|
|
395
|
+
|
|
396
|
+
const label = this._labelText(options.label);
|
|
397
|
+
const listId = uid('zx-composer-suggestions');
|
|
398
|
+
const noteId = uid('zx-composer-note');
|
|
399
|
+
/** @type {'enter'|'mod+enter'} */
|
|
400
|
+
this._submitKey = submitKeyOf(options.submitKey);
|
|
401
|
+
|
|
402
|
+
const input = /** @type {HTMLTextAreaElement} */ (h('textarea', {
|
|
403
|
+
ref: 'input',
|
|
404
|
+
class: 'zx-composer__input',
|
|
405
|
+
rows: this._rows.min,
|
|
406
|
+
ariaLabel: label,
|
|
407
|
+
ariaDescribedby: options.note ? noteId : null,
|
|
408
|
+
placeholder: options.placeholder || null,
|
|
409
|
+
spellcheck: true
|
|
410
|
+
}));
|
|
411
|
+
this._input = input;
|
|
412
|
+
const context = h('ul', {
|
|
413
|
+
ref: 'context', class: 'zx-composer__chips', dataset: { kind: 'context' },
|
|
414
|
+
ariaLabel: this.msg('composer.context'), hidden: true
|
|
415
|
+
});
|
|
416
|
+
const files = h('ul', {
|
|
417
|
+
ref: 'files', class: 'zx-composer__chips', dataset: { kind: 'file' },
|
|
418
|
+
ariaLabel: this.msg('composer.attachments'), hidden: true
|
|
419
|
+
});
|
|
420
|
+
const fileInput = options.attachments ? /** @type {HTMLInputElement} */ (h('input', {
|
|
421
|
+
ref: 'fileInput', class: 'zx-composer__file-input', type: 'file', multiple: true,
|
|
422
|
+
accept: options.accept || null, hidden: true, tabindex: '-1'
|
|
423
|
+
})) : null;
|
|
424
|
+
const glyphs = { attach: 'plus', commands: 'list', ...(options.icons ?? {}) };
|
|
425
|
+
const attach = options.attachments ? iconButton(glyphs.attach, this.msg('composer.attach'), 'attach') : null;
|
|
426
|
+
const commands = this._commands ? iconButton(glyphs.commands, this.msg('composer.commands'), 'commands') : null;
|
|
427
|
+
commands?.setAttribute('aria-keyshortcuts', '/');
|
|
428
|
+
commands?.setAttribute('aria-haspopup', 'dialog');
|
|
429
|
+
const tools = h('span', { ref: 'tools', class: 'zx-composer__tools', hidden: true });
|
|
430
|
+
const send = /** @type {HTMLButtonElement} */ (h('button', {
|
|
431
|
+
ref: 'send', type: 'button', class: 'zx-icon-btn zx-composer__send'
|
|
432
|
+
}));
|
|
433
|
+
const hint = h('span', { ref: 'hint', class: 'zx-composer__hint', hidden: !options.hint }, String(options.hint ?? ''));
|
|
434
|
+
const frame = h('div', { ref: 'frame', class: 'zx-composer__frame' },
|
|
435
|
+
context,
|
|
436
|
+
files,
|
|
437
|
+
input,
|
|
438
|
+
h('div', { class: 'zx-composer__bar' },
|
|
439
|
+
attach, fileInput, commands, tools, hint,
|
|
440
|
+
h('span', { class: 'zx-composer__spacer' }),
|
|
441
|
+
send),
|
|
442
|
+
options.attachments
|
|
443
|
+
? h('div', { class: 'zx-composer__drop', ariaHidden: 'true' }, icon('upload'), this.msg('composer.drop'))
|
|
444
|
+
: null);
|
|
445
|
+
const list = h('div', {
|
|
446
|
+
ref: 'list', class: 'zx-composer__suggestions', id: listId, role: 'listbox',
|
|
447
|
+
ariaLabel: this.msg('composer.suggestions'), hidden: true
|
|
448
|
+
});
|
|
449
|
+
const status = h('div', { ref: 'status', class: 'zx-composer__status', role: 'status', ariaLive: 'polite' });
|
|
450
|
+
const note = h('p', { ref: 'note', class: 'zx-composer__note', id: noteId, hidden: !options.note }, String(options.note ?? ''));
|
|
451
|
+
|
|
452
|
+
root.setAttribute('role', 'group');
|
|
453
|
+
root.setAttribute('aria-label', label);
|
|
454
|
+
root.append(...[frame, note, list, status].filter((node) => node !== null));
|
|
455
|
+
|
|
456
|
+
for (const chip of Array.isArray(options.context) ? options.context : []) this._putContext(chip);
|
|
457
|
+
const initial = typeof options.value === 'string' && options.value
|
|
458
|
+
? options.value
|
|
459
|
+
: (this._draft ? String(this._draft.get(String(options.draftKey), '') ?? '') : '');
|
|
460
|
+
input.value = initial;
|
|
461
|
+
this._syncState();
|
|
462
|
+
this._syncSend();
|
|
463
|
+
|
|
464
|
+
this.listen(input, 'input', () => this._onInput());
|
|
465
|
+
this.listen(input, 'keydown', (event) => this._onKeydown(/** @type {KeyboardEvent} */ (event)));
|
|
466
|
+
this.listen(input, 'keyup', (event) => {
|
|
467
|
+
if (['ArrowLeft', 'ArrowRight', 'Home', 'End'].includes(/** @type {KeyboardEvent} */ (event).key)) this._updateMentions();
|
|
468
|
+
});
|
|
469
|
+
this.listen(input, 'click', () => this._updateMentions());
|
|
470
|
+
this.listen(input, 'blur', () => this._closeMentions());
|
|
471
|
+
this.listen(input, 'paste', (event) => this._onPaste(/** @type {ClipboardEvent} */ (event)));
|
|
472
|
+
this.listen(send, 'click', () => {
|
|
473
|
+
if (this._busy) this._stop();
|
|
474
|
+
else this._send();
|
|
475
|
+
});
|
|
476
|
+
if (attach && fileInput) {
|
|
477
|
+
this.listen(attach, 'click', () => fileInput.click());
|
|
478
|
+
this.listen(fileInput, 'change', () => {
|
|
479
|
+
const picked = Array.from(fileInput.files ?? []);
|
|
480
|
+
fileInput.value = '';
|
|
481
|
+
this._addFiles(picked, true);
|
|
482
|
+
input.focus();
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
if (commands) this.listen(commands, 'click', () => this._openCommands());
|
|
486
|
+
this.listen(tools, 'click', (event) => this._onToolClick(/** @type {MouseEvent} */ (event)));
|
|
487
|
+
this.listen(frame, 'click', (event) => this._onChipClick(/** @type {MouseEvent} */ (event)));
|
|
488
|
+
// The suggestions keep the focus in the text: a press picks without blurring it.
|
|
489
|
+
this.listen(list, 'mousedown', (event) => event.preventDefault());
|
|
490
|
+
this.listen(list, 'click', (event) => {
|
|
491
|
+
const option = /** @type {Element|null} */ (event.target)?.closest?.('[role="option"]');
|
|
492
|
+
if (option && this._mention) this._pickMention(Number(/** @type {HTMLElement} */ (option).dataset.index));
|
|
493
|
+
});
|
|
494
|
+
this.listen(list, 'pointermove', (event) => {
|
|
495
|
+
const option = /** @type {Element|null} */ (event.target)?.closest?.('[role="option"]');
|
|
496
|
+
const index = Number(/** @type {HTMLElement|null} */ (option)?.dataset.index);
|
|
497
|
+
if (option && this._mention && index !== this._mention.index) this._highlight(index);
|
|
498
|
+
});
|
|
499
|
+
if (options.attachments) {
|
|
500
|
+
this.listen(root, 'dragenter', (event) => this._onDrag(/** @type {DragEvent} */ (event)));
|
|
501
|
+
this.listen(root, 'dragover', (event) => this._onDrag(/** @type {DragEvent} */ (event)));
|
|
502
|
+
this.listen(root, 'dragleave', (event) => {
|
|
503
|
+
const related = /** @type {Node|null} */ (/** @type {DragEvent} */ (event).relatedTarget);
|
|
504
|
+
if (!related || !root.contains(related)) this._setDropping(false);
|
|
505
|
+
});
|
|
506
|
+
this.listen(root, 'drop', (event) => this._onDrop(/** @type {DragEvent} */ (event)));
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
// The text re-wraps when its width changes, so its height is measured again.
|
|
510
|
+
this._observer = typeof ResizeObserver === 'function'
|
|
511
|
+
? new ResizeObserver(() => {
|
|
512
|
+
const width = input.clientWidth;
|
|
513
|
+
if (width === this._width) return;
|
|
514
|
+
this._width = width;
|
|
515
|
+
this._autosize();
|
|
516
|
+
})
|
|
517
|
+
: null;
|
|
518
|
+
this._observer?.observe(input);
|
|
519
|
+
this._autosize();
|
|
520
|
+
return root;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/** Focuses the text. @returns {this} */
|
|
524
|
+
focus() {
|
|
525
|
+
this._input.focus();
|
|
526
|
+
return this;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/** @returns {string} The text as written. */
|
|
530
|
+
getValue() {
|
|
531
|
+
return this._input.value;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
* Replaces the text, keeps the draft in step, and puts the caret at the end. Emits nothing.
|
|
536
|
+
* @param {string} text
|
|
537
|
+
* @returns {this}
|
|
538
|
+
*/
|
|
539
|
+
setValue(text) {
|
|
540
|
+
const input = this._input;
|
|
541
|
+
input.value = String(text ?? '');
|
|
542
|
+
input.setSelectionRange(input.value.length, input.value.length);
|
|
543
|
+
this._closeMentions();
|
|
544
|
+
this._afterTextChange();
|
|
545
|
+
return this;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/**
|
|
549
|
+
* Types text at the caret, replacing any selection, as if the person had typed it: the draft
|
|
550
|
+
* follows, and text ending in the mention trigger (`'@'`) opens the suggestions. Emits `input`.
|
|
551
|
+
* Use it for a button that starts a mention or inserts a snippet.
|
|
552
|
+
* @param {string} text
|
|
553
|
+
* @returns {this}
|
|
554
|
+
*/
|
|
555
|
+
insertText(text) {
|
|
556
|
+
const input = this._input;
|
|
557
|
+
const value = String(text ?? '');
|
|
558
|
+
const start = input.selectionStart ?? input.value.length;
|
|
559
|
+
const end = input.selectionEnd ?? start;
|
|
560
|
+
input.setRangeText(value, start, end, 'end');
|
|
561
|
+
input.focus();
|
|
562
|
+
this._onInput();
|
|
563
|
+
return this;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* Empties the text, the attachments and the draft. Context chips stay. Emits nothing.
|
|
568
|
+
* @returns {this}
|
|
569
|
+
*/
|
|
570
|
+
clear() {
|
|
571
|
+
this._input.value = '';
|
|
572
|
+
this._mentions = [];
|
|
573
|
+
this._files = [];
|
|
574
|
+
this._renderFiles();
|
|
575
|
+
this._closeMentions();
|
|
576
|
+
this._afterTextChange();
|
|
577
|
+
return this;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
/**
|
|
581
|
+
* Shows Stop in place of Send while an agent works; Escape in the text then emits `stop`.
|
|
582
|
+
* @param {boolean} busy
|
|
583
|
+
* @returns {this}
|
|
584
|
+
*/
|
|
585
|
+
setBusy(busy) {
|
|
586
|
+
this._busy = Boolean(busy);
|
|
587
|
+
this._syncSend();
|
|
588
|
+
return this;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/** @returns {boolean} Whether Stop is showing. */
|
|
592
|
+
isBusy() {
|
|
593
|
+
return this._busy;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
/**
|
|
597
|
+
* Changes the quiet text in the bar beside Send — what Enter does now, or why sending waits.
|
|
598
|
+
* An empty text hides it.
|
|
599
|
+
* @param {string} text
|
|
600
|
+
* @returns {this}
|
|
601
|
+
*/
|
|
602
|
+
setHint(text) {
|
|
603
|
+
const hint = /** @type {HTMLElement} */ (this.refs.hint);
|
|
604
|
+
hint.textContent = String(text ?? '');
|
|
605
|
+
hint.hidden = !hint.textContent;
|
|
606
|
+
return this;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* Changes the line under the composer, which also describes the text box. An empty text hides
|
|
611
|
+
* it.
|
|
612
|
+
* @param {string} text
|
|
613
|
+
* @returns {this}
|
|
614
|
+
*/
|
|
615
|
+
setNote(text) {
|
|
616
|
+
const note = /** @type {HTMLElement} */ (this.refs.note);
|
|
617
|
+
note.textContent = String(text ?? '');
|
|
618
|
+
note.hidden = !note.textContent;
|
|
619
|
+
if (note.hidden) this._input.removeAttribute('aria-describedby');
|
|
620
|
+
else this._input.setAttribute('aria-describedby', note.id);
|
|
621
|
+
return this;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
/**
|
|
625
|
+
* Turns input on or off.
|
|
626
|
+
* @param {boolean} disabled
|
|
627
|
+
* @returns {this}
|
|
628
|
+
*/
|
|
629
|
+
setDisabled(disabled) {
|
|
630
|
+
this._disabled = Boolean(disabled);
|
|
631
|
+
if (this._disabled) {
|
|
632
|
+
this._closeMentions();
|
|
633
|
+
this._setDropping(false);
|
|
634
|
+
}
|
|
635
|
+
this._syncState();
|
|
636
|
+
this._syncSend();
|
|
637
|
+
return this;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/**
|
|
641
|
+
* Changes the composer in place: the text, its caret and selection, the focus, the attachments,
|
|
642
|
+
* the context chips and the draft all stay. This is how a placeholder follows the model picked
|
|
643
|
+
* beside the composer. Keys left out (or `undefined`) are kept; other keys are ignored. Emits
|
|
644
|
+
* nothing.
|
|
645
|
+
* @param {ComposerUpdate} [partial={}]
|
|
646
|
+
* @returns {this}
|
|
647
|
+
*/
|
|
648
|
+
update(partial = {}) {
|
|
649
|
+
if (this._destroyed || !partial || typeof partial !== 'object') return this;
|
|
650
|
+
/** @param {keyof ComposerUpdate} key @returns {boolean} */
|
|
651
|
+
const has = (key) => Object.hasOwn(partial, key) && partial[key] !== undefined;
|
|
652
|
+
const input = this._input;
|
|
653
|
+
if (has('label')) {
|
|
654
|
+
const label = this._labelText(partial.label);
|
|
655
|
+
input.setAttribute('aria-label', label);
|
|
656
|
+
this.el.setAttribute('aria-label', label);
|
|
657
|
+
}
|
|
658
|
+
if (has('placeholder')) {
|
|
659
|
+
const placeholder = String(partial.placeholder ?? '');
|
|
660
|
+
if (placeholder) input.placeholder = placeholder;
|
|
661
|
+
else input.removeAttribute('placeholder');
|
|
662
|
+
}
|
|
663
|
+
if (has('hint')) this.setHint(String(partial.hint ?? ''));
|
|
664
|
+
if (has('note')) this.setNote(String(partial.note ?? ''));
|
|
665
|
+
if (has('submitKey')) this._submitKey = submitKeyOf(partial.submitKey);
|
|
666
|
+
if (has('sendLabel')) this._sendLabel = sendLabelText(partial.sendLabel);
|
|
667
|
+
if (has('disabled')) this.setDisabled(Boolean(partial.disabled));
|
|
668
|
+
if (has('busy')) this.setBusy(Boolean(partial.busy));
|
|
669
|
+
this._syncSend();
|
|
670
|
+
return this;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
/**
|
|
674
|
+
* Replaces the bar's extra tools. A tool that held the focus keeps it when a tool with the same
|
|
675
|
+
* id is still there (and enabled); otherwise the focus moves to the text. Emits nothing.
|
|
676
|
+
* @param {ComposerTool[]} tools
|
|
677
|
+
* @returns {this}
|
|
678
|
+
*/
|
|
679
|
+
setTools(tools) {
|
|
680
|
+
if (this._destroyed) return this;
|
|
681
|
+
this._tools = normalizeTools(tools);
|
|
682
|
+
this._renderTools();
|
|
683
|
+
return this;
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
/**
|
|
687
|
+
* Presses or releases a tool, which makes it a toggle if it was a plain button. Emits nothing.
|
|
688
|
+
* @param {string} id
|
|
689
|
+
* @param {boolean} pressed
|
|
690
|
+
* @returns {this}
|
|
691
|
+
*/
|
|
692
|
+
setToolPressed(id, pressed) {
|
|
693
|
+
const tool = this._tools.find((entry) => entry.id === String(id));
|
|
694
|
+
if (!tool) return this;
|
|
695
|
+
tool.pressed = Boolean(pressed);
|
|
696
|
+
this._toolButton(tool.id)?.setAttribute('aria-pressed', String(tool.pressed));
|
|
697
|
+
return this;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
/**
|
|
701
|
+
* Attaches files, as far as `accept` and `maxFiles` allow. Emits `attach` with the files taken.
|
|
702
|
+
* @param {FileList|File[]} files
|
|
703
|
+
* @returns {this}
|
|
704
|
+
*/
|
|
705
|
+
addFiles(files) {
|
|
706
|
+
this._addFiles(Array.from(files ?? []), false);
|
|
707
|
+
return this;
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
/** @returns {File[]} The attached files. */
|
|
711
|
+
getFiles() {
|
|
712
|
+
return this._files.map((entry) => entry.file);
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
/**
|
|
716
|
+
* Adds a context chip, or replaces the chip with the same id. Emits nothing.
|
|
717
|
+
* @param {ContextChip} chip
|
|
718
|
+
* @returns {this}
|
|
719
|
+
*/
|
|
720
|
+
addContext(chip) {
|
|
721
|
+
this._putContext(chip);
|
|
722
|
+
this._renderContext();
|
|
723
|
+
return this;
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
/**
|
|
727
|
+
* Removes a context chip. Emits nothing.
|
|
728
|
+
* @param {string} id
|
|
729
|
+
* @returns {this}
|
|
730
|
+
*/
|
|
731
|
+
removeContext(id) {
|
|
732
|
+
this._context = this._context.filter((chip) => chip.id !== String(id));
|
|
733
|
+
this._renderContext();
|
|
734
|
+
return this;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
/** @returns {ContextChip[]} The context chips. */
|
|
738
|
+
getContext() {
|
|
739
|
+
return this._context.map((chip) => ({ ...chip }));
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
/** Removes what the composer drew and the launcher it opened, and stops listening. @returns {void} */
|
|
743
|
+
destroy() {
|
|
744
|
+
if (this._destroyed) return;
|
|
745
|
+
this._destroyed = true;
|
|
746
|
+
this._mentionSequence += 1;
|
|
747
|
+
this._observer?.disconnect();
|
|
748
|
+
this._observer = null;
|
|
749
|
+
this._position?.destroy();
|
|
750
|
+
this._position = null;
|
|
751
|
+
this._launcher?.destroy();
|
|
752
|
+
this._launcher = null;
|
|
753
|
+
super.destroy();
|
|
754
|
+
if (this._original) restoreTarget(this.el, this._original);
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
/* ------------------------------------------------------------------ text -- */
|
|
758
|
+
|
|
759
|
+
/** @private @returns {void} */
|
|
760
|
+
_onInput() {
|
|
761
|
+
this._afterTextChange();
|
|
762
|
+
this._updateMentions();
|
|
763
|
+
this.emit('input', { text: this._input.value });
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
/** @private @returns {void} */
|
|
767
|
+
_afterTextChange() {
|
|
768
|
+
this._autosize();
|
|
769
|
+
this._syncSend();
|
|
770
|
+
this._saveDraft();
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
/** @private @returns {void} */
|
|
774
|
+
_saveDraft() {
|
|
775
|
+
if (!this._draft) return;
|
|
776
|
+
const key = String(this.options.draftKey);
|
|
777
|
+
if (this._input.value) this._draft.set(key, this._input.value);
|
|
778
|
+
else this._draft.remove(key);
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
/** Grows the text with its content, between `minRows` and `maxRows`. @private @returns {void} */
|
|
782
|
+
_autosize() {
|
|
783
|
+
const input = this._input;
|
|
784
|
+
if (!input.isConnected || typeof getComputedStyle !== 'function') return;
|
|
785
|
+
input.style.blockSize = 'auto';
|
|
786
|
+
const style = getComputedStyle(input);
|
|
787
|
+
const fontSize = parseFloat(style.fontSize) || 16;
|
|
788
|
+
const lineHeight = parseFloat(style.lineHeight) || fontSize * 1.5;
|
|
789
|
+
const padding = (parseFloat(style.paddingBlockStart) || 0) + (parseFloat(style.paddingBlockEnd) || 0);
|
|
790
|
+
const border = (parseFloat(style.borderBlockStartWidth) || 0) + (parseFloat(style.borderBlockEndWidth) || 0);
|
|
791
|
+
const { height, scrolls } = autosizeHeight(input.scrollHeight, {
|
|
792
|
+
lineHeight, padding, border, minRows: this._rows.min, maxRows: this._rows.max
|
|
793
|
+
});
|
|
794
|
+
input.style.blockSize = `${height}px`;
|
|
795
|
+
input.dataset.scrolls = String(scrolls);
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
/* ------------------------------------------------------------------ keys -- */
|
|
799
|
+
|
|
800
|
+
/**
|
|
801
|
+
* @private
|
|
802
|
+
* @param {KeyboardEvent} event
|
|
803
|
+
* @returns {void}
|
|
804
|
+
*/
|
|
805
|
+
_onKeydown(event) {
|
|
806
|
+
if (event.isComposing || event.keyCode === 229) return;
|
|
807
|
+
const mention = this._mention;
|
|
808
|
+
if (mention) {
|
|
809
|
+
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
810
|
+
event.preventDefault();
|
|
811
|
+
const step = event.key === 'ArrowDown' ? 1 : -1;
|
|
812
|
+
this._highlight((mention.index + step + mention.items.length) % mention.items.length);
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
815
|
+
if ((event.key === 'Enter' || event.key === 'Tab') && !event.shiftKey && !event.altKey
|
|
816
|
+
&& !event.metaKey && !event.ctrlKey) {
|
|
817
|
+
event.preventDefault();
|
|
818
|
+
this._pickMention(mention.index);
|
|
819
|
+
return;
|
|
820
|
+
}
|
|
821
|
+
if (event.key === 'Escape') {
|
|
822
|
+
event.preventDefault();
|
|
823
|
+
event.stopPropagation();
|
|
824
|
+
this._closeMentions();
|
|
825
|
+
return;
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
const plain = !event.shiftKey && !event.altKey && !event.metaKey && !event.ctrlKey;
|
|
829
|
+
if (isSubmitKey(event, this._submitKey)) {
|
|
830
|
+
event.preventDefault();
|
|
831
|
+
this._send();
|
|
832
|
+
} else if (event.key === 'Escape' && this._busy) {
|
|
833
|
+
event.preventDefault();
|
|
834
|
+
this._stop();
|
|
835
|
+
} else if (event.key === 'ArrowUp' && plain && this._input.value === '') {
|
|
836
|
+
event.preventDefault();
|
|
837
|
+
this.emit('historyprevious', {});
|
|
838
|
+
} else if (event.key === '/' && plain && this._input.value === '' && this._commands) {
|
|
839
|
+
event.preventDefault();
|
|
840
|
+
this._openCommands();
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
/* ------------------------------------------------------------- send/stop -- */
|
|
845
|
+
|
|
846
|
+
/** @private @returns {void} @fires Composer#send */
|
|
847
|
+
_send() {
|
|
848
|
+
if (this._disabled || this._busy) return;
|
|
849
|
+
const text = this._input.value.trim();
|
|
850
|
+
const files = this.getFiles();
|
|
851
|
+
if (!text && !files.length) return;
|
|
852
|
+
const trigger = this._mentionSource?.trigger ?? '@';
|
|
853
|
+
const detail = {
|
|
854
|
+
text,
|
|
855
|
+
files,
|
|
856
|
+
mentions: mentionsInText(text, this._mentions, trigger).map((item) => ({ ...item })),
|
|
857
|
+
context: this.getContext()
|
|
858
|
+
};
|
|
859
|
+
const event = this.emit('send', detail, { honorDomCancellation: true });
|
|
860
|
+
if (event.defaultPrevented || this._destroyed) return;
|
|
861
|
+
this.clear();
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
/** @private @returns {void} @fires Composer#stop */
|
|
865
|
+
_stop() {
|
|
866
|
+
if (!this._busy) return;
|
|
867
|
+
this.emit('stop', {});
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
/**
|
|
871
|
+
* The Send button's glyph, text, name and readiness. With `sendLabel` the text is visible and is
|
|
872
|
+
* the name; without it the button is a glyph named by `aria-label` alone.
|
|
873
|
+
* @private
|
|
874
|
+
* @returns {void}
|
|
875
|
+
*/
|
|
876
|
+
_syncSend() {
|
|
877
|
+
const button = /** @type {HTMLButtonElement} */ (this.refs.send);
|
|
878
|
+
const busy = this._busy;
|
|
879
|
+
const mode = busy ? 'stop' : 'send';
|
|
880
|
+
const drawn = `${mode}\n${this._submitKey}\n${this._sendLabel}`;
|
|
881
|
+
if (drawn !== this._sendDrawn) {
|
|
882
|
+
this._sendDrawn = drawn;
|
|
883
|
+
const chord = this._submitKey === 'mod+enter' ? 'Mod+Enter' : 'Enter';
|
|
884
|
+
const apple = isApplePlatform();
|
|
885
|
+
const name = busy ? this.msg('composer.stop') : (this._sendLabel || this.msg('composer.send'));
|
|
886
|
+
const keys = busy ? 'Escape' : (chord === 'Enter' ? 'Enter' : `${apple ? 'Meta' : 'Control'}+Enter`);
|
|
887
|
+
const shown = keyLabels(busy ? 'Escape' : chord, { apple }).join(apple ? '' : '+');
|
|
888
|
+
button.dataset.mode = mode;
|
|
889
|
+
const glyph = icon(busy ? 'square' : 'arrow-up', { size: ICON_SIZE.control });
|
|
890
|
+
if (this._sendLabel) button.replaceChildren(glyph, h('span', { class: 'zx-composer__send-label' }, name));
|
|
891
|
+
else button.replaceChildren(glyph);
|
|
892
|
+
button.setAttribute('aria-label', name);
|
|
893
|
+
button.setAttribute('aria-keyshortcuts', keys);
|
|
894
|
+
button.title = `${name} (${shown})`;
|
|
895
|
+
}
|
|
896
|
+
// Never disabled while the composer is live: a disabled button drops the focus it holds.
|
|
897
|
+
const empty = !this._input.value.trim() && !this._files.length;
|
|
898
|
+
button.setAttribute('aria-disabled', String(!busy && empty));
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
/** Disabled state on the text and the buttons. @private @returns {void} */
|
|
902
|
+
_syncState() {
|
|
903
|
+
const disabled = this._disabled;
|
|
904
|
+
this._input.disabled = disabled;
|
|
905
|
+
if (disabled) this.el.setAttribute('aria-disabled', 'true');
|
|
906
|
+
else this.el.removeAttribute('aria-disabled');
|
|
907
|
+
for (const button of this.el.querySelectorAll('.zx-composer__frame button')) {
|
|
908
|
+
/** @type {HTMLButtonElement} */ (button).disabled = disabled;
|
|
909
|
+
}
|
|
910
|
+
this._renderTools();
|
|
911
|
+
this._renderFiles();
|
|
912
|
+
this._renderContext();
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
/**
|
|
916
|
+
* The accessible name for a `label` option: the text given, or the `composer.label` message.
|
|
917
|
+
* @private
|
|
918
|
+
* @param {string|null|undefined} label
|
|
919
|
+
* @returns {string}
|
|
920
|
+
*/
|
|
921
|
+
_labelText(label) {
|
|
922
|
+
return String(label ?? this.msg('composer.label'));
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
/* ----------------------------------------------------------------- tools -- */
|
|
926
|
+
|
|
927
|
+
/**
|
|
928
|
+
* Draws the extra tools, and puts the focus back on the tool that held it.
|
|
929
|
+
* @private
|
|
930
|
+
* @returns {void}
|
|
931
|
+
*/
|
|
932
|
+
_renderTools() {
|
|
933
|
+
const box = /** @type {HTMLElement} */ (this.refs.tools);
|
|
934
|
+
const active = /** @type {HTMLElement|null} */ (box.ownerDocument.activeElement);
|
|
935
|
+
const focused = active && box.contains(active) ? active.dataset.tool ?? null : null;
|
|
936
|
+
box.hidden = this._tools.length === 0;
|
|
937
|
+
box.replaceChildren(...this._tools.map((tool) => {
|
|
938
|
+
const button = iconButton(tool.icon, tool.label, 'tool');
|
|
939
|
+
button.dataset.tool = tool.id;
|
|
940
|
+
if (typeof tool.pressed === 'boolean') button.setAttribute('aria-pressed', String(tool.pressed));
|
|
941
|
+
if (typeof tool.expanded === 'boolean') button.setAttribute('aria-expanded', String(tool.expanded));
|
|
942
|
+
if (tool.controls) button.setAttribute('aria-controls', String(tool.controls));
|
|
943
|
+
button.disabled = this._disabled || Boolean(tool.disabled);
|
|
944
|
+
return button;
|
|
945
|
+
}));
|
|
946
|
+
if (focused === null) return;
|
|
947
|
+
const next = this._toolButton(focused);
|
|
948
|
+
if (next && !next.disabled) next.focus();
|
|
949
|
+
else if (!this._input.disabled) this._input.focus();
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
/**
|
|
953
|
+
* @private
|
|
954
|
+
* @param {string} id
|
|
955
|
+
* @returns {HTMLButtonElement|null}
|
|
956
|
+
*/
|
|
957
|
+
_toolButton(id) {
|
|
958
|
+
const buttons = /** @type {HTMLButtonElement[]} */ (Array.from(this.refs.tools.children));
|
|
959
|
+
return buttons.find((button) => button.dataset.tool === id) ?? null;
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
/**
|
|
963
|
+
* @private
|
|
964
|
+
* @param {MouseEvent} event
|
|
965
|
+
* @returns {void}
|
|
966
|
+
* @fires Composer#tool
|
|
967
|
+
*/
|
|
968
|
+
_onToolClick(event) {
|
|
969
|
+
const button = /** @type {HTMLButtonElement|null} */ (/** @type {Element|null} */ (event.target)?.closest?.('[data-tool]'));
|
|
970
|
+
if (!button || button.disabled || this._disabled) return;
|
|
971
|
+
const tool = this._tools.find((entry) => entry.id === button.dataset.tool);
|
|
972
|
+
if (!tool) return;
|
|
973
|
+
this.emit('tool', { id: tool.id, tool: { ...tool } });
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
/* -------------------------------------------------------------- mentions -- */
|
|
977
|
+
|
|
978
|
+
/**
|
|
979
|
+
* Opens, filters or closes the suggestions from the word the caret is in.
|
|
980
|
+
* @private
|
|
981
|
+
* @returns {void}
|
|
982
|
+
*/
|
|
983
|
+
_updateMentions() {
|
|
984
|
+
const source = this._mentionSource;
|
|
985
|
+
const input = this._input;
|
|
986
|
+
if (!source || this._disabled || input.selectionStart !== input.selectionEnd) {
|
|
987
|
+
this._closeMentions();
|
|
988
|
+
return;
|
|
989
|
+
}
|
|
990
|
+
const caret = input.selectionStart ?? input.value.length;
|
|
991
|
+
const found = mentionQuery(input.value, caret, source.trigger);
|
|
992
|
+
if (!found) {
|
|
993
|
+
this._closeMentions();
|
|
994
|
+
return;
|
|
995
|
+
}
|
|
996
|
+
this._mentionSequence += 1;
|
|
997
|
+
const sequence = this._mentionSequence;
|
|
998
|
+
/** @param {unknown} items */
|
|
999
|
+
const apply = (items) => {
|
|
1000
|
+
if (sequence !== this._mentionSequence || this._destroyed) return;
|
|
1001
|
+
this._showMentions(found.start, caret, Array.isArray(items) ? items.filter(isMentionItem) : []);
|
|
1002
|
+
};
|
|
1003
|
+
if (typeof source.items !== 'function') {
|
|
1004
|
+
apply(filterMentions(source.items, found.query));
|
|
1005
|
+
return;
|
|
1006
|
+
}
|
|
1007
|
+
let result;
|
|
1008
|
+
try {
|
|
1009
|
+
result = source.items(found.query);
|
|
1010
|
+
} catch {
|
|
1011
|
+
this._closeMentions();
|
|
1012
|
+
return;
|
|
1013
|
+
}
|
|
1014
|
+
if (result && typeof (/** @type {Promise<unknown>} */ (result)).then === 'function') {
|
|
1015
|
+
/** @type {Promise<MentionItem[]>} */ (result).then(apply, () => {
|
|
1016
|
+
if (sequence === this._mentionSequence) this._closeMentions();
|
|
1017
|
+
});
|
|
1018
|
+
} else {
|
|
1019
|
+
apply(result);
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
/**
|
|
1024
|
+
* @private
|
|
1025
|
+
* @param {number} start Offset of the trigger.
|
|
1026
|
+
* @param {number} end Caret offset.
|
|
1027
|
+
* @param {MentionItem[]} items
|
|
1028
|
+
* @returns {void}
|
|
1029
|
+
*/
|
|
1030
|
+
_showMentions(start, end, items) {
|
|
1031
|
+
if (!items.length) {
|
|
1032
|
+
this._closeMentions();
|
|
1033
|
+
return;
|
|
1034
|
+
}
|
|
1035
|
+
const list = /** @type {HTMLElement} */ (this.refs.list);
|
|
1036
|
+
const highlighted = this._mention?.items[this._mention.index];
|
|
1037
|
+
const kept = highlighted ? items.findIndex((item) => String(item.id) === String(highlighted.id)) : -1;
|
|
1038
|
+
this._mention = { start, end, items, index: Math.max(0, kept) };
|
|
1039
|
+
list.replaceChildren(...items.map((item, index) => h('div', {
|
|
1040
|
+
class: 'zx-composer__option', role: 'option', id: `${list.id}-${index}`,
|
|
1041
|
+
ariaSelected: 'false', dataset: { index: String(index) }
|
|
1042
|
+
},
|
|
1043
|
+
item.icon ? icon(item.icon, { size: ICON_SIZE.control }) : null,
|
|
1044
|
+
h('span', { class: 'zx-composer__option-text' },
|
|
1045
|
+
h('span', { class: 'zx-composer__option-label' }, String(item.label)),
|
|
1046
|
+
item.description ? h('span', { class: 'zx-composer__option-description' }, String(item.description)) : null))));
|
|
1047
|
+
if (list.hidden) {
|
|
1048
|
+
list.hidden = false;
|
|
1049
|
+
this._position = position(/** @type {HTMLElement} */ (this.refs.frame), list, {
|
|
1050
|
+
placement: 'top-start', offset: 6, flip: true
|
|
1051
|
+
});
|
|
1052
|
+
} else {
|
|
1053
|
+
this._position?.update();
|
|
1054
|
+
}
|
|
1055
|
+
this._input.setAttribute('aria-autocomplete', 'list');
|
|
1056
|
+
this._input.setAttribute('aria-controls', list.id);
|
|
1057
|
+
this._highlight(this._mention.index);
|
|
1058
|
+
this._announce(items.length === 1 ? this.msg('composer.match') : this.msg('composer.matches', items.length));
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
/**
|
|
1062
|
+
* @private
|
|
1063
|
+
* @param {number} index
|
|
1064
|
+
* @returns {void}
|
|
1065
|
+
*/
|
|
1066
|
+
_highlight(index) {
|
|
1067
|
+
const mention = this._mention;
|
|
1068
|
+
if (!mention || !Number.isInteger(index) || !mention.items[index]) return;
|
|
1069
|
+
mention.index = index;
|
|
1070
|
+
const options = /** @type {HTMLElement[]} */ (Array.from(this.refs.list.children));
|
|
1071
|
+
options.forEach((option, at) => option.setAttribute('aria-selected', String(at === index)));
|
|
1072
|
+
this._input.setAttribute('aria-activedescendant', options[index].id);
|
|
1073
|
+
options[index].scrollIntoView?.({ block: 'nearest' });
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
/** @private @returns {void} */
|
|
1077
|
+
_closeMentions() {
|
|
1078
|
+
this._mentionSequence += 1;
|
|
1079
|
+
if (!this._mention && /** @type {HTMLElement} */ (this.refs.list).hidden) return;
|
|
1080
|
+
this._mention = null;
|
|
1081
|
+
this._position?.destroy();
|
|
1082
|
+
this._position = null;
|
|
1083
|
+
const list = /** @type {HTMLElement} */ (this.refs.list);
|
|
1084
|
+
list.hidden = true;
|
|
1085
|
+
list.replaceChildren();
|
|
1086
|
+
this._input.removeAttribute('aria-autocomplete');
|
|
1087
|
+
this._input.removeAttribute('aria-controls');
|
|
1088
|
+
this._input.removeAttribute('aria-activedescendant');
|
|
1089
|
+
this._announce('');
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
/**
|
|
1093
|
+
* Writes `@<label> ` where the mention was typed and records the item.
|
|
1094
|
+
* @private
|
|
1095
|
+
* @param {number} index
|
|
1096
|
+
* @returns {void}
|
|
1097
|
+
* @fires Composer#mention
|
|
1098
|
+
*/
|
|
1099
|
+
_pickMention(index) {
|
|
1100
|
+
const mention = this._mention;
|
|
1101
|
+
const item = mention?.items[index];
|
|
1102
|
+
if (!mention || !item) return;
|
|
1103
|
+
const input = this._input;
|
|
1104
|
+
const trigger = this._mentionSource?.trigger ?? '@';
|
|
1105
|
+
const text = `${trigger}${item.label} `;
|
|
1106
|
+
const rest = input.value.slice(mention.end).replace(/^ /, '');
|
|
1107
|
+
input.value = `${input.value.slice(0, mention.start)}${text}${rest}`;
|
|
1108
|
+
const caret = mention.start + text.length;
|
|
1109
|
+
input.setSelectionRange(caret, caret);
|
|
1110
|
+
this._closeMentions();
|
|
1111
|
+
this._mentions = [...this._mentions.filter((entry) => String(entry.id) !== String(item.id)), item];
|
|
1112
|
+
this._afterTextChange();
|
|
1113
|
+
this.emit('mention', { item: { ...item } });
|
|
1114
|
+
this.emit('input', { text: input.value });
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
/**
|
|
1118
|
+
* Sets the polite status, once per change.
|
|
1119
|
+
* @private
|
|
1120
|
+
* @param {string} text
|
|
1121
|
+
* @returns {void}
|
|
1122
|
+
*/
|
|
1123
|
+
_announce(text) {
|
|
1124
|
+
if (text === this._announced) return;
|
|
1125
|
+
this._announced = text;
|
|
1126
|
+
this.refs.status.textContent = text;
|
|
1127
|
+
}
|
|
1128
|
+
|
|
1129
|
+
/* -------------------------------------------------------------- commands -- */
|
|
1130
|
+
|
|
1131
|
+
/**
|
|
1132
|
+
* Opens the commands in a launcher, created on first use.
|
|
1133
|
+
* @private
|
|
1134
|
+
* @returns {void}
|
|
1135
|
+
* @fires Composer#command
|
|
1136
|
+
*/
|
|
1137
|
+
_openCommands() {
|
|
1138
|
+
if (!this._commands || this._disabled || this._destroyed) return;
|
|
1139
|
+
this._closeMentions();
|
|
1140
|
+
this._launcher ??= new Launcher(null, {
|
|
1141
|
+
items: orderedCommands(this._commands),
|
|
1142
|
+
shortcut: false,
|
|
1143
|
+
scope: this.el,
|
|
1144
|
+
label: this.msg('composer.commands'),
|
|
1145
|
+
placeholder: this.msg('composer.commandsPlaceholder'),
|
|
1146
|
+
onselect: (event) => {
|
|
1147
|
+
const item = event.detail.item;
|
|
1148
|
+
this.emit('command', { item: item[COMMAND_SOURCE] ?? item });
|
|
1149
|
+
}
|
|
1150
|
+
});
|
|
1151
|
+
this._launcher.open();
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
/* ----------------------------------------------------------------- files -- */
|
|
1155
|
+
|
|
1156
|
+
/**
|
|
1157
|
+
* @private
|
|
1158
|
+
* @param {File[]} files
|
|
1159
|
+
* @param {boolean} fromPicker Whether the files came through the file input, which applied
|
|
1160
|
+
* `accept` already.
|
|
1161
|
+
* @returns {void}
|
|
1162
|
+
* @fires Composer#attach
|
|
1163
|
+
*/
|
|
1164
|
+
_addFiles(files, fromPicker) {
|
|
1165
|
+
if (!this.options.attachments || this._disabled) return;
|
|
1166
|
+
const room = Math.max(0, (Number(this.options.maxFiles) || 0) - this._files.length);
|
|
1167
|
+
const taken = files
|
|
1168
|
+
.filter((file) => file && typeof file === 'object')
|
|
1169
|
+
.filter((file) => fromPicker || acceptsFile(file, this.options.accept))
|
|
1170
|
+
.slice(0, room);
|
|
1171
|
+
if (!taken.length) return;
|
|
1172
|
+
const event = this.emit('attach', { files: [...taken] });
|
|
1173
|
+
if (event.defaultPrevented || this._destroyed) return;
|
|
1174
|
+
for (const file of taken) this._files.push({ id: uid('zx-composer-file'), file });
|
|
1175
|
+
this._renderFiles();
|
|
1176
|
+
this._syncSend();
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
/**
|
|
1180
|
+
* @private
|
|
1181
|
+
* @param {ClipboardEvent} event
|
|
1182
|
+
* @returns {void}
|
|
1183
|
+
*/
|
|
1184
|
+
_onPaste(event) {
|
|
1185
|
+
const files = Array.from(event.clipboardData?.files ?? []);
|
|
1186
|
+
if (!files.length || !this.options.attachments || this._disabled) return;
|
|
1187
|
+
// A copied file also carries its name as text; a copied selection that includes an image
|
|
1188
|
+
// keeps its text.
|
|
1189
|
+
if (!Array.from(event.clipboardData?.types ?? []).includes('text/plain')) event.preventDefault();
|
|
1190
|
+
this._addFiles(files, false);
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
/**
|
|
1194
|
+
* @private
|
|
1195
|
+
* @param {DragEvent} event
|
|
1196
|
+
* @returns {void}
|
|
1197
|
+
*/
|
|
1198
|
+
_onDrag(event) {
|
|
1199
|
+
if (this._disabled || !Array.from(event.dataTransfer?.types ?? []).includes('Files')) return;
|
|
1200
|
+
event.preventDefault();
|
|
1201
|
+
if (event.dataTransfer) event.dataTransfer.dropEffect = 'copy';
|
|
1202
|
+
this._setDropping(true);
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
/**
|
|
1206
|
+
* @private
|
|
1207
|
+
* @param {DragEvent} event
|
|
1208
|
+
* @returns {void}
|
|
1209
|
+
*/
|
|
1210
|
+
_onDrop(event) {
|
|
1211
|
+
this._setDropping(false);
|
|
1212
|
+
const files = Array.from(event.dataTransfer?.files ?? []);
|
|
1213
|
+
if (this._disabled || !files.length) return;
|
|
1214
|
+
event.preventDefault();
|
|
1215
|
+
this._addFiles(files, false);
|
|
1216
|
+
this._input.focus();
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
/**
|
|
1220
|
+
* @private
|
|
1221
|
+
* @param {boolean} dropping
|
|
1222
|
+
* @returns {void}
|
|
1223
|
+
*/
|
|
1224
|
+
_setDropping(dropping) {
|
|
1225
|
+
if (dropping) this.el.setAttribute('data-state', 'dropping');
|
|
1226
|
+
else if (this.el.getAttribute('data-state') === 'dropping') this.el.removeAttribute('data-state');
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
/* ----------------------------------------------------------------- chips -- */
|
|
1230
|
+
|
|
1231
|
+
/**
|
|
1232
|
+
* @private
|
|
1233
|
+
* @param {ContextChip} chip
|
|
1234
|
+
* @returns {void}
|
|
1235
|
+
*/
|
|
1236
|
+
_putContext(chip) {
|
|
1237
|
+
if (!chip || typeof chip !== 'object' || chip.id == null || chip.label == null) return;
|
|
1238
|
+
const entry = { ...chip, id: String(chip.id), label: String(chip.label) };
|
|
1239
|
+
const index = this._context.findIndex((item) => item.id === entry.id);
|
|
1240
|
+
if (index >= 0) this._context[index] = entry;
|
|
1241
|
+
else this._context.push(entry);
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
/** @private @returns {void} */
|
|
1245
|
+
_renderFiles() {
|
|
1246
|
+
const list = /** @type {HTMLElement} */ (this.refs.files);
|
|
1247
|
+
list.hidden = this._files.length === 0;
|
|
1248
|
+
list.replaceChildren(...this._files.map(({ id, file }) => this._chip({
|
|
1249
|
+
kind: 'file',
|
|
1250
|
+
id,
|
|
1251
|
+
label: String(file.name || ''),
|
|
1252
|
+
glyph: 'file',
|
|
1253
|
+
meta: Number.isFinite(file.size) ? formatFileSize(file.size, { decimals: 0, standard: 'si' }) : ''
|
|
1254
|
+
})));
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
/** @private @returns {void} */
|
|
1258
|
+
_renderContext() {
|
|
1259
|
+
const list = /** @type {HTMLElement} */ (this.refs.context);
|
|
1260
|
+
list.hidden = this._context.length === 0;
|
|
1261
|
+
list.replaceChildren(...this._context.map((chip) => this._chip({
|
|
1262
|
+
kind: 'context', id: chip.id, label: chip.label, glyph: chip.icon ?? null, meta: ''
|
|
1263
|
+
})));
|
|
1264
|
+
}
|
|
1265
|
+
|
|
1266
|
+
/**
|
|
1267
|
+
* @private
|
|
1268
|
+
* @param {{kind: 'file'|'context', id: string, label: string, glyph: string|null, meta: string}} chip
|
|
1269
|
+
* @returns {HTMLElement}
|
|
1270
|
+
*/
|
|
1271
|
+
_chip({ kind, id, label, glyph, meta }) {
|
|
1272
|
+
return h('li', { class: 'zx-composer__chip', dataset: { kind, id } },
|
|
1273
|
+
glyph ? icon(glyph, { size: ICON_SIZE.chip }) : null,
|
|
1274
|
+
h('span', { class: 'zx-composer__chip-label' }, label),
|
|
1275
|
+
meta ? h('span', { class: 'zx-composer__chip-meta' }, meta) : null,
|
|
1276
|
+
h('button', {
|
|
1277
|
+
type: 'button',
|
|
1278
|
+
class: 'zx-icon-btn zx-composer__remove',
|
|
1279
|
+
ariaLabel: this.msg('composer.remove', label),
|
|
1280
|
+
disabled: this._disabled,
|
|
1281
|
+
dataset: { remove: id, kind }
|
|
1282
|
+
}, icon('x', { size: ICON_SIZE.chip })));
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
/**
|
|
1286
|
+
* Removes the chip whose button was pressed, and keeps the focus in the list.
|
|
1287
|
+
* @private
|
|
1288
|
+
* @param {MouseEvent} event
|
|
1289
|
+
* @returns {void}
|
|
1290
|
+
* @fires Composer#remove
|
|
1291
|
+
*/
|
|
1292
|
+
_onChipClick(event) {
|
|
1293
|
+
const button = /** @type {HTMLElement|null} */ (/** @type {Element|null} */ (event.target)?.closest?.('[data-remove]'));
|
|
1294
|
+
if (!button || this._disabled) return;
|
|
1295
|
+
const id = String(button.dataset.remove);
|
|
1296
|
+
const kind = button.dataset.kind === 'context' ? 'context' : 'file';
|
|
1297
|
+
const list = /** @type {HTMLElement} */ (kind === 'file' ? this.refs.files : this.refs.context);
|
|
1298
|
+
const buttons = Array.from(list.querySelectorAll('[data-remove]'));
|
|
1299
|
+
const place = buttons.indexOf(button);
|
|
1300
|
+
/** @type {ComposerRemoveDetail} */
|
|
1301
|
+
let detail;
|
|
1302
|
+
if (kind === 'file') {
|
|
1303
|
+
const entry = this._files.find((item) => item.id === id);
|
|
1304
|
+
if (!entry) return;
|
|
1305
|
+
this._files = this._files.filter((item) => item !== entry);
|
|
1306
|
+
this._renderFiles();
|
|
1307
|
+
this._syncSend();
|
|
1308
|
+
detail = { kind, id, file: entry.file };
|
|
1309
|
+
} else {
|
|
1310
|
+
const chip = this._context.find((item) => item.id === id);
|
|
1311
|
+
if (!chip) return;
|
|
1312
|
+
this.removeContext(id);
|
|
1313
|
+
detail = { kind, id, chip: { ...chip } };
|
|
1314
|
+
}
|
|
1315
|
+
const rest = /** @type {HTMLElement[]} */ (Array.from(list.querySelectorAll('[data-remove]')));
|
|
1316
|
+
(rest[Math.min(place, rest.length - 1)] ?? this._input).focus();
|
|
1317
|
+
this.emit('remove', /** @type {Record<string, unknown>} */ (/** @type {unknown} */ (detail)));
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
/**
|
|
1322
|
+
* A quiet icon button in the composer's bar.
|
|
1323
|
+
* @param {string} name Icon name.
|
|
1324
|
+
* @param {string} label Accessible name.
|
|
1325
|
+
* @param {string} action What it does, for styling.
|
|
1326
|
+
* @returns {HTMLButtonElement}
|
|
1327
|
+
*/
|
|
1328
|
+
function iconButton(name, label, action) {
|
|
1329
|
+
return /** @type {HTMLButtonElement} */ (h('button', {
|
|
1330
|
+
type: 'button',
|
|
1331
|
+
class: 'zx-icon-btn zx-composer__tool',
|
|
1332
|
+
ariaLabel: label,
|
|
1333
|
+
title: label,
|
|
1334
|
+
dataset: { action }
|
|
1335
|
+
}, icon(name, { size: ICON_SIZE.control })));
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
/**
|
|
1339
|
+
* The send key an option names; anything but `'mod+enter'` is `'enter'`.
|
|
1340
|
+
* @param {unknown} value
|
|
1341
|
+
* @returns {'enter'|'mod+enter'}
|
|
1342
|
+
*/
|
|
1343
|
+
function submitKeyOf(value) {
|
|
1344
|
+
return value === 'mod+enter' ? 'mod+enter' : 'enter';
|
|
1345
|
+
}
|
|
1346
|
+
|
|
1347
|
+
/**
|
|
1348
|
+
* The visible Send text a `sendLabel` option asks for; empty keeps Send icon-only.
|
|
1349
|
+
* @param {unknown} value
|
|
1350
|
+
* @returns {string}
|
|
1351
|
+
*/
|
|
1352
|
+
function sendLabelText(value) {
|
|
1353
|
+
return value == null ? '' : String(value).trim();
|
|
1354
|
+
}
|
|
1355
|
+
|
|
1356
|
+
/**
|
|
1357
|
+
* @param {ComposerOptions['mentions']} mentions
|
|
1358
|
+
* @returns {{trigger: string, items: MentionSource['items']}|null}
|
|
1359
|
+
*/
|
|
1360
|
+
function normalizeMentionSource(mentions) {
|
|
1361
|
+
if (!mentions || typeof mentions !== 'object') return null;
|
|
1362
|
+
const items = mentions.items;
|
|
1363
|
+
if (!Array.isArray(items) && typeof items !== 'function') return null;
|
|
1364
|
+
const trigger = typeof mentions.trigger === 'string' && mentions.trigger ? mentions.trigger : '@';
|
|
1365
|
+
return { trigger, items };
|
|
1366
|
+
}
|
|
1367
|
+
|
|
1368
|
+
/**
|
|
1369
|
+
* @param {unknown} item
|
|
1370
|
+
* @returns {item is MentionItem}
|
|
1371
|
+
*/
|
|
1372
|
+
function isMentionItem(item) {
|
|
1373
|
+
return Boolean(item) && typeof item === 'object'
|
|
1374
|
+
&& /** @type {MentionItem} */ (item).id != null && /** @type {MentionItem} */ (item).label != null;
|
|
1375
|
+
}
|
|
1376
|
+
|
|
1377
|
+
/**
|
|
1378
|
+
* Lower case without accents, for matching.
|
|
1379
|
+
* @param {unknown} value
|
|
1380
|
+
* @returns {string}
|
|
1381
|
+
*/
|
|
1382
|
+
function fold(value) {
|
|
1383
|
+
return String(value ?? '').normalize('NFD').replace(/[̀-ͯ]/g, '').toLowerCase().trim();
|
|
1384
|
+
}
|
|
1385
|
+
|
|
1386
|
+
/**
|
|
1387
|
+
* @param {MentionItem} item
|
|
1388
|
+
* @param {string} needle Folded query.
|
|
1389
|
+
* @returns {number} 0 label prefix, 1 word prefix, 2 label substring, 3 id prefix or a word of the
|
|
1390
|
+
* description, 4 no match.
|
|
1391
|
+
*/
|
|
1392
|
+
function mentionRank(item, needle) {
|
|
1393
|
+
const label = fold(item.label);
|
|
1394
|
+
if (label.startsWith(needle)) return 0;
|
|
1395
|
+
if (words(item.label).some((word) => word.startsWith(needle))) return 1;
|
|
1396
|
+
if (label.includes(needle)) return 2;
|
|
1397
|
+
if (fold(item.id).startsWith(needle) || words(item.description).some((word) => word.startsWith(needle))) return 3;
|
|
1398
|
+
return 4;
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1401
|
+
/**
|
|
1402
|
+
* The folded words of a text.
|
|
1403
|
+
* @param {unknown} value
|
|
1404
|
+
* @returns {string[]}
|
|
1405
|
+
*/
|
|
1406
|
+
function words(value) {
|
|
1407
|
+
return fold(value).split(/[^\p{L}\p{N}]+/u).filter(Boolean);
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
/**
|
|
1411
|
+
* Copies of the commands that keep the application's order among equally ranked results (the
|
|
1412
|
+
* launcher otherwise sorts them by label), each remembering the item it was made from.
|
|
1413
|
+
* @param {LauncherItem[]} items
|
|
1414
|
+
* @returns {LauncherItem[]}
|
|
1415
|
+
*/
|
|
1416
|
+
function orderedCommands(items) {
|
|
1417
|
+
return items.filter((item) => item && typeof item === 'object').map((item, index) => ({
|
|
1418
|
+
...item,
|
|
1419
|
+
itemOrder: item.itemOrder ?? index,
|
|
1420
|
+
items: Array.isArray(item.items) ? orderedCommands(item.items) : item.items,
|
|
1421
|
+
[COMMAND_SOURCE]: item
|
|
1422
|
+
}));
|
|
1423
|
+
}
|
|
1424
|
+
|
|
1425
|
+
/** @returns {boolean} Whether the platform's command modifier is Cmd. */
|
|
1426
|
+
function isApplePlatform() {
|
|
1427
|
+
const nav = globalThis.navigator;
|
|
1428
|
+
if (!nav) return false;
|
|
1429
|
+
const platform = /** @type {{userAgentData?: {platform?: string}}} */ (nav).userAgentData?.platform || nav.platform || '';
|
|
1430
|
+
return /mac|iphone|ipad|ios/i.test(platform);
|
|
1431
|
+
}
|
|
1432
|
+
|
|
1433
|
+
/** Fired when the message is sent; preventable, which keeps the text and files. @event Composer#send @type {CustomEvent<ComposerSendDetail>} */
|
|
1434
|
+
/** Fired when Stop is pressed, or Escape in the text while busy. @event Composer#stop @type {CustomEvent<Record<string, never>>} */
|
|
1435
|
+
/** Fired when the reader changes the text. @event Composer#input @type {CustomEvent<{text: string}>} */
|
|
1436
|
+
/** Fired before files are attached; preventable. @event Composer#attach @type {CustomEvent<{files: File[]}>} */
|
|
1437
|
+
/** Fired after the reader removes an attachment or a context chip. @event Composer#remove @type {CustomEvent<ComposerRemoveDetail>} */
|
|
1438
|
+
/** Fired when a suggestion is picked. @event Composer#mention @type {CustomEvent<{item: MentionItem}>} */
|
|
1439
|
+
/** Fired when a command is chosen. @event Composer#command @type {CustomEvent<{item: LauncherItem}>} */
|
|
1440
|
+
/** Fired when one of the extra tools is pressed; the composer changes nothing itself. @event Composer#tool @type {CustomEvent<ComposerToolDetail>} */
|
|
1441
|
+
/** Fired for ArrowUp on an empty text. @event Composer#historyprevious @type {CustomEvent<Record<string, never>>} */
|