@xharness/satori-element 0.0.0-stage → 4.0.1-alpha.2
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/LICENSE +21 -0
- package/lib/index.js +871 -0
- package/lib/types/elements.d.ts +54 -0
- package/lib/types/elements.d.ts.map +1 -0
- package/lib/types/factory.d.ts +84 -0
- package/lib/types/factory.d.ts.map +1 -0
- package/lib/types/index.d.ts +15 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/normalize.d.ts +31 -0
- package/lib/types/normalize.d.ts.map +1 -0
- package/lib/types/text.d.ts +10 -0
- package/lib/types/text.d.ts.map +1 -0
- package/lib/types/variants.d.ts +30 -0
- package/lib/types/variants.d.ts.map +1 -0
- package/lib/types/vocabulary.d.ts +364 -0
- package/lib/types/vocabulary.d.ts.map +1 -0
- package/package.json +53 -3
- package/src/elements.ts +159 -0
- package/src/factory.ts +163 -0
- package/src/index.ts +16 -0
- package/src/normalize.ts +397 -0
- package/src/text.ts +161 -0
- package/src/variants.ts +83 -0
- package/src/vocabulary.ts +300 -0
- package/README.md +0 -3
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Satori element vocabulary: the typed overlay over the `cordis-element` runtime.
|
|
3
|
+
*
|
|
4
|
+
* Runtime objects stay `cordis-element` elements (`type` / `attrs` / `children`, `toString()` for logs and goldens);
|
|
5
|
+
* everything here is structural typing, so no conversion is ever needed.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export type Theme = 'default' | 'info' | 'success' | 'warning' | 'danger'
|
|
9
|
+
export type Color = 'default' | 'muted' | 'info' | 'success' | 'warning' | 'danger'
|
|
10
|
+
export type Align = 'left' | 'center' | 'right'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Payload of a `button[type=action]` interaction.
|
|
14
|
+
*
|
|
15
|
+
* Forward declaration: the vocabulary only knows the platform-neutral fields. `plugin-im-core` augments this
|
|
16
|
+
* interface (module augmentation of `@xharness/satori-element`) with `bot`, `user`, `chat` and `message`, which
|
|
17
|
+
* are protocol types this package cannot depend on.
|
|
18
|
+
*/
|
|
19
|
+
export interface ButtonEvent {
|
|
20
|
+
/** Button id (`button[type=action].id`). */
|
|
21
|
+
id: string
|
|
22
|
+
/** Optional payload (≤ 256 bytes; QQ data, Lark value). */
|
|
23
|
+
data?: string
|
|
24
|
+
/**
|
|
25
|
+
* Values of the `<form>` the button stands in, keyed by control `name` — present when the click submitted a form
|
|
26
|
+
* (`button[type=submit]`, or a plain action button inside a `<form>` on a platform that submits with any button).
|
|
27
|
+
* A checkbox reports a boolean, a multiple `<select>` an array in option order.
|
|
28
|
+
*/
|
|
29
|
+
values?: FormValues
|
|
30
|
+
/** Passive-reply / reply context the adapter may use (QQ event_id, Lark open_message_id). */
|
|
31
|
+
referrer: unknown
|
|
32
|
+
time: number
|
|
33
|
+
/**
|
|
34
|
+
* Answer the interaction on the platform, when the platform lets a click carry an answer (a Lark card callback
|
|
35
|
+
* answers with a toast). Resolves once the answer was sent; only the first call counts, and a later call resolves
|
|
36
|
+
* without effect. Absent on platforms without such an answer, and the platform answers on its own once its deadline
|
|
37
|
+
* passes, so a caller that updates the clicked message after answering awaits this first.
|
|
38
|
+
*/
|
|
39
|
+
respond?(answer: ButtonAnswer): Promise<void>
|
|
40
|
+
/**
|
|
41
|
+
* Claim the answer for a listener that calls {@link respond} only after asynchronous work: the platform then does
|
|
42
|
+
* not answer `{}` as soon as intake settles, and answers on its own only once its deadline passes. Absent where
|
|
43
|
+
* {@link respond} is absent.
|
|
44
|
+
*/
|
|
45
|
+
defer?(): void
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Platform-neutral answer to a button click ({@link ButtonEvent.respond}). */
|
|
49
|
+
export interface ButtonAnswer {
|
|
50
|
+
/** A short notice shown to the clicker. */
|
|
51
|
+
toast?: { type: 'info' | 'success' | 'warning' | 'error'; content: string }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export type ButtonCallback = (event: ButtonEvent) => void | Promise<void>
|
|
55
|
+
|
|
56
|
+
/** Values one `<form>` submitted, keyed by the `name` of each control. */
|
|
57
|
+
export type FormValues = Record<string, string | string[] | boolean>
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Payload of a `<select>` interaction: the reader picked an option.
|
|
61
|
+
*
|
|
62
|
+
* The same forward-declaration rule as {@link ButtonEvent} applies — `plugin-im-core` augments this interface with
|
|
63
|
+
* `bot`, `user`, `chat` and `message`.
|
|
64
|
+
*/
|
|
65
|
+
export interface SelectEvent {
|
|
66
|
+
/** Control id (`select.id`). */
|
|
67
|
+
id: string
|
|
68
|
+
/** Chosen option values: one for a single select, in option order for a `multiple` one. */
|
|
69
|
+
values: string[]
|
|
70
|
+
/** Optional payload (≤ 256 bytes), as on a button. */
|
|
71
|
+
data?: string
|
|
72
|
+
/** Passive-reply / reply context the adapter may use. */
|
|
73
|
+
referrer: unknown
|
|
74
|
+
time: number
|
|
75
|
+
/** Answer the interaction on the platform; same contract as {@link ButtonEvent.respond}. */
|
|
76
|
+
respond?(answer: ButtonAnswer): Promise<void>
|
|
77
|
+
/** Claim the platform's answer for asynchronous work; same contract as {@link ButtonEvent.defer}. */
|
|
78
|
+
defer?(): void
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export type SelectCallback = (event: SelectEvent) => void | Promise<void>
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Payload of a checkbox toggle: the reader checked or cleared a `input[type=checkbox]`.
|
|
85
|
+
*
|
|
86
|
+
* The same forward-declaration rule as {@link ButtonEvent} applies — `plugin-im-core` augments this interface with
|
|
87
|
+
* `bot`, `user`, `chat` and `message`.
|
|
88
|
+
*/
|
|
89
|
+
export interface CheckEvent {
|
|
90
|
+
/** Control id (`input.id`). */
|
|
91
|
+
id: string
|
|
92
|
+
/** State after the toggle. */
|
|
93
|
+
checked: boolean
|
|
94
|
+
/** Optional payload (≤ 256 bytes), as on a button. */
|
|
95
|
+
data?: string
|
|
96
|
+
/** Passive-reply / reply context the adapter may use. */
|
|
97
|
+
referrer: unknown
|
|
98
|
+
time: number
|
|
99
|
+
/** Answer the interaction on the platform; same contract as {@link ButtonEvent.respond}. */
|
|
100
|
+
respond?(answer: ButtonAnswer): Promise<void>
|
|
101
|
+
/** Claim the platform's answer for asynchronous work; same contract as {@link ButtonEvent.defer}. */
|
|
102
|
+
defer?(): void
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export type CheckCallback = (event: CheckEvent) => void | Promise<void>
|
|
106
|
+
|
|
107
|
+
/** Size of a button (`ButtonStyle.size`). */
|
|
108
|
+
export type ButtonSize = 'tiny' | 'small' | 'medium' | 'large'
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Exact visual type of a button on platforms that name one (the Lark card `type`); it takes precedence over `theme`
|
|
112
|
+
* there, and platforms without these types ignore it.
|
|
113
|
+
*/
|
|
114
|
+
export type ButtonVariant =
|
|
115
|
+
| 'default' | 'primary' | 'danger' | 'text' | 'primary_text' | 'danger_text' | 'primary_filled' | 'danger_filled'
|
|
116
|
+
| 'laser'
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Optional presentation of a button, shared by every `button` type; a platform ignores what it cannot show. A type
|
|
120
|
+
* alias rather than an interface, so attribute records stay assignable to `Record<string, unknown>`.
|
|
121
|
+
*/
|
|
122
|
+
export type ButtonStyle = {
|
|
123
|
+
theme?: Theme
|
|
124
|
+
disabled?: boolean
|
|
125
|
+
size?: ButtonSize
|
|
126
|
+
/** `default` (fits the label), `fill` (the available width) or a width in pixels. */
|
|
127
|
+
width?: 'default' | 'fill' | number
|
|
128
|
+
/** Platform icon token (a Feishu standard icon such as `done_outlined`). */
|
|
129
|
+
icon?: string
|
|
130
|
+
/** Confirmation dialog shown before the click takes effect. */
|
|
131
|
+
confirm?: { title: string; text: string }
|
|
132
|
+
/** Hover text. */
|
|
133
|
+
tip?: string
|
|
134
|
+
/** Hover text while `disabled`. */
|
|
135
|
+
disabledTip?: string
|
|
136
|
+
variant?: ButtonVariant
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Merge-extensible: adapters augment it only for namespaced pure-content tags (none in v1). */
|
|
140
|
+
export interface ElementMap {
|
|
141
|
+
message: {}
|
|
142
|
+
quote: { id: string }
|
|
143
|
+
title: { subtitle?: string; theme?: Theme }
|
|
144
|
+
text: { content: string }
|
|
145
|
+
b: {}
|
|
146
|
+
i: {}
|
|
147
|
+
u: {}
|
|
148
|
+
s: {}
|
|
149
|
+
spoiler: {}
|
|
150
|
+
code: {}
|
|
151
|
+
a: { href: string }
|
|
152
|
+
at: { type: 'user' | 'all' | 'here' | 'role'; id?: string; name?: string }
|
|
153
|
+
channel: { id: string; name?: string }
|
|
154
|
+
emoji: { id: string; name?: string }
|
|
155
|
+
span: { color?: Color }
|
|
156
|
+
br: {}
|
|
157
|
+
p: {}
|
|
158
|
+
h1: {}
|
|
159
|
+
h2: {}
|
|
160
|
+
h3: {}
|
|
161
|
+
h4: {}
|
|
162
|
+
h5: {}
|
|
163
|
+
h6: {}
|
|
164
|
+
ul: {}
|
|
165
|
+
ol: { start?: number }
|
|
166
|
+
li: { checked?: boolean }
|
|
167
|
+
blockquote: {}
|
|
168
|
+
pre: { lang?: string }
|
|
169
|
+
hr: {}
|
|
170
|
+
table: {}
|
|
171
|
+
tr: {}
|
|
172
|
+
th: { align?: Align }
|
|
173
|
+
td: { align?: Align }
|
|
174
|
+
details: { open?: boolean }
|
|
175
|
+
summary: {}
|
|
176
|
+
small: {}
|
|
177
|
+
img: { src: string; alt?: string; width?: number; height?: number }
|
|
178
|
+
audio: { src: string; name?: string; duration?: number }
|
|
179
|
+
video: { src: string; name?: string; duration?: number; poster?: string }
|
|
180
|
+
file: { src: string; name?: string; size?: number }
|
|
181
|
+
button:
|
|
182
|
+
| ({ type: 'action'; id: string; data?: string; onClick?: ButtonCallback } & ButtonStyle)
|
|
183
|
+
| ({ type: 'link'; href: string } & ButtonStyle)
|
|
184
|
+
| ({ type: 'input'; text: string; send?: boolean } & ButtonStyle)
|
|
185
|
+
| ({ type: 'submit'; id: string; data?: string; onClick?: ButtonCallback } & ButtonStyle)
|
|
186
|
+
/** `width: 'fill'` shares the row among the buttons (each defaults to `width: 'fill'`); `align` places the row. */
|
|
187
|
+
'button-group': { width?: 'auto' | 'fill'; align?: Align }
|
|
188
|
+
/**
|
|
189
|
+
* A group of form controls whose values one submit button sends together. A platform without a form container
|
|
190
|
+
* renders the controls one by one and reports them as dropped interactions.
|
|
191
|
+
*/
|
|
192
|
+
form: { name: string }
|
|
193
|
+
/**
|
|
194
|
+
* One form control. `checkbox` is the separate native component a platform may have, and the only one that reports
|
|
195
|
+
* itself (`onChange`); every other type delivers its value only with the enclosing `<form>`'s submit, keyed by
|
|
196
|
+
* `name`. At least one of `name` and `id` is required: `name` feeds a form, `id` names the toggle callback.
|
|
197
|
+
*/
|
|
198
|
+
input: {
|
|
199
|
+
/** Form key inside a `<form>`; absent on a live checkbox that only reports its own toggles. */
|
|
200
|
+
name?: string
|
|
201
|
+
/** Callback identity of a live checkbox. */
|
|
202
|
+
id?: string
|
|
203
|
+
onChange?: CheckCallback
|
|
204
|
+
type?: 'text' | 'multiline' | 'password' | 'checkbox'
|
|
205
|
+
/** Visible text of a checkbox; the caption above a text field. */
|
|
206
|
+
label?: string
|
|
207
|
+
placeholder?: string
|
|
208
|
+
/** Initial value: the text of a text field, the state of a checkbox. */
|
|
209
|
+
value?: string | boolean
|
|
210
|
+
maxLength?: number
|
|
211
|
+
required?: boolean
|
|
212
|
+
disabled?: boolean
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* A choice control.
|
|
216
|
+
*
|
|
217
|
+
* `id` and `name` name the channel, and at least one is required: an `id` makes it a live control that reports its
|
|
218
|
+
* pick (`onSelect`), a `name` makes it a form field whose value arrives with the enclosing `<form>`'s submit. A
|
|
219
|
+
* `multiple` control is normally a form field — what a pick reports is one option, while the complete selection is
|
|
220
|
+
* what the submit carries.
|
|
221
|
+
*/
|
|
222
|
+
select: {
|
|
223
|
+
/** Callback identity of a live control; absent on a control that only feeds a form. */
|
|
224
|
+
id?: string
|
|
225
|
+
data?: string
|
|
226
|
+
onSelect?: SelectCallback
|
|
227
|
+
multiple?: boolean
|
|
228
|
+
/** Form key of this control inside a `<form>`; defaults to `id`. */
|
|
229
|
+
name?: string
|
|
230
|
+
placeholder?: string
|
|
231
|
+
/** Initially chosen option value(s). */
|
|
232
|
+
value?: string | string[]
|
|
233
|
+
required?: boolean
|
|
234
|
+
disabled?: boolean
|
|
235
|
+
}
|
|
236
|
+
/** One choice of a `<select>`; the label is the element's content. */
|
|
237
|
+
option: { value: string; selected?: boolean }
|
|
238
|
+
/**
|
|
239
|
+
* Ordered alternatives: each direct child (a fragment counts as one) is a candidate, and a renderer uses the first
|
|
240
|
+
* candidate it renders without degrading anything, else the last candidate.
|
|
241
|
+
*/
|
|
242
|
+
fallback: {}
|
|
243
|
+
/** @internal One candidate of a `<fallback>`; created by the factories and `normalize`, never written by hand. */
|
|
244
|
+
case: {}
|
|
245
|
+
/** Input only; removed by `normalize`. */
|
|
246
|
+
markdown: { content: string }
|
|
247
|
+
/** Inbound only. */
|
|
248
|
+
forward: { id: string }
|
|
249
|
+
/** Inbound only. */
|
|
250
|
+
raw: { platform: string; type: string; data?: unknown }
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export type Tag = keyof ElementMap
|
|
254
|
+
|
|
255
|
+
/** Variant vocabulary: produces `element.<tag>.<variant>` keys. */
|
|
256
|
+
export interface VariantMap {
|
|
257
|
+
at: 'user' | 'all' | 'here' | 'role'
|
|
258
|
+
li: 'checked' | 'nested'
|
|
259
|
+
pre: 'lang'
|
|
260
|
+
span: 'color'
|
|
261
|
+
table: 'align'
|
|
262
|
+
td: 'block'
|
|
263
|
+
img: 'inline'
|
|
264
|
+
button: 'action' | 'link' | 'input' | 'submit' | 'theme' | 'inline'
|
|
265
|
+
input: 'checkbox'
|
|
266
|
+
select: 'multiple'
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** Variants declared for a tag (`never` for tags without variants). */
|
|
270
|
+
export type VariantOf<K extends Tag> = K extends keyof VariantMap ? VariantMap[K] : never
|
|
271
|
+
|
|
272
|
+
/** `element.<tag>` and `element.<tag>.<variant>` capability keys (re-exported by plugin-im-core). */
|
|
273
|
+
export type ElementKey = {
|
|
274
|
+
[K in Tag]: `element.${K}` | (K extends keyof VariantMap ? `element.${K}.${VariantMap[K]}` : never)
|
|
275
|
+
}[Tag]
|
|
276
|
+
|
|
277
|
+
/** One element of a specific tag. */
|
|
278
|
+
export interface TypedElement<K extends Tag> {
|
|
279
|
+
type: K
|
|
280
|
+
attrs: ElementMap[K]
|
|
281
|
+
children: Element[]
|
|
282
|
+
/** Optional stable identity used by streaming diffs (React-like key, no React semantics). */
|
|
283
|
+
key?: string
|
|
284
|
+
/** XML-ish form for logs and goldens (`cordis-element` runtime). */
|
|
285
|
+
toString(strip?: boolean): string
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* An element. `Element` (no argument) is the discriminated union over every tag, so `el.type === 'a'` narrows
|
|
290
|
+
* `el.attrs` to `{ href: string }`; `Element<'a'>` is that single member.
|
|
291
|
+
*/
|
|
292
|
+
export type Element<K extends Tag = Tag> = K extends Tag ? TypedElement<K> : never
|
|
293
|
+
|
|
294
|
+
export type Fragment = string | Element | readonly (string | Element)[]
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Anything the factories, JSX and `normalize` accept as children: a `Fragment` plus numbers, nested arrays and the
|
|
298
|
+
* conditional-rendering holes `null`, `undefined`, `true`, `false` (which render nothing).
|
|
299
|
+
*/
|
|
300
|
+
export type Child = string | number | boolean | null | undefined | Element | readonly Child[]
|
package/README.md
DELETED