@daasboard/ui 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/LICENSE +5 -0
  2. package/README.md +6 -2
  3. package/dist/LICENSE-lucide.txt +43 -0
  4. package/dist/color/color.d.ts +34 -0
  5. package/dist/color/color.js +106 -0
  6. package/dist/color/edit.d.ts +34 -0
  7. package/dist/color/edit.js +40 -0
  8. package/dist/color/gradient.d.ts +39 -0
  9. package/dist/color/gradient.js +129 -0
  10. package/dist/color/index.d.ts +3 -0
  11. package/dist/color/index.js +3 -0
  12. package/dist/controls/buttons.d.ts +54 -0
  13. package/dist/controls/buttons.js +95 -0
  14. package/dist/controls/font-picker.d.ts +35 -0
  15. package/dist/controls/font-picker.js +178 -0
  16. package/dist/controls/index.d.ts +43 -0
  17. package/dist/controls/index.js +32 -0
  18. package/dist/controls/number-field.d.ts +60 -0
  19. package/dist/controls/number-field.js +190 -0
  20. package/dist/controls/place.d.ts +10 -0
  21. package/dist/controls/place.js +77 -0
  22. package/dist/controls/popover.d.ts +84 -0
  23. package/dist/controls/popover.js +366 -0
  24. package/dist/controls/shared.d.ts +30 -0
  25. package/dist/controls/shared.js +134 -0
  26. package/dist/controls/slider.d.ts +82 -0
  27. package/dist/controls/slider.js +241 -0
  28. package/dist/controls/swatch.d.ts +25 -0
  29. package/dist/controls/swatch.js +38 -0
  30. package/dist/controls/tooltip.d.ts +19 -0
  31. package/dist/controls/tooltip.js +84 -0
  32. package/dist/controls/typed.d.ts +28 -0
  33. package/dist/controls/typed.js +126 -0
  34. package/dist/element.d.ts +40 -0
  35. package/dist/element.js +78 -0
  36. package/dist/host.d.ts +99 -0
  37. package/dist/host.js +1 -0
  38. package/dist/icons/index.d.ts +7 -0
  39. package/dist/icons/index.js +31 -0
  40. package/dist/icons/lucide.d.ts +851 -0
  41. package/dist/icons/lucide.js +90 -0
  42. package/dist/index.d.ts +18 -0
  43. package/dist/index.js +16 -0
  44. package/dist/keys.d.ts +30 -0
  45. package/dist/keys.js +122 -0
  46. package/dist/mount.d.ts +11 -0
  47. package/dist/mount.js +96 -0
  48. package/dist/options.d.ts +152 -0
  49. package/dist/options.js +78 -0
  50. package/dist/palette.d.ts +6 -0
  51. package/dist/palette.js +9 -0
  52. package/dist/picker/handles.d.ts +7 -0
  53. package/dist/picker/handles.js +8 -0
  54. package/dist/picker/index.d.ts +21 -0
  55. package/dist/picker/index.js +20 -0
  56. package/dist/picker/palette.d.ts +8 -0
  57. package/dist/picker/palette.js +14 -0
  58. package/dist/picker/picker.d.ts +63 -0
  59. package/dist/picker/picker.js +561 -0
  60. package/dist/picker/solid-editor.d.ts +60 -0
  61. package/dist/picker/solid-editor.js +330 -0
  62. package/dist/picker/stop-bar.d.ts +27 -0
  63. package/dist/picker/stop-bar.js +140 -0
  64. package/dist/prefs.d.ts +6 -0
  65. package/dist/prefs.js +34 -0
  66. package/dist/recents.d.ts +9 -0
  67. package/dist/recents.js +140 -0
  68. package/dist/root.d.ts +31 -0
  69. package/dist/root.js +526 -0
  70. package/dist/scheduler.d.ts +21 -0
  71. package/dist/scheduler.js +132 -0
  72. package/dist/strings.d.ts +284 -0
  73. package/dist/strings.js +287 -0
  74. package/dist/surface.d.ts +27 -0
  75. package/dist/surface.js +8 -0
  76. package/dist/surfaces/context-menu/index.d.ts +2 -0
  77. package/dist/surfaces/context-menu/index.js +2 -0
  78. package/dist/surfaces/context-menu/items.d.ts +14 -0
  79. package/dist/surfaces/context-menu/items.js +38 -0
  80. package/dist/surfaces/context-menu/menu.d.ts +16 -0
  81. package/dist/surfaces/context-menu/menu.js +135 -0
  82. package/dist/surfaces/pane/index.d.ts +16 -0
  83. package/dist/surfaces/pane/index.js +19 -0
  84. package/dist/surfaces/pane/model.d.ts +41 -0
  85. package/dist/surfaces/pane/model.js +139 -0
  86. package/dist/surfaces/pane/outlet.d.ts +16 -0
  87. package/dist/surfaces/pane/outlet.js +20 -0
  88. package/dist/surfaces/pane/pane.d.ts +40 -0
  89. package/dist/surfaces/pane/pane.js +1429 -0
  90. package/dist/surfaces/pane/placement.d.ts +11 -0
  91. package/dist/surfaces/pane/placement.js +62 -0
  92. package/dist/surfaces/pane/text-field.d.ts +16 -0
  93. package/dist/surfaces/pane/text-field.js +20 -0
  94. package/dist/surfaces/selection-toolbar/index.d.ts +2 -0
  95. package/dist/surfaces/selection-toolbar/index.js +2 -0
  96. package/dist/surfaces/selection-toolbar/items.d.ts +37 -0
  97. package/dist/surfaces/selection-toolbar/items.js +183 -0
  98. package/dist/surfaces/selection-toolbar/toolbar.d.ts +27 -0
  99. package/dist/surfaces/selection-toolbar/toolbar.js +956 -0
  100. package/dist/surfaces/tools/index.d.ts +2 -0
  101. package/dist/surfaces/tools/index.js +2 -0
  102. package/dist/surfaces/tools/keys.d.ts +11 -0
  103. package/dist/surfaces/tools/keys.js +21 -0
  104. package/dist/surfaces/tools/model.d.ts +48 -0
  105. package/dist/surfaces/tools/model.js +155 -0
  106. package/dist/surfaces/tools/toolbar.d.ts +27 -0
  107. package/dist/surfaces/tools/toolbar.js +502 -0
  108. package/dist/theme.d.ts +26 -0
  109. package/dist/theme.js +47 -0
  110. package/package.json +31 -3
