@lynn123411/dsh-model-submenu 0.3.17 → 0.3.19

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.
@@ -1,51 +1,24 @@
1
- /**
2
- * The accent one reasoning level carries.
3
- *
4
- * "Off" carries no accent at all — the fill and the caption stay neutral, so
5
- * switching reasoning off reads as switching it off rather than as the first
6
- * shade of a ramp. Every level above it walks a blue-to-violet ramp, and the
7
- * highest level is the only one whose fill is a gradient.
8
- */
9
1
 
10
- /** Off: the dimmed label tone, the same neutral the Host uses for quiet text. */
11
2
  const NEUTRAL = 'var(--dsw-alias-label-dimmed)';
12
3
 
13
- /** Lowest lit level: the blue the composer already uses for accents. */
14
4
  const LOW = [77, 147, 248] as const;
15
5
 
16
- /** Highest level: violet. */
17
6
  const HIGH = [147, 51, 234] as const;
18
7
 
19
- /**
20
- * @param t - a point on the ramp, 0 at the lowest lit level and 1 at the top.
21
- * @returns the ramp color there.
22
- */
23
8
  function ramp(t: number): string {
24
9
  const mix = (from: number, to: number): number => Math.round(from + (to - from) * t);
25
10
  return `rgb(${mix(LOW[0], HIGH[0])}, ${mix(LOW[1], HIGH[1])}, ${mix(LOW[2], HIGH[2])})`;
26
11
  }
27
12
 
28
- /**
29
- * @param index - the level's detent, 0 being "off".
30
- * @param count - number of detents.
31
- * @returns the level's color as a CSS color string.
32
- */
33
13
  export function levelColor(index: number, count: number): string {
34
14
  if (index <= 0) return NEUTRAL;
35
- // The top level is always the violet end, even when it is the only lit one.
36
15
  if (isTopLevel(index, count)) return ramp(1);
37
16
  const span = Math.max(1, count - 2);
38
17
  return ramp(Math.min(Math.max((index - 1) / span, 0), 1));
39
18
  }
40
19
 
41
- /**
42
- * @param index - the level's detent.
43
- * @param count - number of detents.
44
- * @returns whether this level is the only one painted with a gradient.
45
- */
46
20
  export function isTopLevel(index: number, count: number): boolean {
47
21
  return count > 1 && index === count - 1;
48
22
  }
49
23
 
50
- /** The top level's fill: the whole ramp, ending on the color its text takes. */
51
24
  export const LEVEL_GRADIENT = `linear-gradient(90deg, ${ramp(0)}, ${ramp(1)})`;
@@ -1,9 +1,6 @@
1
- /** Locale bundles for the all-provider model picker. */
2
1
 
3
- /** Dictionary namespace owned by this plugin. */
4
2
  export const NS = 'modelSubmenu'
5
3
 
6
- /** Locale keys the picker renders. */
7
4
  export type ModelSubmenuLocaleKey =
8
5
  | 'status.loading'
9
6
  | 'status.empty'
@@ -14,6 +11,8 @@ export type ModelSubmenuLocaleKey =
14
11
  | 'error.sessionInUse'
15
12
  | 'picker.title'
16
13
  | 'picker.browse'
14
+ | 'picker.browseAll'
15
+ | 'picker.quick'
17
16
  | 'picker.close'
18
17
  | 'picker.search'
19
18
  | 'picker.searchEmpty'
@@ -21,6 +20,7 @@ export type ModelSubmenuLocaleKey =
21
20
  | 'picker.favorite'
22
21
  | 'picker.unfavorite'
23
22
  | 'picker.hintClose'
23
+ | 'picker.hintFavorites'
24
24
  | 'meta.effort'
25
25
  | 'meta.images'
26
26
  | 'effort.label'
@@ -29,12 +29,10 @@ export type ModelSubmenuLocaleKey =
29
29
 
30
30
  declare module '@deepseek-ai/dsh-client-ui-slots' {
31
31
  interface LocaleNamespaceMap {
32
- /** The all-provider model picker's copy. */
33
32
  modelSubmenu: ModelSubmenuLocaleKey
34
33
  }
35
34
  }
36
35
 
