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