@dcl-regenesislabs/react-ecs 0.0.0-stage → 7.0.0-42707159203.commit-ff138cd
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/README.md +130 -2
- package/dist/components/Button/index.d.ts +15 -0
- package/dist/components/Button/index.js +62 -0
- package/dist/components/Button/types.d.ts +16 -0
- package/dist/components/Button/types.js +1 -0
- package/dist/components/Dropdown/index.d.ts +21 -0
- package/dist/components/Dropdown/index.js +48 -0
- package/dist/components/Dropdown/types.d.ts +13 -0
- package/dist/components/Dropdown/types.js +1 -0
- package/dist/components/Input/index.d.ts +25 -0
- package/dist/components/Input/index.js +49 -0
- package/dist/components/Input/types.d.ts +15 -0
- package/dist/components/Input/types.js +1 -0
- package/dist/components/InteractableArea/index.d.ts +23 -0
- package/dist/components/InteractableArea/index.js +34 -0
- package/dist/components/InteractableArea/types.d.ts +23 -0
- package/dist/components/InteractableArea/types.js +1 -0
- package/dist/components/Label/index.d.ts +17 -0
- package/dist/components/Label/index.js +37 -0
- package/dist/components/Label/types.d.ts +34 -0
- package/dist/components/Label/types.js +1 -0
- package/dist/components/Label/utils.d.ts +11 -0
- package/dist/components/Label/utils.js +69 -0
- package/dist/components/ScreenInsetArea/index.d.ts +22 -0
- package/dist/components/ScreenInsetArea/index.js +33 -0
- package/dist/components/ScreenInsetArea/types.d.ts +22 -0
- package/dist/components/ScreenInsetArea/types.js +1 -0
- package/dist/components/index.d.ts +26 -0
- package/dist/components/index.js +36 -0
- package/dist/components/listeners/types.d.ts +19 -0
- package/dist/components/listeners/types.js +11 -0
- package/dist/components/types.d.ts +45 -0
- package/dist/components/types.js +1 -0
- package/dist/components/uiBackground/index.d.ts +6 -0
- package/dist/components/uiBackground/index.js +16 -0
- package/dist/components/uiBackground/types.d.ts +61 -0
- package/dist/components/uiBackground/types.js +1 -0
- package/dist/components/uiBackground/utils.d.ts +1 -0
- package/dist/components/uiBackground/utils.js +51 -0
- package/dist/components/uiTransform/index.d.ts +6 -0
- package/dist/components/uiTransform/index.js +91 -0
- package/dist/components/uiTransform/types.d.ts +134 -0
- package/dist/components/uiTransform/types.js +1 -0
- package/dist/components/uiTransform/utils.d.ts +42 -0
- package/dist/components/uiTransform/utils.js +246 -0
- package/dist/components/utils.d.ts +1 -0
- package/dist/components/utils.js +195 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +15 -0
- package/dist/platform.d.ts +12 -0
- package/dist/platform.js +26 -0
- package/dist/react-ecs.d.ts +87 -0
- package/dist/react-ecs.js +10 -0
- package/dist/reconciler/index.d.ts +6 -0
- package/dist/reconciler/index.js +322 -0
- package/dist/reconciler/types.d.ts +31 -0
- package/dist/reconciler/types.js +1 -0
- package/dist/reconciler/utils.d.ts +40 -0
- package/dist/reconciler/utils.js +174 -0
- package/dist/system.d.ts +92 -0
- package/dist/system.js +264 -0
- package/package.json +41 -3
package/dist/platform.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Platform detection hook for the UI scale system.
|
|
3
|
+
*
|
|
4
|
+
* react-ecs stays independent of the scene runtime (`~system/*` modules), so
|
|
5
|
+
* it cannot ask the explorer which platform it runs on. Instead, the host SDK
|
|
6
|
+
* injects the check at module-load time (see `@dcl/sdk/react-ecs`). Until a
|
|
7
|
+
* provider is injected — or when none ever is — the platform is assumed to be
|
|
8
|
+
* non-mobile.
|
|
9
|
+
*/
|
|
10
|
+
let isMobileProvider = () => false;
|
|
11
|
+
/**
|
|
12
|
+
* Injects the function used to detect whether the scene runs on a mobile
|
|
13
|
+
* platform. Called by `@dcl/sdk` with its `isMobile()` platform helper.
|
|
14
|
+
*/
|
|
15
|
+
export function setIsMobileProvider(provider) {
|
|
16
|
+
isMobileProvider = provider;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Whether the scene is running on a mobile platform, according to the
|
|
20
|
+
* injected provider. Platform detection is asynchronous on the SDK side, so
|
|
21
|
+
* this may return false during the first ticks and flip to true once the
|
|
22
|
+
* explorer information arrives.
|
|
23
|
+
*/
|
|
24
|
+
export function isMobile() {
|
|
25
|
+
return isMobileProvider();
|
|
26
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { PBUiBackground, PBUiText, PBUiTransform, PBUiInput, PBUiDropdown, PBUiInputBinding } from '@dcl/ecs';
|
|
2
|
+
import { Callback, Key } from './components';
|
|
3
|
+
/**
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export interface EcsElements {
|
|
7
|
+
entity: Partial<EntityComponents> & {
|
|
8
|
+
children?: ReactEcs.JSX.ReactNode;
|
|
9
|
+
key?: Key;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
export type EntityComponents = {
|
|
16
|
+
uiTransform: PBUiTransform;
|
|
17
|
+
uiText: PBUiText;
|
|
18
|
+
uiBackground: PBUiBackground;
|
|
19
|
+
uiInput: PBUiInput;
|
|
20
|
+
uiDropdown: PBUiDropdown;
|
|
21
|
+
uiInputBinding: PBUiInputBinding;
|
|
22
|
+
onMouseDown: Callback;
|
|
23
|
+
onMouseUp: Callback;
|
|
24
|
+
onMouseEnter: Callback;
|
|
25
|
+
onMouseLeave: Callback;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* @hidden
|
|
29
|
+
*/
|
|
30
|
+
export declare namespace JSX {
|
|
31
|
+
interface Element extends ReactElement<any, any> {
|
|
32
|
+
}
|
|
33
|
+
interface IntrinsicElements extends EcsElements {
|
|
34
|
+
}
|
|
35
|
+
interface Component {
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* @public
|
|
40
|
+
*/
|
|
41
|
+
export type JSXElementConstructor<P> = (props: P) => ReactElement<any, any> | null;
|
|
42
|
+
/**
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
export interface ReactElement<P = any, T extends string | JSXElementConstructor<any> = string | JSXElementConstructor<any>> {
|
|
46
|
+
type: T;
|
|
47
|
+
props: P;
|
|
48
|
+
key: Key | null;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* @public
|
|
52
|
+
*/
|
|
53
|
+
export declare namespace ReactEcs {
|
|
54
|
+
export namespace JSX {
|
|
55
|
+
/**
|
|
56
|
+
* @public
|
|
57
|
+
*/
|
|
58
|
+
type ReactNode = Element | ReactElement | string | number | boolean | null | undefined | ReactNode[];
|
|
59
|
+
/**
|
|
60
|
+
* @public
|
|
61
|
+
*/
|
|
62
|
+
interface Element extends ReactElement<any, any> {
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* @public
|
|
66
|
+
* HTML tag elements
|
|
67
|
+
*/
|
|
68
|
+
interface IntrinsicElements extends EcsElements {
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* @public
|
|
72
|
+
* Component empty interface
|
|
73
|
+
*/
|
|
74
|
+
interface Component {
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
export const createElement: any;
|
|
78
|
+
type SetStateAction<T> = T | ((prevState: T) => T);
|
|
79
|
+
type Dispatch<T> = (action: SetStateAction<T>) => void;
|
|
80
|
+
type StateHook = <T>(initialState: T | (() => T)) => [T, Dispatch<T>];
|
|
81
|
+
type DependencyList = ReadonlyArray<any>;
|
|
82
|
+
type EffectCallback = () => void | (() => void | undefined);
|
|
83
|
+
type EffectHook = (effect: EffectCallback, deps?: DependencyList) => void;
|
|
84
|
+
export const useEffect: EffectHook;
|
|
85
|
+
export const useState: StateHook;
|
|
86
|
+
export {};
|
|
87
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Entity, IEngine, PointerEventsSystem } from '@dcl/ecs';
|
|
2
|
+
import { ReactEcs } from '../react-ecs';
|
|
3
|
+
export declare function createReconciler(engine: Pick<IEngine, 'getComponent' | 'addEntity' | 'removeEntity' | 'defineComponentFromSchema' | 'getEntitiesWith'>, pointerEvents: PointerEventsSystem): {
|
|
4
|
+
update: (component: ReactEcs.JSX.ReactNode) => number;
|
|
5
|
+
getEntities: () => Entity[];
|
|
6
|
+
};
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import * as components from '@dcl/ecs/dist/components';
|
|
2
|
+
import Reconciler from 'react-reconciler';
|
|
3
|
+
import { isListener } from '../components';
|
|
4
|
+
import { CANVAS_ROOT_ENTITY } from '../components/uiTransform';
|
|
5
|
+
import { componentKeys, isNotUndefined, noopConfig, propsChanged } from './utils';
|
|
6
|
+
function getPointerEnum(pointerKey) {
|
|
7
|
+
const pointers = {
|
|
8
|
+
onMouseDown: 1 /* PointerEventType.PET_DOWN */,
|
|
9
|
+
onMouseUp: 0 /* PointerEventType.PET_UP */,
|
|
10
|
+
onMouseEnter: 2 /* PointerEventType.PET_HOVER_ENTER */,
|
|
11
|
+
onMouseLeave: 3 /* PointerEventType.PET_HOVER_LEAVE */
|
|
12
|
+
};
|
|
13
|
+
return pointers[pointerKey];
|
|
14
|
+
}
|
|
15
|
+
export function createReconciler(engine, pointerEvents) {
|
|
16
|
+
// Store all the entities so when we destroy the UI we can also destroy them
|
|
17
|
+
const entities = new Set();
|
|
18
|
+
// Store the onChange callbacks to be runned every time a Result has changed
|
|
19
|
+
const changeEvents = new Map();
|
|
20
|
+
const clickEvents = new Map();
|
|
21
|
+
// Track the last value reported by the renderer for each input entity,
|
|
22
|
+
// so we can avoid echoing it back and causing keystroke drops.
|
|
23
|
+
const lastInputResultValues = new Map();
|
|
24
|
+
// Initialize components
|
|
25
|
+
const UiTransform = components.UiTransform(engine);
|
|
26
|
+
const UiText = components.UiText(engine);
|
|
27
|
+
const UiBackground = components.UiBackground(engine);
|
|
28
|
+
const UiInput = components.UiInput(engine);
|
|
29
|
+
const UiInputResult = components.UiInputResult(engine);
|
|
30
|
+
const UiDropdown = components.UiDropdown(engine);
|
|
31
|
+
const UiDropdownResult = components.UiDropdownResult(engine);
|
|
32
|
+
const UiInputBinding = components.UiInputBinding(engine);
|
|
33
|
+
// Component ID Helper
|
|
34
|
+
const getComponentId = {
|
|
35
|
+
uiTransform: UiTransform.componentId,
|
|
36
|
+
uiText: UiText.componentId,
|
|
37
|
+
uiBackground: UiBackground.componentId,
|
|
38
|
+
uiInput: UiInput.componentId,
|
|
39
|
+
uiDropdown: UiDropdown.componentId,
|
|
40
|
+
uiInputBinding: UiInputBinding.componentId
|
|
41
|
+
};
|
|
42
|
+
function pointerEventCallback(entity, pointerEvent) {
|
|
43
|
+
const callback = clickEvents.get(entity)?.get(pointerEvent);
|
|
44
|
+
if (callback)
|
|
45
|
+
callback();
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
function updateTree(instance, props) {
|
|
49
|
+
// `rightOf` is a required int32 where 0 means "first sibling". A missing left
|
|
50
|
+
// neighbour is written as 0 so the first child of any parent (the canvas root
|
|
51
|
+
// included) serializes the same way as an explicit `rightOf: 0`.
|
|
52
|
+
upsertComponent(instance, { ...props, rightOf: props.rightOf ?? 0 }, 'uiTransform');
|
|
53
|
+
}
|
|
54
|
+
function upsertListener(instance, update) {
|
|
55
|
+
if (update.type === 'delete' || !update.props) {
|
|
56
|
+
clickEvents.get(instance.entity)?.delete(getPointerEnum(update.component));
|
|
57
|
+
if (update.component === 'onMouseDown') {
|
|
58
|
+
pointerEvents.removeOnPointerDown(instance.entity);
|
|
59
|
+
}
|
|
60
|
+
else if (update.component === 'onMouseUp') {
|
|
61
|
+
pointerEvents.removeOnPointerUp(instance.entity);
|
|
62
|
+
}
|
|
63
|
+
else if (update.component === 'onMouseEnter') {
|
|
64
|
+
pointerEvents.removeOnPointerHoverEnter(instance.entity);
|
|
65
|
+
}
|
|
66
|
+
else if (update.component === 'onMouseLeave') {
|
|
67
|
+
pointerEvents.removeOnPointerHoverLeave(instance.entity);
|
|
68
|
+
}
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (update.props) {
|
|
72
|
+
const pointerEvent = getPointerEnum(update.component);
|
|
73
|
+
const entityEvent = clickEvents.get(instance.entity) || clickEvents.set(instance.entity, new Map()).get(instance.entity);
|
|
74
|
+
const alreadyHasPointerEvent = entityEvent.get(pointerEvent);
|
|
75
|
+
entityEvent.set(pointerEvent, update.props);
|
|
76
|
+
if (alreadyHasPointerEvent)
|
|
77
|
+
return;
|
|
78
|
+
const pointerEventSystem = update.component === 'onMouseDown'
|
|
79
|
+
? pointerEvents.onPointerDown
|
|
80
|
+
: update.component === 'onMouseUp'
|
|
81
|
+
? pointerEvents.onPointerUp
|
|
82
|
+
: update.component === 'onMouseEnter'
|
|
83
|
+
? pointerEvents.onPointerHoverEnter
|
|
84
|
+
: update.component === 'onMouseLeave' && pointerEvents.onPointerHoverLeave;
|
|
85
|
+
if (pointerEventSystem) {
|
|
86
|
+
pointerEventSystem({
|
|
87
|
+
entity: instance.entity,
|
|
88
|
+
opts: {
|
|
89
|
+
button: 0 /* InputAction.IA_POINTER */,
|
|
90
|
+
// We add this showFeedBack so the pointerEventSystem creates a PointerEvent component with our entity
|
|
91
|
+
// This is needed for the renderer to know which entities are clickeables
|
|
92
|
+
showFeedback: true
|
|
93
|
+
}
|
|
94
|
+
}, () => pointerEventCallback(instance.entity, pointerEvent));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function removeComponent(instance, component) {
|
|
99
|
+
const componentId = getComponentId[component];
|
|
100
|
+
const Component = engine.getComponent(componentId);
|
|
101
|
+
Component.deleteFrom(instance.entity);
|
|
102
|
+
}
|
|
103
|
+
function upsertComponent(instance, props = {}, componentName) {
|
|
104
|
+
const componentId = getComponentId[componentName];
|
|
105
|
+
const onChangeExists = 'onChange' in props;
|
|
106
|
+
const onSubmitExists = 'onSubmit' in props;
|
|
107
|
+
const entityState = changeEvents.get(instance.entity)?.get(componentId);
|
|
108
|
+
const onChange = onChangeExists
|
|
109
|
+
? props['onChange']
|
|
110
|
+
: entityState?.onChangeCallback;
|
|
111
|
+
const onSubmit = onSubmitExists
|
|
112
|
+
? props['onSubmit']
|
|
113
|
+
: entityState?.onSubmitCallback;
|
|
114
|
+
if (onChangeExists || onSubmitExists) {
|
|
115
|
+
updateOnChange(instance.entity, componentId, {
|
|
116
|
+
onChangeCallback: onChange,
|
|
117
|
+
onSubmitCallback: onSubmit
|
|
118
|
+
});
|
|
119
|
+
delete props.onChange;
|
|
120
|
+
delete props.onSubmit;
|
|
121
|
+
}
|
|
122
|
+
// Prevent keystroke drops: when React echoes back the same value the renderer
|
|
123
|
+
// reported, strip it from the props so the component isn't marked dirty for it.
|
|
124
|
+
// This avoids sending a stale value that overwrites what the user is currently typing.
|
|
125
|
+
if (componentName === 'uiInput' &&
|
|
126
|
+
'value' in props &&
|
|
127
|
+
lastInputResultValues.has(instance.entity) &&
|
|
128
|
+
props.value === lastInputResultValues.get(instance.entity)) {
|
|
129
|
+
delete props.value;
|
|
130
|
+
}
|
|
131
|
+
// We check if there is any key pending to be changed to avoid updating the existing component
|
|
132
|
+
if (!Object.keys(props).length) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
const ComponentDef = engine.getComponent(componentId);
|
|
136
|
+
const component = ComponentDef.getMutableOrNull(instance.entity) || ComponentDef.create(instance.entity);
|
|
137
|
+
for (const key in props) {
|
|
138
|
+
const keyProp = key;
|
|
139
|
+
component[keyProp] = props[keyProp];
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
function removeChildEntity(instance) {
|
|
143
|
+
entities.delete(instance.entity);
|
|
144
|
+
changeEvents.delete(instance.entity);
|
|
145
|
+
clickEvents.delete(instance.entity);
|
|
146
|
+
lastInputResultValues.delete(instance.entity);
|
|
147
|
+
engine.removeEntity(instance.entity);
|
|
148
|
+
for (const child of instance._child) {
|
|
149
|
+
removeChildEntity(child);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function appendChild(parent, child) {
|
|
153
|
+
const isReorder = parent._child.find((c) => c.entity === child.entity);
|
|
154
|
+
// If its a reorder its seems that its a mutation of an array with key prop
|
|
155
|
+
// We need to move the child to the end of the array
|
|
156
|
+
// And update the order of the parent_.child array
|
|
157
|
+
// child.rightOf => Latest entity of the array
|
|
158
|
+
// childThatWasAtRightOfEntity = childEntity.rightOf
|
|
159
|
+
if (isReorder) {
|
|
160
|
+
const rightOfChild = parent._child.find((c) => c.rightOf === child.entity);
|
|
161
|
+
if (rightOfChild) {
|
|
162
|
+
rightOfChild.rightOf = child.rightOf;
|
|
163
|
+
updateTree(rightOfChild, { rightOf: rightOfChild.rightOf });
|
|
164
|
+
}
|
|
165
|
+
// Always remove from old position and push to end for reorders.
|
|
166
|
+
// Previously, filter/push was inside the if(rightOfChild) block, so when
|
|
167
|
+
// rightOfChild was not found (child was last), the child stayed at its old
|
|
168
|
+
// position. This caused _child[length-2] to potentially be the child itself,
|
|
169
|
+
// producing a self-referencing rightOf.
|
|
170
|
+
parent._child = parent._child.filter((c) => c.entity !== child.entity);
|
|
171
|
+
parent._child.push(child);
|
|
172
|
+
// Its a re-order. We are the last element, so we need to fetch the element before us.
|
|
173
|
+
child.rightOf = parent._child[parent._child.length - 2]?.entity;
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
// Its an append. Put it at the end
|
|
177
|
+
child.rightOf = parent._child[parent._child.length - 1]?.entity;
|
|
178
|
+
parent._child.push(child);
|
|
179
|
+
}
|
|
180
|
+
child.parent = parent.entity;
|
|
181
|
+
updateTree(child, { rightOf: child.rightOf, parent: parent.entity });
|
|
182
|
+
}
|
|
183
|
+
function removeChild(parentInstance, child) {
|
|
184
|
+
const childIndex = parentInstance._child.findIndex((c) => c.entity === child.entity);
|
|
185
|
+
if (childIndex !== -1) {
|
|
186
|
+
const childToModify = parentInstance._child[childIndex + 1];
|
|
187
|
+
if (childToModify) {
|
|
188
|
+
childToModify.rightOf = child.rightOf;
|
|
189
|
+
updateTree(childToModify, { rightOf: child.rightOf });
|
|
190
|
+
}
|
|
191
|
+
parentInstance._child.splice(childIndex, 1);
|
|
192
|
+
}
|
|
193
|
+
removeChildEntity(child);
|
|
194
|
+
}
|
|
195
|
+
function insertBefore(parentInstance, child, beforeChild) {
|
|
196
|
+
// Handle reorder: if child already exists in this parent, remove it from its old position
|
|
197
|
+
// and fix up the old neighbor's rightOf before inserting at the new position.
|
|
198
|
+
const existingIndex = parentInstance._child.findIndex((c) => c.entity === child.entity);
|
|
199
|
+
if (existingIndex !== -1) {
|
|
200
|
+
// If beforeChild.rightOf already points to child, child is already in the correct
|
|
201
|
+
// position in the rightOf chain. Setting child.rightOf = beforeChild.rightOf would
|
|
202
|
+
// produce a self-cycle (child.rightOf = child.entity).
|
|
203
|
+
if (beforeChild.rightOf === child.entity) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
const oldNextSibling = parentInstance._child[existingIndex + 1];
|
|
207
|
+
if (oldNextSibling) {
|
|
208
|
+
oldNextSibling.rightOf = child.rightOf;
|
|
209
|
+
updateTree(oldNextSibling, { rightOf: oldNextSibling.rightOf });
|
|
210
|
+
}
|
|
211
|
+
parentInstance._child.splice(existingIndex, 1);
|
|
212
|
+
}
|
|
213
|
+
const beforeChildIndex = parentInstance._child.findIndex((c) => c.entity === beforeChild.entity);
|
|
214
|
+
parentInstance._child = [
|
|
215
|
+
...parentInstance._child.slice(0, beforeChildIndex),
|
|
216
|
+
child,
|
|
217
|
+
...parentInstance._child.slice(beforeChildIndex)
|
|
218
|
+
];
|
|
219
|
+
child.rightOf = beforeChild.rightOf;
|
|
220
|
+
beforeChild.rightOf = child.entity;
|
|
221
|
+
child.parent = parentInstance.entity;
|
|
222
|
+
updateTree(child, { rightOf: child.rightOf, parent: child.parent });
|
|
223
|
+
updateTree(beforeChild, { rightOf: beforeChild.rightOf });
|
|
224
|
+
}
|
|
225
|
+
function updateOnChange(entity, componentId, state) {
|
|
226
|
+
const hasEvent = changeEvents.has(entity);
|
|
227
|
+
const event = changeEvents.get(entity) || changeEvents.set(entity, new Map()).get(entity);
|
|
228
|
+
const onChangeCallback = state?.onChangeCallback;
|
|
229
|
+
const onSubmitCallback = state?.onSubmitCallback;
|
|
230
|
+
event.set(componentId, { onChangeCallback, onSubmitCallback });
|
|
231
|
+
// Create onChange callback if its the first callback event for this entity
|
|
232
|
+
if (!hasEvent) {
|
|
233
|
+
const resultComponentId = componentId === UiDropdown.componentId ? UiDropdownResult.componentId : UiInputResult.componentId;
|
|
234
|
+
engine.getComponent(resultComponentId).onChange(entity, (value) => {
|
|
235
|
+
if (resultComponentId === UiInputResult.componentId) {
|
|
236
|
+
lastInputResultValues.set(entity, value?.value);
|
|
237
|
+
}
|
|
238
|
+
if (value?.isSubmit) {
|
|
239
|
+
const onSubmit = changeEvents.get(entity)?.get(componentId)?.onSubmitCallback;
|
|
240
|
+
onSubmit && onSubmit(value?.value);
|
|
241
|
+
}
|
|
242
|
+
const onChange = changeEvents.get(entity)?.get(componentId)?.onChangeCallback;
|
|
243
|
+
onChange && onChange(value?.value);
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
const hostConfig = {
|
|
248
|
+
...noopConfig,
|
|
249
|
+
createInstance(type, props) {
|
|
250
|
+
const entity = engine.addEntity();
|
|
251
|
+
entities.add(entity);
|
|
252
|
+
const instance = {
|
|
253
|
+
entity,
|
|
254
|
+
_child: [],
|
|
255
|
+
parent: CANVAS_ROOT_ENTITY,
|
|
256
|
+
rightOf: undefined
|
|
257
|
+
};
|
|
258
|
+
for (const key in props) {
|
|
259
|
+
const keyTyped = key;
|
|
260
|
+
if (keyTyped === 'children' || keyTyped === 'key') {
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
if (isListener(keyTyped)) {
|
|
264
|
+
upsertListener(instance, {
|
|
265
|
+
type: 'add',
|
|
266
|
+
props: props[keyTyped],
|
|
267
|
+
component: keyTyped
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
upsertComponent(instance, props[keyTyped], keyTyped);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return instance;
|
|
275
|
+
},
|
|
276
|
+
appendChild,
|
|
277
|
+
appendChildToContainer: appendChild,
|
|
278
|
+
appendInitialChild: appendChild,
|
|
279
|
+
insertInContainerBefore: insertBefore,
|
|
280
|
+
removeChild: removeChild,
|
|
281
|
+
prepareUpdate(_instance, _type, oldProps, newProps) {
|
|
282
|
+
return componentKeys
|
|
283
|
+
.map((component) => propsChanged(component, oldProps[component], newProps[component]))
|
|
284
|
+
.filter(isNotUndefined);
|
|
285
|
+
},
|
|
286
|
+
commitUpdate(instance, updatePayload, _type, _prevProps, _nextProps, _internalHandle) {
|
|
287
|
+
for (const update of updatePayload) {
|
|
288
|
+
if (isListener(update.component)) {
|
|
289
|
+
upsertListener(instance, update);
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
if (update.type === 'delete') {
|
|
293
|
+
removeComponent(instance, update.component);
|
|
294
|
+
}
|
|
295
|
+
else if (update.props) {
|
|
296
|
+
upsertComponent(instance, update.props, update.component);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
},
|
|
300
|
+
insertBefore,
|
|
301
|
+
removeChildFromContainer: removeChild
|
|
302
|
+
};
|
|
303
|
+
const reconciler = Reconciler(hostConfig);
|
|
304
|
+
// The container is a real Instance standing in for the canvas root, so
|
|
305
|
+
// top-level elements get the same parent/rightOf bookkeeping as any other
|
|
306
|
+
// sibling group. It is never rendered itself.
|
|
307
|
+
const containerInstance = {
|
|
308
|
+
entity: CANVAS_ROOT_ENTITY,
|
|
309
|
+
_child: [],
|
|
310
|
+
parent: undefined,
|
|
311
|
+
rightOf: undefined
|
|
312
|
+
};
|
|
313
|
+
const root = reconciler.createContainer(containerInstance, 0, null, false, null, '',
|
|
314
|
+
/* istanbul ignore next */
|
|
315
|
+
function () { }, null);
|
|
316
|
+
return {
|
|
317
|
+
update: function (component) {
|
|
318
|
+
return reconciler.updateContainer(component, root, null);
|
|
319
|
+
},
|
|
320
|
+
getEntities: () => Array.from(entities)
|
|
321
|
+
};
|
|
322
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Entity } from '@dcl/ecs';
|
|
2
|
+
import { Children, Key, Listeners } from '../components';
|
|
3
|
+
import type { EntityComponents } from '../react-ecs';
|
|
4
|
+
export type EngineComponents = Omit<EntityComponents, keyof Listeners>;
|
|
5
|
+
export type OpaqueHandle = any;
|
|
6
|
+
export type Type = 'entity';
|
|
7
|
+
export type Props = EntityComponents & {
|
|
8
|
+
children?: Children;
|
|
9
|
+
key?: Key;
|
|
10
|
+
};
|
|
11
|
+
export type Container = Document | Instance | any;
|
|
12
|
+
export type Instance = {
|
|
13
|
+
entity: Entity;
|
|
14
|
+
parent?: Entity;
|
|
15
|
+
rightOf?: Entity;
|
|
16
|
+
_child: Instance[];
|
|
17
|
+
};
|
|
18
|
+
export type TextInstance = never;
|
|
19
|
+
export type SuspenseInstance = never;
|
|
20
|
+
export type HydratableInstance = never;
|
|
21
|
+
export type PublicInstance = Instance;
|
|
22
|
+
export type HostContext = null;
|
|
23
|
+
export type UpdatePayload = Changes[];
|
|
24
|
+
export type _ChildSet = never;
|
|
25
|
+
export type TimeoutHandle = any;
|
|
26
|
+
export type NoTimeout = number;
|
|
27
|
+
export type Changes<K extends keyof EntityComponents = keyof EntityComponents> = {
|
|
28
|
+
type: 'delete' | 'add' | 'put';
|
|
29
|
+
props?: Partial<EntityComponents[K]>;
|
|
30
|
+
component: K;
|
|
31
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { EntityComponents } from '../react-ecs';
|
|
2
|
+
import { Changes, Container, HostContext, Instance, OpaqueHandle, Props, PublicInstance, SuspenseInstance, TextInstance, TimeoutHandle, Type } from './types';
|
|
3
|
+
export declare function propsChanged<K extends keyof EntityComponents>(component: K, prevProps: Partial<EntityComponents[K]>, nextProps: Partial<EntityComponents[K]>): Changes<K> | undefined;
|
|
4
|
+
export declare const componentKeys: (keyof EntityComponents)[];
|
|
5
|
+
export declare function isEqual<T = unknown>(val1: T, val2: T): boolean;
|
|
6
|
+
export declare const isNotUndefined: <T>(val: T | undefined) => val is T;
|
|
7
|
+
export declare const noopConfig: {
|
|
8
|
+
supportsMutation: boolean;
|
|
9
|
+
supportsPersistence: boolean;
|
|
10
|
+
noTimeout: number;
|
|
11
|
+
isPrimaryRenderer: boolean;
|
|
12
|
+
supportsHydration: boolean;
|
|
13
|
+
insertInContainerBefore(_container: Container, _child: Instance | TextInstance, _beforeChild: Instance | TextInstance | SuspenseInstance): void;
|
|
14
|
+
detachDeletedInstance(_node: Instance): void;
|
|
15
|
+
hideInstance(_instance: Instance): void;
|
|
16
|
+
hideTextInstance(_textInstance: TextInstance): void;
|
|
17
|
+
unhideInstance(_instance: Instance, _props: Props): void;
|
|
18
|
+
unhideTextInstance(_textInstance: TextInstance, _text: string): void;
|
|
19
|
+
clearContainer(_container: Container): void;
|
|
20
|
+
getCurrentEventPriority(): number;
|
|
21
|
+
getInstanceFromNode(_node: Instance): null | undefined;
|
|
22
|
+
beforeActiveInstanceBlur(): void;
|
|
23
|
+
afterActiveInstanceBlur(): void;
|
|
24
|
+
prepareScopeUpdate(): void;
|
|
25
|
+
getInstanceFromScope(): null;
|
|
26
|
+
commitMount(_instance: Instance, _type: Type, _props: Props, _internalInstanceHandle: OpaqueHandle): void;
|
|
27
|
+
resetTextContent(_instance: Instance): void;
|
|
28
|
+
commitTextUpdate(_textInstance: TextInstance, _oldText: string, _newText: string): void;
|
|
29
|
+
prepareForCommit(_containerInfo: Container): Record<string, any> | null;
|
|
30
|
+
resetAfterCommit(_containerInfo: Container): void;
|
|
31
|
+
preparePortalMount(_containerInfo: Container): void;
|
|
32
|
+
createTextInstance(_text: string, _rootContainer: Container, _hostContext: HostContext, _internalHandle: OpaqueHandle): TextInstance;
|
|
33
|
+
scheduleTimeout(_fn: any, _delay?: number): TimeoutHandle;
|
|
34
|
+
cancelTimeout(_id: TimeoutHandle): void;
|
|
35
|
+
shouldSetTextContent(_type: Type, _props: Props): boolean;
|
|
36
|
+
getRootHostContext(_rootContainer: Container): HostContext | null;
|
|
37
|
+
getChildHostContext(_parentHostContext: HostContext, _type: Type, _rootContainer: Container): HostContext;
|
|
38
|
+
getPublicInstance(instance: Instance): PublicInstance;
|
|
39
|
+
finalizeInitialChildren(_instance: Instance, _type: Type, _props: Props, _rootContainer: Container, _hostContext: HostContext): boolean;
|
|
40
|
+
};
|