@chocbite/ts-lib-ui 0.0.0-stage → 1.0.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.
- package/README.md +48 -2
- package/base/index.d.mts +3 -0
- package/base/index.mjs +521 -0
- package/base/index.mjs.map +1 -0
- package/canvas/index.d.mts +74 -0
- package/canvas/index.mjs +619 -0
- package/canvas/index.mjs.map +1 -0
- package/context-menu/index.d.mts +3 -0
- package/context-menu/index.mjs +2 -0
- package/context-menu-BbDVgMiZ.mjs +527 -0
- package/context-menu-BbDVgMiZ.mjs.map +1 -0
- package/document/index.d.mts +27 -0
- package/document/index.mjs +50 -0
- package/document/index.mjs.map +1 -0
- package/dom/index.d.mts +3 -0
- package/dom/index.mjs +8 -0
- package/dom/index.mjs.map +1 -0
- package/form/index.d.mts +747 -0
- package/form/index.mjs +2694 -0
- package/form/index.mjs.map +1 -0
- package/icons/index.d.mts +4109 -0
- package/icons/index.mjs +2 -0
- package/icons/mixins.scss +12 -0
- package/icons-CY4BLQ2F.mjs +4123 -0
- package/index-BvflC2sG.d.mts +212 -0
- package/index-CWQ5KhrP.d.mts +167 -0
- package/index-CdiJN7JF.d.mts +155 -0
- package/index-Cx6QmIC-.d.mts +120 -0
- package/keyboard/index.d.mts +85 -0
- package/keyboard/index.mjs +1499 -0
- package/keyboard/index.mjs.map +1 -0
- package/list/index.d.mts +3 -0
- package/list/index.mjs +2 -0
- package/list-CmrmWLBD.mjs +617 -0
- package/list-CmrmWLBD.mjs.map +1 -0
- package/package.json +95 -4
- package/selection/index.d.mts +16 -0
- package/selection/index.mjs +96 -0
- package/selection/index.mjs.map +1 -0
- package/settings/index.d.mts +43 -0
- package/settings/index.mjs +110 -0
- package/settings/index.mjs.map +1 -0
- package/spinner/index.d.mts +10 -0
- package/spinner/index.mjs +39 -0
- package/spinner/index.mjs.map +1 -0
- package/style.css +2994 -0
- package/svg/index.d.mts +3 -0
- package/svg/index.mjs +2 -0
- package/svg-golJPaR4.mjs +358 -0
- package/svg-golJPaR4.mjs.map +1 -0
- package/theme/index.d.mts +179 -0
- package/theme/index.mjs +2 -0
- package/theme/mixins.scss +39 -0
- package/theme-BtAXQpvm.mjs +338 -0
- package/theme-BtAXQpvm.mjs.map +1 -0
- package/viewport/index.d.mts +1 -0
- package/viewport/index.mjs +2 -0
package/README.md
CHANGED
|
@@ -1,3 +1,49 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @chocbite/ts-lib-ui
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Browser UI TypeScript libraries, built on [@chocbite/ts-lib](https://www.npmjs.com/package/@chocbite/ts-lib).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @chocbite/ts-lib-ui
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
`@chocbite/ts-lib` is installed with it as a dependency.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
Every module is its own subpath export, there is no root export.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import ctm from "@chocbite/ts-lib-ui/context-menu";
|
|
19
|
+
import { svg } from "@chocbite/ts-lib-ui/svg";
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## CSS
|
|
23
|
+
|
|
24
|
+
All styles are in one `style.css`, which every module imports itself. A bundler that handles CSS imports (e.g. Vite) picks it up automatically, nothing needs to be imported by hand.
|
|
25
|
+
|
|
26
|
+
SCSS mixins are available for use in your own styles:
|
|
27
|
+
|
|
28
|
+
```scss
|
|
29
|
+
@use "@chocbite/ts-lib-ui/theme/mixins" as scrollbar;
|
|
30
|
+
@use "@chocbite/ts-lib-ui/icons/mixins" as iconfont;
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Modules
|
|
34
|
+
|
|
35
|
+
- `@chocbite/ts-lib-ui/base`
|
|
36
|
+
- `@chocbite/ts-lib-ui/canvas`
|
|
37
|
+
- `@chocbite/ts-lib-ui/context-menu`
|
|
38
|
+
- `@chocbite/ts-lib-ui/document`
|
|
39
|
+
- `@chocbite/ts-lib-ui/dom`
|
|
40
|
+
- `@chocbite/ts-lib-ui/form`
|
|
41
|
+
- `@chocbite/ts-lib-ui/icons`
|
|
42
|
+
- `@chocbite/ts-lib-ui/keyboard`
|
|
43
|
+
- `@chocbite/ts-lib-ui/list`
|
|
44
|
+
- `@chocbite/ts-lib-ui/selection`
|
|
45
|
+
- `@chocbite/ts-lib-ui/settings`
|
|
46
|
+
- `@chocbite/ts-lib-ui/spinner`
|
|
47
|
+
- `@chocbite/ts-lib-ui/svg`
|
|
48
|
+
- `@chocbite/ts-lib-ui/theme`
|
|
49
|
+
- `@chocbite/ts-lib-ui/viewport`
|
package/base/index.d.mts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import '../style.css';
|
|
2
|
+
import { a as validate_element_name, c as base_loop, d as BaseIntersectObserver, f as BaseIntersectObserverOptions, i as set_base_element_global_namespace, l as base_loop_state, m as AccessTypes, n as base_element_name, o as BaseSVG, p as ACCESSTYPESINFO, r as define_element, s as Loop, t as ELEMENT_LIST, u as Base } from "../index-BvflC2sG.mjs";
|
|
3
|
+
export { ACCESSTYPESINFO, AccessTypes, Base, BaseIntersectObserver, BaseIntersectObserverOptions, BaseSVG, ELEMENT_LIST, Loop, base_element_name, base_loop, base_loop_state, define_element, set_base_element_global_namespace, validate_element_name };
|
package/base/index.mjs
ADDED
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
import "../style.css";
|
|
2
|
+
import state, { state as state$1 } from "@chocbite/ts-lib/state";
|
|
3
|
+
import { some } from "@chocbite/ts-lib/result";
|
|
4
|
+
// src/base/access.ts
|
|
5
|
+
/**Enum of possible access types for base element*/
|
|
6
|
+
const AccessTypes = {
|
|
7
|
+
Write: "w",
|
|
8
|
+
Read: "r",
|
|
9
|
+
None: "n"
|
|
10
|
+
};
|
|
11
|
+
/**List for access type*/
|
|
12
|
+
const ACCESSTYPESINFO = state$1.e.list({
|
|
13
|
+
[AccessTypes.Write]: {
|
|
14
|
+
name: "Write",
|
|
15
|
+
description: "Write access to element"
|
|
16
|
+
},
|
|
17
|
+
[AccessTypes.Read]: {
|
|
18
|
+
name: "Read",
|
|
19
|
+
description: "Read access to element"
|
|
20
|
+
},
|
|
21
|
+
[AccessTypes.None]: {
|
|
22
|
+
name: "None",
|
|
23
|
+
description: "No access to element"
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
// src/base/intersect_observer.ts
|
|
27
|
+
const V2 = "delay" in new IntersectionObserver(() => {});
|
|
28
|
+
var BaseIntersectObserver = class extends IntersectionObserver {
|
|
29
|
+
#deffered_visible_time;
|
|
30
|
+
#deffered_visible_timeout = null;
|
|
31
|
+
#deffered_visible_queue = [];
|
|
32
|
+
#deffered_hidden_time;
|
|
33
|
+
#deffered_hidden_timeout = null;
|
|
34
|
+
#deffered_hidden_queue = [];
|
|
35
|
+
constructor(options) {
|
|
36
|
+
super((e) => {
|
|
37
|
+
if (this.#deffered_hidden_time === 0 && this.#deffered_visible_time === 0) {
|
|
38
|
+
for (let i = 0; i < e.length; i++) e[i].target.internal_set_visible(e[i].isIntersecting);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
for (let i = 0; i < e.length; i++) if (e[i].isIntersecting) {
|
|
42
|
+
this.#deffered_visible_queue.push(e[i].target);
|
|
43
|
+
const index = this.#deffered_hidden_queue.indexOf(e[i].target);
|
|
44
|
+
if (index !== -1) this.#deffered_hidden_queue.splice(index, 1);
|
|
45
|
+
} else {
|
|
46
|
+
this.#deffered_hidden_queue.push(e[i].target);
|
|
47
|
+
const index = this.#deffered_visible_queue.indexOf(e[i].target);
|
|
48
|
+
if (index !== -1) this.#deffered_visible_queue.splice(index, 1);
|
|
49
|
+
}
|
|
50
|
+
if (!this.#deffered_visible_timeout) this.#deffered_visible_timeout = window.setTimeout(() => {
|
|
51
|
+
for (let i = 0; i < this.#deffered_visible_queue.length; i++) this.#deffered_visible_queue[i].internal_set_visible(true);
|
|
52
|
+
this.#deffered_visible_queue = [];
|
|
53
|
+
this.#deffered_visible_timeout = null;
|
|
54
|
+
}, this.#deffered_visible_time);
|
|
55
|
+
if (!this.#deffered_hidden_timeout) this.#deffered_hidden_timeout = window.setTimeout(() => {
|
|
56
|
+
for (let i = 0; i < this.#deffered_hidden_queue.length; i++) this.#deffered_hidden_queue[i].internal_set_visible(false);
|
|
57
|
+
this.#deffered_hidden_queue = [];
|
|
58
|
+
this.#deffered_hidden_timeout = null;
|
|
59
|
+
}, this.#deffered_hidden_time);
|
|
60
|
+
}, V2 && options.deffered_visible === options.deffered_hidden ? {
|
|
61
|
+
delay: options.deffered_visible,
|
|
62
|
+
...options
|
|
63
|
+
} : options);
|
|
64
|
+
if (V2 && options.deffered_visible === options.deffered_hidden) {
|
|
65
|
+
this.#deffered_visible_time = 0;
|
|
66
|
+
this.#deffered_hidden_time = 0;
|
|
67
|
+
} else {
|
|
68
|
+
this.#deffered_visible_time = options.deffered_visible ?? 250;
|
|
69
|
+
this.#deffered_hidden_time = options.deffered_hidden ?? 250;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
// src/base/base.ts
|
|
74
|
+
const base_resize_observer = new ResizeObserver((e) => {
|
|
75
|
+
for (let i = 0; i < e.length; i++) e[i].target.internal_set_size_pos(e[i].contentRect);
|
|
76
|
+
});
|
|
77
|
+
/**Shared class for elements to extend
|
|
78
|
+
* All none abstract elements must use the define_element function to declare itself
|
|
79
|
+
*
|
|
80
|
+
* All none abstracts classes must override the static method element_name to return the name of the element
|
|
81
|
+
* Abstract classes should return @abstract@
|
|
82
|
+
*
|
|
83
|
+
* If another library defines an abstract base class, it is recommended to change the static element_name_space method to the name of the library
|
|
84
|
+
* example for this library '@chocolatelibui/core' becomes 'chocolatelibui-core'
|
|
85
|
+
* static element_name_space() { return 'chocolatelibui-core' }
|
|
86
|
+
* This resets the nametree to the library name and prevents too long element names
|
|
87
|
+
*
|
|
88
|
+
* Elements have an access propery, which builds on the html inert property
|
|
89
|
+
* Access has the following three states
|
|
90
|
+
* write = normal interaction and look
|
|
91
|
+
* read = inert attribute is added making the element uninteractable, and add opacity 0.5 to make the element look inaccessible
|
|
92
|
+
* none = adds display:none to element to make it */
|
|
93
|
+
var Base = class extends HTMLElement {
|
|
94
|
+
/**Returns the name used to define the element, Abstract classes should return @abstract@ */
|
|
95
|
+
static element_name() {
|
|
96
|
+
return "@abstract@";
|
|
97
|
+
}
|
|
98
|
+
/**Returns the namespace override for the element*/
|
|
99
|
+
static element_name_space() {
|
|
100
|
+
return "lib";
|
|
101
|
+
}
|
|
102
|
+
#states = /* @__PURE__ */ new Map();
|
|
103
|
+
is_connected = false;
|
|
104
|
+
/**Works when element is connected to observer, otherwise it is an alias for isConnected*/
|
|
105
|
+
is_visible = false;
|
|
106
|
+
#access;
|
|
107
|
+
#props = /* @__PURE__ */ new Map();
|
|
108
|
+
#attr = /* @__PURE__ */ new Map();
|
|
109
|
+
/**Runs when element is attached to document*/
|
|
110
|
+
connectedCallback() {
|
|
111
|
+
for (const [f, [s, v]] of this.#states) if (!v) s.sub(f, true);
|
|
112
|
+
if (this.#attached_int_observer) this.#attached_int_observer.observe(this);
|
|
113
|
+
else this.internal_set_visible(true);
|
|
114
|
+
this.is_connected = true;
|
|
115
|
+
}
|
|
116
|
+
/**Runs when element is dettached from document*/
|
|
117
|
+
disconnectedCallback() {
|
|
118
|
+
for (const [f, [s, v]] of this.#states) if (!v) s.unsub(f);
|
|
119
|
+
if (this.#attached_int_observer) {
|
|
120
|
+
this.#attached_int_observer.unobserve(this);
|
|
121
|
+
this.internal_set_visible(false);
|
|
122
|
+
}
|
|
123
|
+
this.is_connected = false;
|
|
124
|
+
}
|
|
125
|
+
/**Sets any attribute on the base element, to either a fixed value or a state value */
|
|
126
|
+
opts(opts) {
|
|
127
|
+
for (const key in opts) {
|
|
128
|
+
const opt = opts[key];
|
|
129
|
+
if (state$1.is.roa(opt)) this.attach_state_ROA_to_prop(key, opt);
|
|
130
|
+
else this[key] = opt;
|
|
131
|
+
}
|
|
132
|
+
return this;
|
|
133
|
+
}
|
|
134
|
+
/**Elements that can attach to a writable state can declare them in binds*/
|
|
135
|
+
bind(_opts) {
|
|
136
|
+
return this;
|
|
137
|
+
}
|
|
138
|
+
/**Executes a function with the element as an argument and returns the element
|
|
139
|
+
* Useful when declaring multiple elements in a single statement, without easy access to a scope to execute code in*/
|
|
140
|
+
scope(fn) {
|
|
141
|
+
fn(this);
|
|
142
|
+
return this;
|
|
143
|
+
}
|
|
144
|
+
/**Observer for children of this element */
|
|
145
|
+
#intersect_observer;
|
|
146
|
+
#attached_int_observer;
|
|
147
|
+
/**Returns an observer for the element */
|
|
148
|
+
intersect_observer(options = {
|
|
149
|
+
root: this,
|
|
150
|
+
threshold: 0,
|
|
151
|
+
deffered_hidden: 1e3
|
|
152
|
+
}) {
|
|
153
|
+
return this.#intersect_observer ??= new BaseIntersectObserver(options);
|
|
154
|
+
}
|
|
155
|
+
/**Attaches the component to an observer, which is needed for the isVisible state and event to work and for the state system to work on visible*/
|
|
156
|
+
attach_to_intersect_observer(observer) {
|
|
157
|
+
if (observer) {
|
|
158
|
+
if (this.is_connected) {
|
|
159
|
+
if (this.#attached_int_observer) this.#attached_int_observer.unobserve(this);
|
|
160
|
+
observer.observe(this);
|
|
161
|
+
}
|
|
162
|
+
this.#attached_int_observer = observer;
|
|
163
|
+
} else if (this.#attached_int_observer) {
|
|
164
|
+
if (this.is_connected) this.#attached_int_observer.unobserve(this);
|
|
165
|
+
if (!this.is_visible) this.internal_set_visible(true);
|
|
166
|
+
this.#attached_int_observer = void 0;
|
|
167
|
+
}
|
|
168
|
+
return this;
|
|
169
|
+
}
|
|
170
|
+
internal_set_visible(is) {
|
|
171
|
+
if (this.is_visible !== is) {
|
|
172
|
+
this.is_visible = is;
|
|
173
|
+
if (is) {
|
|
174
|
+
for (const [f, [s, v]] of this.#states) if (v) s.sub(f, true);
|
|
175
|
+
if (this.#resize_observe) base_resize_observer.observe(this);
|
|
176
|
+
} else {
|
|
177
|
+
for (const [f, [s, v]] of this.#states) if (v) s.unsub(f);
|
|
178
|
+
if (this.#resize_observe) base_resize_observer.unobserve(this);
|
|
179
|
+
}
|
|
180
|
+
this.on_visible(is);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
/**Overrideable function called when the element becomes visible or hidden
|
|
184
|
+
* If intersection observer is not attached it is based on connectedCallback, which is less accurate*/
|
|
185
|
+
on_visible(_is) {}
|
|
186
|
+
#resize_observe = false;
|
|
187
|
+
#actual_size;
|
|
188
|
+
/**Returns actual size and position of the element
|
|
189
|
+
* if a resize observer is attached to the element, it will return the result of that, otherwise it will return the result of getBoundingClientRect()*/
|
|
190
|
+
get actual_size() {
|
|
191
|
+
return this.#actual_size ?? this.getBoundingClientRect();
|
|
192
|
+
}
|
|
193
|
+
/**Attaches the component to the global resize observer */
|
|
194
|
+
attach_to_resize_observer() {
|
|
195
|
+
if (this.is_visible) base_resize_observer.observe(this);
|
|
196
|
+
this.#resize_observe = true;
|
|
197
|
+
return this;
|
|
198
|
+
}
|
|
199
|
+
/**Dettaches the component from the global resize observer */
|
|
200
|
+
detach_from_resize_observer() {
|
|
201
|
+
if (this.is_visible) base_resize_observer.unobserve(this);
|
|
202
|
+
this.#resize_observe = false;
|
|
203
|
+
this.#actual_size = void 0;
|
|
204
|
+
return this;
|
|
205
|
+
}
|
|
206
|
+
internal_set_size_pos(rect) {
|
|
207
|
+
this.#actual_size = rect;
|
|
208
|
+
this.on_resize(rect);
|
|
209
|
+
}
|
|
210
|
+
/**Overrideable function called when the element is resized
|
|
211
|
+
* Only triggers when a resize observer is attached to the element, otherwise it will not trigger*/
|
|
212
|
+
on_resize(_rect) {}
|
|
213
|
+
/**Sets the access of the element, passing undefined is the same as passing write access*/
|
|
214
|
+
set access(access) {
|
|
215
|
+
if (this.#access === access) return;
|
|
216
|
+
this.#access = access;
|
|
217
|
+
switch (access) {
|
|
218
|
+
case AccessTypes.Write:
|
|
219
|
+
this.inert = false;
|
|
220
|
+
break;
|
|
221
|
+
case AccessTypes.Read:
|
|
222
|
+
this.inert = true;
|
|
223
|
+
break;
|
|
224
|
+
case AccessTypes.None: this.setAttribute("inert", "none");
|
|
225
|
+
}
|
|
226
|
+
this.on_access(access);
|
|
227
|
+
}
|
|
228
|
+
/**Overrideable function called when access is changed */
|
|
229
|
+
on_access(_access) {}
|
|
230
|
+
/**Returns the current access of the element */
|
|
231
|
+
get access() {
|
|
232
|
+
return this.#access ?? AccessTypes.Write;
|
|
233
|
+
}
|
|
234
|
+
/**Attaches a state to a function, so that the function is subscribed to the state when the component is connected
|
|
235
|
+
* @param visible when set true the function is only subscribed when the element is visible, this requires an observer to be attached to the element*/
|
|
236
|
+
attach_state(state, func, visible) {
|
|
237
|
+
if (this.#states.has(func)) console.error("Function already registered with element", func, this);
|
|
238
|
+
else {
|
|
239
|
+
this.#states.set(func, [state, Boolean(visible)]);
|
|
240
|
+
if (visible ? this.is_visible : this.is_connected) state.sub(func, true);
|
|
241
|
+
}
|
|
242
|
+
return func;
|
|
243
|
+
}
|
|
244
|
+
/**Detaches the function from the state/component */
|
|
245
|
+
detach_state(func) {
|
|
246
|
+
const state = this.#states.get(func);
|
|
247
|
+
if (state) {
|
|
248
|
+
if (state[1] ? this.is_visible : this.is_connected) state[0].unsub(func);
|
|
249
|
+
this.#states.delete(func);
|
|
250
|
+
} else console.error("Function not registered with element", func, this);
|
|
251
|
+
return func;
|
|
252
|
+
}
|
|
253
|
+
/**Attaches a state to a function, so that the function is subscribed to the state when the component is connected
|
|
254
|
+
* @param visible when set true the function is only subscribed when the element is visible, this requires an observer to be attached to the element*/
|
|
255
|
+
attach_state_to_symbol(symbol, state, func, visible) {
|
|
256
|
+
this.detach_state_from_symbol(symbol).#props.set(symbol, this.attach_state(state, func, visible));
|
|
257
|
+
return this;
|
|
258
|
+
}
|
|
259
|
+
/**Detaches the function from the state/component */
|
|
260
|
+
detach_state_from_symbol(symbol) {
|
|
261
|
+
const pro = this.#props.get(symbol);
|
|
262
|
+
if (pro) {
|
|
263
|
+
this.detach_state(pro);
|
|
264
|
+
this.#props.delete(symbol);
|
|
265
|
+
}
|
|
266
|
+
return this;
|
|
267
|
+
}
|
|
268
|
+
/**Attaches a state to a property, so that the property is updated when the state changes
|
|
269
|
+
* @param prop the property to attach the state to
|
|
270
|
+
* @param state the state to attach to the property
|
|
271
|
+
* @param visible when set true the property is only updated when the element is visible, this requires an observer to be attached to the element*/
|
|
272
|
+
attach_state_ROA_to_prop(prop, state, visible) {
|
|
273
|
+
this.detach_state_from_prop(prop).#props.set(prop, this.attach_state(state, (val) => this[prop] = val.value, visible));
|
|
274
|
+
return this;
|
|
275
|
+
}
|
|
276
|
+
/**Attaches a state to a property, so that the property is updated when the state changes
|
|
277
|
+
* @param prop the property to attach the state to
|
|
278
|
+
* @param state the state to attach to the property
|
|
279
|
+
* @param visible when set true the property is only updated when the element is visible, this requires an observer to be attached to the element
|
|
280
|
+
* @param fallback the fallback value for the property when the state is not ok, if undefined the property is not updated when the state is not ok*/
|
|
281
|
+
attach_state_ROA_to_prop_map(prop, state, map, visible) {
|
|
282
|
+
this.detach_state_from_prop(prop).#props.set(prop, this.attach_state(state, (val) => {
|
|
283
|
+
const o = map(val.value);
|
|
284
|
+
if (o.some) this[prop] = o.value;
|
|
285
|
+
}, visible));
|
|
286
|
+
return this;
|
|
287
|
+
}
|
|
288
|
+
/**Attaches a state to a property, so that the property is updated when the state changes
|
|
289
|
+
* @param prop the property to attach the state to
|
|
290
|
+
* @param state the state to attach to the property
|
|
291
|
+
* @param map_err function called when state gives a ResultErr,
|
|
292
|
+
* @param visible when set true the property is only updated when the element is visible, this requires an observer to be attached to the element*/
|
|
293
|
+
attach_state_to_prop(prop, state, map_err, visible) {
|
|
294
|
+
this.detach_state_from_prop(prop).#props.set(prop, this.attach_state(state, (v) => {
|
|
295
|
+
const o = v.ok ? some(v.value) : map_err(v.error);
|
|
296
|
+
if (o.some) this[prop] = o.value;
|
|
297
|
+
}, visible));
|
|
298
|
+
return this;
|
|
299
|
+
}
|
|
300
|
+
/**Attaches a state to a property, so that the property is updated when the state changes
|
|
301
|
+
* @param prop the property to attach the state to
|
|
302
|
+
* @param state the state to attach to the property
|
|
303
|
+
* @param map_err function called when state gives a ResultErr,
|
|
304
|
+
* @param map function called when state gives a ResultOk,
|
|
305
|
+
* @param visible when set true the property is only updated when the element is visible, this requires an observer to be attached to the element*/
|
|
306
|
+
attach_state_to_prop_map(prop, state, map_err, map, visible) {
|
|
307
|
+
this.detach_state_from_prop(prop).#props.set(prop, this.attach_state(state, (v) => {
|
|
308
|
+
const o = v.ok ? map(v.value) : map_err(v.error);
|
|
309
|
+
if (o.some) this[prop] = o.value;
|
|
310
|
+
}, visible));
|
|
311
|
+
return this;
|
|
312
|
+
}
|
|
313
|
+
/**Detaches the state from the property */
|
|
314
|
+
detach_state_from_prop(prop) {
|
|
315
|
+
const pro = this.#props.get(prop);
|
|
316
|
+
if (pro) {
|
|
317
|
+
this.detach_state(pro);
|
|
318
|
+
this.#props.delete(prop);
|
|
319
|
+
}
|
|
320
|
+
return this;
|
|
321
|
+
}
|
|
322
|
+
/**Attaches a state to an element attribute, so that the attribute is updated when the state changes
|
|
323
|
+
* @param qualified_name the qualified name of the attribute to attach the state to
|
|
324
|
+
* @param state the state to attach to the attribute
|
|
325
|
+
* @param visible when set true the attribute is only updated when the element is visible, this requires an observer to be attached to the element*/
|
|
326
|
+
attach_state_ROA_to_attribute(qualified_name, state, visible) {
|
|
327
|
+
this.detach_state_from_attribute(qualified_name).#attr.set(qualified_name, this.attach_state(state, (val) => this.setAttribute(qualified_name, val.value), visible));
|
|
328
|
+
return this;
|
|
329
|
+
}
|
|
330
|
+
/**Attaches a state to an element attribute, so that the attribute is updated when the state changes
|
|
331
|
+
* @param qualified_name the qualified name of the attribute to attach the state to
|
|
332
|
+
* @param state the state to attach to the attribute
|
|
333
|
+
* @param visible when set true the attribute is only updated when the element is visible, this requires an observer to be attached to the element
|
|
334
|
+
* @param fallback the fallback value for the attribute when the state is not ok, if undefined the attribute is not updated when the state is not ok*/
|
|
335
|
+
attach_state_ROA_to_attribute_map(qualified_name, state, map, visible) {
|
|
336
|
+
this.detach_state_from_attribute(qualified_name).#attr.set(qualified_name, this.attach_state(state, (val) => {
|
|
337
|
+
const o = map(val.value);
|
|
338
|
+
if (o.some) this.setAttribute(qualified_name, o.value);
|
|
339
|
+
}, visible));
|
|
340
|
+
return this;
|
|
341
|
+
}
|
|
342
|
+
/**Attaches a state to an element attribute, so that the attribute is updated when the state changes
|
|
343
|
+
* @param qualified_name the qualified name of the attribute to attach the state to
|
|
344
|
+
* @param state the state to attach to the attribute
|
|
345
|
+
* @param map_err function called when state gives a ResultErr,
|
|
346
|
+
* @param visible when set true the attribute is only updated when the element is visible, this requires an observer to be attached to the element*/
|
|
347
|
+
attach_state_to_attribute(qualified_name, state, map_err, visible) {
|
|
348
|
+
this.detach_state_from_attribute(qualified_name).#attr.set(qualified_name, this.attach_state(state, (v) => {
|
|
349
|
+
const o = v.ok ? some(v.value) : map_err(v.error);
|
|
350
|
+
if (o.some) this.setAttribute(qualified_name, o.value);
|
|
351
|
+
}, visible));
|
|
352
|
+
return this;
|
|
353
|
+
}
|
|
354
|
+
/**Attaches a state to an element attribute, so that the attribute is updated when the state changes
|
|
355
|
+
* @param qualified_name the qualified name of the attribute to attach the state to
|
|
356
|
+
* @param state the state to attach to the attribute
|
|
357
|
+
* @param map_err function called when state gives a ResultErr,
|
|
358
|
+
* @param map function called when state gives a ResultOk,
|
|
359
|
+
* @param visible when set true the attribute is only updated when the element is visible, this requires an observer to be attached to the element*/
|
|
360
|
+
attach_state_to_attribute_map(qualified_name, state, map_err, map, visible) {
|
|
361
|
+
this.detach_state_from_attribute(qualified_name).#attr.set(qualified_name, this.attach_state(state, (v) => {
|
|
362
|
+
const o = v.ok ? map(v.value) : map_err(v.error);
|
|
363
|
+
if (o.some) this.setAttribute(qualified_name, o.value);
|
|
364
|
+
}, visible));
|
|
365
|
+
return this;
|
|
366
|
+
}
|
|
367
|
+
/**Detaches the state from the property */
|
|
368
|
+
detach_state_from_attribute(qualified_name) {
|
|
369
|
+
const pro = this.#attr.get(qualified_name);
|
|
370
|
+
if (pro) this.detach_state(pro);
|
|
371
|
+
return this;
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
// src/base/define_element.ts
|
|
375
|
+
const RESERVED_NAMES = /* @__PURE__ */ new Set([
|
|
376
|
+
"annotation-xml",
|
|
377
|
+
"color-profile",
|
|
378
|
+
"font-face",
|
|
379
|
+
"font-face-src",
|
|
380
|
+
"font-face-uri",
|
|
381
|
+
"font-face-format",
|
|
382
|
+
"font-face-name",
|
|
383
|
+
"missing-glyph"
|
|
384
|
+
]);
|
|
385
|
+
const regex = /^[a-z](?:[\.0-9_a-z\xB7\xC0-\xD6\xD8-\xF6\xF8-\u037D\u037F-\u1FFF\u200C\u200D\u203F\u2040\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|[\uD800-\uDB7F][\uDC00-\uDFFF])*-(?:[\x2D\.0-9_a-z\xB7\xC0-\xD6\xD8-\xF6\xF8-\u037D\u037F-\u1FFF\u200C\u200D\u203F\u2040\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|[\uD800-\uDB7F][\uDC00-\uDFFF])*$/;
|
|
386
|
+
/**Gives more information about whats wrong with the given element name */
|
|
387
|
+
const validate_element_name = (name) => {
|
|
388
|
+
if (!name) return "Missing element name.";
|
|
389
|
+
if (/[A-Z]/.test(name)) return "Custom element names must not contain uppercase ASCII characters.";
|
|
390
|
+
if (!name.includes("-")) return "Custom element names must contain a hyphen. Example: unicorn-cake";
|
|
391
|
+
if (/^\d/i.test(name)) return "Custom element names must not start with a digit.";
|
|
392
|
+
if (/^-/i.test(name)) return "Custom element names must not start with a hyphen.";
|
|
393
|
+
if (!regex.test(name)) return "Invalid element name.";
|
|
394
|
+
if (RESERVED_NAMES.has(name)) return "The supplied element name is reserved and can't be used.\nSee: https://html.spec.whatwg.org/multipage/scripting.html#valid-custom-element-name";
|
|
395
|
+
return "Unknown fault";
|
|
396
|
+
};
|
|
397
|
+
let base_element_global_namespace = "";
|
|
398
|
+
const set_base_element_global_namespace = (namespace) => {
|
|
399
|
+
base_element_global_namespace = namespace + (namespace.length ? "-" : "");
|
|
400
|
+
};
|
|
401
|
+
/**Generates the base name for a custom element */
|
|
402
|
+
const base_element_name = (element) => {
|
|
403
|
+
const namespace = element.element_name_space();
|
|
404
|
+
const check = element.element_name;
|
|
405
|
+
let define_name = "";
|
|
406
|
+
let runner = element;
|
|
407
|
+
while (runner !== HTMLElement) {
|
|
408
|
+
if (namespace !== runner.element_name_space()) break;
|
|
409
|
+
const name = runner.element_name();
|
|
410
|
+
runner = Object.getPrototypeOf(runner);
|
|
411
|
+
if (check === runner.element_name) throw new Error("Element uses same name as ancestor, abstract classes should return '@abstract@'");
|
|
412
|
+
if (!name.length) throw new Error("Element doesn't define element name");
|
|
413
|
+
if (name !== "@abstract@") define_name = "-" + name + define_name;
|
|
414
|
+
}
|
|
415
|
+
return base_element_global_namespace + namespace + define_name;
|
|
416
|
+
};
|
|
417
|
+
const ELEMENT_LIST = /* @__PURE__ */ new Set();
|
|
418
|
+
/**Defines elements inheriting from the base*/
|
|
419
|
+
const define_element = (element) => {
|
|
420
|
+
const namespace = element.element_name_space();
|
|
421
|
+
const check = element.element_name;
|
|
422
|
+
let define_name = "";
|
|
423
|
+
let runner = element;
|
|
424
|
+
while (runner !== HTMLElement) {
|
|
425
|
+
if (namespace !== runner.element_name_space()) break;
|
|
426
|
+
const name = runner.element_name();
|
|
427
|
+
runner = Object.getPrototypeOf(runner);
|
|
428
|
+
if (check === runner.element_name) throw new Error("Failed to define element \"" + namespace + "-" + name + define_name + "\" Element uses same name as ancestor, abstract classes should return '@abstract@'");
|
|
429
|
+
if (!name.length) throw new Error("Element doesn't define element name");
|
|
430
|
+
if (name !== "@abstract@") define_name = "-" + name + define_name;
|
|
431
|
+
}
|
|
432
|
+
define_name = base_element_global_namespace + namespace + define_name;
|
|
433
|
+
try {
|
|
434
|
+
customElements.define(define_name, element);
|
|
435
|
+
ELEMENT_LIST.add(define_name);
|
|
436
|
+
} catch (e) {
|
|
437
|
+
if (e instanceof Error && e.message.includes("has already been used with this registry")) throw e;
|
|
438
|
+
else throw new Error("Failed to define element \"" + define_name + "\" " + validate_element_name(define_name));
|
|
439
|
+
}
|
|
440
|
+
};
|
|
441
|
+
// src/base/base_loop.ts
|
|
442
|
+
var Loop = class extends Base {
|
|
443
|
+
static element_name() {
|
|
444
|
+
return "loop";
|
|
445
|
+
}
|
|
446
|
+
static element_name_space() {
|
|
447
|
+
return "base";
|
|
448
|
+
}
|
|
449
|
+
#generator;
|
|
450
|
+
#destructor;
|
|
451
|
+
#change;
|
|
452
|
+
constructor(generator, options) {
|
|
453
|
+
super();
|
|
454
|
+
this.#generator = generator;
|
|
455
|
+
this.#destructor = options?.destructor;
|
|
456
|
+
this.#change = options?.change;
|
|
457
|
+
}
|
|
458
|
+
set array(array) {
|
|
459
|
+
const read = state.a.read(array);
|
|
460
|
+
for (let i = 0; i < read.length; i++) {
|
|
461
|
+
const element = read[i];
|
|
462
|
+
if (element.type === "fresh") this.replaceChildren(...array.map(this.#generator));
|
|
463
|
+
else if (element.type === "added") {
|
|
464
|
+
const child_nodes = this.childNodes;
|
|
465
|
+
if (element.index === child_nodes.length) this.append(...array.slice(element.index).map(this.#generator));
|
|
466
|
+
else for (let i = element.items.length; i > 0; i--) this.insertBefore(this.#generator(element.items[i - 1]), child_nodes[element.index]);
|
|
467
|
+
} else if (element.type === "removed") for (let i = 0; i < element.items.length; i++) {
|
|
468
|
+
this.#destructor?.(this.childNodes[element.index]);
|
|
469
|
+
this.childNodes[element.index].remove();
|
|
470
|
+
}
|
|
471
|
+
else if (element.type === "changed") for (let i = 0; i < element.items.length; i++) if (this.#change) this.#change(this.childNodes[i + element.index], element.items[i]);
|
|
472
|
+
else {
|
|
473
|
+
this.#destructor?.(this.childNodes[i + element.index]);
|
|
474
|
+
this.childNodes[i + element.index].replaceWith(this.#generator(element.items[i]));
|
|
475
|
+
}
|
|
476
|
+
else if (element.type === "moved") {
|
|
477
|
+
const extracted_nodes = [];
|
|
478
|
+
for (let i = 0; i < element.items.length; i++) {
|
|
479
|
+
const node = this.childNodes[element.from_index + i];
|
|
480
|
+
extracted_nodes.push(node);
|
|
481
|
+
node.remove();
|
|
482
|
+
}
|
|
483
|
+
for (let i = 0; i < element.items.length; i++) {
|
|
484
|
+
const node = extracted_nodes[i];
|
|
485
|
+
if (element.to_index + i === this.childNodes.length) this.append(node);
|
|
486
|
+
else this.insertBefore(node, this.childNodes[element.to_index + i]);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
set error(err) {
|
|
492
|
+
this.replaceChildren(err);
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
define_element(Loop);
|
|
496
|
+
function base_loop(generator, options) {
|
|
497
|
+
return new Loop(generator, options);
|
|
498
|
+
}
|
|
499
|
+
function base_loop_state() {}
|
|
500
|
+
// src/base/base_svg.ts
|
|
501
|
+
/**Shared svg class for elements to extend
|
|
502
|
+
* See Base for more information on how to define elements, and the element lifecycle */
|
|
503
|
+
var BaseSVG = class extends Base {
|
|
504
|
+
/**Returns the name used to define the element, Abstract classes should return @abstract@ */
|
|
505
|
+
static element_name() {
|
|
506
|
+
return "@abstract@";
|
|
507
|
+
}
|
|
508
|
+
/**Returns the namespace override for the element*/
|
|
509
|
+
static element_name_space() {
|
|
510
|
+
return "lib";
|
|
511
|
+
}
|
|
512
|
+
canvas = this.default_canvas();
|
|
513
|
+
constructor() {
|
|
514
|
+
super();
|
|
515
|
+
this.canvas.classList.add(base_element_name(this.constructor));
|
|
516
|
+
this.canvas.appendChild(this);
|
|
517
|
+
}
|
|
518
|
+
};
|
|
519
|
+
export { ACCESSTYPESINFO, AccessTypes, Base, BaseIntersectObserver, BaseSVG, ELEMENT_LIST, Loop, base_element_name, base_loop, base_loop_state, define_element, set_base_element_global_namespace, validate_element_name };
|
|
520
|
+
|
|
521
|
+
//# sourceMappingURL=index.mjs.map
|