@dotgui/embed 0.2.0 → 0.4.1
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 +33 -9
- package/dist/fonts/Inter.woff2 +0 -0
- package/dist/fonts/OFL.txt +92 -0
- package/dist/gui-embed.esm.js +1 -120
- package/dist/gui-embed.js +10 -120
- package/dist/pkg/dotgui_renderer_wasm.js +1110 -0
- package/dist/pkg/dotgui_renderer_wasm_bg.wasm +0 -0
- package/dist/render-worker.js +262 -0
- package/package.json +15 -9
- package/src/auto.ts +26 -3
- package/src/error-message.test.ts +55 -0
- package/src/error-message.ts +63 -0
- package/src/gui-embed.ts +299 -112
- package/src/index.ts +8 -6
- package/src/decode.ts +0 -60
- package/src/modes.ts +0 -26
package/src/gui-embed.ts
CHANGED
|
@@ -5,56 +5,98 @@
|
|
|
5
5
|
* lives on `<gui>`. Everything here is host vocabulary, expressed in CSS:
|
|
6
6
|
*
|
|
7
7
|
* <gui-embed style="width:100vw;height:100vh" zoom download copy mode-switch>
|
|
8
|
-
* <gui w="300" h="300"> … </gui>
|
|
8
|
+
* <script type="application/gui"> <gui w="300" h="300"> … </gui> </script>
|
|
9
9
|
* </gui-embed>
|
|
10
10
|
*
|
|
11
|
-
* <gui-embed src="hero.gui" style="width:400px
|
|
11
|
+
* <gui-embed src="hero.gui" style="width:400px"></gui-embed>
|
|
12
12
|
*
|
|
13
|
-
* The
|
|
14
|
-
*
|
|
15
|
-
* player
|
|
13
|
+
* The drawing is `@dotgui/render`'s `<gui-canvas>`: the Rust renderer, as
|
|
14
|
+
* WebAssembly, in a worker. This element only places it — center-center,
|
|
15
|
+
* scaled to fit — and adds the player around it. Controls are individual
|
|
16
|
+
* boolean attributes — present = on. `zoom` defaults on (it is a player);
|
|
17
|
+
* `download`, `copy`, `mode-switch`, `page-switch` default off.
|
|
18
|
+
*
|
|
19
|
+
* A package may hold several documents and a library (RFC-0042). The first
|
|
20
|
+
* page shows by default; `page="name"` picks another, and `page-switch` adds
|
|
21
|
+
* a page dropdown to the controls.
|
|
16
22
|
*/
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
|
|
20
|
-
decodeMarkup,
|
|
21
|
-
revokeAssets,
|
|
22
|
-
toGuiBytes,
|
|
23
|
-
type Decoded,
|
|
24
|
-
} from './decode'
|
|
25
|
-
import { detectModes, switchableAxes, type ModeAxis } from './modes'
|
|
23
|
+
import type { GuiCanvas, GuiPage, ModeAxis, RenderDetail } from '@dotgui/render/canvas'
|
|
24
|
+
import { pack } from '@dotgui/kit/package'
|
|
25
|
+
import { summarizeError } from './error-message'
|
|
26
26
|
|
|
27
27
|
const TAG = 'gui-embed'
|
|
28
28
|
|
|
29
|
+
/** How far a zoom may go, as a multiple of the fitted scale. */
|
|
30
|
+
const MIN_ZOOM = 0.25
|
|
31
|
+
const MAX_ZOOM = 8
|
|
32
|
+
|
|
29
33
|
export class GuiEmbedElement extends HTMLElement {
|
|
30
|
-
private
|
|
34
|
+
private _stage!: HTMLDivElement
|
|
35
|
+
private _view!: HTMLDivElement
|
|
36
|
+
private _canvas!: GuiCanvas
|
|
31
37
|
private _bar!: HTMLDivElement
|
|
32
|
-
private _zoomCtl: ZoomControl | null = null
|
|
33
|
-
private _decoded: Decoded | null = null
|
|
34
|
-
private _modes: Record<string, ModeAxis> = {}
|
|
35
|
-
private _mode: Record<string, string> = {}
|
|
36
38
|
private _ro: ResizeObserver | null = null
|
|
39
|
+
/** The bytes shown, kept for download: a fetched file as it came, or inline markup. */
|
|
40
|
+
private _bytes: Uint8Array | null = null
|
|
41
|
+
private _markup = ''
|
|
42
|
+
/** The document's size in document pixels, once drawn. */
|
|
43
|
+
private _size: { width: number; height: number } | null = null
|
|
44
|
+
/** The view: scale, and the document's top-left in the stage. */
|
|
45
|
+
private _scale = 1
|
|
46
|
+
private _x = 0
|
|
47
|
+
private _y = 0
|
|
37
48
|
/** True once the user pans/zooms — stops auto-fit from snapping their view back. */
|
|
38
49
|
private _userZoomed = false
|
|
50
|
+
private _settle: ReturnType<typeof setTimeout> | null = null
|
|
51
|
+
/** The page the controls were built for, to rebuild them when another page draws. */
|
|
52
|
+
private _controlsPage: string | null = null
|
|
39
53
|
|
|
40
54
|
static get observedAttributes() {
|
|
41
|
-
return ['src']
|
|
55
|
+
return ['src', 'page']
|
|
42
56
|
}
|
|
43
57
|
|
|
44
58
|
connectedCallback() {
|
|
45
|
-
if (!this.
|
|
59
|
+
if (!this._stage) this._build()
|
|
46
60
|
void this._load()
|
|
47
61
|
}
|
|
48
62
|
|
|
49
63
|
disconnectedCallback() {
|
|
50
64
|
this._ro?.disconnect()
|
|
51
65
|
this._ro = null
|
|
52
|
-
if (this._decoded) revokeAssets(this._decoded.assetMap)
|
|
53
|
-
this._decoded = null
|
|
54
66
|
}
|
|
55
67
|
|
|
56
68
|
attributeChangedCallback(name: string) {
|
|
57
|
-
if (
|
|
69
|
+
if (!this._stage) return
|
|
70
|
+
if (name === 'src') void this._load()
|
|
71
|
+
else if (name === 'page') this._showPage(this.getAttribute('page'))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The `<gui-canvas>` drawing the file. */
|
|
75
|
+
get canvas(): GuiCanvas {
|
|
76
|
+
return this._canvas
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The package's pages in order: its documents, then the library's own page. Empty for bare markup. */
|
|
80
|
+
get pages(): GuiPage[] {
|
|
81
|
+
return this._canvas?.pages ?? []
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The page on show, or null for bare markup. Set it, or the `page` attribute, to show another. */
|
|
85
|
+
get page(): string | null {
|
|
86
|
+
return this._canvas?.page ?? null
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
set page(name: string | null) {
|
|
90
|
+
if (name == null) this.removeAttribute('page')
|
|
91
|
+
else this.setAttribute('page', name)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Shows a page by name. An unknown name, or none, leaves the page on show. */
|
|
95
|
+
private _showPage(name: string | null) {
|
|
96
|
+
if (!name || name === this._canvas.page) return
|
|
97
|
+
if (!this._canvas.pages.some((page) => page.name === name)) return
|
|
98
|
+
this._userZoomed = false
|
|
99
|
+
this._canvas.page = name
|
|
58
100
|
}
|
|
59
101
|
|
|
60
102
|
// ── DOM scaffold ──────────────────────────────────────────────────────────
|
|
@@ -65,13 +107,13 @@ export class GuiEmbedElement extends HTMLElement {
|
|
|
65
107
|
if (cs.display === 'inline') this.style.display = 'block'
|
|
66
108
|
this.style.overflow = 'hidden'
|
|
67
109
|
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
// The dot grid is the
|
|
110
|
+
const stage = document.createElement('div')
|
|
111
|
+
stage.className = 'gui-embed-canvas'
|
|
112
|
+
// The dot grid is the STAGE, not the file — it signals "this is rendered in
|
|
71
113
|
// a dotgui surface" and stays behind the file. Toggle with `grid="false"`.
|
|
72
114
|
const dots = this.getAttribute('grid') !== 'false'
|
|
73
|
-
|
|
74
|
-
'position:absolute;inset:0',
|
|
115
|
+
stage.style.cssText = [
|
|
116
|
+
'position:absolute;inset:0;touch-action:none',
|
|
75
117
|
'background-color:var(--gui-embed-bg, #fafaf8)',
|
|
76
118
|
dots
|
|
77
119
|
? 'background-image:radial-gradient(var(--gui-embed-dot, rgba(0,0,0,.14)) 1px, transparent 1.4px);background-size:var(--gui-embed-dot-gap, 18px) var(--gui-embed-dot-gap, 18px);background-position:center'
|
|
@@ -79,17 +121,27 @@ export class GuiEmbedElement extends HTMLElement {
|
|
|
79
121
|
]
|
|
80
122
|
.filter(Boolean)
|
|
81
123
|
.join(';')
|
|
82
|
-
this.appendChild(
|
|
124
|
+
this.appendChild(stage)
|
|
125
|
+
this._stage = stage
|
|
126
|
+
|
|
127
|
+
const view = document.createElement('div')
|
|
128
|
+
// max-content: the canvas keeps its document size here, and the view scales it.
|
|
129
|
+
view.style.cssText = 'position:absolute;left:0;top:0;width:max-content;transform-origin:0 0'
|
|
130
|
+
stage.appendChild(view)
|
|
131
|
+
this._view = view
|
|
132
|
+
|
|
133
|
+
const canvas = document.createElement('gui-canvas') as GuiCanvas
|
|
134
|
+
canvas.addEventListener('render', (event) => this._rendered((event as CustomEvent<RenderDetail>).detail))
|
|
135
|
+
canvas.addEventListener('error', (event) => this._fail((event as CustomEvent<{ message: string }>).detail.message))
|
|
136
|
+
view.appendChild(canvas)
|
|
83
137
|
this._canvas = canvas
|
|
84
138
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
//
|
|
88
|
-
|
|
89
|
-
canvas.addEventListener('wheel', markUser, { passive: true })
|
|
90
|
-
canvas.addEventListener('pointerdown', markUser)
|
|
139
|
+
this._bindZoom()
|
|
140
|
+
|
|
141
|
+
// When the container resizes (responsive width, async aspect-ratio), refit —
|
|
142
|
+
// unless the user has taken over the viewport.
|
|
91
143
|
this._ro = new ResizeObserver(() => {
|
|
92
|
-
if (!this._userZoomed) this.
|
|
144
|
+
if (!this._userZoomed) this.fit()
|
|
93
145
|
})
|
|
94
146
|
this._ro.observe(this)
|
|
95
147
|
|
|
@@ -111,75 +163,191 @@ export class GuiEmbedElement extends HTMLElement {
|
|
|
111
163
|
// ── load + render ───────────────────────────────────────────────────────────
|
|
112
164
|
|
|
113
165
|
private async _load() {
|
|
166
|
+
this._size = null
|
|
167
|
+
this._userZoomed = false
|
|
168
|
+
this._canvas.mode = {}
|
|
114
169
|
try {
|
|
115
|
-
let decoded: Decoded
|
|
116
170
|
const src = this.getAttribute('src')
|
|
117
171
|
if (src) {
|
|
118
|
-
|
|
172
|
+
const response = await fetch(src)
|
|
173
|
+
if (!response.ok) throw new Error(`gui-embed: failed to fetch "${src}" (${response.status})`)
|
|
174
|
+
this._bytes = new Uint8Array(await response.arrayBuffer())
|
|
175
|
+
this._markup = ''
|
|
176
|
+
await this._canvas.loadBytes(this._bytes)
|
|
177
|
+
this._showPage(this.getAttribute('page'))
|
|
119
178
|
} else {
|
|
120
179
|
// Inline content lives in a <script type="application/gui"> child — raw
|
|
121
180
|
// text the HTML parser never mangles (so `<col>`/`<img>` survive).
|
|
122
181
|
const script = this.querySelector(':scope > script[type="application/gui"]')
|
|
123
|
-
if (!script)
|
|
124
|
-
|
|
182
|
+
if (!script) {
|
|
183
|
+
// The CDN script defines this element before the parser has reached
|
|
184
|
+
// its children: look again once the document is parsed.
|
|
185
|
+
if (document.readyState === 'loading') {
|
|
186
|
+
document.addEventListener('DOMContentLoaded', () => void this._load(), { once: true })
|
|
187
|
+
}
|
|
188
|
+
return
|
|
189
|
+
}
|
|
190
|
+
this._bytes = null
|
|
191
|
+
this._markup = script.textContent ?? ''
|
|
192
|
+
await this._canvas.loadMarkup(this._markup)
|
|
125
193
|
}
|
|
126
|
-
|
|
127
|
-
if (this._decoded) revokeAssets(this._decoded.assetMap)
|
|
128
|
-
this._decoded = decoded
|
|
129
|
-
this._userZoomed = false
|
|
130
|
-
|
|
131
|
-
this._modes = detectModes(decoded.xml)
|
|
132
|
-
this._mode = {}
|
|
133
|
-
for (const [axis, ax] of Object.entries(this._modes)) this._mode[axis] = ax.default
|
|
134
|
-
|
|
135
|
-
this._render()
|
|
136
194
|
this._buildControls()
|
|
137
195
|
} catch (err) {
|
|
138
|
-
this.
|
|
139
|
-
this._canvas.style.cssText +=
|
|
140
|
-
';display:flex;align-items:center;justify-content:center;font:13px system-ui;color:#c00;padding:12px;text-align:center'
|
|
196
|
+
this._fail(String((err as Error)?.message ?? err))
|
|
141
197
|
}
|
|
142
198
|
}
|
|
143
199
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
200
|
+
/**
|
|
201
|
+
* Covers the stage with a readable note. Opaque, so the last good render is
|
|
202
|
+
* not mistaken for this document; scrollable, for long reports. The styles
|
|
203
|
+
* are inline but plain (no `!important`), so a host can restyle
|
|
204
|
+
* `.gui-embed-error` and its parts.
|
|
205
|
+
*/
|
|
206
|
+
private _fail(message: string) {
|
|
207
|
+
let note = this.querySelector(':scope > .gui-embed-error') as HTMLDivElement | null
|
|
208
|
+
if (!note) {
|
|
209
|
+
note = document.createElement('div')
|
|
210
|
+
note.className = 'gui-embed-error'
|
|
211
|
+
note.setAttribute('role', 'alert')
|
|
212
|
+
note.style.cssText = [
|
|
213
|
+
'position:absolute;inset:0;z-index:1;overflow:auto;box-sizing:border-box',
|
|
214
|
+
'display:flex;padding:16px',
|
|
215
|
+
'background:var(--gui-embed-bg, #fafaf8)',
|
|
216
|
+
'font:13px/1.45 system-ui,sans-serif;color:#333',
|
|
217
|
+
].join(';')
|
|
218
|
+
this.appendChild(note)
|
|
219
|
+
}
|
|
220
|
+
const { lines, more } = summarizeError(message)
|
|
221
|
+
const body = document.createElement('div')
|
|
222
|
+
body.className = 'gui-embed-error-body'
|
|
223
|
+
// margin:auto centers a short note and still lets a long one scroll from the top.
|
|
224
|
+
body.style.cssText = 'margin:auto;max-width:56ch;text-align:left'
|
|
225
|
+
const title = document.createElement('div')
|
|
226
|
+
title.className = 'gui-embed-error-title'
|
|
227
|
+
title.style.cssText = 'font-weight:600;color:#c00;margin-bottom:6px'
|
|
228
|
+
title.textContent = "This design can't be drawn"
|
|
229
|
+
body.appendChild(title)
|
|
230
|
+
const list = document.createElement('ul')
|
|
231
|
+
list.className = 'gui-embed-error-list'
|
|
232
|
+
list.style.cssText = 'margin:0;padding-left:18px;overflow-wrap:anywhere'
|
|
233
|
+
for (const line of lines) {
|
|
234
|
+
const item = document.createElement('li')
|
|
235
|
+
item.textContent = line
|
|
236
|
+
list.appendChild(item)
|
|
237
|
+
}
|
|
238
|
+
if (more) {
|
|
239
|
+
const item = document.createElement('li')
|
|
240
|
+
item.className = 'gui-embed-error-more'
|
|
241
|
+
item.style.cssText = 'list-style:none;margin-left:-18px;color:#777'
|
|
242
|
+
item.textContent = `and ${more} more`
|
|
243
|
+
list.appendChild(item)
|
|
244
|
+
}
|
|
245
|
+
body.appendChild(list)
|
|
246
|
+
note.replaceChildren(body)
|
|
247
|
+
}
|
|
148
248
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
//
|
|
153
|
-
|
|
249
|
+
/** A frame arrived: size the host on the first, and keep the view fitted. */
|
|
250
|
+
private _rendered(detail: RenderDetail) {
|
|
251
|
+
this.querySelector(':scope > .gui-embed-error')?.remove()
|
|
252
|
+
// Another page brings its own modes: the controls follow it.
|
|
253
|
+
if (this._canvas.page !== this._controlsPage) this._buildControls()
|
|
254
|
+
const first = !this._size || this._size.width !== detail.width || this._size.height !== detail.height
|
|
255
|
+
this._size = { width: detail.width, height: detail.height }
|
|
256
|
+
if (!first) return
|
|
154
257
|
if (!this._hasHeight()) {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
this.style.aspectRatio = `${w} / ${h}`
|
|
163
|
-
} else {
|
|
164
|
-
// No CSS size at all → size to the file's native dimensions, like an SVG.
|
|
165
|
-
this.style.width = `${w}px`
|
|
166
|
-
this.style.height = `${h}px`
|
|
167
|
-
}
|
|
258
|
+
if (this.style.width) {
|
|
259
|
+
// Author fixed a width → derive height from the file's ratio.
|
|
260
|
+
this.style.aspectRatio = `${detail.width} / ${detail.height}`
|
|
261
|
+
} else {
|
|
262
|
+
// No CSS size at all → size to the file's native dimensions, like an SVG.
|
|
263
|
+
this.style.width = `${detail.width}px`
|
|
264
|
+
this.style.height = `${detail.height}px`
|
|
168
265
|
}
|
|
169
|
-
requestAnimationFrame(() => {
|
|
170
|
-
this._zoomCtl = render(xml, this._canvas, assetMap, { zoom: interactive, mode: this._mode })
|
|
171
|
-
})
|
|
172
|
-
return
|
|
173
266
|
}
|
|
174
|
-
|
|
175
|
-
this._zoomCtl = render(xml, this._canvas, assetMap, { zoom: interactive, mode: this._mode })
|
|
267
|
+
if (!this._userZoomed) this.fit()
|
|
176
268
|
}
|
|
177
269
|
|
|
178
|
-
/** Whether the
|
|
270
|
+
/** Whether the host already has a usable height (fixed by CSS or aspect-ratio). */
|
|
179
271
|
private _hasHeight(): boolean {
|
|
180
272
|
if (this.style.height || this.style.aspectRatio) return true
|
|
181
|
-
const
|
|
182
|
-
|
|
273
|
+
const cs = getComputedStyle(this)
|
|
274
|
+
if (cs.aspectRatio && cs.aspectRatio !== 'auto') return true
|
|
275
|
+
return this.clientHeight > 1
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// ── pan + zoom ──────────────────────────────────────────────────────────────
|
|
279
|
+
|
|
280
|
+
/** Places the document center-center, scaled to fit, and hands the view back to auto-fit. */
|
|
281
|
+
fit() {
|
|
282
|
+
if (!this._size) return
|
|
283
|
+
const { width, height } = this._size
|
|
284
|
+
const box = this._stage.getBoundingClientRect()
|
|
285
|
+
if (box.width < 1 || box.height < 1) return
|
|
286
|
+
this._scale = Math.min(box.width / width, box.height / height)
|
|
287
|
+
this._x = (box.width - width * this._scale) / 2
|
|
288
|
+
this._y = (box.height - height * this._scale) / 2
|
|
289
|
+
this._userZoomed = false
|
|
290
|
+
this._apply()
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** Zooms by `factor` about a point in the stage (its center by default). */
|
|
294
|
+
zoomBy(factor: number, anchorX?: number, anchorY?: number) {
|
|
295
|
+
if (!this._size) return
|
|
296
|
+
const box = this._stage.getBoundingClientRect()
|
|
297
|
+
const fitted = Math.min(box.width / this._size.width, box.height / this._size.height)
|
|
298
|
+
const next = Math.min(Math.max(this._scale * factor, fitted * MIN_ZOOM), fitted * MAX_ZOOM)
|
|
299
|
+
const ax = anchorX ?? box.width / 2
|
|
300
|
+
const ay = anchorY ?? box.height / 2
|
|
301
|
+
this._x = ax - ((ax - this._x) * next) / this._scale
|
|
302
|
+
this._y = ay - ((ay - this._y) * next) / this._scale
|
|
303
|
+
this._scale = next
|
|
304
|
+
this._userZoomed = true
|
|
305
|
+
this._apply()
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
private _apply() {
|
|
309
|
+
this._view.style.transform = `translate(${this._x}px, ${this._y}px) scale(${this._scale})`
|
|
310
|
+
// Draw sharp at the zoom the view settles on, not at every wheel tick.
|
|
311
|
+
if (this._settle) clearTimeout(this._settle)
|
|
312
|
+
this._settle = setTimeout(() => {
|
|
313
|
+
const density = Math.min(Math.max(2, Math.ceil((window.devicePixelRatio || 1) * this._scale)), 4)
|
|
314
|
+
if (Number(this._canvas.getAttribute('density')) !== density) this._canvas.setAttribute('density', String(density))
|
|
315
|
+
}, 150)
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
private _bindZoom() {
|
|
319
|
+
const stage = this._stage
|
|
320
|
+
const enabled = () => this.getAttribute('zoom') !== 'false'
|
|
321
|
+
stage.addEventListener(
|
|
322
|
+
'wheel',
|
|
323
|
+
(event) => {
|
|
324
|
+
// Pinch on a trackpad and ctrl/⌘ + wheel zoom; a plain wheel scrolls the page.
|
|
325
|
+
if (!enabled() || !(event.ctrlKey || event.metaKey)) return
|
|
326
|
+
event.preventDefault()
|
|
327
|
+
const box = stage.getBoundingClientRect()
|
|
328
|
+
this.zoomBy(Math.exp(-event.deltaY * 0.01), event.clientX - box.left, event.clientY - box.top)
|
|
329
|
+
},
|
|
330
|
+
{ passive: false },
|
|
331
|
+
)
|
|
332
|
+
let drag: { id: number; x: number; y: number } | null = null
|
|
333
|
+
stage.addEventListener('pointerdown', (event) => {
|
|
334
|
+
if (!enabled() || event.button !== 0) return
|
|
335
|
+
drag = { id: event.pointerId, x: event.clientX, y: event.clientY }
|
|
336
|
+
stage.setPointerCapture(event.pointerId)
|
|
337
|
+
})
|
|
338
|
+
stage.addEventListener('pointermove', (event) => {
|
|
339
|
+
if (!drag || drag.id !== event.pointerId) return
|
|
340
|
+
this._x += event.clientX - drag.x
|
|
341
|
+
this._y += event.clientY - drag.y
|
|
342
|
+
drag.x = event.clientX
|
|
343
|
+
drag.y = event.clientY
|
|
344
|
+
this._userZoomed = true
|
|
345
|
+
this._view.style.transform = `translate(${this._x}px, ${this._y}px) scale(${this._scale})`
|
|
346
|
+
})
|
|
347
|
+
const end = () => (drag = null)
|
|
348
|
+
stage.addEventListener('pointerup', end)
|
|
349
|
+
stage.addEventListener('pointercancel', end)
|
|
350
|
+
stage.addEventListener('dblclick', () => enabled() && this.fit())
|
|
183
351
|
}
|
|
184
352
|
|
|
185
353
|
// ── controls ────────────────────────────────────────────────────────────────
|
|
@@ -187,20 +355,27 @@ export class GuiEmbedElement extends HTMLElement {
|
|
|
187
355
|
private _buildControls() {
|
|
188
356
|
const bar = this._bar
|
|
189
357
|
bar.innerHTML = ''
|
|
358
|
+
this._controlsPage = this._canvas.page
|
|
190
359
|
|
|
191
360
|
if (this.getAttribute('zoom') !== 'false') {
|
|
192
|
-
bar.appendChild(this._btn('Fit', 'Reset zoom', () => this.
|
|
361
|
+
bar.appendChild(this._btn('Fit', 'Reset zoom', () => this.fit()))
|
|
193
362
|
}
|
|
194
363
|
|
|
195
|
-
const
|
|
364
|
+
const pages = this._canvas.pages
|
|
365
|
+
if (this.hasAttribute('page-switch') && pages.length > 1) {
|
|
366
|
+
bar.appendChild(this._pageSelect(pages))
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
const modes = this._canvas.modes
|
|
370
|
+
const axes = Object.keys(modes).filter((axis) => modes[axis].values.length > 1)
|
|
196
371
|
if (this.hasAttribute('mode-switch') && axes.length) {
|
|
197
|
-
for (const axis of axes) bar.appendChild(this._modeSelect(axis))
|
|
372
|
+
for (const axis of axes) bar.appendChild(this._modeSelect(axis, modes[axis]))
|
|
198
373
|
}
|
|
199
374
|
|
|
200
375
|
if (this.hasAttribute('copy')) {
|
|
201
376
|
bar.appendChild(
|
|
202
377
|
this._btn('Copy', 'Copy .gui markup', async () => {
|
|
203
|
-
|
|
378
|
+
await navigator.clipboard.writeText(this._canvas.source)
|
|
204
379
|
}),
|
|
205
380
|
)
|
|
206
381
|
}
|
|
@@ -217,41 +392,53 @@ export class GuiEmbedElement extends HTMLElement {
|
|
|
217
392
|
b.type = 'button'
|
|
218
393
|
b.textContent = label
|
|
219
394
|
b.title = title
|
|
220
|
-
b.style.cssText = [
|
|
221
|
-
'all:unset;cursor:pointer;font:600 12px/1 system-ui',
|
|
222
|
-
'color:#fff;padding:6px 9px;border-radius:5px',
|
|
223
|
-
].join(';')
|
|
395
|
+
b.style.cssText = ['all:unset;cursor:pointer;font:600 12px/1 system-ui', 'color:#fff;padding:6px 9px;border-radius:5px'].join(';')
|
|
224
396
|
b.addEventListener('pointerenter', () => (b.style.background = 'rgba(255,255,255,.18)'))
|
|
225
397
|
b.addEventListener('pointerleave', () => (b.style.background = 'transparent'))
|
|
226
398
|
b.addEventListener('click', onClick)
|
|
227
399
|
return b
|
|
228
400
|
}
|
|
229
401
|
|
|
230
|
-
private
|
|
402
|
+
private _select(title: string): HTMLSelectElement {
|
|
231
403
|
const sel = document.createElement('select')
|
|
232
|
-
sel.title =
|
|
233
|
-
sel.style.cssText = [
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
404
|
+
sel.title = title
|
|
405
|
+
sel.style.cssText = ['all:unset;cursor:pointer;font:600 12px/1 system-ui', 'color:#fff;padding:6px 9px;border-radius:5px'].join(';')
|
|
406
|
+
return sel
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
private _option(value: string, label: string, selected: boolean): HTMLOptionElement {
|
|
410
|
+
const opt = document.createElement('option')
|
|
411
|
+
opt.value = value
|
|
412
|
+
opt.textContent = label
|
|
413
|
+
opt.style.color = '#000'
|
|
414
|
+
if (selected) opt.selected = true
|
|
415
|
+
return opt
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
private _pageSelect(pages: GuiPage[]): HTMLSelectElement {
|
|
419
|
+
const sel = this._select('Page')
|
|
420
|
+
sel.className = 'gui-embed-pages'
|
|
421
|
+
const current = this._canvas.page
|
|
422
|
+
for (const page of pages) {
|
|
423
|
+
const label = page.library ? 'Library' : page.name.replace(/\.guix$/, '')
|
|
424
|
+
sel.appendChild(this._option(page.name, label, page.name === current))
|
|
244
425
|
}
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
426
|
+
// Through the attribute, so `page` always says what is on show.
|
|
427
|
+
sel.addEventListener('change', () => this.setAttribute('page', sel.value))
|
|
428
|
+
return sel
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
private _modeSelect(axis: string, mode: ModeAxis): HTMLSelectElement {
|
|
432
|
+
const sel = this._select(`Mode: ${axis}`)
|
|
433
|
+
const current = this._canvas.mode[axis] ?? mode.default
|
|
434
|
+
for (const value of mode.values) sel.appendChild(this._option(value, value, value === current))
|
|
435
|
+
// The renderer switches via tokens; the markup is unchanged.
|
|
436
|
+
sel.addEventListener('change', () => this._canvas.setMode(axis, sel.value))
|
|
249
437
|
return sel
|
|
250
438
|
}
|
|
251
439
|
|
|
252
440
|
private _download() {
|
|
253
|
-
|
|
254
|
-
const bytes = toGuiBytes(this._decoded.pkg)
|
|
441
|
+
const bytes = this._bytes ?? pack({ xml: this._markup, assets: {} })
|
|
255
442
|
const name = (this.getAttribute('src')?.split('/').pop() || 'design.gui').replace(/\.guix$/, '.gui')
|
|
256
443
|
const url = URL.createObjectURL(new Blob([bytes as unknown as BlobPart], { type: 'application/octet-stream' }))
|
|
257
444
|
const a = document.createElement('a')
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @dotgui/embed — render .gui files anywhere in the browser.
|
|
3
3
|
*
|
|
4
|
-
* A thin
|
|
4
|
+
* A thin player over `@dotgui/render`'s `<gui-canvas>`: the Rust renderer,
|
|
5
|
+
* compiled to WebAssembly. One element:
|
|
5
6
|
*
|
|
6
7
|
* <gui-embed src="x.gui"></gui-embed> external file
|
|
7
8
|
* <gui-embed> inline file (parser-safe)
|
|
@@ -14,15 +15,16 @@
|
|
|
14
15
|
* The CDN build (`gui-embed.js`) calls `install()` automatically. When importing
|
|
15
16
|
* as a module, call `install()` (or `defineGuiEmbed()`) yourself.
|
|
16
17
|
*/
|
|
17
|
-
|
|
18
|
-
export { fetchGui, decodeBytes, decodeMarkup, type Decoded } from './decode'
|
|
19
|
-
export { detectModes, switchableAxes, type ModeAxis } from './modes'
|
|
20
|
-
|
|
18
|
+
import '@dotgui/render/canvas'
|
|
21
19
|
import { defineGuiEmbed } from './gui-embed'
|
|
22
20
|
|
|
21
|
+
export { GuiEmbedElement, defineGuiEmbed } from './gui-embed'
|
|
22
|
+
export { GuiCanvas, config } from '@dotgui/render/canvas'
|
|
23
|
+
export type { ModeAxis, GuiPage, RenderDetail } from '@dotgui/render/canvas'
|
|
24
|
+
|
|
23
25
|
let installed = false
|
|
24
26
|
|
|
25
|
-
/** Register the `<gui-embed>` custom element. Idempotent. */
|
|
27
|
+
/** Register the `<gui-embed>` custom element (and `<gui-canvas>`). Idempotent. */
|
|
26
28
|
export function install() {
|
|
27
29
|
if (installed || typeof window === 'undefined') return
|
|
28
30
|
installed = true
|
package/src/decode.ts
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Decode .gui / .guix bytes into renderable input.
|
|
3
|
-
*
|
|
4
|
-
* The kit owns unzip + .guix/assets discovery (`unpack`); here we only turn the
|
|
5
|
-
* decoded asset bytes into browser `blob:` URLs the renderer can resolve. This
|
|
6
|
-
* mirrors the playground / gui-app decode path so rendering behaves identically.
|
|
7
|
-
*/
|
|
8
|
-
import { unpack, pack, type GuiPackage } from '@dotgui/kit/package'
|
|
9
|
-
|
|
10
|
-
export interface Decoded {
|
|
11
|
-
/** The .guix markup (XML, `<gui>` root). */
|
|
12
|
-
xml: string
|
|
13
|
-
/** assetMap keyed by package path, e.g. "assets/hero.webp" -> blob: URL. */
|
|
14
|
-
assetMap: Record<string, string>
|
|
15
|
-
/** The in-memory package (used for download). */
|
|
16
|
-
pkg: GuiPackage
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function mimeForExt(ext: string): string {
|
|
20
|
-
if (ext === 'svg') return 'image/svg+xml'
|
|
21
|
-
if (ext === 'jpg' || ext === 'jpeg') return 'image/jpeg'
|
|
22
|
-
return `image/${ext}`
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/** Decode raw .gui (ZIP) or .guix (XML) bytes into xml + a blob: asset map. */
|
|
26
|
-
export function decodeBytes(bytes: Uint8Array): Decoded {
|
|
27
|
-
const pkg = unpack(bytes)
|
|
28
|
-
const assetMap: Record<string, string> = {}
|
|
29
|
-
for (const [name, fileBytes] of Object.entries(pkg.assets)) {
|
|
30
|
-
const ext = name.split('.').pop() || 'png'
|
|
31
|
-
const blob = new Blob([fileBytes as unknown as BlobPart], { type: mimeForExt(ext) })
|
|
32
|
-
assetMap[name] = URL.createObjectURL(blob)
|
|
33
|
-
}
|
|
34
|
-
return { xml: pkg.xml, assetMap, pkg }
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/** Build a Decoded from inline .guix markup (no assets). */
|
|
38
|
-
export function decodeMarkup(xml: string): Decoded {
|
|
39
|
-
return { xml, assetMap: {}, pkg: { xml, assets: {} } }
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** Fetch a .gui / .guix from a URL and decode it. */
|
|
43
|
-
export async function fetchGui(src: string): Promise<Decoded> {
|
|
44
|
-
const res = await fetch(src)
|
|
45
|
-
if (!res.ok) throw new Error(`gui-embed: failed to fetch "${src}" (${res.status})`)
|
|
46
|
-
const buf = new Uint8Array(await res.arrayBuffer())
|
|
47
|
-
return decodeBytes(buf)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/** Re-pack a decoded package into .gui (ZIP) bytes for download. */
|
|
51
|
-
export function toGuiBytes(pkg: GuiPackage): Uint8Array {
|
|
52
|
-
return pack(pkg)
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/** Release any blob: URLs created for an asset map. */
|
|
56
|
-
export function revokeAssets(assetMap: Record<string, string>) {
|
|
57
|
-
for (const url of Object.values(assetMap)) {
|
|
58
|
-
if (url.startsWith('blob:')) URL.revokeObjectURL(url)
|
|
59
|
-
}
|
|
60
|
-
}
|
package/src/modes.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Mode detection — reuse the kit parser so the embed knows exactly which token
|
|
3
|
-
* mode axes (RFC-0037) a file declares. Drives the optional mode switcher: it
|
|
4
|
-
* only renders when an axis has more than one value.
|
|
5
|
-
*/
|
|
6
|
-
import { parseXml } from '@dotgui/kit/parser'
|
|
7
|
-
|
|
8
|
-
export interface ModeAxis {
|
|
9
|
-
values: string[]
|
|
10
|
-
default: string
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/** Declared mode axes for a .guix string, keyed by axis name. Empty when none. */
|
|
14
|
-
export function detectModes(xml: string): Record<string, ModeAxis> {
|
|
15
|
-
try {
|
|
16
|
-
const parsed = parseXml(xml)
|
|
17
|
-
return parsed?.modes ?? {}
|
|
18
|
-
} catch {
|
|
19
|
-
return {}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/** Axes worth showing a switcher for (more than one value). */
|
|
24
|
-
export function switchableAxes(modes: Record<string, ModeAxis>): string[] {
|
|
25
|
-
return Object.keys(modes).filter((axis) => modes[axis].values.length > 1)
|
|
26
|
-
}
|