@chocbite/ts-lib-ui 1.0.1 → 1.0.3

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 (56) hide show
  1. package/README.md +16 -0
  2. package/base/index.d.mts +2 -2
  3. package/base/index.mjs +69 -77
  4. package/base/index.mjs.map +1 -1
  5. package/canvas/index.d.mts +3 -1
  6. package/canvas/index.mjs +51 -53
  7. package/canvas/index.mjs.map +1 -1
  8. package/context-menu/index.d.mts +1 -1
  9. package/context-menu/index.mjs +1 -1
  10. package/{context-menu-3OazryD0.mjs → context-menu-_9dMpC-H.mjs} +71 -37
  11. package/context-menu-_9dMpC-H.mjs.map +1 -0
  12. package/document/index.mjs +3 -1
  13. package/document/index.mjs.map +1 -1
  14. package/form/index.d.mts +16 -5
  15. package/form/index.mjs +410 -151
  16. package/form/index.mjs.map +1 -1
  17. package/icons/index.d.mts +1 -1
  18. package/icons/index.mjs +1 -1
  19. package/icons-e2J_Ojic.mjs +4124 -0
  20. package/{index-BcidwqIy.d.mts → index-BDWg5GX2.d.mts} +7 -18
  21. package/{index-BND79jZp.d.mts → index-CvzH3M_5.d.mts} +16 -9
  22. package/{index-CugtUzv_.d.mts → index-CzpbJoAj.d.mts} +19 -10
  23. package/{index-CB3-qP6l.d.mts → index-DhRb2iA8.d.mts} +7 -5
  24. package/keyboard/index.d.mts +1 -1
  25. package/keyboard/index.mjs +121 -50
  26. package/keyboard/index.mjs.map +1 -1
  27. package/list/index.d.mts +1 -1
  28. package/list/index.mjs +1 -1
  29. package/{list-BcNSxtNm.mjs → list-Bkm9Ailw.mjs} +98 -110
  30. package/list-Bkm9Ailw.mjs.map +1 -0
  31. package/package.json +7 -2
  32. package/selection/index.mjs +9 -11
  33. package/selection/index.mjs.map +1 -1
  34. package/settings/index.d.mts +23 -5
  35. package/settings/index.mjs +17 -10
  36. package/settings/index.mjs.map +1 -1
  37. package/spinner/index.d.mts +1 -1
  38. package/spinner/index.mjs +2 -2
  39. package/state/index.d.mts +36 -0
  40. package/state/index.mjs +2 -0
  41. package/state-OCzEmq3j.mjs +87 -0
  42. package/state-OCzEmq3j.mjs.map +1 -0
  43. package/style.css +14 -7
  44. package/svg/index.d.mts +1 -1
  45. package/svg/index.mjs +1 -1
  46. package/{svg-golJPaR4.mjs → svg-1bp-yIPC.mjs} +20 -17
  47. package/svg-1bp-yIPC.mjs.map +1 -0
  48. package/theme/index.d.mts +21 -2
  49. package/theme/index.mjs +1 -1
  50. package/{theme-DhIVwHQw.mjs → theme-DlD5P5tY.mjs} +125 -44
  51. package/theme-DlD5P5tY.mjs.map +1 -0
  52. package/context-menu-3OazryD0.mjs.map +0 -1
  53. package/icons-CY4BLQ2F.mjs +0 -4123
  54. package/list-BcNSxtNm.mjs.map +0 -1
  55. package/svg-golJPaR4.mjs.map +0 -1
  56. package/theme-DhIVwHQw.mjs.map +0 -1
@@ -46,8 +46,8 @@ declare function circle(center_x: number, center_y: number, radius: number): SVG
46
46
  * @param center_x the center point on the x axis
47
47
  * @param center_y the center point on the y axis
48
48
  * @param radius radius in x axis
49
- * @param start_angle start angle in radians
50
- * @param end_angle end anglein radians*/
49
+ * @param start_angle start angle in degrees
50
+ * @param end_angle end angle in degrees, a sweep of 360 degrees or more draws the whole circle*/
51
51
  declare function circle_arc(center_x: number, center_y: number, radius: number, start_angle: number, end_angle: number): SVGAttributes<SVGPathElement>;
