@playcanvas/web-components 0.1.13 → 0.2.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.
@@ -50,7 +50,15 @@ declare class CameraComponentElement extends ComponentElement {
50
50
  toneMapping: number | undefined;
51
51
  };
52
52
  get xrAvailable(): boolean | null | undefined;
53
+ /**
54
+ * Starts the camera in XR mode.
55
+ * @param type - The type of XR mode to start.
56
+ * @param space - The space to start the camera in.
57
+ */
53
58
  startXr(type: 'immersive-ar' | 'immersive-vr', space: 'bounded-floor' | 'local' | 'local-floor' | 'unbounded' | 'viewer'): void;
59
+ /**
60
+ * Ends the camera's XR mode.
61
+ */
54
62
  endXr(): void;
55
63
  /**
56
64
  * Gets the underlying PlayCanvas camera component.
@@ -29,6 +29,36 @@ declare class ScriptComponentElement extends ComponentElement {
29
29
  /** @ignore */
30
30
  constructor();
31
31
  initComponent(): void;
32
+ /**
33
+ * Recursively converts raw attribute data into proper PlayCanvas types. Supported conversions:
34
+ * - "asset:assetId" → resolves to an Asset instance
35
+ * - "entity:entityId" → resolves to an Entity instance
36
+ * - "vec2:1,2" → new Vec2(1,2)
37
+ * - "vec3:1,2,3" → new Vec3(1,2,3)
38
+ * - "vec4:1,2,3,4" → new Vec4(1,2,3,4)
39
+ * - "color:1,0.5,0.5,1" → new Color(1,0.5,0.5,1)
40
+ * @param item - The item to convert.
41
+ * @returns The converted item.
42
+ */
43
+ private convertAttributes;
44
+ /**
45
+ * Preprocess the attributes object by converting its values.
46
+ * @param attrs - The attributes object to preprocess.
47
+ * @returns The preprocessed attributes object.
48
+ */
49
+ private preprocessAttributes;
50
+ /**
51
+ * Recursively merge properties from source into target.
52
+ * @param target - The target object to merge into.
53
+ * @param source - The source object to merge from.
54
+ * @returns The merged object.
55
+ */
56
+ private mergeDeep;
57
+ /**
58
+ * Update script attributes by merging preprocessed values into the script.
59
+ * @param script - The script to update.
60
+ * @param attributes - The attributes to merge into the script.
61
+ */
32
62
  private applyAttributes;
33
63
  private handleScriptAttributesChange;
34
64
  private handleScriptEnableChange;
package/dist/pwc.cjs CHANGED
@@ -1235,6 +1235,11 @@ class CameraComponentElement extends ComponentElement {
1235
1235
  const xrManager = (_a = this.component) === null || _a === undefined ? undefined : _a.system.app.xr;
1236
1236
  return xrManager && xrManager.supported && xrManager.isAvailable(playcanvas.XRTYPE_VR);
1237
1237
  }
1238
+ /**
1239
+ * Starts the camera in XR mode.
1240
+ * @param type - The type of XR mode to start.
1241
+ * @param space - The space to start the camera in.
1242
+ */
1238
1243
  startXr(type, space) {
1239
1244
  if (this.component && this.xrAvailable) {
1240
1245
  this.component.startXr(type, space, {
@@ -1245,6 +1250,9 @@ class CameraComponentElement extends ComponentElement {
1245
1250
  });
1246
1251
  }
1247
1252
  }
1253
+ /**
1254
+ * Ends the camera's XR mode.
1255
+ */
1248
1256
  endXr() {
1249
1257
  if (this.component) {
1250
1258
  this.component.endXr();
@@ -3120,62 +3128,116 @@ class ScriptComponentElement extends ComponentElement {
3120
3128
  }
3121
3129
  });
3122
3130
  }
