@cmodjs/core 5.10.0 → 5.11.0
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/docs/commands.md +19 -3
- package/docs/jobs.md +1 -1
- package/docs/mod.md +1 -1
- package/docs/ui.md +2 -0
- package/package.json +1 -1
- package/records.d.ts +8 -1
- package/records.d.ts.map +1 -1
- package/records.js +18 -5
- package/runtime/engine.d.ts +2 -0
- package/runtime/engine.d.ts.map +1 -1
- package/runtime/lifecycle.js +13 -1
- package/runtime/web.d.ts +82 -0
- package/runtime/web.d.ts.map +1 -0
- package/runtime/web.js +381 -0
- package/src/records.ts +23 -5
- package/src/runtime/engine.ts +2 -0
- package/src/runtime/lifecycle.ts +11 -1
- package/src/runtime/web.ts +450 -0
|
@@ -0,0 +1,450 @@
|
|
|
1
|
+
import type { ListItem, PhrasingContent, RootContent, TableCell } from 'mdast'
|
|
2
|
+
import { fromMarkdown, gfm, gfmFromMarkdown } from '../vendor-markdown.js'
|
|
3
|
+
|
|
4
|
+
const elementKey = '$cmodElement'
|
|
5
|
+
|
|
6
|
+
const handlerKey = '$cmodHandler'
|
|
7
|
+
|
|
8
|
+
type Props = Readonly<Record<string, unknown>>
|
|
9
|
+
|
|
10
|
+
type Declarations = readonly (readonly [string, string | undefined])[]
|
|
11
|
+
|
|
12
|
+
type Drawing = { readonly rules: string[]; readonly groups: Map<string, string>; scopes: number; scope: string | undefined }
|
|
13
|
+
|
|
14
|
+
const themeKeys: ReadonlySet<string> = new Set(['text', 'inverseText', 'inactive', 'subtle', 'suggestion', 'remember', 'success', 'error', 'warning', 'merged', 'claude', 'permission', 'planMode', 'autoAccept', 'promptBorder', 'bashBorder', 'ide', 'diffAdded', 'diffRemoved', 'diffAddedDimmed', 'diffRemovedDimmed', 'diffAddedWord', 'diffRemovedWord'])
|
|
15
|
+
|
|
16
|
+
const spaceSteps: Readonly<Record<string, number>> = { xs: 0.25, sm: 0.5, md: 1, lg: 1.5, xl: 2 }
|
|
17
|
+
|
|
18
|
+
const flexValues: Readonly<Record<string, ReadonlySet<string>>> = {
|
|
19
|
+
flexDirection: new Set(['row', 'column', 'row-reverse', 'column-reverse']),
|
|
20
|
+
flexWrap: new Set(['nowrap', 'wrap', 'wrap-reverse']),
|
|
21
|
+
alignItems: new Set(['flex-start', 'center', 'flex-end', 'stretch']),
|
|
22
|
+
alignSelf: new Set(['flex-start', 'center', 'flex-end', 'auto']),
|
|
23
|
+
justifyContent: new Set(['flex-start', 'center', 'flex-end', 'space-between', 'space-around', 'space-evenly']),
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const cssNames: Readonly<Record<string, string>> = { flexDirection: 'flex-direction', flexWrap: 'flex-wrap', alignItems: 'align-items', alignSelf: 'align-self', justifyContent: 'justify-content' }
|
|
27
|
+
|
|
28
|
+
const sides = [
|
|
29
|
+
['Top', 'Y', 'down'],
|
|
30
|
+
['Right', 'X', 'across'],
|
|
31
|
+
['Bottom', 'Y', 'down'],
|
|
32
|
+
['Left', 'X', 'across'],
|
|
33
|
+
] as const
|
|
34
|
+
|
|
35
|
+
const linkSchemes = /^(https?:|mailto:|file:)/i
|
|
36
|
+
|
|
37
|
+
const hotkeys = /^[0-9a-z]$/
|
|
38
|
+
|
|
39
|
+
const base64 = /^[A-Za-z0-9+/]*={0,2}$/
|
|
40
|
+
|
|
41
|
+
const markdownOptions = { extensions: [gfm()], mdastExtensions: [gfmFromMarkdown()] }
|
|
42
|
+
|
|
43
|
+
type Markdown = { readonly link: number | undefined; readonly pressable: readonly string[] | undefined }
|
|
44
|
+
|
|
45
|
+
export type SidebarTab = { readonly key: string; readonly label: string; readonly isClosable: boolean }
|
|
46
|
+
|
|
47
|
+
export type ClosedPage = { readonly mod: string; readonly page: string; readonly title: string }
|
|
48
|
+
|
|
49
|
+
export type SessionWait = { readonly kind: 'permission'; readonly tool: string; readonly input: Readonly<Record<string, unknown>> } | { readonly kind: 'question'; readonly question: string }
|
|
50
|
+
|
|
51
|
+
export type TurnOutcome = { readonly reason: 'answered' | 'stopped' | 'failed'; readonly at: string; readonly failure?: string }
|
|
52
|
+
|
|
53
|
+
export type SessionAbout = {
|
|
54
|
+
readonly tabs: readonly SidebarTab[]
|
|
55
|
+
readonly pages: readonly ClosedPage[]
|
|
56
|
+
readonly isWorking?: boolean
|
|
57
|
+
readonly waiting?: SessionWait
|
|
58
|
+
readonly lastTurn?: TurnOutcome
|
|
59
|
+
readonly contextPercent?: number
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export type WebView = { readonly id: string; readonly tab: string; readonly columns: number }
|
|
63
|
+
|
|
64
|
+
export type WebInput = { readonly draw: number; readonly handler: number; readonly key: string; readonly kind: 'press' | 'change' | 'submit' | 'select' | 'link'; readonly value?: string; readonly href?: string }
|
|
65
|
+
|
|
66
|
+
export type WebAction =
|
|
67
|
+
| ({ readonly type: 'press'; readonly view: string } & WebInput)
|
|
68
|
+
| { readonly type: 'open'; readonly mod: string; readonly page: string }
|
|
69
|
+
| { readonly type: 'close'; readonly tab: string }
|
|
70
|
+
| { readonly type: 'prompt'; readonly text: string }
|
|
71
|
+
| { readonly type: 'stop' }
|
|
72
|
+
|
|
73
|
+
export type ElementData = { readonly $cmodElement: string; readonly props: Readonly<Record<string, unknown>> }
|
|
74
|
+
|
|
75
|
+
export type HandlerData = { readonly $cmodHandler: number }
|
|
76
|
+
|
|
77
|
+
export type SessionEvents = { readonly views: readonly WebView[]; readonly actions: readonly WebAction[] }
|
|
78
|
+
|
|
79
|
+
export type WebDrawing = { readonly view: string; readonly draw: number; readonly tree: unknown }
|
|
80
|
+
|
|
81
|
+
export type SessionPost = { readonly about?: SessionAbout; readonly drawings?: readonly WebDrawing[] }
|
|
82
|
+
|
|
83
|
+
export function htmlOf(tree: unknown): string {
|
|
84
|
+
const drawing: Drawing = { rules: [], groups: new Map(), scopes: 0, scope: undefined }
|
|
85
|
+
const body = nodeHtml(tree, drawing)
|
|
86
|
+
return drawing.rules.length === 0 ? body : `<style>${drawing.rules.join('')}</style>${body}`
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function nodeHtml(tree: unknown, drawing: Drawing): string {
|
|
90
|
+
if (tree === null || tree === undefined || typeof tree === 'boolean') return ''
|
|
91
|
+
if (typeof tree === 'string') return escaped(tree)
|
|
92
|
+
if (typeof tree === 'number') return String(tree)
|
|
93
|
+
if (Array.isArray(tree)) return tree.map((each: unknown) => nodeHtml(each, drawing)).join('')
|
|
94
|
+
if (typeof tree !== 'object') return ''
|
|
95
|
+
const fields = tree as Props
|
|
96
|
+
const name = fields[elementKey]
|
|
97
|
+
if (typeof name !== 'string') return ''
|
|
98
|
+
const props = (typeof fields['props'] === 'object' && fields['props'] !== null ? fields['props'] : {}) as Props
|
|
99
|
+
const draw = elements[name]
|
|
100
|
+
return draw === undefined ? `<span class="gap">${escaped(`The browser draws no ${name}.`)}</span>` : draw(props, drawing)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const elements: Readonly<Record<string, (props: Props, drawing: Drawing) => string>> = {
|
|
104
|
+
Box: (props, drawing) => {
|
|
105
|
+
const key = keyOf(props)
|
|
106
|
+
const own = key === undefined ? undefined : `s${drawing.scopes++}`
|
|
107
|
+
const outer = drawing.scope
|
|
108
|
+
const hover = hoverClasses(props['hover'], boxHover(props), drawing, own ?? outer)
|
|
109
|
+
const classes = ['b', ...(typeof props['borderStyle'] === 'string' ? ['bordered'] : []), ...(own === undefined ? [] : [own]), ...hover]
|
|
110
|
+
drawing.scope = own ?? outer
|
|
111
|
+
const children = nodeHtml(props['children'], drawing)
|
|
112
|
+
drawing.scope = outer
|
|
113
|
+
return `<div class="${classes.join(' ')}"${attribute('data-key', key)}${attribute('style', boxStyle(props))}>${children}</div>`
|
|
114
|
+
},
|
|
115
|
+
Text: (props, drawing) => {
|
|
116
|
+
const hover = hoverClasses(props['hover'], textHover(props), drawing, drawing.scope)
|
|
117
|
+
return `<span class="${['t', ...textClasses(props), ...hover].join(' ')}"${attribute('style', textStyle(props))}${attribute('dir', props['dir'] === 'rtl' || props['dir'] === 'ltr' ? props['dir'] : undefined)}>${nodeHtml(props['children'], drawing)}</span>`
|
|
118
|
+
},
|
|
119
|
+
Button: (props, drawing) => {
|
|
120
|
+
const label = typeof props['label'] === 'string' ? props['label'] : undefined
|
|
121
|
+
const drawn = nodeHtml(props['children'], drawing)
|
|
122
|
+
const shown = drawn === '' ? escaped(label ?? '') : drawn
|
|
123
|
+
const hotkey = typeof props['hotkey'] === 'string' && hotkeys.test(props['hotkey']) ? props['hotkey'] : undefined
|
|
124
|
+
const prefix = props['plain'] === true && hotkey !== undefined ? `<span class="hotkey">${hotkey}</span>: ` : ''
|
|
125
|
+
const hover = hoverClasses(props['hover'], textHover({}), drawing, drawing.scope)
|
|
126
|
+
const classes = ['button', ...(props['plain'] === true ? ['plain'] : props['variant'] === 'primary' ? ['primary'] : []), ...(props['dimColor'] === true ? ['dim'] : []), ...hover]
|
|
127
|
+
const key = keyOf(props) ?? label ?? (typeof props['children'] === 'string' ? props['children'] : undefined)
|
|
128
|
+
return `<button type="button" class="${classes.join(' ')}"${attribute('aria-label', drawn === '' ? undefined : label)}${attribute('data-key', key)}${handlerAttribute('data-press', props['onPress'])}${attribute('data-hotkey', hotkey)}${props['autoFocus'] === true ? ' autofocus' : ''}>${prefix}${shown}</button>`
|
|
129
|
+
},
|
|
130
|
+
Link: (props, drawing) => {
|
|
131
|
+
const href = typeof props['href'] === 'string' ? props['href'] : ''
|
|
132
|
+
const drawn = nodeHtml(props['children'], drawing)
|
|
133
|
+
const text = drawn === '' ? escaped(typeof props['label'] === 'string' ? props['label'] : href) : drawn
|
|
134
|
+
return linkSchemes.test(href) ? `<a class="link" href="${escaped(href)}" target="_blank" rel="noopener noreferrer">${text}</a>` : `<span class="t">${text}</span>`
|
|
135
|
+
},
|
|
136
|
+
Code: (props) => codeHtml(typeof props['source'] === 'string' ? props['source'] : '', typeof props['startLine'] === 'number' ? props['startLine'] : undefined, props['format'] === 'diff', props['wrap'] === 'truncate-end'),
|
|
137
|
+
Markdown: (props) => {
|
|
138
|
+
const text = typeof props['text'] === 'string' ? props['text'] : ''
|
|
139
|
+
const pressable = Array.isArray(props['pressableLinks']) ? props['pressableLinks'].filter((href): href is string => typeof href === 'string') : undefined
|
|
140
|
+
const blocks = fromMarkdown(text, markdownOptions).children.map((node) => blockHtml(node, { link: handlerOf(props['onLinkPress']), pressable })).join('')
|
|
141
|
+
return `<div class="${props['dimColor'] === true ? 'markdown dim' : 'markdown'}"${attribute('data-key', keyOf(props))}>${blocks}</div>`
|
|
142
|
+
},
|
|
143
|
+
Input: (props) => {
|
|
144
|
+
const value = typeof props['value'] === 'string' ? props['value'] : ''
|
|
145
|
+
const rows = typeof props['rows'] === 'number' && props['rows'] > 1 ? Math.floor(props['rows']) : 1
|
|
146
|
+
const shared = `${attribute('data-key', keyOf(props))}${handlerAttribute('data-input', props['onInput'])}${handlerAttribute('data-submit', props['onSubmit'])}${attribute('data-value', value)}${attribute('placeholder', typeof props['placeholder'] === 'string' ? props['placeholder'] : undefined)}${props['autoFocus'] === true ? ' autofocus' : ''}`
|
|
147
|
+
const field = rows > 1 ? `<textarea class="input" rows="${rows}"${shared}>${escaped(value)}</textarea>` : `<input type="text" class="input"${shared} value="${escaped(value)}">`
|
|
148
|
+
const submitLabel = typeof props['submitLabel'] === 'string' ? props['submitLabel'] : 'submit'
|
|
149
|
+
return `<label class="field">${labelHtml(props['label'])}${field}<span class="hint">enter: ${escaped(submitLabel)}</span></label>`
|
|
150
|
+
},
|
|
151
|
+
Select: (props) => {
|
|
152
|
+
const value = typeof props['value'] === 'string' ? props['value'] : undefined
|
|
153
|
+
const options = (Array.isArray(props['options']) ? props['options'] : []).flatMap((option: unknown) => {
|
|
154
|
+
const each = option as Props
|
|
155
|
+
if (typeof each['value'] !== 'string') return []
|
|
156
|
+
const label = typeof each['label'] === 'string' ? each['label'] : each['value']
|
|
157
|
+
return [`<option value="${escaped(each['value'])}"${each['value'] === value ? ' selected' : ''}>${escaped(label)}</option>`]
|
|
158
|
+
})
|
|
159
|
+
return `<label class="field">${labelHtml(props['label'])}<select class="select"${attribute('data-key', keyOf(props))}${handlerAttribute('data-select', props['onSelect'])}${attribute('data-value', value)}${props['autoFocus'] === true ? ' autofocus' : ''}>${options.join('')}</select></label>`
|
|
160
|
+
},
|
|
161
|
+
Image: (props) => {
|
|
162
|
+
const source = props['source'] as Props | undefined
|
|
163
|
+
const alt = typeof props['alt'] === 'string' ? props['alt'] : ''
|
|
164
|
+
const size = attribute('style', `width:${across(numberOr(props['columns'], 1))};height:${down(numberOr(props['rows'], 1))}`)
|
|
165
|
+
if (typeof source?.['png'] === 'string' && base64.test(source['png'])) return `<img class="image" alt="${escaped(alt)}" src="data:image/png;base64,${source['png']}"${size}>`
|
|
166
|
+
const [width, height] = [source?.['width'], source?.['height']]
|
|
167
|
+
if (typeof source?.['rgba'] === 'string' && base64.test(source['rgba']) && Number.isInteger(width) && Number.isInteger(height)) return `<canvas class="image" width="${width as number}" height="${height as number}" data-rgba="${source['rgba']}"${attribute('aria-label', alt)}${size}></canvas>`
|
|
168
|
+
return `<span class="t dim"${size}>${escaped(alt)}</span>`
|
|
169
|
+
},
|
|
170
|
+
Svg: (props) => {
|
|
171
|
+
const source = typeof props['source'] === 'string' ? props['source'] : ''
|
|
172
|
+
const size = [typeof props['width'] === 'number' ? `width:${props['width']}px` : '', typeof props['height'] === 'number' ? `height:${props['height']}px` : ''].filter((each) => each !== '').join(';')
|
|
173
|
+
return `<img class="svg" alt="${escaped(typeof props['alt'] === 'string' ? props['alt'] : '')}" src="data:image/svg+xml,${encodeURIComponent(source)}"${attribute('style', size === '' ? undefined : size)}>`
|
|
174
|
+
},
|
|
175
|
+
Raster: (props) => rasterHtml(typeof props['cells'] === 'string' ? props['cells'] : '', numberOr(props['columns'], 1)),
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function escaped(text: string): string {
|
|
179
|
+
return text.replace(/[&<>"']/g, (character) => `&#${character.charCodeAt(0)};`)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function attribute(name: string, value: string | undefined): string {
|
|
183
|
+
return value === undefined || value === '' ? '' : ` ${name}="${escaped(value)}"`
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function handlerOf(value: unknown): number | undefined {
|
|
187
|
+
const marker = typeof value === 'object' && value !== null ? (value as Props)[handlerKey] : undefined
|
|
188
|
+
return typeof marker === 'number' ? marker : undefined
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function handlerAttribute(name: string, value: unknown): string {
|
|
192
|
+
const handler = handlerOf(value)
|
|
193
|
+
return handler === undefined ? '' : ` ${name}="${handler}"`
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function keyOf(props: Props): string | undefined {
|
|
197
|
+
return typeof props['key'] === 'string' ? props['key'] : undefined
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function numberOr(value: unknown, fallback: number): number {
|
|
201
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : fallback
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function across(cells: number): string {
|
|
205
|
+
return `${cells}ch`
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function down(cells: number): string {
|
|
209
|
+
return `calc(${cells} * var(--row))`
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function cellsOf(value: unknown): number | undefined {
|
|
213
|
+
if (typeof value === 'number' && Number.isFinite(value)) return value
|
|
214
|
+
return typeof value === 'string' ? spaceSteps[value] : undefined
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function sizeOf(value: unknown, axis: 'across' | 'down'): string | undefined {
|
|
218
|
+
if (typeof value === 'string' && /^\d+(\.\d+)?%$/.test(value)) return value
|
|
219
|
+
const cells = typeof value === 'number' && Number.isFinite(value) ? value : undefined
|
|
220
|
+
return cells === undefined ? undefined : axis === 'across' ? across(cells) : down(cells)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function colorOf(value: unknown): string | undefined {
|
|
224
|
+
if (typeof value !== 'string') return undefined
|
|
225
|
+
if (themeKeys.has(value) || /^[a-z]+[A-Z][A-Za-z]*$/.test(value)) return `var(--${value})`
|
|
226
|
+
return /^#[0-9a-f]{3,8}$/i.test(value) || /^(rgb|hsl)a?\([\d\s.,%]+\)$/i.test(value) || /^[a-z]+$/.test(value) ? value : undefined
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function dimmed(color: string): string {
|
|
230
|
+
return `color-mix(in srgb, ${color} 55%, transparent)`
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function css(declarations: Declarations, suffix = ''): string {
|
|
234
|
+
return declarations.flatMap(([name, value]) => (value === undefined ? [] : [`${name}:${value}${suffix}`])).join(';')
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function hoverClasses(hover: unknown, declarations: (hover: Props) => Declarations, drawing: Drawing, scope: string | undefined): readonly string[] {
|
|
238
|
+
if (typeof hover !== 'object' || hover === null) return []
|
|
239
|
+
const lit = hover as Props
|
|
240
|
+
const rule = css(declarations(lit), ' !important')
|
|
241
|
+
const group = typeof lit['scope'] === 'string' ? (drawing.groups.get(lit['scope']) ?? `g${drawing.groups.size}`) : undefined
|
|
242
|
+
if (rule === '' || (group === undefined && scope === undefined)) return []
|
|
243
|
+
const name = `h${drawing.rules.length}`
|
|
244
|
+
if (group === undefined) {
|
|
245
|
+
drawing.rules.push(`.${scope}:hover .${name},.${scope}.${name}:hover{${rule}}`)
|
|
246
|
+
return [name]
|
|
247
|
+
}
|
|
248
|
+
drawing.groups.set(lit['scope'] as string, group)
|
|
249
|
+
drawing.rules.push(`:root:has(.${group}:hover) .${name}{${rule}}`)
|
|
250
|
+
return [name, group]
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function boxHover(props: Props): (hover: Props) => Declarations {
|
|
254
|
+
return (hover) => {
|
|
255
|
+
const offset = (side: string, axis: 'across' | 'down') => (props['position'] === 'absolute' ? sizeOf(hover[side], axis) : undefined)
|
|
256
|
+
const color = colorOf(hover['borderColor'] ?? props['borderColor']) ?? 'currentColor'
|
|
257
|
+
const isBorderLit = typeof props['borderStyle'] === 'string' && (hover['borderColor'] !== undefined || hover['borderDimColor'] !== undefined)
|
|
258
|
+
return [
|
|
259
|
+
['display', hover['display'] === 'flex' ? 'flex' : undefined],
|
|
260
|
+
['background', colorOf(hover['backgroundColor'])],
|
|
261
|
+
['--border-color', isBorderLit ? ((hover['borderDimColor'] ?? props['borderDimColor']) === true ? dimmed(color) : color) : undefined],
|
|
262
|
+
['top', offset('top', 'down')],
|
|
263
|
+
['left', offset('left', 'across')],
|
|
264
|
+
['right', offset('right', 'across')],
|
|
265
|
+
['bottom', offset('bottom', 'down')],
|
|
266
|
+
]
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function textHover(props: Props): (hover: Props) => Declarations {
|
|
271
|
+
return (hover) => textDeclarations({ ...props, ...hover })
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function boxStyle(props: Props): string {
|
|
275
|
+
const style: [string, string | undefined][] = []
|
|
276
|
+
const add = (name: string, value: string | undefined) => void style.push([name, value])
|
|
277
|
+
for (const [prop, allowed] of Object.entries(flexValues)) add(cssNames[prop] ?? prop, typeof props[prop] === 'string' && allowed.has(props[prop]) ? props[prop] : undefined)
|
|
278
|
+
add('flex-grow', typeof props['flexGrow'] === 'number' ? String(props['flexGrow']) : undefined)
|
|
279
|
+
add('flex-shrink', typeof props['flexShrink'] === 'number' ? String(props['flexShrink']) : undefined)
|
|
280
|
+
const columnGap = cellsOf(props['columnGap'] ?? props['gap'])
|
|
281
|
+
const rowGap = cellsOf(props['rowGap'] ?? props['gap'])
|
|
282
|
+
add('column-gap', columnGap === undefined ? undefined : across(columnGap))
|
|
283
|
+
add('row-gap', rowGap === undefined ? undefined : down(rowGap))
|
|
284
|
+
add('width', sizeOf(props['width'], 'across'))
|
|
285
|
+
add('height', sizeOf(props['height'], 'down'))
|
|
286
|
+
add('min-width', sizeOf(props['minWidth'], 'across'))
|
|
287
|
+
add('min-height', sizeOf(props['minHeight'], 'down'))
|
|
288
|
+
const hasBorder = typeof props['borderStyle'] === 'string'
|
|
289
|
+
for (const [side, axis, unit] of sides) {
|
|
290
|
+
const margin = cellsOf(props[`margin${side}`] ?? props[`margin${axis}`] ?? props['margin'])
|
|
291
|
+
const padding = (cellsOf(props[`padding${side}`] ?? props[`padding${axis}`] ?? props['padding']) ?? 0) + (hasBorder ? 1 : 0)
|
|
292
|
+
add(`margin-${side.toLowerCase()}`, margin === undefined ? undefined : unit === 'across' ? across(margin) : down(margin))
|
|
293
|
+
if (padding > 0) add(`padding-${side.toLowerCase()}`, unit === 'across' ? across(padding) : down(padding))
|
|
294
|
+
}
|
|
295
|
+
if (hasBorder) {
|
|
296
|
+
const color = colorOf(props['borderColor']) ?? 'currentColor'
|
|
297
|
+
add('--border-color', props['borderDimColor'] === true ? dimmed(color) : color)
|
|
298
|
+
add('--border-style', props['borderStyle'] === 'double' ? 'double' : props['borderStyle'] === 'classic' || props['borderStyle'] === 'dashed' ? 'dashed' : 'solid')
|
|
299
|
+
add('--border-radius', props['borderStyle'] === 'round' ? '0.5ch' : '0')
|
|
300
|
+
add('--border-width', props['borderStyle'] === 'bold' || props['borderStyle'] === 'double' ? '3px' : '1px')
|
|
301
|
+
}
|
|
302
|
+
add('background', colorOf(props['backgroundColor']))
|
|
303
|
+
add('overflow', props['overflow'] === 'hidden' ? 'hidden' : undefined)
|
|
304
|
+
add('display', props['display'] === 'none' ? 'none' : undefined)
|
|
305
|
+
if (props['position'] === 'absolute') {
|
|
306
|
+
add('position', 'absolute')
|
|
307
|
+
add('top', sizeOf(props['top'], 'down'))
|
|
308
|
+
add('bottom', sizeOf(props['bottom'], 'down'))
|
|
309
|
+
add('left', sizeOf(props['left'], 'across'))
|
|
310
|
+
add('right', sizeOf(props['right'], 'across'))
|
|
311
|
+
}
|
|
312
|
+
return css(style)
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function textClasses(props: Props): string[] {
|
|
316
|
+
const wrap = props['wrap']
|
|
317
|
+
const isCut = typeof wrap === 'string' && wrap !== 'wrap'
|
|
318
|
+
const size = props['size']
|
|
319
|
+
return [...(isCut ? ['cut'] : []), ...(typeof size === 'string' && ['caption', 'footnote', 'heading', 'title'].includes(size) ? [`size-${size}`] : [])]
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function textDeclarations(props: Props): Declarations {
|
|
323
|
+
const color = colorOf(props['color'])
|
|
324
|
+
const background = colorOf(props['backgroundColor'])
|
|
325
|
+
const isDim = props['dimColor'] === true
|
|
326
|
+
const foreground = color === undefined && !isDim ? undefined : isDim ? dimmed(color ?? 'var(--text)') : color
|
|
327
|
+
const isInverse = props['inverse'] === true
|
|
328
|
+
const weight = props['weight']
|
|
329
|
+
const weights: Readonly<Record<string, string>> = { regular: '400', medium: '500', semibold: '600', bold: '700' }
|
|
330
|
+
const lines = [props['underline'] === true ? 'underline' : '', props['strikethrough'] === true ? 'line-through' : ''].filter((line) => line !== '')
|
|
331
|
+
return [
|
|
332
|
+
['color', isInverse ? (background ?? 'var(--background)') : foreground],
|
|
333
|
+
['background', isInverse ? (foreground ?? 'var(--text)') : background],
|
|
334
|
+
['font-weight', typeof weight === 'string' && weights[weight] !== undefined ? weights[weight] : props['bold'] === true ? '700' : undefined],
|
|
335
|
+
['font-style', props['italic'] === true ? 'italic' : undefined],
|
|
336
|
+
['text-decoration', lines.length === 0 ? undefined : lines.join(' ')],
|
|
337
|
+
['font-variant-numeric', props['tabular'] === true ? 'tabular-nums' : undefined],
|
|
338
|
+
]
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function textStyle(props: Props): string {
|
|
342
|
+
return css(textDeclarations(props))
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function labelHtml(label: unknown): string {
|
|
346
|
+
return typeof label === 'string' ? `<span class="label">${escaped(label)}</span>` : ''
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function codeHtml(source: string, startLine: number | undefined, isDiff: boolean, isCut: boolean): string {
|
|
350
|
+
const lines = source.split('\n')
|
|
351
|
+
const width = String((startLine ?? 1) + lines.length - 1).length
|
|
352
|
+
const rows = lines.map((line, index) => {
|
|
353
|
+
const kind = !isDiff ? '' : line.startsWith('+') ? ' added' : line.startsWith('-') ? ' removed' : line.startsWith('@@') ? ' hunk' : ''
|
|
354
|
+
const gutter = startLine === undefined || isDiff ? '' : `<span class="gutter">${String(startLine + index).padStart(width)} </span>`
|
|
355
|
+
return `<span class="line${kind}">${gutter}${escaped(line)}</span>`
|
|
356
|
+
})
|
|
357
|
+
return `<pre class="${isCut ? 'code cut' : 'code'}">${rows.join('\n')}</pre>`
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function rasterHtml(cells: string, columns: number): string {
|
|
361
|
+
if (cells.length % 4 !== 0 || !base64.test(cells)) return '<span class="gap">The browser draws no Raster whose cells are not base64.</span>'
|
|
362
|
+
const bytes = Uint8Array.from(atob(cells), (character) => character.charCodeAt(0))
|
|
363
|
+
const words = new DataView(bytes.buffer)
|
|
364
|
+
const colorFrom = (word: number) => (word === 0x01000000 ? undefined : `#${(word & 0xffffff).toString(16).padStart(6, '0')}`)
|
|
365
|
+
const rows: string[] = []
|
|
366
|
+
let row = ''
|
|
367
|
+
for (let offset = 0, index = 0; offset + 12 <= bytes.length; offset += 12, index += 1) {
|
|
368
|
+
const glyph = String.fromCodePoint(words.getUint32(offset, true))
|
|
369
|
+
const style = [colorFrom(words.getUint32(offset + 4, true)), colorFrom(words.getUint32(offset + 8, true))]
|
|
370
|
+
const painted = [style[0] === undefined ? '' : `color:${style[0]}`, style[1] === undefined ? '' : `background:${style[1]}`].filter((each) => each !== '').join(';')
|
|
371
|
+
row += painted === '' ? escaped(glyph) : `<span style="${painted}">${escaped(glyph)}</span>`
|
|
372
|
+
if ((index + 1) % Math.max(Math.floor(columns), 1) === 0) {
|
|
373
|
+
rows.push(row)
|
|
374
|
+
row = ''
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
if (row !== '') rows.push(row)
|
|
378
|
+
return `<pre class="raster">${rows.join('\n')}</pre>`
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
function linkHtml(href: string, inner: string, markdown: Markdown): string {
|
|
382
|
+
const isPressed = markdown.link !== undefined && (markdown.pressable === undefined || markdown.pressable.includes(href))
|
|
383
|
+
if (isPressed) return `<a class="link" href="${escaped(href)}" data-link="${markdown.link}">${inner}</a>`
|
|
384
|
+
return linkSchemes.test(href) ? `<a class="link" href="${escaped(href)}" target="_blank" rel="noopener noreferrer">${inner}</a>` : inner
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function inlineHtml(node: PhrasingContent, markdown: Markdown): string {
|
|
388
|
+
const inner = () => ('children' in node ? node.children.map((child) => inlineHtml(child, markdown)).join('') : '')
|
|
389
|
+
switch (node.type) {
|
|
390
|
+
case 'text':
|
|
391
|
+
return escaped(node.value)
|
|
392
|
+
case 'emphasis':
|
|
393
|
+
return `<em>${inner()}</em>`
|
|
394
|
+
case 'strong':
|
|
395
|
+
return `<strong>${inner()}</strong>`
|
|
396
|
+
case 'delete':
|
|
397
|
+
return `<del>${inner()}</del>`
|
|
398
|
+
case 'inlineCode':
|
|
399
|
+
return `<code>${escaped(node.value)}</code>`
|
|
400
|
+
case 'break':
|
|
401
|
+
return '<br>'
|
|
402
|
+
case 'link':
|
|
403
|
+
return linkHtml(node.url, inner(), markdown)
|
|
404
|
+
case 'image':
|
|
405
|
+
return linkHtml(node.url, escaped(node.alt ?? node.url), markdown)
|
|
406
|
+
case 'html':
|
|
407
|
+
return escaped(node.value)
|
|
408
|
+
default:
|
|
409
|
+
return inner()
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
function itemHtml(item: ListItem, markdown: Markdown): string {
|
|
414
|
+
const check = item.checked === true ? '☑ ' : item.checked === false ? '☐ ' : ''
|
|
415
|
+
return `<li>${check}${item.children.map((child) => blockHtml(child, markdown)).join('')}</li>`
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function cellHtml(cell: TableCell, tag: 'th' | 'td', align: string | null | undefined, markdown: Markdown): string {
|
|
419
|
+
return `<${tag}${align === null || align === undefined ? '' : ` style="text-align:${align}"`}>${cell.children.map((child) => inlineHtml(child, markdown)).join('')}</${tag}>`
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function blockHtml(node: RootContent, markdown: Markdown): string {
|
|
423
|
+
switch (node.type) {
|
|
424
|
+
case 'heading':
|
|
425
|
+
return `<h${node.depth}>${node.children.map((child) => inlineHtml(child, markdown)).join('')}</h${node.depth}>`
|
|
426
|
+
case 'paragraph':
|
|
427
|
+
return `<p>${node.children.map((child) => inlineHtml(child, markdown)).join('')}</p>`
|
|
428
|
+
case 'code':
|
|
429
|
+
return codeHtml(node.value, undefined, node.lang === 'diff', false)
|
|
430
|
+
case 'blockquote':
|
|
431
|
+
return `<blockquote>${node.children.map((child) => blockHtml(child, markdown)).join('')}</blockquote>`
|
|
432
|
+
case 'list': {
|
|
433
|
+
const items = node.children.map((item) => itemHtml(item, markdown)).join('')
|
|
434
|
+
return node.ordered === true ? `<ol${node.start === null || node.start === undefined || node.start === 1 ? '' : ` start="${node.start}"`}>${items}</ol>` : `<ul>${items}</ul>`
|
|
435
|
+
}
|
|
436
|
+
case 'table': {
|
|
437
|
+
const [head, ...body] = node.children
|
|
438
|
+
const row = (cells: readonly TableCell[], tag: 'th' | 'td') => `<tr>${cells.map((cell, index) => cellHtml(cell, tag, node.align?.[index], markdown)).join('')}</tr>`
|
|
439
|
+
return `<table>${head === undefined ? '' : `<thead>${row(head.children, 'th')}</thead>`}<tbody>${body.map((each) => row(each.children, 'td')).join('')}</tbody></table>`
|
|
440
|
+
}
|
|
441
|
+
case 'thematicBreak':
|
|
442
|
+
return '<hr>'
|
|
443
|
+
case 'html':
|
|
444
|
+
return `<p>${escaped(node.value)}</p>`
|
|
445
|
+
case 'listItem':
|
|
446
|
+
return itemHtml(node, markdown)
|
|
447
|
+
default:
|
|
448
|
+
return 'children' in node ? node.children.map((child) => blockHtml(child as RootContent, markdown)).join('') : 'value' in node ? `<p>${escaped(String(node.value))}</p>` : ''
|
|
449
|
+
}
|
|
450
|
+
}
|