@zeyos/zx 5.6.0 → 5.7.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 +154 -0
- package/README.md +73 -7
- package/bin/zx-css.js +328 -0
- package/dist/types/components/account-menu/account-menu.d.ts +4 -2
- package/dist/types/components/activity-list/activity-list.d.ts +14 -8
- package/dist/types/components/agent-thread/ag-ui.d.ts +340 -0
- package/dist/types/components/agent-thread/agent-thread.d.ts +142 -14
- package/dist/types/components/agent-thread/ui-message-stream.d.ts +401 -0
- package/dist/types/components/app-sidebar/app-sidebar.d.ts +14 -8
- package/dist/types/components/breadcrumb/breadcrumb.d.ts +3 -3
- package/dist/types/components/calendar/calendar-model.d.ts +9 -2
- package/dist/types/components/calendar/calendar.d.ts +12 -4
- package/dist/types/components/chart/chart.d.ts +25 -13
- package/dist/types/components/code-editor/code-editor.d.ts +7 -2
- package/dist/types/components/composer/composer.d.ts +206 -4
- package/dist/types/components/composer/queue.d.ts +76 -0
- package/dist/types/components/copy/copy-button.d.ts +6 -4
- package/dist/types/components/copy/copy-input.d.ts +10 -6
- package/dist/types/components/datebox/date-range-box.d.ts +33 -0
- package/dist/types/components/datebox/placeholder.d.ts +7 -0
- package/dist/types/components/dock/dock.d.ts +12 -3
- package/dist/types/components/entity-ref/entity-ref.d.ts +5 -3
- package/dist/types/components/field-upload/field-upload.d.ts +3 -2
- package/dist/types/components/file-item/file-item.d.ts +3 -2
- package/dist/types/components/file-list/file-list.d.ts +14 -8
- package/dist/types/components/filter/filter-model.d.ts +31 -1
- package/dist/types/components/filter/filter.d.ts +20 -2
- package/dist/types/components/finder/finder.d.ts +14 -6
- package/dist/types/components/flag/flag.d.ts +99 -0
- package/dist/types/components/grid/grid.d.ts +1 -1
- package/dist/types/components/kanban-view/kanban-view.d.ts +8 -66
- package/dist/types/components/launcher/launcher.d.ts +2 -0
- package/dist/types/components/multi-value-editor/multi-value-editor.d.ts +3 -2
- package/dist/types/components/number-field/number-field.d.ts +17 -6
- package/dist/types/components/prompt-starters/prompt-starters.d.ts +107 -0
- package/dist/types/components/prose/prose.d.ts +9 -6
- package/dist/types/components/questionnaire/conditions.d.ts +270 -0
- package/dist/types/components/questionnaire/items.d.ts +52 -30
- package/dist/types/components/questionnaire/questionnaire.d.ts +44 -4
- package/dist/types/components/rating/rating.d.ts +5 -3
- package/dist/types/components/select/country.d.ts +106 -0
- package/dist/types/components/select/field-adapter.d.ts +2 -1
- package/dist/types/components/select/locale.d.ts +102 -0
- package/dist/types/components/select/select.d.ts +41 -2
- package/dist/types/components/slider/slider.d.ts +1 -0
- package/dist/types/components/sort-control/sort-control.d.ts +7 -3
- package/dist/types/components/source-list/source-list.d.ts +7 -5
- package/dist/types/components/split-view/split-view.d.ts +5 -3
- package/dist/types/components/surface/catalog.d.ts +260 -0
- package/dist/types/components/surface/elicit.d.ts +81 -0
- package/dist/types/components/surface/mcp.d.ts +165 -0
- package/dist/types/components/surface/schema.d.ts +314 -0
- package/dist/types/components/surface/stream.d.ts +118 -0
- package/dist/types/components/surface/surface.d.ts +718 -0
- package/dist/types/components/surface/text.d.ts +277 -0
- package/dist/types/components/surface/validate.d.ts +339 -0
- package/dist/types/components/table/table.d.ts +55 -3
- package/dist/types/components/table/window.d.ts +203 -0
- package/dist/types/components/toolbar/toolbar.d.ts +10 -6
- package/dist/types/components/tooltip/tooltip.d.ts +2 -2
- package/dist/types/components/tree/tree.d.ts +20 -13
- package/dist/types/core/component.d.ts +10 -0
- package/dist/types/core/date.d.ts +8 -1
- package/dist/types/core/dom.d.ts +1 -0
- package/dist/types/core/http.d.ts +10 -0
- package/dist/types/core/i18n.d.ts +38 -0
- package/dist/types/core/messages.d.ts +344 -1
- package/dist/types/core/storage.d.ts +7 -2
- package/dist/types/elements/define.d.ts +21 -46
- package/dist/types/elements/definitions.d.ts +147 -0
- package/dist/types/elements/kit.d.ts +7 -0
- package/dist/types/elements/reflect.d.ts +6 -3
- package/dist/types/index.d.ts +10 -1
- package/dist/types/internal/date-locale.d.ts +7 -0
- package/dist/types/internal/json-patch.d.ts +146 -0
- package/dist/types/locales/de.d.ts +13 -0
- package/dist/types/locales/en.d.ts +2 -0
- package/dist/types/locales/index.d.ts +823 -0
- package/dist/types/zeyos/view.d.ts +8 -1
- package/dist/zx-zeyos.esm.js +16802 -15180
- package/dist/zx-zeyos.esm.min.js +5 -5
- package/dist/zx.core.css +1276 -0
- package/dist/zx.core.min.css +1 -0
- package/dist/zx.css +498 -134
- package/dist/zx.esm.js +40778 -29654
- package/dist/zx.esm.min.js +36 -23
- package/dist/zx.global.js +40778 -29654
- package/dist/zx.global.min.js +36 -23
- package/dist/zx.layered.css +17302 -0
- package/dist/zx.layered.min.css +1 -0
- package/dist/zx.min.css +1 -1
- package/docs/agent-catalog.json +12501 -0
- package/docs/agent-surfaces.md +340 -0
- package/docs/api.json +851 -121
- package/docs/llms.md +1018 -71
- package/docs/llms.txt +30 -9
- package/package.json +29 -6
- package/src/components/account-menu/account-menu.js +6 -3
- package/src/components/activity-item/activity-item.js +2 -1
- package/src/components/activity-list/activity-list.js +16 -10
- package/src/components/agent-thread/ag-ui.js +1439 -0
- package/src/components/agent-thread/agent-thread.js +359 -54
- package/src/components/agent-thread/ui-message-stream.js +1281 -0
- package/src/components/app-sidebar/app-sidebar.js +18 -8
- package/src/components/approval-card/approval-card.js +3 -2
- package/src/components/block-view/block-view.js +7 -6
- package/src/components/breadcrumb/breadcrumb.js +8 -5
- package/src/components/calendar/calendar-model.js +8 -2
- package/src/components/calendar/calendar.js +53 -18
- package/src/components/chart/chart.js +54 -23
- package/src/components/code-editor/code-editor.js +45 -8
- package/src/components/code-editor/field-adapter.js +1 -1
- package/src/components/composer/composer.css +80 -1
- package/src/components/composer/composer.js +390 -16
- package/src/components/composer/queue.js +78 -0
- package/src/components/context-menu/context-menu.js +12 -1
- package/src/components/copy/copy-button.js +9 -7
- package/src/components/copy/copy-input.js +10 -6
- package/src/components/date-picker/date-picker.js +2 -1
- package/src/components/date-picker/date-range-picker.js +8 -7
- package/src/components/datebox/date-range-box.js +3 -9
- package/src/components/datebox/datebox.js +18 -21
- package/src/components/datebox/placeholder.js +18 -0
- package/src/components/dialog/dialog.js +1 -1
- package/src/components/dock/dock.js +34 -13
- package/src/components/entity-ref/entity-ref.js +16 -5
- package/src/components/field-upload/field-upload.js +81 -38
- package/src/components/file-item/file-item.js +70 -40
- package/src/components/file-list/file-list.js +15 -9
- package/src/components/filter/filter-model.js +46 -8
- package/src/components/filter/filter.css +3 -3
- package/src/components/filter/filter.js +29 -10
- package/src/components/filter-panel/filter-panel.js +13 -5
- package/src/components/finder/finder.js +50 -26
- package/src/components/flag/flag.css +40 -0
- package/src/components/flag/flag.js +177 -0
- package/src/components/form/form.js +1 -1
- package/src/components/grid/grid.js +22 -8
- package/src/components/kanban-view/kanban-view.js +51 -81
- package/src/components/launcher/launcher.js +17 -2
- package/src/components/list-toolbar/list-toolbar.js +2 -1
- package/src/components/master-panel/master-panel.js +1 -1
- package/src/components/menu-button/menu-button.js +4 -0
- package/src/components/message/message.js +3 -0
- package/src/components/multi-value-editor/multi-value-editor.js +10 -9
- package/src/components/navigation-bar/navigation-bar.js +3 -3
- package/src/components/number-field/number-field.js +29 -13
- package/src/components/prompt-starters/prompt-starters.css +91 -0
- package/src/components/prompt-starters/prompt-starters.js +206 -0
- package/src/components/prose/prose.js +14 -10
- package/src/components/questionnaire/conditions.js +666 -0
- package/src/components/questionnaire/items.js +150 -41
- package/src/components/questionnaire/questionnaire.js +126 -32
- package/src/components/rating/rating.js +5 -4
- package/src/components/run-status/run-status.js +2 -2
- package/src/components/select/country.js +136 -0
- package/src/components/select/field-adapter.js +7 -2
- package/src/components/select/locale.js +219 -0
- package/src/components/select/select.css +20 -22
- package/src/components/select/select.js +94 -12
- package/src/components/sheet/sheet-stack.js +10 -1
- package/src/components/slider/slider.js +7 -2
- package/src/components/sort-control/sort-control.js +15 -5
- package/src/components/source-list/source-list.js +7 -4
- package/src/components/split-view/split-view.js +5 -3
- package/src/components/surface/catalog.js +1334 -0
- package/src/components/surface/elicit.js +107 -0
- package/src/components/surface/mcp.js +243 -0
- package/src/components/surface/schema.js +644 -0
- package/src/components/surface/stream.js +245 -0
- package/src/components/surface/surface.css +308 -0
- package/src/components/surface/surface.js +2291 -0
- package/src/components/surface/text.js +513 -0
- package/src/components/surface/validate.js +2218 -0
- package/src/components/tabbox/tabbox.js +9 -0
- package/src/components/table/table.css +63 -3
- package/src/components/table/table.js +167 -28
- package/src/components/table/window.js +317 -0
- package/src/components/toolbar/toolbar.js +12 -7
- package/src/components/tooltip/tooltip.css +2 -3
- package/src/components/tooltip/tooltip.js +32 -5
- package/src/components/tree/tree.js +53 -23
- package/src/core/component.js +25 -1
- package/src/core/date.js +62 -16
- package/src/core/dom.js +22 -2
- package/src/core/format.js +15 -13
- package/src/core/http.js +29 -20
- package/src/core/i18n.js +83 -10
- package/src/core/keyboard.js +79 -8
- package/src/core/messages.js +376 -2
- package/src/core/overlay-host.js +1 -0
- package/src/core/storage.js +59 -6
- package/src/elements/define.js +178 -130
- package/src/elements/definitions.js +229 -0
- package/src/elements/kit.js +444 -0
- package/src/elements/reflect.js +8 -3
- package/src/index.js +10 -1
- package/src/internal/date-locale.js +11 -0
- package/src/internal/json-patch.js +405 -0
- package/src/locales/de.js +911 -0
- package/src/locales/en.js +2 -0
- package/src/locales/index.js +7 -0
- package/src/zeyos/calendar.js +2 -1
- package/src/zeyos/form.js +2 -1
- package/src/zeyos/launcher.js +7 -6
- package/src/zeyos/module-bar.js +2 -1
- package/src/zeyos/schema.js +37 -23
- package/src/zeyos/select.js +4 -3
- package/src/zeyos/table.js +2 -13
- package/src/zeyos/view.js +57 -17
- package/styles/core.css +11 -0
- package/styles/tokens/semantic.css +1 -0
- package/styles/zeyos.css +2 -0
- package/styles/zx.css +4 -8
- package/styles/zx.layered.css +1 -0
|
@@ -3,11 +3,16 @@ import { Component } from '../../core/component.js';
|
|
|
3
3
|
import { h, restoreTarget, snapshotTarget } from '../../core/dom.js';
|
|
4
4
|
import { formatFileSize } from '../../core/format.js';
|
|
5
5
|
import { ICON_SIZE, icon } from '../../core/icons.js';
|
|
6
|
+
import { rovingTabindex } from '../../core/keyboard.js';
|
|
6
7
|
import { position } from '../../core/position.js';
|
|
7
8
|
import { storage } from '../../core/storage.js';
|
|
8
9
|
import { uid } from '../../core/util.js';
|
|
10
|
+
import { Dialog } from '../dialog/dialog.js';
|
|
9
11
|
import { keyLabels } from '../kbd/kbd.js';
|
|
10
12
|
import { Launcher } from '../launcher/launcher.js';
|
|
13
|
+
import {
|
|
14
|
+
addToQueue, emptyQueue, isQueueFull, queueLimit, removeFromQueue, takeNext
|
|
15
|
+
} from './queue.js';
|
|
11
16
|
|
|
12
17
|
/** @typedef {import('../launcher/launcher.js').LauncherItem} LauncherItem */
|
|
13
18
|
|
|
@@ -77,6 +82,31 @@ import { Launcher } from '../launcher/launcher.js';
|
|
|
77
82
|
* @property {File[]} files Attached files, in the order they were added.
|
|
78
83
|
* @property {MentionItem[]} mentions The picked mentions whose `@<label>` is still in the text.
|
|
79
84
|
* @property {ContextChip[]} context The context chips.
|
|
85
|
+
* @property {boolean} [queued] `true` on a message that waited in the queue. Absent otherwise.
|
|
86
|
+
*/
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* A message waiting for the agent to finish. Everything it carries was taken when it was queued,
|
|
90
|
+
* so it is sent as it was written.
|
|
91
|
+
* @typedef {Object} QueuedMessage
|
|
92
|
+
* @property {string} id Stable identifier, for `removeQueued()`.
|
|
93
|
+
* @property {string} text The message, trimmed.
|
|
94
|
+
* @property {File[]} files Attached files, in the order they were added.
|
|
95
|
+
* @property {MentionItem[]} mentions The picked mentions whose `@<label>` is in the text.
|
|
96
|
+
* @property {ContextChip[]} context The context chips when it was queued.
|
|
97
|
+
*/
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* @typedef {Object} ComposerQueueDetail
|
|
101
|
+
* @property {QueuedMessage} item The message added.
|
|
102
|
+
* @property {QueuedMessage[]} items The whole queue, in order, the message added included.
|
|
103
|
+
*/
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @typedef {Object} ComposerDequeueDetail
|
|
107
|
+
* @property {QueuedMessage} item The message that left the queue.
|
|
108
|
+
* @property {QueuedMessage[]} items What is left of the queue, in order.
|
|
109
|
+
* @property {'sent'|'removed'|'edited'|'cleared'} reason Why it left.
|
|
80
110
|
*/
|
|
81
111
|
|
|
82
112
|
/**
|
|
@@ -96,6 +126,11 @@ import { Launcher } from '../launcher/launcher.js';
|
|
|
96
126
|
* @property {'enter'|'mod+enter'} [submitKey='enter'] `'enter'`: Enter sends and Shift+Enter breaks
|
|
97
127
|
* the line. `'mod+enter'`: Cmd+Enter (Ctrl+Enter elsewhere) sends and Enter breaks the line.
|
|
98
128
|
* @property {boolean} [busy=false] Whether an agent is working: Send becomes Stop.
|
|
129
|
+
* @property {boolean} [queue=false] Whether the send key holds a message back while the agent works.
|
|
130
|
+
* It is sent when the agent is no longer busy, one message each time busy ends, in order. The queue
|
|
131
|
+
* lives as long as the composer: it is not kept with the draft.
|
|
132
|
+
* @property {number} [queueLimit=10] Most messages that wait at once. At the limit the send key is
|
|
133
|
+
* refused, and the hint says why.
|
|
99
134
|
* @property {boolean} [disabled=false] Whether the composer takes no input.
|
|
100
135
|
* @property {boolean} [attachments=false] Whether files can be attached: an Attach button, paste,
|
|
101
136
|
* and drop.
|
|
@@ -129,6 +164,10 @@ import { Launcher } from '../launcher/launcher.js';
|
|
|
129
164
|
* @property {(event: CustomEvent<{item: MentionItem}>) => void} [onmention] Mention listener.
|
|
130
165
|
* @property {(event: CustomEvent<{item: LauncherItem}>) => void} [oncommand] Command listener.
|
|
131
166
|
* @property {(event: CustomEvent<ComposerToolDetail>) => void} [ontool] Tool listener.
|
|
167
|
+
* @property {(event: CustomEvent<ComposerQueueDetail>) => void} [onqueue] Listener for a message being
|
|
168
|
+
* queued.
|
|
169
|
+
* @property {(event: CustomEvent<ComposerDequeueDetail>) => void} [ondequeue] Listener for a message
|
|
170
|
+
* leaving the queue.
|
|
132
171
|
* @property {(event: CustomEvent<Record<string, never>>) => void} [onhistoryprevious] Listener for
|
|
133
172
|
* ArrowUp on an empty text.
|
|
134
173
|
*/
|
|
@@ -146,7 +185,17 @@ const MESSAGES = Object.freeze({
|
|
|
146
185
|
'composer.matches': '%1 suggestions',
|
|
147
186
|
'composer.commands': 'Commands',
|
|
148
187
|
'composer.commandsPlaceholder': 'Search commands',
|
|
149
|
-
'composer.drop': 'Drop files to attach'
|
|
188
|
+
'composer.drop': 'Drop files to attach',
|
|
189
|
+
'composer.queue': 'Queued (%1)',
|
|
190
|
+
'composer.queued': 'Queued; sends when the agent finishes',
|
|
191
|
+
'composer.queueEdit': 'Edit',
|
|
192
|
+
'composer.queueFile': '1 file',
|
|
193
|
+
'composer.queueFiles': '%1 files',
|
|
194
|
+
'composer.queueFull': 'Queue full (limit %1)',
|
|
195
|
+
'composer.queueRemove': 'Remove',
|
|
196
|
+
'composer.queueReplaceConfirm': 'Replace',
|
|
197
|
+
'composer.queueReplaceMessage': 'The queued message takes its place in the message box.',
|
|
198
|
+
'composer.queueReplaceTitle': 'Replace what you have written?'
|
|
150
199
|
});
|
|
151
200
|
|
|
152
201
|
/** Namespace the drafts are stored under. */
|
|
@@ -316,7 +365,8 @@ export function normalizeTools(tools) {
|
|
|
316
365
|
* A message box for talking to an agent: a text that grows with what is written, Send (Stop while
|
|
317
366
|
* the agent works), attachments, `@` mentions, `/` commands, and context chips. It keeps nothing
|
|
318
367
|
* but the unsent message: sending hands the text, files, mentions and context to the application,
|
|
319
|
-
* which owns the conversation.
|
|
368
|
+
* which owns the conversation. With `queue`, a message written while the agent works waits here
|
|
369
|
+
* until it is no longer busy.
|
|
320
370
|
*
|
|
321
371
|
* @fires Composer#send
|
|
322
372
|
* @fires Composer#stop
|
|
@@ -326,6 +376,8 @@ export function normalizeTools(tools) {
|
|
|
326
376
|
* @fires Composer#mention
|
|
327
377
|
* @fires Composer#command
|
|
328
378
|
* @fires Composer#tool
|
|
379
|
+
* @fires Composer#queue
|
|
380
|
+
* @fires Composer#dequeue
|
|
329
381
|
* @fires Composer#historyprevious
|
|
330
382
|
* @extends {Component<ComposerOptions>}
|
|
331
383
|
*/
|
|
@@ -339,6 +391,8 @@ export class Composer extends Component {
|
|
|
339
391
|
value: '',
|
|
340
392
|
submitKey: 'enter',
|
|
341
393
|
busy: false,
|
|
394
|
+
queue: false,
|
|
395
|
+
queueLimit: 10,
|
|
342
396
|
disabled: false,
|
|
343
397
|
attachments: false,
|
|
344
398
|
accept: '',
|
|
@@ -386,9 +440,22 @@ export class Composer extends Component {
|
|
|
386
440
|
this._width = 0;
|
|
387
441
|
/** What the Send button last drew: mode, send key and label, so it redraws only on a change. */
|
|
388
442
|
this._sendDrawn = '';
|
|
443
|
+
/** @type {QueuedMessage[]} Session state: never saved with the draft. */
|
|
444
|
+
this._queue = [];
|
|
445
|
+
this._queueLimit = queueLimit(options.queueLimit);
|
|
446
|
+
/** Whether the send key was refused at the limit, which the hint then says. */
|
|
447
|
+
this._queueRefused = false;
|
|
448
|
+
/** @type {{destroy: () => void}[]} The keyboard of each queued row. */
|
|
449
|
+
this._queueRoving = [];
|
|
450
|
+
/** Whether a queued message is being sent, and whether busy ended again meanwhile. */
|
|
451
|
+
this._sendingQueued = false;
|
|
452
|
+
this._busyEndedAgain = false;
|
|
453
|
+
/** The application's hint, which `_syncHint()` shows unless the queue has something to say. */
|
|
454
|
+
this._hint = String(options.hint ?? '');
|
|
389
455
|
this._mentionSource = normalizeMentionSource(options.mentions);
|
|
390
456
|
this._commands = Array.isArray(options.commands) && options.commands.length ? options.commands : null;
|
|
391
457
|
this._draft = options.draftKey ? storage(DRAFT_NAMESPACE) : null;
|
|
458
|
+
if (this._draft) this._draft.migrateLegacy([String(options.draftKey)]);
|
|
392
459
|
this._tools = normalizeTools(options.tools);
|
|
393
460
|
this._sendLabel = sendLabelText(options.sendLabel);
|
|
394
461
|
this._rows = rowRange(options.minRows, Number(options.maxRows) || 10);
|
|
@@ -431,7 +498,12 @@ export class Composer extends Component {
|
|
|
431
498
|
ref: 'send', type: 'button', class: 'zx-icon-btn zx-composer__send'
|
|
432
499
|
}));
|
|
433
500
|
const hint = h('span', { ref: 'hint', class: 'zx-composer__hint', hidden: !options.hint }, String(options.hint ?? ''));
|
|
501
|
+
const queueLabelId = uid('zx-composer-queue');
|
|
502
|
+
const queue = options.queue ? h('div', { ref: 'queue', class: 'zx-composer__queue', hidden: true },
|
|
503
|
+
h('span', { ref: 'queueLabel', class: 'zx-composer__queue-label', id: queueLabelId }),
|
|
504
|
+
h('ol', { ref: 'queueList', class: 'zx-composer__queue-list', 'aria-labelledby': queueLabelId })) : null;
|
|
434
505
|
const frame = h('div', { ref: 'frame', class: 'zx-composer__frame' },
|
|
506
|
+
queue,
|
|
435
507
|
context,
|
|
436
508
|
files,
|
|
437
509
|
input,
|
|
@@ -485,6 +557,10 @@ export class Composer extends Component {
|
|
|
485
557
|
if (commands) this.listen(commands, 'click', () => this._openCommands());
|
|
486
558
|
this.listen(tools, 'click', (event) => this._onToolClick(/** @type {MouseEvent} */ (event)));
|
|
487
559
|
this.listen(frame, 'click', (event) => this._onChipClick(/** @type {MouseEvent} */ (event)));
|
|
560
|
+
if (queue) {
|
|
561
|
+
this.listen(this.refs.queueList, 'click', (event) => this._onQueueClick(/** @type {MouseEvent} */ (event)));
|
|
562
|
+
this.listen(this.refs.queueList, 'keydown', (event) => this._onQueueKeydown(/** @type {KeyboardEvent} */ (event)));
|
|
563
|
+
}
|
|
488
564
|
// The suggestions keep the focus in the text: a press picks without blurring it.
|
|
489
565
|
this.listen(list, 'mousedown', (event) => event.preventDefault());
|
|
490
566
|
this.listen(list, 'click', (event) => {
|
|
@@ -578,13 +654,18 @@ export class Composer extends Component {
|
|
|
578
654
|
}
|
|
579
655
|
|
|
580
656
|
/**
|
|
581
|
-
* Shows Stop in place of Send while an agent works; Escape in the text then emits `stop`.
|
|
657
|
+
* Shows Stop in place of Send while an agent works; Escape in the text then emits `stop`. When
|
|
658
|
+
* busy ends, the first queued message is sent; the next waits for the next time it ends.
|
|
582
659
|
* @param {boolean} busy
|
|
583
660
|
* @returns {this}
|
|
661
|
+
* @fires Composer#send
|
|
662
|
+
* @fires Composer#dequeue
|
|
584
663
|
*/
|
|
585
664
|
setBusy(busy) {
|
|
665
|
+
const ended = this._busy && !busy;
|
|
586
666
|
this._busy = Boolean(busy);
|
|
587
667
|
this._syncSend();
|
|
668
|
+
if (ended) this._sendNextQueued();
|
|
588
669
|
return this;
|
|
589
670
|
}
|
|
590
671
|
|
|
@@ -600,9 +681,8 @@ export class Composer extends Component {
|
|
|
600
681
|
* @returns {this}
|
|
601
682
|
*/
|
|
602
683
|
setHint(text) {
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
hint.hidden = !hint.textContent;
|
|
684
|
+
this._hint = String(text ?? '');
|
|
685
|
+
this._syncHint();
|
|
606
686
|
return this;
|
|
607
687
|
}
|
|
608
688
|
|
|
@@ -739,10 +819,51 @@ export class Composer extends Component {
|
|
|
739
819
|
return this._context.map((chip) => ({ ...chip }));
|
|
740
820
|
}
|
|
741
821
|
|
|
742
|
-
/**
|
|
822
|
+
/** @returns {QueuedMessage[]} Copies of the queued messages, the next to be sent first. */
|
|
823
|
+
getQueue() {
|
|
824
|
+
return this._queue.map(copyQueued);
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
/**
|
|
828
|
+
* Removes every queued message. Emits `dequeue` for each, with `reason: 'cleared'`. Stop does not
|
|
829
|
+
* do this: call it from a `stop` listener to drop what was waiting along with the run.
|
|
830
|
+
* @returns {this}
|
|
831
|
+
* @fires Composer#dequeue
|
|
832
|
+
*/
|
|
833
|
+
clearQueue() {
|
|
834
|
+
const { queue, items } = emptyQueue(this._queue);
|
|
835
|
+
if (!items.length) return this;
|
|
836
|
+
this._queue = queue;
|
|
837
|
+
this._renderQueue();
|
|
838
|
+
for (const item of items) this._emitDequeue(item, 'cleared');
|
|
839
|
+
return this;
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
/**
|
|
843
|
+
* Removes one queued message. Emits `dequeue` with `reason: 'removed'`. An id that is not queued,
|
|
844
|
+
* because the message has been sent, say, changes nothing.
|
|
845
|
+
* @param {string} id
|
|
846
|
+
* @returns {this}
|
|
847
|
+
* @fires Composer#dequeue
|
|
848
|
+
*/
|
|
849
|
+
removeQueued(id) {
|
|
850
|
+
const { queue, item } = removeFromQueue(this._queue, String(id));
|
|
851
|
+
if (!item) return this;
|
|
852
|
+
this._queue = queue;
|
|
853
|
+
this._renderQueue();
|
|
854
|
+
this._emitDequeue(item, 'removed');
|
|
855
|
+
return this;
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
/**
|
|
859
|
+
* Removes what the composer drew and the launcher it opened, drops the queue, and stops listening.
|
|
860
|
+
* @returns {void}
|
|
861
|
+
*/
|
|
743
862
|
destroy() {
|
|
744
863
|
if (this._destroyed) return;
|
|
745
864
|
this._destroyed = true;
|
|
865
|
+
this._queue = [];
|
|
866
|
+
this._releaseQueueRoving();
|
|
746
867
|
this._mentionSequence += 1;
|
|
747
868
|
this._observer?.disconnect();
|
|
748
869
|
this._observer = null;
|
|
@@ -843,19 +964,33 @@ export class Composer extends Component {
|
|
|
843
964
|
|
|
844
965
|
/* ------------------------------------------------------------- send/stop -- */
|
|
845
966
|
|
|
846
|
-
/**
|
|
847
|
-
|
|
848
|
-
|
|
967
|
+
/**
|
|
968
|
+
* The message as it stands in the composer, or null when there is nothing to send.
|
|
969
|
+
* @private
|
|
970
|
+
* @returns {ComposerSendDetail|null}
|
|
971
|
+
*/
|
|
972
|
+
_message() {
|
|
849
973
|
const text = this._input.value.trim();
|
|
850
974
|
const files = this.getFiles();
|
|
851
|
-
if (!text && !files.length) return;
|
|
975
|
+
if (!text && !files.length) return null;
|
|
852
976
|
const trigger = this._mentionSource?.trigger ?? '@';
|
|
853
|
-
|
|
977
|
+
return {
|
|
854
978
|
text,
|
|
855
979
|
files,
|
|
856
980
|
mentions: mentionsInText(text, this._mentions, trigger).map((item) => ({ ...item })),
|
|
857
981
|
context: this.getContext()
|
|
858
982
|
};
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
/** @private @returns {void} @fires Composer#send @fires Composer#queue */
|
|
986
|
+
_send() {
|
|
987
|
+
if (this._disabled) return;
|
|
988
|
+
if (this._busy) {
|
|
989
|
+
if (this.options.queue) this._enqueue();
|
|
990
|
+
return;
|
|
991
|
+
}
|
|
992
|
+
const detail = this._message();
|
|
993
|
+
if (!detail) return;
|
|
859
994
|
const event = this.emit('send', detail, { honorDomCancellation: true });
|
|
860
995
|
if (event.defaultPrevented || this._destroyed) return;
|
|
861
996
|
this.clear();
|
|
@@ -898,6 +1033,13 @@ export class Composer extends Component {
|
|
|
898
1033
|
button.setAttribute('aria-disabled', String(!busy && empty));
|
|
899
1034
|
}
|
|
900
1035
|
|
|
1036
|
+
/** The quiet text beside Send: why the queue refused, else the application's. @private @returns {void} */
|
|
1037
|
+
_syncHint() {
|
|
1038
|
+
const hint = /** @type {HTMLElement} */ (this.refs.hint);
|
|
1039
|
+
hint.textContent = this._queueRefused ? this.msg('composer.queueFull', this._queueLimit) : this._hint;
|
|
1040
|
+
hint.hidden = !hint.textContent;
|
|
1041
|
+
}
|
|
1042
|
+
|
|
901
1043
|
/** Disabled state on the text and the buttons. @private @returns {void} */
|
|
902
1044
|
_syncState() {
|
|
903
1045
|
const disabled = this._disabled;
|
|
@@ -1118,12 +1260,228 @@ export class Composer extends Component {
|
|
|
1118
1260
|
* Sets the polite status, once per change.
|
|
1119
1261
|
* @private
|
|
1120
1262
|
* @param {string} text
|
|
1263
|
+
* @param {boolean} [again=false] Whether the same text is news again: a no-break space then tells
|
|
1264
|
+
* it apart from the last time, because a live region says nothing about a text that did not change.
|
|
1265
|
+
* @returns {void}
|
|
1266
|
+
*/
|
|
1267
|
+
_announce(text, again = false) {
|
|
1268
|
+
const spoken = again && text === this._announced ? `${text}\u00A0` : text;
|
|
1269
|
+
if (spoken === this._announced) return;
|
|
1270
|
+
this._announced = spoken;
|
|
1271
|
+
this.refs.status.textContent = spoken;
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
/* ----------------------------------------------------------------- queue -- */
|
|
1275
|
+
|
|
1276
|
+
/**
|
|
1277
|
+
* Holds the message back until the agent is no longer busy, or refuses it at the limit: the
|
|
1278
|
+
* message then stays where it was written.
|
|
1279
|
+
* @private
|
|
1280
|
+
* @returns {void}
|
|
1281
|
+
* @fires Composer#queue
|
|
1282
|
+
*/
|
|
1283
|
+
_enqueue() {
|
|
1284
|
+
const message = this._message();
|
|
1285
|
+
if (!message) return;
|
|
1286
|
+
const { queue, item } = addToQueue(this._queue, { id: uid('zx-composer-queued'), ...message }, this._queueLimit);
|
|
1287
|
+
if (!item) {
|
|
1288
|
+
this._queueRefused = true;
|
|
1289
|
+
this._syncHint();
|
|
1290
|
+
this._announce(this.msg('composer.queueFull', this._queueLimit), true);
|
|
1291
|
+
return;
|
|
1292
|
+
}
|
|
1293
|
+
this._queue = queue;
|
|
1294
|
+
this.clear();
|
|
1295
|
+
this._renderQueue();
|
|
1296
|
+
this._announce(this.msg('composer.queued'), true);
|
|
1297
|
+
this.emit('queue', { item: copyQueued(item), items: this.getQueue() });
|
|
1298
|
+
}
|
|
1299
|
+
|
|
1300
|
+
/**
|
|
1301
|
+
* Sends the first queued message for an end of busy. A `send` listener that ends busy again,
|
|
1302
|
+
* before it returns, does not send from inside this one: the next message goes when it is done.
|
|
1303
|
+
* @private
|
|
1304
|
+
* @returns {void}
|
|
1305
|
+
*/
|
|
1306
|
+
_sendNextQueued() {
|
|
1307
|
+
if (this._sendingQueued) {
|
|
1308
|
+
this._busyEndedAgain = true;
|
|
1309
|
+
return;
|
|
1310
|
+
}
|
|
1311
|
+
this._sendingQueued = true;
|
|
1312
|
+
do {
|
|
1313
|
+
this._busyEndedAgain = false;
|
|
1314
|
+
this._sendQueued();
|
|
1315
|
+
} while (this._busyEndedAgain);
|
|
1316
|
+
this._sendingQueued = false;
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1319
|
+
/**
|
|
1320
|
+
* Sends the first queued message, unless it must wait. What is being written stays: it is not the
|
|
1321
|
+
* message that goes. A prevented `send` was not sent, so the message stays first in line.
|
|
1322
|
+
* @private
|
|
1121
1323
|
* @returns {void}
|
|
1324
|
+
* @fires Composer#send
|
|
1325
|
+
* @fires Composer#dequeue
|
|
1122
1326
|
*/
|
|
1123
|
-
|
|
1124
|
-
if (
|
|
1125
|
-
|
|
1126
|
-
|
|
1327
|
+
_sendQueued() {
|
|
1328
|
+
if (this._busy || this._disabled || this._destroyed) return;
|
|
1329
|
+
const { queue, item } = takeNext(this._queue);
|
|
1330
|
+
if (!item) return;
|
|
1331
|
+
this._queue = queue;
|
|
1332
|
+
const { id, ...message } = copyQueued(item);
|
|
1333
|
+
const event = this.emit('send', { ...message, queued: true }, { honorDomCancellation: true });
|
|
1334
|
+
if (this._destroyed) return;
|
|
1335
|
+
if (event.defaultPrevented) this._queue = [item, ...this._queue];
|
|
1336
|
+
this._renderQueue();
|
|
1337
|
+
if (!event.defaultPrevented) this._emitDequeue(item, 'sent');
|
|
1338
|
+
}
|
|
1339
|
+
|
|
1340
|
+
/**
|
|
1341
|
+
* @private
|
|
1342
|
+
* @param {QueuedMessage} item
|
|
1343
|
+
* @param {ComposerDequeueDetail['reason']} reason
|
|
1344
|
+
* @returns {void}
|
|
1345
|
+
* @fires Composer#dequeue
|
|
1346
|
+
*/
|
|
1347
|
+
_emitDequeue(item, reason) {
|
|
1348
|
+
this.emit('dequeue', { item: copyQueued(item), items: this.getQueue(), reason });
|
|
1349
|
+
}
|
|
1350
|
+
|
|
1351
|
+
/**
|
|
1352
|
+
* Takes a queued message back into the composer, asking first when that would replace what is
|
|
1353
|
+
* being written. The message may have been sent while the question was open; nothing happens then.
|
|
1354
|
+
* @private
|
|
1355
|
+
* @param {string} id
|
|
1356
|
+
* @returns {Promise<void>}
|
|
1357
|
+
* @fires Composer#dequeue
|
|
1358
|
+
* @fires Composer#input
|
|
1359
|
+
*/
|
|
1360
|
+
async _editQueued(id) {
|
|
1361
|
+
if (this._input.value.trim() || this._files.length) {
|
|
1362
|
+
const replace = await Dialog.confirm({
|
|
1363
|
+
title: this.msg('composer.queueReplaceTitle'),
|
|
1364
|
+
message: this.msg('composer.queueReplaceMessage'),
|
|
1365
|
+
okLabel: this.msg('composer.queueReplaceConfirm'),
|
|
1366
|
+
danger: true
|
|
1367
|
+
});
|
|
1368
|
+
if (!replace || this._destroyed) return;
|
|
1369
|
+
}
|
|
1370
|
+
const { queue, item } = removeFromQueue(this._queue, id);
|
|
1371
|
+
if (!item) return;
|
|
1372
|
+
this._queue = queue;
|
|
1373
|
+
this._files = item.files.map((file) => ({ id: uid('zx-composer-file'), file }));
|
|
1374
|
+
this._renderFiles();
|
|
1375
|
+
this.setValue(item.text);
|
|
1376
|
+
this._mentions = item.mentions.map((mention) => ({ ...mention }));
|
|
1377
|
+
this._renderQueue();
|
|
1378
|
+
this._input.focus();
|
|
1379
|
+
this._emitDequeue(item, 'edited');
|
|
1380
|
+
this.emit('input', { text: this._input.value });
|
|
1381
|
+
}
|
|
1382
|
+
|
|
1383
|
+
/**
|
|
1384
|
+
* @private
|
|
1385
|
+
* @param {MouseEvent} event
|
|
1386
|
+
* @returns {void}
|
|
1387
|
+
*/
|
|
1388
|
+
_onQueueClick(event) {
|
|
1389
|
+
const button = /** @type {HTMLElement|null} */ (/** @type {Element|null} */ (event.target)?.closest?.('[data-queue-action]'));
|
|
1390
|
+
const row = /** @type {HTMLElement|null} */ (button?.closest('[data-queued]') ?? null);
|
|
1391
|
+
if (!button || !row || this._disabled) return;
|
|
1392
|
+
if (button.dataset.queueAction === 'edit') this._editQueued(String(row.dataset.queued));
|
|
1393
|
+
else this.removeQueued(String(row.dataset.queued));
|
|
1394
|
+
}
|
|
1395
|
+
|
|
1396
|
+
/**
|
|
1397
|
+
* Delete on a focused row removes it. The arrow keys inside a row belong to its toolbar.
|
|
1398
|
+
* @private
|
|
1399
|
+
* @param {KeyboardEvent} event
|
|
1400
|
+
* @returns {void}
|
|
1401
|
+
*/
|
|
1402
|
+
_onQueueKeydown(event) {
|
|
1403
|
+
if (event.key !== 'Delete' || event.shiftKey || event.altKey || event.metaKey || event.ctrlKey || this._disabled) return;
|
|
1404
|
+
const row = /** @type {HTMLElement|null} */ (/** @type {Element|null} */ (event.target)?.closest?.('[data-queued]'));
|
|
1405
|
+
if (!row) return;
|
|
1406
|
+
event.preventDefault();
|
|
1407
|
+
this.removeQueued(String(row.dataset.queued));
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
/**
|
|
1411
|
+
* Draws the queue, and keeps the focus in it: on the same button of the same row, on the row now
|
|
1412
|
+
* in that place when the row has gone, and in the text when no row is left.
|
|
1413
|
+
* @private
|
|
1414
|
+
* @returns {void}
|
|
1415
|
+
*/
|
|
1416
|
+
_renderQueue() {
|
|
1417
|
+
if (!this.options.queue) return;
|
|
1418
|
+
const list = /** @type {HTMLElement} */ (this.refs.queueList);
|
|
1419
|
+
const active = /** @type {HTMLElement|null} */ (list.ownerDocument.activeElement);
|
|
1420
|
+
const focused = active && list.contains(active) ? active : null;
|
|
1421
|
+
const focusedRow = /** @type {HTMLElement|null} */ (focused?.closest('[data-queued]') ?? null);
|
|
1422
|
+
const place = focusedRow ? Array.from(list.children).indexOf(focusedRow) : -1;
|
|
1423
|
+
|
|
1424
|
+
this._releaseQueueRoving();
|
|
1425
|
+
/** @type {HTMLElement} */ (this.refs.queue).hidden = this._queue.length === 0;
|
|
1426
|
+
this.refs.queueLabel.textContent = this.msg('composer.queue', this._queue.length);
|
|
1427
|
+
list.replaceChildren(...this._queue.map((item) => this._queueRow(item)));
|
|
1428
|
+
this._queueRoving = /** @type {HTMLElement[]} */ (Array.from(list.querySelectorAll('[role="toolbar"]')))
|
|
1429
|
+
.map((toolbar) => rovingTabindex(toolbar, '[data-queue-action]', { orientation: 'horizontal' }));
|
|
1430
|
+
|
|
1431
|
+
if (this._queueRefused && !isQueueFull(this._queue, this._queueLimit)) {
|
|
1432
|
+
this._queueRefused = false;
|
|
1433
|
+
this._syncHint();
|
|
1434
|
+
}
|
|
1435
|
+
if (!focused || !focusedRow) return;
|
|
1436
|
+
const rows = /** @type {HTMLElement[]} */ (Array.from(list.children));
|
|
1437
|
+
const row = rows.find((entry) => entry.dataset.queued === focusedRow.dataset.queued)
|
|
1438
|
+
?? rows[Math.min(place, rows.length - 1)];
|
|
1439
|
+
const next = /** @type {HTMLElement|null} */ (row?.querySelector(`[data-queue-action="${focused.dataset.queueAction}"]`) ?? null);
|
|
1440
|
+
if (next) next.focus();
|
|
1441
|
+
else this._input.focus();
|
|
1442
|
+
}
|
|
1443
|
+
|
|
1444
|
+
/**
|
|
1445
|
+
* One queued message on one line: its text, or the names of its files when it has none, and a
|
|
1446
|
+
* toolbar named by that line.
|
|
1447
|
+
* @private
|
|
1448
|
+
* @param {QueuedMessage} item
|
|
1449
|
+
* @returns {HTMLElement}
|
|
1450
|
+
*/
|
|
1451
|
+
_queueRow(item) {
|
|
1452
|
+
const textId = uid('zx-composer-queued-text');
|
|
1453
|
+
const count = item.files.length;
|
|
1454
|
+
return h('li', { class: 'zx-composer__queued', dataset: { queued: item.id } },
|
|
1455
|
+
h('span', { class: 'zx-composer__queued-text', id: textId },
|
|
1456
|
+
item.text || item.files.map((file) => file.name).join(', ')),
|
|
1457
|
+
item.text && count
|
|
1458
|
+
? h('span', { class: 'zx-composer__queued-meta' },
|
|
1459
|
+
count === 1 ? this.msg('composer.queueFile') : this.msg('composer.queueFiles', count))
|
|
1460
|
+
: null,
|
|
1461
|
+
h('div', { class: 'zx-composer__queued-actions', role: 'toolbar', 'aria-labelledby': textId },
|
|
1462
|
+
this._queueButton('edit', this.msg('composer.queueEdit')),
|
|
1463
|
+
this._queueButton('remove', this.msg('composer.queueRemove'))));
|
|
1464
|
+
}
|
|
1465
|
+
|
|
1466
|
+
/**
|
|
1467
|
+
* @private
|
|
1468
|
+
* @param {'edit'|'remove'} action
|
|
1469
|
+
* @param {string} label
|
|
1470
|
+
* @returns {HTMLElement}
|
|
1471
|
+
*/
|
|
1472
|
+
_queueButton(action, label) {
|
|
1473
|
+
return h('button', {
|
|
1474
|
+
type: 'button',
|
|
1475
|
+
class: 'zx-icon-btn zx-composer__queued-action',
|
|
1476
|
+
disabled: this._disabled,
|
|
1477
|
+
dataset: { queueAction: action }
|
|
1478
|
+
}, label);
|
|
1479
|
+
}
|
|
1480
|
+
|
|
1481
|
+
/** @private @returns {void} */
|
|
1482
|
+
_releaseQueueRoving() {
|
|
1483
|
+
for (const roving of this._queueRoving) roving.destroy();
|
|
1484
|
+
this._queueRoving = [];
|
|
1127
1485
|
}
|
|
1128
1486
|
|
|
1129
1487
|
/* -------------------------------------------------------------- commands -- */
|
|
@@ -1335,6 +1693,20 @@ function iconButton(name, label, action) {
|
|
|
1335
1693
|
}, icon(name, { size: ICON_SIZE.control })));
|
|
1336
1694
|
}
|
|
1337
1695
|
|
|
1696
|
+
/**
|
|
1697
|
+
* A copy of a queued message that shares nothing with the queue but its files.
|
|
1698
|
+
* @param {QueuedMessage} item
|
|
1699
|
+
* @returns {QueuedMessage}
|
|
1700
|
+
*/
|
|
1701
|
+
function copyQueued(item) {
|
|
1702
|
+
return {
|
|
1703
|
+
...item,
|
|
1704
|
+
files: [...item.files],
|
|
1705
|
+
mentions: item.mentions.map((mention) => ({ ...mention })),
|
|
1706
|
+
context: item.context.map((chip) => ({ ...chip }))
|
|
1707
|
+
};
|
|
1708
|
+
}
|
|
1709
|
+
|
|
1338
1710
|
/**
|
|
1339
1711
|
* The send key an option names; anything but `'mod+enter'` is `'enter'`.
|
|
1340
1712
|
* @param {unknown} value
|
|
@@ -1438,4 +1810,6 @@ function isApplePlatform() {
|
|
|
1438
1810
|
/** Fired when a suggestion is picked. @event Composer#mention @type {CustomEvent<{item: MentionItem}>} */
|
|
1439
1811
|
/** Fired when a command is chosen. @event Composer#command @type {CustomEvent<{item: LauncherItem}>} */
|
|
1440
1812
|
/** Fired when one of the extra tools is pressed; the composer changes nothing itself. @event Composer#tool @type {CustomEvent<ComposerToolDetail>} */
|
|
1813
|
+
/** Fired when a message is added to the queue. @event Composer#queue @type {CustomEvent<ComposerQueueDetail>} */
|
|
1814
|
+
/** Fired when a message leaves the queue: sent, removed, taken back to edit, or cleared. @event Composer#dequeue @type {CustomEvent<ComposerDequeueDetail>} */
|
|
1441
1815
|
/** Fired for ArrowUp on an empty text. @event Composer#historyprevious @type {CustomEvent<Record<string, never>>} */
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* The queue behind the Composer's queued messages, as pure functions: each takes the queue as it
|
|
4
|
+
* stands and answers with a new one, in the order the messages were added, and never changes what
|
|
5
|
+
* it was given. It knows nothing of messages but their `id`.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** Most messages that wait at once when `queueLimit` names no usable number. */
|
|
9
|
+
export const QUEUE_LIMIT = 10;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The limit an option names: a whole number of one or more, anything else `QUEUE_LIMIT`.
|
|
13
|
+
* @param {unknown} value
|
|
14
|
+
* @returns {number}
|
|
15
|
+
*/
|
|
16
|
+
export function queueLimit(value) {
|
|
17
|
+
const limit = Math.floor(Number(value));
|
|
18
|
+
return Number.isFinite(limit) && limit >= 1 ? limit : QUEUE_LIMIT;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Whether the queue has no room for another message.
|
|
23
|
+
* @param {readonly {id: string}[]} queue
|
|
24
|
+
* @param {unknown} [limit]
|
|
25
|
+
* @returns {boolean}
|
|
26
|
+
*/
|
|
27
|
+
export function isQueueFull(queue, limit) {
|
|
28
|
+
return queue.length >= queueLimit(limit);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Adds a message at the end, as far as the limit allows.
|
|
33
|
+
* @template {{id: string}} T
|
|
34
|
+
* @param {readonly T[]} queue
|
|
35
|
+
* @param {T} entry
|
|
36
|
+
* @param {unknown} [limit]
|
|
37
|
+
* @returns {{queue: T[], item: T|null}} The new queue, and the message added: null when the queue
|
|
38
|
+
* was full and stays as it was.
|
|
39
|
+
*/
|
|
40
|
+
export function addToQueue(queue, entry, limit) {
|
|
41
|
+
if (isQueueFull(queue, limit)) return { queue: [...queue], item: null };
|
|
42
|
+
return { queue: [...queue, entry], item: entry };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Takes one message out, wherever it stands.
|
|
47
|
+
* @template {{id: string}} T
|
|
48
|
+
* @param {readonly T[]} queue
|
|
49
|
+
* @param {string} id
|
|
50
|
+
* @returns {{queue: T[], item: T|null}} The new queue, and the message taken: null when no message
|
|
51
|
+
* has that id.
|
|
52
|
+
*/
|
|
53
|
+
export function removeFromQueue(queue, id) {
|
|
54
|
+
const item = queue.find((entry) => entry.id === String(id)) ?? null;
|
|
55
|
+
return { queue: queue.filter((entry) => entry !== item), item };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Takes the message whose turn it is.
|
|
60
|
+
* @template {{id: string}} T
|
|
61
|
+
* @param {readonly T[]} queue
|
|
62
|
+
* @returns {{queue: T[], item: T|null}} The new queue, and the first message: null when there is
|
|
63
|
+
* none.
|
|
64
|
+
*/
|
|
65
|
+
export function takeNext(queue) {
|
|
66
|
+
const [item = null, ...rest] = queue;
|
|
67
|
+
return { queue: rest, item };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Takes every message out.
|
|
72
|
+
* @template {{id: string}} T
|
|
73
|
+
* @param {readonly T[]} queue
|
|
74
|
+
* @returns {{queue: T[], items: T[]}} The empty queue, and the messages taken, in order.
|
|
75
|
+
*/
|
|
76
|
+
export function emptyQueue(queue) {
|
|
77
|
+
return { queue: [], items: [...queue] };
|
|
78
|
+
}
|
|
@@ -107,7 +107,7 @@ export class ContextMenu extends Component {
|
|
|
107
107
|
this.#context = null;
|
|
108
108
|
const restore = this.#returnFocus;
|
|
109
109
|
this.#returnFocus = null;
|
|
110
|
-
if (restore instanceof HTMLElement && restore.isConnected) restore.focus();
|
|
110
|
+
if (restore instanceof HTMLElement && restore.isConnected && this.#ownsFocus()) restore.focus();
|
|
111
111
|
this.emit('close');
|
|
112
112
|
});
|
|
113
113
|
|
|
@@ -299,9 +299,14 @@ export class ContextMenu extends Component {
|
|
|
299
299
|
return;
|
|
300
300
|
}
|
|
301
301
|
const context = this.#context;
|
|
302
|
+
const restore = this.#returnFocus;
|
|
303
|
+
const reclaim = restore instanceof HTMLElement && restore.isConnected && this.#ownsFocus();
|
|
304
|
+
// A dialog opened by a callback records the invoking control as its opener.
|
|
305
|
+
if (reclaim) restore.focus();
|
|
302
306
|
const selected = this.emit('select', { value: item.value, item, context });
|
|
303
307
|
if (selected.defaultPrevented) {
|
|
304
308
|
event?.preventDefault();
|
|
309
|
+
if (reclaim && document.activeElement === restore && element instanceof HTMLElement) element.focus();
|
|
305
310
|
return;
|
|
306
311
|
}
|
|
307
312
|
try {
|
|
@@ -315,6 +320,12 @@ export class ContextMenu extends Component {
|
|
|
315
320
|
#menuItems() {
|
|
316
321
|
return Array.from(this.#panel.querySelectorAll(MENU_ITEM_SELECTOR));
|
|
317
322
|
}
|
|
323
|
+
|
|
324
|
+
/** @returns {boolean} Whether focus still belongs to this menu to restore. */
|
|
325
|
+
#ownsFocus() {
|
|
326
|
+
const active = document.activeElement;
|
|
327
|
+
return !active || active === document.body || this.#panel.contains(active);
|
|
328
|
+
}
|
|
318
329
|
}
|
|
319
330
|
|
|
320
331
|
/**
|