flet-box 1.0.3 → 1.0.4
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/package.json +3 -2
- package/src/animations/AnimatedBox.js +32 -1
- package/src/animations/AnimatedText.js +7 -3
- package/src/animations/MatrixRain.js +37 -12
- package/src/animations/ParallaxBox.js +16 -3
- package/src/animations/animate.js +22 -3
- package/src/core/runApp.js +13 -1
- package/src/index.d.ts +191 -78
- package/src/navigations/AppBar.js +8 -2
- package/src/navigations/BottomNavigation.js +14 -2
- package/src/navigations/DrawerItem.js +16 -3
- package/src/navigations/Scaffold.js +8 -1
- package/src/navigations/Tabs.js +34 -6
- package/src/tools/dimensions.js +11 -3
- package/src/tools/useState.js +115 -68
- package/src/utils/getWidgetProps.js +5 -3
- package/src/widget-factory/assignProps.js +7 -10
- package/src/widget-factory/processProps.js +7 -2
- package/src/widget-factory/reactiveValue.js +147 -0
- package/src/widget-factory/warnings.js +15 -0
- package/src/widget-factory/widgetFactory.js +27 -0
- package/src/widgets/Accordion.js +6 -5
- package/src/widgets/Audio.js +30 -8
- package/src/widgets/BottomSheet.js +24 -1
- package/src/widgets/Chart.js +4 -1
- package/src/widgets/Checkbox.js +7 -1
- package/src/widgets/CircularChart.js +4 -1
- package/src/widgets/CodeViewer.js +26 -8
- package/src/widgets/DataTable.js +11 -2
- package/src/widgets/Dropdown.js +5 -1
- package/src/widgets/Image.js +8 -1
- package/src/widgets/Input.js +27 -6
- package/src/widgets/Inspector.js +16 -10
- package/src/widgets/ListView.js +7 -1
- package/src/widgets/Markdown.js +53 -8
- package/src/widgets/Modal.js +24 -1
- package/src/widgets/QRCode.js +9 -2
- package/src/widgets/Radio.js +5 -1
- package/src/widgets/Slider.js +3 -0
- package/src/widgets/SnackBar.js +20 -2
- package/src/widgets/Switch.js +5 -1
- package/src/widgets/TreeView.js +3 -1
|
@@ -7,6 +7,14 @@ const IS_DEV = typeof FLETBOX_DEV !== 'undefined' ? FLETBOX_DEV : true;
|
|
|
7
7
|
const suggestionCache = new Map();
|
|
8
8
|
let propNames = [];
|
|
9
9
|
|
|
10
|
+
// Props already reported. Without this, every prop *application* re-printed the
|
|
11
|
+
// whole block — a captured stack trace plus ~8 styled console calls — so a widget
|
|
12
|
+
// carrying a single unknown prop paid ~137 µs on every `update()`, roughly 400×
|
|
13
|
+
// the cost of a known prop. Because `update()` re-applies every original prop, one
|
|
14
|
+
// unknown prop slowed that widget for its whole lifetime.
|
|
15
|
+
const warnedProps = new Set();
|
|
16
|
+
const warnedDeprecated = new Set();
|
|
17
|
+
|
|
10
18
|
export function setPropNames(names) {
|
|
11
19
|
propNames = names;
|
|
12
20
|
try { if (typeof window !== 'undefined') window.__FLETBOX_PROP_NAMES = names; } catch(e){}
|
|
@@ -216,6 +224,9 @@ function printDocs(prop) {
|
|
|
216
224
|
|
|
217
225
|
export function warnUnknownProp(propName, context = {}) {
|
|
218
226
|
if (!IS_DEV) return;
|
|
227
|
+
// Report each unknown prop once, not once per application.
|
|
228
|
+
if (warnedProps.has(propName)) return;
|
|
229
|
+
warnedProps.add(propName);
|
|
219
230
|
|
|
220
231
|
const suggestions = getSuggestions(propName);
|
|
221
232
|
const location = getCallLocation();
|
|
@@ -231,6 +242,10 @@ export function warnUnknownProp(propName, context = {}) {
|
|
|
231
242
|
|
|
232
243
|
export function warnDeprecatedProp(propName, replacement = null, context = {}) {
|
|
233
244
|
if (!IS_DEV) return;
|
|
245
|
+
// Same reasoning as `warnUnknownProp`: one report per prop/replacement pair.
|
|
246
|
+
const deprecatedKey = `${propName}\u0000${replacement || ""}`;
|
|
247
|
+
if (warnedDeprecated.has(deprecatedKey)) return;
|
|
248
|
+
warnedDeprecated.add(deprecatedKey);
|
|
234
249
|
|
|
235
250
|
const location = getCallLocation();
|
|
236
251
|
|
|
@@ -8,6 +8,29 @@ import { processProps } from "./processProps.js";
|
|
|
8
8
|
import { assignProps } from "./assignProps.js";
|
|
9
9
|
import { stackPosition } from "./stackPosition.js";
|
|
10
10
|
import { addChildren, removePropChildren } from "./addChildren.js";
|
|
11
|
+
import { reactiveKeyOf } from "./reactiveValue.js";
|
|
12
|
+
import { registerWidget } from "../tools/useState.js";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Auto-binds reactive props to their state key.
|
|
16
|
+
*
|
|
17
|
+
* A prop whose value is a live reactive (what `useState` returns) carries the key
|
|
18
|
+
* it came from, so the factory can subscribe this widget to that key. A later
|
|
19
|
+
* change then updates **only this widget** instead of rebuilding the whole app —
|
|
20
|
+
* which is what makes the plain
|
|
21
|
+
* `const [color, setColor] = useState("color", "red"); Widget({ bgColor: color })`
|
|
22
|
+
* syntax surgical.
|
|
23
|
+
*
|
|
24
|
+
* @param {HTMLElement} widget - The widget being built or updated.
|
|
25
|
+
* @param {Object} props - The props to scan.
|
|
26
|
+
* @returns {void}
|
|
27
|
+
*/
|
|
28
|
+
const bindReactiveProps = (widget, props) => {
|
|
29
|
+
for (const [prop, value] of Object.entries(props || {})) {
|
|
30
|
+
const key = reactiveKeyOf(value);
|
|
31
|
+
if (key) registerWidget(key, widget, prop);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
11
34
|
|
|
12
35
|
/**
|
|
13
36
|
* Main widget factory. Creates a fully functional DOM widget with reactivity,
|
|
@@ -96,6 +119,7 @@ export function WidgetFactory(tag, customProps = {}) {
|
|
|
96
119
|
// ============================================================
|
|
97
120
|
const initialProps = processProps(finalProps, finalTag, widgetName);
|
|
98
121
|
assignProps(widget, initialProps);
|
|
122
|
+
bindReactiveProps(widget, finalProps);
|
|
99
123
|
stackPosition(widget, finalProps);
|
|
100
124
|
|
|
101
125
|
if (finalProps.child !== undefined) {
|
|
@@ -160,6 +184,9 @@ export function WidgetFactory(tag, customProps = {}) {
|
|
|
160
184
|
|
|
161
185
|
const processed = processProps(merged, finalTag, widgetName);
|
|
162
186
|
assignProps(widget, processed);
|
|
187
|
+
// Only the incoming keys can introduce a new binding; the ones present at
|
|
188
|
+
// construction were registered then.
|
|
189
|
+
bindReactiveProps(widget, normalized);
|
|
163
190
|
stackPosition(widget, merged);
|
|
164
191
|
|
|
165
192
|
if (hasChild || hasChildren) {
|
package/src/widgets/Accordion.js
CHANGED
|
@@ -156,11 +156,12 @@ export const Accordion = (props) => {
|
|
|
156
156
|
titleElement.style.fontSize = `${currentTitleSize}px`;
|
|
157
157
|
titleElement.style.fontWeight = currentTitleWeight;
|
|
158
158
|
|
|
159
|
-
// Swap icon based on current expanded state
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
159
|
+
// Swap icon based on current expanded state. `Icon` renders its glyph as
|
|
160
|
+
// TEXT (a material-icons ligature), so setting a `name` attribute changed
|
|
161
|
+
// nothing and the chevron stayed on `chevron_right` forever.
|
|
162
|
+
iconElement.textContent = isExpanded
|
|
163
|
+
? currentIconExpanded
|
|
164
|
+
: currentIconCollapsed;
|
|
164
165
|
iconElement.style.color = currentIconColor;
|
|
165
166
|
iconElement.style.fontSize = `${currentIconSize}px`;
|
|
166
167
|
|
package/src/widgets/Audio.js
CHANGED
|
@@ -74,6 +74,9 @@ export const Audio = (props) => {
|
|
|
74
74
|
let duration = 0;
|
|
75
75
|
let durationPoll = null;
|
|
76
76
|
let audioContextFallbackAttempted = false;
|
|
77
|
+
// Set on unmount so deferred work (the duration fallback, in-flight fetches)
|
|
78
|
+
// never touches a discarded widget.
|
|
79
|
+
let disposed = false;
|
|
77
80
|
|
|
78
81
|
/**
|
|
79
82
|
* Recalculates the current playback position and fires the progress callbacks.
|
|
@@ -139,19 +142,25 @@ export const Audio = (props) => {
|
|
|
139
142
|
* cannot determine duration without fully downloading the file.
|
|
140
143
|
*/
|
|
141
144
|
const fallbackGetDuration = async () => {
|
|
142
|
-
if (audioContextFallbackAttempted) return;
|
|
145
|
+
if (audioContextFallbackAttempted || disposed) return;
|
|
143
146
|
audioContextFallbackAttempted = true;
|
|
144
147
|
|
|
145
148
|
try {
|
|
146
149
|
const response = await fetch(src);
|
|
147
150
|
const arrayBuffer = await response.arrayBuffer();
|
|
151
|
+
if (disposed) return;
|
|
148
152
|
const audioContext = new (
|
|
149
153
|
window.AudioContext || window.webkitAudioContext
|
|
150
154
|
)();
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
+
// `close()` must run even when decoding fails: AudioContexts are a capped
|
|
156
|
+
// resource (~6 per page in Chromium) and leaking them makes every later
|
|
157
|
+
// context fail to start, which breaks the fallback for good.
|
|
158
|
+
try {
|
|
159
|
+
const audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
|
|
160
|
+
setDuration(audioBuffer.duration);
|
|
161
|
+
} finally {
|
|
162
|
+
audioContext.close();
|
|
163
|
+
}
|
|
155
164
|
} catch (err) {
|
|
156
165
|
console.warn("[Audio] Fallback duration fetch failed:", err);
|
|
157
166
|
}
|
|
@@ -178,16 +187,29 @@ export const Audio = (props) => {
|
|
|
178
187
|
audio.addEventListener("error", (e) => console.warn("[Audio] Error:", e));
|
|
179
188
|
|
|
180
189
|
// Kick off duration detection immediately; if unavailable after 2 s, use fetch fallback
|
|
190
|
+
let fallbackTimer = null;
|
|
181
191
|
if (!tryGetDurationFromAudio()) {
|
|
182
192
|
pollDuration();
|
|
183
|
-
setTimeout(() => {
|
|
193
|
+
fallbackTimer = setTimeout(() => {
|
|
194
|
+
fallbackTimer = null;
|
|
184
195
|
if (!duration) fallbackGetDuration();
|
|
185
196
|
}, 2000);
|
|
186
197
|
}
|
|
187
198
|
|
|
188
|
-
//
|
|
199
|
+
// Release everything the widget owns on unmount. The 2 s fallback timer was
|
|
200
|
+
// never stored, so after teardown it still fetched the whole file and spun up
|
|
201
|
+
// an AudioContext (and called onLoad) for a widget that no longer exists — and
|
|
202
|
+
// a tree rebuild does that on every state change.
|
|
189
203
|
audio.onUnmount(() => {
|
|
190
|
-
|
|
204
|
+
disposed = true;
|
|
205
|
+
if (durationPoll) {
|
|
206
|
+
clearInterval(durationPoll);
|
|
207
|
+
durationPoll = null;
|
|
208
|
+
}
|
|
209
|
+
if (fallbackTimer) {
|
|
210
|
+
clearTimeout(fallbackTimer);
|
|
211
|
+
fallbackTimer = null;
|
|
212
|
+
}
|
|
191
213
|
});
|
|
192
214
|
|
|
193
215
|
// ========== PUBLIC PLAYBACK API ==========
|
|
@@ -58,6 +58,8 @@ export const BottomSheet = (props) => {
|
|
|
58
58
|
|
|
59
59
|
let isOpen = false;
|
|
60
60
|
let overlay = null;
|
|
61
|
+
// Pending "hide after the close animation" timer, so it can be cancelled.
|
|
62
|
+
let closeTimer = null;
|
|
61
63
|
let sheetContainer = null;
|
|
62
64
|
let startY = 0;
|
|
63
65
|
let currentY = 0;
|
|
@@ -190,6 +192,18 @@ onclick: (e) => e.stopPropagation(), // ✅ Prevents the click from reaching the
|
|
|
190
192
|
overlay.appendChild(sheetContainer);
|
|
191
193
|
document.body.appendChild(overlay);
|
|
192
194
|
|
|
195
|
+
// The overlay lives in `document.body` and can be removed without `close()`
|
|
196
|
+
// ever running (state rebuild, route change, plain removeChild of the host).
|
|
197
|
+
// `_cleanup` is not called on that path, so release the document listener and
|
|
198
|
+
// any pending animation timer through the lifecycle hook too.
|
|
199
|
+
overlay.onUnmount(() => {
|
|
200
|
+
if (closeTimer) {
|
|
201
|
+
clearTimeout(closeTimer);
|
|
202
|
+
closeTimer = null;
|
|
203
|
+
}
|
|
204
|
+
document.removeEventListener("keydown", handleKeyDown);
|
|
205
|
+
});
|
|
206
|
+
|
|
193
207
|
// Drag handlers
|
|
194
208
|
const onTouchStart = (e) => {
|
|
195
209
|
// ✅ Prevent the drag event from propagating to the overlay
|
|
@@ -229,6 +243,11 @@ onclick: (e) => e.stopPropagation(), // ✅ Prevents the click from reaching the
|
|
|
229
243
|
const open = () => {
|
|
230
244
|
if (isOpen) return;
|
|
231
245
|
isOpen = true;
|
|
246
|
+
// Cancel any pending hide from a close that is still animating out.
|
|
247
|
+
if (closeTimer) {
|
|
248
|
+
clearTimeout(closeTimer);
|
|
249
|
+
closeTimer = null;
|
|
250
|
+
}
|
|
232
251
|
overlay.style.visibility = "visible";
|
|
233
252
|
overlay.style.opacity = "1";
|
|
234
253
|
sheetContainer.style.transform = "translateY(0)";
|
|
@@ -250,7 +269,11 @@ onclick: (e) => e.stopPropagation(), // ✅ Prevents the click from reaching the
|
|
|
250
269
|
isOpen = false;
|
|
251
270
|
sheetContainer.style.transform = "translateY(100%)";
|
|
252
271
|
overlay.style.opacity = "0";
|
|
253
|
-
|
|
272
|
+
// Keep the id: `open()` clears it, otherwise reopening within the animation
|
|
273
|
+
// window lets a stale timer hide an OPEN sheet (invisible but isOpen true).
|
|
274
|
+
if (closeTimer) clearTimeout(closeTimer);
|
|
275
|
+
closeTimer = setTimeout(() => {
|
|
276
|
+
closeTimer = null;
|
|
254
277
|
overlay.style.visibility = "hidden";
|
|
255
278
|
}, animationDuration);
|
|
256
279
|
if (onClose) onClose();
|
package/src/widgets/Chart.js
CHANGED
|
@@ -134,7 +134,10 @@ export const Chart = (props) => {
|
|
|
134
134
|
const paddingRange = range * 0.1;
|
|
135
135
|
return {
|
|
136
136
|
maxValue: maxVal + paddingRange,
|
|
137
|
-
|
|
137
|
+
// Do NOT clamp to 0. With all-negative data that inverts the range
|
|
138
|
+
// (min > max) and every point is drawn far outside the canvas, so the
|
|
139
|
+
// chart looks empty and the axis labels run from 0 to a negative max.
|
|
140
|
+
minValue: minVal - paddingRange,
|
|
138
141
|
};
|
|
139
142
|
};
|
|
140
143
|
|
package/src/widgets/Checkbox.js
CHANGED
|
@@ -67,13 +67,19 @@ export const Checkbox = (props) => {
|
|
|
67
67
|
|
|
68
68
|
// Handle click
|
|
69
69
|
if (!disabled) {
|
|
70
|
-
|
|
70
|
+
const handleClick = (e) => {
|
|
71
71
|
e.stopPropagation();
|
|
72
72
|
const newChecked = !internalChecked;
|
|
73
73
|
internalChecked = newChecked;
|
|
74
74
|
updateUI(newChecked);
|
|
75
75
|
if (onPress) onPress(newChecked);
|
|
76
76
|
};
|
|
77
|
+
// Route the handler through the factory's props instead of assigning
|
|
78
|
+
// `onclick` directly. `assignProps` clears `onclick` on every update and only
|
|
79
|
+
// re-applies handlers it can see in the merged props, so a direct assignment
|
|
80
|
+
// died on the first `update()` — and every reactive setter (opacity, bgColor,
|
|
81
|
+
// padding…) calls update — leaving the control unable to toggle ever again.
|
|
82
|
+
checkbox.update({ onPress: handleClick });
|
|
77
83
|
|
|
78
84
|
// Hover effect
|
|
79
85
|
checkbox.addEventListener("mouseenter", () => {
|
|
@@ -335,7 +335,10 @@ export const CircularChart = (props) => {
|
|
|
335
335
|
if (!ctx || !canvas) return;
|
|
336
336
|
if (!startTime) {
|
|
337
337
|
startTime = timestamp;
|
|
338
|
-
|
|
338
|
+
// Keep the id. The previous frame's id is already spent, so cleanup would
|
|
339
|
+
// cancel nothing and the pending frame kept re-arming forever on a detached
|
|
340
|
+
// canvas — and a re-`startAnimation()` could run two loops at once.
|
|
341
|
+
animationId = requestAnimationFrame(updateAnimation);
|
|
339
342
|
return;
|
|
340
343
|
}
|
|
341
344
|
const elapsed = timestamp - startTime;
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import { WidgetFactory } from "../widget-factory/index.js";
|
|
6
6
|
import { colors } from "../utils/themes.js";
|
|
7
|
+
import { composeUpdate } from "../utils/composeUpdate.js";
|
|
7
8
|
import { generateHighlightedHtml } from "../utils/syntaxHighlight.js";
|
|
8
9
|
|
|
9
10
|
export const CodeViewer = (props) => {
|
|
@@ -81,8 +82,14 @@ code = "",
|
|
|
81
82
|
...rest,
|
|
82
83
|
});
|
|
83
84
|
|
|
84
|
-
// Header
|
|
85
|
-
|
|
85
|
+
// Header. Built through a helper so a later `title` update can create one on a
|
|
86
|
+
// widget that was constructed without it.
|
|
87
|
+
const ensureHeader = (text) => {
|
|
88
|
+
if (!showHeader || !text) return null;
|
|
89
|
+
if (headerRef) {
|
|
90
|
+
headerRef.textContent = text;
|
|
91
|
+
return headerRef;
|
|
92
|
+
}
|
|
86
93
|
headerRef = WidgetFactory({
|
|
87
94
|
tag: "div",
|
|
88
95
|
padding: "8px 12px",
|
|
@@ -91,10 +98,12 @@ code = "",
|
|
|
91
98
|
color: "#fff",
|
|
92
99
|
fontSize: "14px",
|
|
93
100
|
fontWeight: "bold",
|
|
94
|
-
textContent:
|
|
101
|
+
textContent: text,
|
|
95
102
|
});
|
|
96
|
-
container.
|
|
97
|
-
|
|
103
|
+
container.insertBefore(headerRef, container.firstChild);
|
|
104
|
+
return headerRef;
|
|
105
|
+
};
|
|
106
|
+
ensureHeader(currentTitle);
|
|
98
107
|
|
|
99
108
|
// Scroll wrapper
|
|
100
109
|
scrollWrapperRef = WidgetFactory({
|
|
@@ -209,11 +218,20 @@ code = "",
|
|
|
209
218
|
|
|
210
219
|
container.updateTitle = (newTitle) => {
|
|
211
220
|
currentTitle = newTitle;
|
|
212
|
-
|
|
213
|
-
headerRef.textContent = currentTitle;
|
|
214
|
-
}
|
|
221
|
+
ensureHeader(currentTitle);
|
|
215
222
|
};
|
|
216
223
|
|
|
224
|
+
// `title` is a reactive prop mapped to `textContent` in the prop database, so
|
|
225
|
+
// letting the factory see it would replace the header and the whole code
|
|
226
|
+
// wrapper with a bare text node. Own it and update the header instead.
|
|
227
|
+
composeUpdate(container, ["title"], (props) => {
|
|
228
|
+
if (props.title === undefined) return;
|
|
229
|
+
container.updateTitle(props.title);
|
|
230
|
+
// `makeReactive` rewrites the value into `_originalProps` on every setter
|
|
231
|
+
// call; leaving it there means the next factory update wipes the widget.
|
|
232
|
+
delete container._originalProps.title;
|
|
233
|
+
});
|
|
234
|
+
|
|
217
235
|
container.scrollTo = (x, y) => {
|
|
218
236
|
if (scrollWrapperRef) {
|
|
219
237
|
scrollWrapperRef.scrollLeft = x;
|
package/src/widgets/DataTable.js
CHANGED
|
@@ -83,7 +83,9 @@ export const DataTable = (props) => {
|
|
|
83
83
|
table.appendChild(thead);
|
|
84
84
|
|
|
85
85
|
// ========== BODY ==========
|
|
86
|
-
const
|
|
86
|
+
// `let`, not `const`: `updateData` replaces the body and must re-point here,
|
|
87
|
+
// otherwise the second call tries to replace an already-detached node.
|
|
88
|
+
let tbody = document.createElement("tbody");
|
|
87
89
|
|
|
88
90
|
rows.forEach((row, index) => {
|
|
89
91
|
const tr = document.createElement("tr");
|
|
@@ -152,8 +154,12 @@ export const DataTable = (props) => {
|
|
|
152
154
|
|
|
153
155
|
// Update table data
|
|
154
156
|
container.updateData = (newRows) => {
|
|
157
|
+
// Snapshot BEFORE clearing. `updateColumns` calls this with `rows` itself as
|
|
158
|
+
// the argument, so clearing first and then spreading produced an empty body
|
|
159
|
+
// and every row silently disappeared.
|
|
160
|
+
const nextRows = [...newRows];
|
|
155
161
|
rows.length = 0;
|
|
156
|
-
rows.push(...
|
|
162
|
+
rows.push(...nextRows);
|
|
157
163
|
|
|
158
164
|
// Rebuild body
|
|
159
165
|
const newTbody = document.createElement("tbody");
|
|
@@ -213,6 +219,9 @@ export const DataTable = (props) => {
|
|
|
213
219
|
});
|
|
214
220
|
|
|
215
221
|
table.replaceChild(newTbody, tbody);
|
|
222
|
+
// Re-point at the live node, otherwise the next call replaces an already
|
|
223
|
+
// detached node and throws NotFoundError.
|
|
224
|
+
tbody = newTbody;
|
|
216
225
|
};
|
|
217
226
|
|
|
218
227
|
// Update table columns
|
package/src/widgets/Dropdown.js
CHANGED
|
@@ -243,7 +243,11 @@ export const Dropdown = (props) => {
|
|
|
243
243
|
if (outsideClickListener)
|
|
244
244
|
document.removeEventListener("click", outsideClickListener);
|
|
245
245
|
if (resizeListener) window.removeEventListener("resize", resizeListener);
|
|
246
|
-
|
|
246
|
+
// The capture flag MUST match the one used when adding (see the `scroll`
|
|
247
|
+
// registration below), otherwise the listener is never removed: every
|
|
248
|
+
// open/close cycle leaked one window listener holding the whole closure.
|
|
249
|
+
if (scrollListener)
|
|
250
|
+
window.removeEventListener("scroll", scrollListener, true);
|
|
247
251
|
outsideClickListener = resizeListener = scrollListener = null;
|
|
248
252
|
};
|
|
249
253
|
|
package/src/widgets/Image.js
CHANGED
|
@@ -5,10 +5,17 @@
|
|
|
5
5
|
import { WidgetFactory } from "../widget-factory/index.js";
|
|
6
6
|
|
|
7
7
|
export const Image = (props) => {
|
|
8
|
+
// `fit` and `circular` are documented Image props with no entry in the prop
|
|
9
|
+
// database, so forwarding them raw only produced an "unknown prop" warning
|
|
10
|
+
// while images stayed stretched (`object-fit: fill`) and square. Map them onto
|
|
11
|
+
// real CSS. `...rest` comes last so an explicit `borderRadius` still wins.
|
|
12
|
+
const { fit, circular, ...rest } = props || {};
|
|
8
13
|
return WidgetFactory({
|
|
9
14
|
tag: "img",
|
|
10
15
|
widgetName: "Image",
|
|
11
|
-
...
|
|
16
|
+
...(fit !== undefined ? { objectFit: fit } : null),
|
|
17
|
+
...(circular ? { borderRadius: "50%" } : null),
|
|
18
|
+
...rest,
|
|
12
19
|
});
|
|
13
20
|
};
|
|
14
21
|
|
package/src/widgets/Input.js
CHANGED
|
@@ -6,6 +6,7 @@ import { WidgetFactory } from "../widget-factory/index.js";
|
|
|
6
6
|
import { colors } from "../utils/themes.js";
|
|
7
7
|
import { Icon } from "./Icon.js";
|
|
8
8
|
import { TextInputValidator } from "../utils/TextInputValidator.js";
|
|
9
|
+
import { composeUpdate } from "../utils/composeUpdate.js";
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* @typedef {Object} InputProps
|
|
@@ -314,11 +315,16 @@ export const Input = (props) => {
|
|
|
314
315
|
});
|
|
315
316
|
|
|
316
317
|
// --- Optional label rendered above the input ---
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
318
|
+
let labelEl = null;
|
|
319
|
+
const renderLabel = (text) => {
|
|
320
|
+
if (labelEl) {
|
|
321
|
+
labelEl.textContent = text;
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
if (!text) return;
|
|
325
|
+
labelEl = WidgetFactory({
|
|
320
326
|
tag: "label",
|
|
321
|
-
text
|
|
327
|
+
text,
|
|
322
328
|
fontSize: 12,
|
|
323
329
|
fontWeight: 500,
|
|
324
330
|
color: error ? colors.danger : colors.textSecondary,
|
|
@@ -329,8 +335,23 @@ export const Input = (props) => {
|
|
|
329
335
|
labelEl.addEventListener("click", () => {
|
|
330
336
|
inputElement?.focus();
|
|
331
337
|
});
|
|
332
|
-
container.
|
|
333
|
-
}
|
|
338
|
+
container.insertBefore(labelEl, container.firstChild);
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
if (label) renderLabel(label + (required ? " *" : ""));
|
|
342
|
+
|
|
343
|
+
// `label` is registered as a REACTIVE_PROP that maps to `textContent`, so if the
|
|
344
|
+
// factory ever sees it, `container.textContent = "…"` replaces this whole
|
|
345
|
+
// composite widget (label + wrapper + native <input> + icons) with a bare text
|
|
346
|
+
// node. Own the key and update the label element instead.
|
|
347
|
+
composeUpdate(container, ["label"], (props) => {
|
|
348
|
+
if (props.label === undefined) return;
|
|
349
|
+
renderLabel(props.label ? String(props.label) + (required ? " *" : "") : "");
|
|
350
|
+
// `makeReactive` writes the value back into `_originalProps` on every setter
|
|
351
|
+
// call; leaving it there means the next factory update merges it, maps it to
|
|
352
|
+
// `textContent` and wipes the widget anyway.
|
|
353
|
+
delete container._originalProps.label;
|
|
354
|
+
});
|
|
334
355
|
|
|
335
356
|
// --- Wrapper — contains icons + native input, owns the border/fill style ---
|
|
336
357
|
let wrapperStyles = {
|
package/src/widgets/Inspector.js
CHANGED
|
@@ -162,6 +162,16 @@ export const Inspector = (widget, indent = 0) => {
|
|
|
162
162
|
// Separate structural props from the rest so we can render them last
|
|
163
163
|
const { child, children, ...otherProps } = props;
|
|
164
164
|
|
|
165
|
+
// `child` may legitimately be an ARRAY (`child: [Text(...)]`), which the
|
|
166
|
+
// factory accepts. Normalise it into the children list: otherwise the subtree
|
|
167
|
+
// is dropped and — because `child` is truthy — the DOM-children fallback below
|
|
168
|
+
// is skipped too, so the whole branch disappears from the output.
|
|
169
|
+
const childNode = Array.isArray(child) ? null : child;
|
|
170
|
+
const kids = [
|
|
171
|
+
...(Array.isArray(child) ? child : []),
|
|
172
|
+
...(Array.isArray(children) ? children : []),
|
|
173
|
+
];
|
|
174
|
+
|
|
165
175
|
const propsList = Object.entries(otherProps)
|
|
166
176
|
.map(([key, value]) => {
|
|
167
177
|
const formattedValue = stringifyValue(value, indent + 1);
|
|
@@ -171,14 +181,14 @@ export const Inspector = (widget, indent = 0) => {
|
|
|
171
181
|
|
|
172
182
|
// Reconstruct a single `child` prop
|
|
173
183
|
let childStr = "";
|
|
174
|
-
if (
|
|
175
|
-
childStr = `\n${nextSpaces}child: ${Inspector(
|
|
184
|
+
if (childNode && childNode.nodeType === 1) {
|
|
185
|
+
childStr = `\n${nextSpaces}child: ${Inspector(childNode, indent + 1)}`;
|
|
176
186
|
}
|
|
177
187
|
|
|
178
|
-
// Reconstruct a `children` array prop
|
|
188
|
+
// Reconstruct a `children` array prop (including an array-valued `child`)
|
|
179
189
|
let childrenStr = "";
|
|
180
|
-
if (
|
|
181
|
-
const childrenItems =
|
|
190
|
+
if (kids.length > 0) {
|
|
191
|
+
const childrenItems = kids
|
|
182
192
|
.filter((c) => c && c.nodeType === 1)
|
|
183
193
|
.map((c) => Inspector(c, indent + 1))
|
|
184
194
|
.join(",\n" + nextSpaces + " ");
|
|
@@ -191,11 +201,7 @@ export const Inspector = (widget, indent = 0) => {
|
|
|
191
201
|
// Fall back to actual DOM children when no explicit child/children props exist
|
|
192
202
|
const domChildren = collectChildren(widget);
|
|
193
203
|
let domChildrenStr = "";
|
|
194
|
-
if (
|
|
195
|
-
domChildren.length > 0 &&
|
|
196
|
-
!child &&
|
|
197
|
-
(!children || children.length === 0)
|
|
198
|
-
) {
|
|
204
|
+
if (domChildren.length > 0 && !childNode && kids.length === 0) {
|
|
199
205
|
const domChildrenItems = domChildren
|
|
200
206
|
.map((c) => Inspector(c, indent + 1))
|
|
201
207
|
.join(",\n" + nextSpaces + " ");
|
package/src/widgets/ListView.js
CHANGED
|
@@ -407,8 +407,14 @@ export const ListView = (props) => {
|
|
|
407
407
|
const updateData = (newData) => {
|
|
408
408
|
_data = [...newData];
|
|
409
409
|
itemCache.clear();
|
|
410
|
+
// The dataset changed but the viewport did not, so the previous window
|
|
411
|
+
// bounds would make `renderVisibleItems()` bail out and leave the OLD rows
|
|
412
|
+
// on screen for same-length data.
|
|
413
|
+
visibleStart = -1;
|
|
414
|
+
visibleEnd = -1;
|
|
410
415
|
refreshUI();
|
|
411
|
-
element.data
|
|
416
|
+
// Do NOT write `element.data` here: `data` is an accessor whose setter calls
|
|
417
|
+
// this very function, so assigning to it recursed until the stack overflowed.
|
|
412
418
|
};
|
|
413
419
|
|
|
414
420
|
/**
|
package/src/widgets/Markdown.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { WidgetFactory } from "../widget-factory/index.js";
|
|
7
7
|
import { colors } from "../utils/themes.js";
|
|
8
|
+
import { composeUpdate } from "../utils/composeUpdate.js";
|
|
8
9
|
import { generateHighlightedHtml } from "../utils/syntaxHighlight.js";
|
|
9
10
|
|
|
10
11
|
/**
|
|
@@ -36,19 +37,49 @@ let markdownScopeUid = 0;
|
|
|
36
37
|
* @param {string} html - Raw HTML produced by `parseMarkdown`.
|
|
37
38
|
* @returns {string} The sanitized HTML.
|
|
38
39
|
*/
|
|
40
|
+
/** True for URL values that must never reach an attribute (see sanitizeMarkdown). */
|
|
41
|
+
const isDangerousUrl = (value) => {
|
|
42
|
+
const v = String(value).replace(/[\u0000-\u0020]/g, "").toLowerCase();
|
|
43
|
+
if (v.startsWith("javascript:") || v.startsWith("vbscript:")) return true;
|
|
44
|
+
// Inline images are legitimate; every other `data:` URL is not.
|
|
45
|
+
return v.startsWith("data:") && !v.startsWith("data:image/");
|
|
46
|
+
};
|
|
47
|
+
|
|
39
48
|
const sanitizeMarkdown = (html) => {
|
|
40
49
|
const withoutScripts = html.replace(
|
|
41
50
|
/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,
|
|
42
51
|
"",
|
|
43
52
|
);
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
53
|
+
// Parse for real and walk the tree. A regex over raw tags cannot see a `>`
|
|
54
|
+
// that sits *inside* a quoted attribute value, so `<img src="x>y" onerror=…>`
|
|
55
|
+
// used to pass through untouched while the browser kept the handler. Letting
|
|
56
|
+
// the browser tokenize removes that entire class of bypass.
|
|
57
|
+
try {
|
|
58
|
+
const doc = new DOMParser().parseFromString(withoutScripts, "text/html");
|
|
59
|
+
doc.querySelectorAll("script").forEach((node) => node.remove());
|
|
60
|
+
doc.querySelectorAll("*").forEach((node) => {
|
|
61
|
+
for (const attr of [...node.attributes]) {
|
|
62
|
+
if (attr.name.toLowerCase().startsWith("on")) {
|
|
63
|
+
node.removeAttribute(attr.name);
|
|
64
|
+
} else if (isDangerousUrl(attr.value)) {
|
|
65
|
+
// Neutralise rather than delete: the documented contract keeps the
|
|
66
|
+
// attribute and points it at "#", so links stay focusable and styled.
|
|
67
|
+
node.setAttribute(attr.name, "#");
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
return doc.body.innerHTML;
|
|
72
|
+
} catch {
|
|
73
|
+
// No DOMParser (non-browser host): keep the previous best-effort regex pass.
|
|
74
|
+
return withoutScripts.replace(/<[^<>]+>/g, (tag) =>
|
|
75
|
+
tag
|
|
76
|
+
.replace(/\son[a-z]+\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>]+)/gi, "")
|
|
77
|
+
.replace(
|
|
78
|
+
/\s(href|src|xlink:href|formaction|action)\s*=\s*(?:"javascript:[^"]*"|'javascript:[^']*'|javascript:[^\s>]+)/gi,
|
|
79
|
+
' $1="#"',
|
|
80
|
+
),
|
|
81
|
+
);
|
|
82
|
+
}
|
|
52
83
|
};
|
|
53
84
|
|
|
54
85
|
// ─── Syntax highlighting ────────────────────────────────────────────────────
|
|
@@ -1230,6 +1261,20 @@ export const Markdown = (props) => {
|
|
|
1230
1261
|
*/
|
|
1231
1262
|
container.getSource = () => markdownText;
|
|
1232
1263
|
|
|
1264
|
+
// `text`/`source`/`content` are reactive props mapped to `textContent` in the
|
|
1265
|
+
// prop database, so letting the factory see them replaced the rendered
|
|
1266
|
+
// Markdown with a bare text node. Own them and re-render instead.
|
|
1267
|
+
composeUpdate(container, ["text", "source", "content"], (props) => {
|
|
1268
|
+
const next = props.text ?? props.source ?? props.content;
|
|
1269
|
+
if (next !== undefined) container.updateContent(String(next));
|
|
1270
|
+
// `makeReactive` rewrites the value into `_originalProps` on every setter
|
|
1271
|
+
// call; leaving it there means the next factory update maps it to
|
|
1272
|
+
// `textContent` and wipes the widget anyway.
|
|
1273
|
+
for (const key of ["text", "source", "content"]) {
|
|
1274
|
+
delete container._originalProps[key];
|
|
1275
|
+
}
|
|
1276
|
+
});
|
|
1277
|
+
|
|
1233
1278
|
return container;
|
|
1234
1279
|
};
|
|
1235
1280
|
|