@widgetic/design-system 0.5.40 → 0.5.41

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 (139) hide show
  1. package/README.md +73 -0
  2. package/dist/components/ContentRefreshOverlay.svelte +103 -0
  3. package/dist/components/ContentRefreshOverlay.svelte.d.ts +18 -0
  4. package/dist/components/chat/Composer.svelte +380 -19
  5. package/dist/components/chat/Composer.svelte.d.ts +88 -1
  6. package/dist/components/chat/PlanCard.svelte +218 -88
  7. package/dist/components/chat/PlanCard.svelte.d.ts +11 -0
  8. package/dist/components/chat/QuestionCard.svelte +180 -70
  9. package/dist/components/chat/QuestionCard.svelte.d.ts +28 -2
  10. package/dist/components/chat/QuestionCardSummary.svelte +96 -0
  11. package/dist/components/chat/QuestionCardSummary.svelte.d.ts +20 -0
  12. package/dist/components/chat/QueueCard.svelte +185 -0
  13. package/dist/components/chat/QueueCard.svelte.d.ts +40 -0
  14. package/dist/components/chat/composerLogic.d.ts +31 -0
  15. package/dist/components/chat/composerLogic.js +37 -0
  16. package/dist/components/chat/composerReferences.d.ts +28 -0
  17. package/dist/components/chat/composerReferences.js +23 -0
  18. package/dist/components/chat/index.d.ts +8 -3
  19. package/dist/components/chat/index.js +10 -2
  20. package/dist/components/chat/questionCardLogic.d.ts +40 -3
  21. package/dist/components/chat/questionCardLogic.js +54 -3
  22. package/dist/components/chat/queueCardLogic.d.ts +7 -0
  23. package/dist/components/chat/queueCardLogic.js +5 -0
  24. package/dist/components/creator/index.d.ts +1 -2
  25. package/dist/components/creator/index.js +2 -0
  26. package/dist/components/index.d.ts +8 -7
  27. package/dist/components/index.js +8 -6
  28. package/dist/components/modals/BaseModal.svelte +14 -4
  29. package/dist/components/navigation/breadcrumbSlideIn.d.ts +6 -2
  30. package/dist/components/navigation/breadcrumbSlideIn.js +6 -6
  31. package/dist/components/search-input/SearchInput.svelte +282 -14
  32. package/dist/components/search-input/SearchInput.svelte.d.ts +3 -0
  33. package/dist/components/search-input/index.d.ts +1 -0
  34. package/dist/components/search-input/index.js +1 -0
  35. package/dist/components/search-input/index2.d.ts +4 -0
  36. package/dist/components/search-input/recentSearches.d.ts +12 -0
  37. package/dist/components/search-input/recentSearches.js +124 -0
  38. package/dist/components/toast/ToasterModal.svelte +8 -3
  39. package/dist/components/toast/ToasterModal.svelte.d.ts +2 -0
  40. package/dist/components/ui/chip/chip.svelte +39 -1
  41. package/dist/components/ui/chip/chip.svelte.d.ts +8 -0
  42. package/dist/components/ui/chip/chipProgress.d.ts +14 -0
  43. package/dist/components/ui/chip/chipProgress.js +21 -0
  44. package/dist/components/ui/icon-button/icon-button.svelte +2 -0
  45. package/dist/components/ui/icon-button/icon-button.svelte.d.ts +5 -0
  46. package/dist/components/ui/number-controls/index.d.ts +2 -0
  47. package/dist/components/ui/number-controls/index.js +2 -0
  48. package/dist/components/ui/number-controls/number-controls.svelte +530 -0
  49. package/dist/components/ui/number-controls/number-controls.svelte.d.ts +46 -0
  50. package/dist/components/ui/number-controls/numberControlsSteps.d.ts +17 -0
  51. package/dist/components/ui/number-controls/numberControlsSteps.js +23 -0
  52. package/dist/components/ui/number-controls/numberControlsValueArea.d.ts +29 -0
  53. package/dist/components/ui/number-controls/numberControlsValueArea.js +51 -0
  54. package/dist/components/ui/text-flip/index.d.ts +1 -0
  55. package/dist/components/ui/text-flip/index.js +1 -0
  56. package/dist/components/ui/text-flip/text-flip.svelte +103 -0
  57. package/dist/components/ui/text-flip/text-flip.svelte.d.ts +15 -0
  58. package/dist/components/ui/text-flip/textFlipLogic.d.ts +49 -0
  59. package/dist/components/ui/text-flip/textFlipLogic.js +151 -0
  60. package/dist/inline-completion/completionAnswer.d.ts +11 -0
  61. package/dist/inline-completion/completionAnswer.js +49 -0
  62. package/dist/inline-completion/completionCache.d.ts +24 -0
  63. package/dist/inline-completion/completionCache.js +47 -0
  64. package/dist/inline-completion/completionController.d.ts +61 -0
  65. package/dist/inline-completion/completionController.js +280 -0
  66. package/dist/inline-completion/completionKeys.d.ts +21 -0
  67. package/dist/inline-completion/completionKeys.js +19 -0
  68. package/dist/inline-completion/completionText.d.ts +53 -0
  69. package/dist/inline-completion/completionText.js +79 -0
  70. package/dist/inline-completion/completionTypes.d.ts +71 -0
  71. package/dist/inline-completion/completionTypes.js +5 -0
  72. package/dist/inline-completion/gatewayCompletion.d.ts +37 -0
  73. package/dist/inline-completion/gatewayCompletion.js +77 -0
  74. package/dist/inline-completion/ghostTap.d.ts +19 -0
  75. package/dist/inline-completion/ghostTap.js +20 -0
  76. package/dist/inline-completion/hintMemory.d.ts +43 -0
  77. package/dist/inline-completion/hintMemory.js +90 -0
  78. package/dist/inline-completion/httpSource.d.ts +40 -0
  79. package/dist/inline-completion/httpSource.js +149 -0
  80. package/dist/inline-completion/index.d.ts +30 -0
  81. package/dist/inline-completion/index.js +20 -0
  82. package/dist/inline-completion/inlineCompletionAction.d.ts +54 -0
  83. package/dist/inline-completion/inlineCompletionAction.js +310 -0
  84. package/dist/inline-completion/outcomeReporter.d.ts +49 -0
  85. package/dist/inline-completion/outcomeReporter.js +139 -0
  86. package/dist/inline-completion/promiseLike.d.ts +9 -0
  87. package/dist/inline-completion/promiseLike.js +9 -0
  88. package/dist/prompt-references/ReferenceMenu.svelte +344 -0
  89. package/dist/prompt-references/ReferenceMenu.svelte.d.ts +26 -0
  90. package/dist/prompt-references/index.d.ts +27 -0
  91. package/dist/prompt-references/index.js +20 -0
  92. package/dist/prompt-references/neutralizingSource.d.ts +21 -0
  93. package/dist/prompt-references/neutralizingSource.js +29 -0
  94. package/dist/prompt-references/promptReferences.d.ts +28 -0
  95. package/dist/prompt-references/promptReferences.js +766 -0
  96. package/dist/prompt-references/referenceBadge.d.ts +61 -0
  97. package/dist/prompt-references/referenceBadge.js +82 -0
  98. package/dist/prompt-references/referenceBadgePainter.d.ts +31 -0
  99. package/dist/prompt-references/referenceBadgePainter.js +106 -0
  100. package/dist/prompt-references/referenceEditing.d.ts +60 -0
  101. package/dist/prompt-references/referenceEditing.js +135 -0
  102. package/dist/prompt-references/referenceLabels.d.ts +56 -0
  103. package/dist/prompt-references/referenceLabels.js +215 -0
  104. package/dist/prompt-references/referenceMeasure.d.ts +12 -0
  105. package/dist/prompt-references/referenceMeasure.js +63 -0
  106. package/dist/prompt-references/referenceMenuHost.svelte.d.ts +29 -0
  107. package/dist/prompt-references/referenceMenuHost.svelte.js +43 -0
  108. package/dist/prompt-references/referenceMenuKeys.d.ts +26 -0
  109. package/dist/prompt-references/referenceMenuKeys.js +49 -0
  110. package/dist/prompt-references/referenceMenuPlacement.d.ts +63 -0
  111. package/dist/prompt-references/referenceMenuPlacement.js +66 -0
  112. package/dist/prompt-references/referenceTokens.d.ts +175 -0
  113. package/dist/prompt-references/referenceTokens.js +332 -0
  114. package/dist/prompt-references/referenceTrigger.d.ts +52 -0
  115. package/dist/prompt-references/referenceTrigger.js +100 -0
  116. package/dist/prompt-references/referenceTypes.d.ts +38 -0
  117. package/dist/prompt-references/referenceTypes.js +4 -0
  118. package/dist/prompt-references/sentReferences.d.ts +50 -0
  119. package/dist/prompt-references/sentReferences.js +122 -0
  120. package/dist/styles/tokens.css +58 -0
  121. package/dist/textarea-overlay/index.d.ts +9 -0
  122. package/dist/textarea-overlay/index.js +7 -0
  123. package/dist/textarea-overlay/mirrorSegments.d.ts +31 -0
  124. package/dist/textarea-overlay/mirrorSegments.js +47 -0
  125. package/dist/textarea-overlay/mirrorSizing.d.ts +39 -0
  126. package/dist/textarea-overlay/mirrorSizing.js +40 -0
  127. package/dist/textarea-overlay/textareaOverlay.d.ts +25 -0
  128. package/dist/textarea-overlay/textareaOverlay.js +277 -0
  129. package/dist/utils/overlayMotion.d.ts +1 -0
  130. package/dist/utils/overlayMotion.js +3 -3
  131. package/dist/utils/reducedMotion.d.ts +9 -0
  132. package/dist/utils/reducedMotion.js +13 -0
  133. package/package.json +16 -1
  134. package/dist/components/ui/number-stepper/index.d.ts +0 -3
  135. package/dist/components/ui/number-stepper/index.js +0 -3
  136. package/dist/components/ui/number-stepper/number-stepper.svelte +0 -212
  137. package/dist/components/ui/number-stepper/number-stepper.svelte.d.ts +0 -24
  138. package/dist/components/ui/number-stepper/numberStepperSteps.d.ts +0 -26
  139. package/dist/components/ui/number-stepper/numberStepperSteps.js +0 -45
