flinker-markdown 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/LICENSE.md +19 -0
- package/README.md +13 -0
- package/dist/esm/components.js +707 -0
- package/dist/esm/core.js +190 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/processor.js +155 -0
- package/dist/types/components.d.ts +156 -0
- package/dist/types/core.d.ts +91 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/processor.d.ts +23 -0
- package/package.json +48 -0
|
@@ -0,0 +1,707 @@
|
|
|
1
|
+
import { RXObservableValue } from 'flinker';
|
|
2
|
+
import { buildClassName } from './core';
|
|
3
|
+
export class UIComponent {
|
|
4
|
+
constructor(tag) {
|
|
5
|
+
this.unsubscribeColl = [];
|
|
6
|
+
this.reactions = [];
|
|
7
|
+
// Rendering
|
|
8
|
+
this.willDomUpdate = false;
|
|
9
|
+
this.affectSet = new Set();
|
|
10
|
+
this.props = {};
|
|
11
|
+
/*
|
|
12
|
+
*
|
|
13
|
+
* Destroing
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
this.isDestroyed = false;
|
|
17
|
+
this.tag = tag;
|
|
18
|
+
this.dom = document.createElement(tag);
|
|
19
|
+
UIComponent.activeParent?.addChild(this);
|
|
20
|
+
}
|
|
21
|
+
observe(rx, affect1 = 'affectsProps', affect2) {
|
|
22
|
+
this.unsubscribeColl.push(rx.pipe()
|
|
23
|
+
.onReceive(() => this.render(affect1, affect2))
|
|
24
|
+
.subscribe());
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
propsDidChange(fn) {
|
|
28
|
+
if (this.onPropsChangeSubscribers)
|
|
29
|
+
this.onPropsChangeSubscribers.push(fn);
|
|
30
|
+
else
|
|
31
|
+
this.onPropsChangeSubscribers = [fn];
|
|
32
|
+
!this.willDomUpdate && fn(this.props);
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
map(fn) {
|
|
36
|
+
if (this.mapStateFn !== fn) {
|
|
37
|
+
this.mapStateFn = fn;
|
|
38
|
+
this.render('affectsProps');
|
|
39
|
+
}
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
react(fn) {
|
|
43
|
+
this.reactions.push(fn);
|
|
44
|
+
this.render('affectsProps');
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
whenHovered(fn) {
|
|
48
|
+
this.whenHoveredFn = fn;
|
|
49
|
+
this.render('affectsProps');
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
children(fn) {
|
|
53
|
+
if (this.instantiateChildrenFn !== fn) {
|
|
54
|
+
this.instantiateChildrenFn = fn;
|
|
55
|
+
this.render('recreateChildren');
|
|
56
|
+
}
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
addChild(c) {
|
|
60
|
+
if (!this.childrenColl)
|
|
61
|
+
this.childrenColl = [];
|
|
62
|
+
this.childrenColl.push(c);
|
|
63
|
+
this.dom.appendChild(c.dom);
|
|
64
|
+
}
|
|
65
|
+
render(affect1, affect2) {
|
|
66
|
+
this.affectSet.add(affect1);
|
|
67
|
+
affect2 && this.affectSet.add(affect2);
|
|
68
|
+
if (!this.willDomUpdate) {
|
|
69
|
+
this.willDomUpdate = true;
|
|
70
|
+
setTimeout(() => this.updateDom());
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
updateDom() {
|
|
74
|
+
if (this.isDestroyed)
|
|
75
|
+
return;
|
|
76
|
+
if (!this.willDomUpdate)
|
|
77
|
+
return;
|
|
78
|
+
this.willDomUpdate = false;
|
|
79
|
+
if (this.affectSet.has('affectsProps')) {
|
|
80
|
+
this.updateProps();
|
|
81
|
+
if (this.props.visible === false) {
|
|
82
|
+
this.dom.hidden = true;
|
|
83
|
+
this.dom.className = '';
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
const cn = buildClassName(this.props, 'none');
|
|
87
|
+
this.dom.className = cn;
|
|
88
|
+
this.dom.hidden = false;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (this.affectSet.has('recreateChildren')) {
|
|
92
|
+
if (this.childrenColl) {
|
|
93
|
+
this.childrenColl?.forEach(c => c.destroy());
|
|
94
|
+
this.childrenColl = undefined;
|
|
95
|
+
}
|
|
96
|
+
if (this.instantiateChildrenFn !== undefined) {
|
|
97
|
+
const p = UIComponent.activeParent;
|
|
98
|
+
UIComponent.activeParent = this;
|
|
99
|
+
this.instantiateChildrenFn();
|
|
100
|
+
UIComponent.activeParent = p;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
else if (this.affectSet.has('affectsChildrenProps')) {
|
|
104
|
+
this.childrenColl?.forEach(c => c.render('affectsProps', 'affectsChildrenProps'));
|
|
105
|
+
}
|
|
106
|
+
if (this.affectSet.size > 0) {
|
|
107
|
+
this.didDomUpdate();
|
|
108
|
+
this.affectSet.clear();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
updateProps() {
|
|
112
|
+
const res = {};
|
|
113
|
+
this.reactions.forEach(fn => fn(res));
|
|
114
|
+
if (this.whenHoveredFn) {
|
|
115
|
+
if (!res.pseudo)
|
|
116
|
+
res.pseudo = {};
|
|
117
|
+
const state = {};
|
|
118
|
+
this.whenHoveredFn(state);
|
|
119
|
+
res.pseudo['hover'] = state;
|
|
120
|
+
}
|
|
121
|
+
if (this.mapStateFn)
|
|
122
|
+
this.mapStateFn(res);
|
|
123
|
+
if (this.props.visible === false)
|
|
124
|
+
this.props = {};
|
|
125
|
+
this.props = res;
|
|
126
|
+
if (this.onPropsChangeSubscribers)
|
|
127
|
+
this.onPropsChangeSubscribers.forEach(fn => fn(this.props));
|
|
128
|
+
}
|
|
129
|
+
didDomUpdate() {
|
|
130
|
+
if (this.props.id)
|
|
131
|
+
this.dom.id = this.props.id;
|
|
132
|
+
if (this.props.popUp)
|
|
133
|
+
this.dom.title = this.props.popUp;
|
|
134
|
+
}
|
|
135
|
+
destroy() {
|
|
136
|
+
if (!this.isDestroyed) {
|
|
137
|
+
this.isDestroyed = true;
|
|
138
|
+
this.unsubscribeColl.forEach(f => f());
|
|
139
|
+
this.unsubscribeColl.length = 0;
|
|
140
|
+
if (this.childrenColl) {
|
|
141
|
+
this.childrenColl.forEach(c => c.destroy());
|
|
142
|
+
this.childrenColl = undefined;
|
|
143
|
+
}
|
|
144
|
+
this.dom.remove();
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/*
|
|
148
|
+
*
|
|
149
|
+
* Event handlers
|
|
150
|
+
*
|
|
151
|
+
*/
|
|
152
|
+
onClick(callback) {
|
|
153
|
+
this.dom.addEventListener('click', callback);
|
|
154
|
+
return this;
|
|
155
|
+
}
|
|
156
|
+
onDoubleClick(callback) {
|
|
157
|
+
this.dom.addEventListener('dblclick', callback);
|
|
158
|
+
return this;
|
|
159
|
+
}
|
|
160
|
+
onMouseDown(callback) {
|
|
161
|
+
this.dom.addEventListener('mousedown', callback);
|
|
162
|
+
return this;
|
|
163
|
+
}
|
|
164
|
+
onKeyUp(callback) {
|
|
165
|
+
this.dom.addEventListener('keyup', callback);
|
|
166
|
+
return this;
|
|
167
|
+
}
|
|
168
|
+
onKeyDown(callback) {
|
|
169
|
+
this.dom.addEventListener('keydown', callback);
|
|
170
|
+
return this;
|
|
171
|
+
}
|
|
172
|
+
onBlur(callback) {
|
|
173
|
+
this.dom.addEventListener('blur', callback);
|
|
174
|
+
return this;
|
|
175
|
+
}
|
|
176
|
+
onFocus(callback) {
|
|
177
|
+
this.dom.addEventListener('focus', callback);
|
|
178
|
+
return this;
|
|
179
|
+
}
|
|
180
|
+
onInput(callback) {
|
|
181
|
+
this.dom.addEventListener('input', callback);
|
|
182
|
+
return this;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
export const vstackMapper = (s) => {
|
|
186
|
+
const halign = s.halign ?? 'left';
|
|
187
|
+
const valign = s.valign ?? 'top';
|
|
188
|
+
switch (halign) {
|
|
189
|
+
case 'left':
|
|
190
|
+
s.alignItems = 'flex-start';
|
|
191
|
+
break;
|
|
192
|
+
case 'center':
|
|
193
|
+
s.alignItems = 'center';
|
|
194
|
+
break;
|
|
195
|
+
case 'right':
|
|
196
|
+
s.alignItems = 'flex-end';
|
|
197
|
+
break;
|
|
198
|
+
case 'stretch':
|
|
199
|
+
s.alignItems = 'stretch';
|
|
200
|
+
break;
|
|
201
|
+
default:
|
|
202
|
+
s.alignItems = 'flex-start';
|
|
203
|
+
}
|
|
204
|
+
switch (valign) {
|
|
205
|
+
case 'top':
|
|
206
|
+
s.justifyContent = 'flex-start';
|
|
207
|
+
break;
|
|
208
|
+
case 'center':
|
|
209
|
+
s.justifyContent = 'center';
|
|
210
|
+
break;
|
|
211
|
+
case 'base':
|
|
212
|
+
s.alignItems = 'baseline';
|
|
213
|
+
break;
|
|
214
|
+
case 'bottom':
|
|
215
|
+
s.justifyContent = 'flex-end';
|
|
216
|
+
break;
|
|
217
|
+
case 'stretch':
|
|
218
|
+
s.justifyContent = 'space-between';
|
|
219
|
+
break;
|
|
220
|
+
default:
|
|
221
|
+
s.alignItems = 'flex-start';
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
export const vstack = () => {
|
|
225
|
+
return new UIComponent('div')
|
|
226
|
+
.react(s => {
|
|
227
|
+
s.display = 'flex';
|
|
228
|
+
s.flexDirection = 'column';
|
|
229
|
+
s.alignItems = 'flex-start';
|
|
230
|
+
s.justifyContent = 'center';
|
|
231
|
+
s.width = '100%';
|
|
232
|
+
s.gap = '10px';
|
|
233
|
+
s.boxSizing = 'border-box';
|
|
234
|
+
})
|
|
235
|
+
.map(vstackMapper);
|
|
236
|
+
};
|
|
237
|
+
export const hstackMapper = (s) => {
|
|
238
|
+
const halign = s.halign ?? 'left';
|
|
239
|
+
const valign = s.valign ?? 'top';
|
|
240
|
+
switch (halign) {
|
|
241
|
+
case 'left':
|
|
242
|
+
s.justifyContent = 'flex-start';
|
|
243
|
+
break;
|
|
244
|
+
case 'center':
|
|
245
|
+
s.justifyContent = 'center';
|
|
246
|
+
break;
|
|
247
|
+
case 'right':
|
|
248
|
+
s.justifyContent = 'flex-end';
|
|
249
|
+
break;
|
|
250
|
+
case 'stretch':
|
|
251
|
+
s.justifyContent = 'space-between';
|
|
252
|
+
break;
|
|
253
|
+
default:
|
|
254
|
+
s.alignItems = 'flex-start';
|
|
255
|
+
}
|
|
256
|
+
switch (valign) {
|
|
257
|
+
case 'top':
|
|
258
|
+
s.alignItems = 'flex-start';
|
|
259
|
+
break;
|
|
260
|
+
case 'center':
|
|
261
|
+
s.alignItems = 'center';
|
|
262
|
+
break;
|
|
263
|
+
case 'base':
|
|
264
|
+
s.alignItems = 'baseline';
|
|
265
|
+
break;
|
|
266
|
+
case 'bottom':
|
|
267
|
+
s.alignItems = 'flex-end';
|
|
268
|
+
break;
|
|
269
|
+
case 'stretch':
|
|
270
|
+
s.alignItems = 'stretch';
|
|
271
|
+
break;
|
|
272
|
+
default:
|
|
273
|
+
s.alignItems = 'flex-start';
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
export const hstack = () => {
|
|
277
|
+
return new UIComponent('div')
|
|
278
|
+
.react(s => {
|
|
279
|
+
s.display = 'flex';
|
|
280
|
+
s.flexDirection = 'row';
|
|
281
|
+
s.alignItems = 'flex-start';
|
|
282
|
+
s.justifyContent = 'center';
|
|
283
|
+
s.wrap = false;
|
|
284
|
+
s.boxSizing = 'border-box';
|
|
285
|
+
})
|
|
286
|
+
.map(hstackMapper);
|
|
287
|
+
};
|
|
288
|
+
export class Text extends UIComponent {
|
|
289
|
+
didDomUpdate() {
|
|
290
|
+
super.didDomUpdate();
|
|
291
|
+
if (!this.childrenColl)
|
|
292
|
+
this.dom.textContent = this.props.text ?? null;
|
|
293
|
+
if (this.props.htmlText !== undefined)
|
|
294
|
+
this.dom.setHTMLUnsafe(this.props.htmlText);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
export const div = () => {
|
|
298
|
+
return new Text('div');
|
|
299
|
+
};
|
|
300
|
+
export const p = () => {
|
|
301
|
+
return new Text('p');
|
|
302
|
+
};
|
|
303
|
+
export const span = () => {
|
|
304
|
+
return new Text('span');
|
|
305
|
+
};
|
|
306
|
+
export const h1 = () => {
|
|
307
|
+
return new Text('h1');
|
|
308
|
+
};
|
|
309
|
+
export const h2 = () => {
|
|
310
|
+
return new Text('h2');
|
|
311
|
+
};
|
|
312
|
+
export const h3 = () => {
|
|
313
|
+
return new Text('h3');
|
|
314
|
+
};
|
|
315
|
+
export const h4 = () => {
|
|
316
|
+
return new Text('h4');
|
|
317
|
+
};
|
|
318
|
+
export const h5 = () => {
|
|
319
|
+
return new Text('h5');
|
|
320
|
+
};
|
|
321
|
+
export const h6 = () => {
|
|
322
|
+
return new Text('h6');
|
|
323
|
+
};
|
|
324
|
+
export class Button extends Text {
|
|
325
|
+
whenSelected(fn) {
|
|
326
|
+
this.whenSelectedFn = fn;
|
|
327
|
+
this.render('affectsProps');
|
|
328
|
+
return this;
|
|
329
|
+
}
|
|
330
|
+
whenDisabled(fn) {
|
|
331
|
+
this.whenDisabledFn = fn;
|
|
332
|
+
this.render('affectsProps');
|
|
333
|
+
return this;
|
|
334
|
+
}
|
|
335
|
+
updateProps() {
|
|
336
|
+
const res = {};
|
|
337
|
+
this.reactions.forEach(fn => fn(res));
|
|
338
|
+
const isSelected = res.isSelected;
|
|
339
|
+
const isDisabled = res.isDisabled;
|
|
340
|
+
if (this.whenSelectedFn && isSelected) {
|
|
341
|
+
this.whenSelectedFn(res);
|
|
342
|
+
}
|
|
343
|
+
if (this.whenDisabledFn && isDisabled) {
|
|
344
|
+
this.whenDisabledFn(res);
|
|
345
|
+
}
|
|
346
|
+
if (!isSelected && !isDisabled && this.whenHoveredFn) {
|
|
347
|
+
if (!res.pseudo)
|
|
348
|
+
res.pseudo = {};
|
|
349
|
+
const state = {};
|
|
350
|
+
this.whenHoveredFn(state);
|
|
351
|
+
res.pseudo['hover'] = state;
|
|
352
|
+
}
|
|
353
|
+
if (!this.childrenColl)
|
|
354
|
+
this.dom.textContent = this.props.text ?? null;
|
|
355
|
+
if (this.mapStateFn)
|
|
356
|
+
this.mapStateFn(res);
|
|
357
|
+
this.props = res;
|
|
358
|
+
if (this.onPropsChangeSubscribers)
|
|
359
|
+
this.onPropsChangeSubscribers.forEach(fn => fn(this.props));
|
|
360
|
+
}
|
|
361
|
+
didDomUpdate() {
|
|
362
|
+
super.didDomUpdate();
|
|
363
|
+
this.props.href && this.dom.setAttribute('href', this.props.href);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
export const btn = () => {
|
|
367
|
+
return new Button('button')
|
|
368
|
+
.react(s => {
|
|
369
|
+
s.textSelectable = false;
|
|
370
|
+
})
|
|
371
|
+
.whenDisabled(s => {
|
|
372
|
+
s.opacity = '0.5';
|
|
373
|
+
s.cursor = 'not-allowed';
|
|
374
|
+
s.mouseEnabled = false;
|
|
375
|
+
});
|
|
376
|
+
};
|
|
377
|
+
export class LinkBtn extends Button {
|
|
378
|
+
didDomUpdate() {
|
|
379
|
+
super.didDomUpdate();
|
|
380
|
+
this.props.href && this.dom.setAttribute('href', this.props.href);
|
|
381
|
+
this.props.target && this.dom.setAttribute('target', this.props.target);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
export const link = () => {
|
|
385
|
+
return new LinkBtn('a')
|
|
386
|
+
.react(s => {
|
|
387
|
+
s.cursor = 'pointer';
|
|
388
|
+
});
|
|
389
|
+
};
|
|
390
|
+
export const switcher = () => {
|
|
391
|
+
const $sharedState = new RXObservableValue({});
|
|
392
|
+
return btn()
|
|
393
|
+
.react(s => {
|
|
394
|
+
s.trackWidth = 34;
|
|
395
|
+
s.trackHeight = 22;
|
|
396
|
+
s.trackColor = '#ffFFff';
|
|
397
|
+
s.thumbColor = '#000000';
|
|
398
|
+
s.thumbDiameter = 16;
|
|
399
|
+
s.minHeight = '0px';
|
|
400
|
+
s.textSelectable = false;
|
|
401
|
+
s.cornerRadius = s.trackHeight + 'px';
|
|
402
|
+
s.animate = 'background-color 300ms';
|
|
403
|
+
})
|
|
404
|
+
.whenSelected(s => {
|
|
405
|
+
s.trackColor = '#aa0000';
|
|
406
|
+
s.thumbColor = '#ffFFff';
|
|
407
|
+
})
|
|
408
|
+
.map(s => {
|
|
409
|
+
s.bgColor = s.trackColor ?? '#ff0000';
|
|
410
|
+
s.width = s.trackWidth + 'px';
|
|
411
|
+
s.height = s.trackHeight + 'px';
|
|
412
|
+
})
|
|
413
|
+
.propsDidChange(props => $sharedState.value = props)
|
|
414
|
+
.children(() => {
|
|
415
|
+
//thumb
|
|
416
|
+
div()
|
|
417
|
+
.observe($sharedState)
|
|
418
|
+
.react(s => {
|
|
419
|
+
const ss = $sharedState.value;
|
|
420
|
+
const thumbDiameter = ss.thumbDiameter ?? 0;
|
|
421
|
+
const trackWidth = ss.trackWidth ?? 0;
|
|
422
|
+
const trackHeight = ss.trackHeight ?? 0;
|
|
423
|
+
const edgeOffset = (trackHeight - thumbDiameter) / 2;
|
|
424
|
+
s.bgColor = ss.thumbColor;
|
|
425
|
+
s.width = ss.thumbDiameter + 'px';
|
|
426
|
+
s.height = ss.thumbDiameter + 'px';
|
|
427
|
+
s.cornerRadius = ss.thumbDiameter + 'px';
|
|
428
|
+
s.animateAll = '300ms';
|
|
429
|
+
s.position = 'relative';
|
|
430
|
+
s.top = '0';
|
|
431
|
+
s.left = (ss.isSelected ? trackWidth - thumbDiameter - edgeOffset : edgeOffset) + 'px';
|
|
432
|
+
});
|
|
433
|
+
});
|
|
434
|
+
};
|
|
435
|
+
/*
|
|
436
|
+
*
|
|
437
|
+
* Observer
|
|
438
|
+
*
|
|
439
|
+
**/
|
|
440
|
+
class Observer extends UIComponent {
|
|
441
|
+
constructor() {
|
|
442
|
+
super('p');
|
|
443
|
+
this.hiddenElement = this.dom;
|
|
444
|
+
this.hiddenElement.hidden = true;
|
|
445
|
+
}
|
|
446
|
+
observe(rx) {
|
|
447
|
+
this.unsubscribeColl.push(rx.pipe()
|
|
448
|
+
.debounce(0)
|
|
449
|
+
.onReceive((v) => this.replaceDom(v))
|
|
450
|
+
.subscribe());
|
|
451
|
+
return this;
|
|
452
|
+
}
|
|
453
|
+
onReceive(fn) {
|
|
454
|
+
this.onReceiveFn = fn;
|
|
455
|
+
}
|
|
456
|
+
replaceDom(value) {
|
|
457
|
+
if (this.isDestroyed)
|
|
458
|
+
return;
|
|
459
|
+
const p = UIComponent.activeParent;
|
|
460
|
+
UIComponent.activeParent = undefined;
|
|
461
|
+
const u = this.onReceiveFn?.(value);
|
|
462
|
+
UIComponent.activeParent = p;
|
|
463
|
+
if (typeof u === 'object' && 'dom' in u) {
|
|
464
|
+
this.dom.replaceWith(u.dom);
|
|
465
|
+
this.instance?.destroy();
|
|
466
|
+
this.instance = u;
|
|
467
|
+
this.dom = u.dom;
|
|
468
|
+
}
|
|
469
|
+
else if (this.dom !== this.hiddenElement) {
|
|
470
|
+
this.dom.replaceWith(this.hiddenElement);
|
|
471
|
+
this.instance?.destroy();
|
|
472
|
+
this.dom = this.hiddenElement;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
didDomUpdate() {
|
|
476
|
+
super.didDomUpdate();
|
|
477
|
+
if (this.instance && !this.instance.isDestroyed) {
|
|
478
|
+
if (this.affectSet.has('affectsProps'))
|
|
479
|
+
this.instance.render('affectsProps');
|
|
480
|
+
if (this.affectSet.has('affectsChildrenProps'))
|
|
481
|
+
this.instance.render('affectsChildrenProps');
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
destroy() {
|
|
485
|
+
super.destroy();
|
|
486
|
+
this.instance?.destroy();
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
export const observer = (rx) => {
|
|
490
|
+
const res = new Observer();
|
|
491
|
+
res.observe(rx);
|
|
492
|
+
return res;
|
|
493
|
+
};
|
|
494
|
+
/*
|
|
495
|
+
*
|
|
496
|
+
* List: div
|
|
497
|
+
*
|
|
498
|
+
**/
|
|
499
|
+
export class List extends UIComponent {
|
|
500
|
+
constructor(tag) {
|
|
501
|
+
super(tag);
|
|
502
|
+
this._itemsFn = undefined;
|
|
503
|
+
this._itemRenderer = (item) => p().react(state => state.text = item);
|
|
504
|
+
this._itemHashFn = (item) => item;
|
|
505
|
+
this._oldItemsHash = [];
|
|
506
|
+
this.childrenColl = [];
|
|
507
|
+
}
|
|
508
|
+
items(fn) {
|
|
509
|
+
this._itemsFn = fn;
|
|
510
|
+
this.render('recreateChildren');
|
|
511
|
+
return this;
|
|
512
|
+
}
|
|
513
|
+
itemRenderer(fn) {
|
|
514
|
+
this._itemRenderer = fn;
|
|
515
|
+
this.render('recreateChildren');
|
|
516
|
+
return this;
|
|
517
|
+
}
|
|
518
|
+
itemHash(fn) {
|
|
519
|
+
this._itemHashFn = fn;
|
|
520
|
+
this.render('recreateChildren');
|
|
521
|
+
return this;
|
|
522
|
+
}
|
|
523
|
+
updateDom() {
|
|
524
|
+
if (this.isDestroyed)
|
|
525
|
+
return;
|
|
526
|
+
if (!this.willDomUpdate)
|
|
527
|
+
return;
|
|
528
|
+
this.willDomUpdate = false;
|
|
529
|
+
if (this.affectSet.has('affectsProps')) {
|
|
530
|
+
this.updateProps();
|
|
531
|
+
if (this.props.visible === false) {
|
|
532
|
+
this.dom.hidden = true;
|
|
533
|
+
this.dom.className = '';
|
|
534
|
+
}
|
|
535
|
+
else {
|
|
536
|
+
const cn = buildClassName(this.props, 'none');
|
|
537
|
+
this.dom.className = cn;
|
|
538
|
+
this.dom.hidden = false;
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
if (this.affectSet.has('recreateChildren')) {
|
|
542
|
+
this.recreateChildren();
|
|
543
|
+
}
|
|
544
|
+
if (this.affectSet.has('affectsChildrenProps')) {
|
|
545
|
+
this.childrenColl?.forEach(c => c.render('affectsProps', 'affectsChildrenProps'));
|
|
546
|
+
}
|
|
547
|
+
if (this.affectSet.size > 0) {
|
|
548
|
+
this.didDomUpdate();
|
|
549
|
+
this.affectSet.clear();
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
recreateChildren() {
|
|
553
|
+
const actualItemsHash = [];
|
|
554
|
+
const actualItems = this._itemsFn ? [...this._itemsFn()] : [];
|
|
555
|
+
const actualChildren = [];
|
|
556
|
+
let index = 0;
|
|
557
|
+
if (!this.childrenColl)
|
|
558
|
+
this.childrenColl = [];
|
|
559
|
+
while (index < actualItems.length) {
|
|
560
|
+
const actualItem = actualItems[index];
|
|
561
|
+
const actualItemHash = this._itemHashFn(actualItem);
|
|
562
|
+
const oldVN = this.childrenColl[index];
|
|
563
|
+
if (index < this._oldItemsHash.length) {
|
|
564
|
+
if (actualItemHash === this._oldItemsHash[index]) {
|
|
565
|
+
actualChildren.push(oldVN);
|
|
566
|
+
}
|
|
567
|
+
else {
|
|
568
|
+
const newVN = this._itemRenderer(actualItem, index);
|
|
569
|
+
oldVN.dom.replaceWith(newVN.dom);
|
|
570
|
+
oldVN.destroy();
|
|
571
|
+
actualChildren.push(newVN);
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
else {
|
|
575
|
+
const newVN = this._itemRenderer(actualItem, index);
|
|
576
|
+
this.dom.appendChild(newVN.dom);
|
|
577
|
+
actualChildren.push(newVN);
|
|
578
|
+
}
|
|
579
|
+
actualItemsHash.push(actualItemHash);
|
|
580
|
+
index++;
|
|
581
|
+
}
|
|
582
|
+
if (index < this.childrenColl.length) {
|
|
583
|
+
this.childrenColl.slice(index).forEach(n => n.destroy());
|
|
584
|
+
}
|
|
585
|
+
this.childrenColl = actualChildren;
|
|
586
|
+
this._oldItemsHash = actualItemsHash;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
export const vlist = () => {
|
|
590
|
+
return new List('div')
|
|
591
|
+
.react(s => {
|
|
592
|
+
s.display = 'flex';
|
|
593
|
+
s.flexDirection = 'column';
|
|
594
|
+
s.alignItems = 'flex-start';
|
|
595
|
+
s.justifyContent = 'center';
|
|
596
|
+
s.width = '100%';
|
|
597
|
+
s.gap = '10px';
|
|
598
|
+
s.boxSizing = 'border-box';
|
|
599
|
+
})
|
|
600
|
+
.map(vstackMapper);
|
|
601
|
+
};
|
|
602
|
+
export const hlist = () => {
|
|
603
|
+
return new List('div')
|
|
604
|
+
.react(s => {
|
|
605
|
+
s.display = 'flex';
|
|
606
|
+
s.flexDirection = 'row';
|
|
607
|
+
s.alignItems = 'flex-start';
|
|
608
|
+
s.justifyContent = 'center';
|
|
609
|
+
s.wrap = false;
|
|
610
|
+
s.boxSizing = 'border-box';
|
|
611
|
+
})
|
|
612
|
+
.map(hstackMapper);
|
|
613
|
+
};
|
|
614
|
+
export const spacer = () => {
|
|
615
|
+
return new UIComponent('div')
|
|
616
|
+
.react(s => {
|
|
617
|
+
s.flexGrow = 1;
|
|
618
|
+
})
|
|
619
|
+
.map(s => {
|
|
620
|
+
if (s.width?.includes('px')) {
|
|
621
|
+
s.minWidth = s.width;
|
|
622
|
+
s.maxWidth = s.width;
|
|
623
|
+
}
|
|
624
|
+
if (s.height?.includes('px')) {
|
|
625
|
+
s.minHeight = s.height;
|
|
626
|
+
s.maxHeight = s.height;
|
|
627
|
+
}
|
|
628
|
+
});
|
|
629
|
+
};
|
|
630
|
+
export class Image extends Button {
|
|
631
|
+
didDomUpdate() {
|
|
632
|
+
super.didDomUpdate();
|
|
633
|
+
this.props.src && this.dom.setAttribute('src', this.props.src);
|
|
634
|
+
this.props.alt && this.dom.setAttribute('alt', this.props.alt);
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
export const image = () => {
|
|
638
|
+
return new Image('img');
|
|
639
|
+
};
|
|
640
|
+
export class Input extends UIComponent {
|
|
641
|
+
bind(rx) {
|
|
642
|
+
this.unsubscribeColl.push(rx.pipe()
|
|
643
|
+
.onReceive(v => this.dom.value = v)
|
|
644
|
+
.subscribe());
|
|
645
|
+
this.onInput((e) => rx.value = e.target.value);
|
|
646
|
+
return this;
|
|
647
|
+
}
|
|
648
|
+
whenFocused(fn) {
|
|
649
|
+
this.whenFocusedFn = fn;
|
|
650
|
+
this.render('affectsProps');
|
|
651
|
+
return this;
|
|
652
|
+
}
|
|
653
|
+
whenPlaceholderShown(fn) {
|
|
654
|
+
this.whenPlaceholderShownFn = fn;
|
|
655
|
+
this.render('affectsProps');
|
|
656
|
+
return this;
|
|
657
|
+
}
|
|
658
|
+
updateProps() {
|
|
659
|
+
const res = {};
|
|
660
|
+
this.reactions.forEach(fn => fn(res));
|
|
661
|
+
if (this.whenHoveredFn) {
|
|
662
|
+
if (!res.pseudo)
|
|
663
|
+
res.pseudo = {};
|
|
664
|
+
const state = {};
|
|
665
|
+
this.whenHoveredFn(state);
|
|
666
|
+
res.pseudo['hover'] = state;
|
|
667
|
+
}
|
|
668
|
+
if (this.whenFocusedFn) {
|
|
669
|
+
if (!res.pseudo)
|
|
670
|
+
res.pseudo = {};
|
|
671
|
+
const state = {};
|
|
672
|
+
this.whenFocusedFn(state);
|
|
673
|
+
res.pseudo['focus'] = state;
|
|
674
|
+
}
|
|
675
|
+
if (this.whenPlaceholderShownFn) {
|
|
676
|
+
if (!res.pseudo)
|
|
677
|
+
res.pseudo = {};
|
|
678
|
+
const state = {};
|
|
679
|
+
this.whenPlaceholderShownFn(state);
|
|
680
|
+
res.pseudo['placeholder'] = state;
|
|
681
|
+
}
|
|
682
|
+
if (this.mapStateFn)
|
|
683
|
+
this.mapStateFn(res);
|
|
684
|
+
this.props = res;
|
|
685
|
+
if (this.onPropsChangeSubscribers)
|
|
686
|
+
this.onPropsChangeSubscribers.forEach(fn => fn(this.props));
|
|
687
|
+
}
|
|
688
|
+
didDomUpdate() {
|
|
689
|
+
super.didDomUpdate();
|
|
690
|
+
this.props.placeholder && this.dom.setAttribute('placeholder', this.props.placeholder);
|
|
691
|
+
this.props.autoCorrect && this.dom.setAttribute('autoCorrect', this.props.autoCorrect);
|
|
692
|
+
this.props.autoComplete && this.dom.setAttribute('autoComplete', this.props.autoComplete);
|
|
693
|
+
this.props.type && this.dom.setAttribute('type', this.props.type);
|
|
694
|
+
this.props.inputMode && this.dom.setAttribute('inputMode', this.props.inputMode);
|
|
695
|
+
this.props.maxLength && this.dom.setAttribute('maxLength', this.props.maxLength);
|
|
696
|
+
this.props.caretColor && this.dom.setAttribute('caretColor', this.props.caretColor);
|
|
697
|
+
this.dom.spellcheck = this.props.spellCheck ?? false;
|
|
698
|
+
if (this.props.autoFocus)
|
|
699
|
+
this.dom.focus();
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
export const input = (type = 'text') => {
|
|
703
|
+
return new Input('input').react(s => s.type = type);
|
|
704
|
+
};
|
|
705
|
+
export const textarea = () => {
|
|
706
|
+
return new Input('textarea').react(s => s.type = 'text');
|
|
707
|
+
};
|