@playcanvas/web-components 0.18.0 → 0.20.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.
Files changed (56) hide show
  1. package/dist/app.d.cts +38 -3
  2. package/dist/app.d.ts +38 -3
  3. package/dist/components/anim-clip.d.cts +0 -2
  4. package/dist/components/anim-clip.d.ts +0 -2
  5. package/dist/components/button-component.d.cts +9 -5
  6. package/dist/components/button-component.d.ts +9 -5
  7. package/dist/components/joint-component.d.cts +24 -10
  8. package/dist/components/joint-component.d.ts +24 -10
  9. package/dist/components/script-component.d.cts +4 -2
  10. package/dist/components/script-component.d.ts +4 -2
  11. package/dist/components/script-instance.d.cts +14 -6
  12. package/dist/components/script-instance.d.ts +14 -6
  13. package/dist/components/scroll-view-component.d.cts +24 -12
  14. package/dist/components/scroll-view-component.d.ts +24 -12
  15. package/dist/components/scrollbar-component.d.cts +6 -3
  16. package/dist/components/scrollbar-component.d.ts +6 -3
  17. package/dist/custom-elements.json +93 -23
  18. package/dist/entity-base.d.cts +4 -3
  19. package/dist/entity-base.d.ts +4 -3
  20. package/dist/entity.d.cts +8 -2
  21. package/dist/entity.d.ts +8 -2
  22. package/dist/model.d.cts +6 -0
  23. package/dist/model.d.ts +6 -0
  24. package/dist/node.d.cts +6 -0
  25. package/dist/node.d.ts +6 -0
  26. package/dist/parse.d.cts +7 -2
  27. package/dist/parse.d.ts +7 -2
  28. package/dist/pwc.cjs +706 -289
  29. package/dist/pwc.cjs.map +1 -1
  30. package/dist/pwc.js +706 -289
  31. package/dist/pwc.js.map +1 -1
  32. package/dist/pwc.min.js +1 -1
  33. package/dist/pwc.min.js.map +1 -1
  34. package/dist/pwc.min.mjs +1 -1
  35. package/dist/pwc.min.mjs.map +1 -1
  36. package/dist/pwc.mjs +706 -289
  37. package/dist/pwc.mjs.map +1 -1
  38. package/dist/scene.d.cts +17 -1
  39. package/dist/scene.d.ts +17 -1
  40. package/dist/vscode.html-custom-data.json +34 -14
  41. package/dist/web-types.json +78 -24
  42. package/package.json +3 -3
  43. package/src/app.ts +197 -87
  44. package/src/components/anim-clip.ts +0 -2
  45. package/src/components/button-component.ts +18 -10
  46. package/src/components/joint-component.ts +29 -15
  47. package/src/components/script-component.ts +25 -12
  48. package/src/components/script-instance.ts +14 -6
  49. package/src/components/scroll-view-component.ts +49 -29
  50. package/src/components/scrollbar-component.ts +13 -8
  51. package/src/entity-base.ts +27 -15
  52. package/src/entity.ts +11 -4
  53. package/src/model.ts +9 -2
  54. package/src/node.ts +9 -2
  55. package/src/parse.ts +213 -16
  56. package/src/scene.ts +33 -2
package/src/parse.ts CHANGED
@@ -12,8 +12,13 @@
12
12
  * - `parseBool` and `parseTags` take no attribute name, because every value is valid for them and
13
13
  * so they never warn.
14
14
  *
15
- * `getEntity` is the exception: it resolves a reference to a live entity rather than parsing a
16
- * literal, and returns `null` instead of falling back to a default.
15
+ * `findEntityElement` and `getEntity` are the exceptions: they resolve a reference rather than
16
+ * parsing a literal, and return `null` instead of falling back to a default. A reference
17
+ * beginning with `#` is a document-wide selector (an element id, or any selector rooted in one);
18
+ * anything else is an entity name, resolved lexically through the entity hierarchy first and
19
+ * against the document after — never as a selector or an id. They also do not warn - what an
20
+ * unresolved reference means depends on the element holding it - so elements report through
21
+ * `resolveEntity`, which takes that meaning as parameters.
17
22
  */
18
23
 
19
24
  import type { Entity } from 'playcanvas';
@@ -324,32 +329,224 @@ export const parseVec4 = <T extends Vec4 | null>(
324
329
  };
325
330
 
