@playcanvas/react
Version:
A React renderer for PlayCanvas – build interactive 3D applications using React's declarative paradigm.
48 lines • 1.66 kB
JavaScript
const toLowerCamelCase = (str) => str[0].toLowerCase() + str.slice(1);
// @ts-ignore
const getScriptName = (script) => script.constructor.__name ?? toLowerCamelCase(script.constructor.name);
export function createInstance(_type, props) {
return {
type: 'script',
props: props,
script: null,
};
}
export function commitUpdate(instance, _type, _oldProps, newProps) {
// console.log('commitUpdate', instance, _type, _oldProps, newProps)
const { script } = instance;
const { script: scriptClass, ...props } = newProps;
// If no script component exists, throw an error
if (!script) {
throw new Error('Script component does not exist');
}
// If the script does not exist, create it
const scriptName = getScriptName(scriptClass);
// @ts-ignore
if (script[scriptName]) {
// @ts-ignore
const scriptInstance = script[scriptName];
Object.assign(scriptInstance, props);
}
}
export function appendChild(parent, child) {
const script = parent.attachedTo?.script;
if (!script) {
throw new Error('Entity does not have a script component');
}
const { script: scriptClass, ...props } = child.props;
const scriptInstance = script.create(scriptClass, {
properties: { ...props },
preloading: false,
});
// @ts-ignore
child.script = scriptInstance;
}
export function appendInitialChild(parent, child) {
appendChild(parent, child);
}
export function removeChild(parent, child) {
parent.attachedTo?.script?.destroy(getScriptName(child.script));
child.script = null;
}
//# sourceMappingURL=script-config.js.map