@@ -0,0 +1,561 @@
1
+ import { css, html, nothing } from "lit";
2
+ import { styleMap } from "lit/directives/style-map.js";
3
+ import { formatHex, parseStored } from "../color/color.js";
4
+ import { applyPaintEdit } from "../color/edit.js";
5
+ import { angleOf, firstColor, normalizeDeg, paintTypeOf, sortedStops, stopColorAt } from "../color/gradient.js";
6
+ import { CLOSE, OPEN } from "../controls/popover.js";
7
+ import { baseStyles, CANCEL, cancelDragsWithin, COMMIT, emit, focusWithin, keepFocus, PREVIEW } from "../controls/shared.js";
8
+ import { swatchBackground } from "../controls/swatch.js";
9
+ import { UiElement } from "../element.js";
10
+ import { COLOR_FORMATS, GRADIENT_TYPES, gradientsFor, PICKER_MODES } from "../options.js";
11
+ import { DEFAULT_RECENTS, memoryRecents, paintKey } from "../recents.js";
12
+ import { tok } from "../theme.js";
13
+ import { sameTarget } from "./handles.js";
14
+ import { pickerStyles } from "./solid-editor.js";
15
+ export const DEFAULT_PICKER_OPTIONS = {
16
+ gradients: GRADIENT_TYPES,
17
+ alpha: true,
18
+ formats: COLOR_FORMATS,
19
+ modes: PICKER_MODES,
20
+ palette: undefined,
21
+ recents: { max: DEFAULT_RECENTS },
22
+ eyedropper: true,
23
+ tokens: true,
24
+ canvasHandles: true,
25
+ };
26
+ const standaloneRecents = memoryRecents();
27
+ export function pickerTypes(color, descriptorType) {
28
+ return ["solid", ...gradientsFor(color, descriptorType)];
29
+ }
30
+ const ALL_TYPES = ["solid", "linear", "radial"];
31
+ const pct = (v) => Math.round(v * 1000) / 10;
32
+ const samePaint = (a, b) => (a === null || b === null ? a === b : paintKey(a) === paintKey(b));
33
+ function focusControl(el) {
34
+ if (!el)
35
+ return;
36
+ void (el.updateComplete ?? Promise.resolve()).then(() => (el.shadowRoot?.querySelector("button, input, [tabindex]") ?? el).focus());
37
+ }
38
+ export class PaintPicker extends UiElement {
39
+ static styles = [
40
+ baseStyles,
41
+ pickerStyles,
42
+ css `
43
+ :host{display: flex;flex-direction: column;gap: 10px;width: 248px;padding: 8px;}.head{display: flex;align-items: center;gap: 6px;}.stops{display: flex;flex-direction: column;gap: 4px;}.stop{display: grid;grid-template-columns: 64px 24px 1fr 32px;gap: 4px;align-items: center;border-radius: 4px;}.stop.selected{background: ${tok("accentSoft")};}.geometry{display: grid;grid-template-columns: 1fr auto auto;gap: 4px;align-items: center;}.geometry.radial{grid-template-columns: 1fr 1fr;}.geometry.radial daas-icon-button{justify-self: end;}.recents{display: flex;flex-wrap: wrap;gap: 4px;}h3{margin: 0;font-size: 11px;font-weight: 600;color: ${tok("muted")};}/* the focus passes through the frame on its way to the next view's control (#holdFocus), within one update */
44
+ .picker:focus-visible{outline: none;}`,
45
+ ];
46
+ static properties = {
47
+ value: { attribute: false },
48
+ mixed: { type: Boolean },
49
+ box: { attribute: false },
50
+ types: { attribute: false },
51
+ color: { attribute: false },
52
+ allowNone: { type: Boolean, attribute: "allow-none" },
53
+ recents: { attribute: false },
54
+ handles: { attribute: false },
55
+ };
56
+ #draft = undefined;
57
+ #gesture = false;
58
+ #gestureFrom = null;
59
+ #gestureMixed = false;
60
+ #stop = 0;
61
+ #stopView = null;
62
+ #holding = false;
63
+ #heldOrder = null;
64
+ #accepted = { draft: undefined, committed: false, paint: null };
65
+ #reverted = false;
66
+ #committed = false;
67
+ #sessionPaint = null;
68
+ #popover = null;
69
+ #subscribed = null;
70
+ #unsubscribe = null;
71
+ #shown = null;
72
+ #shownBy = null;
73
+ #endHandleDrag = null;
74
+ constructor() {
75
+ super();
76
+ this.value = null;
77
+ this.mixed = false;
78
+ this.box = { w: 1, h: 1 };
79
+ this.types = ALL_TYPES;
80
+ this.color = null;
81
+ this.allowNone = true;
82
+ this.recents = null;
83
+ this.handles = null;
84
+ }
85
+ connectedCallback() {
86
+ super.connectedCallback();
87
+ this.#popover = this.closest("daas-popover");
88
+ this.#popover?.addEventListener(OPEN, this.#onOpen);
89
+ this.#popover?.addEventListener(CLOSE, this.#onClose);
90
+ this.#listenToRecents();
91
+ this.requestUpdate();
92
+ }
93
+ disconnectedCallback() {
94
+ this.endSession();
95
+ this.#popover?.removeEventListener(OPEN, this.#onOpen);
96
+ this.#popover?.removeEventListener(CLOSE, this.#onClose);
97
+ if (this.#popover?.keepOpenOn === this.#onHandles)
98
+ this.#popover.keepOpenOn = null;
99
+ this.#popover = null;
100
+ super.disconnectedCallback();
101
+ this.#listenToRecents();
102
+ }
103
+ #listenToRecents() {
104
+ const store = this.isConnected ? this.#store() : null;
105
+ if (store === this.#subscribed)
106
+ return;
107
+ this.#unsubscribe?.();
108
+ this.#subscribed = store;
109
+ this.#unsubscribe = store?.subscribe(() => this.requestUpdate()) ?? null;
110
+ }
111
+ #onOpen = () => {
112
+ this.#committed = this.#accepted.committed = false;
113
+ this.#sessionPaint = this.#accepted.paint = null;
114
+ this.#stopView = null;
115
+ this.#syncHandles();
116
+ this.requestUpdate();
117
+ };
118
+ #onClose = () => this.endSession();
119
+ endSession() {
120
+ cancelDragsWithin(this);
121
+ for (const editor of this.renderRoot?.querySelectorAll("daas-solid-editor") ?? [])
122
+ editor.abortPick();
123
+ this.#hideHandles();
124
+ if (this.#gesture)
125
+ this.#cancel();
126
+ this.settleRecent();
127
+ this.#stopView = null;
128
+ }
129
+ settleRecent() {
130
+ const paint = this.#sessionPaint;
131
+ if (this.#committed && paint !== null)
132
+ this.#store()?.add(paint);
133
+ this.#committed = this.#accepted.committed = false;
134
+ this.#sessionPaint = this.#accepted.paint = null;
135
+ }
136
+ #wantsHandles() {
137
+ return !!this.handles && this.#opts.canvasHandles !== false && !this.mixed && this.#view() !== "solid" && this.#gradient() !== null;
138
+ }
139
+ #sessionOpen() {
140
+ return this.#popover ? this.#popover.open : this.isConnected;
141
+ }
142
+ #syncHandles() {
143
+ const wants = this.#wantsHandles();
144
+ const open = this.#sessionOpen();
145
+ if (this.#shown && (!wants || !open || !sameTarget(this.handles, this.#shownBy)))
146
+ this.#hideHandles();
147
+ if (wants && open && !this.#shown) {
148
+ this.#shownBy = this.handles;
149
+ this.#shown = this.handles.show(this.#handleEvents);
150
+ }
151
+ if (this.#popover)
152
+ this.#popover.keepOpenOn = wants && (this.#shown || !open) ? this.#onHandles : null;
153
+ }
154
+ #onHandles = (e) => this.#shown?.hits(e) ?? false;
155
+ #hideHandles() {
156
+ const shown = this.#shown;
157
+ if (!shown)
158
+ return;
159
+ this.#shown = null;
160
+ this.#shownBy = null;
161
+ const dragging = this.#endHandleDrag !== null;
162
+ this.#releaseHandleDrag();
163
+ shown.hide();
164
+ if (dragging && this.#gesture)
165
+ this.#cancel();
166
+ }
167
+ #releaseHandleDrag() {
168
+ const end = this.#endHandleDrag;
169
+ this.#endHandleDrag = null;
170
+ end?.();
171
+ }
172
+ #handleEvents = {
173
+ preview: (edit) => {
174
+ if (!this.#shown)
175
+ return;
176
+ this.#endHandleDrag ??= this.ui?.beginDrag(() => this.#handleDragCancelled()) ?? (() => { });
177
+ this.#edit(PREVIEW, edit);
178
+ },
179
+ commit: (edit) => {
180
+ if (!this.#shown || !this.#endHandleDrag)
181
+ return;
182
+ this.#releaseHandleDrag();
183
+ this.#edit(COMMIT, edit, true);
184
+ },
185
+ cancel: () => {
186
+ if (!this.#endHandleDrag)
187
+ return;
188
+ this.#releaseHandleDrag();
189
+ if (this.#gesture)
190
+ this.#cancel();
191
+ },
192
+ };
193
+ #handleDragCancelled() {
194
+ this.#endHandleDrag = null;
195
+ this.#shown?.cancelDrag();
196
+ if (this.#gesture)
197
+ this.#cancel();
198
+ }
199
+ get #opts() {
200
+ return this.color ?? DEFAULT_PICKER_OPTIONS;
201
+ }
202
+ #store() {
203
+ const r = this.#opts.recents;
204
+ if (r === false)
205
+ return null;
206
+ return this.recents ?? r.store ?? this.ui?.recents ?? standaloneRecents;
207
+ }
208
+ get current() {
209
+ return this.#draft !== undefined ? this.#draft : this.value;
210
+ }
211
+ #types() {
212
+ const g = this.#opts.gradients;
213
+ const gradients = g === true || g === undefined ? GRADIENT_TYPES : g === false ? [] : g;
214
+ return this.types.filter((t) => t === "solid" || gradients.includes(t));
215
+ }
216
+ #gradientTypes() {
217
+ return this.#types().filter((t) => t !== "solid");
218
+ }
219
+ #view() {
220
+ const p = this.current;
221
+ const t = p === null ? "solid" : paintTypeOf(p);
222
+ return t !== "solid" && this.#types().includes(t) ? t : "solid";
223
+ }
224
+ #gradient() {
225
+ const p = this.current;
226
+ return p !== null && typeof p !== "string" ? p : null;
227
+ }
228
+ willUpdate(changed) {
229
+ if (changed.has("recents") || changed.has("color"))
230
+ this.#listenToRecents();
231
+ if (changed.has("value")) {
232
+ this.#accepted.draft = undefined;
233
+ if (!this.#gesture)
234
+ this.#draft = undefined;
235
+ }
236
+ const g = this.#gradient();
237
+ const n = g?.stops.length ?? 0;
238
+ if (this.#stop >= n)
239
+ this.#stop = Math.max(0, n - 1);
240
+ if (this.#stopView !== null && (this.#view() === "solid" || this.#stopView >= n)) {
241
+ if (this.#hasFocus()) {
242
+ this.#holdFocus(this.#stopView);
243
+ void this.updateComplete.then(() => {
244
+ if (this.#view() === "solid")
245
+ focusControl(this.renderRoot.querySelector("daas-solid-editor"));
246
+ else
247
+ this.renderRoot.querySelector("daas-stop-bar")?.focusHandle(this.#stop);
248
+ });
249
+ }
250
+ this.#stopView = null;
251
+ }
252
+ }
253
+ updated() {
254
+ this.#syncHandles();
255
+ }
256
+ #edit(type, edit, judged = false) {
257
+ const next = applyPaintEdit(edit, this.current, this.box);
258
+ if (type === PREVIEW && !this.#gesture) {
259
+ this.#gestureFrom = this.current;
260
+ this.#gestureMixed = this.mixed;
261
+ }
262
+ const [from, mixed] = this.#gesture ? [this.#gestureFrom, this.#gestureMixed] : [this.current, this.mixed];
263
+ if (type === COMMIT && !judged && !mixed && samePaint(next, from)) {
264
+ if (this.#gesture)
265
+ this.#cancel();
266
+ return;
267
+ }
268
+ this.#draft = next;
269
+ this.#gesture = type === PREVIEW;
270
+ if (type === COMMIT) {
271
+ this.#committed = true;
272
+ this.#sessionPaint = this.#draft;
273
+ }
274
+ this.#reverted = false;
275
+ emit(this, type, edit);
276
+ if (type === COMMIT && !this.#reverted)
277
+ this.#accepted = { draft: this.#draft, committed: true, paint: this.#sessionPaint };
278
+ this.requestUpdate();
279
+ }
280
+ #cancel() {
281
+ this.#gesture = false;
282
+ this.#draft = this.#accepted.draft;
283
+ emit(this, CANCEL);
284
+ this.requestUpdate();
285
+ }
286
+ revert() {
287
+ this.#reverted = true;
288
+ this.#gesture = false;
289
+ this.#draft = this.#accepted.draft;
290
+ this.#committed = this.#accepted.committed;
291
+ this.#sessionPaint = this.#accepted.paint;
292
+ for (const editor of this.renderRoot?.querySelectorAll("daas-solid-editor") ?? [])
293
+ editor.revert();
294
+ this.requestUpdate();
295
+ }
296
+ settle() {
297
+ if (this.#accepted.draft === undefined && (this.#draft === undefined || this.#gesture))
298
+ return;
299
+ this.#accepted.draft = undefined;
300
+ if (!this.#gesture)
301
+ this.#draft = undefined;
302
+ this.requestUpdate();
303
+ }
304
+ #relay(make) {
305
+ const pass = (type) => (e) => {
306
+ e.stopPropagation();
307
+ const edit = make(e.detail);
308
+ if (edit)
309
+ this.#edit(type, edit);
310
+ };
311
+ return {
312
+ preview: pass(PREVIEW),
313
+ commit: pass(COMMIT),
314
+ cancel: (e) => {
315
+ e.stopPropagation();
316
+ this.#cancel();
317
+ },
318
+ };
319
+ }
320
+ #stops() {
321
+ return this.#gradient()?.stops ?? [];
322
+ }
323
+ #withStop(i, change) {
324
+ return this.#stops().map((s, j) => (j === i ? { ...s, ...change } : s));
325
+ }
326
+ #removeStop(i) {
327
+ if (this.#stops().length <= 2 || !this.#stops()[i])
328
+ return;
329
+ const keyboard = this.#hasFocus();
330
+ const at = keyboard ? this.#holdFocus(i) : i;
331
+ const stops = this.#stops();
332
+ this.#edit(COMMIT, { op: "stops", stops: stops.filter((_, j) => j !== at) });
333
+ this.#stop = Math.max(0, Math.min(at, stops.length - 2));
334
+ if (keyboard)
335
+ void this.updateComplete.then(() => this.renderRoot.querySelector("daas-stop-bar")?.focusHandle(this.#stop));
336
+ }
337
+ #keepsView(p) {
338
+ const type = paintTypeOf(p);
339
+ return type === this.#view() && (typeof p === "string" || p.stops.length >= this.#stops().length);
340
+ }
341
+ #focusInReplaced(toSolid) {
342
+ const replaced = [this.renderRoot.querySelector(".body"), toSolid ? this.renderRoot.querySelector(".picker > .head > daas-select") : null];
343
+ return replaced.some((el) => !!el && focusWithin(el));
344
+ }
345
+ #hasFocus(el = this) {
346
+ for (let a = this.ownerDocument.activeElement; a; a = a.shadowRoot?.activeElement ?? null)
347
+ if (a === el)
348
+ return true;
349
+ return false;
350
+ }
351
+ #holdFocus(i) {
352
+ const frame = this.renderRoot.querySelector(".picker");
353
+ if (frame) {
354
+ frame.tabIndex = -1;
355
+ frame.setAttribute("role", "group");
356
+ frame.setAttribute("aria-label", this.t("paint.gradient"));
357
+ frame.addEventListener("blur", () => ["tabindex", "role", "aria-label"].forEach((a) => frame.removeAttribute(a)), { once: true });
358
+ this.#holding = true;
359
+ frame.focus({ preventScroll: true });
360
+ this.#holding = false;
361
+ }
362
+ const order = this.#heldOrder;
363
+ this.#heldOrder = null;
364
+ return order ? order.indexOf(i) : i;
365
+ }
366
+ #openStopView(i) {
367
+ const keyboard = this.#hasFocus();
368
+ const at = keyboard ? this.#holdFocus(i) : i;
369
+ this.#stop = at;
370
+ this.#stopView = at;
371
+ this.requestUpdate();
372
+ if (keyboard)
373
+ void this.updateComplete.then(() => focusControl(this.renderRoot.querySelector(".head daas-icon-button")));
374
+ }
375
+ #closeStopView() {
376
+ const keyboard = this.#hasFocus();
377
+ const i = keyboard && this.#stopView !== null ? this.#holdFocus(this.#stopView) : this.#stopView;
378
+ this.#stopView = null;
379
+ this.requestUpdate();
380
+ if (keyboard && i !== null)
381
+ void this.updateComplete.then(() => focusControl(this.renderRoot.querySelectorAll("[part=stop-color]")[i]));
382
+ }
383
+ #addStop() {
384
+ const stops = sortedStops(this.#stops());
385
+ let at = 0.5;
386
+ let widest = -1;
387
+ for (let i = 0; i <= stops.length; i++) {
388
+ const lo = i === 0 ? 0 : stops[i - 1].at;
389
+ const hi = i === stops.length ? 1 : stops[i].at;
390
+ if (hi - lo > widest) {
391
+ widest = hi - lo;
392
+ at = (lo + hi) / 2;
393
+ }
394
+ }
395
+ at = Math.round(at * 1000) / 1000;
396
+ const added = { at, color: stopColorAt(stops, at) };
397
+ const next = sortedStops([...stops, added]);
398
+ this.#edit(COMMIT, { op: "stops", stops: next });
399
+ this.#stop = next.indexOf(added);
400
+ }
401
+ #onKey(e) {
402
+ const origin = e.composedPath()[0];
403
+ const typing = origin instanceof HTMLElement && (origin.localName === "input" || origin.localName === "textarea" || origin.isContentEditable);
404
+ if (e.key === "Escape" && this.#stopView !== null && !e.defaultPrevented) {
405
+ e.preventDefault();
406
+ this.#closeStopView();
407
+ return;
408
+ }
409
+ if ((e.key === "Delete" || e.key === "Backspace") && !typing && this.#view() !== "solid" && this.#stopView === null) {
410
+ e.preventDefault();
411
+ this.#removeStop(this.#stop);
412
+ }
413
+ }
414
+ #renderTypeSwitch() {
415
+ const gradients = this.#gradientTypes();
416
+ if (gradients.length === 0)
417
+ return nothing;
418
+ const view = this.#view();
419
+ return html `<div class="head">
420
+ <daas-segmented label=${this.t("paint.type")} .options=${[
421
+ { value: "solid", label: this.t("paint.solid") },
422
+ { value: "gradient", label: this.t("paint.gradient") },
423
+ ]} .value=${view === "solid" ? "solid" : "gradient"} @daas-commit=${(e) => {
424
+ e.stopPropagation();
425
+ if (this.#focusInReplaced(e.detail === "solid"))
426
+ e.currentTarget.focusSegment(e.detail);
427
+ this.#edit(COMMIT, { op: "type", to: e.detail === "solid" ? "solid" : gradients[0] });
428
+ }} ></daas-segmented>
429
+ ${view !== "solid" && gradients.length > 1
430
+ ? html `<daas-select class="grow" label=${this.t("paint.gradient")} .options=${gradients.map((t) => ({ value: t, label: this.t(t === "linear" ? "paint.linear" : "paint.radial") }))} .value=${view} @daas-commit=${(e) => {
431
+ e.stopPropagation();
432
+ this.#edit(COMMIT, { op: "type", to: e.detail });
433
+ }} ></daas-select>`
434
+ : nothing}
435
+ </div>`;
436
+ }
437
+ #solidEditor(color, opts, relay) {
438
+ const o = this.#opts;
439
+ return html `<daas-solid-editor .color=${color} .mixed=${opts.mixed} .allowNone=${opts.allowNone} .alpha=${o.alpha} .formats=${o.formats} .modes=${o.modes} .palette=${o.palette} .eyedropper=${o.eyedropper} @daas-preview=${relay.preview} @daas-commit=${relay.commit} @daas-cancel=${relay.cancel} ></daas-solid-editor>`;
440
+ }
441
+ solidRelay(stop) {
442
+ return this.#relay((hex) => {
443
+ if (stop === null)
444
+ return hex === null ? { op: "none" } : { op: "color", hex };
445
+ return hex === null ? null : { op: "stops", stops: this.#withStop(stop, { color: hex }) };
446
+ });
447
+ }
448
+ #renderStopView(i) {
449
+ const s = this.#stops()[i];
450
+ return html `<div class="head">
451
+ <daas-icon-button icon="chevron-left" label=${this.t("paint.back")} @click=${() => this.#closeStopView()}></daas-icon-button>
452
+ <h3 class="grow">${this.t("paint.stop")} ${i + 1}</h3>
453
+ <span class="chip" style=${styleMap({ background: swatchBackground(s.color, false) })}></span>
454
+ </div>
455
+ ${this.#solidEditor(s.color, { allowNone: false, mixed: this.mixed }, this.solidRelay(i))}`;
456
+ }
457
+ #renderGradient(g) {
458
+ const bar = this.#relay((stops) => ({ op: "stops", stops }));
459
+ return html `<daas-stop-bar .stops=${g.stops} .selected=${this.#stop} .mixed=${this.mixed} @daas-stop=${(e) => {
460
+ e.stopPropagation();
461
+ this.#stop = e.detail;
462
+ this.requestUpdate();
463
+ }} @daas-preview=${bar.preview} @daas-commit=${bar.commit} @daas-cancel=${bar.cancel} ></daas-stop-bar>
464
+ <div class="stops">
465
+ <div class="head">
466
+ <h3 class="grow">${this.t("paint.stops")}</h3>
467
+ <daas-icon-button icon="plus" label=${this.t("paint.addStop")} @click=${() => this.#addStop()}></daas-icon-button>
468
+ </div>
469
+ ${g.stops.map((s, i) => this.#renderStopRow(s, i, g.stops.length))}
470
+ </div>
471
+ ${g.type === "linear" ? this.#renderLinear(g) : this.#renderRadial(g)}`;
472
+ }
473
+ #renderStopRow(s, i, count) {
474
+ const rgba = parseStored(s.color);
475
+ const at = this.#relay((v) => ({ op: "stops", stops: this.#withStop(i, { at: Math.min(1, Math.max(0, v / 100)) }) }));
476
+ const atCommit = (e) => {
477
+ e.stopPropagation();
478
+ const typing = this.#hasFocus(e.currentTarget);
479
+ const moved = { ...s, at: Math.min(1, Math.max(0, e.detail / 100)) };
480
+ const stops = this.#stops().map((x, j) => (j === i ? moved : x));
481
+ const order = stops.map((_, j) => j).sort((a, b) => stops[a].at - stops[b].at);
482
+ if (this.#holding)
483
+ this.#heldOrder = order;
484
+ this.#edit(COMMIT, { op: "stops", stops: order.map((j) => stops[j]) });
485
+ const to = (this.#stop = order.indexOf(i));
486
+ if (typing && to !== i)
487
+ void this.updateComplete.then(() => focusControl(this.renderRoot.querySelectorAll("[part=stop]")[to]?.querySelector("daas-number-field")));
488
+ };
489
+ const alpha = this.#relay((v) => ({ op: "stops", stops: this.#withStop(i, { color: formatHex({ ...rgba, a: v / 100 }) }) }));
490
+ return html `<div class="stop ${i === this.#stop ? "selected" : ""}" part="stop">
491
+ <daas-number-field name=${`${this.t("paint.position")} ${i + 1}`} unit="%" .value=${pct(s.at)} .mixed=${this.mixed} .min=${0} .max=${100} .precision=${1} @focusin=${() => (this.#stop = i)} @daas-preview=${at.preview} @daas-commit=${atCommit} @daas-cancel=${at.cancel} ></daas-number-field>
492
+ <button class="chip" part="stop-color" aria-label=${`${this.t("paint.stopColor")} ${i + 1}`} style=${styleMap({ background: swatchBackground(s.color, false) })} @pointerdown=${keepFocus} @mousedown=${keepFocus} @click=${() => this.#openStopView(i)} ></button>
493
+ <daas-number-field name=${`${this.t("color.alpha")} ${i + 1}`} unit="%" .value=${Math.round(rgba.a * 100)} .mixed=${this.mixed} .min=${0} .max=${100} .precision=${0} @daas-preview=${alpha.preview} @daas-commit=${alpha.commit} @daas-cancel=${alpha.cancel} ></daas-number-field>
494
+ <daas-icon-button icon="minus" label=${this.t("paint.removeStop")} ?disabled=${count <= 2} @click=${() => this.#removeStop(i)}></daas-icon-button>
495
+ </div>`;
496
+ }
497
+ #renderLinear(g) {
498
+ const angle = this.#relay((v) => ({ op: "angle", deg: normalizeDeg(v) }));
499
+ return html `<div class="geometry" part="geometry">
500
+ <daas-number-field label-icon="rotation" name=${this.t("paint.angle")} unit="°" .value=${Math.round(angleOf(g, this.box) * 10) / 10} .mixed=${this.mixed} .precision=${1} @daas-preview=${angle.preview} @daas-commit=${angle.commit} @daas-cancel=${angle.cancel} ></daas-number-field>
501
+ <daas-icon-button icon="rotate-90" label=${this.t("paint.rotate")} @click=${() => this.#edit(COMMIT, { op: "rotate90" })}></daas-icon-button>
502
+ <daas-icon-button icon="flip" label=${this.t("paint.flip")} @click=${() => this.#edit(COMMIT, { op: "flip" })}></daas-icon-button>
503
+ </div>`;
504
+ }
505
+ #renderRadial(g) {
506
+ const field = (key, name, label, value, min, max) => {
507
+ const r = this.#relay((v) => ({ op: "radial", [key]: v / 100 }));
508
+ return html `<daas-number-field label=${label} name=${name} unit="%" .value=${pct(value)} .mixed=${this.mixed} .min=${min} .max=${max} .precision=${1} @daas-preview=${r.preview} @daas-commit=${r.commit} @daas-cancel=${r.cancel} ></daas-number-field>`;
509
+ };
510
+ return html `<div class="geometry radial" part="geometry">
511
+ ${field("cx", this.t("paint.centerX"), this.t("paint.centerX.short"), g.cx ?? 0.5, 0, 100)}
512
+ ${field("cy", this.t("paint.centerY"), this.t("paint.centerY.short"), g.cy ?? 0.5, 0, 100)}
513
+ ${field("r", this.t("paint.radius"), this.t("paint.radius.short"), g.r ?? 0.5, 1, 200)}
514
+ <daas-icon-button icon="flip" label=${this.t("paint.flip")} @click=${() => this.#edit(COMMIT, { op: "flip" })}></daas-icon-button>
515
+ </div>`;
516
+ }
517
+ #renderRecents() {
518
+ const store = this.#store();
519
+ const r = this.#opts.recents;
520
+ if (!store || r === false || r.max === 0)
521
+ return nothing;
522
+ const stopView = this.#stopView;
523
+ const shown = store
524
+ .list()
525
+ .filter((p) => {
526
+ const t = paintTypeOf(p);
527
+ return t === "solid" || (stopView === null && this.#types().includes(t));
528
+ })
529
+ .slice(0, r.max);
530
+ if (shown.length === 0)
531
+ return nothing;
532
+ return html `<div class="recents" role="group" aria-label=${this.t("color.recents")} part="recents">
533
+ ${shown.map((p) => html `<button class="chip" aria-label=${typeof p === "string" ? formatHex(parseStored(p)) : this.t(p.type === "linear" ? "paint.linear" : "paint.radial")} style=${styleMap({ background: swatchBackground(p, false, this.box) })} @pointerdown=${keepFocus} @mousedown=${keepFocus} @click=${(e) => {
534
+ if (stopView === null && !this.#keepsView(p) && this.#focusInReplaced(paintTypeOf(p) === "solid"))
535
+ e.currentTarget.focus();
536
+ if (stopView === null)
537
+ this.#edit(COMMIT, { op: "paint", paint: p });
538
+ else
539
+ this.#edit(COMMIT, { op: "stops", stops: this.#withStop(stopView, { color: formatHex(parseStored(firstColor(p))) }) });
540
+ }} ></button>`)}
541
+ </div>`;
542
+ }
543
+ render() {
544
+ const view = this.#view();
545
+ const g = this.#gradient();
546
+ let body;
547
+ if (this.#stopView !== null && g)
548
+ body = this.#renderStopView(this.#stopView);
549
+ else if (view !== "solid" && g)
550
+ body = this.#renderGradient(g);
551
+ else {
552
+ const p = this.current;
553
+ body = this.#solidEditor(p === null ? null : firstColor(p), { allowNone: this.allowNone, mixed: this.mixed }, this.solidRelay(null));
554
+ }
555
+ return html `<div class="picker" part="picker" @keydown=${(e) => this.#onKey(e)}>
556
+ ${this.#stopView === null ? this.#renderTypeSwitch() : nothing}
557
+ <div class="body" style="display: flex; flex-direction: column; gap: 10px; margin-top: ${this.#stopView === null && this.#gradientTypes().length > 0 ? 10 : 0}px">${body}</div>
558
+ ${this.#renderRecents()}
559
+ </div>`;
560
+ }
561
+ }
@@ -0,0 +1,60 @@
1
+ import { UiElement } from "../element.js";
2
+ import { type ColorFormat, type ColorOptions, type PickerMode } from "../options.js";
3
+ export declare const MODE_PREF = "picker.mode";
4
+ interface EyeDropperResult {
5
+ sRGBHex: string;
6
+ }
7
+ type EyeDropperCtor = new () => {
8
+ open(options?: {
9
+ signal?: AbortSignal;
10
+ }): Promise<EyeDropperResult>;
11
+ };
12
+ export declare const eyeDropper: () => EyeDropperCtor | null;
13
+ export declare const pickerStyles: import("lit").CSSResult;
14
+ export declare class SolidEditor extends UiElement {
15
+ #private;
16
+ static styles: import("lit").CSSResult[];
17
+ static properties: {
18
+ color: {};
19
+ mixed: {
20
+ type: BooleanConstructor;
21
+ };
22
+ allowNone: {
23
+ type: BooleanConstructor;
24
+ attribute: string;
25
+ };
26
+ alpha: {
27
+ type: BooleanConstructor;
28
+ };
29
+ formats: {
30
+ attribute: boolean;
31
+ };
32
+ modes: {
33
+ attribute: boolean;
34
+ };
35
+ palette: {
36
+ attribute: boolean;
37
+ };
38
+ eyedropper: {
39
+ type: BooleanConstructor;
40
+ };
41
+ };
42
+ color: string | null;
43
+ mixed: boolean;
44
+ allowNone: boolean;
45
+ alpha: boolean;
46
+ formats: readonly ColorFormat[];
47
+ modes: readonly PickerMode[];
48
+ palette: ColorOptions["palette"];
49
+ eyedropper: boolean;
50
+ constructor();
51
+ disconnectedCallback(): void;
52
+ protected willUpdate(changed: Map<PropertyKey, unknown>): void;
53
+ get hex(): string;
54
+ get mode(): PickerMode;
55
+ get format(): ColorFormat;
56
+ abortPick(): void;
57
+ revert(): void;
58
+ protected render(): import("lit").TemplateResult<1>;
59
+ }
60
+ export {};