3123
- applyAttributes(script, attributes) {
3124
- try {
3125
- const attributesObject = attributes ? JSON.parse(attributes) : {};
3126
- const applyValue = (target, key, value) => {
3127
- // Handle asset references
3128
- if (typeof value === 'string' && value.startsWith('asset:')) {
3129
- const assetId = value.slice(6);
3130
- const assetElement = document.querySelector(`pc-asset#${assetId}`);
3131
- if (assetElement) {
3132
- target[key] = assetElement.asset;
3133
- return;
3134
- }
3131
+ /**
3132
+ * Recursively converts raw attribute data into proper PlayCanvas types. Supported conversions:
3133
+ * - "asset:assetId" resolves to an Asset instance
3134
+ * - "entity:entityId" resolves to an Entity instance
3135
+ * - "vec2:1,2" → new Vec2(1,2)
3136
+ * - "vec3:1,2,3" new Vec3(1,2,3)
3137
+ * - "vec4:1,2,3,4" → new Vec4(1,2,3,4)
3138
+ * - "color:1,0.5,0.5,1" → new Color(1,0.5,0.5,1)
3139
+ * @param item - The item to convert.
3140
+ * @returns The converted item.
3141
+ */
3142
+ convertAttributes(item) {
3143
+ if (typeof item === 'string') {
3144
+ if (item.startsWith('asset:')) {
3145
+ const assetId = item.slice(6);
3146
+ const assetElement = document.querySelector(`pc-asset#${assetId}`);
3147
+ if (assetElement) {
3148
+ return assetElement.asset;
3135
3149
  }
3136
- // Handle arrays
3137
- if (value && typeof value === 'object' && Array.isArray(value)) {
3138
- // If it's an array of objects, recursively apply to each object
3139
- if (value.length > 0 && typeof value[0] === 'object') {
3140
- target[key] = value.map((item) => {
3141
- const obj = {};
3142
- for (const itemKey in item) {
3143
- applyValue(obj, itemKey, item[itemKey]);
3144
- }
3145
- return obj;
3146
- });
3147
- return;
3148
- }
3149
- // Handle vectors
3150
- if (value.length === 2 && typeof value[0] === 'number') {
3151
- target[key] = new playcanvas.Vec2(value[0], value[1]);
3152
- return;
3153
- }
3154
- if (value.length === 3 && typeof value[0] === 'number') {
3155
- target[key] = new playcanvas.Vec3(value[0], value[1], value[2]);
3156
- return;
3157
- }
3158
- if (value.length === 4 && typeof value[0] === 'number') {
3159
- target[key] = new playcanvas.Vec4(value[0], value[1], value[2], value[3]);
3160
- return;
3161
- }
3150
+ }
3151
+ if (item.startsWith('entity:')) {
3152
+ const entityId = item.slice(7);
3153
+ const entityElement = document.querySelector(`pc-entity[name="${entityId}"]`);
3154
+ if (entityElement) {
3155
+ return entityElement.entity;
3162
3156
  }
3163
- // Handle nested objects (non-array)
3164
- if (value && typeof value === 'object' && !Array.isArray(value)) {
3165
- if (!target[key] || typeof target[key] !== 'object') {
3166
- target[key] = {};
3167
- }
3168
- for (const nestedKey in value) {
3169
- applyValue(target[key], nestedKey, value[nestedKey]);
3170
- }
3157
+ }
3158
+ if (item.startsWith('vec2:')) {
3159
+ const parts = item.slice(5).split(',').map(Number);
3160
+ if (parts.length === 2 && parts.every(v => !isNaN(v))) {
3161
+ return new playcanvas.Vec2(parts[0], parts[1]);
3171
3162
  }
3172
- else {
3173
- target[key] = value;
3163
+ }
3164
+ if (item.startsWith('vec3:')) {
3165
+ const parts = item.slice(5).split(',').map(Number);
3166
+ if (parts.length === 3 && parts.every(v => !isNaN(v))) {
3167
+ return new playcanvas.Vec3(parts[0], parts[1], parts[2]);
3168
+ }
3169
+ }
3170
+ if (item.startsWith('vec4:')) {
3171
+ const parts = item.slice(5).split(',').map(Number);
3172
+ if (parts.length === 4 && parts.every(v => !isNaN(v))) {
3173
+ return new playcanvas.Vec4(parts[0], parts[1], parts[2], parts[3]);
3174
+ }
3175
+ }
3176
+ if (item.startsWith('color:')) {
3177
+ const parts = item.slice(6).split(',').map(Number);
3178
+ if (parts.length === 4 && parts.every(v => !isNaN(v))) {
3179
+ return new playcanvas.Color(parts[0], parts[1], parts[2], parts[3]);
3174
3180
  }
3175
- };
3176
- for (const key in attributesObject) {
3177
- applyValue(script, key, attributesObject[key]);
3178
3181
  }
3182
+ return item;
3183
+ }
3184
+ if (Array.isArray(item)) {
3185
+ // If it's an array of objects, convert each element individually.
3186
+ if (item.length > 0 && typeof item[0] === 'object') {
3187
+ return item.map((el) => this.convertAttributes(el));
3188
+ }
3189
+ // Otherwise, leave the numeric array unchanged but process each element.
3190
+ return item.map((el) => this.convertAttributes(el));
3191
+ }
3192
+ if (item && typeof item === 'object') {
3193
+ const result = {};
3194
+ for (const key in item) {
3195
+ result[key] = this.convertAttributes(item[key]);
3196
+ }
3197
+ return result;
3198
+ }
3199
+ return item;
3200
+ }
3201
+ /**
3202
+ * Preprocess the attributes object by converting its values.
3203
+ * @param attrs - The attributes object to preprocess.
3204
+ * @returns The preprocessed attributes object.
3205
+ */
3206
+ preprocessAttributes(attrs) {
3207
+ return this.convertAttributes(attrs);
3208
+ }
3209
+ /**
3210
+ * Recursively merge properties from source into target.
3211
+ * @param target - The target object to merge into.
3212
+ * @param source - The source object to merge from.
3213
+ * @returns The merged object.
3214
+ */
3215
+ mergeDeep(target, source) {
3216
+ for (const key in source) {
3217
+ if (source[key] &&
3218
+ typeof source[key] === 'object' &&
3219
+ !Array.isArray(source[key])) {
3220
+ if (!target[key] || typeof target[key] !== 'object') {
3221
+ target[key] = {};
3222
+ }
3223
+ this.mergeDeep(target[key], source[key]);
3224
+ }
3225
+ else {
3226
+ target[key] = source[key];
3227
+ }
3228
+ }
3229
+ return target;
3230
+ }
3231
+ /**
3232
+ * Update script attributes by merging preprocessed values into the script.
3233
+ * @param script - The script to update.
3234
+ * @param attributes - The attributes to merge into the script.
3235
+ */
3236
+ applyAttributes(script, attributes) {
3237
+ try {
3238
+ const attributesObject = attributes ? JSON.parse(attributes) : {};
3239
+ const converted = this.convertAttributes(attributesObject);
3240
+ this.mergeDeep(script, converted);
3179
3241
  }
3180
3242
  catch (error) {
3181
3243
  console.error(`Error parsing attributes JSON string ${attributes}:`, error);
@@ -3204,10 +3266,20 @@ class ScriptComponentElement extends ComponentElement {
3204
3266
  createScript(name, attributes) {
3205
3267
  if (!this.component)
3206
3268
  return null;
3207
- this.component.on(`create:${name}`, (script) => {
3208
- this.applyAttributes(script, attributes);
3269
+ let attributesObject = {};
3270
+ if (attributes) {
3271
+ try {
3272
+ attributesObject = JSON.parse(attributes);
3273
+ // Preprocess attributes: convert arrays or strings into vectors, colors, asset references, etc.
3274
+ attributesObject = this.preprocessAttributes(attributesObject);
3275
+ }
3276
+ catch (error) {
3277
+ console.error(`Error parsing attributes JSON string ${attributes}:`, error);
3278
+ }
3279
+ }
3280
+ return this.component.create(name, {
3281
+ properties: attributesObject
3209
3282
  });
3210
- return this.component.create(name);
3211
3283
  }
3212
3284
  destroyScript(name) {
3213
3285
  if (!this.component)