326
331
  /**
327
- * Resolves a reference string to the {@link Entity} backing a `<pc-entity>` element. The reference
328
- * can be a CSS selector (e.g. `#my-id`, `pc-entity[name="Foo"]`), a bare element id, or a bare
329
- * entity name. Returns `null` if no matching element (or backing entity) is found.
332
+ * Runs querySelector, absorbing the SyntaxError an unparseable selector throws - references are
333
+ * arbitrary author text, so a lookup must fail to `null`, never throw.
334
+ *
335
+ * @param selector - The selector to query.
336
+ * @returns The matched element, or `null`.
337
+ */
338
+ const query = (selector: string): Element | null => {
339
+ try {
340
+ return document.querySelector(selector);
341
+ } catch {
342
+ return null;
343
+ }
344
+ };
345
+
346
+ /**
347
+ * Runs a lookup against one scope, checking the scope element itself before its subtree — a
348
+ * reference deep in a cloned prefab must be able to name the prefab's root. Absorbs the
349
+ * SyntaxError of an invalid selector like {@link query}: escaping quotes and backslashes does not
350
+ * make arbitrary text a valid CSS string (a reference containing a newline still throws), so a
351
+ * lookup must fail to `null`, never throw.
352
+ *
353
+ * @param scope - The element whose inclusive subtree to search.
354
+ * @param selector - The selector to query.
355
+ * @returns The matched element, or `null`.
356
+ */
357
+ const queryScope = (scope: Element, selector: string): Element | null => {
358
+ try {
359
+ return scope.matches(selector) ? scope : scope.querySelector(selector);
360
+ } catch {
361
+ return null;
362
+ }
363
+ };
364
+
365
+ /**
366
+ * Reads the entity a resolved element is backing, through the `entity` accessor every
367
+ * entity-fronting element exposes. `null` for no element, and for an element backing nothing.
368
+ *
369
+ * @param element - The element to read, or `null`.
370
+ * @returns The backing entity, or `null`.
371
+ */
372
+ const entityOf = (element: Element | null): Entity | null => {
373
+ return (element as { entity?: Entity } | null)?.entity ?? null;
374
+ };
375
+
376
+ /**
377
+ * The elements that front an entity: what a bare name can resolve to, and the scopes of the
378
+ * lexical name lookup.
379
+ */
380
+ const ENTITY_KINDS = ['pc-entity', 'pc-model', 'pc-node'] as const;
381
+
382
+ /**
383
+ * The entity-fronting elements as one selector, for the scope walk.
384
+ */
385
+ const ENTITY_SCOPES = ENTITY_KINDS.join(', ');
386
+
387
+ /**
388
+ * Resolves a reference string to the element it names. The grammar is closed — every reference
389
+ * has exactly one interpretation:
390
+ *
391
+ * - A reference beginning with `#` is a document-wide CSS selector — an element id (`#body`), or
392
+ * any selector rooted in one (`#hud pc-entity`). It is authoritative: the name lookup never
393
+ * runs for it, so an unusually named entity cannot shadow it.
394
+ * - Any other reference is the name of an entity-fronting element (`<pc-entity>`, `<pc-model>` or
395
+ * `<pc-node>` — for a node, the glTF node name it binds), and nothing else. A bare reference is
396
+ * never interpreted as a selector or an element id, so adding or renaming elements can never
397
+ * change which form it takes.
398
+ *
399
+ * When `from` is supplied, a name resolves lexically first: the closest entity-fronting
400
+ * ancestor's inclusive subtree, then each outer entity-fronting ancestor, then the containing
401
+ * `<pc-app>`, then the document. This is what lets a `<template>` prefab reference its own
402
+ * entities by name — every clone resolves within itself before a document-wide lookup could reach
403
+ * an earlier clone — provided the prefab has a single entity-fronting root to be the enclosing
404
+ * scope.
405
+ *
406
+ * Separate from {@link getEntity} so a caller reporting a failure can tell the causes apart
407
+ * ({@link unresolvedCause} words them): nothing in the document matches the reference, or
408
+ * something matches but is not backing an entity (yet, or ever).
330
409
  *
331
410
  * @param ref - The reference string to resolve.
332
- * @returns The resolved entity, or `null`.
411
+ * @param from - The element resolving the reference, whose entity-fronting ancestors scope the
412
+ * name lookup. Omitted, the name lookup is document-wide only.
413
+ * @returns The matched element, or `null`.
333
414
  * @internal
334
415
  */
