@tltdsh/mx 1.4.1 → 1.6.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/skill/ui-mx.js ADDED
@@ -0,0 +1,256 @@
1
+ ((doc, P, comps) => {
2
+ comps = components = { __proto__: null };
3
+ define = (name, obj = {}) => {
4
+ if (obj.$) {
5
+ obj.$$ ||= obj.$;
6
+ obj.$ = function(state) {
7
+ let elements = obj.$$.call(this, assign(this.$state ||= {}, state));
8
+ return elements?._ ?
9
+ this.render(elements) :
10
+ this.render(...elements || []);
11
+ };
12
+ }
13
+ comps[toTag(name)] = obj;
14
+ };
15
+ let isSpecialAttribute = {__proto__: null, checked: 1, selected: 1, value: 1},
16
+ assign = Object.assign,
17
+ svgNS = 'http://www.w3.org/2000/svg',
18
+ store = Symbol(),
19
+ claimed = Symbol(),
20
+ // plain child arrays flatten, nested ones too; a description (an array marked ._) stays
21
+ // whole. Each flattened array leaves a false (skipped) where it stood, so an iterator
22
+ // already past that index resumes on the array's first item.
23
+ flatten = list => list.flatMap(item => item?._ || !item?.pop ? [item] : [false, ...flatten(item)]),
24
+ // mark every real node in the argument list with the list itself: a fresh array per
25
+ // call, so the mark needs no cleanup and no counter. An unspread array anywhere in the
26
+ // list is flattened in place first, so real nodes inside it are claimed before any
27
+ // placement prunes ahead of the cursor.
28
+ claim = nodes => {
29
+ for (let node of nodes) {
30
+ if (node?.nodeType) node[claimed] = nodes;
31
+ else if (!node?._ && node?.pop) nodes.splice(0, Infinity, ...flatten(nodes));
32
+ }
33
+ },
34
+ createElement = (tagName, isSVG) => {
35
+ if (tagName == 'svg' | isSVG)
36
+ return doc.createElementNS(svgNS, tagName);
37
+ let element = doc.createElement(tagName);
38
+ let customElement = comps[tagName];
39
+ if (customElement) assign(element, customElement);
40
+ return element;
41
+ }
42
+
43
+ P.$attrs = function(attrs, key, value) {
44
+ for (key in attrs) {
45
+ value = attrs[key];
46
+ key[0] == '.'
47
+ ? this[key.slice(1)] = value
48
+ : value?.call
49
+ ? this[key] = value
50
+ : value == null | value === false
51
+ ? this.removeAttribute(key)
52
+ : this.setAttribute(key, value);
53
+ }
54
+ return this;
55
+ };
56
+
57
+ P.render = function(...nodes) {
58
+ let hostElement = this,
59
+ currentChild = hostElement.firstChild,
60
+ isSVG = hostElement.namespaceURI == svgNS && hostElement.localName != 'foreignObject';
61
+
62
+ let marked;
63
+
64
+ for (let node of nodes) {
65
+ if (node == null || node === false) continue;
66
+
67
+ let tagName, attributes, childNodes, component;
68
+
69
+ if (node._) {
70
+ [tagName, attributes, childNodes] = node;
71
+ } else if (node.nodeType) {
72
+ // on the first real node, claim every real node in the list; a render with no
73
+ // real nodes never pays for the pass
74
+ if (!marked) claim(nodes), marked = 1;
75
+ // drop the dead nodes sitting ahead of the cursor (real nodes this render
76
+ // does not claim), then place the node before whatever is left
77
+ // (insertBefore(node, null) appends). A claimed node is never detached:
78
+ // pruning a capped list from the head is k removals, prepending is one
79
+ // insert, a swap moves one node. Focus, selection, scroll and
80
+ // custom-element state of kept nodes survive.
81
+ while (currentChild && currentChild !== node && currentChild[claimed] !== nodes) {
82
+ let nextSibling = currentChild.nextSibling;
83
+ hostElement.removeChild(currentChild);
84
+ currentChild = nextSibling;
85
+ }
86
+ // the node sits right behind a claimed cursor: the cursor is the one that
87
+ // moved (first row sent to the end, a far swap), so detach it instead
88
+ if (currentChild !== node)
89
+ currentChild?.nextSibling === node
90
+ ? hostElement.removeChild(currentChild)
91
+ : hostElement.insertBefore(node, currentChild);
92
+ currentChild = node.nextSibling;
93
+ continue;
94
+ } else if (node.pop) {
95
+ // a plain array (an unspread .map() result) is a run of children: flatten the
96
+ // whole argument list in place, once. Everything before this index is already
97
+ // flat, so it maps one-to-one and the iterator resumes on the array's first
98
+ // item. A description is an array but carries ._ and was taken above; only
99
+ // this path pays, so a spread list costs nothing. If a real node came first,
100
+ // the mark pass already flattened the list and this branch is never reached.
101
+ nodes.splice(0, Infinity, ...flatten(nodes));
102
+ continue;
103
+ }
104
+
105
+ // a cursor some render once placed as a real node may be claimed by this render
106
+ // further down the list (h.render(mx.p('x'), n) after h.render(n)): run the claim
107
+ // pass now, and if the list does claim it, never reuse it positionally - insert a
108
+ // fresh node in front of it (insertBefore(x, null) appends when there is no
109
+ // cursor). A cursor that never held a mark costs one missing-property read, and
110
+ // is reused as before - so a node no render ever placed (grabbed from firstChild,
111
+ // appended by hand) and passed later in the list is still taken over.
112
+ if (currentChild?.[claimed] ? (marked || (claim(nodes), marked = 1), currentChild[claimed] === nodes) : !currentChild) {
113
+ currentChild = hostElement.insertBefore(tagName ? createElement(tagName, isSVG) : doc.createTextNode(node), currentChild);
114
+ } else if (!tagName) {
115
+ if (currentChild.splitText) {
116
+ if (currentChild.nodeValue != '' + node) currentChild.nodeValue = '' + node;
117
+ } else {
118
+ currentChild.replaceWith(currentChild = doc.createTextNode(node));
119
+ }
120
+ } else if (currentChild.localName != tagName) {
121
+ currentChild.replaceWith(currentChild = createElement(tagName, isSVG));
122
+ }
123
+
124
+ if (tagName) {
125
+ component = currentChild.$;
126
+ let hasAttributes = attributes;
127
+
128
+ if (component) {
129
+ currentChild[store] = 0;
130
+ } else {
131
+ // The cache is the only record of what the previous description set: every
132
+ // key it holds that the new description no longer names is undone here.
133
+ // This runs BEFORE the children reconcile so that a dropped '.textContent' /
134
+ // '.innerHTML' / '.className' cannot wipe children or a class that the same
135
+ // render is about to produce (nulling them afterwards did exactly that).
136
+ // null-prototype: a key named toString/constructor/hasOwnProperty must read as
137
+ // absent, not as the Object.prototype method (which would then be nulled)
138
+ let elementCache = currentChild[store] ||= {__proto__: null};
139
+
140
+ for (let cachedKey in elementCache) {
141
+ if (!hasAttributes || !Object.hasOwn(attributes, cachedKey)) {
142
+ if (cachedKey[0] == '.') {
143
+ let prop = cachedKey.slice(1);
144
+ // .value/.checked/.selected dropped: the user's form state stays.
145
+ // a truthy own plain same-name key owns the property from here on and
146
+ // is applied below, so don't null what it is about to set; a plain
147
+ // null/false still means removal, so the handler/expando is undone.
148
+ if (!isSpecialAttribute[prop] && !(hasAttributes && Object.hasOwn(attributes, prop) && attributes[prop])) {
149
+ currentChild[prop] = null;
150
+ // reflected props (.src, .title) leave "null" behind in the attribute
151
+ currentChild.removeAttribute(prop);
152
+ }
153
+ } else {
154
+ if (isSpecialAttribute[cachedKey]) currentChild[cachedKey] = '';
155
+ // a dropped handler is detached; a dropped string never touched the property
156
+ else if (elementCache[cachedKey]?.call) currentChild[cachedKey] = null;
157
+ currentChild.removeAttribute(cachedKey);
158
+ }
159
+ delete elementCache[cachedKey];
160
+ }
161
+ }
162
+
163
+ // children first: a <select>'s .value / value must land after its <option>s exist
164
+ currentChild.render(...childNodes);
165
+
166
+ if (hasAttributes) {
167
+ for (let attrKey in attributes) {
168
+ let nextValue = attributes[attrKey];
169
+
170
+ // form-control props are re-asserted (description is truth) but never
171
+ // re-assigned when already held: assigning '' to a segmented date/time
172
+ // input wipes the segments the user is still editing
173
+ if (attrKey[0] == '.') {
174
+ let attr = attrKey.slice(1);
175
+ elementCache[attrKey] = nextValue;
176
+ if (currentChild[attr] !== nextValue) currentChild[attr] = nextValue;
177
+
178
+ // a key never cached (or removed) reads undefined from the cache, so an
179
+ // explicit undefined must not look like a cache hit: it is a removal
180
+ } else if (attrKey in elementCache && nextValue === elementCache[attrKey]) {
181
+ if (isSpecialAttribute[attrKey] && currentChild[attrKey] !== nextValue) currentChild[attrKey] = nextValue;
182
+
183
+ } else if (nextValue != null && nextValue !== false) {
184
+ if (nextValue?.call) {
185
+ currentChild[attrKey] = nextValue;
186
+ } else {
187
+ // a plain value winning a key that held a handler leaves the
188
+ // property alone: setAttribute is the whole job (an on* content
189
+ // attribute replaces the handler by itself)
190
+ if (isSpecialAttribute[attrKey] && currentChild[attrKey] !== nextValue) currentChild[attrKey] = nextValue;
191
+ currentChild.setAttribute(attrKey, nextValue);
192
+ }
193
+ elementCache[attrKey] = nextValue;
194
+
195
+ } else {
196
+ // idempotent like the set paths: a property already reading '' (or
197
+ // false) is not re-written, so a segmented date/time input keeps its
198
+ // partial edit while the description holds value: null every render
199
+ if (isSpecialAttribute[attrKey]) { if (currentChild[attrKey] != '') currentChild[attrKey] = ''; }
200
+ else if (elementCache[attrKey]?.call) currentChild[attrKey] = null;
201
+ currentChild.removeAttribute(attrKey);
202
+ delete elementCache[attrKey];
203
+ }
204
+ }
205
+ }
206
+ }
207
+ }
208
+
209
+ if (component) {
210
+ component.call(currentChild, attributes);
211
+ }
212
+
213
+ currentChild = currentChild.nextSibling;
214
+ }
215
+
216
+ while (currentChild) {
217
+ let nextSibling = currentChild.nextSibling;
218
+ hostElement.removeChild(currentChild);
219
+ currentChild = nextSibling;
220
+ }
221
+
222
+ return this;
223
+ };
224
+
225
+ let tagCacheObject = { __proto__: null },
226
+ toProxy = (fn, cache = { __proto__: null }) => new Proxy(fn, {
227
+ get(_, tagAlias) { return cache[tagAlias] ||= fn.bind(0, tagAlias) }
228
+ }),
229
+ toTag = tagAlias => tagCacheObject[tagAlias] ||= tagAlias.replace(/[A-Z]/g, '-$&').toLowerCase();
230
+
231
+ mx = toProxy((tagAlias, attributes, ...childList) => {
232
+ let definition = [toTag(tagAlias)];
233
+ definition._ = 1;
234
+
235
+ if (attributes?.__proto__ === Object.prototype && !attributes.nodeType) {
236
+ definition[1] = attributes;
237
+ definition[2] = childList;
238
+ } else {
239
+ definition[1] = null;
240
+ definition[2] = [attributes, ...childList];
241
+ }
242
+ return definition;
243
+ });
244
+
245
+ dom = toProxy((tagAlias, attributes, ...childList) => {
246
+ let element = createElement(toTag(tagAlias)), attrs;
247
+ if (attributes?.__proto__ === Object.prototype && !attributes.nodeType) {
248
+ if (element.$) return element.$(attributes), element;
249
+ attrs = attributes;
250
+ } else childList.unshift(attributes);
251
+ element.render(...childList);
252
+ if (attrs) element.$attrs(attrs);
253
+ return element;
254
+ });
255
+
256
+ })(document, Element.prototype);