@playcanvas/web-components 0.1.1 → 0.1.2

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.
@@ -1,85 +1,43 @@
1
- import { ScriptType } from 'playcanvas';
2
-
3
- import { AppElement } from '../app';
4
- import { ScriptComponentElement } from './script-component';
5
-
6
1
  /**
7
2
  * Represents a script in the PlayCanvas engine.
8
3
  */
9
4
  class ScriptElement extends HTMLElement {
10
- private _attributes: Record<string, any> = {};
5
+ private _attributes: string = '{}';
11
6
 
12
7
  private _enabled: boolean = true;
13
8
 
14
9
  private _name: string = '';
15
10
 
16
- private _script: ScriptType | null = null;
17
-
18
- async connectedCallback() {
19
- const appElement = this.closest('pc-app') as AppElement | null;
20
- if (!appElement) {
21
- console.error(`${this.tagName.toLowerCase()} should be a descendant of pc-app`);
22
- return;
23
- }
24
-
25
- await appElement.getApplication();
26
-
27
- const scriptAttributes = this.getAttribute('attributes');
28
- if (scriptAttributes) {
29
- try {
30
- this._attributes = JSON.parse(scriptAttributes);
31
- } catch (e) {
32
- console.error('Failed to parse script attributes:', e);
33
- }
34
- }
35
-
36
- // When the script is created, initialize it with the necessary attributes
37
- this.scriptsElement?.component!.on(`create:${this._name}`, (scriptInstance) => {
38
- Object.assign(scriptInstance, this._attributes);
39
- });
40
-
41
- this._script = this.scriptsElement?.component!.create(this._name, { preloading: false }) ?? null;
42
- }
43
-
44
- disconnectedCallback() {
45
- this.scriptsElement?.component!.destroy(this._name);
46
- }
47
-
48
- refreshAttributes() {
49
- if (this._script) {
50
- Object.entries(this._attributes).forEach(([name, value]) => {
51
- if (this._script && name in this._script) {
52
- (this._script as any)[name] = value;
53
- }
54
- });
55
- }
56
- }
57
-
58
- protected get scriptsElement(): ScriptComponentElement | null {
59
- const scriptsElement = this.parentElement as ScriptComponentElement;
60
-
61
- if (!(scriptsElement instanceof ScriptComponentElement)) {
62
- console.warn('pc-script must be a direct child of a pc-scripts element');
63
- return null;
64
- }
65
-
66
- return scriptsElement;
67
- }
68
-
11
+ /**
12
+ * Sets the attributes of the script.
13
+ * @param value - The attributes of the script.
14
+ */
69
15
  set scriptAttributes(value: string) {
70
- this._attributes = JSON.parse(value);
71
- this.refreshAttributes();
16
+ this._attributes = value;
17
+ this.dispatchEvent(new CustomEvent('scriptattributeschange', {
18
+ detail: { attributes: value },
19
+ bubbles: true
20
+ }));
72
21
  }
73
22
 
23
+ /**
24
+ * Gets the attributes of the script.
25
+ * @returns The attributes of the script.
26
+ */
74
27
  get scriptAttributes() {
75
- return JSON.stringify(this._attributes);
28
+ return this._attributes;
76
29
  }
77
30
 
31
+ /**
32
+ * Sets the enabled state of the script.
33
+ * @param value - The enabled state of the script.
34
+ */
78
35
  set enabled(value: boolean) {
79
36
  this._enabled = value;
80
- if (this._script) {
81
- this._script.enabled = value;
82
- }
37
+ this.dispatchEvent(new CustomEvent('scriptenablechange', {
38
+ detail: { enabled: value },
39
+ bubbles: true
40
+ }));
83
41
  }
84
42
 
85
43
  /**
@@ -112,6 +112,6 @@ class SoundComponentElement extends ComponentElement {
112
112
  }
113
113
  }
114
114
 
115
- customElements.define('pc-sound', SoundComponentElement);
115
+ customElements.define('pc-sounds', SoundComponentElement);
116
116
 
117
117
  export { SoundComponentElement };
@@ -284,6 +284,6 @@ class SoundSlotElement extends HTMLElement {
284
284
  }
285
285
  }
286
286
 
287
- customElements.define('pc-sound-slot', SoundSlotElement);
287
+ customElements.define('pc-sound', SoundSlotElement);
288
288
 
289
289
  export { SoundSlotElement };