@joniguerini/lume 0.0.0-stage → 1.9.1
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/LICENSE +25 -0
- package/README.md +62 -2
- package/base.css +165 -0
- package/bin/lumeui.mjs +163 -0
- package/components/alert.css +156 -0
- package/components/banner.css +98 -0
- package/components/button.css +291 -0
- package/components/calendar.css +532 -0
- package/components/carousel.css +175 -0
- package/components/chart.css +813 -0
- package/components/chat.css +670 -0
- package/components/checkbox.css +212 -0
- package/components/chip.css +224 -0
- package/components/code.css +36 -0
- package/components/color-picker.css +192 -0
- package/components/command.css +148 -0
- package/components/content.css +211 -0
- package/components/editor.css +174 -0
- package/components/feedback.css +320 -0
- package/components/filters.css +103 -0
- package/components/form.css +455 -0
- package/components/image-viewer.css +237 -0
- package/components/layers.css +138 -0
- package/components/link.css +87 -0
- package/components/loading.css +260 -0
- package/components/menu.css +180 -0
- package/components/modal.css +209 -0
- package/components/navigation.css +507 -0
- package/components/notifications.css +186 -0
- package/components/number-field.css +52 -0
- package/components/otp.css +119 -0
- package/components/panel.css +208 -0
- package/components/picker.css +207 -0
- package/components/rating.css +143 -0
- package/components/resizable.css +173 -0
- package/components/scrollbar.css +110 -0
- package/components/select.css +477 -0
- package/components/shell.css +837 -0
- package/components/slider.css +189 -0
- package/components/sortable.css +287 -0
- package/components/surfaces.css +182 -0
- package/components/switch.css +115 -0
- package/components/table.css +228 -0
- package/components/tabs.css +190 -0
- package/components/tags.css +83 -0
- package/components/toast.css +164 -0
- package/components/toolbar.css +80 -0
- package/components/upload.css +181 -0
- package/ds.css +48 -0
- package/fonts/Geist-LICENSE.txt +92 -0
- package/fonts/Geist-Variable.woff2 +0 -0
- package/fonts/GeistSerif-Regular.woff2 +0 -0
- package/fonts/JetBrainsMono-LICENSE.txt +93 -0
- package/fonts/JetBrainsMono-Variable.woff2 +0 -0
- package/fonts.css +29 -0
- package/icons/icons.d.ts +5 -0
- package/icons/icons.js +266 -0
- package/js/back-to-top.d.ts +2 -0
- package/js/back-to-top.js +62 -0
- package/js/banner.js +87 -0
- package/js/calendar.d.ts +3 -0
- package/js/calendar.js +472 -0
- package/js/carousel.d.ts +2 -0
- package/js/carousel.js +258 -0
- package/js/charts/bar.js +361 -0
- package/js/charts/core.js +604 -0
- package/js/charts/donut.js +206 -0
- package/js/charts/figures.js +97 -0
- package/js/charts/heatmap.js +410 -0
- package/js/charts/line.js +268 -0
- package/js/charts/scatter.js +211 -0
- package/js/charts.d.ts +12 -0
- package/js/charts.js +27 -0
- package/js/code-block.d.ts +3 -0
- package/js/code-block.js +32 -0
- package/js/color-picker.d.ts +4 -0
- package/js/color-picker.js +448 -0
- package/js/command-palette.js +233 -0
- package/js/composer.js +573 -0
- package/js/context-menu.d.ts +2 -0
- package/js/context-menu.js +154 -0
- package/js/date-picker.d.ts +2 -0
- package/js/date-picker.js +430 -0
- package/js/drawer.js +48 -0
- package/js/editor.d.ts +5 -0
- package/js/editor.js +615 -0
- package/js/field-support.js +77 -0
- package/js/file-upload.d.ts +2 -0
- package/js/file-upload.js +230 -0
- package/js/filters.js +266 -0
- package/js/form.d.ts +2 -0
- package/js/form.js +409 -0
- package/js/i18n.d.ts +14 -0
- package/js/i18n.js +83 -0
- package/js/image-viewer.d.ts +4 -0
- package/js/image-viewer.js +275 -0
- package/js/menu.d.ts +4 -0
- package/js/menu.js +237 -0
- package/js/messages.js +908 -0
- package/js/modal.d.ts +7 -0
- package/js/modal.js +183 -0
- package/js/notifications.d.ts +4 -0
- package/js/notifications.js +204 -0
- package/js/number-field.d.ts +2 -0
- package/js/number-field.js +175 -0
- package/js/otp-field.d.ts +2 -0
- package/js/otp-field.js +107 -0
- package/js/pagination.d.ts +3 -0
- package/js/pagination.js +143 -0
- package/js/popover.d.ts +2 -0
- package/js/popover.js +155 -0
- package/js/progress-ring.js +48 -0
- package/js/rating.d.ts +3 -0
- package/js/rating.js +130 -0
- package/js/resizable.d.ts +2 -0
- package/js/resizable.js +311 -0
- package/js/scrollbar.d.ts +2 -0
- package/js/scrollbar.js +273 -0
- package/js/segmented.js +60 -0
- package/js/select.d.ts +3 -0
- package/js/select.js +784 -0
- package/js/shell.d.ts +6 -0
- package/js/shell.js +373 -0
- package/js/slider.d.ts +2 -0
- package/js/slider.js +89 -0
- package/js/sortable.d.ts +5 -0
- package/js/sortable.js +463 -0
- package/js/stepper.d.ts +2 -0
- package/js/stepper.js +72 -0
- package/js/tabs.js +99 -0
- package/js/tag-field.d.ts +2 -0
- package/js/tag-field.js +177 -0
- package/js/theme.d.ts +9 -0
- package/js/theme.js +114 -0
- package/js/time-picker.d.ts +2 -0
- package/js/time-picker.js +279 -0
- package/js/toast.d.ts +17 -0
- package/js/toast.js +171 -0
- package/js/toolbar.d.ts +2 -0
- package/js/toolbar.js +107 -0
- package/js/tooltip.d.ts +2 -0
- package/js/tooltip.js +208 -0
- package/js/tree.d.ts +2 -0
- package/js/tree.js +168 -0
- package/lume.css +11574 -0
- package/lume.d.ts +104 -0
- package/lume.js +67 -0
- package/package.json +66 -4
- package/react/Alert.d.ts +24 -0
- package/react/Alert.js +21 -0
- package/react/AppShell.d.ts +67 -0
- package/react/AppShell.js +94 -0
- package/react/Avatar.d.ts +39 -0
- package/react/Avatar.js +40 -0
- package/react/Badge.d.ts +17 -0
- package/react/Badge.js +10 -0
- package/react/Button.d.ts +32 -0
- package/react/Button.js +17 -0
- package/react/Calendar.d.ts +48 -0
- package/react/Calendar.js +67 -0
- package/react/Carousel.d.ts +30 -0
- package/react/Carousel.js +38 -0
- package/react/Charts.d.ts +186 -0
- package/react/Charts.js +58 -0
- package/react/Chip.d.ts +34 -0
- package/react/Chip.js +26 -0
- package/react/Choice.d.ts +72 -0
- package/react/Choice.js +51 -0
- package/react/ColorPicker.d.ts +43 -0
- package/react/ColorPicker.js +73 -0
- package/react/Content.d.ts +58 -0
- package/react/Content.js +42 -0
- package/react/Controls.d.ts +97 -0
- package/react/Controls.js +118 -0
- package/react/Editor.d.ts +28 -0
- package/react/Editor.js +57 -0
- package/react/Field.d.ts +90 -0
- package/react/Field.js +65 -0
- package/react/Form.d.ts +36 -0
- package/react/Form.js +45 -0
- package/react/Icon.d.ts +13 -0
- package/react/Icon.js +8 -0
- package/react/IconButton.d.ts +19 -0
- package/react/IconButton.js +12 -0
- package/react/ImageViewer.d.ts +20 -0
- package/react/ImageViewer.js +22 -0
- package/react/InlineAction.d.ts +15 -0
- package/react/InlineAction.js +12 -0
- package/react/Layers.d.ts +118 -0
- package/react/Layers.js +115 -0
- package/react/Link.d.ts +32 -0
- package/react/Link.js +26 -0
- package/react/Loading.d.ts +67 -0
- package/react/Loading.js +49 -0
- package/react/Navigation.d.ts +181 -0
- package/react/Navigation.js +197 -0
- package/react/Notices.d.ts +43 -0
- package/react/Notices.js +89 -0
- package/react/Panel.d.ts +57 -0
- package/react/Panel.js +33 -0
- package/react/Pickers.d.ts +83 -0
- package/react/Pickers.js +70 -0
- package/react/Rating.d.ts +39 -0
- package/react/Rating.js +41 -0
- package/react/Resizable.d.ts +44 -0
- package/react/Resizable.js +30 -0
- package/react/Select.d.ts +53 -0
- package/react/Select.js +79 -0
- package/react/Sortable.d.ts +52 -0
- package/react/Sortable.js +113 -0
- package/react/Table.d.ts +60 -0
- package/react/Table.js +34 -0
- package/react/Toolbar.d.ts +45 -0
- package/react/Toolbar.js +45 -0
- package/react/cx.d.ts +2 -0
- package/react/cx.js +5 -0
- package/react/icon-names.d.ts +1 -0
- package/react/icon-names.js +2 -0
- package/react/index.d.ts +42 -0
- package/react/index.js +52 -0
- package/react/useController.d.ts +8 -0
- package/react/useController.js +16 -0
- package/react/useLocale.d.ts +5 -0
- package/react/useLocale.js +15 -0
- package/tokens.css +739 -0
- package/tokens.js +4911 -0
package/js/composer.js
ADDED
|
@@ -0,0 +1,573 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lume — composer (caixa de mensagem) e ajudantes de conversa.
|
|
3
|
+
*
|
|
4
|
+
* const composer = createComposer(document.querySelector('#composer'), {
|
|
5
|
+
* label: 'Mensagem para o Lume',
|
|
6
|
+
* placeholder: 'Pergunte algo ou digite / para comandos',
|
|
7
|
+
* commands: [{ id: 'resumir', label: 'Resumir', description: 'Resume em três frases', icon: 'file-text' }],
|
|
8
|
+
* maxLength: 2000,
|
|
9
|
+
* onSend({ text, attachments, command }) { … }, // devolva false para manter o texto no campo
|
|
10
|
+
* onStop() { … }, // botão parar ou Esc, enquanto ocupado
|
|
11
|
+
* });
|
|
12
|
+
* composer.value · composer.attachments · composer.command · composer.setValue('…')
|
|
13
|
+
* composer.setBusy(true) · composer.focus() · composer.clear() · composer.destroy()
|
|
14
|
+
*
|
|
15
|
+
* Teclado: Enter envia · Shift + Enter quebra linha · "/" no começo abre os comandos
|
|
16
|
+
* (setas, Enter ou Tab escolhem, Esc fecha) · Backspace no começo remove o comando · Esc para a resposta.
|
|
17
|
+
*
|
|
18
|
+
* Também exporta markup estático para documentar estados e montar conversas:
|
|
19
|
+
* composerHtml(opts), messageHtml(opts), attachmentHtml(file), typingHtml(label), streamText(el, paragraphs, opts)
|
|
20
|
+
*/
|
|
21
|
+
import { icon } from '../icons/icons.js';
|
|
22
|
+
import { msg, numberFormat } from './i18n.js';
|
|
23
|
+
|
|
24
|
+
let uid = 0;
|
|
25
|
+
|
|
26
|
+
const esc = (s) =>
|
|
27
|
+
String(s ?? '')
|
|
28
|
+
.replace(/&/g, '&')
|
|
29
|
+
.replace(/</g, '<')
|
|
30
|
+
.replace(/>/g, '>')
|
|
31
|
+
.replace(/"/g, '"');
|
|
32
|
+
|
|
33
|
+
const normalize = (s) =>
|
|
34
|
+
String(s ?? '')
|
|
35
|
+
.normalize('NFD')
|
|
36
|
+
.replace(/[\u0300-\u036f]/g, '')
|
|
37
|
+
.toLowerCase();
|
|
38
|
+
|
|
39
|
+
const fmt = (n) => numberFormat().format(n);
|
|
40
|
+
const reducedMotion = () => matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
41
|
+
|
|
42
|
+
export function formatBytes(n) {
|
|
43
|
+
if (n == null) return '';
|
|
44
|
+
if (n < 1024) return `${n} B`;
|
|
45
|
+
if (n < 1024 * 1024) return `${Math.max(1, Math.round(n / 1024))} KB`;
|
|
46
|
+
return `${(n / 1048576).toFixed(1).replace('.', ',')} MB`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/* ───────── Markup estático ───────── */
|
|
50
|
+
|
|
51
|
+
/** Anexo: { name, size?, type?, id?, removable? } */
|
|
52
|
+
export function attachmentHtml({ name, size, type, id, removable = false }) {
|
|
53
|
+
const ic = /^image\//.test(type ?? '') || /\.(png|jpe?g|gif|webp|svg)$/i.test(name) ? 'image' : 'file-text';
|
|
54
|
+
return `<span class="ds-attachment"${id ? ` data-attachment="${esc(id)}"` : ''}>
|
|
55
|
+
<span class="ds-attachment__icon" aria-hidden="true">${icon(ic, { size: 14 })}</span>
|
|
56
|
+
<span class="ds-attachment__name">${esc(name)}</span>
|
|
57
|
+
${size != null ? `<span class="ds-attachment__size">${formatBytes(size)}</span>` : ''}
|
|
58
|
+
${removable ? `<button type="button" class="ds-attachment__remove" data-remove="${esc(id)}" aria-label="${esc(msg('common.remove', name))}">${icon('x', { size: 14 })}</button>` : ''}
|
|
59
|
+
</span>`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function commandChip(command) {
|
|
63
|
+
return `<span class="ds-composer__command">${icon(command.icon ?? 'slash', { size: 14 })}<span>${esc(command.label)}</span>
|
|
64
|
+
<button type="button" class="ds-attachment__remove" data-remove="command" aria-label="${esc(msg('composer.removeCommand', command.label))}">${icon('x', { size: 14 })}</button></span>`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const sendIcon = (busy) => (busy ? '<span class="ds-composer__stop" aria-hidden="true"></span>' : icon('arrow-up', { size: 18, strokeWidth: 2 }));
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Markup do composer (createComposer usa o mesmo). Para documentar estados, passe:
|
|
71
|
+
* value, attachments, command, busy, disabled, maxLength, state ('focus').
|
|
72
|
+
*/
|
|
73
|
+
export function composerHtml({
|
|
74
|
+
id = `ds-composer-${++uid}`,
|
|
75
|
+
label = msg('composer.label'),
|
|
76
|
+
placeholder = msg('composer.placeholder'),
|
|
77
|
+
value = '',
|
|
78
|
+
attachments = [],
|
|
79
|
+
command = null,
|
|
80
|
+
busy = false,
|
|
81
|
+
disabled = false,
|
|
82
|
+
maxLength = 0,
|
|
83
|
+
state,
|
|
84
|
+
hint = true,
|
|
85
|
+
attach = true,
|
|
86
|
+
commands = true,
|
|
87
|
+
mic = false,
|
|
88
|
+
rows = 1,
|
|
89
|
+
} = {}) {
|
|
90
|
+
const len = value.length;
|
|
91
|
+
const showCount = maxLength && len >= maxLength * 0.8;
|
|
92
|
+
const over = maxLength && len > maxLength;
|
|
93
|
+
const tokens = `${command ? commandChip(command) : ''}${attachments.map((a, i) => attachmentHtml({ ...a, id: a.id ?? `${id}-a${i}`, removable: true })).join('')}`;
|
|
94
|
+
const canSend = (len || attachments.length) && !over && !disabled;
|
|
95
|
+
const flags = [busy && 'data-busy', disabled && 'data-disabled', over && 'data-over', state && `data-state="${esc(state)}"`].filter(Boolean).join(' ');
|
|
96
|
+
return `<div class="ds-composer" ${flags}>
|
|
97
|
+
<div class="ds-composer__tokens" data-tokens${tokens ? '' : ' hidden'}>${tokens}</div>
|
|
98
|
+
<label class="ds-visually-hidden" for="${id}-input">${esc(label)}</label>
|
|
99
|
+
<textarea class="ds-composer__input" id="${id}-input" rows="${rows}" placeholder="${esc(placeholder)}" aria-autocomplete="list" aria-controls="${id}-menu"${hint ? ` aria-describedby="${id}-hint"` : ''}${disabled ? ' disabled' : ''}>${esc(value)}</textarea>
|
|
100
|
+
<div class="ds-composer__bar">
|
|
101
|
+
${
|
|
102
|
+
attach
|
|
103
|
+
? `<button type="button" class="ds-icon-btn ds-icon-btn--muted" data-attach aria-label="${msg('composer.attach')}" data-ds-tooltip="${msg('composer.attach')}">${icon('paperclip', { size: 18 })}</button>
|
|
104
|
+
<input type="file" multiple hidden data-file tabindex="-1">`
|
|
105
|
+
: ''
|
|
106
|
+
}
|
|
107
|
+
${commands ? `<button type="button" class="ds-icon-btn ds-icon-btn--muted" data-commands aria-label="${msg('composer.commands')}" data-ds-tooltip="${msg('composer.commands')}" data-ds-tooltip-kbd="/">${icon('slash', { size: 18 })}</button>` : ''}
|
|
108
|
+
${mic ? `<button type="button" class="ds-icon-btn ds-icon-btn--muted" data-mic aria-label="${msg('composer.dictate')}" data-ds-tooltip="${msg('composer.dictate')}">${icon('mic', { size: 18 })}</button>` : ''}
|
|
109
|
+
<span class="ds-composer__spacer"></span>
|
|
110
|
+
<span class="ds-composer__count" data-count aria-hidden="true"${showCount ? '' : ' hidden'}>${maxLength ? `${fmt(len)} / ${fmt(maxLength)}` : ''}</span>
|
|
111
|
+
<button type="button" class="ds-composer__send" data-send aria-label="${msg(busy ? 'composer.stop' : 'composer.send')}" data-ds-tooltip="${msg(busy ? 'composer.stop' : 'composer.sendShort')}" data-ds-tooltip-kbd="${busy ? 'Esc' : '↵'}"${!busy && !canSend ? ' aria-disabled="true"' : ''}>${sendIcon(busy)}</button>
|
|
112
|
+
</div>
|
|
113
|
+
<div class="ds-composer__menu" id="${id}-menu" role="listbox" aria-label="${msg('composer.commands')}" hidden></div>
|
|
114
|
+
</div>
|
|
115
|
+
${
|
|
116
|
+
hint
|
|
117
|
+
? `<p class="ds-composer__hint" id="${id}-hint">
|
|
118
|
+
<span><kbd class="ds-kbd">Enter</kbd> ${msg('composer.hintSend')}</span>
|
|
119
|
+
<span><kbd class="ds-kbd">Shift</kbd> + <kbd class="ds-kbd">Enter</kbd> ${msg('composer.hintNewline')}</span>
|
|
120
|
+
${commands ? `<span><kbd class="ds-kbd">/</kbd> ${msg('composer.hintCommands')}</span>` : ''}
|
|
121
|
+
</p>`
|
|
122
|
+
: ''
|
|
123
|
+
}`;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Indicador "escrevendo" (pontos param com movimento reduzido). */
|
|
127
|
+
export function typingHtml(label = msg('composer.typing', 'Lume')) {
|
|
128
|
+
return `<div class="ds-typing"><span class="ds-typing__dots" aria-hidden="true"><i></i><i></i><i></i></span><span>${esc(label)}</span></div>`;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const MESSAGE_ACTIONS = [
|
|
132
|
+
['copy', 'copy', 'composer.copy'],
|
|
133
|
+
['retry', 'refresh', 'composer.retry'],
|
|
134
|
+
['like', 'thumbs-up', 'composer.like'],
|
|
135
|
+
['dislike', 'thumbs-down', 'composer.dislike'],
|
|
136
|
+
];
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Mensagem.
|
|
140
|
+
* { role: 'user' | 'assistant', id?, author = 'Lume', time?, html? | text?, attachments?, card? (HTML),
|
|
141
|
+
* actions = true (só na resposta), status?: 'error', meta? (texto extra), busy? }
|
|
142
|
+
*/
|
|
143
|
+
export function messageHtml({ role = 'assistant', id, author = 'Lume', time, html, text, attachments = [], card = '', actions = true, status, meta, busy = false, latest = false } = {}) {
|
|
144
|
+
const body = html ?? (text ? String(text).split(/\n{2,}/).map((p) => `<p>${esc(p)}</p>`).join('') : '');
|
|
145
|
+
const attrs = [`class="ds-message${role === 'user' ? ' ds-message--user' : ''}"`, id && `data-message="${esc(id)}"`, status && `data-status="${esc(status)}"`, busy && 'aria-busy="true"', latest && 'data-latest'].filter(Boolean).join(' ');
|
|
146
|
+
const files = attachments.length ? `<div class="ds-message__attachments">${attachments.map((a) => attachmentHtml(a)).join('')}</div>` : '';
|
|
147
|
+
if (role === 'user') {
|
|
148
|
+
const footer =
|
|
149
|
+
status === 'error'
|
|
150
|
+
? `<p class="ds-message__error"><span>${icon('alert-circle', { size: 16 })}${msg('composer.notSent')}</span><button type="button" class="ds-inline-action" data-action="resend">${icon('refresh', { size: 16 })}${msg('composer.tryAgain')}</button></p>`
|
|
151
|
+
: time || meta
|
|
152
|
+
? `<p class="ds-message__meta">${esc(meta ?? msg('composer.sent'))}${time ? ` · <time>${esc(time)}</time>` : ''}</p>`
|
|
153
|
+
: '';
|
|
154
|
+
return `<article ${attrs} aria-label="${msg('composer.youSaid')}">${files}${body ? `<div class="ds-message__body">${body}</div>` : ''}${footer}</article>`;
|
|
155
|
+
}
|
|
156
|
+
return `<article ${attrs} aria-label="${esc(msg('composer.replied', author))}">
|
|
157
|
+
<header class="ds-message__head"><span class="ds-message__mark" aria-hidden="true">L</span><span class="ds-message__author">${esc(author)}</span>${time ? `<time>${esc(time)}</time>` : ''}</header>
|
|
158
|
+
${files}
|
|
159
|
+
<div class="ds-message__body" data-body>${body}</div>
|
|
160
|
+
${card}
|
|
161
|
+
${meta ? `<p class="ds-message__meta">${esc(meta)}</p>` : ''}
|
|
162
|
+
${
|
|
163
|
+
actions
|
|
164
|
+
? `<div class="ds-message__actions" role="group" aria-label="${msg('composer.replyActions')}">${MESSAGE_ACTIONS.map(
|
|
165
|
+
([action, ic, key]) =>
|
|
166
|
+
`<button type="button" class="ds-icon-btn ds-icon-btn--sm ds-icon-btn--muted" data-action="${action}" aria-label="${msg(key)}" data-ds-tooltip="${msg(key)}"${action === 'like' || action === 'dislike' ? ' aria-pressed="false"' : ''}>${icon(ic, { size: 16 })}</button>`,
|
|
167
|
+
).join('')}</div>`
|
|
168
|
+
: ''
|
|
169
|
+
}
|
|
170
|
+
</article>`;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Escreve parágrafos aos poucos em `el` (palavra a palavra). Com movimento reduzido, escreve de uma vez.
|
|
175
|
+
* Devolve uma Promise com { stopped }. Pare com um AbortSignal (opts.signal).
|
|
176
|
+
*/
|
|
177
|
+
export function streamText(el, paragraphs, { delay = 32, signal } = {}) {
|
|
178
|
+
const list = [].concat(paragraphs);
|
|
179
|
+
if (reducedMotion() || delay <= 0) {
|
|
180
|
+
el.innerHTML = list.map((p) => `<p>${esc(p)}</p>`).join('');
|
|
181
|
+
return Promise.resolve({ stopped: false });
|
|
182
|
+
}
|
|
183
|
+
return new Promise((resolve) => {
|
|
184
|
+
let pi = 0;
|
|
185
|
+
let words = [];
|
|
186
|
+
let wi = 0;
|
|
187
|
+
let p = null;
|
|
188
|
+
let timer = 0;
|
|
189
|
+
const stop = () => {
|
|
190
|
+
clearTimeout(timer);
|
|
191
|
+
resolve({ stopped: true });
|
|
192
|
+
};
|
|
193
|
+
signal?.addEventListener('abort', stop, { once: true });
|
|
194
|
+
const tick = () => {
|
|
195
|
+
if (signal?.aborted) return;
|
|
196
|
+
if (!p) {
|
|
197
|
+
if (pi >= list.length) {
|
|
198
|
+
signal?.removeEventListener('abort', stop);
|
|
199
|
+
resolve({ stopped: false });
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
p = document.createElement('p');
|
|
203
|
+
el.append(p);
|
|
204
|
+
words = String(list[pi]).split(/(\s+)/);
|
|
205
|
+
wi = 0;
|
|
206
|
+
}
|
|
207
|
+
// duas "palavras" por passo (texto + espaço)
|
|
208
|
+
p.textContent += words.slice(wi, wi + 2).join('');
|
|
209
|
+
wi += 2;
|
|
210
|
+
if (wi >= words.length) {
|
|
211
|
+
p = null;
|
|
212
|
+
pi += 1;
|
|
213
|
+
}
|
|
214
|
+
timer = setTimeout(tick, delay);
|
|
215
|
+
};
|
|
216
|
+
tick();
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/* ───────── Composer vivo ───────── */
|
|
221
|
+
|
|
222
|
+
export function createComposer(host, options = {}) {
|
|
223
|
+
const o = {
|
|
224
|
+
label: msg('composer.label'),
|
|
225
|
+
placeholder: msg('composer.placeholder'),
|
|
226
|
+
commands: [],
|
|
227
|
+
maxLength: 4000,
|
|
228
|
+
attach: true,
|
|
229
|
+
mic: false,
|
|
230
|
+
hint: true,
|
|
231
|
+
...options,
|
|
232
|
+
};
|
|
233
|
+
const id = `ds-composer-${++uid}`;
|
|
234
|
+
host.innerHTML = `${composerHtml({ ...o, id, commands: o.commands.length > 0 })}<p class="ds-visually-hidden" aria-live="polite" data-live></p>`;
|
|
235
|
+
|
|
236
|
+
const root = host.querySelector('.ds-composer');
|
|
237
|
+
const input = root.querySelector('.ds-composer__input');
|
|
238
|
+
const tokensEl = root.querySelector('[data-tokens]');
|
|
239
|
+
const sendBtn = root.querySelector('[data-send]');
|
|
240
|
+
const countEl = root.querySelector('[data-count]');
|
|
241
|
+
const menu = root.querySelector('.ds-composer__menu');
|
|
242
|
+
const fileInput = root.querySelector('[data-file]');
|
|
243
|
+
const live = host.querySelector('[data-live]');
|
|
244
|
+
|
|
245
|
+
let attachments = [];
|
|
246
|
+
let command = null;
|
|
247
|
+
let busy = false;
|
|
248
|
+
let options$ = [];
|
|
249
|
+
let active = -1;
|
|
250
|
+
let fileSeq = 0;
|
|
251
|
+
|
|
252
|
+
const announce = (text) => {
|
|
253
|
+
live.textContent = '';
|
|
254
|
+
requestAnimationFrame(() => (live.textContent = text));
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
function autosize() {
|
|
258
|
+
input.style.height = '';
|
|
259
|
+
if (!input.value || !input.clientWidth) return; // vazio: uma linha (rows="1")
|
|
260
|
+
input.style.height = 'auto';
|
|
261
|
+
input.style.height = `${input.scrollHeight}px`;
|
|
262
|
+
}
|
|
263
|
+
// a largura muda (painel, rotação): a altura acompanha
|
|
264
|
+
let lastWidth = 0;
|
|
265
|
+
const ro = new ResizeObserver(([entry]) => {
|
|
266
|
+
const w = Math.round(entry.contentRect.width);
|
|
267
|
+
if (w && w !== lastWidth) {
|
|
268
|
+
lastWidth = w;
|
|
269
|
+
autosize();
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
ro.observe(input);
|
|
273
|
+
|
|
274
|
+
const canSend = () => (input.value.trim().length > 0 || attachments.length > 0) && !(o.maxLength && input.value.length > o.maxLength);
|
|
275
|
+
|
|
276
|
+
function sync() {
|
|
277
|
+
const len = input.value.length;
|
|
278
|
+
const over = o.maxLength && len > o.maxLength;
|
|
279
|
+
root.toggleAttribute('data-over', !!over);
|
|
280
|
+
if (o.maxLength) {
|
|
281
|
+
countEl.hidden = len < o.maxLength * 0.8;
|
|
282
|
+
countEl.textContent = `${fmt(len)} / ${fmt(o.maxLength)}`;
|
|
283
|
+
}
|
|
284
|
+
if (busy || canSend()) sendBtn.removeAttribute('aria-disabled');
|
|
285
|
+
else sendBtn.setAttribute('aria-disabled', 'true');
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function renderTokens() {
|
|
289
|
+
tokensEl.innerHTML = `${command ? commandChip(command) : ''}${attachments.map((a) => attachmentHtml({ ...a, removable: true })).join('')}`;
|
|
290
|
+
tokensEl.hidden = !command && !attachments.length;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/* comandos */
|
|
294
|
+
function openMenu(query) {
|
|
295
|
+
const q = normalize(query);
|
|
296
|
+
// o nome do comando vem antes da descrição: /tr acha /traduzir, não "três"
|
|
297
|
+
const score = (c) => {
|
|
298
|
+
if (!q) return 1;
|
|
299
|
+
const idn = normalize(c.id);
|
|
300
|
+
const label = normalize(c.label);
|
|
301
|
+
if (idn.startsWith(q)) return 4;
|
|
302
|
+
if (label.startsWith(q) || label.split(/\s+/).some((w) => w.startsWith(q))) return 3;
|
|
303
|
+
if (idn.includes(q) || label.includes(q)) return 2;
|
|
304
|
+
return normalize(c.description).includes(q) ? 1 : 0;
|
|
305
|
+
};
|
|
306
|
+
options$ = o.commands
|
|
307
|
+
.map((c, i) => ({ c, i, s: score(c) }))
|
|
308
|
+
.filter((r) => r.s > 0)
|
|
309
|
+
.sort((a, b) => b.s - a.s || a.i - b.i)
|
|
310
|
+
.map((r) => r.c);
|
|
311
|
+
active = options$.length ? 0 : -1;
|
|
312
|
+
menu.innerHTML =
|
|
313
|
+
`<p class="ds-composer__menu-label" aria-hidden="true">Comandos</p>` +
|
|
314
|
+
(options$.length
|
|
315
|
+
? options$
|
|
316
|
+
.map(
|
|
317
|
+
(c, i) => `<div class="ds-composer__option" role="option" id="${id}-opt-${esc(c.id)}" data-index="${i}" aria-selected="${i === active}">
|
|
318
|
+
${icon(c.icon ?? 'slash', { size: 18 })}<span class="ds-composer__option-label">/${esc(c.id)}</span><span class="ds-composer__option-desc">${esc(c.description ?? c.label)}</span>
|
|
319
|
+
</div>`,
|
|
320
|
+
)
|
|
321
|
+
.join('')
|
|
322
|
+
: `<p class="ds-composer__empty">${esc(msg('composer.noCommandFor', query))}</p>`);
|
|
323
|
+
const wasOpen = !menu.hidden;
|
|
324
|
+
menu.hidden = false;
|
|
325
|
+
setActive(active);
|
|
326
|
+
if (!wasOpen) announce(options$.length ? msg('composer.commandCount', options$.length) : msg('composer.noCommands'));
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function closeMenu() {
|
|
330
|
+
if (menu.hidden) return;
|
|
331
|
+
menu.hidden = true;
|
|
332
|
+
input.removeAttribute('aria-activedescendant');
|
|
333
|
+
active = -1;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function setActive(i) {
|
|
337
|
+
active = i;
|
|
338
|
+
menu.querySelectorAll('[role="option"]').forEach((el, k) => el.setAttribute('aria-selected', String(k === i)));
|
|
339
|
+
const el = menu.querySelector(`[data-index="${i}"]`);
|
|
340
|
+
if (el) {
|
|
341
|
+
input.setAttribute('aria-activedescendant', el.id);
|
|
342
|
+
el.scrollIntoView({ block: 'nearest' });
|
|
343
|
+
} else input.removeAttribute('aria-activedescendant');
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function choose(c) {
|
|
347
|
+
if (!c) return;
|
|
348
|
+
command = c;
|
|
349
|
+
input.value = input.value.replace(/^\/\S*\s?/, '');
|
|
350
|
+
closeMenu();
|
|
351
|
+
renderTokens();
|
|
352
|
+
autosize();
|
|
353
|
+
sync();
|
|
354
|
+
input.focus();
|
|
355
|
+
announce(msg('composer.commandChosen', c.label));
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function checkSlash() {
|
|
359
|
+
if (!o.commands.length || command) return closeMenu();
|
|
360
|
+
const before = input.value.slice(0, input.selectionStart ?? 0);
|
|
361
|
+
const m = /^\/(\S*)$/.exec(before);
|
|
362
|
+
if (m) openMenu(m[1]);
|
|
363
|
+
else closeMenu();
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/* anexos */
|
|
367
|
+
function addFiles(files) {
|
|
368
|
+
const list = [...(files ?? [])];
|
|
369
|
+
if (!list.length) return;
|
|
370
|
+
for (const f of list) attachments.push({ id: `${id}-f${++fileSeq}`, name: f.name, size: f.size, type: f.type });
|
|
371
|
+
renderTokens();
|
|
372
|
+
sync();
|
|
373
|
+
announce(list.length === 1 ? msg('composer.attachedOne', list[0].name) : msg('composer.attachedMany', list.length));
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/* enviar e parar */
|
|
377
|
+
function send() {
|
|
378
|
+
if (busy) return stop();
|
|
379
|
+
if (!canSend()) {
|
|
380
|
+
if (o.maxLength && input.value.length > o.maxLength) announce(msg('composer.tooLong', o.maxLength));
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
const payload = { text: input.value.trim(), attachments: attachments.map(({ id: _, ...a }) => a), command: command?.id ?? null };
|
|
384
|
+
const result = o.onSend?.(payload);
|
|
385
|
+
if (result !== false) clear();
|
|
386
|
+
input.focus();
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function stop() {
|
|
390
|
+
if (busy) o.onStop?.();
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function clear() {
|
|
394
|
+
input.value = '';
|
|
395
|
+
attachments = [];
|
|
396
|
+
command = null;
|
|
397
|
+
renderTokens();
|
|
398
|
+
closeMenu();
|
|
399
|
+
autosize();
|
|
400
|
+
sync();
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/* eventos */
|
|
404
|
+
const onInput = () => {
|
|
405
|
+
autosize();
|
|
406
|
+
sync();
|
|
407
|
+
checkSlash();
|
|
408
|
+
};
|
|
409
|
+
|
|
410
|
+
const onKeyDown = (event) => {
|
|
411
|
+
if (!menu.hidden) {
|
|
412
|
+
const n = options$.length;
|
|
413
|
+
if (event.key === 'ArrowDown' && n) {
|
|
414
|
+
event.preventDefault();
|
|
415
|
+
return setActive((active + 1) % n);
|
|
416
|
+
}
|
|
417
|
+
if (event.key === 'ArrowUp' && n) {
|
|
418
|
+
event.preventDefault();
|
|
419
|
+
return setActive((active - 1 + n) % n);
|
|
420
|
+
}
|
|
421
|
+
if ((event.key === 'Enter' || event.key === 'Tab') && n && active >= 0 && !event.isComposing) {
|
|
422
|
+
event.preventDefault();
|
|
423
|
+
return choose(options$[active]);
|
|
424
|
+
}
|
|
425
|
+
if (event.key === 'Escape') {
|
|
426
|
+
event.preventDefault();
|
|
427
|
+
event.stopPropagation();
|
|
428
|
+
return closeMenu();
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
if (event.key === 'Enter' && !event.shiftKey && !event.isComposing && event.keyCode !== 229) {
|
|
432
|
+
event.preventDefault();
|
|
433
|
+
if (!busy) send();
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
if (event.key === 'Escape' && busy) {
|
|
437
|
+
event.preventDefault();
|
|
438
|
+
event.stopPropagation();
|
|
439
|
+
return stop();
|
|
440
|
+
}
|
|
441
|
+
if (event.key === 'Backspace' && command && input.selectionStart === 0 && input.selectionEnd === 0) {
|
|
442
|
+
event.preventDefault();
|
|
443
|
+
const removed = command;
|
|
444
|
+
command = null;
|
|
445
|
+
renderTokens();
|
|
446
|
+
sync();
|
|
447
|
+
announce(msg('composer.commandRemoved', removed.label));
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
|
|
451
|
+
const onClick = (event) => {
|
|
452
|
+
const t = event.target;
|
|
453
|
+
if (t.closest('[data-send]')) return send();
|
|
454
|
+
if (t.closest('[data-attach]')) return fileInput?.click();
|
|
455
|
+
if (t.closest('[data-commands]')) {
|
|
456
|
+
if (command) {
|
|
457
|
+
command = null;
|
|
458
|
+
renderTokens();
|
|
459
|
+
}
|
|
460
|
+
if (!input.value.startsWith('/')) input.value = `/${input.value}`;
|
|
461
|
+
input.focus();
|
|
462
|
+
input.setSelectionRange(1, 1);
|
|
463
|
+
autosize();
|
|
464
|
+
sync();
|
|
465
|
+
return checkSlash();
|
|
466
|
+
}
|
|
467
|
+
const remove = t.closest('[data-remove]');
|
|
468
|
+
if (remove) {
|
|
469
|
+
const key = remove.dataset.remove;
|
|
470
|
+
if (key === 'command') {
|
|
471
|
+
announce(msg('composer.commandRemoved', command?.label ?? ''));
|
|
472
|
+
command = null;
|
|
473
|
+
} else {
|
|
474
|
+
const a = attachments.find((x) => x.id === key);
|
|
475
|
+
attachments = attachments.filter((x) => x.id !== key);
|
|
476
|
+
if (a) announce(msg('composer.removed', a.name));
|
|
477
|
+
}
|
|
478
|
+
renderTokens();
|
|
479
|
+
sync();
|
|
480
|
+
input.focus();
|
|
481
|
+
}
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
const onMenuPointerDown = (event) => {
|
|
485
|
+
const opt = event.target.closest('[role="option"]');
|
|
486
|
+
event.preventDefault(); // mantém o foco no campo
|
|
487
|
+
if (opt) choose(options$[+opt.dataset.index]);
|
|
488
|
+
};
|
|
489
|
+
const onMenuMove = (event) => {
|
|
490
|
+
const opt = event.target.closest('[role="option"]');
|
|
491
|
+
if (opt && +opt.dataset.index !== active) setActive(+opt.dataset.index);
|
|
492
|
+
};
|
|
493
|
+
const onDocPointerDown = (event) => {
|
|
494
|
+
if (!root.contains(event.target)) closeMenu();
|
|
495
|
+
};
|
|
496
|
+
const onFile = () => {
|
|
497
|
+
addFiles(fileInput.files);
|
|
498
|
+
fileInput.value = '';
|
|
499
|
+
};
|
|
500
|
+
const onDragOver = (event) => {
|
|
501
|
+
if (!o.attach || !event.dataTransfer?.types?.includes('Files')) return;
|
|
502
|
+
event.preventDefault();
|
|
503
|
+
root.setAttribute('data-drop', '');
|
|
504
|
+
};
|
|
505
|
+
const onDragLeave = (event) => {
|
|
506
|
+
if (!root.contains(event.relatedTarget)) root.removeAttribute('data-drop');
|
|
507
|
+
};
|
|
508
|
+
const onDrop = (event) => {
|
|
509
|
+
if (!o.attach || !event.dataTransfer?.files?.length) return;
|
|
510
|
+
event.preventDefault();
|
|
511
|
+
root.removeAttribute('data-drop');
|
|
512
|
+
addFiles(event.dataTransfer.files);
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
input.addEventListener('input', onInput);
|
|
516
|
+
input.addEventListener('keydown', onKeyDown);
|
|
517
|
+
input.addEventListener('click', checkSlash);
|
|
518
|
+
input.addEventListener('blur', () => setTimeout(() => !root.contains(document.activeElement) && closeMenu(), 0));
|
|
519
|
+
root.addEventListener('click', onClick);
|
|
520
|
+
menu.addEventListener('pointerdown', onMenuPointerDown);
|
|
521
|
+
menu.addEventListener('pointermove', onMenuMove);
|
|
522
|
+
document.addEventListener('pointerdown', onDocPointerDown);
|
|
523
|
+
fileInput?.addEventListener('change', onFile);
|
|
524
|
+
root.addEventListener('dragover', onDragOver);
|
|
525
|
+
root.addEventListener('dragleave', onDragLeave);
|
|
526
|
+
root.addEventListener('drop', onDrop);
|
|
527
|
+
|
|
528
|
+
autosize();
|
|
529
|
+
sync();
|
|
530
|
+
|
|
531
|
+
return {
|
|
532
|
+
el: root,
|
|
533
|
+
input,
|
|
534
|
+
focus: () => input.focus(),
|
|
535
|
+
clear,
|
|
536
|
+
/** O texto do campo. */
|
|
537
|
+
get value() {
|
|
538
|
+
return input.value;
|
|
539
|
+
},
|
|
540
|
+
/** Os anexos, na ordem em que entraram. */
|
|
541
|
+
get attachments() {
|
|
542
|
+
return [...attachments];
|
|
543
|
+
},
|
|
544
|
+
/** O id do comando escolhido com "/", ou null. */
|
|
545
|
+
get command() {
|
|
546
|
+
return command?.id ?? null;
|
|
547
|
+
},
|
|
548
|
+
setValue(text) {
|
|
549
|
+
input.value = text;
|
|
550
|
+
autosize();
|
|
551
|
+
sync();
|
|
552
|
+
checkSlash();
|
|
553
|
+
},
|
|
554
|
+
setBusy(on) {
|
|
555
|
+
busy = !!on;
|
|
556
|
+
root.toggleAttribute('data-busy', busy);
|
|
557
|
+
const label = msg(busy ? 'composer.stop' : 'composer.send');
|
|
558
|
+
sendBtn.setAttribute('aria-label', label);
|
|
559
|
+
sendBtn.dataset.dsTooltip = msg(busy ? 'composer.stop' : 'composer.sendShort');
|
|
560
|
+
sendBtn.dataset.dsTooltipKbd = busy ? 'Esc' : '↵';
|
|
561
|
+
sendBtn.innerHTML = sendIcon(busy);
|
|
562
|
+
sync();
|
|
563
|
+
},
|
|
564
|
+
get busy() {
|
|
565
|
+
return busy;
|
|
566
|
+
},
|
|
567
|
+
destroy() {
|
|
568
|
+
ro.disconnect();
|
|
569
|
+
document.removeEventListener('pointerdown', onDocPointerDown);
|
|
570
|
+
host.innerHTML = '';
|
|
571
|
+
},
|
|
572
|
+
};
|
|
573
|
+
}
|