@ironflock/widget-toolbar 0.0.0-stage → 1.0.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 +110 -2
- package/dist/widget-toolbar.js +1331 -0
- package/dist/widget-toolbar.js.map +1 -0
- package/package.json +53 -5
- package/src/colors.ts +102 -0
- package/src/default-data.json +45 -0
- package/src/definition-schema.d.ts +192 -0
- package/src/definition-schema.json +317 -0
- package/src/layout.ts +153 -0
- package/src/tools.ts +430 -0
- package/src/widget-toolbar.ts +1289 -0
- package/thumbnail.png +0 -0
- package/thumbnail.webp +0 -0
|
@@ -0,0 +1,1289 @@
|
|
|
1
|
+
import { html, css, nothing, LitElement, PropertyValues, TemplateResult } from 'lit'
|
|
2
|
+
import { customElement, property, query, state } from 'lit/decorators.js'
|
|
3
|
+
import { repeat } from 'lit/directives/repeat.js'
|
|
4
|
+
import { live } from 'lit/directives/live.js'
|
|
5
|
+
import { styleMap } from 'lit/directives/style-map.js'
|
|
6
|
+
import '@material/web/switch/switch.js'
|
|
7
|
+
import '@material/web/icon/icon.js'
|
|
8
|
+
import type { MdSwitch } from '@material/web/switch/switch.js'
|
|
9
|
+
import type { ToolbarConfiguration } from './definition-schema.js'
|
|
10
|
+
import { commitFor, paramsOf, reports, settled, toTools, type Commit, type Request, type ToolModel } from './tools.ts'
|
|
11
|
+
import { place, type Density, type Extent, type Item, type Placement } from './layout.ts'
|
|
12
|
+
import { accentColor, onColor, opaqueSurface } from './colors.ts'
|
|
13
|
+
|
|
14
|
+
type Theme = { theme_name: string; theme_object?: any }
|
|
15
|
+
|
|
16
|
+
type Popup = { kind: 'menu' } | { kind: 'dropdown'; key: string }
|
|
17
|
+
|
|
18
|
+
// How long a tool keeps the value an operator set before it shows what its
|
|
19
|
+
// source reports again: a bound value gets time for the row to travel to the
|
|
20
|
+
// table and back, a URL parameter only for Studio to hand over the new route
|
|
21
|
+
// (nothing hands one over in the widget editor, where the tool falls back).
|
|
22
|
+
const DATA_HOLD_MS = 10000
|
|
23
|
+
const URL_HOLD_MS = 2000
|
|
24
|
+
|
|
25
|
+
const ORIENTATIONS = ['horizontal', 'vertical'] as const
|
|
26
|
+
const SIZES = ['small', 'normal', 'big'] as const
|
|
27
|
+
const SHAPES = ['round', 'rounded', 'square'] as const
|
|
28
|
+
const ALIGNMENTS = { start: 'flex-start', center: 'center', end: 'flex-end' } as const
|
|
29
|
+
|
|
30
|
+
const oneOf = <T extends string>(value: unknown, allowed: readonly T[], fallback: T): T =>
|
|
31
|
+
allowed.includes(value as T) ? (value as T) : fallback
|
|
32
|
+
|
|
33
|
+
// Formatted in the viewer's locale; one formatter per number of decimals.
|
|
34
|
+
const formatters = new Map<number, Intl.NumberFormat>()
|
|
35
|
+
const formatNumber = (value: number, decimals: number): string => {
|
|
36
|
+
let formatter = formatters.get(decimals)
|
|
37
|
+
if (!formatter) {
|
|
38
|
+
formatter = new Intl.NumberFormat(undefined, {
|
|
39
|
+
minimumFractionDigits: decimals,
|
|
40
|
+
maximumFractionDigits: decimals
|
|
41
|
+
})
|
|
42
|
+
formatters.set(decimals, formatter)
|
|
43
|
+
}
|
|
44
|
+
return formatter.format(value)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** What decides the tools' sizes: a placement measured for another signature is stale. */
|
|
48
|
+
const layoutSignature = (tools: ToolModel[], orientation: string, size: string): string =>
|
|
49
|
+
JSON.stringify([
|
|
50
|
+
orientation,
|
|
51
|
+
size,
|
|
52
|
+
tools.map((t) => [
|
|
53
|
+
t.control,
|
|
54
|
+
t.label,
|
|
55
|
+
t.buttonValue,
|
|
56
|
+
t.icon,
|
|
57
|
+
t.variant,
|
|
58
|
+
t.unit,
|
|
59
|
+
t.min,
|
|
60
|
+
t.max,
|
|
61
|
+
t.decimals,
|
|
62
|
+
t.options.map((o) => [o.label, o.icon])
|
|
63
|
+
])
|
|
64
|
+
])
|
|
65
|
+
|
|
66
|
+
@customElement('widget-toolbar-versionplaceholder')
|
|
67
|
+
export class WidgetToolbar extends LitElement {
|
|
68
|
+
/** This tile's config, with every data binding already replaced by its data. */
|
|
69
|
+
@property({ type: Object }) inputData?: ToolbarConfiguration
|
|
70
|
+
|
|
71
|
+
/** The board's ECharts theme. theme_object is missing on boards without a theme. */
|
|
72
|
+
@property({ type: Object }) theme?: Theme
|
|
73
|
+
|
|
74
|
+
/** The page path relative to the board, with its ?query. The URL parameters are read from it. */
|
|
75
|
+
@property({ type: String }) route?: string
|
|
76
|
+
|
|
77
|
+
@state() private placement?: { placement: Placement; signature: string }
|
|
78
|
+
@state() private popup?: Popup
|
|
79
|
+
@state() private confirm?: { message: string; accent: string; onAccent: string }
|
|
80
|
+
@state() private dragging?: { key: string; value: number }
|
|
81
|
+
@state() private requestsVersion = 0
|
|
82
|
+
|
|
83
|
+
@query('.bar') private bar?: HTMLElement
|
|
84
|
+
@query('.ruler') private ruler?: HTMLElement
|
|
85
|
+
@query('.popup') private popupEl?: HTMLElement
|
|
86
|
+
@query('dialog.confirm') private dialog?: HTMLDialogElement
|
|
87
|
+
|
|
88
|
+
version: string = 'versionplaceholder'
|
|
89
|
+
|
|
90
|
+
private tools: ToolModel[] = []
|
|
91
|
+
private signature = ''
|
|
92
|
+
private measuredSignature?: string
|
|
93
|
+
// What operators set, by tool key, until the tool's source agrees.
|
|
94
|
+
private requests = new Map<string, Request>()
|
|
95
|
+
private confirmAction?: () => void
|
|
96
|
+
|
|
97
|
+
private observer = new ResizeObserver(() => this.scheduleMeasure())
|
|
98
|
+
private observed: Element[] = []
|
|
99
|
+
private measureFrame = 0
|
|
100
|
+
|
|
101
|
+
private popupAnchor?: HTMLElement
|
|
102
|
+
private popupFrame = 0
|
|
103
|
+
private popupRect = ''
|
|
104
|
+
|
|
105
|
+
// Colours resolved at render (see resolveColors()).
|
|
106
|
+
private surface = '#ffffff'
|
|
107
|
+
private accentDefault = '#5470c6'
|
|
108
|
+
|
|
109
|
+
connectedCallback(): void {
|
|
110
|
+
super.connectedCallback()
|
|
111
|
+
if (!this.hasUpdated) return
|
|
112
|
+
// Studio moves a tile by detaching and re-attaching it in one task.
|
|
113
|
+
this.observeLayout(true)
|
|
114
|
+
// A modal <dialog> leaves the top layer when it is detached but keeps
|
|
115
|
+
// its open attribute: close it and show it again (as widget-form does).
|
|
116
|
+
// The close event this queues finds the dialog open again and is ignored.
|
|
117
|
+
const dialog = this.dialog
|
|
118
|
+
if (this.confirm && dialog?.open) {
|
|
119
|
+
dialog.close()
|
|
120
|
+
this.showDialog()
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
disconnectedCallback(): void {
|
|
125
|
+
super.disconnectedCallback()
|
|
126
|
+
this.observer.disconnect()
|
|
127
|
+
this.observed = []
|
|
128
|
+
cancelAnimationFrame(this.measureFrame)
|
|
129
|
+
this.measureFrame = 0
|
|
130
|
+
// A menu is fleeting; it does not survive a move.
|
|
131
|
+
this.closePopup()
|
|
132
|
+
// Held requests with a timer would fire into a detached element.
|
|
133
|
+
for (const [key, request] of this.requests) {
|
|
134
|
+
if (request.timer === undefined) continue
|
|
135
|
+
clearTimeout(request.timer)
|
|
136
|
+
this.requests.delete(key)
|
|
137
|
+
}
|
|
138
|
+
// A move re-attaches within this task; anything else ends the question.
|
|
139
|
+
setTimeout(() => {
|
|
140
|
+
if (!this.isConnected && this.confirm) this.endConfirm(false)
|
|
141
|
+
})
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
protected willUpdate(changed: PropertyValues): void {
|
|
145
|
+
if (changed.has('inputData') || changed.has('route') || changed.has('requestsVersion')) {
|
|
146
|
+
this.settleRequests()
|
|
147
|
+
}
|
|
148
|
+
this.tools = toTools(this.inputData, this.route, this.requests)
|
|
149
|
+
this.signature = layoutSignature(this.tools, this.orientation, this.size)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
protected updated(): void {
|
|
153
|
+
// md-switch binds its checkbox's checked *attribute*, which a browser
|
|
154
|
+
// stops honouring once the user has clicked it. Without this, a switch
|
|
155
|
+
// moved by a report, a timeout, a cancelled confirmation or the URL
|
|
156
|
+
// keeps its checkbox on the old position, and the next click toggles
|
|
157
|
+
// from there. (widget-switch has the same fix.)
|
|
158
|
+
this.shadowRoot?.querySelectorAll('md-switch').forEach((sw) => {
|
|
159
|
+
const input = sw.shadowRoot?.querySelector('input')
|
|
160
|
+
if (input && input.checked !== sw.selected) input.checked = sw.selected
|
|
161
|
+
})
|
|
162
|
+
this.observeLayout(false)
|
|
163
|
+
if (this.signature !== this.measuredSignature) this.scheduleMeasure()
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
private get orientation() {
|
|
167
|
+
return oneOf(this.inputData?.orientation, ORIENTATIONS, 'horizontal')
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
private get size() {
|
|
171
|
+
return oneOf(this.inputData?.size, SIZES, 'normal')
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
private get shape() {
|
|
175
|
+
return oneOf(this.inputData?.shape, SHAPES, 'round')
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// ---------------------------------------------------------------- state --
|
|
179
|
+
|
|
180
|
+
/** Drop the held values whose source now agrees, or whose tool is gone. */
|
|
181
|
+
private settleRequests() {
|
|
182
|
+
if (!this.requests.size) return
|
|
183
|
+
const current = new Map(reports(this.inputData, paramsOf(this.route)).map((r) => [r.key, r]))
|
|
184
|
+
for (const [key, request] of this.requests) {
|
|
185
|
+
const report = current.get(key)
|
|
186
|
+
if (!report || settled(report, request)) {
|
|
187
|
+
clearTimeout(request.timer)
|
|
188
|
+
this.requests.delete(key)
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** An operator moved a tool to `next`. Writes it, after the confirmation if one is set. */
|
|
194
|
+
private interact(model: ToolModel, next?: unknown) {
|
|
195
|
+
const commit = commitFor(this.inputData, model, next)
|
|
196
|
+
if (!commit) {
|
|
197
|
+
// Nothing to write (a key column lost its value): put the tool back.
|
|
198
|
+
this.requestUpdate()
|
|
199
|
+
return
|
|
200
|
+
}
|
|
201
|
+
if (model.confirmText) {
|
|
202
|
+
this.closePopup()
|
|
203
|
+
const { accent, onAccent } = this.toolColors(model)
|
|
204
|
+
this.confirmAction = () => this.apply(model, commit)
|
|
205
|
+
this.confirm = { message: model.confirmText, accent, onAccent }
|
|
206
|
+
this.updateComplete.then(() => this.showDialog())
|
|
207
|
+
return
|
|
208
|
+
}
|
|
209
|
+
this.apply(model, commit)
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
private apply(model: ToolModel, commit: Commit) {
|
|
213
|
+
const { key } = model
|
|
214
|
+
clearTimeout(this.requests.get(key)?.timer)
|
|
215
|
+
this.requests.delete(key)
|
|
216
|
+
// A button with nothing to report back has no state to hold.
|
|
217
|
+
if (!(model.control === 'button' && model.source === 'local')) {
|
|
218
|
+
const hold = model.source === 'url' ? URL_HOLD_MS : model.source === 'data' ? DATA_HOLD_MS : undefined
|
|
219
|
+
const timer =
|
|
220
|
+
hold === undefined
|
|
221
|
+
? undefined
|
|
222
|
+
: setTimeout(() => {
|
|
223
|
+
this.requests.delete(key)
|
|
224
|
+
this.requestsVersion++
|
|
225
|
+
}, hold)
|
|
226
|
+
this.requests.set(key, { expected: commit.expected, timer })
|
|
227
|
+
}
|
|
228
|
+
this.requestsVersion++
|
|
229
|
+
|
|
230
|
+
// Studio writes the row into the app's table, grouped per table, with
|
|
231
|
+
// tsp; the stored value comes back through the tool's value binding.
|
|
232
|
+
if (commit.items) {
|
|
233
|
+
this.dispatchEvent(new CustomEvent('data-submit', { detail: commit.items, bubbles: false, composed: false }))
|
|
234
|
+
}
|
|
235
|
+
// Studio merges the parameters into the page URL (undefined removes
|
|
236
|
+
// one) and hands the new route back, which is where the tool reads it.
|
|
237
|
+
if (commit.params) {
|
|
238
|
+
this.dispatchEvent(
|
|
239
|
+
new CustomEvent('param-submit', { detail: { params: commit.params }, bubbles: true, composed: true })
|
|
240
|
+
)
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// ------------------------------------------------------------ confirm --
|
|
245
|
+
|
|
246
|
+
private showDialog() {
|
|
247
|
+
const dialog = this.dialog
|
|
248
|
+
if (!dialog || !this.isConnected || dialog.open || !this.confirm) return
|
|
249
|
+
dialog.returnValue = ''
|
|
250
|
+
try {
|
|
251
|
+
dialog.showModal()
|
|
252
|
+
} catch {
|
|
253
|
+
this.endConfirm(false)
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
private onDialogClose() {
|
|
258
|
+
const dialog = this.dialog
|
|
259
|
+
// Open again: the close of a re-show after a tile move.
|
|
260
|
+
if (!dialog || dialog.open || !this.confirm) return
|
|
261
|
+
this.endConfirm(dialog.returnValue === 'ok')
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
private endConfirm(ok: boolean) {
|
|
265
|
+
const action = this.confirmAction
|
|
266
|
+
this.confirmAction = undefined
|
|
267
|
+
this.confirm = undefined
|
|
268
|
+
if (this.dialog?.open) this.dialog.close()
|
|
269
|
+
if (ok) action?.()
|
|
270
|
+
// A cancelled switch or slider goes back to where it was.
|
|
271
|
+
else this.requestUpdate()
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// ------------------------------------------------------------- layout --
|
|
275
|
+
|
|
276
|
+
/** Observe the host and every ruler slot; re-observe when the slots changed. */
|
|
277
|
+
private observeLayout(force: boolean) {
|
|
278
|
+
const slots = this.ruler ? [...this.ruler.children] : []
|
|
279
|
+
if (!force && slots.length === this.observed.length && slots.every((s, i) => s === this.observed[i])) return
|
|
280
|
+
this.observer.disconnect()
|
|
281
|
+
this.observer.observe(this)
|
|
282
|
+
slots.forEach((slot) => this.observer.observe(slot))
|
|
283
|
+
this.observed = slots
|
|
284
|
+
this.scheduleMeasure()
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// A ResizeObserver callback that changes what it observes would loop:
|
|
288
|
+
// measure in the next frame instead.
|
|
289
|
+
private scheduleMeasure() {
|
|
290
|
+
if (this.measureFrame || !this.isConnected) return
|
|
291
|
+
this.measureFrame = requestAnimationFrame(() => {
|
|
292
|
+
this.measureFrame = 0
|
|
293
|
+
this.measure()
|
|
294
|
+
})
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
private measure() {
|
|
298
|
+
const bar = this.bar
|
|
299
|
+
const ruler = this.ruler
|
|
300
|
+
if (!bar || !ruler || !this.isConnected) return
|
|
301
|
+
const box = bar.getBoundingClientRect()
|
|
302
|
+
// A hidden page (display: none) or a collapsed tile measures 0: keep
|
|
303
|
+
// what was placed, and measure again once it has a size.
|
|
304
|
+
if (box.width === 0 || box.height === 0) return
|
|
305
|
+
const slots = [...ruler.children]
|
|
306
|
+
const tools = this.tools
|
|
307
|
+
if (slots.length !== tools.length * 2 + 1) return
|
|
308
|
+
|
|
309
|
+
const vertical = this.orientation === 'vertical'
|
|
310
|
+
const extent = (el: Element): Extent => {
|
|
311
|
+
const r = el.getBoundingClientRect()
|
|
312
|
+
return vertical ? { main: r.height, cross: r.width } : { main: r.width, cross: r.height }
|
|
313
|
+
}
|
|
314
|
+
const items: Item[] = tools.map((tool, i) => ({
|
|
315
|
+
kind: tool.control === 'separator' ? 'separator' : tool.control === 'spacer' ? 'spacer' : 'tool',
|
|
316
|
+
full: extent(slots[2 * i]),
|
|
317
|
+
compact: extent(slots[2 * i + 1])
|
|
318
|
+
}))
|
|
319
|
+
// Not laid out yet: wait for the ruler's own resize.
|
|
320
|
+
if (items.some((item) => item.kind === 'tool' && item.full.main === 0)) return
|
|
321
|
+
|
|
322
|
+
const style = getComputedStyle(bar)
|
|
323
|
+
const px = (value: string) => parseFloat(value) || 0
|
|
324
|
+
const width = box.width - px(style.paddingLeft) - px(style.paddingRight)
|
|
325
|
+
const height = box.height - px(style.paddingTop) - px(style.paddingBottom)
|
|
326
|
+
const placement = place({
|
|
327
|
+
items,
|
|
328
|
+
available: vertical ? { main: height, cross: width } : { main: width, cross: height },
|
|
329
|
+
gap: px(vertical ? style.rowGap : style.columnGap),
|
|
330
|
+
more: extent(slots[slots.length - 1]),
|
|
331
|
+
checkCross: vertical
|
|
332
|
+
})
|
|
333
|
+
this.measuredSignature = this.signature
|
|
334
|
+
const next = { placement, signature: this.signature }
|
|
335
|
+
if (JSON.stringify(next) !== JSON.stringify(this.placement)) this.placement = next
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// -------------------------------------------------------------- popup --
|
|
339
|
+
|
|
340
|
+
private async togglePopup(popup: Popup, anchor: HTMLElement) {
|
|
341
|
+
const current = this.popup
|
|
342
|
+
const same =
|
|
343
|
+
current?.kind === popup.kind &&
|
|
344
|
+
(popup.kind === 'menu' || (current.kind === 'dropdown' && current.key === popup.key))
|
|
345
|
+
if (current) this.closePopup()
|
|
346
|
+
if (same) return
|
|
347
|
+
this.popupAnchor = anchor
|
|
348
|
+
this.popup = popup
|
|
349
|
+
await this.updateComplete
|
|
350
|
+
const el = this.popupEl
|
|
351
|
+
if (!el || !this.isConnected || this.popup !== popup) return
|
|
352
|
+
try {
|
|
353
|
+
if (!el.matches(':popover-open')) el.showPopover()
|
|
354
|
+
} catch {
|
|
355
|
+
this.closePopup()
|
|
356
|
+
return
|
|
357
|
+
}
|
|
358
|
+
this.popupRect = ''
|
|
359
|
+
this.trackPopup()
|
|
360
|
+
window.addEventListener('pointerdown', this.onOutsidePointer, true)
|
|
361
|
+
window.addEventListener('keydown', this.onPopupKey, true)
|
|
362
|
+
const first =
|
|
363
|
+
el.querySelector<HTMLElement>('button[data-current]') ??
|
|
364
|
+
el.querySelector<HTMLElement>('button:not(:disabled), input:not(:disabled), md-switch:not([disabled])')
|
|
365
|
+
first?.focus({ preventScroll: true })
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
private closePopup() {
|
|
369
|
+
cancelAnimationFrame(this.popupFrame)
|
|
370
|
+
this.popupFrame = 0
|
|
371
|
+
window.removeEventListener('pointerdown', this.onOutsidePointer, true)
|
|
372
|
+
window.removeEventListener('keydown', this.onPopupKey, true)
|
|
373
|
+
try {
|
|
374
|
+
if (this.popupEl?.matches(':popover-open')) this.popupEl.hidePopover()
|
|
375
|
+
} catch {
|
|
376
|
+
// Already gone with the element.
|
|
377
|
+
}
|
|
378
|
+
this.popup = undefined
|
|
379
|
+
this.popupAnchor = undefined
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
private onOutsidePointer = (event: Event) => {
|
|
383
|
+
const path = event.composedPath()
|
|
384
|
+
if ((this.popupEl && path.includes(this.popupEl)) || (this.popupAnchor && path.includes(this.popupAnchor))) return
|
|
385
|
+
this.closePopup()
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
private onPopupKey = (event: KeyboardEvent) => {
|
|
389
|
+
if (event.key !== 'Escape') return
|
|
390
|
+
const anchor = this.popupAnchor
|
|
391
|
+
this.closePopup()
|
|
392
|
+
anchor?.focus({ preventScroll: true })
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Keep the open popup next to its trigger, frame by frame: the board
|
|
397
|
+
* scrolls inside its own element, whose scroll events never reach this
|
|
398
|
+
* one. A trigger that collapsed (a hidden page) or left the page (the
|
|
399
|
+
* layout moved it into the menu) closes the popup.
|
|
400
|
+
*/
|
|
401
|
+
private trackPopup = () => {
|
|
402
|
+
const anchor = this.popupAnchor
|
|
403
|
+
const el = this.popupEl
|
|
404
|
+
if (!anchor || !el || !this.popup) return
|
|
405
|
+
const rect = anchor.getBoundingClientRect()
|
|
406
|
+
if (!anchor.isConnected || (rect.width === 0 && rect.height === 0)) {
|
|
407
|
+
this.closePopup()
|
|
408
|
+
return
|
|
409
|
+
}
|
|
410
|
+
const key = [rect.left, rect.top, rect.width, rect.height, innerWidth, innerHeight].join()
|
|
411
|
+
if (key !== this.popupRect) {
|
|
412
|
+
this.popupRect = key
|
|
413
|
+
this.positionPopup(el, rect)
|
|
414
|
+
}
|
|
415
|
+
this.popupFrame = requestAnimationFrame(this.trackPopup)
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
private positionPopup(el: HTMLElement, anchor: DOMRect) {
|
|
419
|
+
const margin = 4
|
|
420
|
+
el.style.minWidth = this.popup?.kind === 'dropdown' ? `${anchor.width}px` : ''
|
|
421
|
+
el.style.maxHeight = `${Math.max(0, innerHeight - 2 * margin)}px`
|
|
422
|
+
const width = el.offsetWidth
|
|
423
|
+
const height = el.offsetHeight
|
|
424
|
+
let top = anchor.bottom + margin
|
|
425
|
+
if (top + height > innerHeight - margin && anchor.top - margin - height >= margin) {
|
|
426
|
+
top = anchor.top - margin - height
|
|
427
|
+
}
|
|
428
|
+
top = Math.min(Math.max(top, margin), Math.max(margin, innerHeight - margin - height))
|
|
429
|
+
// The menu hangs from the ⋮ button's end, a dropdown list from its start.
|
|
430
|
+
let left = this.popup?.kind === 'menu' ? anchor.right - width : anchor.left
|
|
431
|
+
left = Math.min(Math.max(left, margin), Math.max(margin, innerWidth - margin - width))
|
|
432
|
+
el.style.top = `${top}px`
|
|
433
|
+
el.style.left = `${left}px`
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// -------------------------------------------------------------- colours --
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* The colours that have to be real colours, resolved at render and never
|
|
440
|
+
* cached across renders: the opaque surface of the menu and the dialog
|
|
441
|
+
* (the tile is usually translucent), and the accent with the text colour
|
|
442
|
+
* on it. Text itself uses var() chains, which follow a board style edit
|
|
443
|
+
* without a render.
|
|
444
|
+
*/
|
|
445
|
+
private resolveColors() {
|
|
446
|
+
const style = getComputedStyle(this)
|
|
447
|
+
const tile = style.getPropertyValue('--re-tile-background-color').trim()
|
|
448
|
+
const board = style.getPropertyValue('--re-background-color').trim()
|
|
449
|
+
const themeBg = String(this.theme?.theme_object?.backgroundColor ?? '')
|
|
450
|
+
this.surface = opaqueSurface(board ? [board, tile || themeBg] : [themeBg, tile])
|
|
451
|
+
const text =
|
|
452
|
+
style.getPropertyValue('--re-text-color').trim() ||
|
|
453
|
+
this.theme?.theme_object?.title?.textStyle?.color ||
|
|
454
|
+
style.color ||
|
|
455
|
+
'#333333'
|
|
456
|
+
this.accentDefault = accentColor(
|
|
457
|
+
this.inputData?.accentColor,
|
|
458
|
+
this.theme?.theme_object?.color?.[0],
|
|
459
|
+
this.surface,
|
|
460
|
+
text
|
|
461
|
+
)
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
private toolColors(tool: ToolModel) {
|
|
465
|
+
const accent = accentColor(tool.color, undefined, this.surface, this.accentDefault)
|
|
466
|
+
return { accent, onAccent: onColor(accent, this.surface) }
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
private toolStyle(tool: ToolModel) {
|
|
470
|
+
const { accent, onAccent } = this.toolColors(tool)
|
|
471
|
+
return styleMap({ '--accent': accent, '--on-accent': onAccent })
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
// -------------------------------------------------------------- render --
|
|
475
|
+
|
|
476
|
+
static styles = css`
|
|
477
|
+
:host {
|
|
478
|
+
display: block;
|
|
479
|
+
box-sizing: border-box;
|
|
480
|
+
/* clip, not hidden: the ruler is taller than the tile, and a
|
|
481
|
+
hidden overflow can still be scrolled (focus, scrollIntoView),
|
|
482
|
+
which shifted the bar out of the tile. */
|
|
483
|
+
overflow: clip;
|
|
484
|
+
position: relative;
|
|
485
|
+
font-family: Roboto, 'Helvetica Neue', Arial, sans-serif;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
.root {
|
|
489
|
+
height: 100%;
|
|
490
|
+
width: 100%;
|
|
491
|
+
color: var(--text);
|
|
492
|
+
--h: 36px;
|
|
493
|
+
--font: 14px;
|
|
494
|
+
--icon: 20px;
|
|
495
|
+
--gap: 12px;
|
|
496
|
+
--pad: 6px;
|
|
497
|
+
--slider: 140px;
|
|
498
|
+
--slider-compact: 88px;
|
|
499
|
+
--switch-scale: 0.8;
|
|
500
|
+
/* Corner radius as a share of the control's height, so a shape
|
|
501
|
+
holds at every size: 0.5 makes pill-shaped ends. */
|
|
502
|
+
--round: 0.5;
|
|
503
|
+
}
|
|
504
|
+
.root[data-shape='rounded'] {
|
|
505
|
+
--round: 0.2;
|
|
506
|
+
}
|
|
507
|
+
.root[data-shape='square'] {
|
|
508
|
+
--round: 0;
|
|
509
|
+
}
|
|
510
|
+
.root[data-size='small'] {
|
|
511
|
+
--h: 28px;
|
|
512
|
+
--font: 12px;
|
|
513
|
+
--icon: 18px;
|
|
514
|
+
--gap: 8px;
|
|
515
|
+
--pad: 4px;
|
|
516
|
+
--slider: 110px;
|
|
517
|
+
--slider-compact: 72px;
|
|
518
|
+
--switch-scale: 0.65;
|
|
519
|
+
}
|
|
520
|
+
.root[data-size='big'] {
|
|
521
|
+
--h: 48px;
|
|
522
|
+
--font: 18px;
|
|
523
|
+
--icon: 26px;
|
|
524
|
+
--gap: 16px;
|
|
525
|
+
--pad: 8px;
|
|
526
|
+
--slider: 180px;
|
|
527
|
+
--slider-compact: 110px;
|
|
528
|
+
--switch-scale: 1.1;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
.bar {
|
|
532
|
+
box-sizing: border-box;
|
|
533
|
+
height: 100%;
|
|
534
|
+
width: 100%;
|
|
535
|
+
display: flex;
|
|
536
|
+
align-items: center;
|
|
537
|
+
gap: var(--gap);
|
|
538
|
+
padding: var(--pad) calc(var(--pad) * 2);
|
|
539
|
+
overflow: clip;
|
|
540
|
+
}
|
|
541
|
+
.root[data-orientation='vertical'] .bar {
|
|
542
|
+
flex-direction: column;
|
|
543
|
+
align-items: stretch;
|
|
544
|
+
padding: calc(var(--pad) * 2) var(--pad);
|
|
545
|
+
}
|
|
546
|
+
.bar > * {
|
|
547
|
+
flex: none;
|
|
548
|
+
}
|
|
549
|
+
.bar > .spacer {
|
|
550
|
+
flex: 1 1 0;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/* A copy of every tool at both densities, measured, never seen. */
|
|
554
|
+
.ruler {
|
|
555
|
+
position: absolute;
|
|
556
|
+
top: 0;
|
|
557
|
+
left: 0;
|
|
558
|
+
width: max-content;
|
|
559
|
+
display: flex;
|
|
560
|
+
flex-direction: column;
|
|
561
|
+
align-items: flex-start;
|
|
562
|
+
visibility: hidden;
|
|
563
|
+
pointer-events: none;
|
|
564
|
+
z-index: -1;
|
|
565
|
+
}
|
|
566
|
+
.slot {
|
|
567
|
+
display: flex;
|
|
568
|
+
width: max-content;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
md-icon {
|
|
572
|
+
--md-icon-size: var(--icon);
|
|
573
|
+
flex: none;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
.text {
|
|
577
|
+
overflow: hidden;
|
|
578
|
+
text-overflow: ellipsis;
|
|
579
|
+
white-space: nowrap;
|
|
580
|
+
min-width: 0;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
button {
|
|
584
|
+
font: inherit;
|
|
585
|
+
color: inherit;
|
|
586
|
+
}
|
|
587
|
+
button:focus-visible,
|
|
588
|
+
input:focus-visible {
|
|
589
|
+
outline: 2px solid var(--accent, currentColor);
|
|
590
|
+
outline-offset: 2px;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/* Buttons, and the dropdown trigger and ⋮, which look like them. */
|
|
594
|
+
.button {
|
|
595
|
+
box-sizing: border-box;
|
|
596
|
+
display: inline-flex;
|
|
597
|
+
align-items: center;
|
|
598
|
+
justify-content: center;
|
|
599
|
+
gap: 0.4em;
|
|
600
|
+
height: var(--h);
|
|
601
|
+
min-width: var(--h);
|
|
602
|
+
padding: 0 calc(var(--h) * 0.42);
|
|
603
|
+
border: 1px solid transparent;
|
|
604
|
+
border-radius: calc(var(--h) * var(--round));
|
|
605
|
+
font-size: var(--font);
|
|
606
|
+
font-weight: 500;
|
|
607
|
+
line-height: 1;
|
|
608
|
+
white-space: nowrap;
|
|
609
|
+
cursor: pointer;
|
|
610
|
+
background: transparent;
|
|
611
|
+
}
|
|
612
|
+
.button.icon-only {
|
|
613
|
+
padding: 0;
|
|
614
|
+
width: var(--h);
|
|
615
|
+
}
|
|
616
|
+
.button[data-variant='filled'] {
|
|
617
|
+
background-color: var(--accent);
|
|
618
|
+
color: var(--on-accent);
|
|
619
|
+
}
|
|
620
|
+
.button[data-variant='tonal'] {
|
|
621
|
+
background-color: color-mix(in srgb, var(--accent) 22%, transparent);
|
|
622
|
+
}
|
|
623
|
+
.button[data-variant='outlined'] {
|
|
624
|
+
border-color: color-mix(in srgb, currentColor 45%, transparent);
|
|
625
|
+
}
|
|
626
|
+
.button[data-variant='text'] {
|
|
627
|
+
color: var(--accent);
|
|
628
|
+
}
|
|
629
|
+
.button:hover:not(:disabled),
|
|
630
|
+
.segment:hover:not(:disabled),
|
|
631
|
+
.menu-item:hover:not(:disabled) {
|
|
632
|
+
background-image: linear-gradient(
|
|
633
|
+
color-mix(in srgb, currentColor 10%, transparent),
|
|
634
|
+
color-mix(in srgb, currentColor 10%, transparent)
|
|
635
|
+
);
|
|
636
|
+
}
|
|
637
|
+
/* A button whose value is the current one, as in a mode selector. */
|
|
638
|
+
.button[data-active] {
|
|
639
|
+
box-shadow: inset 0 0 0 2px var(--accent);
|
|
640
|
+
}
|
|
641
|
+
.button[data-active]:not([data-variant='filled']) {
|
|
642
|
+
background-color: color-mix(in srgb, var(--accent) 28%, transparent);
|
|
643
|
+
}
|
|
644
|
+
.button[data-active][data-variant='filled'] {
|
|
645
|
+
box-shadow:
|
|
646
|
+
inset 0 0 0 2px var(--accent),
|
|
647
|
+
inset 0 0 0 4px var(--on-accent);
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
button:disabled,
|
|
651
|
+
input:disabled,
|
|
652
|
+
[data-disabled] {
|
|
653
|
+
cursor: default;
|
|
654
|
+
opacity: 0.4;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
.tool {
|
|
658
|
+
display: inline-flex;
|
|
659
|
+
align-items: center;
|
|
660
|
+
gap: 0.5em;
|
|
661
|
+
font-size: var(--font);
|
|
662
|
+
white-space: nowrap;
|
|
663
|
+
min-width: 0;
|
|
664
|
+
}
|
|
665
|
+
/* In a strip the label takes the row, so a switch sits at its end.
|
|
666
|
+
A dropdown, choice group or slider takes the rest of the row
|
|
667
|
+
instead. Every basis is the content's own width, never 0: with
|
|
668
|
+
flex: 1 the label and the control split the row in half, and the
|
|
669
|
+
control was cut off below the width the ruler measured. Only the
|
|
670
|
+
label gives way (with an ellipsis) when the row is short. */
|
|
671
|
+
.root[data-orientation='vertical'] .bar > .tool > .text {
|
|
672
|
+
flex: 1 1 auto;
|
|
673
|
+
}
|
|
674
|
+
.root[data-orientation='vertical'] .bar > .tool:is(.dropdown, .choice, .slider) > .text {
|
|
675
|
+
flex: 0 1 auto;
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
md-switch {
|
|
679
|
+
--md-switch-track-width: calc(52px * var(--switch-scale));
|
|
680
|
+
--md-switch-track-height: calc(32px * var(--switch-scale));
|
|
681
|
+
--md-switch-handle-width: calc(16px * var(--switch-scale));
|
|
682
|
+
--md-switch-handle-height: calc(16px * var(--switch-scale));
|
|
683
|
+
--md-switch-selected-handle-width: calc(24px * var(--switch-scale));
|
|
684
|
+
--md-switch-selected-handle-height: calc(24px * var(--switch-scale));
|
|
685
|
+
--md-switch-pressed-handle-width: calc(28px * var(--switch-scale));
|
|
686
|
+
--md-switch-pressed-handle-height: calc(28px * var(--switch-scale));
|
|
687
|
+
--md-switch-state-layer-size: calc(40px * var(--switch-scale));
|
|
688
|
+
/* The button shape: the track by its height, the handle by its own size. */
|
|
689
|
+
--md-switch-track-shape: calc(32px * var(--switch-scale) * var(--round));
|
|
690
|
+
--md-switch-handle-shape: calc(var(--round) * 100%);
|
|
691
|
+
--md-switch-selected-handle-color: var(--accent);
|
|
692
|
+
--md-switch-selected-hover-handle-color: var(--accent);
|
|
693
|
+
--md-switch-selected-focus-handle-color: var(--accent);
|
|
694
|
+
--md-switch-selected-pressed-handle-color: var(--accent);
|
|
695
|
+
--md-switch-selected-track-color: color-mix(in srgb, var(--accent) 40%, transparent);
|
|
696
|
+
--md-switch-selected-hover-track-color: color-mix(in srgb, var(--accent) 50%, transparent);
|
|
697
|
+
--md-switch-selected-focus-track-color: color-mix(in srgb, var(--accent) 50%, transparent);
|
|
698
|
+
--md-switch-selected-pressed-track-color: color-mix(in srgb, var(--accent) 50%, transparent);
|
|
699
|
+
/* OFF: Material 2.4.1 names these without "unselected". */
|
|
700
|
+
--switch-off-track: color-mix(in srgb, var(--text) 20%, transparent);
|
|
701
|
+
--md-switch-handle-color: var(--text);
|
|
702
|
+
--md-switch-hover-handle-color: var(--text);
|
|
703
|
+
--md-switch-focus-handle-color: var(--text);
|
|
704
|
+
--md-switch-pressed-handle-color: var(--text);
|
|
705
|
+
--md-switch-track-color: var(--switch-off-track);
|
|
706
|
+
--md-switch-hover-track-color: var(--switch-off-track);
|
|
707
|
+
--md-switch-focus-track-color: var(--switch-off-track);
|
|
708
|
+
--md-switch-pressed-track-color: var(--switch-off-track);
|
|
709
|
+
--md-switch-track-outline-color: var(--text);
|
|
710
|
+
--md-switch-hover-track-outline-color: var(--text);
|
|
711
|
+
--md-switch-focus-track-outline-color: var(--text);
|
|
712
|
+
--md-switch-pressed-track-outline-color: var(--text);
|
|
713
|
+
/* Material's disabled colours are its light scheme's near-black,
|
|
714
|
+
which vanishes on a dark tile. */
|
|
715
|
+
--md-switch-disabled-handle-color: var(--text);
|
|
716
|
+
--md-switch-disabled-track-outline-color: var(--text);
|
|
717
|
+
--md-switch-disabled-selected-track-color: var(--text);
|
|
718
|
+
--md-switch-disabled-selected-handle-color: var(--text);
|
|
719
|
+
flex: none;
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
input[type='range'] {
|
|
723
|
+
accent-color: var(--accent);
|
|
724
|
+
width: var(--slider);
|
|
725
|
+
margin: 0;
|
|
726
|
+
min-width: 0;
|
|
727
|
+
cursor: pointer;
|
|
728
|
+
}
|
|
729
|
+
.compact input[type='range'] {
|
|
730
|
+
width: var(--slider-compact);
|
|
731
|
+
}
|
|
732
|
+
.root[data-orientation='vertical'] .bar input[type='range'] {
|
|
733
|
+
flex: 1 0 auto;
|
|
734
|
+
}
|
|
735
|
+
.readout {
|
|
736
|
+
font-variant-numeric: tabular-nums;
|
|
737
|
+
white-space: nowrap;
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
/* Several texts in one grid cell, only one visible: the cell is as
|
|
741
|
+
wide as the widest, so a changing value never changes the width. */
|
|
742
|
+
.stack {
|
|
743
|
+
display: inline-grid;
|
|
744
|
+
text-align: end;
|
|
745
|
+
}
|
|
746
|
+
.stack > * {
|
|
747
|
+
grid-area: 1 / 1;
|
|
748
|
+
}
|
|
749
|
+
.stack > :not([data-current]) {
|
|
750
|
+
visibility: hidden;
|
|
751
|
+
}
|
|
752
|
+
/* In a strip the trigger is wider than its text: the text takes the
|
|
753
|
+
room, so it starts after the icon and the arrow stays at the end. */
|
|
754
|
+
.trigger .stack {
|
|
755
|
+
flex: 1 1 auto;
|
|
756
|
+
text-align: start;
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
.segments {
|
|
760
|
+
display: inline-flex;
|
|
761
|
+
height: var(--h);
|
|
762
|
+
box-sizing: border-box;
|
|
763
|
+
border: 1px solid color-mix(in srgb, currentColor 40%, transparent);
|
|
764
|
+
border-radius: calc(var(--h) * var(--round));
|
|
765
|
+
overflow: hidden;
|
|
766
|
+
}
|
|
767
|
+
.root[data-orientation='vertical'] .bar .segments {
|
|
768
|
+
flex: 1 0 auto;
|
|
769
|
+
}
|
|
770
|
+
.segment {
|
|
771
|
+
display: inline-flex;
|
|
772
|
+
align-items: center;
|
|
773
|
+
justify-content: center;
|
|
774
|
+
gap: 0.35em;
|
|
775
|
+
flex: 1 0 auto;
|
|
776
|
+
padding: 0 0.85em;
|
|
777
|
+
border: none;
|
|
778
|
+
background: transparent;
|
|
779
|
+
font-size: var(--font);
|
|
780
|
+
cursor: pointer;
|
|
781
|
+
white-space: nowrap;
|
|
782
|
+
}
|
|
783
|
+
.segment.icon-only {
|
|
784
|
+
padding: 0 0.5em;
|
|
785
|
+
}
|
|
786
|
+
.segment + .segment {
|
|
787
|
+
border-left: 1px solid color-mix(in srgb, currentColor 40%, transparent);
|
|
788
|
+
}
|
|
789
|
+
.segment[aria-checked='true'] {
|
|
790
|
+
background-color: var(--accent);
|
|
791
|
+
color: var(--on-accent);
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
.trigger {
|
|
795
|
+
justify-content: space-between;
|
|
796
|
+
padding: 0 0.3em 0 0.8em;
|
|
797
|
+
}
|
|
798
|
+
.root[data-orientation='vertical'] .bar .trigger {
|
|
799
|
+
flex: 1 0 auto;
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
.label {
|
|
803
|
+
opacity: 0.85;
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
.separator {
|
|
807
|
+
align-self: center;
|
|
808
|
+
width: 1px;
|
|
809
|
+
height: calc(var(--h) * 0.7);
|
|
810
|
+
background: color-mix(in srgb, currentColor 30%, transparent);
|
|
811
|
+
}
|
|
812
|
+
.root[data-orientation='vertical'] .separator {
|
|
813
|
+
width: auto;
|
|
814
|
+
height: 1px;
|
|
815
|
+
align-self: stretch;
|
|
816
|
+
}
|
|
817
|
+
.spacer {
|
|
818
|
+
min-width: 0;
|
|
819
|
+
min-height: 0;
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
/* The menu and dropdown lists live in the top layer: the tile clips. */
|
|
823
|
+
.popup {
|
|
824
|
+
position: fixed;
|
|
825
|
+
inset: auto;
|
|
826
|
+
margin: 0;
|
|
827
|
+
box-sizing: border-box;
|
|
828
|
+
min-width: 180px;
|
|
829
|
+
max-width: min(340px, calc(100vw - 8px));
|
|
830
|
+
padding: 6px 0;
|
|
831
|
+
overflow: auto;
|
|
832
|
+
border: 1px solid color-mix(in srgb, var(--text) 18%, transparent);
|
|
833
|
+
border-radius: 10px;
|
|
834
|
+
background: var(--surface);
|
|
835
|
+
color: var(--text);
|
|
836
|
+
box-shadow: 0 6px 20px rgb(0 0 0 / 0.25);
|
|
837
|
+
font-size: 14px;
|
|
838
|
+
--h: 36px;
|
|
839
|
+
--font: 14px;
|
|
840
|
+
--icon: 20px;
|
|
841
|
+
--switch-scale: 0.8;
|
|
842
|
+
}
|
|
843
|
+
.menu-item {
|
|
844
|
+
box-sizing: border-box;
|
|
845
|
+
display: flex;
|
|
846
|
+
align-items: center;
|
|
847
|
+
gap: 12px;
|
|
848
|
+
width: 100%;
|
|
849
|
+
min-height: 40px;
|
|
850
|
+
padding: 4px 16px;
|
|
851
|
+
border: none;
|
|
852
|
+
background: transparent;
|
|
853
|
+
text-align: start;
|
|
854
|
+
font-size: 14px;
|
|
855
|
+
cursor: pointer;
|
|
856
|
+
}
|
|
857
|
+
.menu-item .text {
|
|
858
|
+
flex: 1;
|
|
859
|
+
}
|
|
860
|
+
.menu-item.column {
|
|
861
|
+
flex-direction: column;
|
|
862
|
+
align-items: stretch;
|
|
863
|
+
gap: 4px;
|
|
864
|
+
cursor: default;
|
|
865
|
+
}
|
|
866
|
+
.menu-item.column > .row {
|
|
867
|
+
display: flex;
|
|
868
|
+
align-items: center;
|
|
869
|
+
gap: 12px;
|
|
870
|
+
}
|
|
871
|
+
.menu-item input[type='range'] {
|
|
872
|
+
flex: 1;
|
|
873
|
+
width: auto;
|
|
874
|
+
}
|
|
875
|
+
.menu-item[aria-checked='true'],
|
|
876
|
+
.menu-item[data-active] {
|
|
877
|
+
background-color: color-mix(in srgb, var(--accent) 18%, transparent);
|
|
878
|
+
}
|
|
879
|
+
.menu-heading {
|
|
880
|
+
display: flex;
|
|
881
|
+
align-items: center;
|
|
882
|
+
gap: 12px;
|
|
883
|
+
padding: 8px 16px 2px;
|
|
884
|
+
font-size: 12px;
|
|
885
|
+
opacity: 0.75;
|
|
886
|
+
}
|
|
887
|
+
.check {
|
|
888
|
+
visibility: hidden;
|
|
889
|
+
color: var(--accent);
|
|
890
|
+
}
|
|
891
|
+
[aria-checked='true'] > .check {
|
|
892
|
+
visibility: visible;
|
|
893
|
+
}
|
|
894
|
+
.popup hr {
|
|
895
|
+
margin: 6px 0;
|
|
896
|
+
border: none;
|
|
897
|
+
border-top: 1px solid color-mix(in srgb, var(--text) 18%, transparent);
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
dialog.confirm {
|
|
901
|
+
box-sizing: border-box;
|
|
902
|
+
max-width: min(380px, calc(100vw - 32px));
|
|
903
|
+
padding: 20px 24px 16px;
|
|
904
|
+
border: none;
|
|
905
|
+
border-radius: 14px;
|
|
906
|
+
background: var(--surface);
|
|
907
|
+
color: var(--text);
|
|
908
|
+
box-shadow: 0 8px 28px rgb(0 0 0 / 0.3);
|
|
909
|
+
font-size: 15px;
|
|
910
|
+
line-height: 1.4;
|
|
911
|
+
}
|
|
912
|
+
dialog.confirm::backdrop {
|
|
913
|
+
background: rgb(0 0 0 / 0.32);
|
|
914
|
+
}
|
|
915
|
+
dialog.confirm p {
|
|
916
|
+
margin: 0;
|
|
917
|
+
overflow-wrap: anywhere;
|
|
918
|
+
}
|
|
919
|
+
dialog.confirm .actions {
|
|
920
|
+
display: flex;
|
|
921
|
+
justify-content: flex-end;
|
|
922
|
+
gap: 8px;
|
|
923
|
+
margin-top: 20px;
|
|
924
|
+
--h: 36px;
|
|
925
|
+
--font: 14px;
|
|
926
|
+
}
|
|
927
|
+
`
|
|
928
|
+
|
|
929
|
+
render() {
|
|
930
|
+
this.resolveColors()
|
|
931
|
+
const tools = this.tools
|
|
932
|
+
const orientation = this.orientation
|
|
933
|
+
const vertical = orientation === 'vertical'
|
|
934
|
+
const alignment = oneOf(this.inputData?.alignment, ['start', 'center', 'end'] as const, 'start')
|
|
935
|
+
// Until the tools are measured for this config, show them all.
|
|
936
|
+
const placement: Placement =
|
|
937
|
+
this.placement && this.placement.signature === this.signature
|
|
938
|
+
? this.placement.placement
|
|
939
|
+
: { visible: tools.map((_, i) => i), compact: [], overflow: [] }
|
|
940
|
+
const themeText = this.theme?.theme_object?.title?.textStyle?.color
|
|
941
|
+
// The host's --re-text-color wins; the theme's colour is the fallback.
|
|
942
|
+
const text = `var(--re-text-color, ${typeof themeText === 'string' && themeText ? themeText : 'inherit'})`
|
|
943
|
+
|
|
944
|
+
return html`
|
|
945
|
+
<div
|
|
946
|
+
class="root"
|
|
947
|
+
data-orientation=${orientation}
|
|
948
|
+
data-size=${this.size}
|
|
949
|
+
data-shape=${this.shape}
|
|
950
|
+
style=${styleMap({ '--text': text, '--surface': this.surface })}
|
|
951
|
+
>
|
|
952
|
+
<div
|
|
953
|
+
class="bar"
|
|
954
|
+
role="toolbar"
|
|
955
|
+
aria-orientation=${orientation}
|
|
956
|
+
style=${styleMap({ justifyContent: ALIGNMENTS[alignment] })}
|
|
957
|
+
>
|
|
958
|
+
${repeat(
|
|
959
|
+
placement.visible.filter((i) => tools[i]),
|
|
960
|
+
(i) => tools[i].key,
|
|
961
|
+
(i) => this.renderTool(tools[i], placement.compact.includes(i) ? 'compact' : 'full')
|
|
962
|
+
)}
|
|
963
|
+
${placement.overflow.length ? this.renderMore(vertical, this.popup?.kind === 'menu') : nothing}
|
|
964
|
+
</div>
|
|
965
|
+
<div class="ruler" inert aria-hidden="true">
|
|
966
|
+
${repeat(
|
|
967
|
+
tools,
|
|
968
|
+
(tool) => tool.key,
|
|
969
|
+
(tool) => html`
|
|
970
|
+
<div class="slot">${this.renderTool(tool, 'full', true)}</div>
|
|
971
|
+
<div class="slot">${this.renderTool(tool, 'compact', true)}</div>
|
|
972
|
+
`
|
|
973
|
+
)}
|
|
974
|
+
<div class="slot">${this.renderMore(vertical, false)}</div>
|
|
975
|
+
</div>
|
|
976
|
+
<div class="popup" popover="manual" role=${this.popup?.kind === 'dropdown' ? 'listbox' : nothing}>
|
|
977
|
+
${this.renderPopup(placement)}
|
|
978
|
+
</div>
|
|
979
|
+
<dialog
|
|
980
|
+
class="confirm"
|
|
981
|
+
@close=${this.onDialogClose}
|
|
982
|
+
@cancel=${(e: Event) => {
|
|
983
|
+
// Escape answers "no".
|
|
984
|
+
e.preventDefault()
|
|
985
|
+
this.endConfirm(false)
|
|
986
|
+
}}
|
|
987
|
+
>
|
|
988
|
+
${this.confirm
|
|
989
|
+
? html`
|
|
990
|
+
<p>${this.confirm.message}</p>
|
|
991
|
+
<form method="dialog" class="actions">
|
|
992
|
+
<button class="button" value="cancel">Cancel</button>
|
|
993
|
+
<button
|
|
994
|
+
class="button"
|
|
995
|
+
data-variant="filled"
|
|
996
|
+
value="ok"
|
|
997
|
+
autofocus
|
|
998
|
+
style=${styleMap({
|
|
999
|
+
'--accent': this.confirm.accent,
|
|
1000
|
+
'--on-accent': this.confirm.onAccent
|
|
1001
|
+
})}
|
|
1002
|
+
>
|
|
1003
|
+
OK
|
|
1004
|
+
</button>
|
|
1005
|
+
</form>
|
|
1006
|
+
`
|
|
1007
|
+
: nothing}
|
|
1008
|
+
</dialog>
|
|
1009
|
+
</div>
|
|
1010
|
+
`
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
private renderMore(vertical: boolean, open: boolean) {
|
|
1014
|
+
return html`
|
|
1015
|
+
<button
|
|
1016
|
+
class="button icon-only more"
|
|
1017
|
+
aria-label="More tools"
|
|
1018
|
+
aria-haspopup="true"
|
|
1019
|
+
aria-expanded=${open ? 'true' : 'false'}
|
|
1020
|
+
@click=${(e: Event) => this.togglePopup({ kind: 'menu' }, e.currentTarget as HTMLElement)}
|
|
1021
|
+
>
|
|
1022
|
+
<md-icon aria-hidden="true">${vertical ? 'more_horiz' : 'more_vert'}</md-icon>
|
|
1023
|
+
</button>
|
|
1024
|
+
`
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
private icon(name?: string) {
|
|
1028
|
+
return name ? html`<md-icon aria-hidden="true">${name}</md-icon>` : nothing
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
/** One tool in the bar, or with `ruler` its measured copy. */
|
|
1032
|
+
private renderTool(tool: ToolModel, density: Density, ruler = false): TemplateResult {
|
|
1033
|
+
const compact = density === 'compact'
|
|
1034
|
+
// Compact drops a label only where an icon stands in for it.
|
|
1035
|
+
const showLabel = !!tool.label && !(compact && tool.icon)
|
|
1036
|
+
|
|
1037
|
+
switch (tool.control) {
|
|
1038
|
+
case 'separator':
|
|
1039
|
+
return html`<span class="separator" role="separator"></span>`
|
|
1040
|
+
case 'spacer':
|
|
1041
|
+
return html`<span class="spacer"></span>`
|
|
1042
|
+
case 'label':
|
|
1043
|
+
return html`<span class="tool label">${this.icon(tool.icon)}<span class="text">${tool.label}</span></span>`
|
|
1044
|
+
case 'button': {
|
|
1045
|
+
const label = tool.label || tool.buttonValue
|
|
1046
|
+
const iconOnly = !!tool.icon && (!label || compact)
|
|
1047
|
+
return html`
|
|
1048
|
+
<button
|
|
1049
|
+
class="button ${iconOnly ? 'icon-only' : ''}"
|
|
1050
|
+
data-variant=${tool.variant}
|
|
1051
|
+
?data-active=${tool.active}
|
|
1052
|
+
aria-pressed=${tool.buttonValue.trim() ? (tool.active ? 'true' : 'false') : nothing}
|
|
1053
|
+
aria-label=${label || nothing}
|
|
1054
|
+
title=${iconOnly && label ? label : nothing}
|
|
1055
|
+
?disabled=${tool.disabled}
|
|
1056
|
+
style=${this.toolStyle(tool)}
|
|
1057
|
+
@click=${ruler ? nothing : () => this.interact(tool)}
|
|
1058
|
+
>
|
|
1059
|
+
${this.icon(tool.icon)}${iconOnly ? nothing : html`<span class="text">${label}</span>`}
|
|
1060
|
+
</button>
|
|
1061
|
+
`
|
|
1062
|
+
}
|
|
1063
|
+
case 'switch':
|
|
1064
|
+
return html`
|
|
1065
|
+
<label class="tool switch" ?data-disabled=${tool.disabled} style=${this.toolStyle(tool)}>
|
|
1066
|
+
${this.icon(tool.icon)}${showLabel ? html`<span class="text">${tool.label}</span>` : nothing}
|
|
1067
|
+
<md-switch
|
|
1068
|
+
aria-label=${tool.label || nothing}
|
|
1069
|
+
.selected=${live(tool.on)}
|
|
1070
|
+
?disabled=${tool.disabled}
|
|
1071
|
+
@change=${ruler
|
|
1072
|
+
? nothing
|
|
1073
|
+
: (e: Event) => this.interact(tool, (e.target as MdSwitch).selected)}
|
|
1074
|
+
></md-switch>
|
|
1075
|
+
</label>
|
|
1076
|
+
`
|
|
1077
|
+
case 'slider':
|
|
1078
|
+
return html`
|
|
1079
|
+
<div
|
|
1080
|
+
class="tool slider ${compact ? 'compact' : ''}"
|
|
1081
|
+
?data-disabled=${tool.disabled}
|
|
1082
|
+
style=${this.toolStyle(tool)}
|
|
1083
|
+
>
|
|
1084
|
+
${this.icon(tool.icon)}${showLabel ? html`<span class="text">${tool.label}</span>` : nothing}
|
|
1085
|
+
${this.renderRange(tool, ruler)}
|
|
1086
|
+
</div>
|
|
1087
|
+
`
|
|
1088
|
+
case 'choice':
|
|
1089
|
+
return html`
|
|
1090
|
+
<div
|
|
1091
|
+
class="tool choice"
|
|
1092
|
+
role="radiogroup"
|
|
1093
|
+
aria-label=${tool.label || nothing}
|
|
1094
|
+
style=${this.toolStyle(tool)}
|
|
1095
|
+
>
|
|
1096
|
+
${tool.label && !compact ? html`<span class="text">${tool.label}</span>` : nothing}
|
|
1097
|
+
<div class="segments">
|
|
1098
|
+
${tool.options.map((option, j) => {
|
|
1099
|
+
const iconOnly = compact && !!option.icon
|
|
1100
|
+
return html`
|
|
1101
|
+
<button
|
|
1102
|
+
class="segment ${iconOnly ? 'icon-only' : ''}"
|
|
1103
|
+
role="radio"
|
|
1104
|
+
aria-checked=${j === tool.selected ? 'true' : 'false'}
|
|
1105
|
+
aria-label=${option.label || nothing}
|
|
1106
|
+
title=${iconOnly && option.label ? option.label : nothing}
|
|
1107
|
+
?disabled=${tool.disabled}
|
|
1108
|
+
@click=${ruler ? nothing : () => this.pick(tool, j)}
|
|
1109
|
+
>
|
|
1110
|
+
${this.icon(option.icon)}${iconOnly ? nothing : option.label}
|
|
1111
|
+
</button>
|
|
1112
|
+
`
|
|
1113
|
+
})}
|
|
1114
|
+
</div>
|
|
1115
|
+
</div>
|
|
1116
|
+
`
|
|
1117
|
+
case 'dropdown': {
|
|
1118
|
+
const open = !ruler && this.popup?.kind === 'dropdown' && this.popup.key === tool.key
|
|
1119
|
+
return html`
|
|
1120
|
+
<div class="tool dropdown" style=${this.toolStyle(tool)}>
|
|
1121
|
+
${tool.label && !compact ? html`<span class="text">${tool.label}</span>` : nothing}
|
|
1122
|
+
<button
|
|
1123
|
+
class="button trigger"
|
|
1124
|
+
data-variant="outlined"
|
|
1125
|
+
aria-haspopup="listbox"
|
|
1126
|
+
aria-expanded=${open ? 'true' : 'false'}
|
|
1127
|
+
aria-label=${tool.label || nothing}
|
|
1128
|
+
?disabled=${tool.disabled}
|
|
1129
|
+
@click=${ruler
|
|
1130
|
+
? nothing
|
|
1131
|
+
: (e: Event) =>
|
|
1132
|
+
this.togglePopup({ kind: 'dropdown', key: tool.key }, e.currentTarget as HTMLElement)}
|
|
1133
|
+
>
|
|
1134
|
+
${this.icon(tool.icon)}
|
|
1135
|
+
<span class="stack">
|
|
1136
|
+
${tool.options.map(
|
|
1137
|
+
(option, j) => html`<span ?data-current=${j === tool.selected}>${option.label}</span>`
|
|
1138
|
+
)}
|
|
1139
|
+
<span ?data-current=${tool.selected < 0}>–</span>
|
|
1140
|
+
</span>
|
|
1141
|
+
<md-icon aria-hidden="true">arrow_drop_down</md-icon>
|
|
1142
|
+
</button>
|
|
1143
|
+
</div>
|
|
1144
|
+
`
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
/** A slider's range input and its value, in a width that never depends on the value. */
|
|
1150
|
+
private renderRange(tool: ToolModel, ruler: boolean) {
|
|
1151
|
+
const dragged = !ruler && this.dragging?.key === tool.key ? this.dragging.value : undefined
|
|
1152
|
+
const value = dragged ?? tool.sliderValue
|
|
1153
|
+
return html`
|
|
1154
|
+
<input
|
|
1155
|
+
type="range"
|
|
1156
|
+
min=${tool.min}
|
|
1157
|
+
max=${tool.max}
|
|
1158
|
+
step=${tool.step}
|
|
1159
|
+
.value=${live(String(value))}
|
|
1160
|
+
aria-label=${tool.label || nothing}
|
|
1161
|
+
aria-valuetext=${`${formatNumber(value, tool.decimals)}${tool.unit ? ' ' + tool.unit : ''}`}
|
|
1162
|
+
?disabled=${tool.disabled}
|
|
1163
|
+
@input=${ruler
|
|
1164
|
+
? nothing
|
|
1165
|
+
: (e: Event) => {
|
|
1166
|
+
this.dragging = { key: tool.key, value: Number((e.target as HTMLInputElement).value) }
|
|
1167
|
+
}}
|
|
1168
|
+
@change=${ruler
|
|
1169
|
+
? nothing
|
|
1170
|
+
: (e: Event) => {
|
|
1171
|
+
this.dragging = undefined
|
|
1172
|
+
this.interact(tool, Number((e.target as HTMLInputElement).value))
|
|
1173
|
+
}}
|
|
1174
|
+
/>
|
|
1175
|
+
<span class="readout"
|
|
1176
|
+
><span class="stack"
|
|
1177
|
+
><span>${formatNumber(tool.min, tool.decimals)}</span
|
|
1178
|
+
><span>${formatNumber(tool.max, tool.decimals)}</span
|
|
1179
|
+
><span data-current>${formatNumber(value, tool.decimals)}</span></span
|
|
1180
|
+
>${tool.unit ? ` ${tool.unit}` : nothing}</span
|
|
1181
|
+
>
|
|
1182
|
+
`
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
private pick(tool: ToolModel, index: number) {
|
|
1186
|
+
// Picking what is already picked writes nothing: no duplicate row, no
|
|
1187
|
+
// extra step in the browser history.
|
|
1188
|
+
if (index === tool.selected) return
|
|
1189
|
+
const option = tool.options[index]
|
|
1190
|
+
if (option) this.interact(tool, option.value)
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
/** The open popup's content: the ⋮ menu's tools, or a dropdown's options. */
|
|
1194
|
+
private renderPopup(placement: Placement) {
|
|
1195
|
+
const popup = this.popup
|
|
1196
|
+
if (!popup) return nothing
|
|
1197
|
+
if (popup.kind === 'dropdown') {
|
|
1198
|
+
const tool = this.tools.find((t) => t.key === popup.key)
|
|
1199
|
+
if (!tool) return nothing
|
|
1200
|
+
return html`<div style=${this.toolStyle(tool)}>${this.renderChoices(tool, 'option')}</div>`
|
|
1201
|
+
}
|
|
1202
|
+
return placement.overflow.map((i) => (this.tools[i] ? this.renderMenuTool(this.tools[i]) : nothing))
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
private renderChoices(tool: ToolModel, role: 'option' | 'radio') {
|
|
1206
|
+
return tool.options.map(
|
|
1207
|
+
(option, j) => html`
|
|
1208
|
+
<button
|
|
1209
|
+
class="menu-item"
|
|
1210
|
+
role=${role}
|
|
1211
|
+
aria-checked=${role === 'radio' ? (j === tool.selected ? 'true' : 'false') : nothing}
|
|
1212
|
+
aria-selected=${role === 'option' ? (j === tool.selected ? 'true' : 'false') : nothing}
|
|
1213
|
+
?data-current=${j === tool.selected}
|
|
1214
|
+
?disabled=${tool.disabled}
|
|
1215
|
+
@click=${() => {
|
|
1216
|
+
this.closePopup()
|
|
1217
|
+
this.pick(tool, j)
|
|
1218
|
+
}}
|
|
1219
|
+
>
|
|
1220
|
+
${this.icon(option.icon)}<span class="text">${option.label}</span>
|
|
1221
|
+
<md-icon
|
|
1222
|
+
class="check"
|
|
1223
|
+
aria-hidden="true"
|
|
1224
|
+
style=${j === tool.selected ? 'visibility: visible' : nothing}
|
|
1225
|
+
>check</md-icon
|
|
1226
|
+
>
|
|
1227
|
+
</button>
|
|
1228
|
+
`
|
|
1229
|
+
)
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
/** A tool as a row of the ⋮ menu. */
|
|
1233
|
+
private renderMenuTool(tool: ToolModel): TemplateResult | typeof nothing {
|
|
1234
|
+
const style = this.toolStyle(tool)
|
|
1235
|
+
switch (tool.control) {
|
|
1236
|
+
case 'separator':
|
|
1237
|
+
return html`<hr />`
|
|
1238
|
+
case 'spacer':
|
|
1239
|
+
return nothing
|
|
1240
|
+
case 'label':
|
|
1241
|
+
return html`<div class="menu-heading">${this.icon(tool.icon)}${tool.label}</div>`
|
|
1242
|
+
case 'button': {
|
|
1243
|
+
const label = tool.label || tool.buttonValue
|
|
1244
|
+
return html`
|
|
1245
|
+
<button
|
|
1246
|
+
class="menu-item"
|
|
1247
|
+
?data-active=${tool.active}
|
|
1248
|
+
aria-pressed=${tool.buttonValue.trim() ? (tool.active ? 'true' : 'false') : nothing}
|
|
1249
|
+
?disabled=${tool.disabled}
|
|
1250
|
+
style=${style}
|
|
1251
|
+
@click=${() => {
|
|
1252
|
+
this.closePopup()
|
|
1253
|
+
this.interact(tool)
|
|
1254
|
+
}}
|
|
1255
|
+
>
|
|
1256
|
+
${this.icon(tool.icon)}<span class="text">${label}</span>
|
|
1257
|
+
</button>
|
|
1258
|
+
`
|
|
1259
|
+
}
|
|
1260
|
+
case 'switch':
|
|
1261
|
+
return html`
|
|
1262
|
+
<label class="menu-item" ?data-disabled=${tool.disabled} style=${style}>
|
|
1263
|
+
${this.icon(tool.icon)}<span class="text">${tool.label}</span>
|
|
1264
|
+
<md-switch
|
|
1265
|
+
aria-label=${tool.label || nothing}
|
|
1266
|
+
.selected=${live(tool.on)}
|
|
1267
|
+
?disabled=${tool.disabled}
|
|
1268
|
+
@change=${(e: Event) => this.interact(tool, (e.target as MdSwitch).selected)}
|
|
1269
|
+
></md-switch>
|
|
1270
|
+
</label>
|
|
1271
|
+
`
|
|
1272
|
+
case 'slider':
|
|
1273
|
+
return html`
|
|
1274
|
+
<div class="menu-item column" ?data-disabled=${tool.disabled} style=${style}>
|
|
1275
|
+
<div class="row">${this.icon(tool.icon)}<span class="text">${tool.label}</span></div>
|
|
1276
|
+
<div class="row">${this.renderRange(tool, false)}</div>
|
|
1277
|
+
</div>
|
|
1278
|
+
`
|
|
1279
|
+
case 'choice':
|
|
1280
|
+
case 'dropdown':
|
|
1281
|
+
return html`
|
|
1282
|
+
<div role="radiogroup" aria-label=${tool.label || nothing} style=${style}>
|
|
1283
|
+
${tool.label ? html`<div class="menu-heading">${this.icon(tool.icon)}${tool.label}</div>` : nothing}
|
|
1284
|
+
${this.renderChoices(tool, 'radio')}
|
|
1285
|
+
</div>
|
|
1286
|
+
`
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
}
|