@vev/silke 1.1.7 → 1.1.9
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/.turbo/turbo-build.log +800 -0
- package/CHANGELOG.md +14 -0
- package/CLAUDE.md +102 -0
- package/dist/index.js +36 -36
- package/dist/index.js.LEGAL.txt +1 -1
- package/dist/styles.css +498 -282
- package/dist/types/components/index.d.ts +2 -0
- package/dist/types/components/silke-command-text-field/attachment-chip.d.ts +8 -0
- package/dist/types/components/silke-command-text-field/attachment-list.d.ts +15 -0
- package/dist/types/components/silke-command-text-field/file-utils.d.ts +33 -0
- package/dist/types/components/silke-command-text-field/file-utils.spec.d.ts +1 -0
- package/dist/types/components/silke-command-text-field/index.d.ts +6 -1
- package/dist/types/components/silke-command-text-field/silke-command-text-field.d.ts +23 -6
- package/dist/types/components/silke-command-text-field/utils.d.ts +14 -1
- package/dist/types/components/silke-conditions-input/utils.d.ts +1 -1
- package/dist/types/components/silke-css-number-field/silke-css-context.d.ts +2 -0
- package/dist/types/components/silke-css-number-field/silke-css-number-field.d.ts +2 -0
- package/dist/types/components/silke-css-number-field/utils.d.ts +1 -1
- package/dist/types/components/silke-form-schema/fields/color-field.d.ts +3 -0
- package/dist/types/components/silke-form-schema/fields/group-field.d.ts +3 -0
- package/dist/types/components/silke-form-schema/fields/object-field.d.ts +1 -1
- package/dist/types/components/silke-form-schema/fields/popover-field.d.ts +3 -0
- package/dist/types/components/silke-form-schema/fields/utils/generate-image.d.ts +5 -0
- package/dist/types/components/silke-form-schema/fields/utils/index.d.ts +1 -1
- package/dist/types/components/silke-form-schema/schema-utils.d.ts +6 -1
- package/dist/types/components/silke-form-schema/silke-schema-field.d.ts +2 -1
- package/dist/types/components/silke-gradient-picker/index.d.ts +3 -0
- package/dist/types/components/silke-icon/silke-icon.d.ts +1 -1
- package/dist/types/components/silke-list/silke-list.d.ts +6 -1
- package/dist/types/components/silke-list/types.d.ts +2 -0
- package/dist/types/components/silke-shortcut-button/silke-shortcut-button.d.ts +4 -1
- package/dist/types/components/silke-theme-card/index.d.ts +1 -0
- package/dist/types/components/silke-theme-card/silke-theme-card.d.ts +18 -0
- package/dist/types/components/silke-theme-swatch/index.d.ts +1 -0
- package/dist/types/components/silke-theme-swatch/silke-theme-swatch.d.ts +11 -0
- package/dist/types/icons.d.ts +7 -2
- package/package.json +32 -20
- package/src/components/index.ts +2 -0
- package/src/components/silke-autocomplete-field/silke-autocomplete-popup-native.tsx +25 -18
- package/src/components/silke-button/silke-dropdown-button.mdx +1 -1
- package/src/components/silke-color-picker/color.ts +5 -3
- package/src/components/silke-color-swatches/silke-swatches.tsx +3 -3
- package/src/components/silke-command-text-field/attachment-chip.tsx +71 -0
- package/src/components/silke-command-text-field/attachment-list.tsx +72 -0
- package/src/components/silke-command-text-field/file-utils.spec.ts +148 -0
- package/src/components/silke-command-text-field/file-utils.ts +159 -0
- package/src/components/silke-command-text-field/index.ts +14 -0
- package/src/components/silke-command-text-field/silke-command-text-field.mdx +109 -13
- package/src/components/silke-command-text-field/silke-command-text-field.module.scss +65 -40
- package/src/components/silke-command-text-field/silke-command-text-field.tsx +120 -135
- package/src/components/silke-command-text-field/utils.spec.ts +109 -1
- package/src/components/silke-command-text-field/utils.ts +44 -7
- package/src/components/silke-css-number-field/silke-css-context.tsx +2 -0
- package/src/components/silke-css-number-field/silke-css-number-field.tsx +7 -4
- package/src/components/silke-css-number-field/utils.ts +8 -2
- package/src/components/silke-form-schema/fields/array-field-modal.tsx +71 -46
- package/src/components/silke-form-schema/fields/array-field.tsx +160 -122
- package/src/components/silke-form-schema/fields/color-field.tsx +136 -0
- package/src/components/silke-form-schema/fields/group-field.tsx +49 -0
- package/src/components/silke-form-schema/fields/icon-field.tsx +59 -39
- package/src/components/silke-form-schema/fields/image-field.tsx +50 -39
- package/src/components/silke-form-schema/fields/number-field.tsx +148 -184
- package/src/components/silke-form-schema/fields/object-field.tsx +2 -0
- package/src/components/silke-form-schema/fields/popover-field.tsx +65 -0
- package/src/components/silke-form-schema/fields/select-field.tsx +43 -30
- package/src/components/silke-form-schema/fields/string-field.tsx +1 -1
- package/src/components/silke-form-schema/fields/styles.scss +2 -39
- package/src/components/silke-form-schema/fields/toggle-field.tsx +48 -15
- package/src/components/silke-form-schema/fields/utils/generate-image.ts +20 -0
- package/src/components/silke-form-schema/fields/utils/generate-title.ts +1 -1
- package/src/components/silke-form-schema/fields/utils/index.ts +1 -1
- package/src/components/silke-form-schema/schema-utils.ts +27 -1
- package/src/components/silke-form-schema/silke-form-schema.mdx +498 -311
- package/src/components/silke-form-schema/silke-schema-field.tsx +12 -3
- package/src/components/silke-form-schema/silke-schema-fields.tsx +9 -5
- package/src/components/silke-gradient-picker/index.ts +3 -0
- package/src/components/silke-icon/silke-icon.mdx +13 -1
- package/src/components/silke-icon/silke-icon.tsx +6 -2
- package/src/components/silke-list/silke-list.tsx +59 -26
- package/src/components/silke-list/types.ts +2 -0
- package/src/components/silke-navigation-link/silke-navigation-link.scss +1 -0
- package/src/components/silke-shortcut-button/silke-shortcut-button.scss +16 -0
- package/src/components/silke-shortcut-button/silke-shortcut-button.tsx +9 -0
- package/src/components/silke-slider/silke-slider.module.scss +1 -1
- package/src/components/silke-tag/silke-tag.scss +1 -1
- package/src/components/silke-text-field/silke-text-field-outline.tsx +9 -6
- package/src/components/silke-text-field/silke-text-field.scss +3 -0
- package/src/components/silke-text-field/silke-text-field.tsx +1 -1
- package/src/components/silke-theme-card/index.ts +1 -0
- package/src/components/silke-theme-card/silke-theme-card.mdx +50 -0
- package/src/components/silke-theme-card/silke-theme-card.module.scss +82 -0
- package/src/components/silke-theme-card/silke-theme-card.tsx +82 -0
- package/src/components/silke-theme-swatch/index.ts +1 -0
- package/src/components/silke-theme-swatch/silke-theme-swatch.mdx +59 -0
- package/src/components/silke-theme-swatch/silke-theme-swatch.module.scss +108 -0
- package/src/components/silke-theme-swatch/silke-theme-swatch.tsx +49 -0
- package/src/components/silke-variables-wrapper/silke-variables-wrapper.tsx +9 -8
- package/src/icons.tsx +41 -7
- package/tsconfig.build.tsbuildinfo +1 -1
- package/project.json +0 -12
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import React, { useState } from 'react';
|
|
2
|
+
import { SilkeButton } from '../silke-button';
|
|
3
|
+
import { SilkeIcon } from '../silke-icon/silke-icon';
|
|
4
|
+
import { SilkePopover } from '../silke-popover/silke-popover';
|
|
5
|
+
import { getFileIcon, truncateFilename } from './file-utils';
|
|
6
|
+
import { imageValueToDataUrl } from './image-utils';
|
|
7
|
+
import styles from './silke-command-text-field.module.scss';
|
|
8
|
+
import { isImage, SilkeFileValue, SilkeImageValue } from './utils';
|
|
9
|
+
|
|
10
|
+
interface AttachmentChipProps {
|
|
11
|
+
attachment: SilkeImageValue | SilkeFileValue;
|
|
12
|
+
disabled?: boolean;
|
|
13
|
+
onRemove?: () => void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function AttachmentChip({ attachment, disabled, onRemove }: AttachmentChipProps) {
|
|
17
|
+
const name = isImage(attachment) ? attachment.name || 'Image' : attachment.name;
|
|
18
|
+
const [showPreview, setShowPreview] = useState(false);
|
|
19
|
+
|
|
20
|
+
return (
|
|
21
|
+
<div
|
|
22
|
+
className={`${styles.attachmentChip}${isImage(attachment) ? ` ${styles.imageChip}` : ''}`}
|
|
23
|
+
title={name}
|
|
24
|
+
>
|
|
25
|
+
{isImage(attachment) ? (
|
|
26
|
+
<>
|
|
27
|
+
<div
|
|
28
|
+
className={styles.chipThumbnail}
|
|
29
|
+
onClick={() => setShowPreview((v) => !v)}
|
|
30
|
+
role="button"
|
|
31
|
+
tabIndex={0}
|
|
32
|
+
onKeyDown={(e) => {
|
|
33
|
+
if (e.key === 'Enter' || e.key === ' ') {
|
|
34
|
+
e.preventDefault();
|
|
35
|
+
setShowPreview((v) => !v);
|
|
36
|
+
}
|
|
37
|
+
}}
|
|
38
|
+
>
|
|
39
|
+
<img src={imageValueToDataUrl(attachment)} alt={name} />
|
|
40
|
+
</div>
|
|
41
|
+
<SilkePopover
|
|
42
|
+
hide={!showPreview}
|
|
43
|
+
shadow="level3"
|
|
44
|
+
contextId="attachment-preview"
|
|
45
|
+
onRequestClose={() => setShowPreview(false)}
|
|
46
|
+
>
|
|
47
|
+
<div className={styles.imagePreview}>
|
|
48
|
+
<img src={imageValueToDataUrl(attachment)} alt={name} />
|
|
49
|
+
</div>
|
|
50
|
+
</SilkePopover>
|
|
51
|
+
</>
|
|
52
|
+
) : (
|
|
53
|
+
<SilkeIcon icon={getFileIcon(attachment.mimeType)} />
|
|
54
|
+
)}
|
|
55
|
+
<span className={styles.chipName}>{truncateFilename(name)}</span>
|
|
56
|
+
{!disabled && onRemove && (
|
|
57
|
+
<SilkeButton
|
|
58
|
+
kind="ghost"
|
|
59
|
+
size="s"
|
|
60
|
+
icon="close"
|
|
61
|
+
className={styles.chipRemove}
|
|
62
|
+
onClick={(e) => {
|
|
63
|
+
e.stopPropagation();
|
|
64
|
+
onRemove();
|
|
65
|
+
}}
|
|
66
|
+
aria-label={`Remove ${name}`}
|
|
67
|
+
/>
|
|
68
|
+
)}
|
|
69
|
+
</div>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import React, { useCallback, useImperativeHandle, useRef } from 'react';
|
|
2
|
+
import { AttachmentChip } from './attachment-chip';
|
|
3
|
+
import { fileToAttachmentValue } from './file-utils';
|
|
4
|
+
import styles from './silke-command-text-field.module.scss';
|
|
5
|
+
import { SilkeFileValue, SilkeImageValue } from './utils';
|
|
6
|
+
|
|
7
|
+
interface AttachmentListProps {
|
|
8
|
+
acceptedTypes: string[];
|
|
9
|
+
attachments: (SilkeImageValue | SilkeFileValue)[];
|
|
10
|
+
disabled?: boolean;
|
|
11
|
+
onAdd: (attachments: (SilkeImageValue | SilkeFileValue)[]) => void;
|
|
12
|
+
onRemove: (index: number) => void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface AttachmentListHandle {
|
|
16
|
+
/** Open the file picker dialog */
|
|
17
|
+
openFilePicker: () => void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const AttachmentList = React.forwardRef<AttachmentListHandle, AttachmentListProps>(
|
|
21
|
+
({ acceptedTypes, attachments, disabled, onAdd, onRemove }, ref) => {
|
|
22
|
+
const fileInputRef = useRef<HTMLInputElement>(null);
|
|
23
|
+
|
|
24
|
+
const handleFileSelect = useCallback(
|
|
25
|
+
(e: React.ChangeEvent<HTMLInputElement>) => {
|
|
26
|
+
const { files } = e.target;
|
|
27
|
+
if (!files || files.length === 0) return;
|
|
28
|
+
|
|
29
|
+
Promise.all(
|
|
30
|
+
Array.from(files).map((file) => fileToAttachmentValue(file, acceptedTypes)),
|
|
31
|
+
).then((results) => {
|
|
32
|
+
const valid = results.filter((v): v is SilkeImageValue | SilkeFileValue => v !== null);
|
|
33
|
+
if (valid.length > 0) onAdd(valid);
|
|
34
|
+
});
|
|
35
|
+
// Reset so the same file can be selected again
|
|
36
|
+
e.target.value = '';
|
|
37
|
+
},
|
|
38
|
+
[acceptedTypes, onAdd],
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
useImperativeHandle(ref, () => ({
|
|
42
|
+
openFilePicker: () => fileInputRef.current?.click(),
|
|
43
|
+
}));
|
|
44
|
+
|
|
45
|
+
return (
|
|
46
|
+
<>
|
|
47
|
+
{attachments.length > 0 && (
|
|
48
|
+
<div className={styles.attachmentList}>
|
|
49
|
+
{attachments.map((attachment, idx) => (
|
|
50
|
+
<AttachmentChip
|
|
51
|
+
key={idx}
|
|
52
|
+
attachment={attachment}
|
|
53
|
+
disabled={disabled}
|
|
54
|
+
onRemove={() => onRemove(idx)}
|
|
55
|
+
/>
|
|
56
|
+
))}
|
|
57
|
+
</div>
|
|
58
|
+
)}
|
|
59
|
+
<input
|
|
60
|
+
ref={fileInputRef}
|
|
61
|
+
type="file"
|
|
62
|
+
className={styles.fileInput}
|
|
63
|
+
accept={acceptedTypes.join(',')}
|
|
64
|
+
multiple
|
|
65
|
+
onChange={handleFileSelect}
|
|
66
|
+
/>
|
|
67
|
+
</>
|
|
68
|
+
);
|
|
69
|
+
},
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
AttachmentList.displayName = 'AttachmentList';
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { matchesMimePattern, truncateFilename, getFileIcon, resolveFileCategories } from './file-utils';
|
|
2
|
+
|
|
3
|
+
describe('matchesMimePattern', () => {
|
|
4
|
+
test('matches exact MIME type', () => {
|
|
5
|
+
expect(matchesMimePattern('text/csv', ['text/csv'])).toBe(true);
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
test('matches wildcard pattern', () => {
|
|
9
|
+
expect(matchesMimePattern('image/png', ['image/*'])).toBe(true);
|
|
10
|
+
expect(matchesMimePattern('image/jpeg', ['image/*'])).toBe(true);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test('does not match wrong wildcard pattern', () => {
|
|
14
|
+
expect(matchesMimePattern('text/csv', ['image/*'])).toBe(false);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test('matches */* pattern', () => {
|
|
18
|
+
expect(matchesMimePattern('text/csv', ['*/*'])).toBe(true);
|
|
19
|
+
expect(matchesMimePattern('application/pdf', ['*/*'])).toBe(true);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('rejects non-matching exact types', () => {
|
|
23
|
+
expect(matchesMimePattern('text/plain', ['text/csv'])).toBe(false);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test('matches when any pattern matches', () => {
|
|
27
|
+
expect(matchesMimePattern('text/csv', ['image/*', 'text/csv'])).toBe(true);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test('returns false for empty patterns array', () => {
|
|
31
|
+
expect(matchesMimePattern('text/csv', [])).toBe(false);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('matches Excel MIME types', () => {
|
|
35
|
+
expect(
|
|
36
|
+
matchesMimePattern('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', [
|
|
37
|
+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
38
|
+
]),
|
|
39
|
+
).toBe(true);
|
|
40
|
+
expect(matchesMimePattern('application/vnd.ms-excel', ['application/vnd.ms-excel'])).toBe(true);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('matches PDF MIME type', () => {
|
|
44
|
+
expect(matchesMimePattern('application/pdf', ['application/pdf'])).toBe(true);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('matches Word MIME types', () => {
|
|
48
|
+
expect(matchesMimePattern('application/msword', ['application/msword'])).toBe(true);
|
|
49
|
+
expect(
|
|
50
|
+
matchesMimePattern(
|
|
51
|
+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
52
|
+
['application/vnd.openxmlformats-officedocument.wordprocessingml.document'],
|
|
53
|
+
),
|
|
54
|
+
).toBe(true);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('matches application/* wildcard for document types', () => {
|
|
58
|
+
expect(matchesMimePattern('application/pdf', ['application/*'])).toBe(true);
|
|
59
|
+
expect(matchesMimePattern('application/msword', ['application/*'])).toBe(true);
|
|
60
|
+
expect(
|
|
61
|
+
matchesMimePattern('application/vnd.openxmlformats-officedocument.wordprocessingml.document', [
|
|
62
|
+
'application/*',
|
|
63
|
+
]),
|
|
64
|
+
).toBe(true);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe('resolveFileCategories', () => {
|
|
69
|
+
test('resolves image category to image/* pattern', () => {
|
|
70
|
+
const patterns = resolveFileCategories(['image']);
|
|
71
|
+
expect(patterns).toContain('image/*');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('resolves text category to document MIME types', () => {
|
|
75
|
+
const patterns = resolveFileCategories(['text']);
|
|
76
|
+
expect(patterns).toContain('text/plain');
|
|
77
|
+
expect(patterns).toContain('text/csv');
|
|
78
|
+
expect(patterns).toContain('application/pdf');
|
|
79
|
+
expect(patterns).toContain('application/msword');
|
|
80
|
+
expect(patterns).toContain('application/vnd.openxmlformats-officedocument.wordprocessingml.document');
|
|
81
|
+
expect(patterns).toContain('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
82
|
+
expect(patterns).toContain('application/json');
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('resolves both categories together', () => {
|
|
86
|
+
const patterns = resolveFileCategories(['image', 'text']);
|
|
87
|
+
expect(patterns).toContain('image/*');
|
|
88
|
+
expect(patterns).toContain('application/pdf');
|
|
89
|
+
expect(patterns).toContain('text/plain');
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test('returns empty array for empty input', () => {
|
|
93
|
+
expect(resolveFileCategories([])).toEqual([]);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
describe('truncateFilename', () => {
|
|
98
|
+
test('returns short names unchanged', () => {
|
|
99
|
+
expect(truncateFilename('data.csv')).toBe('data.csv');
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test('truncates long names preserving extension', () => {
|
|
103
|
+
const result = truncateFilename('very-long-filename-here.csv', 20);
|
|
104
|
+
expect(result).toHaveLength(20);
|
|
105
|
+
expect(result).toMatch(/\.csv$/);
|
|
106
|
+
expect(result).toContain('...');
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test('handles names without extension', () => {
|
|
110
|
+
const result = truncateFilename('averyverylongfilename', 10);
|
|
111
|
+
expect(result).toHaveLength(10);
|
|
112
|
+
expect(result).toContain('...');
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test('handles custom maxLength', () => {
|
|
116
|
+
expect(truncateFilename('short.txt', 50)).toBe('short.txt');
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe('getFileIcon', () => {
|
|
121
|
+
test('returns file icon for csv', () => {
|
|
122
|
+
expect(getFileIcon('text/csv')).toBe('file');
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test('returns file icon for xlsx', () => {
|
|
126
|
+
expect(
|
|
127
|
+
getFileIcon('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'),
|
|
128
|
+
).toBe('file');
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test('returns file icon for plain text', () => {
|
|
132
|
+
expect(getFileIcon('text/plain')).toBe('file');
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test('returns file icon for PDF', () => {
|
|
136
|
+
expect(getFileIcon('application/pdf')).toBe('file');
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
test('returns file icon for Word doc', () => {
|
|
140
|
+
expect(getFileIcon('application/msword')).toBe('file');
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test('returns file icon for Word docx', () => {
|
|
144
|
+
expect(
|
|
145
|
+
getFileIcon('application/vnd.openxmlformats-officedocument.wordprocessingml.document'),
|
|
146
|
+
).toBe('file');
|
|
147
|
+
});
|
|
148
|
+
});
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import type { SilkeIcons } from '../silke-icon/silke-icon';
|
|
2
|
+
import { SilkeFileValue, SilkeImageValue } from './utils';
|
|
3
|
+
|
|
4
|
+
/** File type category for the acceptedFileTypes prop */
|
|
5
|
+
export type SilkeFileCategory = 'image' | 'text';
|
|
6
|
+
|
|
7
|
+
/** MIME patterns for each file category */
|
|
8
|
+
const FILE_CATEGORY_PATTERNS: Record<SilkeFileCategory, string[]> = {
|
|
9
|
+
image: ['image/*'],
|
|
10
|
+
text: [
|
|
11
|
+
'text/plain',
|
|
12
|
+
'text/csv',
|
|
13
|
+
'text/markdown',
|
|
14
|
+
'text/html',
|
|
15
|
+
'application/pdf',
|
|
16
|
+
'application/msword',
|
|
17
|
+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
18
|
+
'application/vnd.ms-excel',
|
|
19
|
+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
20
|
+
'application/vnd.ms-powerpoint',
|
|
21
|
+
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
22
|
+
'application/json',
|
|
23
|
+
],
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/** Resolve file categories to a flat list of MIME patterns */
|
|
27
|
+
export function resolveFileCategories(categories: SilkeFileCategory[]): string[] {
|
|
28
|
+
return categories.flatMap((cat) => FILE_CATEGORY_PATTERNS[cat] ?? []);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Check if a MIME type matches a set of MIME patterns.
|
|
33
|
+
* Supports exact matches ('text/csv') and wildcard subtypes ('image/*').
|
|
34
|
+
*/
|
|
35
|
+
export function matchesMimePattern(mimeType: string, patterns: string[]): boolean {
|
|
36
|
+
return patterns.some((pattern) => {
|
|
37
|
+
if (pattern === '*/*') return true;
|
|
38
|
+
if (pattern.endsWith('/*')) {
|
|
39
|
+
const prefix = pattern.slice(0, pattern.indexOf('/'));
|
|
40
|
+
return mimeType.startsWith(prefix + '/');
|
|
41
|
+
}
|
|
42
|
+
return mimeType === pattern;
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Reads a File and converts it to a SilkeImageValue or SilkeFileValue.
|
|
48
|
+
* Images produce SilkeImageValue for backward compatibility; other files produce SilkeFileValue.
|
|
49
|
+
* Returns null if the file type is not in the accepted patterns.
|
|
50
|
+
*/
|
|
51
|
+
export function fileToAttachmentValue(
|
|
52
|
+
file: File,
|
|
53
|
+
acceptedTypes: string[],
|
|
54
|
+
): Promise<SilkeImageValue | SilkeFileValue | null> {
|
|
55
|
+
return new Promise((resolve) => {
|
|
56
|
+
if (!matchesMimePattern(file.type, acceptedTypes)) {
|
|
57
|
+
resolve(null);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const reader = new FileReader();
|
|
62
|
+
reader.onload = (event) => {
|
|
63
|
+
const result = event.target?.result;
|
|
64
|
+
if (typeof result === 'string') {
|
|
65
|
+
const [, base64Data] = result.split(',');
|
|
66
|
+
|
|
67
|
+
if (file.type.startsWith('image/')) {
|
|
68
|
+
resolve({
|
|
69
|
+
type: 'image',
|
|
70
|
+
data: base64Data,
|
|
71
|
+
mimeType: file.type,
|
|
72
|
+
name: file.name || undefined,
|
|
73
|
+
});
|
|
74
|
+
} else {
|
|
75
|
+
resolve({
|
|
76
|
+
type: 'file',
|
|
77
|
+
data: base64Data,
|
|
78
|
+
mimeType: file.type,
|
|
79
|
+
name: file.name || 'Untitled',
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
83
|
+
resolve(null);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
reader.onerror = () => resolve(null);
|
|
87
|
+
reader.readAsDataURL(file);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Extract the first matching file from a ClipboardEvent given accepted MIME patterns.
|
|
93
|
+
*/
|
|
94
|
+
export function getFileFromClipboard(
|
|
95
|
+
e: React.ClipboardEvent,
|
|
96
|
+
acceptedTypes: string[],
|
|
97
|
+
): File | null {
|
|
98
|
+
const items = e.clipboardData?.items;
|
|
99
|
+
if (!items) return null;
|
|
100
|
+
|
|
101
|
+
for (const item of items) {
|
|
102
|
+
if (matchesMimePattern(item.type, acceptedTypes)) {
|
|
103
|
+
return item.getAsFile();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Extract the first matching file from a DragEvent given accepted MIME patterns.
|
|
111
|
+
*/
|
|
112
|
+
export function getFileFromDragEvent(
|
|
113
|
+
e: DragEvent,
|
|
114
|
+
acceptedTypes: string[],
|
|
115
|
+
): File | null {
|
|
116
|
+
const files = e.dataTransfer?.files;
|
|
117
|
+
if (!files || files.length === 0) return null;
|
|
118
|
+
|
|
119
|
+
for (const file of files) {
|
|
120
|
+
if (matchesMimePattern(file.type, acceptedTypes)) {
|
|
121
|
+
return file;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Maps a MIME type to a SilkeIcon key for display.
|
|
129
|
+
*/
|
|
130
|
+
export function getFileIcon(mimeType: string): SilkeIcons {
|
|
131
|
+
if (mimeType === 'application/pdf') return 'file';
|
|
132
|
+
if (
|
|
133
|
+
mimeType === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ||
|
|
134
|
+
mimeType === 'application/vnd.ms-excel'
|
|
135
|
+
) {
|
|
136
|
+
return 'file';
|
|
137
|
+
}
|
|
138
|
+
if (
|
|
139
|
+
mimeType === 'application/msword' ||
|
|
140
|
+
mimeType === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
|
|
141
|
+
) {
|
|
142
|
+
return 'file';
|
|
143
|
+
}
|
|
144
|
+
if (mimeType.startsWith('text/')) return 'file';
|
|
145
|
+
return 'file';
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Truncate a filename for display, preserving the extension.
|
|
150
|
+
*/
|
|
151
|
+
export function truncateFilename(name: string, maxLength = 20): string {
|
|
152
|
+
if (name.length <= maxLength) return name;
|
|
153
|
+
const dotIndex = name.lastIndexOf('.');
|
|
154
|
+
const ext = dotIndex >= 0 ? name.slice(dotIndex) : '';
|
|
155
|
+
const stem = name.slice(0, name.length - ext.length);
|
|
156
|
+
const available = maxLength - ext.length - 3; // 3 for "..."
|
|
157
|
+
if (available <= 0) return name.slice(0, maxLength - 3) + '...';
|
|
158
|
+
return stem.slice(0, available) + '...' + ext;
|
|
159
|
+
}
|
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
export { AttachmentChip } from './attachment-chip';
|
|
2
|
+
export { AttachmentList } from './attachment-list';
|
|
3
|
+
export type { AttachmentListHandle } from './attachment-list';
|
|
4
|
+
export {
|
|
5
|
+
fileToAttachmentValue,
|
|
6
|
+
getFileFromClipboard,
|
|
7
|
+
getFileFromDragEvent,
|
|
8
|
+
matchesMimePattern,
|
|
9
|
+
resolveFileCategories,
|
|
10
|
+
} from './file-utils';
|
|
11
|
+
export type { SilkeFileCategory } from './file-utils';
|
|
1
12
|
export { fileToImageValue, imageValueToDataUrl } from './image-utils';
|
|
2
13
|
export { parseTextToSilkeCommands } from './silke-command-parser';
|
|
3
14
|
export * from './silke-command-text-field';
|
|
@@ -5,8 +16,11 @@ export {
|
|
|
5
16
|
SilkeCommand,
|
|
6
17
|
SilkeCommandTextFieldValue,
|
|
7
18
|
SilkeCommandType,
|
|
19
|
+
SilkeFileValue,
|
|
8
20
|
SilkeImageValue,
|
|
9
21
|
SilkeTextValue,
|
|
10
22
|
convertSilkeCommandsToString,
|
|
23
|
+
isAttachment,
|
|
24
|
+
isFile,
|
|
11
25
|
isImage,
|
|
12
26
|
} from './utils';
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
# SilkeCommandTextField
|
|
2
2
|
|
|
3
|
-
A rich text input component that supports inline commands (mentions and actions) with autocomplete suggestions. Perfect for chat interfaces, command palettes, or any input that needs to reference users, actions, or other entities.
|
|
3
|
+
A rich text input component that supports inline commands (mentions and actions) with autocomplete suggestions, and file attachments via paste or drag-and-drop. Perfect for chat interfaces, command palettes, or any input that needs to reference users, actions, or other entities.
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
7
|
- **Mentions (`@`)**: Reference users, entities, or any mentionable items
|
|
8
8
|
- **Actions (`/`)**: Trigger commands or actions
|
|
9
9
|
- **Autocomplete**: Show suggestions as users type
|
|
10
|
+
- **File attachments**: Attach images, PDF, Word, CSV, Excel, TXT, or any file type via paste, drag-and-drop, or programmatically via `openFilePicker()`
|
|
11
|
+
- **Configurable file types**: Control which file categories are accepted with `acceptedFileTypes`
|
|
10
12
|
- **Keyboard navigation**: Navigate and select suggestions with keyboard
|
|
11
13
|
- **Placeholder support**: Show placeholder text when empty
|
|
12
14
|
- **Disabled state**: Prevent editing when needed
|
|
@@ -141,6 +143,69 @@ const availableCommands = [
|
|
|
141
143
|
<SilkeCommandTextField availableCommands={availableCommands} onChange={setValue} value={value} />;
|
|
142
144
|
```
|
|
143
145
|
|
|
146
|
+
## With File Attachments
|
|
147
|
+
|
|
148
|
+
Accept images and document files via paste, drag-and-drop, or programmatically via the `openFilePicker()` ref method:
|
|
149
|
+
|
|
150
|
+
```js
|
|
151
|
+
const [value, setValue] = React.useState([]);
|
|
152
|
+
const ref = React.useRef(null);
|
|
153
|
+
|
|
154
|
+
const availableCommands = [
|
|
155
|
+
{ type: SilkeCommandType.MENTION, value: 'alice' },
|
|
156
|
+
{ type: SilkeCommandType.ACTION, value: 'analyze' },
|
|
157
|
+
];
|
|
158
|
+
|
|
159
|
+
<SilkeBox column gap="s">
|
|
160
|
+
<SilkeCommandTextField
|
|
161
|
+
ref={ref}
|
|
162
|
+
acceptedFileTypes={['image', 'text']}
|
|
163
|
+
availableCommands={availableCommands}
|
|
164
|
+
onChange={setValue}
|
|
165
|
+
placeholder="Type a message or paste/drop files..."
|
|
166
|
+
value={value}
|
|
167
|
+
>
|
|
168
|
+
<SilkeButton
|
|
169
|
+
kind="ghost"
|
|
170
|
+
size="s"
|
|
171
|
+
icon="add"
|
|
172
|
+
onClick={() => ref.current?.openFilePicker()}
|
|
173
|
+
aria-label="Add file"
|
|
174
|
+
/>
|
|
175
|
+
</SilkeCommandTextField>
|
|
176
|
+
</SilkeBox>;
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
All attachments are displayed as horizontally scrollable chips. Image chips show a small thumbnail (click to preview full-size). File chips show an icon and filename. Each chip has a remove button. Use `openFilePicker()` via the ref to open the system file picker from an external button.
|
|
180
|
+
|
|
181
|
+
By default, no file attachments are accepted. You must explicitly provide `acceptedFileTypes` to enable them.
|
|
182
|
+
|
|
183
|
+
### Images only
|
|
184
|
+
|
|
185
|
+
```js
|
|
186
|
+
const [value, setValue] = React.useState([]);
|
|
187
|
+
|
|
188
|
+
<SilkeCommandTextField
|
|
189
|
+
acceptedFileTypes={['image']}
|
|
190
|
+
availableCommands={[]}
|
|
191
|
+
onChange={setValue}
|
|
192
|
+
value={value}
|
|
193
|
+
/>
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### Documents only (no images)
|
|
197
|
+
|
|
198
|
+
```js
|
|
199
|
+
const [value, setValue] = React.useState([]);
|
|
200
|
+
|
|
201
|
+
<SilkeCommandTextField
|
|
202
|
+
acceptedFileTypes={['text']}
|
|
203
|
+
availableCommands={[]}
|
|
204
|
+
onChange={setValue}
|
|
205
|
+
value={value}
|
|
206
|
+
/>
|
|
207
|
+
```
|
|
208
|
+
|
|
144
209
|
## Disabled State
|
|
145
210
|
|
|
146
211
|
```js
|
|
@@ -257,17 +322,18 @@ const availableCommands = [
|
|
|
257
322
|
|
|
258
323
|
## Props
|
|
259
324
|
|
|
260
|
-
| Prop | Type | Default
|
|
261
|
-
| ------------------- | --------------------------------------------------------------- |
|
|
262
|
-
| `availableCommands` | `SilkeCommand[]` | Required
|
|
263
|
-
| `value` | `SilkeCommandTextFieldValue[]` | Required
|
|
264
|
-
| `onChange` | `(value: SilkeCommandTextFieldValue[]) => void` | Required
|
|
265
|
-
| `
|
|
266
|
-
| `
|
|
267
|
-
| `
|
|
268
|
-
| `
|
|
269
|
-
| `
|
|
270
|
-
| `
|
|
325
|
+
| Prop | Type | Default | Description |
|
|
326
|
+
| ------------------- | --------------------------------------------------------------- | ------------ | ------------------------------------------------------------------------------------ |
|
|
327
|
+
| `availableCommands` | `SilkeCommand[]` | Required | List of valid commands for autocomplete and validation |
|
|
328
|
+
| `value` | `SilkeCommandTextFieldValue[]` | Required | Current value of the field |
|
|
329
|
+
| `onChange` | `(value: SilkeCommandTextFieldValue[]) => void` | Required | Called when the value changes |
|
|
330
|
+
| `acceptedFileTypes` | `SilkeFileCategory[]` | `[]` | File categories to accept: `'image'` and/or `'text'` |
|
|
331
|
+
| `onSuggestions` | `(suggestions: SilkeCommand[]) => void` | - | Called with matching suggestions when typing a partial command |
|
|
332
|
+
| `onCommandFocus` | `(index: number, command?: SilkeCommandTextFieldValue) => void` | - | Called when the active command changes |
|
|
333
|
+
| `onKeyDown` | `(e: React.KeyboardEvent) => void` | - | Called on key down events |
|
|
334
|
+
| `placeholder` | `string` | - | Placeholder text shown when empty |
|
|
335
|
+
| `disabled` | `boolean` | `false` | Disable editing |
|
|
336
|
+
| `autoFocus` | `boolean` | `false` | Focus on mount |
|
|
271
337
|
|
|
272
338
|
## Ref Methods
|
|
273
339
|
|
|
@@ -275,6 +341,7 @@ const availableCommands = [
|
|
|
275
341
|
| ------------------------- | -------------------------------------------------- | ------------------------------------------------------------------------------------------ |
|
|
276
342
|
| `focus()` | `() => void` | Focus the input |
|
|
277
343
|
| `blur()` | `() => void` | Remove focus from the input |
|
|
344
|
+
| `openFilePicker()` | `() => void` | Open the system file picker (requires `acceptedFileTypes` to be set) |
|
|
278
345
|
| `update(command, index?)` | `(command: SilkeCommand, index?: number) => void` | Insert or update a command at the given index (defaults to active index, use -1 to append) |
|
|
279
346
|
| `toString(value?)` | `(value?: SilkeCommandTextFieldValue[]) => string` | Convert value to plain string |
|
|
280
347
|
|
|
@@ -297,5 +364,34 @@ type SilkeTextValue = {
|
|
|
297
364
|
value: string;
|
|
298
365
|
};
|
|
299
366
|
|
|
300
|
-
type
|
|
367
|
+
type SilkeImageValue = {
|
|
368
|
+
type: 'image';
|
|
369
|
+
/** Base64-encoded image data */
|
|
370
|
+
data: string;
|
|
371
|
+
/** MIME type (e.g., 'image/png', 'image/jpeg') */
|
|
372
|
+
mimeType: string;
|
|
373
|
+
/** Optional filename */
|
|
374
|
+
name?: string;
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
type SilkeFileValue = {
|
|
378
|
+
type: 'file';
|
|
379
|
+
/** Base64-encoded file data */
|
|
380
|
+
data: string;
|
|
381
|
+
/** MIME type (e.g., 'text/csv', 'application/vnd.ms-excel') */
|
|
382
|
+
mimeType: string;
|
|
383
|
+
/** Filename */
|
|
384
|
+
name: string;
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
type SilkeCommandTextFieldValue = SilkeCommand | SilkeTextValue | SilkeImageValue | SilkeFileValue;
|
|
301
388
|
```
|
|
389
|
+
|
|
390
|
+
### File categories
|
|
391
|
+
|
|
392
|
+
The `acceptedFileTypes` prop accepts an array of category strings:
|
|
393
|
+
|
|
394
|
+
| Category | Accepted file types |
|
|
395
|
+
| -------- | ------------------- |
|
|
396
|
+
| `'image'` | All image types (PNG, JPEG, GIF, WebP, SVG, etc.) |
|
|
397
|
+
| `'text'` | PDF, Word (.doc/.docx), Excel (.xls/.xlsx), PowerPoint (.ppt/.pptx), CSV, plain text, Markdown, HTML, JSON |
|