@chocbite/ts-lib-ui 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.
Files changed (57) hide show
  1. package/README.md +48 -2
  2. package/base/index.d.mts +3 -0
  3. package/base/index.mjs +521 -0
  4. package/base/index.mjs.map +1 -0
  5. package/canvas/index.d.mts +74 -0
  6. package/canvas/index.mjs +619 -0
  7. package/canvas/index.mjs.map +1 -0
  8. package/context-menu/index.d.mts +3 -0
  9. package/context-menu/index.mjs +2 -0
  10. package/context-menu-3OazryD0.mjs +527 -0
  11. package/context-menu-3OazryD0.mjs.map +1 -0
  12. package/document/index.d.mts +27 -0
  13. package/document/index.mjs +50 -0
  14. package/document/index.mjs.map +1 -0
  15. package/dom/index.d.mts +3 -0
  16. package/dom/index.mjs +8 -0
  17. package/dom/index.mjs.map +1 -0
  18. package/form/index.d.mts +747 -0
  19. package/form/index.mjs +2694 -0
  20. package/form/index.mjs.map +1 -0
  21. package/icons/index.d.mts +4109 -0
  22. package/icons/index.mjs +2 -0
  23. package/icons/mixins.scss +12 -0
  24. package/icons-CY4BLQ2F.mjs +4123 -0
  25. package/index-BND79jZp.d.mts +212 -0
  26. package/index-BcidwqIy.d.mts +167 -0
  27. package/index-CB3-qP6l.d.mts +120 -0
  28. package/index-CugtUzv_.d.mts +155 -0
  29. package/keyboard/index.d.mts +85 -0
  30. package/keyboard/index.mjs +1499 -0
  31. package/keyboard/index.mjs.map +1 -0
  32. package/list/index.d.mts +3 -0
  33. package/list/index.mjs +2 -0
  34. package/list-BcNSxtNm.mjs +617 -0
  35. package/list-BcNSxtNm.mjs.map +1 -0
  36. package/package.json +95 -4
  37. package/selection/index.d.mts +16 -0
  38. package/selection/index.mjs +96 -0
  39. package/selection/index.mjs.map +1 -0
  40. package/settings/index.d.mts +43 -0
  41. package/settings/index.mjs +110 -0
  42. package/settings/index.mjs.map +1 -0
  43. package/spinner/index.d.mts +10 -0
  44. package/spinner/index.mjs +39 -0
  45. package/spinner/index.mjs.map +1 -0
  46. package/style.css +2994 -0
  47. package/svg/index.d.mts +3 -0
  48. package/svg/index.mjs +2 -0
  49. package/svg-golJPaR4.mjs +358 -0
  50. package/svg-golJPaR4.mjs.map +1 -0
  51. package/theme/index.d.mts +179 -0
  52. package/theme/index.mjs +2 -0
  53. package/theme/mixins.scss +39 -0
  54. package/theme-DhIVwHQw.mjs +338 -0
  55. package/theme-DhIVwHQw.mjs.map +1 -0
  56. package/viewport/index.d.mts +1 -0
  57. package/viewport/index.mjs +2 -0
