@vev/silke 1.1.6 → 1.1.7
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 +8 -0
- package/dist/index.js +35 -35
- package/dist/index.js.LEGAL.txt +12 -12
- package/dist/styles.css +4416 -4020
- package/dist/types/components/index.d.ts +3 -2
- package/dist/types/components/silke-button/silke-dropdown-button.d.ts +8 -13
- package/dist/types/components/silke-card/silke-card-grid.d.ts +33 -1
- package/dist/types/components/silke-command-text-field/image-utils.d.ts +20 -0
- package/dist/types/components/silke-command-text-field/index.d.ts +4 -0
- package/dist/types/components/silke-command-text-field/silke-command-parser.d.ts +3 -0
- package/dist/types/components/silke-command-text-field/silke-command-parser.spec.d.ts +1 -0
- package/dist/types/components/silke-command-text-field/silke-command-text-field.d.ts +46 -0
- package/dist/types/components/silke-command-text-field/utils.d.ts +61 -0
- package/dist/types/components/silke-command-text-field/utils.spec.d.ts +1 -0
- package/dist/types/components/silke-form-schema/silke-schema-fields.d.ts +1 -0
- package/dist/types/components/silke-image/silke-image.d.ts +2 -1
- package/dist/types/components/silke-list/index.d.ts +1 -0
- package/dist/types/components/silke-list/silke-list-item.d.ts +24 -6
- package/dist/types/components/silke-list/silke-list.d.ts +9 -9
- package/dist/types/components/silke-list/types.d.ts +19 -0
- package/dist/types/components/silke-list/utils.d.ts +3 -0
- package/dist/types/components/silke-navigation-link/silke-navigation-link.d.ts +1 -0
- package/dist/types/components/silke-overflow-menu/silke-overflow-menu.d.ts +6 -1
- package/dist/types/components/silke-popover-native/silke-popover-native.d.ts +3 -1
- package/dist/types/components/silke-shortcut-button/index.d.ts +1 -0
- package/dist/types/components/silke-shortcut-button/silke-shortcut-button.d.ts +15 -0
- package/dist/types/components/silke-tag/silke-tag.d.ts +1 -1
- package/dist/types/icons.d.ts +30 -0
- package/jest.config.js +2 -3
- package/package.json +7 -2
- package/src/_variables.scss +125 -129
- package/src/components/index.ts +3 -2
- package/src/components/silke-action-box/silke-action-box.scss +2 -3
- package/src/components/silke-alert-banner/silke-alert-banner.scss +9 -10
- package/src/components/silke-autocomplete-field/autocomplete-item.tsx +2 -2
- package/src/components/silke-autocomplete-field/silke-autocomplete-field.scss +18 -12
- package/src/components/silke-autocomplete-field/silke-autocomplete-popup-native.tsx +3 -2
- package/src/components/silke-autocomplete-field/silke-autocomplete-popup.tsx +63 -44
- package/src/components/silke-box/silke-box.mdx +321 -21
- package/src/components/silke-box/silke-box.scss +5 -5
- package/src/components/silke-breadcrumbs/silke-breadcrumb.mdx +147 -9
- package/src/components/silke-breadcrumbs/silke-breadcrumb.scss +3 -3
- package/src/components/silke-button/silke-big-button.scss +1 -1
- package/src/components/silke-button/silke-button.scss +24 -15
- package/src/components/silke-button/silke-dropdown-button.mdx +67 -0
- package/src/components/silke-button/silke-dropdown-button.tsx +40 -122
- package/src/components/silke-card/silke-card-grid.tsx +5 -3
- package/src/components/silke-card/silke-card.scss +8 -7
- package/src/components/silke-card/silke-card.tsx +7 -1
- package/src/components/silke-checkbox/silke-checkbox.mdx +139 -14
- package/src/components/silke-checkbox/silke-checkbox.scss +11 -11
- package/src/components/silke-color-picker/color-picker.scss +8 -8
- package/src/components/silke-color-swatches/silke-color-swatches.mdx +130 -0
- package/src/components/silke-command-text-field/image-utils.ts +71 -0
- package/src/components/silke-command-text-field/index.ts +12 -0
- package/src/components/silke-command-text-field/silke-command-parser.spec.ts +89 -0
- package/src/components/silke-command-text-field/silke-command-parser.ts +47 -0
- package/src/components/silke-command-text-field/silke-command-text-field.mdx +301 -0
- package/src/components/silke-command-text-field/silke-command-text-field.module.scss +119 -0
- package/src/components/silke-command-text-field/silke-command-text-field.tsx +665 -0
- package/src/components/silke-command-text-field/utils.spec.ts +366 -0
- package/src/components/silke-command-text-field/utils.ts +287 -0
- package/src/components/silke-conditions-input/silke-conditions-input.scss +4 -4
- package/src/components/silke-content-column/silke-content-column.scss +1 -1
- package/src/components/silke-context-menu/silke-context-menu.scss +3 -4
- package/src/components/silke-context-menu/silke-context-menu.tsx +1 -1
- package/src/components/silke-css-number-field/silke-css-number-field.scss +14 -12
- package/src/components/silke-divider/silke-divider.mdx +154 -32
- package/src/components/silke-divider/silke-divider.scss +3 -2
- package/src/components/silke-drawer/silke-drawer.scss +6 -2
- package/src/components/silke-form/silke-form.mdx +254 -27
- package/src/components/silke-form-schema/fields/array-field-inline.tsx +1 -0
- package/src/components/silke-form-schema/fields/array-field-modal.tsx +1 -0
- package/src/components/silke-form-schema/fields/styles.scss +34 -35
- package/src/components/silke-form-schema/silke-schema-fields.tsx +4 -3
- package/src/components/silke-gradient-picker/silke-gradient-picker.module.scss +2 -2
- package/src/components/silke-icon/silke-icon.mdx +212 -8
- package/src/components/silke-icon/silke-icon.scss +10 -2
- package/src/components/silke-icon/silke-icon.tsx +1 -0
- package/src/components/silke-icon/silke-stacked-icon.scss +42 -45
- package/src/components/silke-icon-browser/silke-icon-browser.mdx +143 -0
- package/src/components/silke-image/silke-image-color-backdrop.scss +6 -6
- package/src/components/silke-image/silke-image.mdx +189 -41
- package/src/components/silke-image/silke-image.scss +3 -3
- package/src/components/silke-image/silke-image.tsx +14 -1
- package/src/components/silke-link/silke-link.scss +3 -5
- package/src/components/silke-list/index.ts +1 -0
- package/src/components/silke-list/silke-list-item.mdx +83 -0
- package/src/components/silke-list/silke-list-item.scss +119 -0
- package/src/components/silke-list/silke-list-item.tsx +137 -12
- package/src/components/silke-list/silke-list.mdx +108 -13
- package/src/components/silke-list/silke-list.scss +8 -15
- package/src/components/silke-list/silke-list.tsx +63 -9
- package/src/components/silke-list/types.ts +22 -0
- package/src/components/silke-list/utils.ts +24 -0
- package/src/components/silke-loader/silke-loader.mdx +139 -5
- package/src/components/silke-loader/silke-loader.scss +1 -1
- package/src/components/silke-modal/silke-modal.scss +9 -9
- package/src/components/silke-navigation-link/silke-navigation-link.mdx +27 -2
- package/src/components/silke-navigation-link/silke-navigation-link.scss +34 -40
- package/src/components/silke-navigation-link/silke-navigation-link.tsx +7 -6
- package/src/components/silke-overflow-menu/silke-overflow-menu.mdx +228 -31
- package/src/components/silke-overflow-menu/silke-overflow-menu.scss +8 -8
- package/src/components/silke-overflow-menu/silke-overflow-menu.tsx +14 -8
- package/src/components/silke-popover/silke-popover.scss +5 -5
- package/src/components/silke-popover-native/silke-popover-native.scss +93 -78
- package/src/components/silke-popover-native/silke-popover-native.tsx +4 -0
- package/src/components/silke-popover-native/use-popover-positioning.ts +1 -3
- package/src/components/silke-progress-indicator/silke-progress-indicator.scss +1 -1
- package/src/components/silke-radio-button/silke-radio-button.scss +18 -11
- package/src/components/silke-search-field/silke-search-field.mdx +161 -7
- package/src/components/silke-shortcut-button/index.ts +1 -0
- package/src/components/silke-shortcut-button/silke-shortcut-button.mdx +89 -0
- package/src/components/silke-shortcut-button/silke-shortcut-button.scss +145 -0
- package/src/components/silke-shortcut-button/silke-shortcut-button.tsx +55 -0
- package/src/components/silke-skeleton/silke-skeleton.mdx +109 -0
- package/src/components/silke-skeleton/silke-skeleton.scss +1 -1
- package/src/components/silke-slider/silke-slider.module.scss +19 -19
- package/src/components/silke-sortable-list/silke-nested-sortable-list.scss +3 -3
- package/src/components/silke-sortable-list/silke-sortable-list.scss +4 -5
- package/src/components/silke-table/silke-table.scss +1 -1
- package/src/components/silke-tabs/silke-tabs.scss +3 -3
- package/src/components/silke-tag/silke-tag.mdx +179 -43
- package/src/components/silke-tag/silke-tag.scss +49 -12
- package/src/components/silke-tag/silke-tag.tsx +17 -7
- package/src/components/silke-text/silke-text.scss +6 -4
- package/src/components/silke-text/silke-title.scss +7 -14
- package/src/components/silke-text-field/silke-text-field.scss +9 -9
- package/src/components/silke-text-field/silke-text-field.tsx +1 -1
- package/src/components/silke-tile/silke-tile.scss +7 -3
- package/src/components/silke-toast-notification/silke-toast-notification.scss +4 -5
- package/src/components/silke-toggle/silke-toggle.mdx +163 -33
- package/src/components/silke-toggle/silke-toggle.scss +4 -4
- package/src/components/silke-toolbar/silke-toolbar.scss +1 -1
- package/src/components/silke-tooltip/silke-shortcuts.tsx +3 -3
- package/src/components/silke-tooltip/silke-tooltip.scss +11 -16
- package/src/components/silke-variables-wrapper/silke-variables-wrapper.scss +9 -7
- package/src/components/silke-virtual/silke-virtual.mdx +161 -0
- package/src/components/silke-virtual/silke-virtual.scss +8 -8
- package/src/icons.tsx +43 -1
- package/src/styles/core.scss +42 -45
- package/src/styles/math.scss +66 -66
- package/tsconfig.build.json +2 -6
- package/tsconfig.build.tsbuildinfo +1 -1
- package/tsconfig.json +3 -14
- package/dist/types/components/silke-action-list/index.d.ts +0 -1
- package/dist/types/components/silke-action-list/silke-action-list.d.ts +0 -11
- package/dist/types/components/silke-list-card/index.d.ts +0 -1
- package/dist/types/components/silke-list-card/silke-list-card.d.ts +0 -14
- package/src/components/silke-action-list/index.ts +0 -1
- package/src/components/silke-action-list/silke-action-list.mdx +0 -48
- package/src/components/silke-action-list/silke-action-list.scss +0 -21
- package/src/components/silke-action-list/silke-action-list.tsx +0 -85
- package/src/components/silke-list-card/index.ts +0 -1
- package/src/components/silke-list-card/silke-list-card.mdx +0 -46
- package/src/components/silke-list-card/silke-list-card.scss +0 -57
- package/src/components/silke-list-card/silke-list-card.tsx +0 -63
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
import {
|
|
2
|
+
escapeHtml,
|
|
3
|
+
filterAndSortCommands,
|
|
4
|
+
getActiveCommandIndex,
|
|
5
|
+
getCommandMatchScore,
|
|
6
|
+
getItemLength,
|
|
7
|
+
getTextLength,
|
|
8
|
+
isSilkeCommand,
|
|
9
|
+
isText,
|
|
10
|
+
SilkeCommand,
|
|
11
|
+
SilkeCommandTextFieldValue,
|
|
12
|
+
SilkeCommandType,
|
|
13
|
+
valueToHtml,
|
|
14
|
+
convertSilkeCommandsToString,
|
|
15
|
+
} from './utils';
|
|
16
|
+
|
|
17
|
+
describe('escapeHtml', () => {
|
|
18
|
+
test('escapes HTML special characters', () => {
|
|
19
|
+
expect(escapeHtml('<script>alert("xss")</script>')).toBe(
|
|
20
|
+
'<script>alert("xss")</script>',
|
|
21
|
+
);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test('escapes ampersand', () => {
|
|
25
|
+
expect(escapeHtml('foo & bar')).toBe('foo & bar');
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('escapes single quotes', () => {
|
|
29
|
+
expect(escapeHtml("it's")).toBe('it's');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('returns empty string for empty input', () => {
|
|
33
|
+
expect(escapeHtml('')).toBe('');
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('returns text unchanged if no special characters', () => {
|
|
37
|
+
expect(escapeHtml('Hello world 123')).toBe('Hello world 123');
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe('getActiveCommandIndex', () => {
|
|
42
|
+
const testValue: SilkeCommandTextFieldValue[] = [
|
|
43
|
+
{ type: 'text', value: 'Hello ' },
|
|
44
|
+
{ type: SilkeCommandType.MENTION, value: 'user' },
|
|
45
|
+
{ type: 'text', value: ' world' },
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
test('returns 0 for empty array', () => {
|
|
49
|
+
expect(getActiveCommandIndex(0, [])).toBe(0);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('returns 0 for caret at start', () => {
|
|
53
|
+
expect(getActiveCommandIndex(0, testValue)).toBe(0);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test('returns correct index for caret in text segment', () => {
|
|
57
|
+
expect(getActiveCommandIndex(3, testValue)).toBe(0);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('returns command index when caret at boundary (start of command)', () => {
|
|
61
|
+
// Position 6 is at '@', the first char of the command
|
|
62
|
+
expect(getActiveCommandIndex(6, testValue)).toBe(1);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test('returns correct index for caret at command', () => {
|
|
66
|
+
// "Hello " = 6 chars, "@user" = 5 chars (@ + user), caret at 8 is inside @user
|
|
67
|
+
expect(getActiveCommandIndex(8, testValue)).toBe(1);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('returns correct index for caret after command', () => {
|
|
71
|
+
// "Hello " (6) + "@user" (5) + " " (1) = 12, caret at 13 is in " world"
|
|
72
|
+
expect(getActiveCommandIndex(13, testValue)).toBe(2);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test('returns last index for caret at end', () => {
|
|
76
|
+
// Total: "Hello " (6) + "@user" (5) + " world" (6) = 17
|
|
77
|
+
expect(getActiveCommandIndex(17, testValue)).toBe(2);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('handles undefined value array', () => {
|
|
81
|
+
expect(getActiveCommandIndex(0, undefined as unknown as SilkeCommandTextFieldValue[])).toBe(0);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
describe('getTextLength', () => {
|
|
86
|
+
const testValue: SilkeCommandTextFieldValue[] = [
|
|
87
|
+
{ type: 'text', value: 'Hello ' },
|
|
88
|
+
{ type: SilkeCommandType.MENTION, value: 'user' },
|
|
89
|
+
{ type: 'text', value: ' world' },
|
|
90
|
+
];
|
|
91
|
+
|
|
92
|
+
test('returns 0 for empty array', () => {
|
|
93
|
+
expect(getTextLength([])).toBe(0);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test('returns 0 for index 0', () => {
|
|
97
|
+
expect(getTextLength(testValue, 0)).toBe(0);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test('returns correct length for text segment', () => {
|
|
101
|
+
expect(getTextLength(testValue, 1)).toBe(6); // "Hello "
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test('returns correct length including command (with @ symbol)', () => {
|
|
105
|
+
expect(getTextLength(testValue, 2)).toBe(11); // "Hello " + "@user"
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test('returns total length for full array', () => {
|
|
109
|
+
expect(getTextLength(testValue)).toBe(17); // "Hello " + "@user" + " world"
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test('handles negative index', () => {
|
|
113
|
+
expect(getTextLength(testValue, -1)).toBe(0);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test('handles undefined value array', () => {
|
|
117
|
+
expect(getTextLength(undefined as unknown as SilkeCommandTextFieldValue[])).toBe(0);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
describe('getItemLength', () => {
|
|
122
|
+
test('returns correct length for text', () => {
|
|
123
|
+
expect(getItemLength({ type: 'text', value: 'hello' })).toBe(5);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test('returns correct length for command (value + 1 for symbol)', () => {
|
|
127
|
+
expect(getItemLength({ type: SilkeCommandType.MENTION, value: 'user' })).toBe(5);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test('returns 0 for undefined', () => {
|
|
131
|
+
expect(getItemLength(undefined as unknown as SilkeCommandTextFieldValue)).toBe(0);
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
describe('valueToHtml', () => {
|
|
136
|
+
const mockStyles = {
|
|
137
|
+
action: 'action-class',
|
|
138
|
+
mention: 'mention-class',
|
|
139
|
+
partial: 'partial-class',
|
|
140
|
+
tag: 'tag-class',
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
test('returns empty string for empty array', () => {
|
|
144
|
+
expect(valueToHtml([], mockStyles)).toBe('');
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
test('wraps text in span', () => {
|
|
148
|
+
const value: SilkeCommandTextFieldValue[] = [{ type: 'text', value: 'hello' }];
|
|
149
|
+
expect(valueToHtml(value, mockStyles)).toBe('<span>hello</span>');
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test('renders mention command with correct classes', () => {
|
|
153
|
+
const value: SilkeCommandTextFieldValue[] = [{ type: SilkeCommandType.MENTION, value: 'user' }];
|
|
154
|
+
const html = valueToHtml(value, mockStyles);
|
|
155
|
+
expect(html).toContain('tag-class');
|
|
156
|
+
expect(html).toContain('mention-class');
|
|
157
|
+
expect(html).toContain('@user');
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test('renders action command with correct classes', () => {
|
|
161
|
+
const value: SilkeCommandTextFieldValue[] = [{ type: SilkeCommandType.ACTION, value: 'cmd' }];
|
|
162
|
+
const html = valueToHtml(value, mockStyles);
|
|
163
|
+
expect(html).toContain('tag-class');
|
|
164
|
+
expect(html).toContain('action-class');
|
|
165
|
+
expect(html).toContain('/cmd');
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
test('adds partial class for partial commands', () => {
|
|
169
|
+
const value: SilkeCommandTextFieldValue[] = [
|
|
170
|
+
{ partialMatch: true, type: SilkeCommandType.MENTION, value: 'unknown' },
|
|
171
|
+
];
|
|
172
|
+
const html = valueToHtml(value, mockStyles);
|
|
173
|
+
expect(html).toContain('partial-class');
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test('escapes HTML in text values', () => {
|
|
177
|
+
const value: SilkeCommandTextFieldValue[] = [
|
|
178
|
+
{ type: 'text', value: '<script>alert("xss")</script>' },
|
|
179
|
+
];
|
|
180
|
+
const html = valueToHtml(value, mockStyles);
|
|
181
|
+
expect(html).not.toContain('<script>');
|
|
182
|
+
expect(html).toContain('<script>');
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test('escapes HTML in command values', () => {
|
|
186
|
+
const value: SilkeCommandTextFieldValue[] = [
|
|
187
|
+
{ partialMatch: true, type: SilkeCommandType.MENTION, value: '<img onerror=alert(1)>' },
|
|
188
|
+
];
|
|
189
|
+
const html = valueToHtml(value, mockStyles);
|
|
190
|
+
expect(html).not.toContain('<img');
|
|
191
|
+
expect(html).toContain('<img');
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test('handles undefined value array', () => {
|
|
195
|
+
expect(valueToHtml(undefined as unknown as SilkeCommandTextFieldValue[], mockStyles)).toBe('');
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
describe('valueToString', () => {
|
|
200
|
+
test('returns empty string for empty array', () => {
|
|
201
|
+
expect(convertSilkeCommandsToString([])).toBe('');
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test('converts text segments correctly', () => {
|
|
205
|
+
const value: SilkeCommandTextFieldValue[] = [{ type: 'text', value: 'hello world' }];
|
|
206
|
+
expect(convertSilkeCommandsToString(value)).toBe('hello world');
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
test('converts commands with their symbol', () => {
|
|
210
|
+
const value: SilkeCommandTextFieldValue[] = [{ type: SilkeCommandType.MENTION, value: 'user' }];
|
|
211
|
+
expect(convertSilkeCommandsToString(value)).toBe('@user');
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test('converts mixed content correctly', () => {
|
|
215
|
+
const value: SilkeCommandTextFieldValue[] = [
|
|
216
|
+
{ type: 'text', value: 'Hello ' },
|
|
217
|
+
{ type: SilkeCommandType.MENTION, value: 'user' },
|
|
218
|
+
{ type: 'text', value: ' please /run' },
|
|
219
|
+
];
|
|
220
|
+
expect(convertSilkeCommandsToString(value)).toBe('Hello @user please /run');
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
test('handles undefined value array', () => {
|
|
224
|
+
expect(convertSilkeCommandsToString(undefined as unknown as SilkeCommandTextFieldValue[])).toBe(
|
|
225
|
+
'',
|
|
226
|
+
);
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
describe('isCommand', () => {
|
|
231
|
+
test('returns true for mention command', () => {
|
|
232
|
+
expect(isSilkeCommand({ type: SilkeCommandType.MENTION, value: 'user' })).toBe(true);
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
test('returns true for action command', () => {
|
|
236
|
+
expect(isSilkeCommand({ type: SilkeCommandType.ACTION, value: 'cmd' })).toBe(true);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
test('returns false for text', () => {
|
|
240
|
+
expect(isSilkeCommand({ type: 'text', value: 'hello' })).toBe(false);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
test('returns false for undefined', () => {
|
|
244
|
+
expect(isSilkeCommand(undefined)).toBe(false);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
test('returns false for null', () => {
|
|
248
|
+
expect(isSilkeCommand(null as unknown as SilkeCommandTextFieldValue)).toBe(false);
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
describe('isText', () => {
|
|
253
|
+
test('returns true for text', () => {
|
|
254
|
+
expect(isText({ type: 'text', value: 'hello' })).toBe(true);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
test('returns false for mention command', () => {
|
|
258
|
+
expect(isText({ type: SilkeCommandType.MENTION, value: 'user' })).toBe(false);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
test('returns false for action command', () => {
|
|
262
|
+
expect(isText({ type: SilkeCommandType.ACTION, value: 'cmd' })).toBe(false);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
test('returns false for undefined', () => {
|
|
266
|
+
expect(isText(undefined)).toBe(false);
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
test('returns false for null', () => {
|
|
270
|
+
expect(isText(null as unknown as SilkeCommandTextFieldValue)).toBe(false);
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
describe('getCommandMatchScore', () => {
|
|
275
|
+
const createCommand = (value: string): SilkeCommand => ({
|
|
276
|
+
type: SilkeCommandType.MENTION,
|
|
277
|
+
value,
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
test('returns -1 for empty query', () => {
|
|
281
|
+
expect(getCommandMatchScore(createCommand('user'), '')).toBe(-1);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
test('returns highest score for exact match', () => {
|
|
285
|
+
expect(getCommandMatchScore(createCommand('user'), 'user')).toBe(100);
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
test('returns high score for startsWith match', () => {
|
|
289
|
+
const score = getCommandMatchScore(createCommand('username'), 'user');
|
|
290
|
+
expect(score).toBeGreaterThan(80);
|
|
291
|
+
expect(score).toBeLessThan(100);
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
test('returns medium-high score for word boundary match', () => {
|
|
295
|
+
const score = getCommandMatchScore(createCommand('my-user'), 'user');
|
|
296
|
+
expect(score).toBeGreaterThan(60);
|
|
297
|
+
expect(score).toBeLessThan(80);
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
test('returns medium score for contains match', () => {
|
|
301
|
+
const score = getCommandMatchScore(createCommand('superuser'), 'user');
|
|
302
|
+
expect(score).toBeGreaterThan(40);
|
|
303
|
+
expect(score).toBeLessThan(60);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
test('returns low score for fuzzy match', () => {
|
|
307
|
+
const score = getCommandMatchScore(createCommand('user'), 'ur');
|
|
308
|
+
expect(score).toBeGreaterThanOrEqual(20);
|
|
309
|
+
expect(score).toBeLessThan(40);
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
test('returns -1 for no match', () => {
|
|
313
|
+
expect(getCommandMatchScore(createCommand('user'), 'xyz')).toBe(-1);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
test('is case insensitive', () => {
|
|
317
|
+
expect(getCommandMatchScore(createCommand('User'), 'user')).toBe(100);
|
|
318
|
+
expect(getCommandMatchScore(createCommand('user'), 'USER')).toBe(100);
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
describe('filterAndSortCommands', () => {
|
|
323
|
+
const commands: SilkeCommand[] = [
|
|
324
|
+
{ type: SilkeCommandType.MENTION, value: 'alice' },
|
|
325
|
+
{ type: SilkeCommandType.MENTION, value: 'bob' },
|
|
326
|
+
{ type: SilkeCommandType.MENTION, value: 'alice-smith' },
|
|
327
|
+
{ type: SilkeCommandType.MENTION, value: 'charlie' },
|
|
328
|
+
{ type: SilkeCommandType.ACTION, value: 'run' },
|
|
329
|
+
];
|
|
330
|
+
|
|
331
|
+
test('returns all commands of type sorted alphabetically for empty query', () => {
|
|
332
|
+
const results = filterAndSortCommands(commands, '', SilkeCommandType.MENTION);
|
|
333
|
+
expect(results).toHaveLength(4);
|
|
334
|
+
expect(results[0].value).toBe('alice');
|
|
335
|
+
expect(results[1].value).toBe('alice-smith');
|
|
336
|
+
expect(results[2].value).toBe('bob');
|
|
337
|
+
expect(results[3].value).toBe('charlie');
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
test('filters by command type', () => {
|
|
341
|
+
const results = filterAndSortCommands(commands, 'r', SilkeCommandType.ACTION);
|
|
342
|
+
expect(results).toHaveLength(1);
|
|
343
|
+
expect(results[0].value).toBe('run');
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
test('sorts exact matches first', () => {
|
|
347
|
+
const results = filterAndSortCommands(commands, 'alice', SilkeCommandType.MENTION);
|
|
348
|
+
expect(results[0].value).toBe('alice');
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
test('sorts startsWith before contains', () => {
|
|
352
|
+
const results = filterAndSortCommands(commands, 'ali', SilkeCommandType.MENTION);
|
|
353
|
+
expect(results[0].value).toBe('alice');
|
|
354
|
+
expect(results[1].value).toBe('alice-smith');
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
test('returns partial matches', () => {
|
|
358
|
+
const results = filterAndSortCommands(commands, 'ice', SilkeCommandType.MENTION);
|
|
359
|
+
expect(results.length).toBeGreaterThan(0);
|
|
360
|
+
expect(results.some((r) => r.value === 'alice')).toBe(true);
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
test('returns empty array when no matches', () => {
|
|
364
|
+
expect(filterAndSortCommands(commands, 'xyz', SilkeCommandType.MENTION)).toEqual([]);
|
|
365
|
+
});
|
|
366
|
+
});
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
export enum SilkeCommandType {
|
|
2
|
+
ACTION = '/',
|
|
3
|
+
MENTION = '@',
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export type SilkeCommand = {
|
|
7
|
+
partialMatch?: boolean;
|
|
8
|
+
type: SilkeCommandType;
|
|
9
|
+
value: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type SilkeTextValue = {
|
|
13
|
+
type: 'text';
|
|
14
|
+
value: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type SilkeImageValue = {
|
|
18
|
+
type: 'image';
|
|
19
|
+
/** Base64-encoded image data */
|
|
20
|
+
data: string;
|
|
21
|
+
/** MIME type (e.g., 'image/png', 'image/jpeg') */
|
|
22
|
+
mimeType: string;
|
|
23
|
+
/** Optional filename */
|
|
24
|
+
name?: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type SilkeCommandTextFieldValue = SilkeCommand | SilkeTextValue | SilkeImageValue;
|
|
28
|
+
|
|
29
|
+
/** Key direction mappings for navigation and editing */
|
|
30
|
+
export const KEY_DIRECTION: Record<string, number> = {
|
|
31
|
+
ArrowLeft: -1,
|
|
32
|
+
ArrowRight: 1,
|
|
33
|
+
Backspace: -1,
|
|
34
|
+
Delete: 1,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** Get the caret offset within a contentEditable element */
|
|
38
|
+
export function getCaretOffset(element: HTMLDivElement | null): number {
|
|
39
|
+
if (!element) return 0;
|
|
40
|
+
|
|
41
|
+
const selection = window.getSelection();
|
|
42
|
+
if (!selection || selection.rangeCount === 0) return 0;
|
|
43
|
+
|
|
44
|
+
const range = selection.getRangeAt(0);
|
|
45
|
+
const preCaretRange = range.cloneRange();
|
|
46
|
+
preCaretRange.selectNodeContents(element);
|
|
47
|
+
preCaretRange.setEnd(range.endContainer, range.endOffset);
|
|
48
|
+
|
|
49
|
+
return preCaretRange.toString().length;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Check if there is a text selection with length > 0 */
|
|
53
|
+
export function hasSelectionLength(): boolean {
|
|
54
|
+
const selection = window.getSelection();
|
|
55
|
+
if (!selection || selection.rangeCount === 0) return false;
|
|
56
|
+
|
|
57
|
+
const range = selection.getRangeAt(0);
|
|
58
|
+
return range.toString().length > 0;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Set the caret to a specific character offset within a contentEditable element */
|
|
62
|
+
export function setCaretOffset(element: HTMLDivElement | null, offset: number): void {
|
|
63
|
+
if (!element) return;
|
|
64
|
+
|
|
65
|
+
const selection = window.getSelection();
|
|
66
|
+
if (!selection) return;
|
|
67
|
+
|
|
68
|
+
const range = document.createRange();
|
|
69
|
+
let currentPos = 0;
|
|
70
|
+
const clampedOffset = Math.max(0, offset);
|
|
71
|
+
|
|
72
|
+
const traverseNodes = (node: Node): boolean => {
|
|
73
|
+
if (node.nodeType === Node.TEXT_NODE) {
|
|
74
|
+
const textNode = node as Text;
|
|
75
|
+
const nextPos = currentPos + textNode.length;
|
|
76
|
+
|
|
77
|
+
if (clampedOffset <= nextPos) {
|
|
78
|
+
range.setStart(node, clampedOffset - currentPos);
|
|
79
|
+
range.collapse(true);
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
currentPos = nextPos;
|
|
83
|
+
} else {
|
|
84
|
+
for (const child of node.childNodes) {
|
|
85
|
+
if (traverseNodes(child)) return true;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return false;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
if (traverseNodes(element)) {
|
|
92
|
+
selection.removeAllRanges();
|
|
93
|
+
selection.addRange(range);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Escape HTML special characters to prevent XSS */
|
|
98
|
+
export function escapeHtml(text: string): string {
|
|
99
|
+
const htmlEscapes: Record<string, string> = {
|
|
100
|
+
'&': '&',
|
|
101
|
+
'<': '<',
|
|
102
|
+
'>': '>',
|
|
103
|
+
'"': '"',
|
|
104
|
+
"'": ''',
|
|
105
|
+
};
|
|
106
|
+
return text.replace(/[&<>"']/g, (char) => htmlEscapes[char]);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Get the index of the command at a given caret position */
|
|
110
|
+
export function getActiveCommandIndex(
|
|
111
|
+
caretPos: number,
|
|
112
|
+
value: SilkeCommandTextFieldValue[],
|
|
113
|
+
): number {
|
|
114
|
+
if (!value || value.length === 0) return 0;
|
|
115
|
+
|
|
116
|
+
let textLength = 0;
|
|
117
|
+
|
|
118
|
+
for (let i = 0; i < value.length; i++) {
|
|
119
|
+
const item = value[i];
|
|
120
|
+
if (item.type === 'image') continue; // Images have no text length
|
|
121
|
+
textLength += item.value?.length || 0;
|
|
122
|
+
if (item.type !== 'text') textLength++;
|
|
123
|
+
if (textLength > caretPos) return i;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return value.length - 1;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Calculate total text length up to a given index */
|
|
130
|
+
export function getTextLength(value: SilkeCommandTextFieldValue[], index?: number): number {
|
|
131
|
+
if (!value || value.length === 0) return 0;
|
|
132
|
+
|
|
133
|
+
const endIndex = index ?? value.length;
|
|
134
|
+
if (endIndex <= 0) return 0;
|
|
135
|
+
|
|
136
|
+
return value.slice(0, endIndex).reduce((acc, curr) => {
|
|
137
|
+
if (curr.type === 'image') return acc; // Images have no text length
|
|
138
|
+
return acc + (curr.type === 'text' ? curr.value.length : curr.value.length + 1);
|
|
139
|
+
}, 0);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Get the rendered length of a single command/text value */
|
|
143
|
+
export function getItemLength(item: SilkeCommandTextFieldValue): number {
|
|
144
|
+
if (!item || item.type === 'image') return 0;
|
|
145
|
+
return item.type === 'text' ? item.value.length : item.value.length + 1;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Convert command values to HTML for rendering (excludes images) */
|
|
149
|
+
export function valueToHtml(
|
|
150
|
+
value: SilkeCommandTextFieldValue[],
|
|
151
|
+
styles: Record<string, string>,
|
|
152
|
+
): string {
|
|
153
|
+
if (!value || value.length === 0) return '';
|
|
154
|
+
|
|
155
|
+
return value
|
|
156
|
+
.filter((item): item is SilkeCommand | SilkeTextValue => item.type !== 'image')
|
|
157
|
+
.map((item) => {
|
|
158
|
+
const escapedValue = escapeHtml(item.value);
|
|
159
|
+
|
|
160
|
+
if (item.type === 'text') {
|
|
161
|
+
// Preserve spaces by replacing them with non-breaking spaces in space-only segments
|
|
162
|
+
// This prevents browser whitespace collapsing
|
|
163
|
+
const preservedValue =
|
|
164
|
+
item.value.trim() === '' ? item.value.replace(/ /g, '\u00A0') : escapedValue;
|
|
165
|
+
return `<span>${preservedValue}</span>`;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const typeClass = item.type === SilkeCommandType.MENTION ? styles.mention : styles.action;
|
|
169
|
+
const partialClass = item.partialMatch ? styles.partial : '';
|
|
170
|
+
return `<span class="${styles.tag} ${typeClass} ${partialClass}">${item.type}${escapedValue}</span>`;
|
|
171
|
+
})
|
|
172
|
+
.join('');
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Convert command values to plain string (excludes images) */
|
|
176
|
+
export function convertSilkeCommandsToString(value: SilkeCommandTextFieldValue[]): string {
|
|
177
|
+
if (!value) return '';
|
|
178
|
+
return value
|
|
179
|
+
.filter((v): v is SilkeCommand | SilkeTextValue => v.type !== 'image')
|
|
180
|
+
.map((v) => (v.type === 'text' ? v.value : v.type + v.value))
|
|
181
|
+
.join('');
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** Check if a command value is a non-text, non-image command */
|
|
185
|
+
export function isSilkeCommand(
|
|
186
|
+
value: SilkeCommandTextFieldValue | undefined,
|
|
187
|
+
): value is SilkeCommand {
|
|
188
|
+
return value !== null && value !== undefined && value.type !== 'text' && value.type !== 'image';
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** Check if a command value is a text segment */
|
|
192
|
+
export function isText(value: SilkeCommandTextFieldValue | undefined): value is SilkeTextValue {
|
|
193
|
+
return value !== null && value !== undefined && value.type === 'text';
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Check if a command value is an image */
|
|
197
|
+
export function isImage(value: SilkeCommandTextFieldValue | undefined): value is SilkeImageValue {
|
|
198
|
+
return value !== null && value !== undefined && value.type === 'image';
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Calculate a match score for a command against a search query.
|
|
203
|
+
* Higher scores indicate better matches.
|
|
204
|
+
* Returns -1 if there's no match.
|
|
205
|
+
*/
|
|
206
|
+
export function getCommandMatchScore(command: SilkeCommand, query: string): number {
|
|
207
|
+
if (!query || !command.value) return -1;
|
|
208
|
+
|
|
209
|
+
const queryLower = query.toLowerCase();
|
|
210
|
+
const valueLower = command.value.toLowerCase();
|
|
211
|
+
|
|
212
|
+
// Exact match (highest priority)
|
|
213
|
+
if (valueLower === queryLower) return 100;
|
|
214
|
+
|
|
215
|
+
// Starts with (high priority)
|
|
216
|
+
if (valueLower.startsWith(queryLower)) {
|
|
217
|
+
// Shorter matches score higher (more specific)
|
|
218
|
+
return 80 + (queryLower.length / valueLower.length) * 10;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// Contains as word boundary (medium-high priority)
|
|
222
|
+
// e.g., "test" matches "run-test" or "run_test"
|
|
223
|
+
const wordBoundaryRegex = new RegExp(`(?:^|[-_\\s])${escapeRegExp(queryLower)}`);
|
|
224
|
+
if (wordBoundaryRegex.test(valueLower)) {
|
|
225
|
+
return 60 + (queryLower.length / valueLower.length) * 10;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// Contains anywhere (medium priority)
|
|
229
|
+
const containsIndex = valueLower.indexOf(queryLower);
|
|
230
|
+
if (containsIndex !== -1) {
|
|
231
|
+
// Earlier matches score higher
|
|
232
|
+
return 40 + (1 - containsIndex / valueLower.length) * 10;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// Fuzzy match - all query characters appear in order (low priority)
|
|
236
|
+
if (fuzzyMatch(queryLower, valueLower)) {
|
|
237
|
+
return 20;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return -1;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** Escape special regex characters */
|
|
244
|
+
function escapeRegExp(string: string): string {
|
|
245
|
+
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Check if all characters in query appear in value in order */
|
|
249
|
+
function fuzzyMatch(query: string, value: string): boolean {
|
|
250
|
+
let queryIndex = 0;
|
|
251
|
+
for (let i = 0; i < value.length && queryIndex < query.length; i++) {
|
|
252
|
+
if (value[i] === query[queryIndex]) {
|
|
253
|
+
queryIndex++;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return queryIndex === query.length;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Filter and sort commands based on partial matching against a query.
|
|
261
|
+
* Returns commands sorted by match quality (best matches first).
|
|
262
|
+
* If query is empty, returns all commands of the given type sorted alphabetically.
|
|
263
|
+
*/
|
|
264
|
+
export function filterAndSortCommands(
|
|
265
|
+
commands: SilkeCommand[],
|
|
266
|
+
query: string,
|
|
267
|
+
type: SilkeCommandType,
|
|
268
|
+
): SilkeCommand[] {
|
|
269
|
+
const typeCommands = commands.filter((cmd) => cmd.type === type);
|
|
270
|
+
|
|
271
|
+
// If no query, return all commands of this type sorted alphabetically
|
|
272
|
+
if (!query) {
|
|
273
|
+
return [...typeCommands].sort((a, b) => a.value.localeCompare(b.value));
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
const scored = typeCommands
|
|
277
|
+
.map((cmd) => ({ cmd, score: getCommandMatchScore(cmd, query) }))
|
|
278
|
+
.filter(({ score }) => score >= 0);
|
|
279
|
+
|
|
280
|
+
// Sort by score descending, then alphabetically for ties
|
|
281
|
+
scored.sort((a, b) => {
|
|
282
|
+
if (b.score !== a.score) return b.score - a.score;
|
|
283
|
+
return a.cmd.value.localeCompare(b.cmd.value);
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
return scored.map(({ cmd }) => cmd);
|
|
287
|
+
}
|
|
@@ -19,11 +19,11 @@
|
|
|
19
19
|
padding: 0;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
.field[data-hidden=
|
|
22
|
+
.field[data-hidden='true'] {
|
|
23
23
|
position: absolute;
|
|
24
|
-
clip: rect(0,0,0,0);
|
|
24
|
+
clip: rect(0, 0, 0, 0);
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
.field[data-empty=
|
|
27
|
+
.field[data-empty='true'] {
|
|
28
28
|
width: 100px;
|
|
29
|
-
}
|
|
29
|
+
}
|
|
@@ -1,20 +1,19 @@
|
|
|
1
|
-
.menu{
|
|
1
|
+
.menu {
|
|
2
2
|
border-radius: var(--border-radius-small);
|
|
3
3
|
overflow: hidden;
|
|
4
4
|
}
|
|
5
5
|
|
|
6
6
|
.item {
|
|
7
7
|
font-size: 12px;
|
|
8
|
-
|
|
8
|
+
|
|
9
9
|
&:not(.leftIcon) {
|
|
10
10
|
justify-content: space-between !important;
|
|
11
|
-
|
|
11
|
+
|
|
12
12
|
i {
|
|
13
13
|
font-size: 10px;
|
|
14
14
|
color: var(--color-neutral-70);
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
|
-
|
|
18
17
|
}
|
|
19
18
|
|
|
20
19
|
.selected {
|