canvasengine 2.0.0-beta.5 → 2.0.0-beta.7
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/dist/index.d.ts +25 -9
- package/dist/index.js +123 -40
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/components/Video.ts +2 -2
- package/src/components/types/DisplayObject.ts +1 -0
- package/src/engine/reactive.ts +158 -47
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as _signe_reactive from '@signe/reactive';
|
|
2
|
-
import { WritableSignal, Signal, WritableArraySignal } from '@signe/reactive';
|
|
2
|
+
import { WritableSignal, Signal, WritableArraySignal, WritableObjectSignal } from '@signe/reactive';
|
|
3
3
|
export * from '@signe/reactive';
|
|
4
4
|
import { Subscription, Subject, Observable } from 'rxjs';
|
|
5
5
|
export { isObservable } from 'rxjs';
|
|
@@ -54,6 +54,7 @@ type AlignContent = 'flex-start' | 'flex-end' | 'center' | 'space-between' | 'sp
|
|
|
54
54
|
type Size = number | `${number}%`;
|
|
55
55
|
type EdgeSize = SignalOrPrimitive<Size | [Size, Size] | [Size, Size, Size, Size]>;
|
|
56
56
|
interface DisplayObjectProps {
|
|
57
|
+
attach?: any;
|
|
57
58
|
ref?: string;
|
|
58
59
|
x?: SignalOrPrimitive<number>;
|
|
59
60
|
y?: SignalOrPrimitive<number>;
|
|
@@ -203,6 +204,12 @@ type ArrayChange<T> = {
|
|
|
203
204
|
index?: number;
|
|
204
205
|
items: T[];
|
|
205
206
|
};
|
|
207
|
+
type ObjectChange<T> = {
|
|
208
|
+
type: "add" | "remove" | "update" | "init" | "reset";
|
|
209
|
+
key?: string;
|
|
210
|
+
value?: T;
|
|
211
|
+
items: T[];
|
|
212
|
+
};
|
|
206
213
|
type NestedSignalObjects = {
|
|
207
214
|
[Key in string]: NestedSignalObjects | Signal<any>;
|
|
208
215
|
};
|
|
@@ -225,10 +232,12 @@ interface Element<T = ComponentInstance> {
|
|
|
225
232
|
destroy: () => void;
|
|
226
233
|
allElements: Subject<void>;
|
|
227
234
|
}
|
|
228
|
-
type
|
|
235
|
+
type FlowResult = {
|
|
229
236
|
elements: Element[];
|
|
230
237
|
prev?: Element;
|
|
231
|
-
|
|
238
|
+
fullElements?: Element[];
|
|
239
|
+
};
|
|
240
|
+
type FlowObservable = Observable<FlowResult>;
|
|
232
241
|
declare const isElement: (value: any) => value is Element;
|
|
233
242
|
declare const isPrimitive: (value: any) => boolean;
|
|
234
243
|
declare function registerComponent(name: any, component: any): void;
|
|
@@ -244,14 +253,21 @@ declare function registerComponent(name: any, component: any): void;
|
|
|
244
253
|
*/
|
|
245
254
|
declare function createComponent(tag: string, props?: Props): Element;
|
|
246
255
|
/**
|
|
247
|
-
* Observes a BehaviorSubject containing an array of items and dynamically creates child elements for each item.
|
|
256
|
+
* Observes a BehaviorSubject containing an array or object of items and dynamically creates child elements for each item.
|
|
248
257
|
*
|
|
249
|
-
* @param {
|
|
258
|
+
* @param {WritableArraySignal<T> | WritableObjectSignal<T>} itemsSubject - A signal that emits an array or object of items.
|
|
250
259
|
* @param {Function} createElementFn - A function that takes an item and returns an element representation.
|
|
251
260
|
* @returns {Observable} An observable that emits the list of created child elements.
|
|
252
261
|
*/
|
|
253
|
-
declare function loop<T
|
|
254
|
-
|
|
262
|
+
declare function loop<T>(itemsSubject: WritableArraySignal<T[]> | WritableObjectSignal<T>, createElementFn: (item: T, index: number | string) => Element | null): FlowObservable;
|
|
263
|
+
/**
|
|
264
|
+
* Conditionally creates and destroys elements based on a condition signal.
|
|
265
|
+
*
|
|
266
|
+
* @param {Signal<boolean> | boolean} condition - A signal or boolean that determines whether to create an element.
|
|
267
|
+
* @param {Function} createElementFn - A function that returns an element or a promise that resolves to an element.
|
|
268
|
+
* @returns {Observable} An observable that emits the created or destroyed element.
|
|
269
|
+
*/
|
|
270
|
+
declare function cond(condition: Signal<boolean> | boolean, createElementFn: () => Element | Promise<Element>): FlowObservable;
|
|
255
271
|
|
|
256
272
|
declare abstract class Directive {
|
|
257
273
|
abstract onDestroy(): any;
|
|
@@ -758,7 +774,7 @@ type SpritePropTypes = SpritePropsWithImage | SpritePropsWithSheet;
|
|
|
758
774
|
declare const Sprite: ComponentFunction<SpritePropTypes>;
|
|
759
775
|
|
|
760
776
|
interface VideoProps {
|
|
761
|
-
|
|
777
|
+
src: string;
|
|
762
778
|
paused?: boolean;
|
|
763
779
|
loop?: boolean;
|
|
764
780
|
muted?: boolean;
|
|
@@ -1098,4 +1114,4 @@ declare namespace utils {
|
|
|
1098
1114
|
export { utils_arrayEquals as arrayEquals, utils_calculateDistance as calculateDistance, utils_error as error, utils_fps2ms as fps2ms, utils_get as get, utils_isBrowser as isBrowser, utils_isFunction as isFunction, utils_isObject as isObject, utils_isPromise as isPromise, utils_log as log, utils_preciseNow as preciseNow, utils_set as set, utils_setObservablePoint as setObservablePoint };
|
|
1099
1115
|
}
|
|
1100
1116
|
|
|
1101
|
-
export { type AnimatedSignal, type AnimatedState, type ArrayChange, Canvas, Circle, type ComponentFunction, type ComponentInstance, Container, DisplayObject, EVENTS, Easing, type Element, Ellipse, Graphics, NineSliceSprite, ParticlesEmitter, type Props, RadialGradient, Rect, Scene, Sprite, Text, TilingSprite, Triangle, utils as Utils, Video, Viewport, animatedSignal, bootstrapCanvas, cond, createComponent, currentSubscriptionsTracker, h, isAnimatedSignal, isElement, isPrimitive, isTrigger, loop, mount, mountTracker, on, registerComponent, Svg as svg, tick, trigger, useDefineProps, useProps };
|
|
1117
|
+
export { type AnimatedSignal, type AnimatedState, type ArrayChange, Canvas, Circle, type ComponentFunction, type ComponentInstance, Container, DisplayObject, EVENTS, Easing, type Element, Ellipse, Graphics, NineSliceSprite, type ObjectChange, ParticlesEmitter, type Props, RadialGradient, Rect, Scene, Sprite, Text, TilingSprite, Triangle, utils as Utils, Video, Viewport, animatedSignal, bootstrapCanvas, cond, createComponent, currentSubscriptionsTracker, h, isAnimatedSignal, isElement, isPrimitive, isTrigger, loop, mount, mountTracker, on, registerComponent, Svg as svg, tick, trigger, useDefineProps, useProps };
|
package/dist/index.js
CHANGED
|
@@ -1115,8 +1115,7 @@ import {
|
|
|
1115
1115
|
defer,
|
|
1116
1116
|
from,
|
|
1117
1117
|
map,
|
|
1118
|
-
of
|
|
1119
|
-
switchMap
|
|
1118
|
+
of
|
|
1120
1119
|
} from "rxjs";
|
|
1121
1120
|
var components = {};
|
|
1122
1121
|
var isElement = (value) => {
|
|
@@ -1308,19 +1307,42 @@ function createComponent(tag, props) {
|
|
|
1308
1307
|
}
|
|
1309
1308
|
function loop(itemsSubject, createElementFn) {
|
|
1310
1309
|
let elements = [];
|
|
1311
|
-
const
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1310
|
+
const isArraySignal = "_subject" in itemsSubject && "items" in itemsSubject._subject;
|
|
1311
|
+
const addAt = (items2, insertIndex, keys2) => {
|
|
1312
|
+
return items2.map((item, index) => {
|
|
1313
|
+
const key = keys2 ? keys2[index] : typeof insertIndex === "number" ? insertIndex + index : insertIndex;
|
|
1314
|
+
const element = createElementFn(item, key);
|
|
1315
|
+
if (typeof insertIndex === "number") {
|
|
1316
|
+
elements.splice(insertIndex + index, 0, element);
|
|
1317
|
+
} else {
|
|
1318
|
+
elements.push(element);
|
|
1319
|
+
}
|
|
1315
1320
|
return element;
|
|
1316
1321
|
});
|
|
1317
1322
|
};
|
|
1323
|
+
const getInitialItems = () => {
|
|
1324
|
+
if (isArraySignal) {
|
|
1325
|
+
return {
|
|
1326
|
+
items: itemsSubject._subject.items,
|
|
1327
|
+
keys: void 0
|
|
1328
|
+
};
|
|
1329
|
+
} else {
|
|
1330
|
+
const entries = Object.entries(itemsSubject._subject.value.value);
|
|
1331
|
+
return {
|
|
1332
|
+
items: entries.map(([_, value]) => value),
|
|
1333
|
+
keys: entries.map(([key]) => key)
|
|
1334
|
+
};
|
|
1335
|
+
}
|
|
1336
|
+
};
|
|
1337
|
+
const { items, keys } = getInitialItems();
|
|
1318
1338
|
return defer(() => {
|
|
1319
|
-
let initialItems = [...
|
|
1339
|
+
let initialItems = [...items];
|
|
1340
|
+
let initialKeys = keys ? [...keys] : void 0;
|
|
1320
1341
|
let init = true;
|
|
1321
1342
|
return itemsSubject.observable.pipe(
|
|
1322
1343
|
map((event) => {
|
|
1323
|
-
const { type, items
|
|
1344
|
+
const { type, items: items2 } = event;
|
|
1345
|
+
const index = "index" in event ? event.index : event.key;
|
|
1324
1346
|
if (init) {
|
|
1325
1347
|
if (elements.length > 0) {
|
|
1326
1348
|
return {
|
|
@@ -1328,8 +1350,9 @@ function loop(itemsSubject, createElementFn) {
|
|
|
1328
1350
|
fullElements: elements
|
|
1329
1351
|
};
|
|
1330
1352
|
}
|
|
1331
|
-
const newElements = addAt(initialItems, 0);
|
|
1353
|
+
const newElements = addAt(initialItems, 0, initialKeys);
|
|
1332
1354
|
initialItems = [];
|
|
1355
|
+
initialKeys = void 0;
|
|
1333
1356
|
init = false;
|
|
1334
1357
|
return {
|
|
1335
1358
|
elements: newElements,
|
|
@@ -1342,25 +1365,60 @@ function loop(itemsSubject, createElementFn) {
|
|
|
1342
1365
|
});
|
|
1343
1366
|
elements = [];
|
|
1344
1367
|
}
|
|
1345
|
-
|
|
1368
|
+
if (!isArraySignal) {
|
|
1369
|
+
const entries = Object.entries(itemsSubject._subject.value.value);
|
|
1370
|
+
const newElements2 = addAt(
|
|
1371
|
+
entries.map(([_, value]) => value),
|
|
1372
|
+
0,
|
|
1373
|
+
entries.map(([key]) => key)
|
|
1374
|
+
);
|
|
1375
|
+
return {
|
|
1376
|
+
elements: newElements2,
|
|
1377
|
+
fullElements: elements
|
|
1378
|
+
};
|
|
1379
|
+
}
|
|
1380
|
+
const newElements = addAt(items2, 0);
|
|
1346
1381
|
return {
|
|
1347
1382
|
elements: newElements,
|
|
1348
1383
|
fullElements: elements
|
|
1349
1384
|
};
|
|
1350
1385
|
} else if (type == "add" && index != void 0) {
|
|
1351
|
-
const lastElement = elements[index - 1];
|
|
1352
|
-
|
|
1386
|
+
const lastElement = typeof index === "number" ? elements[index - 1] : elements[elements.length - 1];
|
|
1387
|
+
let newElements;
|
|
1388
|
+
if (!isArraySignal && typeof index === "string") {
|
|
1389
|
+
const value = event.value;
|
|
1390
|
+
if (value !== void 0) {
|
|
1391
|
+
newElements = [createElementFn(value, index)];
|
|
1392
|
+
elements.push(newElements[0]);
|
|
1393
|
+
} else {
|
|
1394
|
+
newElements = [];
|
|
1395
|
+
}
|
|
1396
|
+
} else {
|
|
1397
|
+
newElements = addAt(items2, index);
|
|
1398
|
+
}
|
|
1353
1399
|
return {
|
|
1354
1400
|
prev: lastElement,
|
|
1355
1401
|
elements: newElements,
|
|
1356
1402
|
fullElements: elements
|
|
1357
1403
|
};
|
|
1358
|
-
} else if (
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1404
|
+
} else if (type == "remove") {
|
|
1405
|
+
if (!isArraySignal && typeof index === "string") {
|
|
1406
|
+
const elementIndex = elements.findIndex((el) => {
|
|
1407
|
+
return el.props.text === index || el.props.key === index;
|
|
1408
|
+
});
|
|
1409
|
+
if (elementIndex !== -1) {
|
|
1410
|
+
const currentElement = elements[elementIndex];
|
|
1411
|
+
destroyElement(currentElement);
|
|
1412
|
+
elements.splice(elementIndex, 1);
|
|
1413
|
+
}
|
|
1414
|
+
} else if (typeof index === "number") {
|
|
1415
|
+
const currentElement = elements[index];
|
|
1416
|
+
destroyElement(currentElement);
|
|
1417
|
+
elements.splice(index, 1);
|
|
1418
|
+
}
|
|
1362
1419
|
return {
|
|
1363
|
-
elements: []
|
|
1420
|
+
elements: [],
|
|
1421
|
+
fullElements: elements
|
|
1364
1422
|
};
|
|
1365
1423
|
}
|
|
1366
1424
|
return {
|
|
@@ -1373,34 +1431,59 @@ function loop(itemsSubject, createElementFn) {
|
|
|
1373
1431
|
}
|
|
1374
1432
|
function cond(condition, createElementFn) {
|
|
1375
1433
|
let element = null;
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
if (
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1434
|
+
if (isSignal2(condition)) {
|
|
1435
|
+
const signalCondition = condition;
|
|
1436
|
+
return new Observable((subscriber) => {
|
|
1437
|
+
return signalCondition.observable.subscribe((bool) => {
|
|
1438
|
+
if (bool) {
|
|
1439
|
+
let _el = createElementFn();
|
|
1440
|
+
if (isPromise(_el)) {
|
|
1441
|
+
from(_el).subscribe((el) => {
|
|
1442
|
+
element = el;
|
|
1443
|
+
subscriber.next({
|
|
1385
1444
|
type: "init",
|
|
1386
1445
|
elements: [el]
|
|
1387
|
-
};
|
|
1388
|
-
})
|
|
1389
|
-
|
|
1446
|
+
});
|
|
1447
|
+
});
|
|
1448
|
+
} else {
|
|
1449
|
+
element = _el;
|
|
1450
|
+
subscriber.next({
|
|
1451
|
+
type: "init",
|
|
1452
|
+
elements: [element]
|
|
1453
|
+
});
|
|
1454
|
+
}
|
|
1455
|
+
} else if (element) {
|
|
1456
|
+
destroyElement(element);
|
|
1457
|
+
subscriber.next({
|
|
1458
|
+
elements: []
|
|
1459
|
+
});
|
|
1460
|
+
} else {
|
|
1461
|
+
subscriber.next({
|
|
1462
|
+
elements: []
|
|
1463
|
+
});
|
|
1390
1464
|
}
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1465
|
+
});
|
|
1466
|
+
});
|
|
1467
|
+
} else {
|
|
1468
|
+
if (condition) {
|
|
1469
|
+
let _el = createElementFn();
|
|
1470
|
+
if (isPromise(_el)) {
|
|
1471
|
+
return from(_el).pipe(
|
|
1472
|
+
map((el) => ({
|
|
1473
|
+
type: "init",
|
|
1474
|
+
elements: [el]
|
|
1475
|
+
}))
|
|
1476
|
+
);
|
|
1398
1477
|
}
|
|
1399
1478
|
return of({
|
|
1400
|
-
|
|
1479
|
+
type: "init",
|
|
1480
|
+
elements: [_el]
|
|
1401
1481
|
});
|
|
1402
|
-
}
|
|
1403
|
-
|
|
1482
|
+
}
|
|
1483
|
+
return of({
|
|
1484
|
+
elements: []
|
|
1485
|
+
});
|
|
1486
|
+
}
|
|
1404
1487
|
}
|
|
1405
1488
|
|
|
1406
1489
|
// src/hooks/useProps.ts
|
|
@@ -2625,7 +2708,7 @@ function Video(props) {
|
|
|
2625
2708
|
});
|
|
2626
2709
|
return h(Sprite2, {
|
|
2627
2710
|
...props,
|
|
2628
|
-
image: props.
|
|
2711
|
+
image: props.src,
|
|
2629
2712
|
loader: {
|
|
2630
2713
|
onComplete: (texture) => {
|
|
2631
2714
|
const source = texture.source.resource;
|