@vev/silke 1.1.10 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +162 -77
- package/CHANGELOG.md +41 -0
- package/dist/index.js +44 -41
- package/dist/index.js.LEGAL.txt +1 -1
- package/dist/index.js.map +7 -0
- package/dist/styles.css +1596 -1013
- package/dist/types/components/index.d.ts +2 -0
- package/dist/types/components/silke-autocomplete-field/autocomplete-item.d.ts +1 -1
- package/dist/types/components/silke-autocomplete-field/silke-autocomplete-field-native.d.ts +5 -0
- package/dist/types/components/silke-box/silke-box.d.ts +2 -0
- package/dist/types/components/silke-button/silke-dropdown-button.d.ts +3 -1
- package/dist/types/components/silke-card/silke-card-grid.d.ts +1 -0
- package/dist/types/components/silke-card/silke-card.d.ts +1 -0
- package/dist/types/components/silke-command-text-field/index.d.ts +1 -1
- package/dist/types/components/silke-command-text-field/silke-command-text-field.d.ts +16 -0
- package/dist/types/components/silke-command-text-field/utils.d.ts +25 -0
- package/dist/types/components/silke-css-number-field/silke-css-number-field.spec.d.ts +1 -0
- package/dist/types/components/silke-expression-input/index.d.ts +3 -0
- package/dist/types/components/silke-expression-input/silke-expression-input-tag.d.ts +6 -0
- package/dist/types/components/silke-expression-input/silke-expression-input.d.ts +29 -0
- package/dist/types/components/silke-expression-input/utils.d.ts +18 -0
- package/dist/types/components/silke-form-schema/fields/array-field-inline.d.ts +2 -1
- package/dist/types/components/silke-form-schema/fields/array-field-modal.d.ts +2 -1
- package/dist/types/components/silke-form-schema/fields/array-field.d.ts +1 -1
- package/dist/types/components/silke-form-schema/fields/group-field.d.ts +1 -1
- package/dist/types/components/silke-form-schema/fields/layout-field.d.ts +1 -1
- 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 +1 -1
- package/dist/types/components/silke-form-schema/fields/string-field.d.ts +1 -1
- package/dist/types/components/silke-form-schema/fields/utils/generate-preview.spec.d.ts +1 -0
- package/dist/types/components/silke-form-schema/fields/utils/generate-title.d.ts +10 -0
- package/dist/types/components/silke-form-schema/index.d.ts +1 -0
- package/dist/types/components/silke-form-schema/silke-form-schema.d.ts +4 -1
- package/dist/types/components/silke-form-schema/silke-schema-decorator.d.ts +17 -0
- package/dist/types/components/silke-form-schema/silke-schema-field.d.ts +2 -1
- package/dist/types/components/silke-form-schema/silke-schema-fields.d.ts +2 -0
- package/dist/types/components/silke-form-schema/silke-schema-path.spec.d.ts +1 -0
- package/dist/types/components/silke-gradient-picker/silke-gradient-utils.d.ts +2 -0
- package/dist/types/components/silke-icon-picker/color-swatch-section.d.ts +6 -0
- package/dist/types/components/silke-icon-picker/emoji-tab.d.ts +1 -1
- package/dist/types/components/silke-icon-picker/icons-tab.d.ts +1 -1
- package/dist/types/components/silke-icon-picker/silke-icon-picker.d.ts +2 -0
- package/dist/types/components/silke-icon-picker/utils.d.ts +2 -0
- package/dist/types/components/silke-list/silke-list-item.d.ts +5 -1
- package/dist/types/components/silke-list/types.d.ts +1 -0
- package/dist/types/components/silke-navigation-link/silke-navigation-link.d.ts +15 -0
- package/dist/types/components/silke-popover/popover-context.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/blockquote.d.ts +21 -0
- package/dist/types/components/silke-rich-text-field/blockquote.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/dropcap.d.ts +27 -0
- package/dist/types/components/silke-rich-text-field/dropcap.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/floating-layer.d.ts +12 -0
- package/dist/types/components/silke-rich-text-field/font-size.d.ts +2 -0
- package/dist/types/components/silke-rich-text-field/formatting-toolbar.d.ts +9 -0
- package/dist/types/components/silke-rich-text-field/index.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/jsdom-rects.d.ts +10 -0
- package/dist/types/components/silke-rich-text-field/link-menu.d.ts +26 -0
- package/dist/types/components/silke-rich-text-field/link-menu.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/link-suggestions.d.ts +48 -0
- package/dist/types/components/silke-rich-text-field/link-suggestions.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/link-tooltip.d.ts +15 -0
- package/dist/types/components/silke-rich-text-field/list-item.d.ts +15 -0
- package/dist/types/components/silke-rich-text-field/list-link-fontsize.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/lorem-ipsum.d.ts +10 -0
- package/dist/types/components/silke-rich-text-field/lorem-ipsum.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/paste.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/paste.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/plugins.d.ts +18 -0
- package/dist/types/components/silke-rich-text-field/plugins.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/silke-rich-text-field.d.ts +60 -0
- package/dist/types/components/silke-rich-text-field/slash-menu-items.d.ts +10 -0
- package/dist/types/components/silke-rich-text-field/slash-menu.d.ts +11 -0
- package/dist/types/components/silke-rich-text-field/table-content-sync.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/table-toolbar.d.ts +17 -0
- package/dist/types/components/silke-rich-text-field/table-toolbar.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/table.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/use-link-hover.d.ts +15 -0
- package/dist/types/components/silke-rich-text-field/use-slash-menu.d.ts +13 -0
- package/dist/types/components/silke-rich-text-field/utils.d.ts +13 -0
- package/dist/types/components/silke-sortable-list/index.d.ts +1 -1
- package/dist/types/components/silke-sortable-list/silke-nested-sortable-list.d.ts +8 -1
- package/dist/types/components/silke-theme-card/silke-theme-card.d.ts +13 -6
- package/dist/types/components/silke-theme-swatch/silke-theme-swatch.d.ts +6 -1
- package/dist/types/icons.d.ts +16 -0
- package/esbuild.config.mjs +9 -4
- package/package.json +22 -9
- package/src/components/index.ts +2 -0
- package/src/components/silke-alert-banner/silke-alert-banner.tsx +1 -1
- package/src/components/silke-autocomplete-field/autocomplete-item.tsx +47 -61
- package/src/components/silke-autocomplete-field/silke-autocomplete-field-native.tsx +13 -2
- package/src/components/silke-autocomplete-field/silke-autocomplete-field.tsx +8 -5
- package/src/components/silke-autocomplete-field/silke-autocomplete-popup-native.tsx +2 -1
- package/src/components/silke-box/silke-box.scss +4 -0
- package/src/components/silke-box/silke-box.tsx +4 -0
- package/src/components/silke-button/silke-dropdown-button.tsx +4 -1
- package/src/components/silke-card/silke-card.scss +4 -0
- package/src/components/silke-card/silke-card.tsx +3 -0
- package/src/components/silke-command-text-field/index.ts +1 -0
- package/src/components/silke-command-text-field/silke-command-parser.spec.ts +55 -0
- package/src/components/silke-command-text-field/silke-command-parser.ts +33 -2
- package/src/components/silke-command-text-field/silke-command-text-field.module.scss +3 -0
- package/src/components/silke-command-text-field/silke-command-text-field.tsx +53 -48
- package/src/components/silke-command-text-field/utils.spec.ts +192 -0
- package/src/components/silke-command-text-field/utils.ts +99 -16
- package/src/components/silke-css-number-field/silke-css-number-field.spec.tsx +115 -0
- package/src/components/silke-css-number-field/silke-css-number-field.tsx +33 -0
- package/src/components/silke-expression-input/index.ts +3 -0
- package/src/components/silke-expression-input/silke-expression-input-tag.tsx +37 -0
- package/src/components/silke-expression-input/silke-expression-input.mdx +40 -0
- package/src/components/silke-expression-input/silke-expression-input.scss +24 -0
- package/src/components/silke-expression-input/silke-expression-input.tsx +343 -0
- package/src/components/silke-expression-input/utils.ts +144 -0
- package/src/components/silke-form-schema/fields/array-field-inline.tsx +4 -0
- package/src/components/silke-form-schema/fields/array-field-modal.tsx +4 -0
- package/src/components/silke-form-schema/fields/array-field.tsx +6 -11
- package/src/components/silke-form-schema/fields/group-field.tsx +2 -0
- package/src/components/silke-form-schema/fields/layout-field.tsx +2 -0
- package/src/components/silke-form-schema/fields/object-field.tsx +2 -0
- package/src/components/silke-form-schema/fields/popover-field.tsx +2 -0
- package/src/components/silke-form-schema/fields/string-field.tsx +33 -5
- package/src/components/silke-form-schema/fields/utils/generate-image.ts +5 -14
- package/src/components/silke-form-schema/fields/utils/generate-preview.spec.ts +49 -0
- package/src/components/silke-form-schema/fields/utils/generate-title.ts +23 -10
- package/src/components/silke-form-schema/index.ts +1 -0
- package/src/components/silke-form-schema/silke-form-schema.mdx +30 -1
- package/src/components/silke-form-schema/silke-form-schema.tsx +12 -6
- package/src/components/silke-form-schema/silke-schema-decorator.tsx +32 -0
- package/src/components/silke-form-schema/silke-schema-field.tsx +19 -2
- package/src/components/silke-form-schema/silke-schema-fields.tsx +30 -4
- package/src/components/silke-form-schema/silke-schema-path.spec.tsx +160 -0
- package/src/components/silke-gradient-picker/silke-gradient-picker.tsx +3 -1
- package/src/components/silke-gradient-picker/silke-gradient-utils.ts +23 -3
- package/src/components/silke-icon/silke-shape.tsx +1 -1
- package/src/components/silke-icon-picker/color-swatch-section.tsx +61 -0
- package/src/components/silke-icon-picker/emoji-tab.tsx +8 -6
- package/src/components/silke-icon-picker/icons-tab.tsx +8 -7
- package/src/components/silke-icon-picker/silke-icon-picker.tsx +52 -25
- package/src/components/silke-icon-picker/styles.scss +66 -1
- package/src/components/silke-icon-picker/utils.ts +2 -0
- package/src/components/silke-list/silke-list-item.scss +10 -0
- package/src/components/silke-list/silke-list-item.tsx +16 -3
- package/src/components/silke-list/silke-list.mdx +18 -0
- package/src/components/silke-list/types.ts +1 -0
- package/src/components/silke-navigation-link/silke-navigation-link.scss +162 -42
- package/src/components/silke-navigation-link/silke-navigation-link.tsx +99 -21
- package/src/components/silke-popover/popover-context.spec.tsx +44 -0
- package/src/components/silke-popover-native/silke-popover-native.scss +5 -3
- package/src/components/silke-rich-text-field/blockquote.spec.ts +151 -0
- package/src/components/silke-rich-text-field/blockquote.ts +90 -0
- package/src/components/silke-rich-text-field/dropcap.spec.ts +67 -0
- package/src/components/silke-rich-text-field/dropcap.ts +59 -0
- package/src/components/silke-rich-text-field/floating-layer.tsx +15 -0
- package/src/components/silke-rich-text-field/font-size.ts +62 -0
- package/src/components/silke-rich-text-field/formatting-toolbar.tsx +145 -0
- package/src/components/silke-rich-text-field/index.ts +1 -0
- package/src/components/silke-rich-text-field/jsdom-rects.ts +15 -0
- package/src/components/silke-rich-text-field/link-menu.spec.tsx +502 -0
- package/src/components/silke-rich-text-field/link-menu.tsx +553 -0
- package/src/components/silke-rich-text-field/link-suggestions.spec.ts +203 -0
- package/src/components/silke-rich-text-field/link-suggestions.ts +188 -0
- package/src/components/silke-rich-text-field/link-tooltip.tsx +60 -0
- package/src/components/silke-rich-text-field/list-item.ts +64 -0
- package/src/components/silke-rich-text-field/list-link-fontsize.spec.ts +135 -0
- package/src/components/silke-rich-text-field/lorem-ipsum.spec.ts +131 -0
- package/src/components/silke-rich-text-field/lorem-ipsum.ts +51 -0
- package/src/components/silke-rich-text-field/paste.spec.ts +93 -0
- package/src/components/silke-rich-text-field/paste.ts +39 -0
- package/src/components/silke-rich-text-field/plugins.spec.ts +143 -0
- package/src/components/silke-rich-text-field/plugins.ts +230 -0
- package/src/components/silke-rich-text-field/silke-rich-text-field.mdx +180 -0
- package/src/components/silke-rich-text-field/silke-rich-text-field.scss +343 -0
- package/src/components/silke-rich-text-field/silke-rich-text-field.tsx +505 -0
- package/src/components/silke-rich-text-field/slash-menu-items.ts +112 -0
- package/src/components/silke-rich-text-field/slash-menu.tsx +50 -0
- package/src/components/silke-rich-text-field/table-content-sync.spec.tsx +61 -0
- package/src/components/silke-rich-text-field/table-toolbar.spec.tsx +168 -0
- package/src/components/silke-rich-text-field/table-toolbar.tsx +121 -0
- package/src/components/silke-rich-text-field/table.spec.ts +87 -0
- package/src/components/silke-rich-text-field/use-link-hover.ts +63 -0
- package/src/components/silke-rich-text-field/use-slash-menu.ts +130 -0
- package/src/components/silke-rich-text-field/utils.ts +55 -0
- package/src/components/silke-sortable-list/index.ts +1 -1
- package/src/components/silke-sortable-list/silke-nested-sortable-list.scss +11 -1
- package/src/components/silke-sortable-list/silke-nested-sortable-list.tsx +9 -0
- package/src/components/silke-sortable-list/silke-sortable-list.mdx +13 -0
- package/src/components/silke-tag/silke-tag.scss +12 -3
- package/src/components/silke-theme-card/silke-theme-card.module.scss +141 -45
- package/src/components/silke-theme-card/silke-theme-card.tsx +76 -47
- package/src/components/silke-theme-swatch/silke-theme-swatch.mdx +56 -0
- package/src/components/silke-theme-swatch/silke-theme-swatch.module.scss +53 -63
- package/src/components/silke-theme-swatch/silke-theme-swatch.tsx +99 -14
- package/src/icons.tsx +51 -5
- package/tsconfig.build.tsbuildinfo +0 -1
|
@@ -3,9 +3,11 @@ import {
|
|
|
3
3
|
escapeHtml,
|
|
4
4
|
filterAndSortCommands,
|
|
5
5
|
getActiveCommandIndex,
|
|
6
|
+
getCommandLabel,
|
|
6
7
|
getCommandMatchScore,
|
|
7
8
|
getItemLength,
|
|
8
9
|
getTextLength,
|
|
10
|
+
insertCharacterAtCommandBoundary,
|
|
9
11
|
isAttachment,
|
|
10
12
|
isFile,
|
|
11
13
|
isImage,
|
|
@@ -85,6 +87,21 @@ describe('getActiveCommandIndex', () => {
|
|
|
85
87
|
test('handles undefined value array', () => {
|
|
86
88
|
expect(getActiveCommandIndex(0, undefined as unknown as SilkeCommandTextFieldValue[])).toBe(0);
|
|
87
89
|
});
|
|
90
|
+
|
|
91
|
+
test('uses the rendered label so a caret after a mention is not on the chip', () => {
|
|
92
|
+
const mention = {
|
|
93
|
+
label: 'Ada Lovelace',
|
|
94
|
+
type: SilkeCommandType.MENTION,
|
|
95
|
+
value: 'uid-abcdefghijklmnopqrstuvwxyz',
|
|
96
|
+
};
|
|
97
|
+
const value: SilkeCommandTextFieldValue[] = [mention, { type: 'text', value: ' ' }];
|
|
98
|
+
const chipLength = getItemLength(mention);
|
|
99
|
+
|
|
100
|
+
expect(getActiveCommandIndex(0, value)).toBe(0);
|
|
101
|
+
expect(getActiveCommandIndex(chipLength - 1, value)).toBe(0);
|
|
102
|
+
expect(getActiveCommandIndex(chipLength, value)).toBe(1);
|
|
103
|
+
expect(getActiveCommandIndex(chipLength + 1, value)).toBe(1);
|
|
104
|
+
});
|
|
88
105
|
});
|
|
89
106
|
|
|
90
107
|
describe('getTextLength', () => {
|
|
@@ -472,3 +489,178 @@ describe('functions skip file items', () => {
|
|
|
472
489
|
expect(html).toBe('<span>Hello</span><span> world</span>');
|
|
473
490
|
});
|
|
474
491
|
});
|
|
492
|
+
|
|
493
|
+
describe('labelled commands', () => {
|
|
494
|
+
const mockStyles = {
|
|
495
|
+
action: 'action-class',
|
|
496
|
+
mention: 'mention-class',
|
|
497
|
+
partial: 'partial-class',
|
|
498
|
+
tag: 'tag-class',
|
|
499
|
+
};
|
|
500
|
+
const labelled = {
|
|
501
|
+
type: SilkeCommandType.MENTION,
|
|
502
|
+
value: 'uid-1',
|
|
503
|
+
label: 'Nicolay Hansen',
|
|
504
|
+
};
|
|
505
|
+
|
|
506
|
+
test('getCommandLabel falls back to the value when there is no label', () => {
|
|
507
|
+
expect(getCommandLabel({ type: SilkeCommandType.MENTION, value: 'user' })).toBe('user');
|
|
508
|
+
expect(getCommandLabel(labelled)).toBe('Nicolay Hansen');
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
test('getItemLength measures the rendered label, not the value', () => {
|
|
512
|
+
// '@' + 'Nicolay Hansen'
|
|
513
|
+
expect(getItemLength(labelled)).toBe(15);
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
test('getTextLength measures labels so the caret lands correctly', () => {
|
|
517
|
+
const value = [{ type: 'text' as const, value: 'Hi ' }, labelled];
|
|
518
|
+
expect(getTextLength(value)).toBe(18);
|
|
519
|
+
expect(getTextLength(value, 1)).toBe(3);
|
|
520
|
+
});
|
|
521
|
+
|
|
522
|
+
test('valueToHtml renders the label with non-collapsing spaces', () => {
|
|
523
|
+
const html = valueToHtml([labelled], mockStyles);
|
|
524
|
+
expect(html).toContain('@Nicolay Hansen');
|
|
525
|
+
expect(html).not.toContain('uid-1');
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
test('valueToHtml escapes a label containing markup', () => {
|
|
529
|
+
const html = valueToHtml(
|
|
530
|
+
[{ type: SilkeCommandType.MENTION, value: 'uid-2', label: '<img src=x>' }],
|
|
531
|
+
mockStyles,
|
|
532
|
+
);
|
|
533
|
+
expect(html).not.toContain('<img');
|
|
534
|
+
expect(html).toContain('<img');
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
test('getCommandMatchScore scores against the label the user sees', () => {
|
|
538
|
+
expect(getCommandMatchScore(labelled, 'Nicolay')).toBeGreaterThan(0);
|
|
539
|
+
expect(getCommandMatchScore(labelled, 'Hansen')).toBeGreaterThan(0);
|
|
540
|
+
});
|
|
541
|
+
|
|
542
|
+
test('filterAndSortCommands finds a labelled command by its label', () => {
|
|
543
|
+
const result = filterAndSortCommands([labelled], 'nico', SilkeCommandType.MENTION);
|
|
544
|
+
expect(result).toEqual([labelled]);
|
|
545
|
+
});
|
|
546
|
+
|
|
547
|
+
test('getActiveCommandIndex uses the label so typing after the mention is in the text', () => {
|
|
548
|
+
const value: SilkeCommandTextFieldValue[] = [labelled, { type: 'text', value: ' ' }];
|
|
549
|
+
expect(getActiveCommandIndex(getItemLength(labelled), value)).toBe(1);
|
|
550
|
+
expect(getActiveCommandIndex(getItemLength(labelled) + 1, value)).toBe(1);
|
|
551
|
+
});
|
|
552
|
+
});
|
|
553
|
+
|
|
554
|
+
describe('insertCharacterAtCommandBoundary', () => {
|
|
555
|
+
const mention = {
|
|
556
|
+
label: 'Ada Lovelace',
|
|
557
|
+
type: SilkeCommandType.MENTION,
|
|
558
|
+
value: 'uid-abcdefghijklmnopqrstuvwxyz',
|
|
559
|
+
};
|
|
560
|
+
|
|
561
|
+
test('appends left-to-right after a completed mention and its trailing space', () => {
|
|
562
|
+
let value: SilkeCommandTextFieldValue[] = [mention, { type: 'text', value: ' ' }];
|
|
563
|
+
// Caret after the chip + space, still measured from the mention (the bug path)
|
|
564
|
+
let localOffset = getItemLength(mention) + 1;
|
|
565
|
+
|
|
566
|
+
for (const char of 'yes') {
|
|
567
|
+
const result = insertCharacterAtCommandBoundary(value, 0, localOffset, char);
|
|
568
|
+
expect(result).not.toBeNull();
|
|
569
|
+
value = result!.value;
|
|
570
|
+
localOffset = getItemLength(mention) + result!.localOffset;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
expect(value).toEqual([mention, { type: 'text', value: ' yes' }]);
|
|
574
|
+
});
|
|
575
|
+
|
|
576
|
+
test('does not reverse letters when the active index is still the mention chip', () => {
|
|
577
|
+
let value: SilkeCommandTextFieldValue[] = [mention, { type: 'text', value: ' ' }];
|
|
578
|
+
let caretPos = getItemLength(mention) + 1;
|
|
579
|
+
|
|
580
|
+
for (const char of 'yes') {
|
|
581
|
+
const commandIndex = getActiveCommandIndex(caretPos, value);
|
|
582
|
+
const item = value[commandIndex];
|
|
583
|
+
if (isSilkeCommand(item) && !item.partialMatch) {
|
|
584
|
+
const result = insertCharacterAtCommandBoundary(
|
|
585
|
+
value,
|
|
586
|
+
commandIndex,
|
|
587
|
+
caretPos - getTextLength(value, commandIndex),
|
|
588
|
+
char,
|
|
589
|
+
);
|
|
590
|
+
expect(result).not.toBeNull();
|
|
591
|
+
value = result!.value;
|
|
592
|
+
caretPos = getTextLength(value, result!.index) + result!.localOffset;
|
|
593
|
+
} else if (isText(item)) {
|
|
594
|
+
const offset = caretPos - getTextLength(value, commandIndex);
|
|
595
|
+
const next = [...value];
|
|
596
|
+
next[commandIndex] = {
|
|
597
|
+
type: 'text',
|
|
598
|
+
value: item.value.slice(0, offset) + char + item.value.slice(offset),
|
|
599
|
+
};
|
|
600
|
+
value = next;
|
|
601
|
+
caretPos += 1;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
expect(value).toEqual([mention, { type: 'text', value: ' yes' }]);
|
|
606
|
+
});
|
|
607
|
+
|
|
608
|
+
test('inserts before a mention by appending to the previous text', () => {
|
|
609
|
+
const value: SilkeCommandTextFieldValue[] = [
|
|
610
|
+
{ type: 'text', value: 'Hi' },
|
|
611
|
+
mention,
|
|
612
|
+
];
|
|
613
|
+
expect(insertCharacterAtCommandBoundary(value, 1, 0, '!')).toEqual({
|
|
614
|
+
index: 0,
|
|
615
|
+
localOffset: 3,
|
|
616
|
+
value: [{ type: 'text', value: 'Hi!' }, mention],
|
|
617
|
+
});
|
|
618
|
+
});
|
|
619
|
+
|
|
620
|
+
test('inserts a new text segment before a mention with no previous text', () => {
|
|
621
|
+
expect(insertCharacterAtCommandBoundary([mention], 0, 0, 'x')).toEqual({
|
|
622
|
+
index: 0,
|
|
623
|
+
localOffset: 1,
|
|
624
|
+
value: [{ type: 'text', value: 'x' }, mention],
|
|
625
|
+
});
|
|
626
|
+
});
|
|
627
|
+
|
|
628
|
+
test('inserts at the start of the following text when the caret is at the chip end', () => {
|
|
629
|
+
const value: SilkeCommandTextFieldValue[] = [mention, { type: 'text', value: ' later' }];
|
|
630
|
+
expect(insertCharacterAtCommandBoundary(value, 0, getItemLength(mention), 'x')).toEqual({
|
|
631
|
+
index: 1,
|
|
632
|
+
localOffset: 1,
|
|
633
|
+
value: [mention, { type: 'text', value: 'x later' }],
|
|
634
|
+
});
|
|
635
|
+
});
|
|
636
|
+
|
|
637
|
+
test('inserts after a mention when there is no following text', () => {
|
|
638
|
+
expect(insertCharacterAtCommandBoundary([mention], 0, getItemLength(mention), 'x')).toEqual({
|
|
639
|
+
index: 1,
|
|
640
|
+
localOffset: 1,
|
|
641
|
+
value: [mention, { type: 'text', value: 'x' }],
|
|
642
|
+
});
|
|
643
|
+
});
|
|
644
|
+
|
|
645
|
+
test('returns null when the caret is strictly inside the command', () => {
|
|
646
|
+
expect(insertCharacterAtCommandBoundary([mention], 0, 2, 'x')).toBeNull();
|
|
647
|
+
});
|
|
648
|
+
|
|
649
|
+
test('appends between two mentions into the separating space', () => {
|
|
650
|
+
const second = {
|
|
651
|
+
label: 'Alan Turing',
|
|
652
|
+
type: SilkeCommandType.MENTION,
|
|
653
|
+
value: 'uid-2',
|
|
654
|
+
};
|
|
655
|
+
const value: SilkeCommandTextFieldValue[] = [
|
|
656
|
+
mention,
|
|
657
|
+
{ type: 'text', value: ' ' },
|
|
658
|
+
second,
|
|
659
|
+
];
|
|
660
|
+
expect(insertCharacterAtCommandBoundary(value, 0, getItemLength(mention) + 1, 'x')).toEqual({
|
|
661
|
+
index: 1,
|
|
662
|
+
localOffset: 2,
|
|
663
|
+
value: [mention, { type: 'text', value: ' x' }, second],
|
|
664
|
+
});
|
|
665
|
+
});
|
|
666
|
+
});
|
|
@@ -7,8 +7,22 @@ export type SilkeCommand = {
|
|
|
7
7
|
partialMatch?: boolean;
|
|
8
8
|
type: SilkeCommandType;
|
|
9
9
|
value: string;
|
|
10
|
+
/**
|
|
11
|
+
* Text shown in the field in place of `value`. Use it when the value is an
|
|
12
|
+
* opaque id (a user id, say) but the field should read as a human name.
|
|
13
|
+
*
|
|
14
|
+
* The field parses its own rendered text back into values, so labels must be
|
|
15
|
+
* unique within `availableCommands` — two commands sharing a label cannot be
|
|
16
|
+
* told apart once written.
|
|
17
|
+
*/
|
|
18
|
+
label?: string;
|
|
10
19
|
};
|
|
11
20
|
|
|
21
|
+
/** The text a command renders as, without its `@` or `/` prefix */
|
|
22
|
+
export function getCommandLabel(command: SilkeCommand): string {
|
|
23
|
+
return command.label ?? command.value;
|
|
24
|
+
}
|
|
25
|
+
|
|
12
26
|
export type SilkeTextValue = {
|
|
13
27
|
type: 'text';
|
|
14
28
|
value: string;
|
|
@@ -130,10 +144,10 @@ export function getActiveCommandIndex(
|
|
|
130
144
|
let textLength = 0;
|
|
131
145
|
|
|
132
146
|
for (let i = 0; i < value.length; i++) {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
147
|
+
// Use rendered length (label + @/ prefix), not the stored value. Mentions
|
|
148
|
+
// store a uid that is longer than the name shown in the field; measuring
|
|
149
|
+
// the uid keeps the caret "inside" the chip after a completed mention.
|
|
150
|
+
textLength += getItemLength(value[i]);
|
|
137
151
|
if (textLength > caretPos) return i;
|
|
138
152
|
}
|
|
139
153
|
|
|
@@ -147,16 +161,81 @@ export function getTextLength(value: SilkeCommandTextFieldValue[], index?: numbe
|
|
|
147
161
|
const endIndex = index ?? value.length;
|
|
148
162
|
if (endIndex <= 0) return 0;
|
|
149
163
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
return acc + (curr.type === 'text' ? curr.value.length : curr.value.length + 1);
|
|
153
|
-
}, 0);
|
|
164
|
+
// Attachments render no text, so getItemLength scores them 0
|
|
165
|
+
return value.slice(0, endIndex).reduce((acc, curr) => acc + getItemLength(curr), 0);
|
|
154
166
|
}
|
|
155
167
|
|
|
156
168
|
/** Get the rendered length of a single command/text value */
|
|
157
169
|
export function getItemLength(item: SilkeCommandTextFieldValue): number {
|
|
158
170
|
if (!item || item.type === 'image' || item.type === 'file') return 0;
|
|
159
|
-
|
|
171
|
+
// +1 for the `@` or `/` prefix the command renders with
|
|
172
|
+
return item.type === 'text' ? item.value.length : getCommandLabel(item).length + 1;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export interface InsertCharacterAtCommandBoundaryResult {
|
|
176
|
+
index: number;
|
|
177
|
+
localOffset: number;
|
|
178
|
+
value: SilkeCommandTextFieldValue[];
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Insert a typed character at the boundary of a completed command.
|
|
183
|
+
* Returns null when the caret is strictly inside the command (no insertion).
|
|
184
|
+
*
|
|
185
|
+
* After a mention the field inserts a trailing space. If the caret is past the
|
|
186
|
+
* chip, insert at that offset in the following text — do not always prepend,
|
|
187
|
+
* or each keystroke lands at the front and reverses the word.
|
|
188
|
+
*/
|
|
189
|
+
export function insertCharacterAtCommandBoundary(
|
|
190
|
+
value: SilkeCommandTextFieldValue[],
|
|
191
|
+
commandIndex: number,
|
|
192
|
+
localOffset: number,
|
|
193
|
+
char: string,
|
|
194
|
+
): InsertCharacterAtCommandBoundaryResult | null {
|
|
195
|
+
const commandItem = value[commandIndex];
|
|
196
|
+
if (!commandItem || !char || char.length !== 1) return null;
|
|
197
|
+
|
|
198
|
+
const itemLength = getItemLength(commandItem);
|
|
199
|
+
const newValue = [...value];
|
|
200
|
+
|
|
201
|
+
if (localOffset <= 0) {
|
|
202
|
+
const prevItem = newValue[commandIndex - 1];
|
|
203
|
+
if (commandIndex > 0 && isText(prevItem)) {
|
|
204
|
+
newValue[commandIndex - 1] = {
|
|
205
|
+
type: 'text',
|
|
206
|
+
value: prevItem.value + char,
|
|
207
|
+
};
|
|
208
|
+
return {
|
|
209
|
+
index: commandIndex - 1,
|
|
210
|
+
localOffset: prevItem.value.length + 1,
|
|
211
|
+
value: newValue,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
newValue.splice(commandIndex, 0, { type: 'text', value: char });
|
|
216
|
+
return { index: commandIndex, localOffset: 1, value: newValue };
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (localOffset >= itemLength) {
|
|
220
|
+
const nextItem = newValue[commandIndex + 1];
|
|
221
|
+
if (commandIndex < newValue.length - 1 && isText(nextItem)) {
|
|
222
|
+
const insertAt = Math.max(0, Math.min(localOffset - itemLength, nextItem.value.length));
|
|
223
|
+
newValue[commandIndex + 1] = {
|
|
224
|
+
type: 'text',
|
|
225
|
+
value: nextItem.value.slice(0, insertAt) + char + nextItem.value.slice(insertAt),
|
|
226
|
+
};
|
|
227
|
+
return {
|
|
228
|
+
index: commandIndex + 1,
|
|
229
|
+
localOffset: insertAt + 1,
|
|
230
|
+
value: newValue,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
newValue.splice(commandIndex + 1, 0, { type: 'text', value: char });
|
|
235
|
+
return { index: commandIndex + 1, localOffset: 1, value: newValue };
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return null;
|
|
160
239
|
}
|
|
161
240
|
|
|
162
241
|
/** Convert command values to HTML for rendering (excludes images) */
|
|
@@ -172,19 +251,22 @@ export function valueToHtml(
|
|
|
172
251
|
item.type !== 'image' && item.type !== 'file',
|
|
173
252
|
)
|
|
174
253
|
.map((item) => {
|
|
175
|
-
const escapedValue = escapeHtml(item.value);
|
|
176
|
-
|
|
177
254
|
if (item.type === 'text') {
|
|
178
255
|
// Preserve spaces by replacing them with non-breaking spaces in space-only segments
|
|
179
256
|
// This prevents browser whitespace collapsing
|
|
180
257
|
const preservedValue =
|
|
181
|
-
item.value.trim() === ''
|
|
258
|
+
item.value.trim() === ''
|
|
259
|
+
? item.value.replace(/ /g, '\u00A0')
|
|
260
|
+
: escapeHtml(item.value);
|
|
182
261
|
return `<span>${preservedValue}</span>`;
|
|
183
262
|
}
|
|
184
263
|
|
|
185
264
|
const typeClass = item.type === SilkeCommandType.MENTION ? styles.mention : styles.action;
|
|
186
265
|
const partialClass = item.partialMatch ? styles.partial : '';
|
|
187
|
-
|
|
266
|
+
// A label may contain spaces; keep them from collapsing so the rendered
|
|
267
|
+
// text still round-trips back through the parser
|
|
268
|
+
const label = escapeHtml(getCommandLabel(item)).replace(/ /g, '\u00A0');
|
|
269
|
+
return `<span class="${styles.tag} ${typeClass} ${partialClass}">${item.type}${label}</span>`;
|
|
188
270
|
})
|
|
189
271
|
.join('');
|
|
190
272
|
}
|
|
@@ -244,7 +326,8 @@ export function getCommandMatchScore(command: SilkeCommand, query: string): numb
|
|
|
244
326
|
if (!query || !command.value) return -1;
|
|
245
327
|
|
|
246
328
|
const queryLower = query.toLowerCase();
|
|
247
|
-
|
|
329
|
+
// Match what the user sees and types, which is the label when there is one
|
|
330
|
+
const valueLower = getCommandLabel(command).toLowerCase();
|
|
248
331
|
|
|
249
332
|
// Exact match (highest priority)
|
|
250
333
|
if (valueLower === queryLower) return 100;
|
|
@@ -307,7 +390,7 @@ export function filterAndSortCommands(
|
|
|
307
390
|
|
|
308
391
|
// If no query, return all commands of this type sorted alphabetically
|
|
309
392
|
if (!query) {
|
|
310
|
-
return [...typeCommands].sort((a, b) => a.
|
|
393
|
+
return [...typeCommands].sort((a, b) => getCommandLabel(a).localeCompare(getCommandLabel(b)));
|
|
311
394
|
}
|
|
312
395
|
|
|
313
396
|
const scored = typeCommands
|
|
@@ -317,7 +400,7 @@ export function filterAndSortCommands(
|
|
|
317
400
|
// Sort by score descending, then alphabetically for ties
|
|
318
401
|
scored.sort((a, b) => {
|
|
319
402
|
if (b.score !== a.score) return b.score - a.score;
|
|
320
|
-
return a.cmd.
|
|
403
|
+
return getCommandLabel(a.cmd).localeCompare(getCommandLabel(b.cmd));
|
|
321
404
|
});
|
|
322
405
|
|
|
323
406
|
return scored.map(({ cmd }) => cmd);
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { fireEvent, render, screen } from '@testing-library/react';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { SilkeCssNumberField } from './silke-css-number-field';
|
|
4
|
+
|
|
5
|
+
const pasteData = ({
|
|
6
|
+
files = [],
|
|
7
|
+
html = '',
|
|
8
|
+
text = '',
|
|
9
|
+
}: {
|
|
10
|
+
files?: File[];
|
|
11
|
+
html?: string;
|
|
12
|
+
text?: string;
|
|
13
|
+
}) => ({
|
|
14
|
+
clipboardData: {
|
|
15
|
+
files,
|
|
16
|
+
getData: (type: string) => {
|
|
17
|
+
if (type === 'text/html') return html;
|
|
18
|
+
if (type === 'text/plain') return text;
|
|
19
|
+
return '';
|
|
20
|
+
},
|
|
21
|
+
items: files.map((file) => ({
|
|
22
|
+
getAsFile: () => file,
|
|
23
|
+
kind: 'file',
|
|
24
|
+
type: file.type,
|
|
25
|
+
})),
|
|
26
|
+
types: [
|
|
27
|
+
...(text ? ['text/plain'] : []),
|
|
28
|
+
...(html ? ['text/html'] : []),
|
|
29
|
+
...(files.length ? ['Files'] : []),
|
|
30
|
+
],
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe('SilkeCssNumberField paste (E-1245)', () => {
|
|
35
|
+
const execCommand = jest.fn();
|
|
36
|
+
|
|
37
|
+
beforeEach(() => {
|
|
38
|
+
execCommand.mockReset();
|
|
39
|
+
execCommand.mockImplementation((command: string, _ui?: boolean, value?: string) => {
|
|
40
|
+
if (command !== 'insertText' || !value) return false;
|
|
41
|
+
const el = screen.getByRole('textbox');
|
|
42
|
+
el.textContent = value;
|
|
43
|
+
el.innerText = value;
|
|
44
|
+
fireEvent.input(el);
|
|
45
|
+
return true;
|
|
46
|
+
});
|
|
47
|
+
Object.defineProperty(document, 'execCommand', {
|
|
48
|
+
configurable: true,
|
|
49
|
+
value: execCommand,
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const renderField = (onChange = jest.fn()) => {
|
|
54
|
+
render(<SilkeCssNumberField formats={['%', 'px']} onChange={onChange} value="10px" />);
|
|
55
|
+
const field = screen.getByRole('textbox');
|
|
56
|
+
fireEvent.focus(field);
|
|
57
|
+
return { field, onChange };
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
it('does not insert an image or HTML when pasting an image file', () => {
|
|
61
|
+
const { field } = renderField();
|
|
62
|
+
const file = new File(['png'], 'shot.png', { type: 'image/png' });
|
|
63
|
+
|
|
64
|
+
fireEvent.paste(
|
|
65
|
+
field,
|
|
66
|
+
pasteData({
|
|
67
|
+
files: [file],
|
|
68
|
+
html: '<img src="data:image/png;base64,abc" alt="shot">',
|
|
69
|
+
}),
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
expect(field.querySelector('img')).toBeNull();
|
|
73
|
+
expect(field.innerHTML).not.toContain('<img');
|
|
74
|
+
expect(execCommand).not.toHaveBeenCalled();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('does not insert rich HTML when the clipboard has no plain text', () => {
|
|
78
|
+
const { field } = renderField();
|
|
79
|
+
|
|
80
|
+
fireEvent.paste(
|
|
81
|
+
field,
|
|
82
|
+
pasteData({ html: '<img src="https://example.com/a.png"><b>24px</b>' }),
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
expect(field.querySelector('img')).toBeNull();
|
|
86
|
+
expect(field.querySelector('b')).toBeNull();
|
|
87
|
+
expect(execCommand).not.toHaveBeenCalled();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('inserts pasted plain text including format suffixes', () => {
|
|
91
|
+
const { field, onChange } = renderField();
|
|
92
|
+
|
|
93
|
+
fireEvent.paste(field, pasteData({ text: '24px' }));
|
|
94
|
+
|
|
95
|
+
expect(field.querySelector('img')).toBeNull();
|
|
96
|
+
expect(execCommand).toHaveBeenCalledWith('insertText', false, '24px');
|
|
97
|
+
expect(onChange).toHaveBeenCalledWith('24px');
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('uses text/plain when the clipboard also has HTML with an image', () => {
|
|
101
|
+
const { field, onChange } = renderField();
|
|
102
|
+
|
|
103
|
+
fireEvent.paste(
|
|
104
|
+
field,
|
|
105
|
+
pasteData({
|
|
106
|
+
html: '<img src="data:image/png;base64,abc">50%',
|
|
107
|
+
text: '50%',
|
|
108
|
+
}),
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
expect(field.querySelector('img')).toBeNull();
|
|
112
|
+
expect(execCommand).toHaveBeenCalledWith('insertText', false, '50%');
|
|
113
|
+
expect(onChange).toHaveBeenCalledWith('50%');
|
|
114
|
+
});
|
|
115
|
+
});
|
|
@@ -52,6 +52,22 @@ const handleSelectText = (el: HTMLDivElement, selectAll: boolean) => {
|
|
|
52
52
|
|
|
53
53
|
const preventDefault = (e: React.SyntheticEvent) => e.preventDefault();
|
|
54
54
|
|
|
55
|
+
function insertPlainTextAtCaret(text: string, fallbackEl: HTMLDivElement | null) {
|
|
56
|
+
const selection = window.getSelection();
|
|
57
|
+
if (selection?.rangeCount) {
|
|
58
|
+
const range = selection.getRangeAt(0);
|
|
59
|
+
range.deleteContents();
|
|
60
|
+
const node = document.createTextNode(text);
|
|
61
|
+
range.insertNode(node);
|
|
62
|
+
range.setStartAfter(node);
|
|
63
|
+
range.collapse(true);
|
|
64
|
+
selection.removeAllRanges();
|
|
65
|
+
selection.addRange(range);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (fallbackEl) fallbackEl.innerText = text;
|
|
69
|
+
}
|
|
70
|
+
|
|
55
71
|
export function SilkeCssNumberField(props: SilkeCssNumberFieldProps) {
|
|
56
72
|
const {
|
|
57
73
|
children,
|
|
@@ -207,6 +223,22 @@ export function SilkeCssNumberField(props: SilkeCssNumberFieldProps) {
|
|
|
207
223
|
}
|
|
208
224
|
}, [number, format, variables, isAuto, isMixed]);
|
|
209
225
|
|
|
226
|
+
const handlePaste = (e: React.ClipboardEvent<HTMLDivElement>) => {
|
|
227
|
+
e.preventDefault();
|
|
228
|
+
if (disabled) return;
|
|
229
|
+
|
|
230
|
+
const text = e.clipboardData.getData('text/plain');
|
|
231
|
+
if (!text) return;
|
|
232
|
+
|
|
233
|
+
const inserted =
|
|
234
|
+
typeof document.execCommand === 'function' &&
|
|
235
|
+
document.execCommand('insertText', false, text);
|
|
236
|
+
if (!inserted) {
|
|
237
|
+
insertPlainTextAtCaret(text, numberRef.current);
|
|
238
|
+
handleNumberChange();
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
|
|
210
242
|
const handleNumberKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {
|
|
211
243
|
if (openFormat) setFormatOpen(false);
|
|
212
244
|
let step = 0;
|
|
@@ -328,6 +360,7 @@ export function SilkeCssNumberField(props: SilkeCssNumberFieldProps) {
|
|
|
328
360
|
spellCheck={false}
|
|
329
361
|
onInput={disabled ? preventDefault : handleNumberChange}
|
|
330
362
|
onKeyDown={disabled ? preventDefault : handleNumberKeyDown}
|
|
363
|
+
onPaste={disabled ? preventDefault : handlePaste}
|
|
331
364
|
onFocus={(e) => {
|
|
332
365
|
handleFocus();
|
|
333
366
|
if (props.onFocus) props.onFocus(e.nativeEvent);
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { DisplayToken } from '@vev/variables';
|
|
3
|
+
import { SilkeBox } from '../silke-box';
|
|
4
|
+
import { SilkeText } from '../silke-text';
|
|
5
|
+
import { SilkeColor } from '../../silke-theme-provider';
|
|
6
|
+
|
|
7
|
+
export interface SilkeExpressionInputTagProps {
|
|
8
|
+
token: DisplayToken;
|
|
9
|
+
label?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const colorForToken = (token: DisplayToken): SilkeColor => {
|
|
13
|
+
switch (token.type) {
|
|
14
|
+
case 'variable':
|
|
15
|
+
return 'success-30';
|
|
16
|
+
case 'function':
|
|
17
|
+
return 'support-tertiary-40';
|
|
18
|
+
case 'number':
|
|
19
|
+
return 'neutral-60';
|
|
20
|
+
case 'operator':
|
|
21
|
+
case 'paren':
|
|
22
|
+
return 'neutral-100';
|
|
23
|
+
case 'unknown':
|
|
24
|
+
default:
|
|
25
|
+
return 'support-secondary-40';
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export function SilkeExpressionInputTag({ token, label }: SilkeExpressionInputTagProps) {
|
|
30
|
+
return (
|
|
31
|
+
<SilkeBox align="center">
|
|
32
|
+
<SilkeText size="small" color={colorForToken(token)}>
|
|
33
|
+
{label ?? token.value}
|
|
34
|
+
</SilkeText>
|
|
35
|
+
</SilkeBox>
|
|
36
|
+
);
|
|
37
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# SilkeExpressionInput
|
|
2
|
+
|
|
3
|
+
A pill-based input for math expressions. Users build expressions by selecting variables, operators, and functions from a context-aware suggestion popover, or by typing values directly. Operators (`+`, `-`, `*`, `/`, `^`), parentheses, and a set of math functions (`sqrt`, `sin`, `cos`, `round`, …) are supported.
|
|
4
|
+
|
|
5
|
+
The on-disk value is a plain expression string compatible with `@vev/variables`'s `evaluateExpression`.
|
|
6
|
+
|
|
7
|
+
## Basic Usage
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
function Example() {
|
|
11
|
+
const [value, setValue] = React.useState('count + 1');
|
|
12
|
+
return (
|
|
13
|
+
<SilkeExpressionInput
|
|
14
|
+
value={value}
|
|
15
|
+
onChange={setValue}
|
|
16
|
+
variables={[{ key: 'count', name: 'count' }]}
|
|
17
|
+
placeholder="Enter an expression"
|
|
18
|
+
/>
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Props
|
|
24
|
+
|
|
25
|
+
| Prop | Type | Description |
|
|
26
|
+
| ------------- | ----------------------------------------------------- | ------------------------------------------------------------------------ |
|
|
27
|
+
| `value` | `string` | The expression string. Empty means no tokens. |
|
|
28
|
+
| `onChange` | `(next: string) => void` | Called whenever the expression changes. |
|
|
29
|
+
| `variables` | `Array<{ key: string; name: string; type?: string }>` | Variables available for reference. Pill labels use `name`. |
|
|
30
|
+
| `functions` | `string[]` | Override the list of available math functions. |
|
|
31
|
+
| `placeholder` | `string` | Placeholder shown when the input is empty. |
|
|
32
|
+
| `disabled` | `boolean` | Renders the pills but disables editing. |
|
|
33
|
+
| `onClear` | `() => void` | Called after the user clicks the trailing × button. |
|
|
34
|
+
|
|
35
|
+
## Behaviour
|
|
36
|
+
|
|
37
|
+
- The popover suggestions are context-aware: after an operand the list shows operators and (when relevant) a closing paren; after an operator, `(`, or the start, it shows variables, functions, and `(`; after a function it offers `(`.
|
|
38
|
+
- Typing an operator character (`+`, `-`, `*`, `/`, `^`, `(`, `)`) commits the buffer and adds the operator/paren as a token.
|
|
39
|
+
- `Backspace` on an empty input buffer removes the last token; `Cmd+Backspace` clears everything.
|
|
40
|
+
- The clear (`×`) button appears whenever the expression has at least one token.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
.inner {
|
|
2
|
+
min-height: 32px;
|
|
3
|
+
column-gap: 4px;
|
|
4
|
+
align-items: center !important;
|
|
5
|
+
cursor: text;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
.field {
|
|
9
|
+
width: 40px;
|
|
10
|
+
line-height: 24px;
|
|
11
|
+
background-color: transparent;
|
|
12
|
+
font-size: 12px;
|
|
13
|
+
margin: 0;
|
|
14
|
+
padding: 0;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
.field[data-hidden='true'] {
|
|
18
|
+
position: absolute;
|
|
19
|
+
clip: rect(0, 0, 0, 0);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
.field[data-empty='true'] {
|
|
23
|
+
width: 100px;
|
|
24
|
+
}
|