lilact 0.31.15 → 0.31.18
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/bin/bundle.cjs +1 -1
- package/dist/lilact.development.js +406 -393
- package/dist/lilact.development.js.map +4 -4
- package/dist/lilact.development.min.js +35 -35
- package/dist/lilact.development.min.js.map +4 -4
- package/dist/lilact.production.min.js +38 -38
- package/docs/assets/hierarchy.js +1 -1
- package/docs/assets/navigation.js +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/classes/accessories.ErrorBoundary.html +11 -11
- package/docs/classes/accessories.Suspense.html +10 -10
- package/docs/classes/components.Component.html +11 -11
- package/docs/classes/components.HTMLComponent.html +11 -11
- package/docs/classes/components.RootComponent.html +11 -11
- package/docs/functions/accessories.DragHandle.html +1 -1
- package/docs/functions/accessories.Spinner.html +1 -1
- package/docs/functions/components.cloneComponent.html +1 -1
- package/docs/functions/components.createComponent.html +1 -1
- package/docs/functions/components.createPortal.html +1 -1
- package/docs/functions/components.createRoot.html +1 -1
- package/docs/functions/components.memo.html +1 -1
- package/docs/functions/components.render.html +1 -1
- package/docs/functions/hooks.useLocalStorage.html +2 -2
- package/docs/functions/run.lazy.html +1 -1
- package/docs/functions/run.require.html +1 -1
- package/docs/functions/run.run.html +1 -1
- package/docs/functions/run.runScripts.html +1 -1
- package/docs/hierarchy.html +1 -1
- package/docs/modules/components.html +1 -1
- package/docs/static/index.html +1 -1
- package/docs/static/lilact.development.js +406 -393
- package/docs/static/lilact.development.js.map +4 -4
- package/docs/static/lilact.development.min.js +35 -35
- package/docs/static/lilact.development.min.js.map +4 -4
- package/docs/static/lilact.production.min.js +38 -38
- package/docs/variables/accessories.SplitPane.html +1 -1
- package/docs/variables/components.cloneElement.html +1 -1
- package/examples/index.html +1 -1
- package/examples/lilact.development.js +406 -393
- package/examples/lilact.development.js.map +4 -4
- package/examples/lilact.development.min.js +35 -35
- package/examples/lilact.development.min.js.map +4 -4
- package/examples/lilact.production.min.js +38 -38
- package/package.json +2 -2
- package/scripts/build.mjs +1 -1
- package/src/accessories.jsx +4 -2
- package/src/cache.jsx +299 -0
- package/src/components.jsx +6 -1042
- package/src/core.jsx +794 -0
- package/src/hooks.jsx +2 -2
- package/src/run.jsx +2 -1
- package/src/symbols.jsx +0 -2
- package/docs/functions/components.replaceAllComponentInstances.html +0 -1
- package/docs/functions/components.replaceComponentEntity.html +0 -1
package/src/core.jsx
ADDED
|
@@ -0,0 +1,794 @@
|
|
|
1
|
+
/*
|
|
2
|
+
|
|
3
|
+
Lilact
|
|
4
|
+
Copyright (C) 2024-2026 Arash Kazemi <contact.arash.kazemi@gmail.com>
|
|
5
|
+
All rights reserved.
|
|
6
|
+
|
|
7
|
+
BSD-2-Clause
|
|
8
|
+
|
|
9
|
+
Redistribution and use in source and binary forms, with or without
|
|
10
|
+
modification, are permitted provided that the following conditions are met:
|
|
11
|
+
|
|
12
|
+
* Redistributions of source code must retain the above copyright
|
|
13
|
+
notice, this list of conditions and the following disclaimer.
|
|
14
|
+
* Redistributions in binary form must reproduce the above copyright
|
|
15
|
+
notice, this list of conditions and the following disclaimer in the
|
|
16
|
+
documentation and/or other materials provided with the distribution.
|
|
17
|
+
|
|
18
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
19
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
20
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
21
|
+
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
|
|
22
|
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
23
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
24
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
25
|
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
26
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
27
|
+
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
28
|
+
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import Lilact, { LOCATION } from './lilact.jsx';
|
|
32
|
+
import { resolveHotEntity, ComponentCache, registerComponentCore, unregisterComponentCore } from './cache.jsx';
|
|
33
|
+
|
|
34
|
+
import { CORE, COMPONENT, TEXT, CHILD_CLASS_ADDENDUM, MEMOIZED, CLONED_STYLE } from "./symbols.jsx"
|
|
35
|
+
import { shallowEqual, toBool, isClass } from "./misc.jsx";
|
|
36
|
+
|
|
37
|
+
import { Component, HTMLComponent, RootComponent } from './components.jsx';
|
|
38
|
+
import { PropTypes } from './proptypes.jsx';
|
|
39
|
+
|
|
40
|
+
const SVG_NS = "http://www.w3.org/2000/svg";
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
const renderErrorHandler = (c, e) =>
|
|
44
|
+
{
|
|
45
|
+
const value =
|
|
46
|
+
e?.error instanceof Error
|
|
47
|
+
? e.error
|
|
48
|
+
: e?.reason !== undefined
|
|
49
|
+
? e.reason
|
|
50
|
+
: e;
|
|
51
|
+
|
|
52
|
+
const error = Lilact.isThenable(e)? e : Lilact.traceError(
|
|
53
|
+
value,
|
|
54
|
+
e?.fileName || null
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
const stack = [c];
|
|
58
|
+
|
|
59
|
+
while(c && !c.component?.componentDidCatch) {
|
|
60
|
+
c = c.parent;
|
|
61
|
+
if(c) stack.push(c);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if(c?.component?.componentDidCatch) {
|
|
65
|
+
if(c.entity?.getDerivedStateFromError) {
|
|
66
|
+
c.component.setState(c.entity.getDerivedStateFromError.call(c, error));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
let stack_log = Array.prototype
|
|
71
|
+
.map.call(stack, x => (`in ${typeof(x.component.displayName)==='function'?
|
|
72
|
+
x.component.displayName():x.component.displayName}` ) )
|
|
73
|
+
.join('\n');
|
|
74
|
+
|
|
75
|
+
error.componentStack = stack;
|
|
76
|
+
error.componentStackLog = stack_log;
|
|
77
|
+
|
|
78
|
+
if(c?.component?.componentDidCatch) {
|
|
79
|
+
c.component.componentDidCatch(error, {componentStack: stack, componentStackLog: stack_log});
|
|
80
|
+
}
|
|
81
|
+
else throw(e); // should be e not error
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
/** ComponentCore - Mostly for internal use. This is where all the component data and methods
|
|
87
|
+
* used by lilact are kept. The Component class uses it under the hood, so there is a separation
|
|
88
|
+
* and user can set whatever property they want in the component. Each Lilact.Component
|
|
89
|
+
* has a core that is accessible via lilact symbol CORE, i.e. component[CORE]. Note
|
|
90
|
+
* that you should define a LILACT:CORE symbol.
|
|
91
|
+
*
|
|
92
|
+
* ComponentCore methods are not to be called by the user. But it can also be used
|
|
93
|
+
* to store data more efficiently, and I have used it extensively. But it is
|
|
94
|
+
* better for the user to work according react paradigms instead if memory
|
|
95
|
+
* efficiency is not a high priority. It is not compatible with React API. So if
|
|
96
|
+
* you want to do so, check the code to prevent accidental shadowing of the methods
|
|
97
|
+
* and properties.
|
|
98
|
+
*
|
|
99
|
+
* @class ComponentCore
|
|
100
|
+
*/
|
|
101
|
+
|
|
102
|
+
export class ComponentCore
|
|
103
|
+
{
|
|
104
|
+
/*
|
|
105
|
+
|
|
106
|
+
// these are commented so they are not allocated by default. i just wanted to keep a list.
|
|
107
|
+
|
|
108
|
+
entity = undefined
|
|
109
|
+
|
|
110
|
+
ref = undefined;
|
|
111
|
+
|
|
112
|
+
context = undefined;
|
|
113
|
+
state = undefined;
|
|
114
|
+
|
|
115
|
+
container = null;
|
|
116
|
+
outlet = null;
|
|
117
|
+
|
|
118
|
+
depo = new ComponentCache;
|
|
119
|
+
|
|
120
|
+
element = undefined;
|
|
121
|
+
mount_state
|
|
122
|
+
|
|
123
|
+
insert_index
|
|
124
|
+
loader_args
|
|
125
|
+
|
|
126
|
+
is_svg
|
|
127
|
+
|
|
128
|
+
*/
|
|
129
|
+
|
|
130
|
+
component;
|
|
131
|
+
props;
|
|
132
|
+
|
|
133
|
+
constructor(comp, props)
|
|
134
|
+
{
|
|
135
|
+
this.component = comp;
|
|
136
|
+
this.props = props || {};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// TODO: should componentDidUpdate be called after arranging/appending the outlet or before?
|
|
140
|
+
apply(next_props = this.props, next_state = this.next_state || this.state)
|
|
141
|
+
{
|
|
142
|
+
try {
|
|
143
|
+
|
|
144
|
+
let do_rerender = true;
|
|
145
|
+
|
|
146
|
+
if(this.outlet && this?.[MEMOIZED]) {
|
|
147
|
+
|
|
148
|
+
if( shallowEqual(this.props, next_props, "children") &&
|
|
149
|
+
shallowEqual(this.props?.children, next_props?.children) ) {
|
|
150
|
+
do_rerender=false;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if(do_rerender) {
|
|
155
|
+
|
|
156
|
+
if(DEBUG) {
|
|
157
|
+
if(this.entity?.propTypes) {
|
|
158
|
+
PropTypes.checkPropTypes( this.entity.propTypes, next_props, 'prop', this.entity.name );
|
|
159
|
+
}
|
|
160
|
+
else if(this.component?.propTypes) {
|
|
161
|
+
PropTypes.checkPropTypes( this.component.propTypes, next_props, 'prop', this.component.name );
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if(typeof(next_state)==='function') next_state = next_state(this.state);
|
|
166
|
+
|
|
167
|
+
const defaultProps = this.entity?.defaultProps || this.component?.constructor?.defaultProps;
|
|
168
|
+
|
|
169
|
+
if (defaultProps) {
|
|
170
|
+
next_props = {
|
|
171
|
+
...defaultProps,
|
|
172
|
+
...(next_props || {}),
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if(next_props?.style && typeof next_props.style === 'object' && !next_props.style[CLONED_STYLE]) {
|
|
177
|
+
next_props.style = { ...next_props.style, [CLONED_STYLE]: true };
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if(this?.parent?.component?.context || this?.parent?.component?.getChildContext ) {
|
|
181
|
+
this.context = { ...this.parent.component.context, ...this.parent.component.getChildContext?.() };
|
|
182
|
+
|
|
183
|
+
if(DEBUG && this.component.constructor.contextTypes) {
|
|
184
|
+
PropTypes.checkPropTypes(this.component.constructor.contextTypes, this.context, 'context', this.entity.name);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if( this.component.shouldComponentUpdate &&
|
|
189
|
+
!this.component.shouldComponentUpdate
|
|
190
|
+
(next_state, next_props, this.context) ) return;
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
if( typeof(this.entity)==='string' ) {
|
|
194
|
+
if(!(this.element instanceof Element)) {
|
|
195
|
+
|
|
196
|
+
if(this.is_svg || this.container.is_svg) {
|
|
197
|
+
this.is_svg = true;
|
|
198
|
+
this.element = document.createElementNS(SVG_NS, this.entity);
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
this.element = document.createElement(this.entity);
|
|
202
|
+
}
|
|
203
|
+
if(next_props?.defaultValue) this.element.value = String(next_props.defaultValue).slice(0, next_props?.maxLength);
|
|
204
|
+
if(next_props?.defaultChecked) this.element.checked = next_props.defaultChecked;
|
|
205
|
+
}
|
|
206
|
+
this.element[COMPONENT] = this.component;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if(next_props.ref) {
|
|
210
|
+
if(typeof(next_props.ref)==='function') {
|
|
211
|
+
next_props.ref(this.element || this.component);
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
next_props.ref.current = this.element || this.component;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if(next_props!==undefined && this.component.componentWillReceiveProps) {
|
|
219
|
+
this.component.componentWillReceiveProps(next_props);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if(this.component.componentWillUpdate) {
|
|
223
|
+
this.component.componentWillUpdate(next_props, next_state);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const prev_state = this.state, prev_props=this.props;
|
|
227
|
+
|
|
228
|
+
if( this.element && ! (this?.component instanceof Lilact.RootComponent) ) {
|
|
229
|
+
this.updateElementProps(next_props);
|
|
230
|
+
}
|
|
231
|
+
this.props = next_props;
|
|
232
|
+
|
|
233
|
+
if(typeof this.next_state==='object') {
|
|
234
|
+
if(!this.state) this.state = {...next_state};
|
|
235
|
+
else Object.assign( this.state, next_state );
|
|
236
|
+
}
|
|
237
|
+
else if(this.next_state!==undefined) throw new Error('Component.setState only accepts objects or functions is new state.');
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
if(this.next_state) delete this.next_state;
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
if(this.hooks!==undefined) {
|
|
244
|
+
this.hook_index = 0;
|
|
245
|
+
Lilact.current_component = [this, Lilact.current_component];
|
|
246
|
+
|
|
247
|
+
this.outlet = this.component.render(next_props, {current: this.element || this.component} );
|
|
248
|
+
|
|
249
|
+
Lilact.current_component = Lilact.current_component[1];
|
|
250
|
+
}
|
|
251
|
+
else {
|
|
252
|
+
this.outlet = this.component.render({current: this.element || this.component});
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if( this?.portal ) {
|
|
256
|
+
this.element = this.portal;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if(this.outlet?.constructor?.name!=='Array') {
|
|
260
|
+
this.outlet = [this.outlet];
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
this.outlet = [...this.outlet];
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
for (let i=0;i<this.outlet.length;i++) {
|
|
267
|
+
let item = this.outlet[i];
|
|
268
|
+
|
|
269
|
+
if(item===undefined || item===null || typeof(item)==='boolean') {
|
|
270
|
+
this.outlet.splice(i, 1);
|
|
271
|
+
i--;
|
|
272
|
+
}
|
|
273
|
+
else if(typeof item==='function') {
|
|
274
|
+
const res = this.childFunctionHandler(item);
|
|
275
|
+
this.outlet.splice(i, 1, res);
|
|
276
|
+
i--;
|
|
277
|
+
}
|
|
278
|
+
else if(item.constructor.name === 'Array') {
|
|
279
|
+
this.outlet.splice(i, 1, ...item);
|
|
280
|
+
i--;
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
const core = prepareCore(this, item);
|
|
284
|
+
this.outlet[i] = core;
|
|
285
|
+
|
|
286
|
+
if(core[TEXT]===undefined) {
|
|
287
|
+
core.container = this.element? this : this.container;
|
|
288
|
+
core.apply(item.props);
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
if(!core.element) {
|
|
292
|
+
core.element = document.createTextNode(item[TEXT]);
|
|
293
|
+
}
|
|
294
|
+
else if(core[TEXT]!==item[TEXT]) {
|
|
295
|
+
core.element.textContent = item[TEXT];
|
|
296
|
+
}
|
|
297
|
+
core[TEXT] = item[TEXT];
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if(this.cache) this.cache.commit();
|
|
303
|
+
|
|
304
|
+
if(this.element) this.arrangeOutlet();
|
|
305
|
+
|
|
306
|
+
if(this.component.componentDidUpdate) {
|
|
307
|
+
this.component.componentDidUpdate(prev_props, prev_state, this.last_snapshot);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if(this.last_snapshot) delete this.last_snapshot;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
}
|
|
314
|
+
catch(e) {
|
|
315
|
+
renderErrorHandler(this, e);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
async cleanup()
|
|
321
|
+
{
|
|
322
|
+
unregisterComponentCore(this);
|
|
323
|
+
try {
|
|
324
|
+
const promises = [];
|
|
325
|
+
|
|
326
|
+
if(this.props?.ref) {
|
|
327
|
+
if(typeof(this.props.ref)==='function') {
|
|
328
|
+
this.props.ref(null);
|
|
329
|
+
}
|
|
330
|
+
else {
|
|
331
|
+
this.props.ref.current = null;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if(this.component.componentWillUnmount) {
|
|
336
|
+
this.component.componentWillUnmount();
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if(this?.element?.parentElement && !this.portal) {
|
|
340
|
+
this.element.remove();
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
if(this.outlet!==undefined) {
|
|
344
|
+
for(let c of this.outlet) {
|
|
345
|
+
if(c.cleanup) {
|
|
346
|
+
promises.push(c.cleanup());
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
if(this.props?.children!==undefined) {
|
|
352
|
+
for(let c of this.props.children) {
|
|
353
|
+
if(c.cleanup) {
|
|
354
|
+
promises.push(c.cleanup());
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if(this.hooks!==undefined) {
|
|
360
|
+
for(let h of this.hooks) {
|
|
361
|
+
if(h.cleanup) {
|
|
362
|
+
promises.push(h.cleanup());
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
await Promise.all(promises);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
catch(e) {
|
|
371
|
+
throw(e);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
updateElementProps(patch, force=false)
|
|
376
|
+
{
|
|
377
|
+
// note: string.slice(0,undefined) returns the complete string. that's why
|
|
378
|
+
// the slices are correct even if maxLength is not set.
|
|
379
|
+
|
|
380
|
+
if(this.entity==="input") {
|
|
381
|
+
if(!patch?.type) patch.type = 'text';
|
|
382
|
+
if(patch.type!==this.element.type) {
|
|
383
|
+
this.element.type=patch.type;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
if(patch?.value!==undefined && patch?.value!==this.element.value) {
|
|
387
|
+
if(patch.value===undefined) patch.value='';
|
|
388
|
+
this.element.value=String(patch.value).slice(0, patch?.maxLength);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
else if(this.entity==="textarea") {
|
|
392
|
+
if(patch?.value!==this.element.value) {
|
|
393
|
+
this.element.value=String(patch.value).slice(0, patch?.maxLength);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
else if(this.entity==="select") {
|
|
397
|
+
if(typeof(patch.value)==='string') {
|
|
398
|
+
if(patch.value!==this.element.value) {
|
|
399
|
+
this.element.value=patch.value;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
else if(this.entity==="option") {
|
|
404
|
+
if(this.container?.props?.value?.constructor?.name==='Array') {
|
|
405
|
+
patch.selected = this.container.props.value.indexOf(patch.value)!==-1;
|
|
406
|
+
}
|
|
407
|
+
else if(typeof(this.container?.props?.value)==='string') {
|
|
408
|
+
patch.selected = this.container.props.value===patch.value;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// old ones that don't exist in the new one
|
|
413
|
+
for(let a in this.props) {
|
|
414
|
+
const al = a.toLowerCase();
|
|
415
|
+
|
|
416
|
+
if( !patch.hasOwnProperty(a) || ( !a.startsWith('data-') && !a.startsWith('aria-') &&
|
|
417
|
+
(patch[a]===undefined || patch[a]===null || patch[a]==='' || patch[a]===false ) ) )
|
|
418
|
+
{
|
|
419
|
+
if( events_set.has(al) ) {
|
|
420
|
+
this.event_detachers[al]();
|
|
421
|
+
}
|
|
422
|
+
else {
|
|
423
|
+
this.element.removeAttribute(a);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
for(let a in patch) {
|
|
430
|
+
const al = a.toLowerCase();
|
|
431
|
+
|
|
432
|
+
if( special_attributes.has(al) ) continue;
|
|
433
|
+
|
|
434
|
+
if( patch===this.props || !Lilact.defaultIsEqual(patch[a], this.props[a]) || force ) {
|
|
435
|
+
|
|
436
|
+
if( events_set.has(al) ) {
|
|
437
|
+
this.event_detachers ??= {};
|
|
438
|
+
this.event_detachers[al]?.();
|
|
439
|
+
this.event_detachers[al] = Lilact.addWrappedEventListener(this.element, al.substring(2), patch[a]);
|
|
440
|
+
}
|
|
441
|
+
else if( capture_events_set.hasOwnProperty(al) ) {
|
|
442
|
+
const alc = capture_events_set[al];
|
|
443
|
+
this.event_detachers ??= {};
|
|
444
|
+
this.event_detachers[al]?.();
|
|
445
|
+
this.event_detachers[al] =
|
|
446
|
+
Lilact.addWrappedEventListener(this.element, alc.substring(2), patch[a], {capture: true});
|
|
447
|
+
}
|
|
448
|
+
else if(a==='style') {
|
|
449
|
+
if(typeof(patch.style)==='string') {
|
|
450
|
+
this.element.style = patch.style;
|
|
451
|
+
}
|
|
452
|
+
else {
|
|
453
|
+
if(this.props?.style) {
|
|
454
|
+
if(typeof(this.props.style)==='string') {
|
|
455
|
+
this.element.style = "";
|
|
456
|
+
}
|
|
457
|
+
else {
|
|
458
|
+
for(let p in this.props.style) {
|
|
459
|
+
if( p!==CLONED_STYLE && !patch?.style?.hasOwnProperty?.(p) ) {
|
|
460
|
+
this.element.style[p] = "";
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
for(const x in patch.style) {
|
|
466
|
+
if( length_css_attributes_set.has(x) ) {
|
|
467
|
+
if(isFinite(patch.style[x])) {
|
|
468
|
+
patch.style[x]+='px';
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
Object.assign(this.element.style, patch.style);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
else if(boolean_html_attributes_set.has(a)) { // not lower cased(al), as it is set as a js property
|
|
476
|
+
const v = toBool(patch[a]);
|
|
477
|
+
|
|
478
|
+
if(v!==this.element[a]) {
|
|
479
|
+
this.element[a] = v;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
else if(a==='autoFocus') { // not lower cased(al), as it is set as a js property
|
|
483
|
+
this.element['autofocus'] = toBool(patch[a]);
|
|
484
|
+
}
|
|
485
|
+
else if(a==='htmlFor' || a==='for') { // not lower cased(al), as it is set as a js property
|
|
486
|
+
this.element.setAttribute('for', patch[a]);
|
|
487
|
+
}
|
|
488
|
+
else if(al!=='value' || ['input', 'textarea', 'select'].indexOf(this.entity)===-1) {
|
|
489
|
+
this.element.setAttribute(a, patch[a]);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
if(patch?.action) {
|
|
496
|
+
this.element.onsubmit = patch.action;
|
|
497
|
+
}
|
|
498
|
+
else {
|
|
499
|
+
this.element.onsubmit = undefined;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
this.updateElementClass(patch);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
updateElementClass(patch=this.props)
|
|
506
|
+
{
|
|
507
|
+
let cn = patch?.className ?? patch?.class ?? '';
|
|
508
|
+
|
|
509
|
+
if(this?.parent?.[CHILD_CLASS_ADDENDUM]) {
|
|
510
|
+
cn += ' ' + this.parent[CHILD_CLASS_ADDENDUM];
|
|
511
|
+
}
|
|
512
|
+
cn = cn.trim();
|
|
513
|
+
|
|
514
|
+
for(const n of Array.from(this.element.classList)) {
|
|
515
|
+
if(cn.indexOf(n)===-1) {
|
|
516
|
+
this.element.classList.remove(n);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
if(cn.length>0) {
|
|
521
|
+
cn = cn.split(/\s+/g);
|
|
522
|
+
for(const n of cn) {
|
|
523
|
+
if(n.length>0) {
|
|
524
|
+
this.element.classList.add(n);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
appendElement(core)
|
|
531
|
+
{
|
|
532
|
+
if(core.portal) return;
|
|
533
|
+
|
|
534
|
+
if(core?.element.parentNode===null) {
|
|
535
|
+
core.container.element.insertBefore(
|
|
536
|
+
core.element,
|
|
537
|
+
core.container.element.childNodes[core.container.insert_index] || null
|
|
538
|
+
);
|
|
539
|
+
|
|
540
|
+
if(core?.component?.componentDidMount) {
|
|
541
|
+
core.component.componentDidMount();
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
}
|
|
545
|
+
else {
|
|
546
|
+
if(core.container.element.childNodes[core.container.insert_index]!==core.element) {
|
|
547
|
+
core.container.element.insertBefore(
|
|
548
|
+
core.element,
|
|
549
|
+
core.container.element.childNodes[core.container.insert_index] || null
|
|
550
|
+
);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
core.container.insert_index++;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
arrangeOutlet()
|
|
557
|
+
{
|
|
558
|
+
this.insert_index = 0;
|
|
559
|
+
|
|
560
|
+
for(const core of this.outlet) {
|
|
561
|
+
if(core) {
|
|
562
|
+
if(core.element) {
|
|
563
|
+
core.container = this.element?this:this.container;
|
|
564
|
+
core.container.appendElement(core);
|
|
565
|
+
}
|
|
566
|
+
else {
|
|
567
|
+
if(core.arrangeOutlet && core.outlet) core.arrangeOutlet();
|
|
568
|
+
|
|
569
|
+
// todo: is there a way to remove this useless flag?
|
|
570
|
+
if(!core?.mounted) {
|
|
571
|
+
core.mounted = true;
|
|
572
|
+
if(core?.component?.componentDidMount) {
|
|
573
|
+
core.component.componentDidMount();
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
// note: override this to tailor function children like <Transition>{(state)=>{...}}</Transition>
|
|
585
|
+
childFunctionHandler(func) {
|
|
586
|
+
return func(this.state);
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
function prepareCore(parent, core)
|
|
595
|
+
{
|
|
596
|
+
try {
|
|
597
|
+
parent.cache ??= new ComponentCache(parent);
|
|
598
|
+
|
|
599
|
+
const isText = core[TEXT] !== undefined;
|
|
600
|
+
const isExistingCore = core instanceof ComponentCore;
|
|
601
|
+
|
|
602
|
+
const entity = isText || isExistingCore
|
|
603
|
+
? undefined
|
|
604
|
+
: resolveHotEntity(core.entity);
|
|
605
|
+
|
|
606
|
+
const hasExplicitKey =
|
|
607
|
+
!isText &&
|
|
608
|
+
core.props &&
|
|
609
|
+
core.props.key !== undefined &&
|
|
610
|
+
core.props.key !== null;
|
|
611
|
+
|
|
612
|
+
let key;
|
|
613
|
+
|
|
614
|
+
if (isText) {
|
|
615
|
+
key = ':text:';
|
|
616
|
+
}
|
|
617
|
+
else if (hasExplicitKey) {
|
|
618
|
+
key = core.props.key;
|
|
619
|
+
}
|
|
620
|
+
else if (typeof entity === "function") {
|
|
621
|
+
key = getHotEntityCacheToken(entity);
|
|
622
|
+
}
|
|
623
|
+
else {
|
|
624
|
+
key = entity;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
return parent.cache.pick(
|
|
628
|
+
key,
|
|
629
|
+
() => (
|
|
630
|
+
isText || isExistingCore
|
|
631
|
+
? core
|
|
632
|
+
: constructFunc(core, parent)[CORE]
|
|
633
|
+
),
|
|
634
|
+
entity
|
|
635
|
+
);
|
|
636
|
+
}
|
|
637
|
+
catch (e) {
|
|
638
|
+
if (core?.component?.componentDidCatch) {
|
|
639
|
+
core.component.componentDidCatch(e);
|
|
640
|
+
}
|
|
641
|
+
else {
|
|
642
|
+
throw e;
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
|
|
648
|
+
|
|
649
|
+
function constructFunc(core, parent) {
|
|
650
|
+
let comp = core;
|
|
651
|
+
|
|
652
|
+
if (core[TEXT] === undefined) {
|
|
653
|
+
let entity = core.entity;
|
|
654
|
+
let memoized = false;
|
|
655
|
+
|
|
656
|
+
const loc = core?.props?.[LOCATION];
|
|
657
|
+
if(loc) delete core.props[LOCATION];
|
|
658
|
+
|
|
659
|
+
if (typeof entity === "object") {
|
|
660
|
+
if (entity[MEMOIZED]) {
|
|
661
|
+
memoized = entity[MEMOIZED];
|
|
662
|
+
entity = entity.component;
|
|
663
|
+
} else {
|
|
664
|
+
throw new Error("Invalid component.");
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
entity = resolveHotEntity(entity);
|
|
669
|
+
|
|
670
|
+
if (typeof entity === "string") {
|
|
671
|
+
comp = new HTMLComponent(entity, core.props);
|
|
672
|
+
}
|
|
673
|
+
else if (isClass(entity)) {
|
|
674
|
+
if (entity?.defaultProps) {
|
|
675
|
+
core.props = {
|
|
676
|
+
...entity.defaultProps,
|
|
677
|
+
...core.props,
|
|
678
|
+
};
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
comp = new entity(core.props);
|
|
682
|
+
|
|
683
|
+
const desc = Object.getOwnPropertyDescriptor(comp, "state");
|
|
684
|
+
|
|
685
|
+
if (desc) {
|
|
686
|
+
if (
|
|
687
|
+
typeof desc.get !== "function" &&
|
|
688
|
+
typeof desc.set !== "function"
|
|
689
|
+
) {
|
|
690
|
+
comp[CORE].state = comp.state;
|
|
691
|
+
|
|
692
|
+
Object.defineProperty(comp, "state", {
|
|
693
|
+
get() {
|
|
694
|
+
return this[CORE].state;
|
|
695
|
+
},
|
|
696
|
+
set(value) {
|
|
697
|
+
if (this[CORE].state === undefined) {
|
|
698
|
+
this[CORE].state = value;
|
|
699
|
+
} else {
|
|
700
|
+
throw new Error(
|
|
701
|
+
"Assigning component state this way is not allowed."
|
|
702
|
+
);
|
|
703
|
+
}
|
|
704
|
+
},
|
|
705
|
+
});
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
else if (typeof entity === "function") {
|
|
710
|
+
if (entity?.defaultProps) {
|
|
711
|
+
core.props = {
|
|
712
|
+
...entity.defaultProps,
|
|
713
|
+
...core.props,
|
|
714
|
+
};
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
comp = new Component(core.props);
|
|
718
|
+
comp.render = entity.bind(comp);
|
|
719
|
+
comp[CORE].hooks = [];
|
|
720
|
+
comp[CORE].hook_index = 0;
|
|
721
|
+
}
|
|
722
|
+
else {
|
|
723
|
+
throw new Error(`Error in constructing component (${String(core.entity)}).`);
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
if(loc) comp[CORE].location = loc;
|
|
727
|
+
|
|
728
|
+
comp[CORE].entity = entity;
|
|
729
|
+
registerComponentCore(comp[CORE]);
|
|
730
|
+
|
|
731
|
+
if (core.container) {
|
|
732
|
+
comp[CORE].container = core.container;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
if (memoized) {
|
|
736
|
+
comp[CORE][MEMOIZED] = true;
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
if (parent instanceof ComponentCore) {
|
|
741
|
+
comp[CORE].parent = parent;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
return comp;
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
|
|
748
|
+
/** @ignore */
|
|
749
|
+
export const special_attributes = new Set([
|
|
750
|
+
"classname", "ref", "action", "lilact_jsx_loc", "children", "key",
|
|
751
|
+
"defaultvalue", "defaultchecked"
|
|
752
|
+
]);
|
|
753
|
+
|
|
754
|
+
/** @ignore */
|
|
755
|
+
export const events_set = new Set([
|
|
756
|
+
"onafterprint","onbeforeprint","onbeforeunload","onerror","onhashchange","onload","onmessage",
|
|
757
|
+
"onoffline","ononline","onpagehide","onpageshow","onpopstate","onresize","onstorage","onunload",
|
|
758
|
+
"onblur","onchange","oncontextmenu","onfocus","oninput","oninvalid","onreset","onsearch","onselect",
|
|
759
|
+
"onsubmit", "oncancel",
|
|
760
|
+
"onkeydown","onkeypress","onkeyup",
|
|
761
|
+
"onclick","ondblclick","onmousedown","onmousemove","onmouseout","onmouseover","onmouseup","onmousewheel",
|
|
762
|
+
"onwheel",
|
|
763
|
+
"ondrag","ondragend","ondragenter","ondragleave","ondragover","ondragstart","ondrop","onscroll",
|
|
764
|
+
"oncopy","oncut","onpaste",
|
|
765
|
+
"onabort","oncanplay","oncanplaythrough","oncuechange","ondurationchange","onemptied","onended","onerror",
|
|
766
|
+
"onloadeddata","onloadedmetadata","onloadstart","onpause","onplay","onplaying","onprogress","onratechange",
|
|
767
|
+
"onseeked","onseeking","onstalled","onsuspend","ontimeupdate","onvolumechange","onwaiting",
|
|
768
|
+
"ontoggle",
|
|
769
|
+
"onpointerdown", "onpointerup", "onpointermove", "onpointercancel", "onpointerover", "onpointerout",
|
|
770
|
+
"onpointerenter", "onpointerleave"
|
|
771
|
+
]);
|
|
772
|
+
|
|
773
|
+
/** @ignore */
|
|
774
|
+
export const capture_events_set = {};
|
|
775
|
+
for (const x of events_set) {
|
|
776
|
+
capture_events_set[x+"capture"] = x;
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
/** @ignore */
|
|
780
|
+
export const length_css_attributes_set = new Set([
|
|
781
|
+
"width","height","minWidth","minHeight","maxWidth","maxHeight","top","right","bottom","left","margin",
|
|
782
|
+
"marginTop","marginRight","marginBottom","marginLeft","padding","paddingTop","paddingRight","paddingBottom",
|
|
783
|
+
"paddingLeft","borderWidth","borderTopWidth","borderRightWidth","borderBottomWidth","borderLeftWidth",
|
|
784
|
+
"outlineWidth","fontSize","lineHeight","letterSpacing","wordSpacing","textIndent","borderRadius",
|
|
785
|
+
"borderTopLeftRadius","borderTopRightRadius","borderBottomLeftRadius","borderBottomRightRadius",
|
|
786
|
+
"columnGap","rowGap","gap"
|
|
787
|
+
]);
|
|
788
|
+
|
|
789
|
+
/** @ignore */
|
|
790
|
+
export const boolean_html_attributes_set = new
|
|
791
|
+
Set(["disabled", "readOnly", "required", "checked", "multiple", "selected",
|
|
792
|
+
"hidden","open","loop","muted","controls","playsInline","allowFullScreen"]);
|
|
793
|
+
|
|
794
|
+
|