335
- export const getEntity = (ref: string): Entity | null => {
416
+ export const findEntityElement = (ref: string, from?: Element): Element | null => {
336
417
  if (!ref) {
337
418
  return null;
338
419
  }
339
420
 
340
- let element: Element | null = null;
421
+ // A '#' reference is document-wide and bypasses the name lookup entirely - an entity named
422
+ // '#body' must never shadow the element whose id is 'body'.
423
+ if (ref.startsWith('#')) {
424
+ return query(ref);
425
+ }
341
426
 
342
- // Try the reference as a CSS selector. An invalid selector (e.g. a bare name containing
343
- // spaces) throws, in which case we fall back to id/name lookups below.
344
- try {
345
- element = document.querySelector(ref);
346
- } catch {
347
- element = null;
427
+ // The name lands inside a quoted CSS string, so its quotes and backslashes are escaped -
428
+ // a name like `say "hi"` must resolve, not turn the lookup into a SyntaxError.
429
+ const escaped = ref.replace(/["\\]/g, '\\$&');
430
+ const nameSelector = ENTITY_KINDS.map(kind => `${kind}[name="${escaped}"]`).join(', ');
431
+
432
+ if (from) {
433
+ let scope = from.parentElement?.closest(ENTITY_SCOPES);
434
+ while (scope) {
435
+ const element = queryScope(scope, nameSelector);
436
+ if (element) {
437
+ return element;
438
+ }
439
+ scope = scope.parentElement?.closest(ENTITY_SCOPES);
440
+ }
441
+
442
+ const app = from.parentElement?.closest('pc-app');
443
+ if (app) {
444
+ const element = queryScope(app, nameSelector);
445
+ if (element) {
446
+ return element;
447
+ }
448
+ }
348
449
  }
349
450
 
451
+ return query(nameSelector);
452
+ };
453
+
454
+ /**
455
+ * Resolves a reference string to the {@link Entity} backing an entity-fronting element
456
+ * (`<pc-entity>`, `<pc-model>` or `<pc-node>`). The reference is a name — resolved lexically
457
+ * through the entity hierarchy first when `from` is supplied — or a document-wide `#` selector
458
+ * ({@link findEntityElement} details the grammar and order). Returns `null` if no matching
459
+ * element (or backing entity) is found.
460
+ *
461
+ * @param ref - The reference string to resolve.
462
+ * @param from - The element resolving the reference, whose entity-fronting ancestors scope the
463
+ * name lookup. Omitted, the name lookup is document-wide only.
464
+ * @returns The resolved entity, or `null`.
465
+ * @internal
466
+ */
467
+ export const getEntity = (ref: string, from?: Element): Entity | null => {
468
+ return entityOf(findEntityElement(ref, from));
469
+ };
470
+
471
+ /**
472
+ * Describes why a non-empty reference did not resolve, for a warning. Three causes, because they
473
+ * have three different fixes: nothing matches (usually a typo), the matched element is not backing
474
+ * an entity yet (usually timing - a `pc-node` whose asset has not loaded - so resolving again
475
+ * later can work), or the matched element can never back one (the reference points at the wrong
476
+ * element, so only correcting it can). Capability is the `entity` accessor every entity-backing
477
+ * element inherits from EntityBaseElement.
478
+ *
479
+ * @param element - The element the reference matched, or `null` when nothing did.
480
+ * @returns The cause, phrased to follow `could not resolve ... -`.
481
+ * @internal
482
+ */
483
+ export const unresolvedCause = (element: Element | null): string => {
350
484
  if (!element) {
351
- element = document.getElementById(ref) ?? document.querySelector(`pc-entity[name="${ref}"]`);
485
+ return 'nothing in the document matches it';
352
486
  }
487
+ const tag = `<${element.tagName.toLowerCase()}>`;
488
+ return 'entity' in element
489
+ ? `${tag} matches it but is not backing an entity yet`
490
+ : `${tag} matches it but cannot back an entity`;
491
+ };
353
492
 
354
- return (element as { entity?: Entity } | null)?.entity ?? null;
493
+ /**
494
+ * Builds the migration pointer for a bare reference that names nothing but matches the id of an
495
+ * entity-fronting element - it was almost certainly meant as an id, so point at the form that
496
+ * expresses it, escaped so the suggestion actually parses as a selector (an id like `a:b` must
497
+ * be written `#a\:b`). Empty when the reference is already a `#` form, matches no id, or the id
498
+ * belongs to an element that could never back an entity - suggesting it would only trade this
499
+ * warning for the wrong-target one.
500
+ *
501
+ * @param ref - The unresolved reference.
502
+ * @param prefix - Text the suggested form must carry in the caller's syntax (e.g. `entity:`).
503
+ * @returns The advice sentence, or an empty string.
504
+ * @internal
505
+ */
506
+ export const idHint = (ref: string, prefix = ''): string => {
507
+ const match = !ref.startsWith('#') && document.getElementById(ref);
508
+ return match && 'entity' in match
509
+ ? `A bare reference is a name - write '${prefix}#${CSS.escape(ref)}' to reference the element with that id.`
510
+ : '';
511
+ };
512
+
513
+ /**
514
+ * Resolves a reference string to the {@link Entity} backing an entity-fronting element, scoped to
515
+ * the resolving element ({@link findEntityElement} details the order) and warning when a
516
+ * non-empty reference does not resolve - otherwise the reference fails silently, invisible
517
+ * except through the behavior it should have driven. The message names which of the three causes
518
+ * ({@link unresolvedCause}) it hit, and advises reassigning later only when that can work.
519
+ *
520
+ * An empty reference stays silent: it is the unset state of an optional attribute, and on some
521
+ * elements (`pc-joint` `entity-b`, `pc-button` `image`) a documented value of its own.
522
+ *
523
+ * @param ref - The reference string to resolve.
524
+ * @param from - The element resolving the reference; scopes the lookup and names the message.
525
+ * @param attribute - The attribute being resolved, for the message.
526
+ * @param consequence - What the unresolved reference means for the element, for the message.
527
+ * @returns The resolved entity, or `null`.
528
+ * @internal
529
+ */
530
+ export const resolveEntity = (ref: string, from: Element, attribute: string, consequence: string): Entity | null => {
531
+ if (!ref) {
532
+ return null;
533
+ }
534
+
535
+ const element = findEntityElement(ref, from);
536
+ const entity = entityOf(element);
537
+ if (!entity) {
538
+ let advice = `Assign ${attribute} again once the entity exists.`;
539
+ if (element && !('entity' in element)) {
540
+ advice = `Point ${attribute} at a pc-entity, pc-model or pc-node instead.`;
541
+ } else if (!element) {
542
+ const hint = idHint(ref);
543
+ if (hint) {
544
+ advice = hint;
545
+ }
546
+ }
547
+ console.warn(
548
+ `${from.tagName.toLowerCase()} could not resolve ${attribute} '${ref}' - ${unresolvedCause(element)} - ${consequence}. ${advice}`
549
+ );
550
+ }
551
+ return entity;
355
552
  };
package/src/scene.ts CHANGED
@@ -11,9 +11,15 @@ import { parseColor, parseEnum, parseNumber, parseVec3 } from './parse';
11
11
  * {@link HTMLElement} interface.
12
12
  *
13
13
  * @elementSummary The `<pc-scene>` element holds the entity hierarchy the application renders,
14
- * along with the scene-wide fog and gravity settings. Must be a direct child of `<pc-app>`.
14
+ * along with the scene-wide fog, exposure and gravity settings. Must be a direct child of
15
+ * `<pc-app>`.
15
16
  */
16
17
  class SceneElement extends AsyncElement {
18
+ /**
19
+ * The exposure of the scene.
20
+ */
21
+ private _exposure = 1;
22
+
17
23
  /**
18
24
  * The fog type of the scene.
19
25
  */
@@ -96,6 +102,8 @@ class SceneElement extends AsyncElement {
96
102
 
97
103
  private _updateSceneSettings() {
98
104
  if (this._scene) {
105
+ this._scene.exposure = this._exposure;
106
+
99
107
  this._scene.fog.type = this._fog;
100
108
  this._scene.fog.color = this._fogColor;
101
109
  this._scene.fog.density = this._fogDensity;
@@ -117,6 +125,26 @@ class SceneElement extends AsyncElement {
117
125
  this.closestApp?.app?.systems.rigidbody?.gravity.copy(value);
118
126
  }
119
127
 
128
+ /**
129
+ * Sets the exposure of the scene, which tweaks the overall brightness of the rendered image.
130
+ * Ignored if the scene is using physical units. Defaults to 1.
131
+ * @param value - The exposure.
132
+ */
133
+ set exposure(value: number) {
134
+ this._exposure = value;
135
+ if (this.scene) {
136
+ this.scene.exposure = value;
137
+ }
138
+ }
139
+
140
+ /**
141
+ * Gets the exposure of the scene.
142
+ * @returns The exposure.
143
+ */
144
+ get exposure() {
145
+ return this._exposure;
146
+ }
147
+
120
148
  /**
121
149
  * Sets the fog type of the scene. Can be `none`, `linear`, `exp` or `exp2`. Defaults to
122
150
  * `none`.
@@ -233,11 +261,14 @@ class SceneElement extends AsyncElement {
233
261
  }
234
262
 
235
263
  static get observedAttributes() {
236
- return ['fog', 'fog-color', 'fog-density', 'fog-start', 'fog-end', 'gravity'];
264
+ return ['exposure', 'fog', 'fog-color', 'fog-density', 'fog-start', 'fog-end', 'gravity'];
237
265
  }
238
266
 
239
267
  attributeChangedCallback(name: string, _oldValue: string | null, newValue: string | null) {
240
268
  switch (name) {
269
+ case 'exposure':
270
+ this.exposure = parseNumber(newValue, 1, name);
271
+ break;
241
272
  case 'fog':
242
273
  this.fog = parseEnum(newValue, ['none', 'linear', 'exp', 'exp2'], 'none', name);
243
274
  break;