@glassly/miniapp 0.4.3-beta.1240 → 0.4.3-beta.1278
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/README.md +41 -3
- package/dist/display/profiles/types.d.ts.map +1 -1
- package/dist/display/scene/types.d.ts.map +1 -1
- package/dist/display/scene/types.js.map +1 -1
- package/dist/host/index.d.ts +1 -0
- package/dist/host/index.d.ts.map +1 -1
- package/dist/host/index.js +1 -0
- package/dist/host/index.js.map +1 -1
- package/dist/host/settingsBridge.d.ts +31 -8
- package/dist/host/settingsBridge.d.ts.map +1 -1
- package/dist/host/settingsBridge.js +63 -11
- package/dist/host/settingsBridge.js.map +1 -1
- package/dist/host/ttsSanitizer.d.ts +5 -0
- package/dist/host/ttsSanitizer.d.ts.map +1 -0
- package/dist/host/ttsSanitizer.js +61 -0
- package/dist/host/ttsSanitizer.js.map +1 -0
- package/dist/modules/cloud.d.ts +2 -0
- package/dist/modules/cloud.d.ts.map +1 -1
- package/dist/modules/cloud.js +17 -7
- package/dist/modules/cloud.js.map +1 -1
- package/dist/modules/settings.d.ts +4 -1
- package/dist/modules/settings.d.ts.map +1 -1
- package/dist/modules/settings.js +4 -1
- package/dist/modules/settings.js.map +1 -1
- package/dist/reader/Reader.d.ts +147 -0
- package/dist/reader/Reader.d.ts.map +1 -0
- package/dist/reader/Reader.js +525 -0
- package/dist/reader/Reader.js.map +1 -0
- package/dist/reader/document.d.ts +54 -0
- package/dist/reader/document.d.ts.map +1 -0
- package/dist/reader/document.js +114 -0
- package/dist/reader/document.js.map +1 -0
- package/dist/reader/index.d.ts +12 -0
- package/dist/reader/index.d.ts.map +1 -0
- package/dist/reader/index.js +9 -0
- package/dist/reader/index.js.map +1 -0
- package/dist/reader/lens.d.ts +55 -0
- package/dist/reader/lens.d.ts.map +1 -0
- package/dist/reader/lens.js +210 -0
- package/dist/reader/lens.js.map +1 -0
- package/dist/sub-path-types.test-d.d.ts +3 -1
- package/dist/sub-path-types.test-d.d.ts.map +1 -1
- package/dist/sub-path-types.test-d.js +1 -7
- package/dist/sub-path-types.test-d.js.map +1 -1
- package/dist/theme.css +856 -0
- package/dist/ui/index.d.ts +1 -0
- package/dist/ui/index.d.ts.map +1 -1
- package/dist/ui/index.js +1 -0
- package/dist/ui/index.js.map +1 -1
- package/dist/ui/kit/Badge.d.ts +8 -0
- package/dist/ui/kit/Badge.d.ts.map +1 -0
- package/dist/ui/kit/Badge.js +6 -0
- package/dist/ui/kit/Badge.js.map +1 -0
- package/dist/ui/kit/Button.d.ts +9 -0
- package/dist/ui/kit/Button.d.ts.map +1 -0
- package/dist/ui/kit/Button.js +7 -0
- package/dist/ui/kit/Button.js.map +1 -0
- package/dist/ui/kit/Card.d.ts +10 -0
- package/dist/ui/kit/Card.d.ts.map +1 -0
- package/dist/ui/kit/Card.js +21 -0
- package/dist/ui/kit/Card.js.map +1 -0
- package/dist/ui/kit/Dock.d.ts +29 -0
- package/dist/ui/kit/Dock.d.ts.map +1 -0
- package/dist/ui/kit/Dock.js +34 -0
- package/dist/ui/kit/Dock.js.map +1 -0
- package/dist/ui/kit/Feedback.d.ts +37 -0
- package/dist/ui/kit/Feedback.d.ts.map +1 -0
- package/dist/ui/kit/Feedback.js +21 -0
- package/dist/ui/kit/Feedback.js.map +1 -0
- package/dist/ui/kit/Field.d.ts +8 -0
- package/dist/ui/kit/Field.d.ts.map +1 -0
- package/dist/ui/kit/Field.js +16 -0
- package/dist/ui/kit/Field.js.map +1 -0
- package/dist/ui/kit/Page.d.ts +33 -0
- package/dist/ui/kit/Page.d.ts.map +1 -0
- package/dist/ui/kit/Page.js +77 -0
- package/dist/ui/kit/Page.js.map +1 -0
- package/dist/ui/kit/Section.d.ts +17 -0
- package/dist/ui/kit/Section.d.ts.map +1 -0
- package/dist/ui/kit/Section.js +22 -0
- package/dist/ui/kit/Section.js.map +1 -0
- package/dist/ui/kit/SetupGuide.d.ts +45 -0
- package/dist/ui/kit/SetupGuide.d.ts.map +1 -0
- package/dist/ui/kit/SetupGuide.js +40 -0
- package/dist/ui/kit/SetupGuide.js.map +1 -0
- package/dist/ui/kit/Switch.d.ts +7 -0
- package/dist/ui/kit/Switch.d.ts.map +1 -0
- package/dist/ui/kit/Switch.js +11 -0
- package/dist/ui/kit/Switch.js.map +1 -0
- package/dist/ui/kit/Tabs.d.ts +20 -0
- package/dist/ui/kit/Tabs.d.ts.map +1 -0
- package/dist/ui/kit/Tabs.js +53 -0
- package/dist/ui/kit/Tabs.js.map +1 -0
- package/dist/ui/kit/cx.d.ts +2 -0
- package/dist/ui/kit/cx.d.ts.map +1 -0
- package/dist/ui/kit/cx.js +4 -0
- package/dist/ui/kit/cx.js.map +1 -0
- package/dist/ui/kit/hooks.d.ts +17 -0
- package/dist/ui/kit/hooks.d.ts.map +1 -0
- package/dist/ui/kit/hooks.js +34 -0
- package/dist/ui/kit/hooks.js.map +1 -0
- package/dist/ui/kit/index.d.ts +14 -0
- package/dist/ui/kit/index.d.ts.map +1 -0
- package/dist/ui/kit/index.js +14 -0
- package/dist/ui/kit/index.js.map +1 -0
- package/dist/ui/kit/useWide.d.ts +4 -0
- package/dist/ui/kit/useWide.d.ts.map +1 -0
- package/dist/ui/kit/useWide.js +18 -0
- package/dist/ui/kit/useWide.js.map +1 -0
- package/package.json +16 -5
- package/src/display/profiles/types.ts +9 -1
- package/src/display/scene/types.ts +6 -1
- package/src/host/index.ts +1 -0
- package/src/host/settingsBridge.ts +72 -12
- package/src/host/ttsSanitizer.ts +66 -0
- package/src/modules/cloud.ts +21 -7
- package/src/modules/settings.ts +4 -1
- package/src/reader/Reader.ts +580 -0
- package/src/reader/document.ts +154 -0
- package/src/reader/index.ts +12 -0
- package/src/reader/lens.ts +250 -0
- package/src/sub-path-types.test-d.ts +5 -0
- package/src/ui/index.ts +2 -0
- package/src/ui/kit/Badge.tsx +19 -0
- package/src/ui/kit/Button.tsx +27 -0
- package/src/ui/kit/Card.tsx +29 -0
- package/src/ui/kit/Dock.tsx +77 -0
- package/src/ui/kit/Feedback.tsx +101 -0
- package/src/ui/kit/Field.tsx +38 -0
- package/src/ui/kit/Page.tsx +154 -0
- package/src/ui/kit/Section.tsx +80 -0
- package/src/ui/kit/SetupGuide.tsx +153 -0
- package/src/ui/kit/Switch.tsx +28 -0
- package/src/ui/kit/Tabs.tsx +108 -0
- package/src/ui/kit/cx.ts +3 -0
- package/src/ui/kit/hooks.ts +53 -0
- package/src/ui/kit/index.ts +51 -0
- package/src/ui/kit/useWide.ts +19 -0
- package/src/ui/theme.css +856 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The text as the lens reads it: every section wrapped into display lines,
|
|
3
|
+
* with each line's word count, where paragraphs end, and the parts between
|
|
4
|
+
* stops (an end screen). Pure: the Reader rebuilds it when the text, the
|
|
5
|
+
* glasses or the highlight style change.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type {TextWrapper} from "../display/wrapper"
|
|
9
|
+
|
|
10
|
+
export interface ReaderSection {
|
|
11
|
+
label?: string
|
|
12
|
+
text: string
|
|
13
|
+
status?: string
|
|
14
|
+
stop?: ReaderStop
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface ReaderStop {
|
|
18
|
+
title?: string
|
|
19
|
+
tap?: {label: string; action?: string}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface ReaderLine {
|
|
23
|
+
text: string
|
|
24
|
+
words: number
|
|
25
|
+
section: number
|
|
26
|
+
kind: "label" | "text" | "blank"
|
|
27
|
+
paragraphEnd: boolean
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface ReaderPart {
|
|
31
|
+
first: number
|
|
32
|
+
last: number
|
|
33
|
+
words: number
|
|
34
|
+
stopSection: number
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const PARAGRAPH_BEAT_WORDS = 3
|
|
38
|
+
|
|
39
|
+
const countWords = (text: string) => text.split(/\s+/).filter(Boolean).length
|
|
40
|
+
|
|
41
|
+
export class ReaderDocument {
|
|
42
|
+
readonly lines: ReaderLine[] = []
|
|
43
|
+
readonly parts: ReaderPart[] = []
|
|
44
|
+
private readonly before: number[] = []
|
|
45
|
+
|
|
46
|
+
constructor(
|
|
47
|
+
readonly sections: ReaderSection[],
|
|
48
|
+
wrapper: TextWrapper,
|
|
49
|
+
wrapWidthPx: number,
|
|
50
|
+
) {
|
|
51
|
+
const wrap = (text: string) =>
|
|
52
|
+
wrapper.wrap(text, {
|
|
53
|
+
maxWidthPx: wrapWidthPx,
|
|
54
|
+
maxLines: Infinity,
|
|
55
|
+
maxBytes: Infinity,
|
|
56
|
+
breakMode: "word",
|
|
57
|
+
trimLines: true,
|
|
58
|
+
preserveNewlines: true,
|
|
59
|
+
}).lines
|
|
60
|
+
sections.forEach((section, index) => {
|
|
61
|
+
if (index > 0 && this.lines.length)
|
|
62
|
+
this.push({text: "", words: 0, section: index, kind: "blank", paragraphEnd: false})
|
|
63
|
+
if (section.label) {
|
|
64
|
+
for (const text of wrap(section.label))
|
|
65
|
+
this.push({text, words: countWords(text), section: index, kind: "label", paragraphEnd: false})
|
|
66
|
+
}
|
|
67
|
+
const body = wrap(section.text.trim())
|
|
68
|
+
body.forEach((text, i) => {
|
|
69
|
+
const blank = text.trim() === ""
|
|
70
|
+
const next = body[i + 1]
|
|
71
|
+
this.push({
|
|
72
|
+
text: blank ? "" : text,
|
|
73
|
+
words: blank ? 0 : countWords(text),
|
|
74
|
+
section: index,
|
|
75
|
+
kind: blank ? "blank" : "text",
|
|
76
|
+
paragraphEnd: !blank && (next === undefined || next.trim() === ""),
|
|
77
|
+
})
|
|
78
|
+
})
|
|
79
|
+
})
|
|
80
|
+
if (!this.lines.length) this.push({text: "", words: 0, section: 0, kind: "blank", paragraphEnd: true})
|
|
81
|
+
|
|
82
|
+
let first = 0
|
|
83
|
+
let words = 0
|
|
84
|
+
this.lines.forEach((line, i) => {
|
|
85
|
+
this.before.push(words)
|
|
86
|
+
words += line.words
|
|
87
|
+
const next = this.lines[i + 1]
|
|
88
|
+
const sectionEnds = !next || next.section !== line.section
|
|
89
|
+
const stops = sectionEnds && (!next || sections[line.section]?.stop !== undefined)
|
|
90
|
+
if (!stops) return
|
|
91
|
+
this.parts.push({first, last: i, words, stopSection: line.section})
|
|
92
|
+
first = i + 1
|
|
93
|
+
words = 0
|
|
94
|
+
})
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
private push(line: ReaderLine): void {
|
|
98
|
+
this.lines.push(line)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
partOf(line: number): number {
|
|
102
|
+
const index = this.parts.findIndex((part) => line <= part.last)
|
|
103
|
+
return index < 0 ? this.parts.length - 1 : index
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
percent(line: number, finished = false): number {
|
|
107
|
+
const part = this.parts[this.partOf(line)]
|
|
108
|
+
if (finished || !part.words) return finished ? 100 : 0
|
|
109
|
+
return Math.min(100, Math.round((this.before[line] / part.words) * 100))
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
lineAt(partIndex: number, percent: number): number {
|
|
113
|
+
const part = this.parts[Math.max(0, Math.min(this.parts.length - 1, partIndex))]
|
|
114
|
+
return this.lineAtWord(partIndex, (Math.max(0, Math.min(100, percent)) / 100) * part.words)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** The line holding the part's word at `words` (words read before it); past the end, the part's last line. */
|
|
118
|
+
lineAtWord(partIndex: number, words: number): number {
|
|
119
|
+
const part = this.parts[Math.max(0, Math.min(this.parts.length - 1, partIndex))]
|
|
120
|
+
for (let i = part.first; i <= part.last; i++) {
|
|
121
|
+
if (this.before[i] + this.lines[i].words > words && this.lines[i].words > 0) return i
|
|
122
|
+
}
|
|
123
|
+
return this.previousReadable(part.last + 1, part.first) ?? part.first
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
wordsBefore(line: number): number {
|
|
127
|
+
return this.before[line] ?? 0
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
nextReadable(line: number, limit = this.lines.length - 1): number | null {
|
|
131
|
+
for (let i = line + 1; i <= limit; i++) if (this.lines[i].words > 0) return i
|
|
132
|
+
return null
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
previousReadable(line: number, floor = 0): number | null {
|
|
136
|
+
for (let i = line - 1; i >= floor; i--) if (this.lines[i].words > 0) return i
|
|
137
|
+
return null
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
wordsLeft(line: number): number {
|
|
141
|
+
const part = this.parts[this.partOf(line)]
|
|
142
|
+
return part.words - this.before[line]
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
lineMs(line: number, wpm: number, beats: boolean): number {
|
|
146
|
+
const entry = this.lines[line]
|
|
147
|
+
const beat = beats && entry.paragraphEnd ? PARAGRAPH_BEAT_WORDS : 0
|
|
148
|
+
return ((entry.words + beat) * 60_000) / wpm
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
rowsAround(line: number, at: number, count: number): string[] {
|
|
152
|
+
return Array.from({length: count}, (_, i) => this.lines[line - at + i]?.text ?? "")
|
|
153
|
+
}
|
|
154
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @glassly/miniapp/reader — text that reads itself on the glasses. Open it
|
|
3
|
+
* from the background side with the session and your text; see
|
|
4
|
+
* sdk/docs/reader.md.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export {openReader, Reader, READER_WPM} from "./Reader"
|
|
8
|
+
export type {ReaderDeps, ReaderEvent, ReaderOptions, ReaderSession} from "./Reader"
|
|
9
|
+
export {ReaderDocument, PARAGRAPH_BEAT_WORDS} from "./document"
|
|
10
|
+
export type {ReaderLine, ReaderPart, ReaderSection, ReaderStop} from "./document"
|
|
11
|
+
export {LENS_COLOR, lensElements, lensLayout, serializeRows, statusLine, textWrapWidth, usesBrackets} from "./lens"
|
|
12
|
+
export type {LensDisplay, LensFrame, LensHighlight, LensLayout} from "./lens"
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Reader's lens, lifted from Teleprompter's (#51): the text moves up
|
|
3
|
+
* under a fixed marked row just above the middle, lines already read sit
|
|
4
|
+
* dimmed above it, and a status row along the bottom always says the state.
|
|
5
|
+
* Glasses that place elements get a small scene; text-only glasses get one
|
|
6
|
+
* block of rows with the marked row in the same place. Pure: no timers, no I/O.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type {DisplayProfile} from "../display/profiles"
|
|
10
|
+
import type {TextMeasurer} from "../display/measurer"
|
|
11
|
+
import {textBoxLines} from "../display/profiles"
|
|
12
|
+
import type {RenderElement} from "../modules/display"
|
|
13
|
+
|
|
14
|
+
export type LensHighlight = "filled" | "brackets" | "dim"
|
|
15
|
+
|
|
16
|
+
export interface LensDisplay {
|
|
17
|
+
width?: number
|
|
18
|
+
height?: number
|
|
19
|
+
canPosition?: boolean
|
|
20
|
+
maxTextElements?: number
|
|
21
|
+
shapes?: string[]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface LensLayout {
|
|
25
|
+
mode: "scene" | "text"
|
|
26
|
+
width: number
|
|
27
|
+
height: number
|
|
28
|
+
pitch: number
|
|
29
|
+
rows: number
|
|
30
|
+
marked: number
|
|
31
|
+
wrapWidthPx: number
|
|
32
|
+
rect: boolean
|
|
33
|
+
budget: number
|
|
34
|
+
rowsPerText: number
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface LensFrame {
|
|
38
|
+
rows: string[]
|
|
39
|
+
status: string
|
|
40
|
+
detail: string
|
|
41
|
+
marked: number | null
|
|
42
|
+
highlight: LensHighlight
|
|
43
|
+
dimBefore?: number
|
|
44
|
+
toast?: string | null
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const DEFAULT_PITCH = 27
|
|
48
|
+
const PAD = 6
|
|
49
|
+
const TEXT_INSET = 4
|
|
50
|
+
const MAX_ROWS = 11
|
|
51
|
+
const MIN_ROWS = 3
|
|
52
|
+
const CANVAS = {width: 576, height: 288}
|
|
53
|
+
const BRACKET_OPEN = "> "
|
|
54
|
+
const BRACKET_CLOSE = " <"
|
|
55
|
+
|
|
56
|
+
export const LENS_COLOR = {read: 5, marked: 15, coming: 12, status: 9, bar: 3} as const
|
|
57
|
+
|
|
58
|
+
function markedRow(rows: number): number {
|
|
59
|
+
return Math.floor((rows - 1) / 2)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function usesBrackets(layout: LensLayout, highlight: LensHighlight): boolean {
|
|
63
|
+
if (highlight === "dim") return false
|
|
64
|
+
return highlight === "brackets" || layout.mode === "text" || !layout.rect
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** How the lens fits a model: rows, the marked row, the wrap width, and what it can afford to draw. */
|
|
68
|
+
export function lensLayout(profile: DisplayProfile, display: LensDisplay | null | undefined): LensLayout {
|
|
69
|
+
const budget = display?.maxTextElements ?? 0
|
|
70
|
+
if (display?.canPosition && display.width && display.height && budget >= 4) {
|
|
71
|
+
const pitch = profile.lineHeightPx ?? DEFAULT_PITCH
|
|
72
|
+
const total = Math.max(MIN_ROWS, Math.min(MAX_ROWS, Math.floor(display.height / pitch)))
|
|
73
|
+
return {
|
|
74
|
+
mode: "scene",
|
|
75
|
+
width: display.width,
|
|
76
|
+
height: display.height,
|
|
77
|
+
pitch,
|
|
78
|
+
rows: total - 1,
|
|
79
|
+
marked: markedRow(total - 1),
|
|
80
|
+
wrapWidthPx: Math.floor(Math.min(display.width - 2 * PAD - TEXT_INSET, profile.displayWidthPx)),
|
|
81
|
+
rect: (display.shapes ?? []).includes("rect") && budget >= 5,
|
|
82
|
+
budget,
|
|
83
|
+
rowsPerText: profile.lineHeightPx ? total : profile.maxLines,
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
const width = display?.width ?? CANVAS.width
|
|
87
|
+
const height = display?.height ?? CANVAS.height
|
|
88
|
+
const {maxLines} = textBoxLines(profile, {w: width, h: profile.displayHeightPx ?? height})
|
|
89
|
+
const total = Math.max(MIN_ROWS, Math.min(profile.maxLines, maxLines))
|
|
90
|
+
return {
|
|
91
|
+
mode: "text",
|
|
92
|
+
width,
|
|
93
|
+
height,
|
|
94
|
+
pitch: profile.lineHeightPx ?? DEFAULT_PITCH,
|
|
95
|
+
rows: total - 1,
|
|
96
|
+
marked: markedRow(total - 1),
|
|
97
|
+
wrapWidthPx: Math.floor(Math.min(profile.displayWidthPx, width)),
|
|
98
|
+
rect: false,
|
|
99
|
+
budget,
|
|
100
|
+
rowsPerText: total,
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function textWrapWidth(layout: LensLayout, highlight: LensHighlight, measurer: TextMeasurer): number {
|
|
105
|
+
if (!usesBrackets(layout, highlight)) return layout.wrapWidthPx
|
|
106
|
+
return layout.wrapWidthPx - measurer.measureText(BRACKET_OPEN + BRACKET_CLOSE)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function serializeRows(rows: string[]): string {
|
|
110
|
+
return rows.map((row) => (row === "" ? "" : row)).join("\n")
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function fit(measurer: TextMeasurer, text: string, widthPx: number): string {
|
|
114
|
+
if (measurer.measureText(text) <= widthPx) return text
|
|
115
|
+
let out = text
|
|
116
|
+
while (out && measurer.measureText(`${out}…`) > widthPx) out = out.slice(0, -1)
|
|
117
|
+
return out ? `${out.trimEnd()}…` : ""
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The status row as one line: the state, then the detail when both fit, the state shortened first. */
|
|
121
|
+
export function statusLine(measurer: TextMeasurer, status: string, detail: string, widthPx: number): string {
|
|
122
|
+
if (!detail) return fit(measurer, status, widthPx)
|
|
123
|
+
const both = `${status} ${detail}`
|
|
124
|
+
if (measurer.measureText(both) <= widthPx) return both
|
|
125
|
+
const room = widthPx - measurer.measureText(` ${detail}`)
|
|
126
|
+
const state = room > measurer.measureText("x…") ? fit(measurer, status, room) : ""
|
|
127
|
+
return state ? `${state} ${detail}` : fit(measurer, status, widthPx)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function markText(layout: LensLayout, frame: LensFrame, row: string): string {
|
|
131
|
+
return row && usesBrackets(layout, frame.highlight) ? `${BRACKET_OPEN}${row}${BRACKET_CLOSE}` : row
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function lensElements(layout: LensLayout, frame: LensFrame, measurer: TextMeasurer): RenderElement[] {
|
|
135
|
+
const rows = Array.from({length: layout.rows}, (_, i) => frame.rows[i] ?? "")
|
|
136
|
+
const marked = frame.marked
|
|
137
|
+
if (layout.mode === "text") {
|
|
138
|
+
const body = rows.map((row, i) => (i === marked ? markText(layout, frame, row) : row))
|
|
139
|
+
const status = frame.toast
|
|
140
|
+
? fit(measurer, frame.toast.replace(/\n/g, " · "), layout.wrapWidthPx)
|
|
141
|
+
: statusLine(measurer, frame.status, frame.detail, layout.wrapWidthPx)
|
|
142
|
+
return [
|
|
143
|
+
{
|
|
144
|
+
type: "text",
|
|
145
|
+
id: "reader-text",
|
|
146
|
+
box: {x: 0, y: 0, w: layout.width, h: layout.height},
|
|
147
|
+
text: serializeRows([...body, status]),
|
|
148
|
+
},
|
|
149
|
+
]
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const {pitch, width} = layout
|
|
153
|
+
const textWidth = width - 2 * PAD
|
|
154
|
+
const statusY = layout.rows * pitch
|
|
155
|
+
const elements: RenderElement[] = []
|
|
156
|
+
const block = (id: string, from: number, to: number, color: number) => {
|
|
157
|
+
for (let at = from, part = 1; at < to; at += layout.rowsPerText, part++) {
|
|
158
|
+
const end = Math.min(to, at + layout.rowsPerText)
|
|
159
|
+
if (part > 1 && rows.slice(at, end).every((row) => row === "")) continue
|
|
160
|
+
elements.push({
|
|
161
|
+
type: "text",
|
|
162
|
+
id: part === 1 ? id : `${id}-${part}`,
|
|
163
|
+
box: {x: PAD, y: at * pitch, w: textWidth, h: (end - at) * pitch},
|
|
164
|
+
text: serializeRows(rows.slice(at, end)),
|
|
165
|
+
style: {color},
|
|
166
|
+
})
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (marked === null) {
|
|
171
|
+
const dim = Math.max(0, Math.min(layout.rows, frame.dimBefore ?? 0))
|
|
172
|
+
block("reader-read", 0, dim, LENS_COLOR.read)
|
|
173
|
+
block("reader-next", dim, layout.rows, LENS_COLOR.marked)
|
|
174
|
+
} else {
|
|
175
|
+
block("reader-read", 0, marked, LENS_COLOR.read)
|
|
176
|
+
if (layout.rect && frame.highlight === "filled") {
|
|
177
|
+
elements.push({
|
|
178
|
+
type: "rect",
|
|
179
|
+
id: "reader-bar",
|
|
180
|
+
box: {x: 0, y: marked * pitch, w: width, h: pitch},
|
|
181
|
+
style: {fill: true, color: LENS_COLOR.bar, radius: 4},
|
|
182
|
+
})
|
|
183
|
+
}
|
|
184
|
+
elements.push({
|
|
185
|
+
type: "text",
|
|
186
|
+
id: "reader-mark",
|
|
187
|
+
box: {x: PAD, y: marked * pitch, w: textWidth, h: pitch},
|
|
188
|
+
text: serializeRows([markText(layout, frame, rows[marked] ?? "")]),
|
|
189
|
+
style: {color: LENS_COLOR.marked},
|
|
190
|
+
})
|
|
191
|
+
block("reader-next", marked + 1, layout.rows, LENS_COLOR.coming)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const statusBox = {y: statusY, h: layout.height - statusY}
|
|
195
|
+
const inner = textWidth - TEXT_INSET
|
|
196
|
+
const toastFits = layout.budget >= elements.length + 2
|
|
197
|
+
const toast = frame.toast && toastFits ? frame.toast : null
|
|
198
|
+
if (frame.toast && !toastFits) frame = {...frame, status: frame.toast.replace(/\n/g, " · "), detail: ""}
|
|
199
|
+
const split = !frame.toast && layout.budget > elements.length + 1
|
|
200
|
+
const detailWidth = frame.detail ? measurer.measureText(frame.detail) : 0
|
|
201
|
+
if (split && frame.detail && detailWidth + measurer.measureText(frame.status) + 24 <= inner) {
|
|
202
|
+
const detailBox = detailWidth + TEXT_INSET
|
|
203
|
+
elements.push(
|
|
204
|
+
{
|
|
205
|
+
type: "text",
|
|
206
|
+
id: "reader-status",
|
|
207
|
+
box: {x: PAD, ...statusBox, w: textWidth - detailBox},
|
|
208
|
+
text: frame.status || "",
|
|
209
|
+
style: {color: LENS_COLOR.status},
|
|
210
|
+
},
|
|
211
|
+
{
|
|
212
|
+
type: "text",
|
|
213
|
+
id: "reader-detail",
|
|
214
|
+
box: {x: width - PAD - detailBox, ...statusBox, w: detailBox},
|
|
215
|
+
text: frame.detail,
|
|
216
|
+
style: {color: LENS_COLOR.status},
|
|
217
|
+
},
|
|
218
|
+
)
|
|
219
|
+
} else {
|
|
220
|
+
elements.push({
|
|
221
|
+
type: "text",
|
|
222
|
+
id: "reader-status",
|
|
223
|
+
box: {x: PAD, ...statusBox, w: textWidth},
|
|
224
|
+
text: statusLine(measurer, frame.status, frame.detail, inner) || "",
|
|
225
|
+
style: {color: LENS_COLOR.status},
|
|
226
|
+
})
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (toast) {
|
|
230
|
+
const lines = toast.split("\n").map((line) => fit(measurer, line, textWidth - 2 * PAD))
|
|
231
|
+
const top = layout.rows - lines.length
|
|
232
|
+
for (let i = elements.length - 1; i >= 0; i--) {
|
|
233
|
+
const el = elements[i]
|
|
234
|
+
if (el.type !== "text" || !/^reader-(read|next)/.test(el.id ?? "")) continue
|
|
235
|
+
const from = el.box.y / pitch
|
|
236
|
+
const to = from + el.box.h / pitch
|
|
237
|
+
if (to <= top) continue
|
|
238
|
+
if (from >= top) elements.splice(i, 1)
|
|
239
|
+
else elements[i] = {...el, box: {...el.box, h: (top - from) * pitch}, text: serializeRows(rows.slice(from, top))}
|
|
240
|
+
}
|
|
241
|
+
elements.push({
|
|
242
|
+
type: "text",
|
|
243
|
+
id: "reader-toast",
|
|
244
|
+
box: {x: PAD, y: top * pitch, w: textWidth, h: lines.length * pitch},
|
|
245
|
+
text: serializeRows(lines),
|
|
246
|
+
style: {color: LENS_COLOR.marked, border: 2, radius: 6},
|
|
247
|
+
})
|
|
248
|
+
}
|
|
249
|
+
return elements
|
|
250
|
+
}
|
|
@@ -58,3 +58,8 @@ export type _Typed = TypedUiGlobal<{foo: number}>
|
|
|
58
58
|
// disappear. The asymmetric exports are enforced by package.json's
|
|
59
59
|
// `exports` map at consumer time; this file proves the source files
|
|
60
60
|
// expose what they should.)
|
|
61
|
+
|
|
62
|
+
// ----- @glassly/miniapp/reader -------------------------------------------
|
|
63
|
+
// A real session is a ReaderSession, so apps pass theirs straight in.
|
|
64
|
+
import type {ReaderSession} from "./reader/index"
|
|
65
|
+
export const sessionIsReaderSession = (session: MiniappSession): ReaderSession => session
|
package/src/ui/index.ts
CHANGED
|
@@ -132,3 +132,5 @@ export type TypedUiGlobal<TChannels extends object> = UiGlobal<TChannels>
|
|
|
132
132
|
|
|
133
133
|
export {MirrorCanvas, type MirrorCanvasProps} from "../react/MirrorCanvas"
|
|
134
134
|
export type {MirrorSnapshot, MirrorContent, MirrorElement} from "../mirror/types"
|
|
135
|
+
|
|
136
|
+
export * from "./kit"
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type {HTMLAttributes} from "react"
|
|
2
|
+
|
|
3
|
+
import {cx} from "./cx"
|
|
4
|
+
|
|
5
|
+
export type BadgeVariant = "default" | "secondary" | "outline" | "destructive"
|
|
6
|
+
|
|
7
|
+
export interface BadgeProps extends HTMLAttributes<HTMLSpanElement> {
|
|
8
|
+
variant?: BadgeVariant
|
|
9
|
+
dot?: boolean
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function Badge({variant = "secondary", dot, className, children, ...props}: BadgeProps) {
|
|
13
|
+
return (
|
|
14
|
+
<span data-variant={variant} className={cx("gl-badge", className)} {...props}>
|
|
15
|
+
{dot ? <span className="gl-badge-dot" aria-hidden="true" /> : null}
|
|
16
|
+
<span className="gl-badge-label">{children}</span>
|
|
17
|
+
</span>
|
|
18
|
+
)
|
|
19
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import {forwardRef, type ButtonHTMLAttributes} from "react"
|
|
2
|
+
|
|
3
|
+
import {cx} from "./cx"
|
|
4
|
+
|
|
5
|
+
export type ButtonVariant = "default" | "secondary" | "outline" | "ghost" | "destructive"
|
|
6
|
+
export type ButtonSize = "default" | "sm" | "lg" | "icon"
|
|
7
|
+
|
|
8
|
+
export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
|
|
9
|
+
variant?: ButtonVariant
|
|
10
|
+
size?: ButtonSize
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const Button = forwardRef<HTMLButtonElement, ButtonProps>(function Button(
|
|
14
|
+
{variant = "default", size = "default", type = "button", className, ...props},
|
|
15
|
+
ref,
|
|
16
|
+
) {
|
|
17
|
+
return (
|
|
18
|
+
<button
|
|
19
|
+
ref={ref}
|
|
20
|
+
type={type}
|
|
21
|
+
data-variant={variant}
|
|
22
|
+
data-size={size}
|
|
23
|
+
className={cx("gl-button", className)}
|
|
24
|
+
{...props}
|
|
25
|
+
/>
|
|
26
|
+
)
|
|
27
|
+
})
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type {HTMLAttributes} from "react"
|
|
2
|
+
|
|
3
|
+
import {cx} from "./cx"
|
|
4
|
+
|
|
5
|
+
type DivProps = HTMLAttributes<HTMLDivElement>
|
|
6
|
+
|
|
7
|
+
export function Card({className, ...props}: DivProps) {
|
|
8
|
+
return <div className={cx("gl-card", className)} {...props} />
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function CardHeader({className, ...props}: DivProps) {
|
|
12
|
+
return <div className={cx("gl-card-header", className)} {...props} />
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function CardTitle({className, ...props}: HTMLAttributes<HTMLHeadingElement>) {
|
|
16
|
+
return <h3 className={cx("gl-card-title", className)} {...props} />
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function CardDescription({className, ...props}: HTMLAttributes<HTMLParagraphElement>) {
|
|
20
|
+
return <p className={cx("gl-card-description", className)} {...props} />
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function CardContent(props: DivProps) {
|
|
24
|
+
return <div {...props} />
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function CardFooter({className, ...props}: DivProps) {
|
|
28
|
+
return <div className={cx("gl-card-footer", className)} {...props} />
|
|
29
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type {ReactNode} from "react"
|
|
2
|
+
|
|
3
|
+
import type {MiniappSafeAreaInsets} from "../../globals"
|
|
4
|
+
import {useSafeArea} from "../../react/useSafeArea"
|
|
5
|
+
import {cx} from "./cx"
|
|
6
|
+
import type {HoldHandlers} from "./hooks"
|
|
7
|
+
|
|
8
|
+
export const DOCK_HEIGHT = 54
|
|
9
|
+
export const RAIL_MIN_WIDTH = 60
|
|
10
|
+
export const RAIL_CENTER = 48
|
|
11
|
+
export const PILL_WIDTH = 44
|
|
12
|
+
|
|
13
|
+
export function railSide(insets: MiniappSafeAreaInsets): "left" | "right" | null {
|
|
14
|
+
if (insets.right >= RAIL_MIN_WIDTH) return "right"
|
|
15
|
+
if (insets.left >= RAIL_MIN_WIDTH) return "left"
|
|
16
|
+
return null
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function dockClearance(insets: MiniappSafeAreaInsets, gap = 12): number {
|
|
20
|
+
return railSide(insets) ? insets.bottom : Math.max(insets.bottom, 12) + DOCK_HEIGHT + gap
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function useDockClearance(gap = 12): number {
|
|
24
|
+
return dockClearance(useSafeArea().insets, gap)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface DockItem {
|
|
28
|
+
value: string
|
|
29
|
+
label: string
|
|
30
|
+
icon: ReactNode
|
|
31
|
+
holdHandlers?: HoldHandlers
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface DockProps {
|
|
35
|
+
items: DockItem[]
|
|
36
|
+
value: string
|
|
37
|
+
onValueChange: (value: string) => void
|
|
38
|
+
label?: string
|
|
39
|
+
className?: string
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The floating tab dock: a pill along the bottom, or an icon column beside the
|
|
44
|
+
* host's menu button in the folded iPhone Duo's status rail.
|
|
45
|
+
*/
|
|
46
|
+
export function Dock({items, value, onValueChange, label = "Main navigation", className}: DockProps) {
|
|
47
|
+
const {insets} = useSafeArea()
|
|
48
|
+
const rail = railSide(insets)
|
|
49
|
+
const bottom = Math.max(insets.bottom, 12)
|
|
50
|
+
return (
|
|
51
|
+
<nav
|
|
52
|
+
aria-label={label}
|
|
53
|
+
className={cx("gl-dock", className)}
|
|
54
|
+
data-rail={rail ?? undefined}
|
|
55
|
+
style={rail ? {bottom, [rail]: RAIL_CENTER - PILL_WIDTH / 2} : {bottom}}>
|
|
56
|
+
<div role="tablist" aria-orientation={rail ? "vertical" : "horizontal"} className="gl-dock-pill">
|
|
57
|
+
{items.map((item) => {
|
|
58
|
+
const selected = item.value === value
|
|
59
|
+
return (
|
|
60
|
+
<button
|
|
61
|
+
key={item.value}
|
|
62
|
+
type="button"
|
|
63
|
+
role="tab"
|
|
64
|
+
aria-selected={selected}
|
|
65
|
+
aria-label={item.label}
|
|
66
|
+
className="gl-dock-item"
|
|
67
|
+
onClick={() => onValueChange(item.value)}
|
|
68
|
+
{...item.holdHandlers}>
|
|
69
|
+
{item.icon}
|
|
70
|
+
{selected && !rail ? <span>{item.label}</span> : null}
|
|
71
|
+
</button>
|
|
72
|
+
)
|
|
73
|
+
})}
|
|
74
|
+
</div>
|
|
75
|
+
</nav>
|
|
76
|
+
)
|
|
77
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type {HTMLAttributes, ReactNode} from "react"
|
|
2
|
+
|
|
3
|
+
import {Button} from "./Button"
|
|
4
|
+
import {cx} from "./cx"
|
|
5
|
+
|
|
6
|
+
export type BannerVariant = "info" | "warning" | "error"
|
|
7
|
+
|
|
8
|
+
export interface BannerProps extends Omit<HTMLAttributes<HTMLDivElement>, "title"> {
|
|
9
|
+
variant?: BannerVariant
|
|
10
|
+
title?: ReactNode
|
|
11
|
+
action?: {label: string; onClick: () => void}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** A notice across the page: what happened, in plain words, and what to do about it. */
|
|
15
|
+
export function Banner({variant = "info", title, action, className, children, ...props}: BannerProps) {
|
|
16
|
+
return (
|
|
17
|
+
<div
|
|
18
|
+
role={variant === "error" ? "alert" : "status"}
|
|
19
|
+
data-variant={variant}
|
|
20
|
+
className={cx("gl-banner", className)}
|
|
21
|
+
{...props}>
|
|
22
|
+
<div className="gl-banner-text">
|
|
23
|
+
{title ? <strong className="gl-banner-title">{title}</strong> : null}
|
|
24
|
+
{children ? <span>{children}</span> : null}
|
|
25
|
+
</div>
|
|
26
|
+
{action ? (
|
|
27
|
+
<Button size="sm" variant="outline" onClick={action.onClick}>
|
|
28
|
+
{action.label}
|
|
29
|
+
</Button>
|
|
30
|
+
) : null}
|
|
31
|
+
</div>
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface EmptyStateProps extends Omit<HTMLAttributes<HTMLDivElement>, "title"> {
|
|
36
|
+
icon?: ReactNode
|
|
37
|
+
title: ReactNode
|
|
38
|
+
description?: ReactNode
|
|
39
|
+
action?: ReactNode
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function EmptyState({icon, title, description, action, className, ...props}: EmptyStateProps) {
|
|
43
|
+
return (
|
|
44
|
+
<div className={cx("gl-empty", className)} {...props}>
|
|
45
|
+
{icon ? <div className="gl-empty-icon">{icon}</div> : null}
|
|
46
|
+
<p className="gl-empty-title">{title}</p>
|
|
47
|
+
{description ? <p className="gl-empty-description">{description}</p> : null}
|
|
48
|
+
{action}
|
|
49
|
+
</div>
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface SpinnerProps {
|
|
54
|
+
size?: number
|
|
55
|
+
label?: string
|
|
56
|
+
className?: string
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function Spinner({size = 18, label = "Loading", className}: SpinnerProps) {
|
|
60
|
+
return (
|
|
61
|
+
<span
|
|
62
|
+
role="status"
|
|
63
|
+
aria-label={label}
|
|
64
|
+
className={cx("gl-spinner", className)}
|
|
65
|
+
style={{width: size, height: size}}
|
|
66
|
+
/>
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface StatProps extends HTMLAttributes<HTMLDivElement> {
|
|
71
|
+
label: ReactNode
|
|
72
|
+
value: ReactNode
|
|
73
|
+
hint?: ReactNode
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** A labelled number: a score, a count, a reading. */
|
|
77
|
+
export function Stat({label, value, hint, className, ...props}: StatProps) {
|
|
78
|
+
return (
|
|
79
|
+
<div className={cx("gl-stat", className)} {...props}>
|
|
80
|
+
<span className="gl-stat-label">{label}</span>
|
|
81
|
+
<span className="gl-stat-value">{value}</span>
|
|
82
|
+
{hint ? <span className="gl-stat-hint">{hint}</span> : null}
|
|
83
|
+
</div>
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface SeparatorProps extends HTMLAttributes<HTMLDivElement> {
|
|
88
|
+
orientation?: "horizontal" | "vertical"
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function Separator({orientation = "horizontal", className, ...props}: SeparatorProps) {
|
|
92
|
+
return (
|
|
93
|
+
<div
|
|
94
|
+
role="separator"
|
|
95
|
+
aria-orientation={orientation}
|
|
96
|
+
data-orientation={orientation}
|
|
97
|
+
className={cx("gl-separator", className)}
|
|
98
|
+
{...props}
|
|
99
|
+
/>
|
|
100
|
+
)
|
|
101
|
+
}
|