52
52
  /**This creates a svg ellipse
53
53
  * @param center_x x coordinate of center
@@ -60,8 +60,8 @@ declare function ellipse(center_x: number, center_y: number, radius_x: number, r
60
60
  * @param center_y the center point on the y axis
61
61
  * @param radius_x radius in x axis
62
62
  * @param radius_y radius in y axis
63
- * @param start_angle start angle in radians
64
- * @param end_angle end anglein radians*/
63
+ * @param start_angle start angle in degrees
64
+ * @param end_angle end angle in degrees, a sweep of 360 degrees or more draws the whole ellipse*/
65
65
  declare function ellipse_arc(center_x: number, center_y: number, radius_x: number, radius_y: number, start_angle: number, end_angle: number): SVGAttributes<SVGPathElement>;
66
66
  /**This draws a triangle*/
67
67
  declare function group(): SVGAttributes<SVGGElement>;
@@ -87,18 +87,7 @@ declare function rectangle_from_center(center_x: number, center_y: number, width
87
87
  * @param height height
88
88
  * @param corner_radius radius of corner*/
89
89
  declare function rectangle_from_corner(start_x: number, start_y: number, width: number, height: number, corner_radius: number): SVGAttributes<SVGRectElement>;
90
- type SVGElements = {
91
- ellipse: SVGEllipseElement;
92
- circle: SVGCircleElement;
93
- path: SVGPathElement;
94
- line: SVGLineElement;
95
- rect: SVGRectElement;
96
- text: SVGTextElement;
97
- g: SVGGElement;
98
- svg: SVGSVGElement;
99
- foreignObject: SVGForeignObjectElement;
100
- };
101
- declare function create_svg_element<K extends keyof SVGElements>(name: K): SVGAttributes<SVGElements[K]>;
90
+ declare function create_svg_element<K extends keyof SVGElementTagNameMap>(name: K): SVGAttributes<SVGElementTagNameMap[K]>;
102
91
  /** This returns an empty svg element
103
92
  * @param width width of svg
104
93
  * @param height height of svg
@@ -154,7 +143,7 @@ declare const svg: {
154
143
  text: typeof text;
155
144
  multi_line_text: typeof multi_line_text;
156
145
  isosceles_triangle: typeof isosceles_triangle;
157
- angle_to_anchor_point: (angle: number) => 0 | 2 | 1 | 3 | 4 | 5 | 6 | 7;
146
+ angle_to_anchor_point: (angle: number) => 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;
158
147
  };
159
148
  type SVGFunc = () => SVGSVGElement;
160
149
  declare global {
@@ -164,4 +153,4 @@ declare global {
164
153
  }
165
154
  }
166
155
  export { svg as n, SVGAnchorPoint as r, SVGFunc as t };
167
- //# sourceMappingURL=index-BcidwqIy.d.mts.map
156
+ //# sourceMappingURL=index-BDWg5GX2.d.mts.map
@@ -1,5 +1,5 @@
1
1
  import './style.css';
2
- import { t as SVGFunc } from "./index-BcidwqIy.mjs";
2
+ import { t as SVGFunc } from "./index-BDWg5GX2.mjs";
3
3
  import { State, StateInferSub, StateREA, StateROA, StateSub } from "@chocbite/ts-lib/state";
4
4
  import { Option } from "@chocbite/ts-lib/result";
5
5
  /**Enum of possible access types for base element*/
@@ -28,22 +28,27 @@ declare const ACCESSTYPESINFO: {
28
28
  };
29
29
  };
30
30
  type BaseIntersectObserverOptions = {
31
- /**Deffers updating visible state for so many milliseconds
31
+ /**Deffers updating visible state for so many milliseconds, defaults to 250
32
32
  * this will also batch updates if they are constantly coming*/
33
33
  deffered_visible?: number;
34
- /**Deffers updating visible state for so many milliseconds
34
+ /**Deffers updating visible state for so many milliseconds, defaults to 250
35
35
  * this will also batch updates if they are constantly coming*/
36
36
  deffered_hidden?: number;
37
37
  } & IntersectionObserverInit;