@@ -0,0 +1,1499 @@
1
+ import "../style.css";
2
+ import { DOCUMENT_HANDLER } from "../document/index.mjs";
3
+ import { Base, define_element } from "../base/index.mjs";
4
+ import { get_cursor_position, set_cursor_position } from "../selection/index.mjs";
5
+ import { g as version, r as theme_init_variable_root } from "../theme-DhIVwHQw.mjs";
6
+ import { settings_init } from "../settings/index.mjs";
7
+ import state, { state as state$1 } from "@chocbite/ts-lib/state";
8
+ import { some } from "@chocbite/ts-lib/result";
9
+ import { GREY } from "@chocbite/ts-lib/colors";
10
+ // src/keyboard/base.ts
11
+ var KeyboardBase = class extends Base {
12
+ static element_name() {
13
+ return "@abstract@";
14
+ }
15
+ static element_name_space() {
16
+ return "onscreen-keyboard";
17
+ }
18
+ elem;
19
+ constructor() {
20
+ super();
21
+ this.addEventListener("mousedown", (event) => {
22
+ if (event.target instanceof HTMLInputElement || event.target instanceof HTMLTextAreaElement || event.target instanceof HTMLSelectElement || event.target instanceof HTMLButtonElement) return;
23
+ event.preventDefault();
24
+ });
25
+ }
26
+ #listener = (e) => {
27
+ if (e.target === this || this.contains(e.target)) return;
28
+ if (this.elem && (e.target === this.elem || this.elem.contains(e.target))) return;
29
+ this.close();
30
+ };
31
+ connectedCallback() {
32
+ super.connectedCallback();
33
+ this.elem?.ownerDocument.addEventListener("pointerdown", this.#listener, { capture: true });
34
+ this.elem?.ownerDocument.addEventListener("blur", this.#listener, { capture: true });
35
+ }
36
+ disconnectedCallback() {
37
+ super.disconnectedCallback();
38
+ this.elem?.ownerDocument.removeEventListener("pointerdown", this.#listener, { capture: true });
39
+ this.elem?.ownerDocument.removeEventListener("blur", this.#listener, { capture: true });
40
+ }
41
+ open(element) {
42
+ this.elem = element;
43
+ this.elem.ownerDocument.documentElement.appendChild(this);
44
+ const erect = element.getBoundingClientRect();
45
+ const krect = this.getBoundingClientRect();
46
+ if (erect.top + erect.height > krect.top) {
47
+ element.scrollIntoView({
48
+ behavior: "instant",
49
+ block: "center"
50
+ });
51
+ const erect = element.getBoundingClientRect();
52
+ element.ownerDocument.body.style.transform = `translateY(${krect.top - (erect.top + erect.height)}px)`;
53
+ }
54
+ }
55
+ close() {
56
+ this.remove();
57
+ if (this.elem) this.elem.ownerDocument.body.style.transform = "";
58
+ }
59
+ };
60
+ // src/keyboard/types.ts
61
+ const KeyboardSpecialKeys = {
62
+ Close: "close",
63
+ Enter: "enter",
64
+ Backspace: "backspace",
65
+ Escape: "escape",
66
+ Left: "left",
67
+ Right: "right"
68
+ };
69
+ const KeyboardType = {
70
+ /**Text input type */
71
+ Text: "text",
72
+ /**Email input type */
73
+ Email: "email",
74
+ /**Color input type */
75
+ Color: "color",
76
+ /**Date input type, eg. 2026-09-22 */
77
+ Date: "date",
78
+ /**Local date and time input type, eg. 2026-09-22T14:30 */
79
+ DateTimeLocal: "datetime_local",
80
+ /**Month input type, eg. 2026-09 */
81
+ Month: "month",
82
+ /**Time input type, eg. 14:30 */
83
+ Time: "time",
84
+ /**Week input type, eg. 2026-W39 */
85
+ Week: "week",
86
+ /**Whole number input type, eg. 0, 1, 2 ...*/
87
+ NumberWhole: "number_whole",
88
+ /**Hexadecimal number input type, eg. 0A, FF, 1C3 */
89
+ NumberHex: "number_hex",
90
+ /**Integer number input type, eg. -2, -1, 0, 1, 2 ... */
91
+ NumberInteger: "number_integer",
92
+ /**Positive rational number input type, eg. 0.5, 1.2 ... */
93
+ NumberRationalPositive: "number_rational_positive",
94
+ /**Rational number input type, eg. -2.5, -1.1, 0.5, 1.2 ... */
95
+ NumberRational: "number_rational",
96
+ /**Phone number input type, eg. +1 234 567 890 */
97
+ Phone: "phone"
98
+ };
99
+ // src/keyboard/helpers.ts
100
+ function input_keyboard_type(input) {
101
+ if (input.type === "tel") return KeyboardType.Phone;
102
+ if (input.type === "email") return KeyboardType.Email;
103
+ if (input.type === "color") return KeyboardType.Color;
104
+ if (input.type === "date") return KeyboardType.Date;
105
+ if (input.type === "datetime-local") return KeyboardType.DateTimeLocal;
106
+ if (input.type === "month") return KeyboardType.Month;
107
+ if (input.type === "time") return KeyboardType.Time;
108
+ if (input.type === "week") return KeyboardType.Week;
109
+ if (input.type !== "number") return KeyboardType.Text;
110
+ const positive = input.min !== "" && Number(input.min) >= 0;
111
+ if (input.step === "any" || input.step !== "" && Number.isFinite(Number(input.step)) && !Number.isInteger(Number(input.step))) return positive ? KeyboardType.NumberRationalPositive : KeyboardType.NumberRational;
112
+ return positive ? KeyboardType.NumberWhole : KeyboardType.NumberInteger;
113
+ }
114
+ function keyboard_type(element) {
115
+ if (element instanceof HTMLTextAreaElement) return KeyboardType.Text;
116
+ if (element instanceof HTMLInputElement) {
117
+ if (element.disabled || element.readOnly || element.type === "hidden" || element.dataset.keyboardManual === "true") return void 0;
118
+ if ([
119
+ "button",
120
+ "checkbox",
121
+ "file",
122
+ "image",
123
+ "radio",
124
+ "range",
125
+ "reset",
126
+ "submit"
127
+ ].includes(element.type)) return void 0;
128
+ return input_keyboard_type(element);
129
+ }
130
+ return element.isContentEditable ? KeyboardType.Text : void 0;
131
+ }
132
+ // src/keyboard/keyboard_color.ts
133
+ function hex_to_rgb(hex) {
134
+ const value = Number.parseInt(hex.slice(1), 16);
135
+ return {
136
+ red: value >> 16 & 255,
137
+ green: value >> 8 & 255,
138
+ blue: value & 255
139
+ };
140
+ }
141
+ function rgb_to_hex({ red, green, blue }) {
142
+ const channel = (value) => value.toString(16).padStart(2, "0");
143
+ return `#${channel(red)}${channel(green)}${channel(blue)}`;
144
+ }
145
+ function rgb_to_hsv({ red, green, blue }) {
146
+ const [r, g, b] = [
147
+ red / 255,
148
+ green / 255,
149
+ blue / 255
150
+ ];
151
+ const maximum = Math.max(r, g, b);
152
+ const delta = maximum - Math.min(r, g, b);
153
+ return {
154
+ hue: delta === 0 ? 0 : ((maximum === r ? (g - b) / delta : maximum === g ? (b - r) / delta + 2 : (r - g) / delta + 4) * 60 + 360) % 360,
155
+ saturation: maximum === 0 ? 0 : delta / maximum,
156
+ value: maximum
157
+ };
158
+ }
159
+ function hsv_to_rgb({ hue, saturation, value }) {
160
+ const chroma = value * saturation;
161
+ const second = chroma * (1 - Math.abs(hue / 60 % 2 - 1));
162
+ const match = value - chroma;
163
+ const [red, green, blue] = hue < 60 ? [
164
+ chroma,
165
+ second,
166
+ 0
167
+ ] : hue < 120 ? [
168
+ second,
169
+ chroma,
170
+ 0
171
+ ] : hue < 180 ? [
172
+ 0,
173
+ chroma,
174
+ second
175
+ ] : hue < 240 ? [
176
+ 0,
177
+ second,
178
+ chroma
179
+ ] : hue < 300 ? [
180
+ second,
181
+ 0,
182
+ chroma
183
+ ] : [
184
+ chroma,
185
+ 0,
186
+ second
187
+ ];
188
+ return {
189
+ red: Math.round((red + match) * 255),
190
+ green: Math.round((green + match) * 255),
191
+ blue: Math.round((blue + match) * 255)
192
+ };
193
+ }
194
+ var Keyboard_Color = class extends KeyboardBase {
195
+ static element_name() {
196
+ return "keyboard-color";
197
+ }
198
+ #input;
199
+ #hex;
200
+ #square;
201
+ #marker;
202
+ #preview;
203
+ #sliders;
204
+ #hsv = {
205
+ hue: 0,
206
+ saturation: 0,
207
+ value: 0
208
+ };
209
+ #rgb = {
210
+ red: 0,
211
+ green: 0,
212
+ blue: 0
213
+ };
214
+ #on_input = () => this.#sync_from_input();
215
+ constructor() {
216
+ super();
217
+ const container = this.appendChild(document.createElement("div"));
218
+ container.className = "keyboard-color-content";
219
+ const picker = container.appendChild(document.createElement("div"));
220
+ picker.className = "keyboard-color-picker";
221
+ this.#square = picker.appendChild(document.createElement("div"));
222
+ this.#square.className = "keyboard-color-square";
223
+ this.#marker = this.#square.appendChild(document.createElement("div"));
224
+ this.#marker.className = "keyboard-color-marker";
225
+ const sliders = picker.appendChild(document.createElement("div"));
226
+ sliders.className = "keyboard-color-sliders";
227
+ this.#sliders = {
228
+ hue: this.#make_slider(sliders, "Hue", 359),
229
+ red: this.#make_slider(sliders, "Red", 255),
230
+ green: this.#make_slider(sliders, "Green", 255),
231
+ blue: this.#make_slider(sliders, "Blue", 255)
232
+ };
233
+ const controls = container.appendChild(document.createElement("div"));
234
+ controls.className = "keyboard-color-controls";
235
+ this.#preview = controls.appendChild(document.createElement("output"));
236
+ this.#preview.className = "keyboard-color-preview";
237
+ this.#preview.ariaLabel = "Selected color";
238
+ this.#hex = controls.appendChild(document.createElement("input"));
239
+ this.#hex.type = "text";
240
+ this.#hex.ariaLabel = "Hex color";
241
+ this.#hex.dataset.keyboardManual = "true";
242
+ this.#hex.inert = true;
243
+ this.#square.addEventListener("pointerdown", (event) => {
244
+ this.#square.setPointerCapture(event.pointerId);
245
+ this.#set_square(event);
246
+ });
247
+ this.#square.addEventListener("pointermove", (event) => {
248
+ if (this.#square.hasPointerCapture(event.pointerId)) this.#set_square(event);
249
+ });
250
+ this.#square.addEventListener("pointerup", (event) => {
251
+ if (this.#square.hasPointerCapture(event.pointerId)) {
252
+ this.#square.releasePointerCapture(event.pointerId);
253
+ this.#write_color(true);
254
+ }
255
+ });
256
+ }
257
+ #make_slider(parent, label, max) {
258
+ const container = parent.appendChild(document.createElement("label"));
259
+ container.title = label;
260
+ container.textContent = label[0];
261
+ const slider = container.appendChild(document.createElement("input"));
262
+ slider.type = "range";
263
+ slider.min = "0";
264
+ slider.max = max.toString();
265
+ slider.ariaLabel = label;
266
+ slider.addEventListener("input", () => {
267
+ if (label === "Hue") this.#hsv.hue = Number(slider.value);
268
+ else {
269
+ this.#rgb[label.toLowerCase()] = Number(slider.value);
270
+ this.#hsv = rgb_to_hsv(this.#rgb);
271
+ }
272
+ this.#write_color();
273
+ });
274
+ slider.addEventListener("change", () => this.#write_color(true));
275
+ return slider;
276
+ }
277
+ open(element) {
278
+ if (!(element instanceof HTMLInputElement) || element.type !== "color") return;
279
+ this.#input?.removeEventListener("input", this.#on_input);
280
+ this.#input = element;
281
+ this.#input.addEventListener("input", this.#on_input);
282
+ this.#sync_from_input();
283
+ super.open(element);
284
+ }
285
+ close() {
286
+ this.#input?.removeEventListener("input", this.#on_input);
287
+ this.#input = void 0;
288
+ super.close();
289
+ }
290
+ #set_square(event) {
291
+ const bounds = this.#square.getBoundingClientRect();
292
+ this.#hsv.saturation = Math.min(1, Math.max(0, (event.clientX - bounds.left) / bounds.width));
293
+ this.#hsv.value = Math.min(1, Math.max(0, 1 - (event.clientY - bounds.top) / bounds.height));
294
+ this.#write_color();
295
+ }
296
+ #write_color(commit = false) {
297
+ const input = this.#input;
298
+ if (!input) return;
299
+ this.#rgb = hsv_to_rgb(this.#hsv);
300
+ const color = rgb_to_hex(this.#rgb);
301
+ const changed = input.value !== color;
302
+ input.value = color;
303
+ this.#sync_controls(color);
304
+ if (changed) input.dispatchEvent(new Event("input", { bubbles: true }));
305
+ if (commit && changed) input.dispatchEvent(new Event("change", { bubbles: true }));
306
+ }
307
+ #sync_from_input() {
308
+ if (!this.#input) return;
309
+ this.#rgb = hex_to_rgb(this.#input.value);
310
+ this.#hsv = rgb_to_hsv(this.#rgb);
311
+ this.#sync_controls(this.#input.value);
312
+ }
313
+ #sync_controls(color) {
314
+ this.#square.style.setProperty("--keyboard-color-hue", `hsl(${this.#hsv.hue} 100% 50%)`);
315
+ this.#marker.style.left = `${this.#hsv.saturation * 100}%`;
316
+ this.#marker.style.top = `${(1 - this.#hsv.value) * 100}%`;
317
+ this.#sliders.hue.value = Math.round(this.#hsv.hue).toString();
318
+ this.#sliders.red.value = this.#rgb.red.toString();
319
+ this.#sliders.green.value = this.#rgb.green.toString();
320
+ this.#sliders.blue.value = this.#rgb.blue.toString();
321
+ this.#hex.value = color;
322
+ this.#preview.style.backgroundColor = color;
323
+ }
324
+ };
325
+ define_element(Keyboard_Color);
326
+ // src/keyboard/keyboard_datetime.ts
327
+ const MONTHS = [
328
+ "January",
329
+ "February",
330
+ "March",
331
+ "April",
332
+ "May",
333
+ "June",
334
+ "July",
335
+ "August",
336
+ "September",
337
+ "October",
338
+ "November",
339
+ "December"
340
+ ];
341
+ const WEEKDAYS = [
342
+ "Sun",
343
+ "Mon",
344
+ "Tue",
345
+ "Wed",
346
+ "Thu",
347
+ "Fri",
348
+ "Sat"
349
+ ];
350
+ const DATE_TYPES = [
351
+ "date",
352
+ "datetime-local",
353
+ "month",
354
+ "time",
355
+ "week"
356
+ ];
357
+ function date_value(date) {
358
+ return `${date.getFullYear().toString().padStart(4, "0")}-${(date.getMonth() + 1).toString().padStart(2, "0")}-${date.getDate().toString().padStart(2, "0")}`;
359
+ }
360
+ function date_from_value(value) {
361
+ const match = /^(\d{4})-(\d{2})-(\d{2})/.exec(value);
362
+ if (!match) return void 0;
363
+ const date = new Date(Number(match[1]), Number(match[2]) - 1, Number(match[3]));
364
+ return date_value(date) === match[0] ? date : void 0;
365
+ }
366
+ function week_value(date) {
367
+ const utc = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
368
+ utc.setUTCDate(utc.getUTCDate() + 4 - (utc.getUTCDay() || 7));
369
+ const year = utc.getUTCFullYear();
370
+ const start = new Date(Date.UTC(year, 0, 1));
371
+ return `${year}-W${Math.ceil(((utc.getTime() - start.getTime()) / 864e5 + 1) / 7).toString().padStart(2, "0")}`;
372
+ }
373
+ var Keyboard_DateTime = class extends KeyboardBase {
374
+ #input;
375
+ #view = /* @__PURE__ */ new Date();
376
+ #changed = false;
377
+ #hour_group = 0;
378
+ #minute_group = 0;
379
+ #hour = 0;
380
+ #minute = 0;
381
+ #selected_datetime_date;
382
+ static element_name() {
383
+ return "keyboard-datetime";
384
+ }
385
+ static element_name_space() {
386
+ return "onscreen-keyboard";
387
+ }
388
+ constructor() {
389
+ super();
390
+ this.classList.add("keyboard-datetime");
391
+ this.addEventListener("mousedown", (event) => event.preventDefault());
392
+ }
393
+ open(element) {
394
+ if (!(element instanceof HTMLInputElement) || !DATE_TYPES.includes(element.type)) return;
395
+ this.#input = element;
396
+ this.#view = date_from_value(element.value) ?? /* @__PURE__ */ new Date();
397
+ this.#view.setDate(1);
398
+ this.#selected_datetime_date = element.type === "datetime-local" ? date_from_value(element.value) : void 0;
399
+ const time = element.value.match(/T?(\d{2}):(\d{2})/) ?? [];
400
+ this.#hour = Number(time[1] ?? 0);
401
+ this.#minute = Number(time[2] ?? 0);
402
+ this.#hour_group = Math.floor(this.#hour / 4);
403
+ this.#minute_group = Math.floor(this.#minute / 10);
404
+ this.#changed = false;
405
+ this.#render();
406
+ super.open(element);
407
+ }
408
+ close() {
409
+ if (this.elem && this.#changed) this.elem.dispatchEvent(new Event("change", { bubbles: true }));
410
+ this.#input = void 0;
411
+ this.#selected_datetime_date = void 0;
412
+ this.#changed = false;
413
+ super.close();
414
+ }
415
+ #render() {
416
+ const container = document.createElement("div");
417
+ const type = this.#input?.type;
418
+ if (type === "datetime-local") container.className = "keyboard-datetime-local";
419
+ if (type === "month") container.appendChild(this.#month_picker());
420
+ else if (type === "time") container.appendChild(this.#time_picker());
421
+ else {
422
+ container.appendChild(type === "week" ? this.#week_picker() : this.#calendar());
423
+ if (type === "datetime-local") container.appendChild(this.#time_picker());
424
+ }
425
+ this.replaceChildren(container);
426
+ }
427
+ #calendar() {
428
+ const picker = document.createElement("div");
429
+ const header = document.createElement("div");
430
+ header.className = "keyboard-datetime-header";
431
+ header.append(this.#button("‹", () => this.#move_month(-1)), Object.assign(document.createElement("strong"), { textContent: `${MONTHS[this.#view.getMonth()]} ${this.#view.getFullYear()}` }), this.#button("›", () => this.#move_month(1)), this.#button("🗙", () => this.close()));
432
+ const calendar = document.createElement("div");
433
+ calendar.className = "keyboard-datetime-calendar";
434
+ WEEKDAYS.forEach((day) => calendar.append(Object.assign(document.createElement("span"), { textContent: day })));
435
+ const first = new Date(this.#view.getFullYear(), this.#view.getMonth(), 1);
436
+ const start = new Date(first);
437
+ start.setDate(1 - first.getDay());
438
+ for (let index = 0; index < 42; index += 1) {
439
+ const date = new Date(start);
440
+ date.setDate(start.getDate() + index);
441
+ const value = this.#input?.type === "week" ? week_value(date) : this.#input?.type === "datetime-local" ? `${date_value(date)}T${this.#time_value()}` : date_value(date);
442
+ const button = this.#button(date.getDate().toString(), () => this.#select_date(date));
443
+ button.disabled = !this.#value_allowed(value);
444
+ if (date.getMonth() !== this.#view.getMonth()) button.classList.add("outside");
445
+ if (date_value(date) === this.#selected_date_value()) button.classList.add("selected");
446
+ calendar.append(button);
447
+ }
448
+ picker.append(header, calendar);
449
+ return picker;
450
+ }
451
+ #month_picker() {
452
+ const picker = document.createElement("div");
453
+ const header = document.createElement("div");
454
+ header.className = "keyboard-datetime-header";
455
+ header.append(this.#button("‹", () => this.#move_year(-1)), Object.assign(document.createElement("strong"), { textContent: this.#view.getFullYear().toString() }), this.#button("›", () => this.#move_year(1)), this.#button("🗙", () => this.close()));
456
+ const months = document.createElement("div");
457
+ months.className = "keyboard-datetime-months";
458
+ MONTHS.forEach((month, index) => {
459
+ const value = `${this.#view.getFullYear()}-${(index + 1).toString().padStart(2, "0")}`;
460
+ const button = this.#button(month.slice(0, 3), () => {
461
+ this.#set_value(value);
462
+ this.close();
463
+ });
464
+ button.disabled = !this.#value_allowed(value);
465
+ if (value === this.#input?.value) button.classList.add("selected");
466
+ months.append(button);
467
+ });
468
+ picker.append(header, months);
469
+ return picker;
470
+ }
471
+ #week_picker() {
472
+ const picker = document.createElement("div");
473
+ const header = document.createElement("div");
474
+ header.className = "keyboard-datetime-header";
475
+ header.append(this.#button("‹", () => this.#move_month(-1)), Object.assign(document.createElement("strong"), { textContent: `${MONTHS[this.#view.getMonth()]} ${this.#view.getFullYear()}` }), this.#button("›", () => this.#move_month(1)), this.#button("🗙", () => this.close()));
476
+ const weeks = document.createElement("div");
477
+ weeks.className = "keyboard-datetime-weeks";
478
+ const first = new Date(this.#view.getFullYear(), this.#view.getMonth(), 1);
479
+ const start = new Date(first);
480
+ start.setDate(first.getDate() - (first.getDay() + 6) % 7);
481
+ for (let index = 0; index < 6; index += 1) {
482
+ const monday = new Date(start);
483
+ monday.setDate(start.getDate() + index * 7);
484
+ const value = week_value(monday);
485
+ const row = document.createElement("div");
486
+ row.className = "keyboard-datetime-week";
487
+ const button = this.#button(`W${value.slice(-2)}`, () => {
488
+ this.#set_value(value);
489
+ this.close();
490
+ });
491
+ button.disabled = !this.#value_allowed(value);
492
+ if (value === this.#input?.value) button.classList.add("selected");
493
+ const dates = document.createElement("div");
494
+ dates.className = "keyboard-datetime-week-dates";
495
+ for (let day = 0; day < 7; day += 1) {
496
+ const date = new Date(monday);
497
+ date.setDate(monday.getDate() + day);
498
+ const label = document.createElement("span");
499
+ label.textContent = date.getDate().toString();
500
+ if (date.getMonth() !== this.#view.getMonth()) label.classList.add("outside");
501
+ dates.append(label);
502
+ }
503
+ row.append(button, dates);
504
+ weeks.append(row);
505
+ }
506
+ picker.append(header, weeks);
507
+ return picker;
508
+ }
509
+ #time_picker() {
510
+ const controls = document.createElement("div");
511
+ controls.className = "keyboard-datetime-time";
512
+ const hour_start = this.#hour_group * 4;
513
+ const minute_start = this.#minute_group * 10;
514
+ controls.append(this.#time_navigation(() => this.#move_time_group("hour", -1), `${hour_start.toString().padStart(2, "0")}-${(hour_start + 3).toString().padStart(2, "0")}`, () => this.#move_time_group("hour", 1)), this.#time_choices(hour_start, 4, this.#hour, (hour) => {
515
+ this.#hour = hour;
516
+ this.#render();
517
+ }), this.#time_navigation(() => this.#move_time_group("minute", -1), `${minute_start.toString().padStart(2, "0")}-${Math.min(minute_start + 9, 59).toString().padStart(2, "0")}`, () => this.#move_time_group("minute", 1)), this.#time_choices(minute_start, 10, this.#minute, (minute) => {
518
+ this.#minute = minute;
519
+ this.#render();
520
+ }, "minutes"), this.#time_footer());
521
+ return controls;
522
+ }
523
+ #time_navigation(previous, label, next) {
524
+ const navigation = document.createElement("div");
525
+ navigation.className = "keyboard-datetime-time-navigation";
526
+ navigation.append(this.#button("‹", previous), Object.assign(document.createElement("strong"), { textContent: label }), this.#button("›", next));
527
+ return navigation;
528
+ }
529
+ #time_choices(start, count, selected, select, class_name) {
530
+ const choices = document.createElement("div");
531
+ choices.className = "keyboard-datetime-time-choices";
532
+ if (class_name) choices.classList.add(class_name);
533
+ for (let offset = 0; offset < count; offset += 1) {
534
+ const value = start + offset;
535
+ const button = this.#button(value.toString().padStart(2, "0"), () => select(value));
536
+ if (value === selected) button.classList.add("selected");
537
+ choices.append(button);
538
+ }
539
+ return choices;
540
+ }
541
+ #time_footer() {
542
+ const footer = document.createElement("div");
543
+ footer.className = "keyboard-datetime-time-footer";
544
+ footer.append(this.#button("Submit", () => {
545
+ this.#set_time();
546
+ this.close();
547
+ }), this.#button("🗙", () => this.close()));
548
+ return footer;
549
+ }
550
+ #move_time_group(part, offset) {
551
+ const next = Math.max(0, Math.min(part === "hour" ? 5 : 5, (part === "hour" ? this.#hour_group : this.#minute_group) + offset));
552
+ if (part === "hour") this.#hour_group = next;
553
+ else this.#minute_group = next;
554
+ this.#render();
555
+ }
556
+ #set_time() {
557
+ const time = this.#time_value();
558
+ if (this.#input?.type === "time") this.#set_value(time);
559
+ else this.#set_value(`${date_value(this.#selected_date())}T${time}`);
560
+ }
561
+ #time_value() {
562
+ return `${this.#hour.toString().padStart(2, "0")}:${this.#minute.toString().padStart(2, "0")}`;
563
+ }
564
+ #selected_date() {
565
+ return this.#selected_datetime_date ?? date_from_value(this.#input?.value ?? "") ?? new Date(this.#view);
566
+ }
567
+ #selected_date_value() {
568
+ return this.#input?.type === "datetime-local" ? this.#selected_datetime_date && date_value(this.#selected_datetime_date) : date_from_value(this.#input?.value ?? "") && this.#input?.value.slice(0, 10);
569
+ }
570
+ #button(text, click) {
571
+ const button = document.createElement("button");
572
+ button.type = "button";
573
+ button.textContent = text;
574
+ button.addEventListener("click", click);
575
+ return button;
576
+ }
577
+ #move_month(offset) {
578
+ this.#view.setMonth(this.#view.getMonth() + offset, 1);
579
+ this.#render();
580
+ }
581
+ #move_year(offset) {
582
+ this.#view.setFullYear(this.#view.getFullYear() + offset, 0, 1);
583
+ this.#render();
584
+ }
585
+ #select_date(date) {
586
+ const type = this.#input?.type;
587
+ if (type === "week") {
588
+ this.#set_value(week_value(date));
589
+ return this.close();
590
+ }
591
+ const value = type === "datetime-local" ? `${date_value(date)}T${this.#input?.value.match(/T(\d{2}:\d{2})/)?.[1] ?? "00:00"}` : date_value(date);
592
+ if (type === "datetime-local") {
593
+ this.#selected_datetime_date = date;
594
+ this.#render();
595
+ return;
596
+ }
597
+ this.#set_value(value);
598
+ if (type === "date") this.close();
599
+ }
600
+ #value_allowed(value) {
601
+ const input = this.#input;
602
+ if (!input) return false;
603
+ const candidate = input.ownerDocument.createElement("input");
604
+ candidate.type = input.type;
605
+ candidate.min = input.min;
606
+ candidate.max = input.max;
607
+ candidate.step = input.step;
608
+ candidate.value = value;
609
+ return candidate.value === value && candidate.checkValidity();
610
+ }
611
+ #set_value(value) {
612
+ const input = this.#input;
613
+ if (!input || !this.#value_allowed(value) || input.value === value) return;
614
+ input.value = value;
615
+ this.#changed = true;
616
+ input.dispatchEvent(new InputEvent("input", { bubbles: true }));
617
+ }
618
+ };
619
+ define_element(Keyboard_DateTime);
620
+ const DATETIME_KEYBOARD = new Keyboard_DateTime();
621
+ // src/keyboard/shared.ts
622
+ const colors = theme_init_variable_root("keyboard", "Virtual Keyboard", "Theme variables for the virtual keyboard").make_sub_group("colors", "Colors", "Colors used in the virtual keyboard");
623
+ colors.make_variable("background", "Background Color", "", GREY["50"], GREY["900"], "Color", void 0);
624
+ colors.make_variable("keytext", "Key Text Color", "", GREY["800"], GREY["200"], "Color", void 0);
625
+ colors.make_variable("border", "Border Color", "", GREY["900"], GREY["50"], "Color", void 0);
626
+ colors.make_variable("shadow", "Shadow Color", "", GREY["900"] + "88", GREY["50"] + "FF", "Color", void 0);
627
+ colors.make_variable("active", "Pressed Color", "Color of the key when pressed", GREY["400"], GREY["700"], "Color", void 0);
628
+ // src/keyboard/keyboard_keys.ts
629
+ var KeyboardKey = class extends Base {
630
+ static element_name() {
631
+ return "key";
632
+ }
633
+ static element_name_space() {
634
+ return "onscreen-keyboard";
635
+ }
636
+ constructor(keyboard, text, key, ri, col_count, rows, cols) {
637
+ super();
638
+ this.textContent = text;
639
+ this.style.gridRow = (ri + 1).toString();
640
+ this.style.gridColumn = (col_count + 1).toString();
641
+ this.style.gridRowEnd = `${ri + 1 + rows}`;
642
+ this.style.gridColumnEnd = `${col_count + 1 + cols}`;
643
+ this.onclick = () => keyboard.execute_key(key);
644
+ if (key.includes("layer")) {
645
+ const layer_name = key.split(":")[1];
646
+ this.attach_state_ROA_to_attribute_map("active", keyboard.layer, (v) => some(v === layer_name ? "true" : "false"));
647
+ }
648
+ }
649
+ };
650
+ define_element(KeyboardKey);
651
+ var KeyboardLayer = class extends Base {
652
+ static element_name() {
653
+ return "layer";
654
+ }
655
+ static element_name_space() {
656
+ return "onscreen-keyboard";
657
+ }
658
+ constructor(keyboard, config) {
659
+ super();
660
+ if (config.max_width) this.style.maxWidth = config.max_width + "rem";
661
+ this.addEventListener("mousedown", (e) => e.preventDefault());
662
+ let highest_col_count = 0;
663
+ let highest_row = 0;
664
+ config.keys.forEach((r, i) => {
665
+ let col_count = 0;
666
+ let highest_row_in_layer = 0;
667
+ const ri = i * (config.key_rows ?? 1);
668
+ r.forEach((k) => {
669
+ if (typeof k === "number") {
670
+ col_count += k;
671
+ return;
672
+ }
673
+ const { text, rows = config.key_rows ?? 1, cols = config.key_cols ?? 1 } = k;
674
+ this.appendChild(new KeyboardKey(keyboard, text, k.key ?? text, ri, col_count, rows, cols));
675
+ col_count += cols;
676
+ highest_row_in_layer = Math.max(highest_row_in_layer, ri + rows);
677
+ });
678
+ highest_col_count = Math.max(highest_col_count, col_count);
679
+ highest_row = Math.max(highest_row, highest_row_in_layer);
680
+ });
681
+ this.style.gridTemplateColumns = "repeat(" + highest_col_count + ", 1fr)";
682
+ const row_height = 3 / (config.key_rows ?? 1);
683
+ const row_max = 40 / highest_row;
684
+ this.style.gridTemplateRows = "repeat(" + highest_row + ", min(" + row_height + "rem, " + row_max + "vh))";
685
+ }
686
+ };
687
+ define_element(KeyboardLayer);
688
+ const KEO = {
689
+ ctrlKey: true,
690
+ shiftKey: false,
691
+ altKey: false,
692
+ metaKey: false,
693
+ repeat: false,
694
+ bubbles: true,
695
+ cancelable: true
696
+ };
697
+ const IEO = { bubbles: true };
698
+ var Keyboard_Keys = class extends KeyboardBase {
699
+ static element_name() {
700
+ return "keyboard-keys";
701
+ }
702
+ #layer = state.ok_w("default");
703
+ layer = this.#layer.read_only;
704
+ #changed = false;
705
+ constructor(config) {
706
+ super();
707
+ const container = this.appendChild(document.createElement("div"));
708
+ this.addEventListener("mousedown", (e) => e.preventDefault());
709
+ this.classList.add("keyboard");
710
+ const layer_map = {};
711
+ [["default", config.default], ...config.layers ? Object.entries(config.layers) : []].forEach((l) => {
712
+ const [layer_name, layer_config] = l;
713
+ const layer = new KeyboardLayer(this, layer_config);
714
+ layer_map[layer_name] = layer;
715
+ });
716
+ this.attach_state(this.#layer, (v) => {
717
+ container.replaceChildren(layer_map[v.value] ?? layer_map["default"]);
718
+ });
719
+ }
720
+ open(element) {
721
+ super.open(element);
722
+ this.#changed = false;
723
+ this.#layer.write("default");
724
+ }
725
+ close() {
726
+ this.#dispatch_change();
727
+ super.close();
728
+ }
729
+ #dispatch_change() {
730
+ if (this.elem && this.#changed) this.elem.dispatchEvent(new Event("change", { bubbles: true }));
731
+ this.#changed = false;
732
+ }
733
+ #dis_keydown(opts) {
734
+ return this.elem?.dispatchEvent(new KeyboardEvent("keydown", {
735
+ ...KEO,
736
+ ...opts
737
+ })) ?? false;
738
+ }
739
+ #dis_keypress(opts) {
740
+ return this.elem?.dispatchEvent(new KeyboardEvent("keypress", {
741
+ ...KEO,
742
+ ...opts
743
+ })) ?? false;
744
+ }
745
+ #dis_keyup(opts) {
746
+ return this.elem?.dispatchEvent(new KeyboardEvent("keyup", {
747
+ ...KEO,
748
+ ...opts
749
+ })) ?? false;
750
+ }
751
+ #dis_input(opts) {
752
+ return this.elem?.dispatchEvent(new InputEvent("input", {
753
+ ...IEO,
754
+ cancelable: false,
755
+ ...opts
756
+ })) ?? false;
757
+ }
758
+ #dis_beforeinput(opts) {
759
+ return this.elem?.dispatchEvent(new InputEvent("beforeinput", {
760
+ ...IEO,
761
+ cancelable: true,
762
+ ...opts
763
+ })) ?? false;
764
+ }
765
+ #edit_value(data, delete_backward = false) {
766
+ const element = this.elem;
767
+ if (!element) return false;
768
+ if (element.tagName === "INPUT" || element.tagName === "TEXTAREA") {
769
+ const input = element;
770
+ const start = input.selectionStart ?? 0;
771
+ const end = input.selectionEnd ?? 0;
772
+ if (start === null || end === null) return false;
773
+ const replace_start = delete_backward && start === end ? start - 1 : start;
774
+ if (replace_start < 0) return false;
775
+ input.setRangeText(data, replace_start, end, "end");
776
+ return true;
777
+ }
778
+ if (!element.isContentEditable) return false;
779
+ const selection = element.ownerDocument.getSelection();
780
+ if (!selection?.rangeCount) return false;
781
+ let range = selection.getRangeAt(0);
782
+ if (!element.contains(range.commonAncestorContainer)) return false;
783
+ if (delete_backward && range.collapsed) {
784
+ selection.modify("extend", "backward", "character");
785
+ if (!selection.rangeCount) return false;
786
+ range = selection.getRangeAt(0);
787
+ if (range.collapsed) return false;
788
+ }
789
+ if (delete_backward || !range.collapsed) range.deleteContents();
790
+ if (data) {
791
+ const text = element.ownerDocument.createTextNode(data);
792
+ range.insertNode(text);
793
+ range.setStartAfter(text);
794
+ }
795
+ range.collapse(true);
796
+ selection.removeAllRanges();
797
+ selection.addRange(range);
798
+ return true;
799
+ }
800
+ execute_key(key) {
801
+ if (!this.elem) return this.close();
802
+ if (key.length === 0) return this.close();
803
+ if (key.includes("layer")) {
804
+ const layer_name = key.split(":")[1];
805
+ if (layer_name === this.#layer.ok()) this.#layer.write("default");
806
+ else this.#layer.write(layer_name);
807
+ return;
808
+ }
809
+ if (key === KeyboardSpecialKeys.Close) return this.close();
810
+ const opts = {
811
+ [KeyboardSpecialKeys.Backspace]: {
812
+ key: "Backspace",
813
+ code: "Backspace"
814
+ },
815
+ [KeyboardSpecialKeys.Enter]: {
816
+ key: "Enter",
817
+ code: "Enter"
818
+ },
819
+ [KeyboardSpecialKeys.Escape]: {
820
+ key: "Escape",
821
+ code: "Escape"
822
+ },
823
+ [KeyboardSpecialKeys.Left]: {
824
+ key: "ArrowLeft",
825
+ code: "ArrowLeft"
826
+ },
827
+ [KeyboardSpecialKeys.Right]: {
828
+ key: "ArrowRight",
829
+ code: "ArrowRight"
830
+ }
831
+ }[key] ?? {
832
+ key,
833
+ code: key === " " ? "Space" : "Key" + key.toUpperCase()
834
+ };
835
+ const keydown = this.#dis_keydown(opts);
836
+ if (keydown && key.length === 1) {
837
+ this.#dis_keypress(opts);
838
+ if (this.#dis_beforeinput({
839
+ data: key,
840
+ inputType: "insertText"
841
+ }) && this.#edit_value(key)) {
842
+ this.#changed = true;
843
+ this.#dis_input({
844
+ data: key,
845
+ inputType: "insertText"
846
+ });
847
+ }
848
+ } else if (keydown && key === KeyboardSpecialKeys.Backspace && this.#dis_beforeinput({
849
+ data: null,
850
+ inputType: "deleteContentBackward"
851
+ }) && this.#edit_value("", true)) {
852
+ this.#changed = true;
853
+ this.#dis_input({
854
+ data: null,
855
+ inputType: "deleteContentBackward"
856
+ });
857
+ } else if (keydown && key === KeyboardSpecialKeys.Enter && this.elem.tagName !== "INPUT" && this.#dis_beforeinput({
858
+ data: "\n",
859
+ inputType: "insertLineBreak"
860
+ })) {
861
+ if (this.#edit_value("\n")) {
862
+ this.#changed = true;
863
+ this.#dis_input({
864
+ data: "\n",
865
+ inputType: "insertLineBreak"
866
+ });
867
+ }
868
+ } else if (keydown && key === KeyboardSpecialKeys.Left) set_cursor_position(this.elem, get_cursor_position(this.elem) - 1);
869
+ else if (keydown && key === KeyboardSpecialKeys.Right) set_cursor_position(this.elem, get_cursor_position(this.elem) + 1);
870
+ this.#dis_keyup(opts);
871
+ if (key === KeyboardSpecialKeys.Enter) this.#dispatch_change();
872
+ }
873
+ };
874
+ define_element(Keyboard_Keys);
875
+ // src/keyboard/number.ts
876
+ const NUMBER_RATIONAL_KEYBOARD = new Keyboard_Keys({ default: {
877
+ max_width: 24,
878
+ keys: [
879
+ [..."789-".split("").map((n) => ({ text: n })), {
880
+ text: "⌫",
881
+ key: KeyboardSpecialKeys.Backspace
882
+ }],
883
+ [..."456.".split("").map((n) => ({ text: n })), {
884
+ text: "Esc",
885
+ key: KeyboardSpecialKeys.Escape
886
+ }],
887
+ [..."123".split("").map((n) => ({ text: n })), {
888
+ text: "⏎",
889
+ key: KeyboardSpecialKeys.Enter,
890
+ cols: 2
891
+ }],
892
+ [
893
+ {
894
+ text: "0",
895
+ cols: 2
896
+ },
897
+ {
898
+ text: "🡸",
899
+ key: KeyboardSpecialKeys.Left
900
+ },
901
+ {
902
+ text: "🡺",
903
+ key: KeyboardSpecialKeys.Right
904
+ },
905
+ {
906
+ text: "🗙",
907
+ key: KeyboardSpecialKeys.Close
908
+ }
909
+ ]
910
+ ]
911
+ } });
912
+ const NUMBER_WHOLE_KEYBOARD = new Keyboard_Keys({ default: {
913
+ max_width: 24,
914
+ keys: [
915
+ [..."789".split("").map((n) => ({ text: n })), {
916
+ text: "⌫",
917
+ key: KeyboardSpecialKeys.Backspace,
918
+ cols: 2
919
+ }],
920
+ [..."456".split("").map((n) => ({ text: n })), {
921
+ text: "Esc",
922
+ key: KeyboardSpecialKeys.Escape,
923
+ cols: 2
924
+ }],
925
+ [..."123".split("").map((n) => ({ text: n })), {
926
+ text: "⏎",
927
+ key: KeyboardSpecialKeys.Enter,
928
+ cols: 2
929
+ }],
930
+ [
931
+ {
932
+ text: "0",
933
+ cols: 2
934
+ },
935
+ {
936
+ text: "🡸",
937
+ key: KeyboardSpecialKeys.Left
938
+ },
939
+ {
940
+ text: "🡺",
941
+ key: KeyboardSpecialKeys.Right
942
+ },
943
+ {
944
+ text: "🗙",
945
+ key: KeyboardSpecialKeys.Close
946
+ }
947
+ ]
948
+ ]
949
+ } });
950
+ const NUMBER_HEX_KEYBOARD = new Keyboard_Keys({ default: {
951
+ max_width: 24,
952
+ keys: [
953
+ [..."789A".split("").map((n) => ({ text: n })), {
954
+ text: "⌫",
955
+ key: KeyboardSpecialKeys.Backspace
956
+ }],
957
+ [..."456B".split("").map((n) => ({ text: n })), {
958
+ text: "Esc",
959
+ key: KeyboardSpecialKeys.Escape
960
+ }],
961
+ [..."123C".split("").map((n) => ({ text: n })), {
962
+ text: "⏎",
963
+ key: KeyboardSpecialKeys.Enter
964
+ }],
965
+ [
966
+ {
967
+ text: "0",
968
+ cols: 2
969
+ },
970
+ { text: "D" },
971
+ { text: "E" },
972
+ { text: "F" },
973
+ {
974
+ text: "🗙",
975
+ key: KeyboardSpecialKeys.Close
976
+ }
977
+ ]
978
+ ]
979
+ } });
980
+ const NUMBER_INTEGER_KEYBOARD = new Keyboard_Keys({ default: {
981
+ max_width: 24,
982
+ keys: [
983
+ [..."789-".split("").map((n) => ({ text: n })), {
984
+ text: "⌫",
985
+ key: KeyboardSpecialKeys.Backspace
986
+ }],
987
+ [..."456".split("").map((n) => ({ text: n })), {
988
+ text: "Esc",
989
+ key: KeyboardSpecialKeys.Escape,
990
+ cols: 2
991
+ }],
992
+ [..."123".split("").map((n) => ({ text: n })), {
993
+ text: "⏎",
994
+ key: KeyboardSpecialKeys.Enter,
995
+ cols: 2
996
+ }],
997
+ [
998
+ {
999
+ text: "0",
1000
+ cols: 2
1001
+ },
1002
+ {
1003
+ text: "🡸",
1004
+ key: KeyboardSpecialKeys.Left
1005
+ },
1006
+ {
1007
+ text: "🡺",
1008
+ key: KeyboardSpecialKeys.Right
1009
+ },
1010
+ {
1011
+ text: "🗙",
1012
+ key: KeyboardSpecialKeys.Close
1013
+ }
1014
+ ]
1015
+ ]
1016
+ } });
1017
+ const NUMBER_RATIONAL_POSITIVE_KEYBOARD = new Keyboard_Keys({ default: {
1018
+ max_width: 24,
1019
+ keys: [
1020
+ [..."789".split("").map((n) => ({ text: n })), {
1021
+ text: "⌫",
1022
+ key: KeyboardSpecialKeys.Backspace,
1023
+ cols: 2
1024
+ }],
1025
+ [..."456.".split("").map((n) => ({ text: n })), {
1026
+ text: "Esc",
1027
+ key: KeyboardSpecialKeys.Escape
1028
+ }],
1029
+ [..."123".split("").map((n) => ({ text: n })), {
1030
+ text: "⏎",
1031
+ key: KeyboardSpecialKeys.Enter,
1032
+ cols: 2
1033
+ }],
1034
+ [
1035
+ {
1036
+ text: "0",
1037
+ cols: 2
1038
+ },
1039
+ {
1040
+ text: "🡸",
1041
+ key: KeyboardSpecialKeys.Left
1042
+ },
1043
+ {
1044
+ text: "🡺",
1045
+ key: KeyboardSpecialKeys.Right
1046
+ },
1047
+ {
1048
+ text: "🗙",
1049
+ key: KeyboardSpecialKeys.Close
1050
+ }
1051
+ ]
1052
+ ]
1053
+ } });
1054
+ const PHONE_KEYBOARD = new Keyboard_Keys({ default: {
1055
+ max_width: 24,
1056
+ keys: [
1057
+ [
1058
+ ..."789".split("").map((n) => ({ text: n })),
1059
+ 1,
1060
+ {
1061
+ text: "⌫",
1062
+ key: KeyboardSpecialKeys.Backspace
1063
+ }
1064
+ ],
1065
+ [
1066
+ ..."456".split("").map((n) => ({ text: n })),
1067
+ {
1068
+ text: "+",
1069
+ rows: 2
1070
+ },
1071
+ {
1072
+ text: "Esc",
1073
+ key: KeyboardSpecialKeys.Escape
1074
+ }
1075
+ ],
1076
+ [
1077
+ ..."123".split("").map((n) => ({ text: n })),
1078
+ 1,
1079
+ {
1080
+ text: "⏎",
1081
+ key: KeyboardSpecialKeys.Enter,
1082
+ cols: 1
1083
+ }
1084
+ ],
1085
+ [
1086
+ {
1087
+ text: "0",
1088
+ cols: 2
1089
+ },
1090
+ {
1091
+ text: "🡸",
1092
+ key: KeyboardSpecialKeys.Left
1093
+ },
1094
+ {
1095
+ text: "🡺",
1096
+ key: KeyboardSpecialKeys.Right
1097
+ },
1098
+ {
1099
+ text: "🗙",
1100
+ key: KeyboardSpecialKeys.Close
1101
+ }
1102
+ ]
1103
+ ]
1104
+ } });
1105
+ // src/keyboard/settings.ts
1106
+ const SETTINGS = settings_init("@chocbite/ts-lib-keyboard", version, "Keyboard", "Settings for virtual keyboard");
1107
+ const THEME_ID = "virtual_keyboard";
1108
+ const PRIVATE_VIRTUAL_KEYBOARD = state$1.rosw(SETTINGS.get(THEME_ID, false), true);
1109
+ SETTINGS.register(THEME_ID, "Virtual Keyboard", "Enable or disable the virtual keyboard", PRIVATE_VIRTUAL_KEYBOARD);
1110
+ const VIRTUAL_KEYBOARD = PRIVATE_VIRTUAL_KEYBOARD.read_write;
1111
+ // src/keyboard/text.ts
1112
+ const TEXT_KEYBOARD = new Keyboard_Keys({
1113
+ default: {
1114
+ max_width: 60,
1115
+ key_cols: 4,
1116
+ keys: [
1117
+ [..."1234567890".split("").map((n) => ({ text: n })), {
1118
+ text: "⌫",
1119
+ key: KeyboardSpecialKeys.Backspace
1120
+ }],
1121
+ [2, ..."qwertyuiop".split("").map((n) => ({ text: n }))],
1122
+ [
1123
+ {
1124
+ text: "Esc",
1125
+ key: KeyboardSpecialKeys.Escape,
1126
+ cols: 3
1127
+ },
1128
+ ..."asdfghjkl".split("").map((n) => ({ text: n })),
1129
+ {
1130
+ text: "⏎",
1131
+ key: KeyboardSpecialKeys.Enter,
1132
+ cols: 5
1133
+ }
1134
+ ],
1135
+ [{
1136
+ text: "⇧",
1137
+ key: "layer:shift",
1138
+ cols: 5
1139
+ }, ..."zxcvbnm,.".split("").map((n) => ({ text: n }))],
1140
+ [
1141
+ {
1142
+ text: "Sym",
1143
+ key: "layer:sym",
1144
+ cols: 5
1145
+ },
1146
+ 6,
1147
+ {
1148
+ text: "Space",
1149
+ key: " ",
1150
+ cols: 20
1151
+ },
1152
+ {
1153
+ text: "🡸",
1154
+ key: KeyboardSpecialKeys.Left
1155
+ },
1156
+ {
1157
+ text: "🡺",
1158
+ key: KeyboardSpecialKeys.Right
1159
+ },
1160
+ {
1161
+ text: "🗙",
1162
+ key: KeyboardSpecialKeys.Close,
1163
+ cols: 5
1164
+ }
1165
+ ]
1166
+ ]
1167
+ },
1168
+ layers: {
1169
+ shift: {
1170
+ max_width: 60,
1171
+ key_cols: 4,
1172
+ keys: [
1173
+ [..."1234567890".split("").map((n) => ({ text: n })), {
1174
+ text: "⌫",
1175
+ key: KeyboardSpecialKeys.Backspace
1176
+ }],
1177
+ [2, ..."QWERTYUIOP".split("").map((n) => ({ text: n }))],
1178
+ [
1179
+ {
1180
+ text: "Esc",
1181
+ key: KeyboardSpecialKeys.Escape,
1182
+ cols: 3
1183
+ },
1184
+ ..."ASDFGHJKL".split("").map((n) => ({ text: n })),
1185
+ {
1186
+ text: "⏎",
1187
+ key: KeyboardSpecialKeys.Enter,
1188
+ cols: 5
1189
+ }
1190
+ ],
1191
+ [{
1192
+ text: "⇧",
1193
+ key: "layer:shift",
1194
+ cols: 5
1195
+ }, ..."ZXCVBNM;:".split("").map((n) => ({ text: n }))],
1196
+ [
1197
+ {
1198
+ text: "Sym",
1199
+ key: "layer:sym",
1200
+ cols: 5
1201
+ },
1202
+ 6,
1203
+ {
1204
+ text: "Space",
1205
+ key: " ",
1206
+ cols: 20
1207
+ },
1208
+ {
1209
+ text: "🡸",
1210
+ key: KeyboardSpecialKeys.Left
1211
+ },
1212
+ {
1213
+ text: "🡺",
1214
+ key: KeyboardSpecialKeys.Right
1215
+ },
1216
+ {
1217
+ text: "🗙",
1218
+ key: KeyboardSpecialKeys.Close,
1219
+ cols: 5
1220
+ }
1221
+ ]
1222
+ ]
1223
+ },
1224
+ sym: {
1225
+ max_width: 60,
1226
+ key_cols: 4,
1227
+ keys: [
1228
+ [..."!\"#¤%&/()=".split("").map((n) => ({ text: n })), {
1229
+ text: "⌫",
1230
+ key: KeyboardSpecialKeys.Backspace
1231
+ }],
1232
+ [2, ..."+@£$€?{[]}".split("").map((n) => ({ text: n }))],
1233
+ [
1234
+ {
1235
+ text: "Esc",
1236
+ key: KeyboardSpecialKeys.Escape,
1237
+ cols: 3
1238
+ },
1239
+ ..."-_½§¨^~'*".split("").map((n) => ({ text: n })),
1240
+ {
1241
+ text: "⏎",
1242
+ key: KeyboardSpecialKeys.Enter,
1243
+ cols: 5
1244
+ }
1245
+ ],
1246
+ [{
1247
+ text: "⇧",
1248
+ key: "layer:shift",
1249
+ cols: 5
1250
+ }, ..."<>|\\°`¶".split("").map((n) => ({ text: n }))],
1251
+ [
1252
+ {
1253
+ text: "Sym",
1254
+ key: "layer:sym",
1255
+ cols: 5
1256
+ },
1257
+ 6,
1258
+ {
1259
+ text: "Space",
1260
+ key: " ",
1261
+ cols: 20
1262
+ },
1263
+ {
1264
+ text: "🡸",
1265
+ key: KeyboardSpecialKeys.Left
1266
+ },
1267
+ {
1268
+ text: "🡺",
1269
+ key: KeyboardSpecialKeys.Right
1270
+ },
1271
+ {
1272
+ text: "🗙",
1273
+ key: KeyboardSpecialKeys.Close,
1274
+ cols: 5
1275
+ }
1276
+ ]
1277
+ ]
1278
+ }
1279
+ }
1280
+ });
1281
+ const EMAIL_KEYBOARD = new Keyboard_Keys({
1282
+ default: {
1283
+ max_width: 60,
1284
+ key_cols: 4,
1285
+ keys: [
1286
+ [..."1234567890".split("").map((n) => ({ text: n })), {
1287
+ text: "⌫",
1288
+ key: KeyboardSpecialKeys.Backspace
1289
+ }],
1290
+ [2, ..."qwertyuiop".split("").map((n) => ({ text: n }))],
1291
+ [
1292
+ {
1293
+ text: "Esc",
1294
+ key: KeyboardSpecialKeys.Escape,
1295
+ cols: 3
1296
+ },
1297
+ ..."asdfghjkl".split("").map((n) => ({ text: n })),
1298
+ {
1299
+ text: "⏎",
1300
+ key: KeyboardSpecialKeys.Enter,
1301
+ cols: 5
1302
+ }
1303
+ ],
1304
+ [{
1305
+ text: "⇧",
1306
+ key: "layer:shift",
1307
+ cols: 5
1308
+ }, ..."zxcvbnm,.".split("").map((n) => ({ text: n }))],
1309
+ [
1310
+ {
1311
+ text: "Sym",
1312
+ key: "layer:sym",
1313
+ cols: 5
1314
+ },
1315
+ 2,
1316
+ { text: "@" },
1317
+ {
1318
+ text: "Space",
1319
+ key: " ",
1320
+ cols: 20
1321
+ },
1322
+ {
1323
+ text: "🡸",
1324
+ key: KeyboardSpecialKeys.Left
1325
+ },
1326
+ {
1327
+ text: "🡺",
1328
+ key: KeyboardSpecialKeys.Right
1329
+ },
1330
+ {
1331
+ text: "🗙",
1332
+ key: KeyboardSpecialKeys.Close,
1333
+ cols: 5
1334
+ }
1335
+ ]
1336
+ ]
1337
+ },
1338
+ layers: {
1339
+ shift: {
1340
+ max_width: 60,
1341
+ key_cols: 4,
1342
+ keys: [
1343
+ [..."1234567890@".split("").map((n) => ({ text: n })), {
1344
+ text: "⌫",
1345
+ key: KeyboardSpecialKeys.Backspace
1346
+ }],
1347
+ [2, ..."QWERTYUIOP".split("").map((n) => ({ text: n }))],
1348
+ [
1349
+ {
1350
+ text: "Esc",
1351
+ key: KeyboardSpecialKeys.Escape,
1352
+ cols: 3
1353
+ },
1354
+ ..."ASDFGHJKL".split("").map((n) => ({ text: n })),
1355
+ {
1356
+ text: "⏎",
1357
+ key: KeyboardSpecialKeys.Enter,
1358
+ cols: 5
1359
+ }
1360
+ ],
1361
+ [{
1362
+ text: "⇧",
1363
+ key: "layer:shift",
1364
+ cols: 5
1365
+ }, ..."ZXCVBNM;:".split("").map((n) => ({ text: n }))],
1366
+ [
1367
+ {
1368
+ text: "Sym",
1369
+ key: "layer:sym",
1370
+ cols: 5
1371
+ },
1372
+ 6,
1373
+ {
1374
+ text: "Space",
1375
+ key: " ",
1376
+ cols: 20
1377
+ },
1378
+ {
1379
+ text: "🡸",
1380
+ key: KeyboardSpecialKeys.Left
1381
+ },
1382
+ {
1383
+ text: "🡺",
1384
+ key: KeyboardSpecialKeys.Right
1385
+ },
1386
+ {
1387
+ text: "🗙",
1388
+ key: KeyboardSpecialKeys.Close,
1389
+ cols: 5
1390
+ }
1391
+ ]
1392
+ ]
1393
+ },
1394
+ sym: {
1395
+ max_width: 60,
1396
+ key_cols: 4,
1397
+ keys: [
1398
+ [..."!\"#¤%&/()=".split("").map((n) => ({ text: n })), {
1399
+ text: "⌫",
1400
+ key: KeyboardSpecialKeys.Backspace
1401
+ }],
1402
+ [2, ..."+@£$€?{[]}".split("").map((n) => ({ text: n }))],
1403
+ [
1404
+ {
1405
+ text: "Esc",
1406
+ key: KeyboardSpecialKeys.Escape,
1407
+ cols: 3
1408
+ },
1409
+ ..."-_½§¨^~'*".split("").map((n) => ({ text: n })),
1410
+ {
1411
+ text: "⏎",
1412
+ key: KeyboardSpecialKeys.Enter,
1413
+ cols: 5
1414
+ }
1415
+ ],
1416
+ [{
1417
+ text: "⇧",
1418
+ key: "layer:shift",
1419
+ cols: 5
1420
+ }, ..."<>|\\°`¶".split("").map((n) => ({ text: n }))],
1421
+ [
1422
+ {
1423
+ text: "Sym",
1424
+ key: "layer:sym",
1425
+ cols: 5
1426
+ },
1427
+ 6,
1428
+ {
1429
+ text: "Space",
1430
+ key: " ",
1431
+ cols: 20
1432
+ },
1433
+ {
1434
+ text: "🡸",
1435
+ key: KeyboardSpecialKeys.Left
1436
+ },
1437
+ {
1438
+ text: "🡺",
1439
+ key: KeyboardSpecialKeys.Right
1440
+ },
1441
+ {
1442
+ text: "🗙",
1443
+ key: KeyboardSpecialKeys.Close,
1444
+ cols: 5
1445
+ }
1446
+ ]
1447
+ ]
1448
+ }
1449
+ }
1450
+ });
1451
+ // src/keyboard/index.ts
1452
+ const KEYBOARDS = {
1453
+ [KeyboardType.Text]: TEXT_KEYBOARD,
1454
+ [KeyboardType.Email]: EMAIL_KEYBOARD,
1455
+ [KeyboardType.Color]: new Keyboard_Color(),
1456
+ [KeyboardType.Date]: DATETIME_KEYBOARD,
1457
+ [KeyboardType.DateTimeLocal]: DATETIME_KEYBOARD,
1458
+ [KeyboardType.Month]: DATETIME_KEYBOARD,
1459
+ [KeyboardType.Time]: DATETIME_KEYBOARD,
1460
+ [KeyboardType.Week]: DATETIME_KEYBOARD,
1461
+ [KeyboardType.NumberWhole]: NUMBER_WHOLE_KEYBOARD,
1462
+ [KeyboardType.NumberHex]: NUMBER_HEX_KEYBOARD,
1463
+ [KeyboardType.NumberInteger]: NUMBER_INTEGER_KEYBOARD,
1464
+ [KeyboardType.NumberRationalPositive]: NUMBER_RATIONAL_POSITIVE_KEYBOARD,
1465
+ [KeyboardType.NumberRational]: NUMBER_RATIONAL_KEYBOARD,
1466
+ [KeyboardType.Phone]: PHONE_KEYBOARD
1467
+ };
1468
+ /**Opens the on-screen keyboard for the specified element with the given mode and returns a function to close it
1469
+ * @param element The HTML element to attach the keyboard to.
1470
+ * @param mode The keyboard mode, which can be a predefined type, a custom keyboard instance, or a keyboard configuration.
1471
+ * @returns A function that closes the keyboard when called. */
1472
+ function keyboard_open(element, mode) {
1473
+ const keyboard = mode instanceof KeyboardBase ? mode : typeof mode === "object" ? new Keyboard_Keys(mode) : KEYBOARDS[mode];
1474
+ if (!keyboard) return () => {};
1475
+ keyboard.open(element);
1476
+ return () => {
1477
+ keyboard.close();
1478
+ };
1479
+ }
1480
+ /** Attaches automatic virtual-keyboard handling to a document and returns a cleanup function. */
1481
+ const on_focus = (event) => {
1482
+ const type = keyboard_type(event.target);
1483
+ if (event.target instanceof HTMLElement && type) keyboard_open(event.target, type);
1484
+ };
1485
+ VIRTUAL_KEYBOARD.sub((v) => {
1486
+ if (v.value) DOCUMENT_HANDLER.for_documents((doc) => doc.addEventListener("focusin", on_focus));
1487
+ else DOCUMENT_HANDLER.for_documents((doc) => doc.removeEventListener("focusin", on_focus));
1488
+ }, true);
1489
+ DOCUMENT_HANDLER.events.on("added", (e) => {
1490
+ if (VIRTUAL_KEYBOARD.ok()) e.data.addEventListener("focusin", on_focus);
1491
+ else e.data.removeEventListener("focusin", on_focus);
1492
+ });
1493
+ /** Creates a new on-screen keyboard instance with the specified configuration */
1494
+ function keyboard_create(config) {
1495
+ return new Keyboard_Keys(config);
1496
+ }
1497
+ export { KeyboardType, VIRTUAL_KEYBOARD, keyboard_create, keyboard_open };
1498
+
1499
+ //# sourceMappingURL=index.mjs.map