@vev/silke 1.1.6 → 1.1.8

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.
Files changed (164) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/index.js +34 -34
  3. package/dist/index.js.LEGAL.txt +12 -12
  4. package/dist/styles.css +4416 -4020
  5. package/dist/types/components/index.d.ts +3 -2
  6. package/dist/types/components/silke-button/silke-dropdown-button.d.ts +8 -13
  7. package/dist/types/components/silke-card/silke-card-grid.d.ts +33 -1
  8. package/dist/types/components/silke-command-text-field/image-utils.d.ts +20 -0
  9. package/dist/types/components/silke-command-text-field/index.d.ts +4 -0
  10. package/dist/types/components/silke-command-text-field/silke-command-parser.d.ts +3 -0
  11. package/dist/types/components/silke-command-text-field/silke-command-parser.spec.d.ts +1 -0
  12. package/dist/types/components/silke-command-text-field/silke-command-text-field.d.ts +46 -0
  13. package/dist/types/components/silke-command-text-field/utils.d.ts +61 -0
  14. package/dist/types/components/silke-command-text-field/utils.spec.d.ts +1 -0
  15. package/dist/types/components/silke-conditions-input/utils.d.ts +1 -1
  16. package/dist/types/components/silke-form-schema/fields/color-field.d.ts +3 -0
  17. package/dist/types/components/silke-form-schema/schema-utils.d.ts +6 -1
  18. package/dist/types/components/silke-form-schema/silke-schema-fields.d.ts +1 -0
  19. package/dist/types/components/silke-image/silke-image.d.ts +2 -1
  20. package/dist/types/components/silke-list/index.d.ts +1 -0
  21. package/dist/types/components/silke-list/silke-list-item.d.ts +24 -6
  22. package/dist/types/components/silke-list/silke-list.d.ts +9 -9
  23. package/dist/types/components/silke-list/types.d.ts +19 -0
  24. package/dist/types/components/silke-list/utils.d.ts +3 -0
  25. package/dist/types/components/silke-navigation-link/silke-navigation-link.d.ts +1 -0
  26. package/dist/types/components/silke-overflow-menu/silke-overflow-menu.d.ts +6 -1
  27. package/dist/types/components/silke-popover-native/silke-popover-native.d.ts +3 -1
  28. package/dist/types/components/silke-shortcut-button/index.d.ts +1 -0
  29. package/dist/types/components/silke-shortcut-button/silke-shortcut-button.d.ts +15 -0
  30. package/dist/types/components/silke-tag/silke-tag.d.ts +1 -1
  31. package/dist/types/icons.d.ts +32 -0
  32. package/jest.config.js +2 -3
  33. package/package.json +7 -2
  34. package/src/_variables.scss +125 -129
  35. package/src/components/index.ts +3 -2
  36. package/src/components/silke-action-box/silke-action-box.scss +2 -3
  37. package/src/components/silke-alert-banner/silke-alert-banner.scss +9 -10
  38. package/src/components/silke-autocomplete-field/autocomplete-item.tsx +2 -2
  39. package/src/components/silke-autocomplete-field/silke-autocomplete-field.scss +18 -12
  40. package/src/components/silke-autocomplete-field/silke-autocomplete-popup-native.tsx +3 -2
  41. package/src/components/silke-autocomplete-field/silke-autocomplete-popup.tsx +63 -44
  42. package/src/components/silke-box/silke-box.mdx +321 -21
  43. package/src/components/silke-box/silke-box.scss +5 -5
  44. package/src/components/silke-breadcrumbs/silke-breadcrumb.mdx +147 -9
  45. package/src/components/silke-breadcrumbs/silke-breadcrumb.scss +3 -3
  46. package/src/components/silke-button/silke-big-button.scss +1 -1
  47. package/src/components/silke-button/silke-button.scss +24 -15
  48. package/src/components/silke-button/silke-dropdown-button.mdx +67 -0
  49. package/src/components/silke-button/silke-dropdown-button.tsx +40 -122
  50. package/src/components/silke-card/silke-card-grid.tsx +5 -3
  51. package/src/components/silke-card/silke-card.scss +8 -7
  52. package/src/components/silke-card/silke-card.tsx +7 -1
  53. package/src/components/silke-checkbox/silke-checkbox.mdx +139 -14
  54. package/src/components/silke-checkbox/silke-checkbox.scss +11 -11
  55. package/src/components/silke-color-picker/color-picker.scss +8 -8
  56. package/src/components/silke-color-swatches/silke-color-swatches.mdx +130 -0
  57. package/src/components/silke-command-text-field/image-utils.ts +71 -0
  58. package/src/components/silke-command-text-field/index.ts +12 -0
  59. package/src/components/silke-command-text-field/silke-command-parser.spec.ts +89 -0
  60. package/src/components/silke-command-text-field/silke-command-parser.ts +47 -0
  61. package/src/components/silke-command-text-field/silke-command-text-field.mdx +301 -0
  62. package/src/components/silke-command-text-field/silke-command-text-field.module.scss +119 -0
  63. package/src/components/silke-command-text-field/silke-command-text-field.tsx +672 -0
  64. package/src/components/silke-command-text-field/utils.spec.ts +366 -0
  65. package/src/components/silke-command-text-field/utils.ts +287 -0
  66. package/src/components/silke-conditions-input/silke-conditions-input.scss +4 -4
  67. package/src/components/silke-content-column/silke-content-column.scss +1 -1
  68. package/src/components/silke-context-menu/silke-context-menu.scss +3 -4
  69. package/src/components/silke-context-menu/silke-context-menu.tsx +1 -1
  70. package/src/components/silke-css-number-field/silke-css-number-field.scss +14 -12
  71. package/src/components/silke-divider/silke-divider.mdx +154 -32
  72. package/src/components/silke-divider/silke-divider.scss +3 -2
  73. package/src/components/silke-drawer/silke-drawer.scss +6 -2
  74. package/src/components/silke-form/silke-form.mdx +254 -27
  75. package/src/components/silke-form-schema/fields/array-field-inline.tsx +1 -0
  76. package/src/components/silke-form-schema/fields/array-field-modal.tsx +9 -4
  77. package/src/components/silke-form-schema/fields/array-field.tsx +27 -3
  78. package/src/components/silke-form-schema/fields/color-field.tsx +36 -0
  79. package/src/components/silke-form-schema/fields/styles.scss +34 -35
  80. package/src/components/silke-form-schema/schema-utils.ts +27 -1
  81. package/src/components/silke-form-schema/silke-schema-field.tsx +2 -0
  82. package/src/components/silke-form-schema/silke-schema-fields.tsx +4 -3
  83. package/src/components/silke-gradient-picker/silke-gradient-picker.module.scss +2 -2
  84. package/src/components/silke-icon/silke-icon.mdx +212 -8
  85. package/src/components/silke-icon/silke-icon.scss +10 -2
  86. package/src/components/silke-icon/silke-icon.tsx +1 -0
  87. package/src/components/silke-icon/silke-stacked-icon.scss +42 -45
  88. package/src/components/silke-icon-browser/silke-icon-browser.mdx +143 -0
  89. package/src/components/silke-image/silke-image-color-backdrop.scss +6 -6
  90. package/src/components/silke-image/silke-image.mdx +189 -41
  91. package/src/components/silke-image/silke-image.scss +3 -3
  92. package/src/components/silke-image/silke-image.tsx +14 -1
  93. package/src/components/silke-link/silke-link.scss +3 -5
  94. package/src/components/silke-list/index.ts +1 -0
  95. package/src/components/silke-list/silke-list-item.mdx +83 -0
  96. package/src/components/silke-list/silke-list-item.scss +119 -0
  97. package/src/components/silke-list/silke-list-item.tsx +137 -12
  98. package/src/components/silke-list/silke-list.mdx +108 -13
  99. package/src/components/silke-list/silke-list.scss +8 -15
  100. package/src/components/silke-list/silke-list.tsx +63 -9
  101. package/src/components/silke-list/types.ts +22 -0
  102. package/src/components/silke-list/utils.ts +24 -0
  103. package/src/components/silke-loader/silke-loader.mdx +139 -5
  104. package/src/components/silke-loader/silke-loader.scss +1 -1
  105. package/src/components/silke-modal/silke-modal.scss +9 -9
  106. package/src/components/silke-navigation-link/silke-navigation-link.mdx +27 -2
  107. package/src/components/silke-navigation-link/silke-navigation-link.scss +34 -40
  108. package/src/components/silke-navigation-link/silke-navigation-link.tsx +7 -6
  109. package/src/components/silke-overflow-menu/silke-overflow-menu.mdx +228 -31
  110. package/src/components/silke-overflow-menu/silke-overflow-menu.scss +8 -8
  111. package/src/components/silke-overflow-menu/silke-overflow-menu.tsx +14 -8
  112. package/src/components/silke-popover/silke-popover.scss +5 -5
  113. package/src/components/silke-popover-native/silke-popover-native.scss +93 -78
  114. package/src/components/silke-popover-native/silke-popover-native.tsx +4 -0
  115. package/src/components/silke-popover-native/use-popover-positioning.ts +1 -3
  116. package/src/components/silke-progress-indicator/silke-progress-indicator.scss +1 -1
  117. package/src/components/silke-radio-button/silke-radio-button.scss +18 -11
  118. package/src/components/silke-search-field/silke-search-field.mdx +161 -7
  119. package/src/components/silke-shortcut-button/index.ts +1 -0
  120. package/src/components/silke-shortcut-button/silke-shortcut-button.mdx +89 -0
  121. package/src/components/silke-shortcut-button/silke-shortcut-button.scss +145 -0
  122. package/src/components/silke-shortcut-button/silke-shortcut-button.tsx +55 -0
  123. package/src/components/silke-skeleton/silke-skeleton.mdx +109 -0
  124. package/src/components/silke-skeleton/silke-skeleton.scss +1 -1
  125. package/src/components/silke-slider/silke-slider.module.scss +19 -19
  126. package/src/components/silke-sortable-list/silke-nested-sortable-list.scss +3 -3
  127. package/src/components/silke-sortable-list/silke-sortable-list.scss +4 -5
  128. package/src/components/silke-table/silke-table.scss +1 -1
  129. package/src/components/silke-tabs/silke-tabs.scss +3 -3
  130. package/src/components/silke-tag/silke-tag.mdx +179 -43
  131. package/src/components/silke-tag/silke-tag.scss +50 -13
  132. package/src/components/silke-tag/silke-tag.tsx +17 -7
  133. package/src/components/silke-text/silke-text.scss +6 -4
  134. package/src/components/silke-text/silke-title.scss +7 -14
  135. package/src/components/silke-text-field/silke-text-field.scss +9 -9
  136. package/src/components/silke-text-field/silke-text-field.tsx +1 -1
  137. package/src/components/silke-tile/silke-tile.scss +7 -3
  138. package/src/components/silke-toast-notification/silke-toast-notification.scss +4 -5
  139. package/src/components/silke-toggle/silke-toggle.mdx +163 -33
  140. package/src/components/silke-toggle/silke-toggle.scss +4 -4
  141. package/src/components/silke-toolbar/silke-toolbar.scss +1 -1
  142. package/src/components/silke-tooltip/silke-shortcuts.tsx +3 -3
  143. package/src/components/silke-tooltip/silke-tooltip.scss +11 -16
  144. package/src/components/silke-variables-wrapper/silke-variables-wrapper.scss +9 -7
  145. package/src/components/silke-virtual/silke-virtual.mdx +161 -0
  146. package/src/components/silke-virtual/silke-virtual.scss +8 -8
  147. package/src/icons.tsx +58 -1
  148. package/src/styles/core.scss +42 -45
  149. package/src/styles/math.scss +66 -66
  150. package/tsconfig.build.json +2 -6
  151. package/tsconfig.build.tsbuildinfo +1 -1
  152. package/tsconfig.json +3 -14
  153. package/dist/types/components/silke-action-list/index.d.ts +0 -1
  154. package/dist/types/components/silke-action-list/silke-action-list.d.ts +0 -11
  155. package/dist/types/components/silke-list-card/index.d.ts +0 -1
  156. package/dist/types/components/silke-list-card/silke-list-card.d.ts +0 -14
  157. package/src/components/silke-action-list/index.ts +0 -1
  158. package/src/components/silke-action-list/silke-action-list.mdx +0 -48
  159. package/src/components/silke-action-list/silke-action-list.scss +0 -21
  160. package/src/components/silke-action-list/silke-action-list.tsx +0 -85
  161. package/src/components/silke-list-card/index.ts +0 -1
  162. package/src/components/silke-list-card/silke-list-card.mdx +0 -46
  163. package/src/components/silke-list-card/silke-list-card.scss +0 -57
  164. package/src/components/silke-list-card/silke-list-card.tsx +0 -63