38
38
  declare class BaseIntersectObserver extends IntersectionObserver {
39
39
  #private;
40
40
  constructor(options: BaseIntersectObserverOptions);
41
+ /**Also drops the element from pending deferred updates, so it does not change visibility after it is unobserved*/
42
+ unobserve(target: Element): void;
41
43
  }
42
44
  type DataProps<T> = { [K in keyof T as T[K] extends Function ? never : K]: T[K]; };
43
45
  type WithStateROA<T> = { [K in keyof T]?: T[K] | StateROA<T[K]>; };
44
46
  /**Shared class for elements to extend
45
47
  * All none abstract elements must use the define_element function to declare itself
46
48
  *
49
+ * Elements are only created in code, with new or a factory function, HTML is not supported,
50
+ * constructors add children and attributes, which document.createElement and the HTML parser do not allow
51
+ *
47
52
  * All none abstracts classes must override the static method element_name to return the name of the element
48
53
  * Abstract classes should return @abstract@
49
54
  *
@@ -171,11 +176,14 @@ declare abstract class Base extends HTMLElement {
171
176
  /**Detaches the state from the property */
172
177
  detach_state_from_attribute(qualified_name: string): this;
173
178
  }
174
- interface LoopOptions<T, N extends Node> {
179
+ interface LoopOptions<T, N extends ChildNode> {
180
+ /**Updates a node to a new value, when given nodes are reused instead of replaced*/
175
181
  change?: (element: N, new_value: T) => void;
182
+ /**Called for every node that is removed or replaced*/
176
183
  destructor?: (element: N) => void;
177
184
  }
178
- declare class Loop<T, N extends Node> extends Base {
185
+ /**Element with one child node per value of an array, follows the changes of array states*/
186
+ declare class Loop<T, N extends ChildNode> extends Base {
179
187
  #private;
180
188
  static element_name(): string;
181
189
  static element_name_space(): string;
@@ -183,8 +191,7 @@ declare class Loop<T, N extends Node> extends Base {
183
191
  set array(array: readonly T[]);
184
192
  set error(err: Node);
185
193
  }
186
- declare function base_loop<T, N extends Node>(generator: (val: T) => N, options?: LoopOptions<T, N>): Loop<T, N>;
187
- declare function base_loop_state(): void;
194
+ declare function base_loop<T, N extends ChildNode>(generator: (val: T) => N, options?: LoopOptions<T, N>): Loop<T, N>;
188
195
  /**Shared svg class for elements to extend
189
196
  * See Base for more information on how to define elements, and the element lifecycle */
190
197
  declare abstract class BaseSVG extends Base {
@@ -208,5 +215,5 @@ declare const base_element_name: (element: ElementConstructor) => string;
208
215
  declare const ELEMENT_LIST: Set<string>;
209
216
  /**Defines elements inheriting from the base*/
210
217
  declare const define_element: (element: ElementConstructor) => void;
211
- export { validate_element_name as a, base_loop as c, BaseIntersectObserver as d, BaseIntersectObserverOptions as f, set_base_element_global_namespace as i, base_loop_state as l, AccessTypes as m, base_element_name as n, BaseSVG as o, ACCESSTYPESINFO as p, define_element as r, Loop as s, ELEMENT_LIST as t, Base as u };
212
- //# sourceMappingURL=index-BND79jZp.d.mts.map
218
+ export { validate_element_name as a, base_loop as c, BaseIntersectObserverOptions as d, ACCESSTYPESINFO as f, set_base_element_global_namespace as i, Base as l, base_element_name as n, BaseSVG as o, AccessTypes as p, define_element as r, Loop as s, ELEMENT_LIST as t, BaseIntersectObserver as u };
219
+ //# sourceMappingURL=index-CvzH3M_5.d.mts.map
@@ -1,7 +1,7 @@
1
1
  import './style.css';
2
- import { d as BaseIntersectObserver, u as Base } from "./index-BND79jZp.mjs";
3
- import { t as SVGFunc } from "./index-BcidwqIy.mjs";
4
- import { n as ContextMenu } from "./index-CB3-qP6l.mjs";
2
+ import { l as Base, u as BaseIntersectObserver } from "./index-CvzH3M_5.mjs";
3
+ import { t as SVGFunc } from "./index-BDWg5GX2.mjs";
4
+ import { n as ContextMenu } from "./index-DhRb2iA8.mjs";
5
5
  import { State } from "@chocbite/ts-lib/state";
6
6
  import { Option } from "@chocbite/ts-lib/result";
7
7
  declare abstract class ListField extends Base {
@@ -44,8 +44,10 @@ declare class ListRow<R, T extends {}, A extends ListType<R>, S extends boolean>
44
44
  static element_name(): string;
45
45
  static element_name_space(): string;
46
46
  readonly depth: number;
47
- state: A;
47
+ state: ListParentRows<A>;
48
48
  constructor(root: ListRoot<R, T, A, S>, parent: ListRowParent<A>, data: R);
49
+ /**Regenerates the fields of the row and its sub rows after the columns changed */
50
+ columns_changed(): void;
49
51
  select_adjacent(direction: "next" | "previous" | "p_next" | "p_previous" | "last", field: Option<number>): void;
50
52
  set data(data: R);
51
53
  set add_row(options: ListAddRowOptions | undefined);
@@ -59,6 +61,8 @@ declare class ListRow<R, T extends {}, A extends ListType<R>, S extends boolean>
59
61
  }
60
62
  type ListType<R> = R[] | State<R[]>;
61
63
  type ListTypeExtract<A extends ListType<any>> = A extends (infer U)[] ? U : A extends State<(infer U)[]> ? U : never;
64
+ /**Rows of a row parent, the top level rows or sub rows, which can be an array or a state regardless of the top level rows */
65
+ type ListParentRows<A extends ListType<any>> = A | ListType<ListTypeExtract<A>>;
62
66
  declare const ListDataType: {
63
67
  readonly number: "number";
64
68
  readonly string: "string";
@@ -70,7 +74,8 @@ interface ListColumnOptions<V, C extends ListField> {
70
74
  init_width?: number;
71
75
  /**If the column has a fixed width in rem, overrides init_width and disables resizing*/
72
76
  fixed_width?: number | "min" | "max";
73
- /**Initial order of the column, lower numbers appear first, will order by column object if undefined*/
77
+ /**Initial order of the column, lower numbers appear first, will order by column object if undefined
78
+ * A column without order uses its position in the column object as order*/
74
79
  order?: number;
75
80
  /**Whether the column is reorderable by the user*/
76
81
  reorderable?: boolean;
@@ -90,8 +95,12 @@ declare function column_string(
90
95
  /**Column title*/
91
96
  title: string): ListColumnOptions<string, ListTextField>;
92
97
  type ListRowTransformer<R, T extends {}, A extends ListType<any>, S extends boolean> = (
93
- /**Test */
94
- data: R, row: ListRow<R, T, A, S>, state: A) => ListRowOptions<R, T, S>;
98
+ /**Data of the row */
99
+ data: R,
100
+ /**The row the options are for */
101
+ row: ListRow<R, T, A, S>,
102
+ /**Rows the data is from, sub rows can come from an array or a state regardless of the top level rows */
103
+ state: S extends true ? A | ListType<R> : A) => ListRowOptions<R, T, S>;
95
104
  interface ListRoot<R, T extends {}, A extends ListType<any>, S extends boolean> {
96
105
  /**Sub rows */
97
106
  readonly sub_rows: S;
@@ -111,8 +120,8 @@ interface ListRowParent<A extends ListType<any>> {
111
120
  open: boolean;
112
121
  /**Selects the adjacent row in the given direction */
113
122
  select_adjacent(direction: "next" | "previous" | "p_next" | "p_previous" | "last", field: Option<number>): void;
114
- /**State associated with the row parent, if state is used */
115
- state: A;
123
+ /**Rows of the parent, the rows of the container or the sub rows of a row */
124
+ state: ListParentRows<A>;
116
125
  }
117
126
  interface ListAddRowOptions {
118
127
  text: string | State<string>;
@@ -152,4 +161,4 @@ declare const list: {
152
161
  node_field: typeof node_field;
153
162
  };
154
163
  export { ListField as n, list as t };
155
- //# sourceMappingURL=index-CugtUzv_.d.mts.map
164
+ //# sourceMappingURL=index-CzpbJoAj.d.mts.map
@@ -1,5 +1,5 @@
1
1
  import './style.css';
2
- import { u as Base } from "./index-BND79jZp.mjs";
2
+ import { l as Base } from "./index-CvzH3M_5.mjs";
3
3
  import { Option } from "@chocbite/ts-lib/result";
4
4
  interface ContextMenuLine {}
5
5
  declare abstract class MenuLine extends Base implements ContextMenuLine {
@@ -8,7 +8,9 @@ declare abstract class MenuLine extends Base implements ContextMenuLine {
8
8
  /**Returns the namespace override for the element*/
9
9
  static element_name_space(): string;
10
10
  abstract do_focus(dir?: boolean): void;
11
- /**Changes focus to the next line
11
+ /**True for lines that can not take focus, like dividers, focus moves past them*/
12
+ get skips_focus(): boolean;
13
+ /**Changes focus to the next line, wrapping around and skipping lines that can not take focus
12
14
  * @param direction false is next sibling, true is previous */
13
15
  focus_next(direction?: boolean): void;
14
16
  }
@@ -100,9 +102,9 @@ declare function context_menu_detach(element: Element): void;
100
102
  * @param dont_cover when set true */
101
103
  declare function context_menu_summon(menu: ContextMenu, element?: Element, x?: number, y?: number, dont_cover?: boolean): void;
102
104
  /**Sets the default context menu for the page, the one used if no other context menu has been attached to the element
103
- * If set to a boolean the operating system context menu is disabled and nothing will appear
105
+ * If set to false the operating system context menu is disabled and nothing will appear
104
106
  * If set undefined the operating systems context menu will be used*/
105
- declare function context_menu_default(lines: (ContextMenu | (() => Option<ContextMenu>)) | false): void;
107
+ declare function context_menu_default(lines: ContextMenu | (() => Option<ContextMenu>) | false | undefined): void;
106
108
  interface ContextMenuOption extends ContextMenuLine {}
107
109
  declare function context_line(text: string, func: () => void, icon?: SVGSVGElement, shortcut?: string, checkmark?: boolean): ContextMenuOption;
108
110
  declare const ctm: {
@@ -117,4 +119,4 @@ declare const ctm: {
117
119
  devider: typeof context_devider;
118
120
  };
119
121
  export { ContextMenu as n, ContextMenuLines as r, ctm as t };
120
- //# sourceMappingURL=index-CB3-qP6l.d.mts.map
122
+ //# sourceMappingURL=index-DhRb2iA8.d.mts.map
@@ -1,5 +1,5 @@
1
1
  import '../style.css';
2
- import { u as Base } from "../index-BND79jZp.mjs";
2
+ import { l as Base } from "../index-CvzH3M_5.mjs";
3
3
  declare abstract class KeyboardBase extends Base {
4
4
  #private;
5
5
  static element_name(): string;
@@ -2,13 +2,13 @@ import "../style.css";
2
2
  import { DOCUMENT_HANDLER } from "../document/index.mjs";
3
3
  import { Base, define_element } from "../base/index.mjs";
4
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";
5
+ import { _ as version, i as theme_init_variable_root } from "../theme-DlD5P5tY.mjs";
6
6
  import { settings_init } from "../settings/index.mjs";
7
7
  import state, { state as state$1 } from "@chocbite/ts-lib/state";
8
8
  import { some } from "@chocbite/ts-lib/result";
9
9
  import { GREY } from "@chocbite/ts-lib/colors";
10
10
  // src/keyboard/base.ts
11
- var KeyboardBase = class extends Base {
11
+ var KeyboardBase = class KeyboardBase extends Base {
12
12
  static element_name() {
13
13
  return "@abstract@";
14
14
  }
@@ -23,22 +23,33 @@ var KeyboardBase = class extends Base {
23
23
  event.preventDefault();
24
24
  });
25
25
  }
26
+ /**Closes on a press or focus outside the keyboard and the edited element*/
26
27
  #listener = (e) => {
27
28
  if (e.target === this || this.contains(e.target)) return;
28
29
  if (this.elem && (e.target === this.elem || this.elem.contains(e.target))) return;
29
30
  this.close();
30
31
  };
32
+ /**The document the listeners are on, the element can move to another document*/
33
+ #listening;
31
34
  connectedCallback() {
32
35
  super.connectedCallback();
33
- this.elem?.ownerDocument.addEventListener("pointerdown", this.#listener, { capture: true });
34
- this.elem?.ownerDocument.addEventListener("blur", this.#listener, { capture: true });
36
+ this.#listening = this.ownerDocument;
37
+ this.#listening.addEventListener("pointerdown", this.#listener, { capture: true });
38
+ this.#listening.addEventListener("focusin", this.#listener, { capture: true });
35
39
  }
36
40
  disconnectedCallback() {
37
41
  super.disconnectedCallback();
38
- this.elem?.ownerDocument.removeEventListener("pointerdown", this.#listener, { capture: true });
39
- this.elem?.ownerDocument.removeEventListener("blur", this.#listener, { capture: true });
42
+ this.#listening?.removeEventListener("pointerdown", this.#listener, { capture: true });
43
+ this.#listening?.removeEventListener("focusin", this.#listener, { capture: true });
44
+ this.#listening = void 0;
40
45
  }
46
+ /**The page's own body transform, kept while the page is shifted so it can be restored*/
47
+ #body_transform;
48
+ /**Only one keyboard is open at a time, focus can move to another input without a pointer*/
49
+ static #open;
41
50
  open(element) {
51
+ if (KeyboardBase.#open !== this) KeyboardBase.#open?.close();
52
+ KeyboardBase.#open = this;
42
53
  this.elem = element;
43
54
  this.elem.ownerDocument.documentElement.appendChild(this);
44
55
  const erect = element.getBoundingClientRect();
@@ -49,12 +60,21 @@ var KeyboardBase = class extends Base {
49
60
  block: "center"
50
61
  });
51
62
  const erect = element.getBoundingClientRect();
52
- element.ownerDocument.body.style.transform = `translateY(${krect.top - (erect.top + erect.height)}px)`;
63
+ const shift = Math.max(krect.top - (erect.top + erect.height), -erect.top);
64
+ if (shift < 0) {
65
+ const body = element.ownerDocument.body;
66
+ this.#body_transform ??= body.style.transform;
67
+ body.style.transform = `${this.#body_transform} translateY(${shift}px)`.trim();
68
+ }
53
69
  }
54
70
  }
55
71
  close() {
72
+ if (KeyboardBase.#open === this) KeyboardBase.#open = void 0;
56
73
  this.remove();
57
- if (this.elem) this.elem.ownerDocument.body.style.transform = "";
74
+ if (this.elem && this.#body_transform !== void 0) {
75
+ this.elem.ownerDocument.body.style.transform = this.#body_transform;
76
+ this.#body_transform = void 0;
77
+ }
58
78
  }
59
79
  };
60
80
  // src/keyboard/types.ts
@@ -211,7 +231,13 @@ var Keyboard_Color = class extends KeyboardBase {
211
231
  green: 0,
212
232
  blue: 0
213
233
  };
214
- #on_input = () => this.#sync_from_input();
234
+ /**Set while the keyboard dispatches input, so its own writes are not read back, a grey would lose the hue*/
235
+ #writing = false;
236
+ /**Values were written since the last change event*/
237
+ #uncommitted = false;
238
+ #on_input = () => {
239
+ if (!this.#writing) this.#sync_from_input();
240
+ };
215
241
  constructor() {
216
242
  super();
217
243
  const container = this.appendChild(document.createElement("div"));
@@ -276,6 +302,7 @@ var Keyboard_Color = class extends KeyboardBase {
276
302
  }
277
303
  open(element) {
278
304
  if (!(element instanceof HTMLInputElement) || element.type !== "color") return;
305
+ if (element !== this.#input) this.#commit();
279
306
  this.#input?.removeEventListener("input", this.#on_input);
280
307
  this.#input = element;
281
308
  this.#input.addEventListener("input", this.#on_input);
@@ -283,6 +310,7 @@ var Keyboard_Color = class extends KeyboardBase {
283
310
  super.open(element);
284
311
  }
285
312
  close() {
313
+ this.#commit();
286
314
  this.#input?.removeEventListener("input", this.#on_input);
287
315
  this.#input = void 0;
288
316
  super.close();
@@ -301,8 +329,18 @@ var Keyboard_Color = class extends KeyboardBase {
301
329
  const changed = input.value !== color;
302
330
  input.value = color;
303
331
  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 }));
332
+ if (changed) {
333
+ this.#uncommitted = true;
334
+ this.#writing = true;
335
+ input.dispatchEvent(new Event("input", { bubbles: true }));
336
+ this.#writing = false;
337
+ }
338
+ if (commit) this.#commit();
339
+ }
340
+ /**Emits change when values were written since the last change*/
341
+ #commit() {
342
+ if (this.#input && this.#uncommitted) this.#input.dispatchEvent(new Event("change", { bubbles: true }));
343
+ this.#uncommitted = false;
306
344
  }
307
345
  #sync_from_input() {
308
346
  if (!this.#input) return;
@@ -363,6 +401,22 @@ function date_from_value(value) {
363
401
  const date = new Date(Number(match[1]), Number(match[2]) - 1, Number(match[3]));
364
402
  return date_value(date) === match[0] ? date : void 0;
365
403
  }
404
+ /**Date to show for the value of an input of the given type*/
405
+ function view_from_value(value, type) {
406
+ if (type === "month") {
407
+ const match = /^(\d{4})-(\d{2})$/.exec(value);
408
+ if (!match) return void 0;
409
+ return new Date(Number(match[1]), Number(match[2]) - 1, 1);
410
+ }
411
+ if (type === "week") {
412
+ const match = /^(\d{4})-W(\d{2})$/.exec(value);
413
+ if (!match) return void 0;
414
+ const date = new Date(Number(match[1]), 0, 4);
415
+ date.setDate(date.getDate() - (date.getDay() + 6) % 7 + (Number(match[2]) - 1) * 7);
416
+ return date;
417
+ }
418
+ return date_from_value(value);
419
+ }
366
420
  function week_value(date) {
367
421
  const utc = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
368
422
  utc.setUTCDate(utc.getUTCDate() + 4 - (utc.getUTCDay() || 7));
@@ -393,7 +447,7 @@ var Keyboard_DateTime = class extends KeyboardBase {
393
447
  open(element) {
394
448
  if (!(element instanceof HTMLInputElement) || !DATE_TYPES.includes(element.type)) return;
395
449
  this.#input = element;
396
- this.#view = date_from_value(element.value) ?? /* @__PURE__ */ new Date();
450
+ this.#view = view_from_value(element.value, element.type) ?? /* @__PURE__ */ new Date();
397
451
  this.#view.setDate(1);
398
452
  this.#selected_datetime_date = element.type === "datetime-local" ? date_from_value(element.value) : void 0;
399
453
  const time = element.value.match(/T?(\d{2}):(\d{2})/) ?? [];
@@ -686,7 +740,7 @@ var KeyboardLayer = class extends Base {
686
740
  };
687
741
  define_element(KeyboardLayer);
688
742
  const KEO = {
689
- ctrlKey: true,
743
+ ctrlKey: false,
690
744
  shiftKey: false,
691
745
  altKey: false,
692
746
  metaKey: false,
@@ -695,6 +749,18 @@ const KEO = {
695
749
  cancelable: true
696
750
  };
697
751
  const IEO = { bubbles: true };
752
+ /**KeyboardEvent code of the key typing a character, empty for characters without a key of their own*/
753
+ function key_code(key) {
754
+ if (key === " ") return "Space";
755
+ if (key >= "0" && key <= "9") return "Digit" + key;
756
+ const upper = key.toUpperCase();
757
+ if (upper.length === 1 && upper >= "A" && upper <= "Z") return "Key" + upper;
758
+ return "";
759
+ }
760
+ /**Email and number inputs have no selection api*/
761
+ function no_selection(element) {
762
+ return element.tagName === "INPUT" && element.selectionStart === null;
763
+ }
698
764
  var Keyboard_Keys = class extends KeyboardBase {
699
765
  static element_name() {
700
766
  return "keyboard-keys";
@@ -718,8 +784,8 @@ var Keyboard_Keys = class extends KeyboardBase {
718
784
  });
719
785
  }
720
786
  open(element) {
787
+ if (element !== this.elem) this.#dispatch_change();
721
788
  super.open(element);
722
- this.#changed = false;
723
789
  this.#layer.write("default");
724
790
  }
725
791
  close() {
@@ -762,9 +828,33 @@ var Keyboard_Keys = class extends KeyboardBase {
762
828
  ...opts
763
829
  })) ?? false;
764
830
  }
831
+ /**Edits like a typed key, beforeinput, the edit, then input*/
832
+ #edit(data, input_type) {
833
+ if (!this.#dis_beforeinput({
834
+ data,
835
+ inputType: input_type
836
+ })) return;
837
+ const edited = this.#edit_value(data ?? "", data === null);
838
+ if (!edited) return;
839
+ this.#changed = true;
840
+ if (edited !== "native") this.#dis_input({
841
+ data,
842
+ inputType: input_type
843
+ });
844
+ }
845
+ /**Returns "native" when the browser dispatched the input event itself*/
765
846
  #edit_value(data, delete_backward = false) {
766
847
  const element = this.elem;
767
848
  if (!element) return false;
849
+ if (no_selection(element)) {
850
+ let edited = false;
851
+ const mark = () => edited = true;
852
+ element.addEventListener("input", mark);
853
+ element.focus();
854
+ element.ownerDocument.execCommand(delete_backward ? "delete" : "insertText", false, data);
855
+ element.removeEventListener("input", mark);
856
+ return edited && "native";
857
+ }
768
858
  if (element.tagName === "INPUT" || element.tagName === "TEXTAREA") {
769
859
  const input = element;
770
860
  const start = input.selectionStart ?? 0;
@@ -830,46 +920,27 @@ var Keyboard_Keys = class extends KeyboardBase {
830
920
  }
831
921
  }[key] ?? {
832
922
  key,
833
- code: key === " " ? "Space" : "Key" + key.toUpperCase()
923
+ code: key_code(key)
834
924
  };
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);
925
+ if (this.#dis_keydown(opts)) {
926
+ if (key.length === 1) {
927
+ this.#dis_keypress(opts);
928
+ this.#edit(key, "insertText");
929
+ } else if (key === KeyboardSpecialKeys.Backspace) this.#edit(null, "deleteContentBackward");
930
+ else if (key === KeyboardSpecialKeys.Enter && this.elem.tagName !== "INPUT") this.#edit("\n", "insertLineBreak");
931
+ else if (key === KeyboardSpecialKeys.Left) this.#move_cursor(-1);
932
+ else if (key === KeyboardSpecialKeys.Right) this.#move_cursor(1);
933
+ }
870
934
  this.#dis_keyup(opts);
871
935
  if (key === KeyboardSpecialKeys.Enter) this.#dispatch_change();
872
936
  }
937
+ #move_cursor(by) {
938
+ const element = this.elem;
939
+ if (no_selection(element)) {
940
+ element.focus();
941
+ element.ownerDocument.getSelection()?.modify("move", by < 0 ? "backward" : "forward", "character");
942
+ } else set_cursor_position(element, Math.max(get_cursor_position(element) + by, 0));
943
+ }
873
944
  };
874
945
  define_element(Keyboard_Keys);
875
946
  // src/keyboard/number.ts