@@ -0,0 +1,215 @@
1
+ /**
2
+ * Labels of attached files: the word a person types after `#` to point at one ("make #hero_banner brighter"). Pure functions, no
3
+ * DOM, so Node's test runner loads this file.
4
+ *
5
+ * A label is made ONCE, when the file joins the tray, and kept: the text the person typed already contains it, so it is never
6
+ * worked out again. It is one unbreakable word (letters, digits and underscores, at most 24 characters), so a person can type it,
7
+ * a screen reader reads it and a model takes it as one name. The same rule is held by the gateway for the labels it receives
8
+ * (`attachmentReferences`); this file is the one place the browser makes them.
9
+ */
10
+ /** The longest label, counted in characters (code points), not in UTF-16 units. The gateway holds the same number. */
11
+ export const MAX_REFERENCE_LABEL_LENGTH = 24;
12
+ /** The characters a label is made of: letters and marks of any script, digits and the underscore. Never a space or a hyphen. */
13
+ export const REFERENCE_LABEL_CHARACTER = /[\p{L}\p{M}\p{N}_]/u;
14
+ /** A whole label: one to 24 of those characters. */
15
+ export const REFERENCE_LABEL_PATTERN = /^[\p{L}\p{M}\p{N}_]{1,24}$/u;
16
+ /** The word generic names become, with a number after it (`Image_1`). A host in another language may pass its own. */
17
+ export const DEFAULT_GENERIC_LABEL_WORD = 'Image';
18
+ /** How many characters of text there are, counting a character outside the basic plane once. */
19
+ export function countCharacters(text) {
20
+ let count = 0;
21
+ for (const _character of text)
22
+ count += 1;
23
+ return count;
24
+ }
25
+ /** The first `maxCharacters` characters of the text, never cutting a character outside the basic plane in two. */
26
+ export function truncateToCharacters(text, maxCharacters) {
27
+ if (maxCharacters <= 0)
28
+ return '';
29
+ let kept = '';
30
+ let count = 0;
31
+ for (const character of text) {
32
+ if (count >= maxCharacters)
33
+ break;
34
+ kept += character;
35
+ count += 1;
36
+ }
37
+ return kept;
38
+ }
39
+ /** Whether the text is a whole, valid label. */
40
+ export function isValidReferenceLabel(label) {
41
+ return REFERENCE_LABEL_PATTERN.test(label);
42
+ }
43
+ /** The file name without its extension: `hero-banner-final.png` is `hero-banner-final`. A dot that starts the name is not one. */
44
+ export function stripFileExtension(fileName) {
45
+ return fileName.replace(/(?<=.)\.[A-Za-z0-9]{1,8}$/u, '');
46
+ }
47
+ /** Text as label characters: every run of anything else becomes one underscore, and none is left at either end. */
48
+ function toLabelCharacters(text) {
49
+ return text
50
+ .normalize('NFC')
51
+ .replace(/[^\p{L}\p{M}\p{N}_]+/gu, '_')
52
+ .replace(/_{2,}/g, '_')
53
+ .replace(/^_+|_+$/g, '');
54
+ }
55
+ /**
56
+ * Words that say nothing about a picture: what a phone, a screenshot tool or a paste gives it. A name made only of these and of
57
+ * numbers is generic, and is labelled `Image_1`, `Image_2` instead of `Screenshot_2026_11_17_at_7_55_AM`. Compared in lower case
58
+ * without accents.
59
+ */
60
+ const GENERIC_NAME_WORDS = new Set([
61
+ 'image',
62
+ 'images',
63
+ 'img',
64
+ 'photo',
65
+ 'photos',
66
+ 'picture',
67
+ 'pictures',
68
+ 'pic',
69
+ 'pics',
70
+ 'screenshot',
71
+ 'screenshots',
72
+ 'screen',
73
+ 'shot',
74
+ 'snapshot',
75
+ 'capture',
76
+ 'scan',
77
+ 'untitled',
78
+ 'unnamed',
79
+ 'pasted',
80
+ 'paste',
81
+ 'clipboard',
82
+ 'download',
83
+ 'downloaded',
84
+ 'file',
85
+ 'blob',
86
+ 'copy',
87
+ 'new',
88
+ 'document',
89
+ 'attachment',
90
+ 'upload',
91
+ 'uploaded',
92
+ 'whatsapp',
93
+ 'signal',
94
+ 'telegram',
95
+ 'dsc',
96
+ 'dscn',
97
+ 'dscf',
98
+ 'pxl',
99
+ 'mvimg',
100
+ 'at',
101
+ 'on',
102
+ 'am',
103
+ 'pm',
104
+ // Other languages' versions of the same, as phones and tools write them.
105
+ 'imagen',
106
+ 'imagem',
107
+ 'imagine',
108
+ 'immagine',
109
+ 'bild',
110
+ 'bilder',
111
+ 'foto',
112
+ 'fotos',
113
+ 'captura',
114
+ 'pantalla',
115
+ 'ecran',
116
+ 'bildschirmfoto',
117
+ 'aufnahme',
118
+ 'poza',
119
+ 'fotografie',
120
+ 'sin',
121
+ 'titulo',
122
+ 'de'
123
+ ]);
124
+ /** A piece of a name that carries no meaning: digits, or a long run of hexadecimal (an id, a hash, a timestamp). */
125
+ function isNoisePiece(piece) {
126
+ return /^\d+$/.test(piece) || /^[0-9a-f]{8,}$/i.test(piece);
127
+ }
128
+ /** A piece of a name without its accents and in lower case, so `Écran` and `ecran` are the same word. */
129
+ function plainWord(piece) {
130
+ return piece
131
+ .normalize('NFD')
132
+ .replace(/\p{M}+/gu, '')
133
+ .toLowerCase();
134
+ }
135
+ /**
136
+ * Whether a file name says nothing about the file: empty, only numbers or ids, or only the generic words of cameras, screenshot
137
+ * tools and pastes (`image.png`, `IMG_1234.jpg`, `Screenshot 2026-11-17 at 7.55.01 AM.png`, `3f2a9c1e-7b…`).
138
+ */
139
+ export function isGenericFileName(fileName) {
140
+ const pieces = toLabelCharacters(stripFileExtension(fileName)).split('_').filter(Boolean);
141
+ if (pieces.length === 0)
142
+ return true;
143
+ return pieces.every((piece) => isNoisePiece(piece) || isGenericWord(piece));
144
+ }
145
+ /** A generic word, alone (`Screenshot`) or with its number written straight after it (`DSC01234`, `IMG2026`). */
146
+ function isGenericWord(piece) {
147
+ if (GENERIC_NAME_WORDS.has(plainWord(piece)))
148
+ return true;
149
+ const withNumber = /^(\p{L}+)\d+$/u.exec(piece);
150
+ return withNumber !== null && GENERIC_NAME_WORDS.has(plainWord(withNumber[1]));
151
+ }
152
+ /**
153
+ * The label a file name gives, or '' when the name says nothing (see `isGenericFileName`): the name without its extension, as label
154
+ * characters, cut at 24. `hero-banner-final.png` is `hero_banner_final`.
155
+ */
156
+ export function labelFromFileName(fileName) {
157
+ if (isGenericFileName(fileName))
158
+ return '';
159
+ const cleaned = toLabelCharacters(stripFileExtension(fileName));
160
+ // A cut can end on an underscore: that one goes too.
161
+ return truncateToCharacters(cleaned, MAX_REFERENCE_LABEL_LENGTH).replace(/_+$/g, '');
162
+ }
163
+ /** The number in `Image_3` (for the word given), or null when the label is not of that shape. */
164
+ export function genericLabelNumber(label, genericWord = DEFAULT_GENERIC_LABEL_WORD) {
165
+ const escapedWord = genericWord.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
166
+ const match = new RegExp(`^${escapedWord}_(\\d+)$`, 'iu').exec(label);
167
+ if (!match)
168
+ return null;
169
+ const number = Number.parseInt(match[1], 10);
170
+ return Number.isSafeInteger(number) ? number : null;
171
+ }
172
+ /** The most numbers tried for one name: far above what a tray can hold, so a loop can never run away. */
173
+ const MAX_LABEL_ATTEMPTS = 10_000;
174
+ /**
175
+ * The label for a file that joins the tray.
176
+ * - A name that says something gives it (`hero-banner-final.png` → `hero_banner_final`); a name already taken gets `_2`, `_3`, …
177
+ * (`hero_banner_final_2`), cut so the whole label stays within 24 characters.
178
+ * - A name that says nothing becomes `Image_N`, N above every generic number handed out so far.
179
+ * The answer is always a valid label.
180
+ */
181
+ export function assignReferenceLabel(fileName, options = {}) {
182
+ const taken = new Set();
183
+ for (const label of options.takenLabels ?? [])
184
+ taken.add(label.toLowerCase());
185
+ const genericWord = isValidReferenceLabel(options.genericWord ?? '')
186
+ ? options.genericWord
187
+ : DEFAULT_GENERIC_LABEL_WORD;
188
+ const named = labelFromFileName(fileName);
189
+ if (named !== '') {
190
+ if (!taken.has(named.toLowerCase()))
191
+ return named;
192
+ for (let attempt = 2; attempt < MAX_LABEL_ATTEMPTS; attempt += 1) {
193
+ const suffix = `_${attempt}`;
194
+ const room = MAX_REFERENCE_LABEL_LENGTH - countCharacters(suffix);
195
+ const stem = truncateToCharacters(named, room).replace(/_+$/g, '');
196
+ const candidate = `${stem}${suffix}`;
197
+ if (!taken.has(candidate.toLowerCase()))
198
+ return candidate;
199
+ }
200
+ }
201
+ // The highest generic number in the tray counts too: a tray restored from a draft carries labels this draft did not hand out.
202
+ let highest = Math.max(0, Math.floor(options.genericNumberFloor ?? 0));
203
+ for (const label of taken) {
204
+ const number = genericLabelNumber(label, genericWord);
205
+ if (number !== null && number > highest)
206
+ highest = number;
207
+ }
208
+ for (let number = highest + 1; number < highest + MAX_LABEL_ATTEMPTS; number += 1) {
209
+ const candidate = `${genericWord}_${number}`;
210
+ if (!taken.has(candidate.toLowerCase()))
211
+ return candidate;
212
+ }
213
+ // Unreachable with a real tray; a label that is valid and almost certainly free keeps the promise of the function.
214
+ return `${genericWord}_${highest + MAX_LABEL_ATTEMPTS}`;
215
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Two measurements of a text field, in CSS pixels: how wide one figure space is in the field's own font, and how tall one line is.
3
+ * The first decides how many figure spaces a token's rooms need (see `roomForFigureSpaceWidth`); the second is the height of the mark the
4
+ * menu hangs from. Both read the field as it is NOW (font, size and zoom change), and neither leaves anything in the page.
5
+ */
6
+ import { type TokenRoom } from './referenceTokens.ts';
7
+ /** The width of one figure space in the field's font, or 0 when it could not be measured (the field is not in the page). */
8
+ export declare function measureFigureSpaceWidth(field: HTMLTextAreaElement | HTMLInputElement): number;
9
+ /** How many figure spaces each room of a token needs in this field right now (the defaults when nothing could be measured). */
10
+ export declare function measureTokenRoom(field: HTMLTextAreaElement | HTMLInputElement): TokenRoom;
11
+ /** The height of one line of the field, in pixels: its `line-height` when it has a length, otherwise a line of its font (`normal`). */
12
+ export declare function measureLineHeight(field: HTMLTextAreaElement | HTMLInputElement): number;
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Two measurements of a text field, in CSS pixels: how wide one figure space is in the field's own font, and how tall one line is.
3
+ * The first decides how many figure spaces a token's rooms need (see `roomForFigureSpaceWidth`); the second is the height of the mark the
4
+ * menu hangs from. Both read the field as it is NOW (font, size and zoom change), and neither leaves anything in the page.
5
+ */
6
+ import { FIGURE_SPACE, roomForFigureSpaceWidth } from "./referenceTokens.js";
7
+ /** The properties that decide how wide a glyph is. */
8
+ const GLYPH_WIDTH_PROPERTIES = [
9
+ 'font-family',
10
+ 'font-size',
11
+ 'font-style',
12
+ 'font-weight',
13
+ 'font-stretch',
14
+ 'font-variant',
15
+ 'font-kerning',
16
+ 'font-feature-settings',
17
+ 'font-variation-settings',
18
+ 'letter-spacing',
19
+ 'word-spacing',
20
+ 'text-transform',
21
+ 'text-rendering'
22
+ ];
23
+ /** How many figure spaces are laid out for one measurement: the width of all of them, divided, is the width of one without rounding. */
24
+ const SAMPLE_LENGTH = 20;
25
+ /** The width of one figure space in the field's font, or 0 when it could not be measured (the field is not in the page). */
26
+ export function measureFigureSpaceWidth(field) {
27
+ const doc = field.ownerDocument;
28
+ const win = doc.defaultView;
29
+ const parent = field.parentElement;
30
+ if (!win || !parent)
31
+ return 0;
32
+ const computed = win.getComputedStyle(field);
33
+ const probe = doc.createElement('span');
34
+ probe.setAttribute('aria-hidden', 'true');
35
+ probe.style.cssText =
36
+ 'position:absolute;visibility:hidden;pointer-events:none;white-space:pre;left:0;top:0;';
37
+ for (const property of GLYPH_WIDTH_PROPERTIES)
38
+ probe.style.setProperty(property, computed.getPropertyValue(property));
39
+ probe.textContent = FIGURE_SPACE.repeat(SAMPLE_LENGTH);
40
+ parent.appendChild(probe);
41
+ try {
42
+ return probe.getBoundingClientRect().width / SAMPLE_LENGTH;
43
+ }
44
+ finally {
45
+ probe.remove();
46
+ }
47
+ }
48
+ /** How many figure spaces each room of a token needs in this field right now (the defaults when nothing could be measured). */
49
+ export function measureTokenRoom(field) {
50
+ return roomForFigureSpaceWidth(measureFigureSpaceWidth(field));
51
+ }
52
+ /** The height of one line of the field, in pixels: its `line-height` when it has a length, otherwise a line of its font (`normal`). */
53
+ export function measureLineHeight(field) {
54
+ const win = field.ownerDocument.defaultView;
55
+ if (!win)
56
+ return 20;
57
+ const computed = win.getComputedStyle(field);
58
+ const lineHeight = Number.parseFloat(computed.lineHeight);
59
+ if (Number.isFinite(lineHeight) && lineHeight > 0)
60
+ return lineHeight;
61
+ const fontSize = Number.parseFloat(computed.fontSize);
62
+ return Number.isFinite(fontSize) && fontSize > 0 ? Math.round(fontSize * 1.4) : 20;
63
+ }
@@ -0,0 +1,29 @@
1
+ import type { Box, MenuDirection } from './referenceMenuPlacement.ts';
2
+ import type { ReferenceItem } from './referenceTypes.ts';
3
+ /** What the menu shows at one moment. */
4
+ export interface ReferenceMenuView {
5
+ items: readonly ReferenceItem[];
6
+ activeIndex: number;
7
+ anchor: Box | null;
8
+ previewItem: ReferenceItem | null;
9
+ previewAnchor: Box | null;
10
+ menuLabel: string;
11
+ direction: MenuDirection;
12
+ }
13
+ /** What the menu tells the action. */
14
+ export interface ReferenceMenuCallbacks {
15
+ onChoose: (item: ReferenceItem) => void;
16
+ onActiveChange: (index: number) => void;
17
+ }
18
+ export interface ReferenceMenuHost {
19
+ /** The id of the list: the box points at it with `aria-controls`, and at a row with `aria-activedescendant`. */
20
+ readonly menuId: string;
21
+ /** The id of the row at `index`. */
22
+ optionId(index: number): string;
23
+ /** Replaces some or all of what is shown. */
24
+ update(view: Partial<ReferenceMenuView>): void;
25
+ /** Takes the menu out of the page. Safe to call twice. */
26
+ destroy(): void;
27
+ }
28
+ /** Mounts the menu into `parent` (empty until given a list or a preview). */
29
+ export declare function createReferenceMenuHost(parent: HTMLElement, callbacks: ReferenceMenuCallbacks): ReferenceMenuHost;
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Puts the `#` menu in the page for the action. The action is plain TypeScript (so a host written in any style of Svelte can use it
3
+ * as `use:promptReferences`); this module is the one place that mounts a component, with props that are reactive state, so the action
4
+ * can hand the menu a new list, a new highlight or a new place and only what changed is drawn again.
5
+ */
6
+ import { mount, unmount } from 'svelte';
7
+ import ReferenceMenu from './ReferenceMenu.svelte';
8
+ /** Counts the menus made, so two boxes on a page never share an id. */
9
+ let menusMade = 0;
10
+ /** Mounts the menu into `parent` (empty until given a list or a preview). */
11
+ export function createReferenceMenuHost(parent, callbacks) {
12
+ menusMade += 1;
13
+ const menuId = `prompt-references-menu-${menusMade}`;
14
+ const props = $state({
15
+ items: [],
16
+ activeIndex: -1,
17
+ anchor: null,
18
+ previewItem: null,
19
+ previewAnchor: null,
20
+ menuId,
21
+ menuLabel: '',
22
+ direction: 'ltr',
23
+ onChoose: callbacks.onChoose,
24
+ onActiveChange: callbacks.onActiveChange
25
+ });
26
+ const instance = mount(ReferenceMenu, { target: parent, props });
27
+ let destroyed = false;
28
+ return {
29
+ menuId,
30
+ optionId: (index) => `${menuId}-option-${index}`,
31
+ update(view) {
32
+ if (destroyed)
33
+ return;
34
+ Object.assign(props, view);
35
+ },
36
+ destroy() {
37
+ if (destroyed)
38
+ return;
39
+ destroyed = true;
40
+ void unmount(instance);
41
+ }
42
+ };
43
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * What the keys do while the `#` menu is open. Pure functions, no DOM.
3
+ *
4
+ * The menu is a combobox popup: the focus stays in the text box, and these keys drive the list. Every other key, and every key
5
+ * with a modifier that means something else (a shortcut, Shift+Tab for the focus order, Shift+Enter for a new line), goes on to the
6
+ * box as if no menu were there.
7
+ */
8
+ /** The parts of a keyboard event these rules read. */
9
+ export interface MenuKeyPress {
10
+ key: string;
11
+ shiftKey?: boolean;
12
+ ctrlKey?: boolean;
13
+ metaKey?: boolean;
14
+ altKey?: boolean;
15
+ /** True while an input method is composing (the key belongs to it). */
16
+ isComposing?: boolean;
17
+ /** Old browsers say 229 for a key an input method took. */
18
+ keyCode?: number;
19
+ }
20
+ export type MenuKeyAction = 'next' | 'previous' | 'choose' | 'close' | 'none';
21
+ /** What a key does to the menu: move the highlight, choose the highlighted file, close the menu, or nothing (the key goes on). */
22
+ export declare function interpretMenuKey(press: MenuKeyPress, menuOpen: boolean): MenuKeyAction;
23
+ /** The highlighted row after a move: down from the last goes to the first and up from the first to the last. -1 when the list is empty. */
24
+ export declare function moveActiveIndex(current: number, count: number, action: 'next' | 'previous'): number;
25
+ /** The highlighted row kept, by file, when the list narrows: the same file stays highlighted if it is still listed, else the first. */
26
+ export declare function keepActiveLabel(previousLabel: string | null, labels: readonly string[]): number;
@@ -0,0 +1,49 @@
1
+ /**
2
+ * What the keys do while the `#` menu is open. Pure functions, no DOM.
3
+ *
4
+ * The menu is a combobox popup: the focus stays in the text box, and these keys drive the list. Every other key, and every key
5
+ * with a modifier that means something else (a shortcut, Shift+Tab for the focus order, Shift+Enter for a new line), goes on to the
6
+ * box as if no menu were there.
7
+ */
8
+ /** What a key does to the menu: move the highlight, choose the highlighted file, close the menu, or nothing (the key goes on). */
9
+ export function interpretMenuKey(press, menuOpen) {
10
+ if (!menuOpen)
11
+ return 'none';
12
+ if (press.isComposing === true || press.keyCode === 229)
13
+ return 'none';
14
+ if (press.ctrlKey === true || press.metaKey === true || press.altKey === true)
15
+ return 'none';
16
+ switch (press.key) {
17
+ case 'ArrowDown':
18
+ return press.shiftKey === true ? 'none' : 'next';
19
+ case 'ArrowUp':
20
+ return press.shiftKey === true ? 'none' : 'previous';
21
+ case 'Enter':
22
+ case 'Tab':
23
+ return press.shiftKey === true ? 'none' : 'choose';
24
+ case 'Escape':
25
+ return 'close';
26
+ default:
27
+ return 'none';
28
+ }
29
+ }
30
+ /** The highlighted row after a move: down from the last goes to the first and up from the first to the last. -1 when the list is empty. */
31
+ export function moveActiveIndex(current, count, action) {
32
+ if (!Number.isInteger(count) || count <= 0)
33
+ return -1;
34
+ const from = Number.isInteger(current) && current >= 0 && current < count
35
+ ? current
36
+ : action === 'next'
37
+ ? -1
38
+ : 0;
39
+ return action === 'next' ? (from + 1) % count : (from - 1 + count) % count;
40
+ }
41
+ /** The highlighted row kept, by file, when the list narrows: the same file stays highlighted if it is still listed, else the first. */
42
+ export function keepActiveLabel(previousLabel, labels) {
43
+ if (labels.length === 0)
44
+ return -1;
45
+ if (previousLabel === null)
46
+ return 0;
47
+ const index = labels.findIndex((label) => label === previousLabel);
48
+ return index >= 0 ? index : 0;
49
+ }
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Where the `#` menu and the preview of a file go. Pure functions, no DOM: rectangles in, rectangles out, all in viewport pixels.
3
+ *
4
+ * The composer sits at the bottom edge of a page, so the menu opens ABOVE the `#` when there is no room below, and it never leaves the
5
+ * viewport. The side it hangs from follows the field's direction: a right-to-left field has its menu start at the `#`'s right edge.
6
+ */
7
+ /** A rectangle in viewport pixels. */
8
+ export interface Box {
9
+ left: number;
10
+ top: number;
11
+ right: number;
12
+ bottom: number;
13
+ }
14
+ export interface Size {
15
+ width: number;
16
+ height: number;
17
+ }
18
+ export type MenuDirection = 'ltr' | 'rtl';
19
+ /** The space kept clear at the edges of the viewport, and between the `#` and the menu. */
20
+ export declare const MENU_VIEWPORT_MARGIN = 8;
21
+ export declare const MENU_ANCHOR_GAP = 6;
22
+ /** The least a menu is worth showing at: shorter than two rows and it flips to the side with more room. */
23
+ export declare const MENU_MIN_COMFORTABLE_HEIGHT = 96;
24
+ export interface MenuPlacement {
25
+ /** Viewport coordinates of the menu's top-left corner. */
26
+ left: number;
27
+ top: number;
28
+ /** Which side of the `#` the menu is on. */
29
+ placement: 'above' | 'below';
30
+ /** The tallest the menu may be there: the list scrolls beyond it. */
31
+ maxHeight: number;
32
+ }
33
+ export interface PlaceMenuInput {
34
+ /** The `#` (or the badge) the menu hangs from. */
35
+ anchor: Box;
36
+ /** The menu's size at its natural height (capped by the caller's own maximum). */
37
+ menu: Size;
38
+ viewport: Size;
39
+ direction?: MenuDirection;
40
+ /** The most the list may grow to. Default: the natural height. */
41
+ preferredMaxHeight?: number;
42
+ }
43
+ /**
44
+ * The menu's place. Below the anchor when it fits there, otherwise on the side with more room (above, when the composer is at the bottom
45
+ * edge); the height it may have is the room on that side. Horizontally it starts at the `#` (at the right edge of the `#` in a
46
+ * right-to-left field), moved in if it would leave the viewport.
47
+ */
48
+ export declare function placeMenu(input: PlaceMenuInput): MenuPlacement;
49
+ export interface PlacePreviewInput {
50
+ /** The menu (or the badge) the preview goes beside. */
51
+ beside: Box;
52
+ preview: Size;
53
+ viewport: Size;
54
+ direction?: MenuDirection;
55
+ }
56
+ /**
57
+ * The preview card's place: beside the menu on its end side (right in a left-to-right page) when it fits, otherwise on the other side,
58
+ * otherwise over the menu's top edge. Aligned with the menu's top, and kept inside the viewport.
59
+ */
60
+ export declare function placePreview(input: PlacePreviewInput): {
61
+ left: number;
62
+ top: number;
63
+ };
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Where the `#` menu and the preview of a file go. Pure functions, no DOM: rectangles in, rectangles out, all in viewport pixels.
3
+ *
4
+ * The composer sits at the bottom edge of a page, so the menu opens ABOVE the `#` when there is no room below, and it never leaves the
5
+ * viewport. The side it hangs from follows the field's direction: a right-to-left field has its menu start at the `#`'s right edge.
6
+ */
7
+ /** The space kept clear at the edges of the viewport, and between the `#` and the menu. */
8
+ export const MENU_VIEWPORT_MARGIN = 8;
9
+ export const MENU_ANCHOR_GAP = 6;
10
+ /** The least a menu is worth showing at: shorter than two rows and it flips to the side with more room. */
11
+ export const MENU_MIN_COMFORTABLE_HEIGHT = 96;
12
+ function clamp(value, low, high) {
13
+ return Math.min(Math.max(value, low), Math.max(low, high));
14
+ }
15
+ /**
16
+ * The menu's place. Below the anchor when it fits there, otherwise on the side with more room (above, when the composer is at the bottom
17
+ * edge); the height it may have is the room on that side. Horizontally it starts at the `#` (at the right edge of the `#` in a
18
+ * right-to-left field), moved in if it would leave the viewport.
19
+ */
20
+ export function placeMenu(input) {
21
+ const { anchor, menu, viewport } = input;
22
+ const margin = MENU_VIEWPORT_MARGIN;
23
+ const wanted = Math.min(menu.height, input.preferredMaxHeight ?? menu.height);
24
+ const roomBelow = viewport.height - anchor.bottom - MENU_ANCHOR_GAP - margin;
25
+ const roomAbove = anchor.top - MENU_ANCHOR_GAP - margin;
26
+ const fitsBelow = wanted <= roomBelow;
27
+ const fitsAbove = wanted <= roomAbove;
28
+ let placement;
29
+ if (fitsBelow)
30
+ placement = 'below';
31
+ else if (fitsAbove)
32
+ placement = 'above';
33
+ else
34
+ placement = roomAbove > roomBelow ? 'above' : 'below';
35
+ const room = placement === 'below' ? roomBelow : roomAbove;
36
+ const maxHeight = Math.max(MENU_MIN_COMFORTABLE_HEIGHT, Math.min(wanted, room));
37
+ const height = Math.min(wanted, maxHeight);
38
+ const top = placement === 'below' ? anchor.bottom + MENU_ANCHOR_GAP : anchor.top - MENU_ANCHOR_GAP - height;
39
+ const preferredLeft = input.direction === 'rtl' ? anchor.right - menu.width : anchor.left;
40
+ const left = clamp(preferredLeft, margin, viewport.width - menu.width - margin);
41
+ return { left, top: Math.max(margin, top), placement, maxHeight };
42
+ }
43
+ /**
44
+ * The preview card's place: beside the menu on its end side (right in a left-to-right page) when it fits, otherwise on the other side,
45
+ * otherwise over the menu's top edge. Aligned with the menu's top, and kept inside the viewport.
46
+ */
47
+ export function placePreview(input) {
48
+ const { beside, preview, viewport } = input;
49
+ const margin = MENU_VIEWPORT_MARGIN;
50
+ const gap = MENU_ANCHOR_GAP;
51
+ const endSideLeft = input.direction === 'rtl' ? beside.left - gap - preview.width : beside.right + gap;
52
+ const startSideLeft = input.direction === 'rtl' ? beside.right + gap : beside.left - gap - preview.width;
53
+ const fits = (left) => left >= margin && left + preview.width <= viewport.width - margin;
54
+ let left;
55
+ let top = beside.top;
56
+ if (fits(endSideLeft))
57
+ left = endSideLeft;
58
+ else if (fits(startSideLeft))
59
+ left = startSideLeft;
60
+ else {
61
+ // No room at either side: the card sits above the box instead of over it.
62
+ left = clamp(beside.left, margin, viewport.width - preview.width - margin);
63
+ top = beside.top - gap - preview.height;
64
+ }
65
+ return { left, top: clamp(top, margin, viewport.height - preview.height - margin) };
66
+ }