@@ -0,0 +1,672 @@
1
+ import React, {
2
+ useCallback,
3
+ useEffect,
4
+ useImperativeHandle,
5
+ useLayoutEffect,
6
+ useMemo,
7
+ useRef,
8
+ useState,
9
+ } from 'react';
10
+ import { useDropZone } from '../../hooks/use-drop-zone';
11
+ import { useFormField } from '../../hooks/form';
12
+ import { SilkeButton } from '../silke-button';
13
+ import {
14
+ SilkeTextFieldOutline,
15
+ SilkeTextFieldOutlineProps,
16
+ } from '../silke-text-field/silke-text-field-outline';
17
+ import {
18
+ fileToImageValue,
19
+ getImageFromClipboard,
20
+ getImageFromDragEvent,
21
+ imageValueToDataUrl,
22
+ } from './image-utils';
23
+ import { parseTextToSilkeCommands } from './silke-command-parser';
24
+ import styles from './silke-command-text-field.module.scss';
25
+ import {
26
+ filterAndSortCommands,
27
+ getActiveCommandIndex,
28
+ getCaretOffset,
29
+ getItemLength,
30
+ getTextLength,
31
+ hasSelectionLength,
32
+ isImage,
33
+ isSilkeCommand,
34
+ isText,
35
+ KEY_DIRECTION,
36
+ setCaretOffset,
37
+ SilkeCommand,
38
+ SilkeCommandTextFieldValue,
39
+ SilkeImageValue,
40
+ valueToHtml,
41
+ convertSilkeCommandsToString,
42
+ } from './utils';
43
+
44
+ export type SilkeCommandTextFieldProps = Omit<SilkeTextFieldOutlineProps, 'children'> & {
45
+ autoFocus?: boolean;
46
+ availableCommands: SilkeCommand[];
47
+ disabled?: boolean;
48
+ /** When true, images cannot be added via paste or drag-and-drop */
49
+ disableImages?: boolean;
50
+ name?: string;
51
+ onChange: (value: SilkeCommandTextFieldValue[]) => void;
52
+ onCommandFocus?: (index: number, command: SilkeCommandTextFieldValue | undefined) => void;
53
+ onKeyDown?: (e: React.KeyboardEvent<HTMLDivElement>) => void;
54
+ onSuggestions?: (suggestions: SilkeCommand[]) => void;
55
+ placeholder?: string;
56
+ value: SilkeCommandTextFieldValue[];
57
+ };
58
+
59
+ type ActiveCommandState = {
60
+ index: number;
61
+ localOffset: number;
62
+ };
63
+
64
+ export type SilkeCommandTextFieldHandle = {
65
+ /** Remove focus from the input */
66
+ blur: () => void;
67
+ /** Focus the input */
68
+ focus: () => void;
69
+ /** Get the current value as a string */
70
+ toString: (value?: SilkeCommandTextFieldValue[]) => string;
71
+ /**
72
+ * Update command in the commands array.
73
+ * @param command - the command to update
74
+ * @param index - defaults to active index, use -1 to append
75
+ */
76
+ update: (command: SilkeCommand, index?: number) => void;
77
+ };
78
+
79
+ const SilkeCommandTextField = React.forwardRef<
80
+ SilkeCommandTextFieldHandle,
81
+ SilkeCommandTextFieldProps
82
+ >((props, ref) => {
83
+ const {
84
+ autoFocus = false,
85
+ availableCommands,
86
+ disabled = false,
87
+ disableImages = false,
88
+ onChange,
89
+ onCommandFocus,
90
+ onKeyDown,
91
+ onSuggestions,
92
+ placeholder,
93
+ value,
94
+ ...rest
95
+ } = useFormField(props);
96
+
97
+ const inputRef = useRef<HTMLDivElement>(null);
98
+ const valueRef = useRef<SilkeCommandTextFieldValue[]>(value);
99
+ valueRef.current = value;
100
+
101
+ // Extract images from value
102
+ const images = useMemo(() => value.filter(isImage), [value]);
103
+ // Get non-image values for text field rendering
104
+ const textValues = useMemo(() => value.filter((v) => !isImage(v)), [value]);
105
+
106
+ // Track which image is being previewed (-1 means no preview)
107
+ const [previewImageIndex, setPreviewImageIndex] = useState(-1);
108
+
109
+ const [activeCommand, setActiveCommand] = useState<ActiveCommandState>(() => {
110
+ const lastIndex = Math.max(0, value.length - 1);
111
+ const lastItem = value[lastIndex];
112
+ return {
113
+ index: lastIndex,
114
+ localOffset: lastItem ? getItemLength(lastItem) : 0,
115
+ };
116
+ });
117
+
118
+ const html = useMemo(() => valueToHtml(textValues || [], styles), [textValues]);
119
+ const isEmpty =
120
+ (!value || value.length === 0 || convertSilkeCommandsToString(value).length === 0) &&
121
+ images.length === 0;
122
+
123
+ // Restore caret position after value changes
124
+ useLayoutEffect(() => {
125
+ const element = inputRef.current;
126
+ if (!element || document.activeElement !== element) return;
127
+
128
+ const calculatedCaretPos =
129
+ getTextLength(value, activeCommand.index) + activeCommand.localOffset;
130
+ setCaretOffset(element, calculatedCaretPos);
131
+ }, [activeCommand.index, activeCommand.localOffset, value]);
132
+
133
+ const updateValue = useCallback(
134
+ (command: SilkeCommand, index: number, insert = false) => {
135
+ const newValue = [...valueRef.current];
136
+
137
+ if (index === -1) {
138
+ newValue.push(command);
139
+ index = newValue.length - 1;
140
+ } else if (insert) {
141
+ // Insert at index without replacing
142
+ newValue.splice(index, 0, command);
143
+ } else {
144
+ newValue[index] = command;
145
+ }
146
+
147
+ // Insert space after the command
148
+ newValue.splice(index + 1, 0, { type: 'text', value: ' ' });
149
+
150
+ // Move caret after the space
151
+ setActiveCommand({ index: index + 1, localOffset: 1 });
152
+ onChange(newValue);
153
+ inputRef.current?.focus();
154
+ },
155
+ [onChange],
156
+ );
157
+
158
+ const focusAtEnd = useCallback(() => {
159
+ const element = inputRef.current;
160
+ if (!element) return;
161
+ element.focus();
162
+ const currentValue = valueRef.current;
163
+ const lastIndex = Math.max(0, currentValue.length - 1);
164
+ const lastItem = currentValue[lastIndex];
165
+ const offset = lastItem ? getItemLength(lastItem) : 0;
166
+ const caretPos = getTextLength(currentValue, lastIndex) + offset;
167
+ setCaretOffset(element, caretPos);
168
+ setActiveCommand({ index: lastIndex, localOffset: offset });
169
+ }, []);
170
+
171
+ useImperativeHandle(
172
+ ref,
173
+ () => ({
174
+ blur: () => inputRef.current?.blur(),
175
+ focus: focusAtEnd,
176
+ toString: (val = valueRef.current || []) => convertSilkeCommandsToString(val),
177
+ update: (command, index = activeCommand.index) => {
178
+ // If the current item is a completed command, insert after it instead of replacing
179
+ const currentItem = valueRef.current[index];
180
+ if (isSilkeCommand(currentItem) && !currentItem.partialMatch) {
181
+ updateValue(command, index + 1, true);
182
+ } else {
183
+ updateValue(command, index);
184
+ }
185
+ },
186
+ }),
187
+ [activeCommand.index, focusAtEnd, updateValue],
188
+ );
189
+
190
+ // Handle auto focus
191
+ useEffect(() => {
192
+ if (autoFocus && !disabled) {
193
+ focusAtEnd();
194
+ }
195
+ }, [autoFocus, disabled, focusAtEnd]);
196
+
197
+ // Handle focus event to position caret at end
198
+ const handleFocus = useCallback(() => {
199
+ const element = inputRef.current;
200
+ if (!element) return;
201
+ // Use requestAnimationFrame to ensure DOM is ready
202
+ requestAnimationFrame(() => {
203
+ const currentValue = valueRef.current;
204
+ const lastIndex = Math.max(0, currentValue.length - 1);
205
+ const lastItem = currentValue[lastIndex];
206
+ const offset = lastItem ? getItemLength(lastItem) : 0;
207
+ const caretPos = getTextLength(currentValue, lastIndex) + offset;
208
+ setCaretOffset(element, caretPos);
209
+ setActiveCommand({ index: lastIndex, localOffset: offset });
210
+ });
211
+ }, []);
212
+
213
+ // Notify parent of command focus changes
214
+ useEffect(() => {
215
+ onCommandFocus?.(activeCommand.index, valueRef.current[activeCommand.index]);
216
+ }, [activeCommand.index, onCommandFocus]);
217
+
218
+ // Track selection changes to update active command
219
+ useEffect(() => {
220
+ const handleSelectionChange = () => {
221
+ const element = inputRef.current;
222
+ if (!element) return;
223
+
224
+ // Only update if selection is within this element
225
+ const { activeElement } = document;
226
+ if (activeElement !== element && !element.contains(activeElement)) return;
227
+
228
+ const caretPos = getCaretOffset(element);
229
+ const currentValue = valueRef.current;
230
+ const newActiveIndex = getActiveCommandIndex(caretPos, currentValue);
231
+ let newLocalOffset = caretPos - getTextLength(currentValue, newActiveIndex);
232
+
233
+ const item = currentValue[newActiveIndex];
234
+
235
+ // Snap caret to boundary if inside a non-partial command
236
+ if (isSilkeCommand(item) && !item.partialMatch) {
237
+ const itemLength = getItemLength(item);
238
+ if (newLocalOffset > 0 && newLocalOffset < itemLength) {
239
+ // Snap to nearest boundary
240
+ newLocalOffset = newLocalOffset < itemLength / 2 ? 0 : itemLength;
241
+ const newCaretPos = getTextLength(currentValue, newActiveIndex) + newLocalOffset;
242
+ setCaretOffset(element, newCaretPos);
243
+ }
244
+ }
245
+
246
+ setActiveCommand({ index: newActiveIndex, localOffset: newLocalOffset });
247
+ };
248
+
249
+ document.addEventListener('selectionchange', handleSelectionChange);
250
+ return () => document.removeEventListener('selectionchange', handleSelectionChange);
251
+ }, []);
252
+
253
+ const updateActiveCommandFromCaret = useCallback(
254
+ (newValue: SilkeCommandTextFieldValue[], caretPos?: number) => {
255
+ const pos = caretPos ?? getCaretOffset(inputRef.current);
256
+ const newActiveIndex = getActiveCommandIndex(pos, newValue);
257
+ const newLocalOffset = pos - getTextLength(newValue, newActiveIndex);
258
+ setActiveCommand({ index: newActiveIndex, localOffset: newLocalOffset });
259
+ },
260
+ [],
261
+ );
262
+
263
+ const handleInput = useCallback(
264
+ (e: React.FormEvent<HTMLDivElement>) => {
265
+ // Preserve existing images when parsing new text
266
+ const existingImages = valueRef.current.filter(isImage);
267
+ const parsedValue = parseTextToSilkeCommands(e.currentTarget.innerText, availableCommands);
268
+ const newValue = [...existingImages, ...parsedValue];
269
+ valueRef.current = newValue;
270
+ updateActiveCommandFromCaret(newValue);
271
+ onChange(newValue);
272
+ },
273
+ [availableCommands, onChange, updateActiveCommandFromCaret],
274
+ );
275
+
276
+ // Update suggestions when active command changes
277
+ useEffect(() => {
278
+ if (!onSuggestions) return;
279
+
280
+ const currentCommand = valueRef.current[activeCommand.index];
281
+ if (isSilkeCommand(currentCommand) && currentCommand.partialMatch) {
282
+ // Get commands already used in the value (completed, non-partial commands)
283
+ const usedCommands = new Set(
284
+ valueRef.current
285
+ .filter((item): item is SilkeCommand => isSilkeCommand(item) && !item.partialMatch)
286
+ .map((cmd) => `${cmd.type}:${cmd.value}`),
287
+ );
288
+
289
+ const suggestions = filterAndSortCommands(
290
+ availableCommands,
291
+ currentCommand.value,
292
+ currentCommand.type,
293
+ ).filter((cmd) => !usedCommands.has(`${cmd.type}:${cmd.value}`));
294
+
295
+ onSuggestions(suggestions);
296
+ } else {
297
+ onSuggestions([]);
298
+ }
299
+ }, [activeCommand.index, activeCommand.localOffset, availableCommands, onSuggestions]);
300
+
301
+ const handleArrowNavigation = useCallback(
302
+ (
303
+ e: React.KeyboardEvent,
304
+ direction: number,
305
+ commandIndex: number,
306
+ currentValue: SilkeCommandTextFieldValue[],
307
+ ) => {
308
+ e.preventDefault();
309
+
310
+ if (direction > 0) {
311
+ // Moving right: skip to end of command or start of next item
312
+ if (commandIndex >= currentValue.length - 1) {
313
+ const item = currentValue[commandIndex];
314
+ setActiveCommand({ index: commandIndex, localOffset: getItemLength(item) });
315
+ } else {
316
+ setActiveCommand({ index: commandIndex + 1, localOffset: 0 });
317
+ }
318
+ } else {
319
+ // Moving left: skip to start of command or end of previous item
320
+ if (commandIndex <= 0) {
321
+ setActiveCommand({ index: 0, localOffset: 0 });
322
+ } else {
323
+ const prevItem = currentValue[commandIndex - 1];
324
+ setActiveCommand({
325
+ index: commandIndex - 1,
326
+ localOffset: getItemLength(prevItem),
327
+ });
328
+ }
329
+ }
330
+ },
331
+ [],
332
+ );
333
+
334
+ const handleDelete = useCallback(
335
+ (
336
+ e: React.KeyboardEvent,
337
+ commandIndex: number,
338
+ currentValue: SilkeCommandTextFieldValue[],
339
+ isForward: boolean,
340
+ ) => {
341
+ e.preventDefault();
342
+
343
+ const newValue = [...currentValue];
344
+ newValue.splice(commandIndex, 1);
345
+
346
+ if (isForward) {
347
+ // Delete key: stay at current position
348
+ const nextIndex = Math.min(commandIndex, newValue.length - 1);
349
+ setActiveCommand({ index: Math.max(0, nextIndex), localOffset: 0 });
350
+ } else {
351
+ // Backspace: move to end of previous item
352
+ const prevIndex = Math.max(commandIndex - 1, 0);
353
+ const prevItem = newValue[prevIndex];
354
+ setActiveCommand({ index: prevIndex, localOffset: prevItem ? getItemLength(prevItem) : 0 });
355
+ }
356
+ onChange(newValue);
357
+ },
358
+ [onChange],
359
+ );
360
+
361
+ const handleCharacterInput = useCallback(
362
+ (
363
+ e: React.KeyboardEvent,
364
+ commandIndex: number,
365
+ commandItem: SilkeCommandTextFieldValue,
366
+ currentValue: SilkeCommandTextFieldValue[],
367
+ ) => {
368
+ const element = inputRef.current;
369
+ if (!element) return;
370
+
371
+ // Only handle single character input without modifiers
372
+ if (e.key.length !== 1 || e.ctrlKey || e.metaKey || e.altKey) return;
373
+
374
+ const currentCaretPos = getCaretOffset(element);
375
+ const startOfCommand = getTextLength(currentValue, commandIndex);
376
+ const itemLength = getItemLength(commandItem);
377
+ const localOffset = currentCaretPos - startOfCommand;
378
+
379
+ e.preventDefault();
380
+
381
+ const newValue = [...currentValue];
382
+
383
+ if (localOffset <= 0) {
384
+ // At start of command - insert text before
385
+ const prevItem = newValue[commandIndex - 1];
386
+ if (commandIndex > 0 && isText(prevItem)) {
387
+ // Append to previous text segment
388
+ newValue[commandIndex - 1] = {
389
+ type: 'text',
390
+ value: prevItem.value + e.key,
391
+ };
392
+ setActiveCommand({
393
+ index: commandIndex - 1,
394
+ localOffset: prevItem.value.length + 1,
395
+ });
396
+ } else {
397
+ // Insert new text segment before command
398
+ newValue.splice(commandIndex, 0, { type: 'text', value: e.key });
399
+ setActiveCommand({ index: commandIndex, localOffset: 1 });
400
+ }
401
+ onChange(newValue);
402
+ } else if (localOffset >= itemLength) {
403
+ // At end of command - insert text after
404
+ const nextItem = newValue[commandIndex + 1];
405
+ if (commandIndex < newValue.length - 1 && isText(nextItem)) {
406
+ // Prepend to next text segment
407
+ newValue[commandIndex + 1] = {
408
+ type: 'text',
409
+ value: e.key + nextItem.value,
410
+ };
411
+ setActiveCommand({ index: commandIndex + 1, localOffset: 1 });
412
+ } else {
413
+ // Insert new text segment after command
414
+ newValue.splice(commandIndex + 1, 0, { type: 'text', value: e.key });
415
+ setActiveCommand({ index: commandIndex + 1, localOffset: 1 });
416
+ }
417
+ onChange(newValue);
418
+ }
419
+ // If inside command (not at boundary), just prevent - no insertion
420
+ },
421
+ [onChange],
422
+ );
423
+
424
+ const handleKeyDown = useCallback(
425
+ (e: React.KeyboardEvent<HTMLDivElement>) => {
426
+ onKeyDown?.(e);
427
+
428
+ if (disabled) {
429
+ e.preventDefault();
430
+ return;
431
+ }
432
+
433
+ // Cmd+Enter or Shift+Enter inserts a line break
434
+ if (e.key === 'Enter' && (e.metaKey || e.shiftKey)) {
435
+ e.preventDefault();
436
+ document.execCommand('insertLineBreak');
437
+ return;
438
+ }
439
+
440
+ const element = inputRef.current;
441
+ if (!element) return;
442
+
443
+ const direction = KEY_DIRECTION[e.key] || 0;
444
+ const currentCaretPos = getCaretOffset(element);
445
+ const caretPos = currentCaretPos + direction;
446
+ const currentValue = valueRef.current;
447
+ const commandIndex = getActiveCommandIndex(caretPos, currentValue);
448
+ const commandItem = currentValue[commandIndex];
449
+
450
+ // Don't interfere if there's a text selection
451
+ if (hasSelectionLength()) return;
452
+
453
+ // Prevent deleting a space that separates two completed commands
454
+ if (e.key === 'Backspace' || e.key === 'Delete') {
455
+ const isBackspace = e.key === 'Backspace';
456
+ // For backspace, check char before caret; for delete, check char at caret
457
+ const deleteTargetPos = isBackspace ? currentCaretPos - 1 : currentCaretPos;
458
+ const targetIndex = getActiveCommandIndex(deleteTargetPos, currentValue);
459
+ const targetItem = currentValue[targetIndex];
460
+
461
+ // Check if we're about to delete a space-only segment between two commands
462
+ if (isText(targetItem) && targetItem.value.trim() === '' && targetItem.value.length === 1) {
463
+ const prevItem = currentValue[targetIndex - 1];
464
+ const nextItem = currentValue[targetIndex + 1];
465
+ const hasPrevCommand = isSilkeCommand(prevItem) && !prevItem.partialMatch;
466
+ const hasNextCommand = isSilkeCommand(nextItem) && !nextItem.partialMatch;
467
+
468
+ if (hasPrevCommand && hasNextCommand) {
469
+ // Delete the space and the following command instead
470
+ e.preventDefault();
471
+ const newValue = [...currentValue];
472
+ newValue.splice(targetIndex, 2); // Remove space and next command
473
+ const prevItemLen = getItemLength(prevItem);
474
+ setActiveCommand({ index: targetIndex - 1, localOffset: prevItemLen });
475
+ onChange(newValue);
476
+ return;
477
+ }
478
+ }
479
+ }
480
+
481
+ // Only handle special keys for complete (non-partial) commands
482
+ if (!isSilkeCommand(commandItem) || commandItem.partialMatch) return;
483
+
484
+ // Calculate if target position is strictly inside the command (not at boundaries)
485
+ const startOfCommand = getTextLength(currentValue, commandIndex);
486
+ const itemLength = getItemLength(commandItem);
487
+ const localOffset = caretPos - startOfCommand;
488
+ const isStrictlyInside = localOffset > 0 && localOffset < itemLength;
489
+
490
+ switch (e.key) {
491
+ case 'ArrowLeft':
492
+ case 'ArrowRight':
493
+ // Only skip command if we would land strictly inside it
494
+ if (isStrictlyInside) {
495
+ handleArrowNavigation(e, direction, commandIndex, currentValue);
496
+ }
497
+ // If at boundary, allow normal navigation
498
+ break;
499
+ case 'Backspace':
500
+ case 'Delete':
501
+ // Delete entire command if target is inside or we're at appropriate boundary
502
+ if (isStrictlyInside) {
503
+ handleDelete(e, commandIndex, currentValue, e.key === 'Delete');
504
+ }
505
+ // If at boundary (localOffset = 0 or itemLength), let browser handle
506
+ break;
507
+ default:
508
+ handleCharacterInput(e, commandIndex, commandItem, currentValue);
509
+ }
510
+ },
511
+ [disabled, handleArrowNavigation, handleCharacterInput, handleDelete, onKeyDown],
512
+ );
513
+
514
+ const addImage = useCallback(
515
+ (imageValue: SilkeImageValue) => {
516
+ const newValue = [...valueRef.current, imageValue];
517
+ onChange(newValue);
518
+ },
519
+ [onChange],
520
+ );
521
+
522
+ const removeImage = useCallback(
523
+ (index: number) => {
524
+ // Find the nth image in the value array and remove it
525
+ let imageCount = 0;
526
+ const newValue = valueRef.current.filter((item) => {
527
+ if (isImage(item)) {
528
+ if (imageCount === index) {
529
+ imageCount++;
530
+ return false;
531
+ }
532
+ imageCount++;
533
+ }
534
+ return true;
535
+ });
536
+ onChange(newValue);
537
+ },
538
+ [onChange],
539
+ );
540
+
541
+ const handlePaste = useCallback(
542
+ (e: React.ClipboardEvent<HTMLDivElement>) => {
543
+ if (disableImages) return;
544
+
545
+ const file = getImageFromClipboard(e);
546
+ if (!file) return;
547
+
548
+ e.preventDefault();
549
+ fileToImageValue(file).then((imageValue) => {
550
+ if (imageValue) addImage(imageValue);
551
+ });
552
+ },
553
+ [addImage, disableImages],
554
+ );
555
+
556
+ const containerRef = useRef<HTMLDivElement>(null);
557
+
558
+ const handleDrop = useCallback(
559
+ (e: DragEvent) => {
560
+ if (disableImages) return;
561
+
562
+ const file = getImageFromDragEvent(e);
563
+ if (!file) return;
564
+
565
+ fileToImageValue(file).then((imageValue) => {
566
+ if (imageValue) addImage(imageValue);
567
+ });
568
+ },
569
+ [addImage, disableImages],
570
+ );
571
+
572
+ useDropZone(containerRef, handleDrop);
573
+
574
+ return (
575
+ <SilkeTextFieldOutline {...rest}>
576
+ <div className={styles.container} ref={containerRef}>
577
+ {!disableImages && images.length > 0 && (
578
+ <div className={styles.imageList}>
579
+ {images.map((img, idx) => (
580
+ <div
581
+ key={idx}
582
+ className={styles.imageThumbnail}
583
+ onClick={() => setPreviewImageIndex(idx)}
584
+ role="button"
585
+ tabIndex={0}
586
+ onKeyDown={(e) => {
587
+ if (e.key === 'Enter' || e.key === ' ') {
588
+ e.preventDefault();
589
+ setPreviewImageIndex(idx);
590
+ }
591
+ }}
592
+ >
593
+ <img src={imageValueToDataUrl(img)} alt={img.name || `Image ${idx + 1}`} />
594
+ {!disabled && (
595
+ <SilkeButton
596
+ kind="secondary"
597
+ size="s"
598
+ icon="close"
599
+ style={{
600
+ position: 'absolute',
601
+ top: 2,
602
+ right: 2,
603
+ backdropFilter: 'blur(8px) brightness(0.7)',
604
+ }}
605
+ className={styles.imageRemove}
606
+ onClick={(e) => {
607
+ e.stopPropagation();
608
+ removeImage(idx);
609
+ }}
610
+ aria-label={`Remove ${img.name || `image ${idx + 1}`}`}
611
+ />
612
+ )}
613
+ </div>
614
+ ))}
615
+ </div>
616
+ )}
617
+ {previewImageIndex >= 0 && images[previewImageIndex] && (
618
+ <div
619
+ className={styles.imagePreviewOverlay}
620
+ onClick={() => setPreviewImageIndex(-1)}
621
+ role="button"
622
+ tabIndex={0}
623
+ onKeyDown={(e) => {
624
+ if (e.key === 'Escape' || e.key === 'Enter' || e.key === ' ') {
625
+ setPreviewImageIndex(-1);
626
+ }
627
+ }}
628
+ >
629
+ <div className={styles.imagePreview} onClick={(e) => e.stopPropagation()}>
630
+ <img
631
+ src={imageValueToDataUrl(images[previewImageIndex])}
632
+ alt={images[previewImageIndex].name || `Image ${previewImageIndex + 1}`}
633
+ />
634
+ <SilkeButton
635
+ kind="secondary"
636
+ icon="close"
637
+ style={{
638
+ position: 'absolute',
639
+ top: 8,
640
+ right: 8,
641
+ backdropFilter: 'blur(8px) brightness(0.7)',
642
+ }}
643
+ onClick={() => setPreviewImageIndex(-1)}
644
+ aria-label="Close preview"
645
+ />
646
+ </div>
647
+ </div>
648
+ )}
649
+ {placeholder && isEmpty && <div className={styles.placeholder}>{placeholder}</div>}
650
+ <div
651
+ aria-disabled={disabled}
652
+ aria-placeholder={placeholder}
653
+ className={styles.textfield}
654
+ contentEditable={!disabled}
655
+ dangerouslySetInnerHTML={{ __html: html }}
656
+ onFocus={handleFocus}
657
+ onInput={handleInput}
658
+ onKeyDown={handleKeyDown}
659
+ onPaste={handlePaste}
660
+ ref={inputRef}
661
+ role="textbox"
662
+ suppressContentEditableWarning
663
+ tabIndex={disabled ? -1 : 0}
664
+ />
665
+ </div>
666
+ </SilkeTextFieldOutline>
667
+ );
668
+ });
669
+
670
+ SilkeCommandTextField.displayName = 'SilkeCommandTextField';
671
+
672
+ export { SilkeCommandTextField };