37
- /** Simplified Chinese dictionary (the key-set source of truth). */
38
36
  export const zh: Record<ModelSubmenuLocaleKey, string> = {
39
37
  'status.loading': '正在加载模型…',
40
38
  'status.empty': '没有可用的模型。',
@@ -45,6 +43,8 @@ export const zh: Record<ModelSubmenuLocaleKey, string> = {
45
43
  'error.sessionInUse': '当前会话已被占用,可能是其他正在运行的 DSH 导致的(如其他 dsh web、桌面端),请退出其他正在运行的 DSH 后重试。',
46
44
  'picker.title': '切换模型',
47
45
  'picker.browse': '浏览模型',
46
+ 'picker.browseAll': '浏览全部模型',
47
+ 'picker.quick': '快捷切换模型',
48
48
  'picker.close': '关闭',
49
49
  'picker.search': '搜索模型或提供商…',
50
50
  'picker.searchEmpty': '没有匹配的模型。',
@@ -52,6 +52,7 @@ export const zh: Record<ModelSubmenuLocaleKey, string> = {
52
52
  'picker.favorite': '收藏 {model}',
53
53
  'picker.unfavorite': '取消收藏 {model}',
54
54
  'picker.hintClose': '关闭',
55
+ 'picker.hintFavorites': '收藏的模型置顶,并加入快捷菜单',
55
56
  'meta.effort': '思考',
56
57
  'meta.images': '图片输入',
57
58
  'effort.label': '推理等级',
@@ -59,7 +60,6 @@ export const zh: Record<ModelSubmenuLocaleKey, string> = {
59
60
  'effort.useDefault': '切换为默认档位',
60
61
  }
61
62
 
62
- /** English dictionary, checked complete against the zh key set. */
63
63
  export const en: Record<ModelSubmenuLocaleKey, string> = {
64
64
  'status.loading': 'Loading models…',
65
65
  'status.empty': 'No models available.',
@@ -70,6 +70,8 @@ export const en: Record<ModelSubmenuLocaleKey, string> = {
70
70
  'error.sessionInUse': 'This session is already in use, possibly by another running DSH instance (such as dsh web or the desktop app). Quit other running DSH instances and try again.',
71
71
  'picker.title': 'Switch model',
72
72
  'picker.browse': 'Browse models',
73
+ 'picker.browseAll': 'Browse all models',
74
+ 'picker.quick': 'Quick model selection',
73
75
  'picker.close': 'Close',
74
76
  'picker.search': 'Search models or providers…',
75
77
  'picker.searchEmpty': 'No matching models.',
@@ -77,6 +79,7 @@ export const en: Record<ModelSubmenuLocaleKey, string> = {
77
79
  'picker.favorite': 'Favorite {model}',
78
80
  'picker.unfavorite': 'Remove {model} from favorites',
79
81
  'picker.hintClose': 'close',
82
+ 'picker.hintFavorites': 'Favorites first, also in the quick menu',
80
83
  'meta.effort': 'Effort',
81
84
  'meta.images': 'Images',
82
85
  'effort.label': 'Reasoning effort',
@@ -1,31 +1,6 @@
1
- /**
2
- * What the settings document knows about one model, for the picker's metadata
3
- * line.
4
- *
5
- * The Host's model catalog carries only a name and a reasoning list; context
6
- * window and accepted modalities live in each provider's settings profile,
7
- * which the settings mirror already holds in the browser. This module joins
8
- * the two by the identity both agree on: the catalog's provider route key and
9
- * model id are exactly the settings profile's provider key and model id.
10
- *
11
- * Every field is answered independently. A provider whose profile lists no
12
- * models (an OAuth route such as the Kimi subscription, or the account
13
- * provider) answers `unknown` rather than guessing the route default, because
14
- * the line's rule is to omit what cannot be asserted.
15
- */
16
-
17
- /** Accepted-modality answer for one model. */
1
+
18
2
  export type ImageAnswer = 'yes' | 'no' | 'unknown'
19
3
 
20
- /**
21
- * The model knowledge a shipped adapter carries for its own account route.
22
- *
23
- * The account route has no `models` list on purpose: the account listing
24
- * supplies the catalog and settings only carry per-model overrides. The same
25
- * adapter family ships a default catalog for its API-key route, and the account
26
- * route serves the same models, so those entries answer what the account
27
- * profile cannot. Ids and facts mirror the adapter's own table.
28
- */
29
4
  const FAMILY_DEFAULTS: Readonly<Record<string, readonly {
30
5
  readonly id: string
31
6
  readonly contextWindow: number
@@ -37,28 +12,22 @@ const FAMILY_DEFAULTS: Readonly<Record<string, readonly {
37
12
  ],
38
13
  }
39
14
 
40
- /** The metadata the settings document can answer for one model. */
41
15
  export interface ModelFacts {
42
- /** Exact advertised capacity, or undefined when the model declares none. */
43
16
  readonly contextWindow: number | undefined
44
- /** Whether the model accepts images; `unknown` means the document is silent. */
45
17
  readonly images: ImageAnswer
46
18
  }
47
19
 
48
- /** One provider directory row, as the Host's LLM directory reports it. */
49
20
  export interface ConfigurableProviderRow {
50
21
  readonly provider: string
51
22
  readonly settingsNs: string
52
23
  readonly settingsPath: readonly string[]
53
24
  }
54
25
 
55
- /** One settings namespace view, as the describe answer carries it. */
56
26
  export interface NamespaceRow {
57
27
  readonly ns: string
58
28
  readonly value: unknown
59
29
  }
60
30
 
61
- /** What one provider's profile may carry, across both adapter families. */
62
31
  interface ProfileView {
63
32
  readonly models?: readonly Record<string, unknown>[]
64
33
  readonly defaultContextWindow?: unknown
@@ -67,12 +36,6 @@ interface ProfileView {
67
36
 
68
37
  type Record_ = Record<string, unknown>
69
38
 
70
- /**
71
- * Build the fact lookup from the settings answer.
72
- * @param providers - provider directory rows giving each route's settings address.
73
- * @param namespaces - the describe answer's namespace views.
74
- * @returns a lookup answering both fields for one provider and model.
75
- */
76
39
  export function buildModelFacts(
77
40
  providers: readonly ConfigurableProviderRow[],
78
41
  namespaces: readonly NamespaceRow[],
@@ -95,8 +58,6 @@ export function buildModelFacts(
95
58
  : undefined
96
59
  return { contextWindow: window, images: imageAnswer(entry, profile) }
97
60
  }
98
- // A route with no model list of its own (the account route) still serves its
99
- // adapter family's models; a declared override there wins over the default.
100
61
  const override = readOverride(profile, model)
101
62
  if (override !== undefined) {
102
63
  const window = typeof override.contextWindow === 'number' && override.contextWindow > 0
@@ -114,14 +75,6 @@ export function buildModelFacts(
114
75
  }
115
76
  }
116
77
 
117
- /**
118
- * Read one model entry's accepted image modalities.
119
- *
120
- * The two adapter families disagree about an omission. The Messages family
121
- * names the field `inputModalities`, and leaving it out means text only. The
122
- * pi-ai family names it `input`, and an empty or absent list defers to the
123
- * route's `defaultInput`; with no route default the answer stays unknown.
124
- */
125
78
  function imageAnswer(entry: Record_, profile: ProfileView): ImageAnswer {
126
79
  if (Array.isArray(entry.inputModalities)) return containsImage(entry.inputModalities)
127
80
  const defaultInput = Array.isArray(profile.defaultInput) ? profile.defaultInput : undefined
@@ -132,7 +85,6 @@ function imageAnswer(entry: Record_, profile: ProfileView): ImageAnswer {
132
85
  return defaultInput === undefined ? 'no' : containsImage(defaultInput)
133
86
  }
134
87
 
135
- /** One model's declared override, when the route keeps a `modelOverrides` table. */
136
88
  function readOverride(profile: ProfileView | undefined, model: string): Record_ | undefined {
137
89
  const overrides = (profile as Record_ | undefined)?.modelOverrides
138
90
  if (!isRecord(overrides)) return undefined
@@ -144,26 +96,12 @@ function containsImage(modalities: readonly unknown[]): ImageAnswer {
144
96
  return modalities.includes('image') ? 'yes' : 'no'
145
97
  }
146
98
 
147
- /**
148
- * Abbreviate one capacity the way the metadata line shows it.
149
- * @param tokens - exact advertised capacity in tokens.
150
- * @returns e.g. `1M ctx`, `1.5M ctx`, `272k ctx`, or `512 ctx` below a thousand.
151
- */
152
99
  export function formatContextWindow(tokens: number): string {
153
100
  if (tokens >= 1_000_000) return `${trimNumber(tokens / 1_000_000)}M ctx`
154
101
  if (tokens >= 1_000) return `${Math.round(tokens / 1_000)}k ctx`
155
102
  return `${tokens} ctx`
156
103
  }
157
104
 
158
- /**
159
- * Build the metadata line's segments.
160
- * @param input.providerName - the provider's display name, omitted inside that provider's own tab.
161
- * @param input.facts - what the settings document answered.
162
- * @param input.thinks - whether the catalog advertises reasoning levels for this model.
163
- * @param input.effortLabel - the localized word for the reasoning segment.
164
- * @param input.imageLabel - the localized word for the image segment.
165
- * @returns the segments to join; an empty list means show no line.
166
- */
167
105
  export function metaSegments(input: {
168
106
  readonly providerName?: string
169
107
  readonly facts: ModelFacts
@@ -1,36 +1,16 @@
1
- /**
2
- * Which session's picker is open.
3
- *
4
- * The row that asks for the picker lives inside the composer model menu, and the
5
- * Host unmounts that menu the moment a row is clicked. The dialog lives in the
6
- * composer's overlay slot, which stays mounted. One plugin-wide store carries the
7
- * request across that unmount — the same split the Host's own command popups use
8
- * between their controller and their overlay view.
9
- */
10
1
 
11
- /** What the store publishes. */
12
2
  export interface PickerSnapshot {
13
- /** Session whose picker is open, or null when none is. */
14
3
  readonly sessionId: string | null
15
- /** Toolbar button to refocus after the dialog closes, when still present. */
16
4
  readonly returnFocus?: HTMLButtonElement
17
5
  }
18
6
 
19
- /** Open and close verbs plus the observable the overlay view subscribes to. */
20
7
  export interface PickerStore {
21
- /** @returns the current request. */
22
8
  readonly getSnapshot: () => PickerSnapshot
23
- /** @param listener - called after every change. @returns the unsubscribe. */
24
9
  readonly subscribe: (listener: () => void) => () => void
25
- /** Open the picker for one session, optionally returning focus to its toolbar button. */
26
10
  readonly open: (sessionId: string, returnFocus?: HTMLButtonElement) => void
27
- /** Close whatever is open. */
28
11
  readonly close: () => void
29
12
  }
30
13
 
31
- /**
32
- * @returns a store that starts closed.
33
- */
34
14
  export function createPickerStore(): PickerStore {
35
15
  let snapshot: PickerSnapshot = { sessionId: null };
36
16
  const listeners = new Set<() => void>();
@@ -1,49 +1,29 @@
1
- /**
2
- * Catalog flattening, tab derivation, and query matching for the picker.
3
- *
4
- * The model catalog groups models by provider; the picker shows one flat list
5
- * with a tab per provider. The shared text search index matches model and
6
- * provider names and IDs.
7
- */
8
1
 
9
2
  import type { ModelDirectoryState } from '@deepseek-ai/dsh-client-ui-model-selection/client'
10
3
  import { TextSearchIndex } from '@deepseek-ai/dsh-util-text-search'
11
4
 
12
- /** One provider group of the shared catalog. */
13
5
  export type ProviderGroup = ModelDirectoryState['groups'][number]
14
6
 
15
- /** One model inside a provider group. */
16
7
  export type CatalogModel = ProviderGroup['models'][number]
17
8
 
18
- /** One flat catalog row, carrying the provider identity beside the model. */
19
9
  export interface ModelRow extends ModelRef {
20
10
  readonly providerName: string
21
- /** The model id inside its provider, the other half of a selection. */
22
11
  readonly model: string
23
- /** The catalog entry itself, for name and reasoning metadata. */
24
12
  readonly entry: CatalogModel
25
13
  }
26
14
 
27
- /** A provider and one of its models: the pair every selection and favorite names. */
28
15
  export interface ModelRef {
29
16
  readonly provider: string
30
17
  readonly model: string
31
18
  }
32
19
 
33
- /** One picker tab. */
34
20
  export interface PickerTab {
35
21
  readonly id: string
36
22
  readonly label: string
37
23
  }
38
24
 
39
- /** The tab id standing for every provider. */
40
25
  export const ALL_TAB = '*'
41
26
 
42
- /**
43
- * Flatten the catalog to one row per model.
44
- * @param groups - the shared directory's provider groups, in display order.
45
- * @returns the rows in catalog order.
46
- */
47
27
  export function flattenGroups(groups: readonly ProviderGroup[]): readonly ModelRow[] {
48
28
  return groups.flatMap((group) => group.models.map((model) => ({
49
29
  provider: group.id,
@@ -53,12 +33,6 @@ export function flattenGroups(groups: readonly ProviderGroup[]): readonly ModelR
53
33
  })))
54
34
  }
55
35
 
56
- /**
57
- * Build the picker's tabs: the all-tab, then one per provider that has a model.
58
- * @param groups - the shared directory's provider groups.
59
- * @param allLabel - localized label for the all-tab.
60
- * @returns the tabs in catalog order.
61
- */
62
36
  export function pickerTabs(groups: readonly ProviderGroup[], allLabel = 'All'): readonly PickerTab[] {
63
37
  return [
64
38
  { id: ALL_TAB, label: allLabel },
@@ -66,27 +40,16 @@ export function pickerTabs(groups: readonly ProviderGroup[], allLabel = 'All'):
66
40
  ]
67
41
  }
68
42
 
69
- /**
70
- * @param rows - the flat catalog.
71
- * @param tab - the tab id; the all-tab keeps every row.
72
- * @returns the rows the tab shows, in their current order.
73
- */
74
43
  export function rowsForTab(rows: readonly ModelRow[], tab: string): readonly ModelRow[] {
75
44
  return tab === ALL_TAB ? rows : rows.filter((row) => row.provider === tab)
76
45
  }
77
46
 
78
- /** Build a reusable search index for the rows in the current provider tab. */
79
47
  export function createModelSearch(rows: readonly ModelRow[]): TextSearchIndex<ModelRow> {
80
48
  return new TextSearchIndex(rows, (row) => ({
81
49
  fuzzy: [row.entry.name, row.model, row.providerName, row.provider],
82
50
  }))
83
51
  }
84
52
 
85
- /**
86
- * Read the reasoning metadata the catalog advertises for one model.
87
- * @param groups - the shared directory's provider groups.
88
- * @returns the metadata, or undefined when that model advertises none.
89
- */
90
53
  export function reasoningOf(
91
54
  groups: readonly ProviderGroup[],
92
55
  ): (provider: string, model: string) => CatalogModel['reasoning'] {
@@ -1,39 +1,19 @@
1
- /**
2
- * When the composer menu writes a selection.
3
- *
4
- * Both gestures are immediate: clicking a level submits it, clicking a model
5
- * submits it. The only quiet path is clicking the model already in use — that
6
- * closes the menu without a write. A model click also restores the level this
7
- * browser last used for that model, so switching away and back never silently
8
- * resets the effort.
9
- */
10
1
 
11
2
  import type { ModelReasoning } from '@deepseek-ai/dsh-api-session-controller/types'
12
3
  import type { ModelDirectoryState } from '@deepseek-ai/dsh-client-ui-model-selection/client'
13
4
 
14
- /** One complete selection, as the directory's `select` takes it. */
15
5
  export interface Selection {
16
6
  readonly provider: string
17
7
  readonly model: string
18
8
  readonly reasoningEffort?: string
19
9
  }
20
10
 
21
- /** One segment of the reasoning row. */
22
11
  export interface EffortChoice {
23
12
  readonly key: string
24
13
  readonly effort: string | undefined
25
14
  readonly label: string
26
15
  }
27
16
 
28
- /**
29
- * Build the slider's detents for one model.
30
- *
31
- * The provider default is deliberately absent: it is a state, not a level, and
32
- * giving it a detent would read as the weakest reasoning instead of "let the
33
- * provider decide". The label above the track switches to it.
34
- * @param reasoning - the catalog's reasoning metadata, undefined when the model has none.
35
- * @returns one detent per advertised level, in catalog order.
36
- */
37
17
  export function effortChoices(reasoning: ModelReasoning | undefined): readonly EffortChoice[] {
38
18
  if (reasoning === undefined) return []
39
19
  return reasoning.efforts.map((level) => ({
@@ -43,12 +23,6 @@ export function effortChoices(reasoning: ModelReasoning | undefined): readonly E
43
23
  }))
44
24
  }
45
25
 
46
- /**
47
- * The selection a level click submits.
48
- * @param live - the selection in force right now.
49
- * @param effort - the clicked level, undefined meaning the provider default.
50
- * @returns the selection to submit, or null when the level already is the live one.
51
- */
52
26
  export function planEffortCommit(live: Selection, effort: string | undefined): Selection | null {
53
27
  if (sameEffort(live.reasoningEffort, effort)) return null
54
28
  return {
@@ -58,15 +32,6 @@ export function planEffortCommit(live: Selection, effort: string | undefined): S
58
32
  }
59
33
  }
60
34
 
61
- /**
62
- * The selection a model click submits.
63
- * @param live - the selection in force right now, null before a first selection resolves.
64
- * @param provider - the clicked model's provider.
65
- * @param model - the clicked model id.
66
- * @param reasoningOf - the catalog's reasoning metadata for one provider and model.
67
- * @param remembered - the level this browser last used for the target model, if any.
68
- * @returns the selection to submit, or null when the click changes nothing.
69
- */
70
35
  export function planModelCommit(
71
36
  live: Selection | null,
72
37
  provider: string,
@@ -76,8 +41,6 @@ export function planModelCommit(
76
41
  ): Selection | null {
77
42
  if (live !== null && sameModel(live, provider, model)) return null
78
43
  const offered = reasoningOf(provider, model)
79
- // The target model's own memory is restored, so switching away and back never
80
- // silently resets the effort.
81
44
  const carried = offered !== undefined
82
45
  && remembered !== undefined
83
46
  && offered.efforts.some((level) => level.id === remembered)
@@ -88,16 +51,10 @@ export function planModelCommit(
88
51
  }
89
52
  }
90
53
 
91
- /**
92
- * The reasoning metadata the catalog advertises for the model in force.
93
- * @param state - the session's directory snapshot.
94
- * @returns the metadata, or undefined when no model is live or it advertises none.
95
- */
96
54
  export function liveReasoning(state: ModelDirectoryState): ModelReasoning | undefined {
97
55
  return currentCatalogModel(state)?.reasoning
98
56
  }
99
57
 
100
- /** Find the current model by its provider and model IDs. */
101
58
  export function currentCatalogModel(state: ModelDirectoryState) {
102
59
  const current = state.current
103
60
  if (current === null) return undefined
@@ -106,11 +63,6 @@ export function currentCatalogModel(state: ModelDirectoryState) {
106
63
  ?.models.find((model) => model.id === current.model)
107
64
  }
108
65
 
109
- /**
110
- * The selection in force, or null while the Host has not resolved one.
111
- * @param state - the session's directory snapshot.
112
- * @returns the live selection, with the effort only when one is set.
113
- */
114
66
  export function selectionOf(state: ModelDirectoryState): Selection | null {
115
67
  if (state.current === null) return null
116
68
  return state.current.reasoningEffort === undefined
@@ -122,21 +74,10 @@ export function selectionOf(state: ModelDirectoryState): Selection | null {
122
74
  }
123
75
  }
124
76
 
125
- /**
126
- * @param live - one selection.
127
- * @param provider - the provider to compare.
128
- * @param model - the model id to compare.
129
- * @returns whether both name the same model.
130
- */
131
77
  export function sameModel(live: Selection, provider: string, model: string): boolean {
132
78
  return live.provider === provider && live.model === model
133
79
  }
134
80
 
135
- /**
136
- * @param liveEffort - the level in force, undefined meaning the provider default.
137
- * @param effort - the level in question.
138
- * @returns whether they name the same level.
139
- */
140
81
  export function sameEffort(liveEffort: string | undefined, effort: string | undefined): boolean {
141
82
  return liveEffort === effort
142
83
  }
@@ -1,48 +1,16 @@
1
- /**
2
- * Geometry of the discrete effort slider.
3
- *
4
- * The track is a capsule and the knob a slightly larger circle riding on it, so
5
- * the knob overhangs the track instead of merging with its rounded end. The
6
- * knob's centre travels between two points inset by the knob's radius, so the
7
- * knob never runs past either end. A pointer position becomes a position along
8
- * that travel; a position becomes the detent nearest it, or the CSS lengths that
9
- * put the knob and the fill there.
10
- */
11
1
 
12
- /**
13
- * Track height in CSS pixels; the stylesheet keeps the same value. Half of it is
14
- * the radius of the track's ends and of the fill's cap.
15
- */
16
2
  export const TRACK_HEIGHT = 16;
17
3
 
18
- /**
19
- * Knob diameter in CSS pixels. It is larger than the track, so the knob reads as
20
- * a knob riding on the track rather than as the track's own end cap.
21
- */
22
4
  export const KNOB_SIZE = 20;
23
5
 
24
- /** The knob's centre keeps its own radius away from either end of the track. */
25
6
  const KNOB_RADIUS = KNOB_SIZE / 2;
26
7
 
27
- /**
28
- * @param index - detent index.
29
- * @param count - number of detents.
30
- * @returns the knob centre as a fraction of the track, 0 at the first detent and 1 at the last.
31
- */
32
8
  export function offsetOf(index: number, count: number): number {
33
9
  if (count <= 1) return 0;
34
10
  const clamped = Math.min(Math.max(index, 0), count - 1);
35
11
  return clamped / (count - 1);
36
12
  }
37
13
 
38
- /**
39
- * Where a pointer sits along the knob's travel. The drag reads this rather than a
40
- * detent, so the knob follows the pointer instead of jumping a level at a time.
41
- * @param clientX - pointer position in viewport coordinates.
42
- * @param left - the track's left edge in viewport coordinates.
43
- * @param width - the track's width in pixels.
44
- * @returns the position as a fraction of the travel, clamped to 0 and 1.
45
- */
46
14
  export function fractionAt(clientX: number, left: number, width: number): number {
47
15
  const usable = width - KNOB_SIZE;
48
16
  if (usable <= 0) return 0;
@@ -50,48 +18,22 @@ export function fractionAt(clientX: number, left: number, width: number): number
50
18
  return Math.min(Math.max(fraction, 0), 1);
51
19
  }
52
20
 
53
- /**
54
- * @param fraction - a knob position between 0 and 1.
55
- * @param count - number of detents.
56
- * @returns the detent nearest that position.
57
- */
58
21
  export function detentAt(fraction: number, count: number): number {
59
22
  if (count <= 1) return 0;
60
23
  const clamped = Math.min(Math.max(fraction, 0), 1);
61
24
  return Math.round(clamped * (count - 1));
62
25
  }
63
26
 
64
- /**
65
- * @param clientX - pointer position in viewport coordinates.
66
- * @param left - the track's left edge in viewport coordinates.
67
- * @param width - the track's width in pixels.
68
- * @param count - number of detents.
69
- * @returns the detent nearest the pointer, clamped to the two ends.
70
- */
71
27
  export function stepAt(clientX: number, left: number, width: number, count: number): number {
72
28
  if (count <= 1) return 0;
73
29
  return detentAt(fractionAt(clientX, left, width), count);
74
30
  }
75
31
 
76
- /**
77
- * @param fraction - a knob position between 0 and 1.
78
- * @returns the CSS length that puts the knob centre there, inset at both ends.
79
- */
80
32
  export function knobOffset(fraction: number): string {
81
33
  const clamped = Math.min(Math.max(fraction, 0), 1);
82
34
  return `calc(${KNOB_RADIUS}px + ${clamped} * (100% - ${KNOB_SIZE}px))`;
83
35
  }
84
36
 
85
- /**
86
- * Width of the colored fill for a knob position.
87
- *
88
- * The fill is the track's own shape cut at the knob: its right cap carries the
89
- * track's radius and ends under the knob, so the color runs into the knob
90
- * instead of stopping at a straight edge, and the last detent reaches the track's
91
- * end rather than the knob's centre.
92
- * @param fraction - a knob position between 0 and 1.
93
- * @returns the CSS width of the fill.
94
- */
95
37
  export function fillWidth(fraction: number): string {
96
38
  const clamped = Math.min(Math.max(fraction, 0), 1);
97
39
  return `calc(${TRACK_HEIGHT}px + ${clamped} * (100% - ${TRACK_HEIGHT}px))`;
package/src/index.ts CHANGED
@@ -1,2 +1 @@
1
- /** Host entry. This plugin is client-only; the browser half is `./client`. */
2
1
  export function apply(): void {}