@istmx/ui 0.1.9 → 0.1.11

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.
@@ -0,0 +1,357 @@
1
+ import { cn } from './utils'
2
+
3
+ export type TokenType =
4
+ | 'tag'
5
+ | 'attribute'
6
+ | 'string'
7
+ | 'keyword'
8
+ | 'component'
9
+ | 'type'
10
+ | 'function'
11
+ | 'literal'
12
+ | 'number'
13
+ | 'comment'
14
+ | 'punctuation'
15
+ | 'identifier'
16
+ | 'space'
17
+
18
+ export type Token = {
19
+ type: TokenType
20
+ value: string
21
+ }
22
+
23
+ // Visual color palette matching the target editor reference
24
+ export const TOKEN_COLORS: Record<TokenType, string> = {
25
+ tag: '#fb923c', // Warm orange for JSX / HTML tags (<AppleCarouselRoot>, <AnimatedText>, </MotionLink>)
26
+ attribute: '#fde047', // Light / warm yellow for JSX attributes (aria-label, prefix, href, initial, animate)
27
+ string: '#4ade80', // Emerald green for string values ("Featured", "blur", "@/components/...")
28
+ keyword: '#f472b6', // Pink / magenta for language keywords (import, from, const, return, export)
29
+ function: '#38bdf8', // Sky blue for function calls and methods (motion.create, useState)
30
+ component: '#2dd4bf', // Teal / cyan for capitalized components & types in expressions
31
+ type: '#2dd4bf', // Teal / cyan for TypeScript types
32
+ literal: '#fdba74', // Warm peach for booleans & null (false, true, null)
33
+ number: '#fdba74', // Warm peach for numbers
34
+ comment: '#71717a', // Muted slate gray for comments
35
+ punctuation: '#94a3b8', // Subtle neutral gray for punctuation (<, >, =, ;, {}, ())
36
+ identifier: '#e2e8f0', // Warm off-white for plain variables & identifiers
37
+ space: 'inherit',
38
+ }
39
+
40
+ const KEYWORDS = new Set([
41
+ 'import',
42
+ 'from',
43
+ 'export',
44
+ 'default',
45
+ 'const',
46
+ 'let',
47
+ 'var',
48
+ 'function',
49
+ 'return',
50
+ 'if',
51
+ 'else',
52
+ 'switch',
53
+ 'case',
54
+ 'break',
55
+ 'continue',
56
+ 'for',
57
+ 'while',
58
+ 'do',
59
+ 'try',
60
+ 'catch',
61
+ 'finally',
62
+ 'throw',
63
+ 'new',
64
+ 'delete',
65
+ 'typeof',
66
+ 'instanceof',
67
+ 'void',
68
+ 'in',
69
+ 'of',
70
+ 'yield',
71
+ 'await',
72
+ 'async',
73
+ 'as',
74
+ ])
75
+
76
+ const TYPES = new Set([
77
+ 'type',
78
+ 'interface',
79
+ 'extends',
80
+ 'implements',
81
+ 'class',
82
+ 'enum',
83
+ 'string',
84
+ 'number',
85
+ 'boolean',
86
+ 'any',
87
+ 'unknown',
88
+ 'never',
89
+ 'symbol',
90
+ 'bigint',
91
+ 'void',
92
+ 'Record',
93
+ 'ReactNode',
94
+ 'React',
95
+ 'keyof',
96
+ 'readonly',
97
+ ])
98
+
99
+ const LITERALS = new Set(['true', 'false', 'null', 'undefined'])
100
+
101
+ export function tokenizeCode(code: string): Token[][] {
102
+ const lines = code.replace(/\r\n/g, '\n').split('\n')
103
+ const result: Token[][] = []
104
+ let inTag = false
105
+ let braceDepth = 0
106
+
107
+ for (const line of lines) {
108
+ const tokens: Token[] = []
109
+ let i = 0
110
+ const len = line.length
111
+
112
+ while (i < len) {
113
+ // 1. Whitespace
114
+ if (/\s/.test(line[i])) {
115
+ let space = ''
116
+ while (i < len && /\s/.test(line[i])) {
117
+ space += line[i++]
118
+ }
119
+ tokens.push({ type: 'space', value: space })
120
+ continue
121
+ }
122
+
123
+ // 2. Comments
124
+ if (line.startsWith('{/*', i)) {
125
+ const end = line.indexOf('*/}', i)
126
+ const val = end === -1 ? line.slice(i) : line.slice(i, end + 3)
127
+ tokens.push({ type: 'comment', value: val })
128
+ i += val.length
129
+ continue
130
+ }
131
+ if (line.startsWith('//', i)) {
132
+ const val = line.slice(i)
133
+ tokens.push({ type: 'comment', value: val })
134
+ break
135
+ }
136
+ if (line.startsWith('/*', i)) {
137
+ const end = line.indexOf('*/', i)
138
+ const val = end === -1 ? line.slice(i) : line.slice(i, end + 2)
139
+ tokens.push({ type: 'comment', value: val })
140
+ i += val.length
141
+ continue
142
+ }
143
+
144
+ // 3. Strings
145
+ if (line[i] === '"' || line[i] === "'" || line[i] === '`') {
146
+ const quote = line[i]
147
+ let str = quote
148
+ i++
149
+ while (i < len) {
150
+ if (line[i] === '\\') {
151
+ str += line[i++]
152
+ if (i < len) str += line[i++]
153
+ } else if (line[i] === quote) {
154
+ str += line[i++]
155
+ break
156
+ } else {
157
+ str += line[i++]
158
+ }
159
+ }
160
+ tokens.push({ type: 'string', value: str })
161
+ continue
162
+ }
163
+
164
+ // 4. Closing JSX Tag: </Tag
165
+ if (line.startsWith('</', i)) {
166
+ const m = line.slice(i + 2).match(/^[a-zA-Z_$][a-zA-Z0-9_$.-]*/)
167
+ if (m) {
168
+ tokens.push({ type: 'punctuation', value: '</' })
169
+ tokens.push({ type: 'tag', value: m[0] })
170
+ i += 2 + m[0].length
171
+ inTag = true
172
+ braceDepth = 0
173
+ continue
174
+ }
175
+ }
176
+
177
+ // 5. Opening JSX Tag: <Tag
178
+ if (line[i] === '<') {
179
+ const m = line.slice(i + 1).match(/^[a-zA-Z_$][a-zA-Z0-9_$.-]*/)
180
+ if (m && !line.startsWith('<=', i) && !line.startsWith('<<', i)) {
181
+ tokens.push({ type: 'punctuation', value: '<' })
182
+ tokens.push({ type: 'tag', value: m[0] })
183
+ i += 1 + m[0].length
184
+ inTag = true
185
+ braceDepth = 0
186
+ continue
187
+ }
188
+ }
189
+
190
+ // Track braces inside JSX attributes
191
+ if (inTag && line[i] === '{') {
192
+ braceDepth++
193
+ tokens.push({ type: 'punctuation', value: '{' })
194
+ i++
195
+ continue
196
+ }
197
+ if (inTag && line[i] === '}') {
198
+ if (braceDepth > 0) braceDepth--
199
+ tokens.push({ type: 'punctuation', value: '}' })
200
+ i++
201
+ continue
202
+ }
203
+
204
+ // Tag closers: /> or > (when outside attribute expressions)
205
+ if (inTag && braceDepth === 0 && line.startsWith('/>', i)) {
206
+ tokens.push({ type: 'punctuation', value: '/>' })
207
+ i += 2
208
+ inTag = false
209
+ continue
210
+ }
211
+ if (inTag && braceDepth === 0 && line[i] === '>') {
212
+ tokens.push({ type: 'punctuation', value: '>' })
213
+ i += 1
214
+ inTag = false
215
+ continue
216
+ }
217
+
218
+ // 6. Multi-character operators
219
+ const two = line.slice(i, i + 2)
220
+ const three = line.slice(i, i + 3)
221
+ if (three === '===' || three === '!==') {
222
+ tokens.push({ type: 'punctuation', value: three })
223
+ i += 3
224
+ continue
225
+ }
226
+ if (
227
+ two === '=>' ||
228
+ two === '==' ||
229
+ two === '!=' ||
230
+ two === '<=' ||
231
+ two === '>=' ||
232
+ two === '&&' ||
233
+ two === '||' ||
234
+ two === '??' ||
235
+ two === '?.'
236
+ ) {
237
+ tokens.push({ type: 'punctuation', value: two })
238
+ i += 2
239
+ continue
240
+ }
241
+
242
+ // Check preceding non-space token in this line
243
+ const prevToken = [...tokens].reverse().find((t) => t.type !== 'space')
244
+ const isAfterDeclaration =
245
+ prevToken && ['const', 'let', 'var', 'export'].includes(prevToken.value)
246
+
247
+ // 7. JSX Attribute: word before = (inTag or in multiline JSX)
248
+ const attrMatch = line
249
+ .slice(i)
250
+ .match(/^[a-zA-Z_$][a-zA-Z0-9_$-]*(?=\s*=)/)
251
+ if (attrMatch && (inTag || !isAfterDeclaration)) {
252
+ tokens.push({ type: 'attribute', value: attrMatch[0] })
253
+ i += attrMatch[0].length
254
+ continue
255
+ }
256
+
257
+ // 8. Boolean attribute inside tag when braceDepth === 0
258
+ if (inTag && braceDepth === 0) {
259
+ const boolAttrMatch = line.slice(i).match(/^[a-zA-Z_$][a-zA-Z0-9_$-]*/)
260
+ if (boolAttrMatch) {
261
+ const val = boolAttrMatch[0]
262
+ let nextIdx = i + val.length
263
+ while (nextIdx < len && /\s/.test(line[nextIdx])) nextIdx++
264
+ if (
265
+ nextIdx >= len ||
266
+ line[nextIdx] === '>' ||
267
+ line.startsWith('/>', nextIdx) ||
268
+ /^[a-zA-Z]/.test(line[nextIdx])
269
+ ) {
270
+ tokens.push({ type: 'attribute', value: val })
271
+ i += val.length
272
+ continue
273
+ }
274
+ }
275
+ }
276
+
277
+ // 9. Numbers
278
+ const numMatch = line.slice(i).match(/^\d+(?:\.\d+)?\b/)
279
+ if (numMatch) {
280
+ tokens.push({ type: 'number', value: numMatch[0] })
281
+ i += numMatch[0].length
282
+ continue
283
+ }
284
+
285
+ // 10. Word / identifier
286
+ const idMatch = line.slice(i).match(/^[a-zA-Z_$][a-zA-Z0-9_$]*/)
287
+ if (idMatch) {
288
+ const val = idMatch[0]
289
+ i += val.length
290
+
291
+ let j = i
292
+ while (j < len && /\s/.test(line[j])) j++
293
+ const isCall = line[j] === '('
294
+
295
+ if (KEYWORDS.has(val)) {
296
+ tokens.push({ type: 'keyword', value: val })
297
+ } else if (LITERALS.has(val)) {
298
+ tokens.push({ type: 'literal', value: val })
299
+ } else if (TYPES.has(val)) {
300
+ tokens.push({ type: 'type', value: val })
301
+ } else if (isCall) {
302
+ tokens.push({ type: 'function', value: val })
303
+ } else if (/^[A-Z]/.test(val)) {
304
+ tokens.push({ type: 'component', value: val })
305
+ } else {
306
+ tokens.push({ type: 'identifier', value: val })
307
+ }
308
+ continue
309
+ }
310
+
311
+ // 11. Single-char punctuation
312
+ tokens.push({ type: 'punctuation', value: line[i] })
313
+ i++
314
+ }
315
+
316
+ result.push(tokens)
317
+ }
318
+
319
+ return result
320
+ }
321
+
322
+ export function TokenSpan({ token }: { token: Token }) {
323
+ if (token.type === 'space') {
324
+ return <>{token.value}</>
325
+ }
326
+
327
+ const color = TOKEN_COLORS[token.type] ?? '#e2e8f0'
328
+ const isItalic = token.type === 'comment'
329
+
330
+ return (
331
+ <span style={{ color }} className={isItalic ? 'italic' : undefined}>
332
+ {token.value}
333
+ </span>
334
+ )
335
+ }
336
+
337
+
338
+ export function CodeHighlighter({
339
+ code,
340
+ className,
341
+ }: {
342
+ code: string
343
+ className?: string
344
+ }) {
345
+ const lines = tokenizeCode(code)
346
+ return (
347
+ <pre className={cn('w-full max-w-full overflow-x-auto p-4 font-mono text-[13.5px] leading-[1.7] antialiased select-text sm:p-5 sm:text-[14.5px] sm:leading-[1.72]', className)} style={{ fontFamily: 'var(--font-geist-mono), ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace' }}>
348
+ <code>{lines.map((line, lineIndex) => (
349
+ <span key={`highlight-line-${lineIndex}`} className="block min-h-[1.7em] whitespace-pre">
350
+ {line.length ? line.map((token, tokenIndex) => (
351
+ <TokenSpan key={`highlight-${lineIndex}-${tokenIndex}`} token={token} />
352
+ )) : <>&nbsp;</>}
353
+ </span>
354
+ ))}</code>
355
+ </pre>
356
+ )
357
+ }
@@ -0,0 +1,42 @@
1
+ import {
2
+ IconFile,
3
+ IconHeadphones,
4
+ IconPhoto,
5
+ IconVideo,
6
+ } from '@tabler/icons-react'
7
+ import type { AttachmentOption } from './types'
8
+
9
+ export const DEFAULT_ATTACHMENT_OPTIONS: readonly AttachmentOption[] = [
10
+ {
11
+ id: 'images',
12
+ label: 'Images',
13
+ description: 'Add photos and images',
14
+ icon: <IconPhoto size={17} stroke={1.7} aria-hidden="true" />,
15
+ accept: 'image/*',
16
+ multiple: true,
17
+ },
18
+ {
19
+ id: 'documents',
20
+ label: 'Files and documents',
21
+ description: 'PDF, text, office files, and more',
22
+ icon: <IconFile size={17} stroke={1.7} aria-hidden="true" />,
23
+ accept: '.pdf,.txt,.md,.csv,.json,.doc,.docx,.xls,.xlsx,.ppt,.pptx,application/*,text/*',
24
+ multiple: true,
25
+ },
26
+ {
27
+ id: 'audio',
28
+ label: 'Audio',
29
+ description: 'Add an audio recording',
30
+ icon: <IconHeadphones size={17} stroke={1.7} aria-hidden="true" />,
31
+ accept: 'audio/*',
32
+ multiple: true,
33
+ },
34
+ {
35
+ id: 'video',
36
+ label: 'Video',
37
+ description: 'Add a video clip',
38
+ icon: <IconVideo size={17} stroke={1.7} aria-hidden="true" />,
39
+ accept: 'video/*',
40
+ multiple: true,
41
+ },
42
+ ]
@@ -0,0 +1,30 @@
1
+ 'use client'
2
+
3
+ export { default as AiChatBlock } from './ai-chat-block'
4
+ export { default as ChatMessage } from './chat-message'
5
+ export { default as ChatComposer } from './chat-composer'
6
+ export { default as MessageList } from './message-list'
7
+ export { default as ChatSidebar } from './chat-sidebar'
8
+ export { default as ChatUserAvatar } from './chat-user-avatar'
9
+ export { default as ChatLogo } from './chat-logo'
10
+ export { default as AttachmentDialog, AttachmentPreview } from './attachment'
11
+ export { DEFAULT_ATTACHMENT_OPTIONS } from './default-attachments'
12
+ export type {
13
+ AttachmentOption,
14
+ ChatAttachment,
15
+ ChatAttachmentKind,
16
+ ChatRequest,
17
+ ChatResponse,
18
+ ChatSendError,
19
+ ChatStreamChunk,
20
+ ChatThread,
21
+ ChatTurn,
22
+ } from './types'
23
+ export type { AiChatBlockProps } from './ai-chat-block'
24
+ export { default as AiChatInput } from './ai-chat-input'
25
+ export type {
26
+ AiChatInputAction,
27
+ AiChatInputAttachment,
28
+ AiChatInputGlow,
29
+ AiChatInputProps,
30
+ } from './ai-chat-input'