@zcomponent/core 1.16.0-beta → 1.18.0-beta

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.
@@ -0,0 +1,21 @@
1
+ import { ActionBehavior } from '../actionbehavior';
2
+ /**
3
+ * Calls emit on an event that's a prop of this ZComponent
4
+ *
5
+ * @zbehavior
6
+ * @zicon function
7
+ * @zgroup Actions
8
+ */
9
+ export declare class EmitComponentPropEvent extends ActionBehavior {
10
+ /**
11
+ * The event to emit
12
+ * @zprop
13
+ * @ztype function-emit-component-prop-event
14
+ */
15
+ eventToEmit: () => any;
16
+ /**
17
+ * @zui
18
+ * @zprop
19
+ */
20
+ perform(): void;
21
+ }
@@ -0,0 +1,19 @@
1
+ import { ActionBehavior } from '../actionbehavior';
2
+ import { registerBehaviorRunAtDesignTime } from '../behavior';
3
+ /**
4
+ * Calls emit on an event that's a prop of this ZComponent
5
+ *
6
+ * @zbehavior
7
+ * @zicon function
8
+ * @zgroup Actions
9
+ */
10
+ export class EmitComponentPropEvent extends ActionBehavior {
11
+ /**
12
+ * @zui
13
+ * @zprop
14
+ */
15
+ perform() {
16
+ this.eventToEmit();
17
+ }
18
+ }
19
+ registerBehaviorRunAtDesignTime(EmitComponentPropEvent);
@@ -100,7 +100,8 @@ export interface ContextValueEntityPropOverride extends BaseEntityPropOverride {
100
100
  export declare enum FunctionCallType {
101
101
  Entity = "entity",
102
102
  Context = "context",
103
- Import = "import"
103
+ Import = "import",
104
+ EmitComponentEvent = "emitcomponentevent"
104
105
  }
105
106
  export interface BaseFunctionCall {
106
107
  _zfn: true;
@@ -125,4 +126,8 @@ export interface ImportFunctionCall extends BaseFunctionCall {
125
126
  t: FunctionCallType.Import;
126
127
  resolved?: any;
127
128
  }
128
- export type FunctionCall = EntityFunctionCall | ContextFunctionCall | ImportFunctionCall;
129
+ export interface EmitComponentEventFunctionCall extends BaseFunctionCall {
130
+ prop: string;
131
+ t: FunctionCallType.EmitComponentEvent;
132
+ }
133
+ export type FunctionCall = EntityFunctionCall | ContextFunctionCall | ImportFunctionCall | EmitComponentEventFunctionCall;
package/lib/data/core.js CHANGED
@@ -9,4 +9,5 @@ export var FunctionCallType;
9
9
  FunctionCallType["Entity"] = "entity";
10
10
  FunctionCallType["Context"] = "context";
11
11
  FunctionCallType["Import"] = "import";
12
+ FunctionCallType["EmitComponentEvent"] = "emitcomponentevent";
12
13
  })(FunctionCallType || (FunctionCallType = {}));
package/lib/selectors.js CHANGED
@@ -319,7 +319,7 @@ export const typeDefinitionForComponent = (nodes, props, constructorProps, scrip
319
319
  }
320
320
  }
321
321
  }
322
- return `import { ZComponent, ContextManager, Observable, Animation, Layer, LayerClip } from "@zcomponent/core";
322
+ return `import { ZComponent, ContextManager, Observable, Animation, Layer, LayerClip, Event } from "@zcomponent/core";
323
323
 
324
324
  ${importStrings.join('\n')}
325
325
 
@@ -429,6 +429,9 @@ function getJSDocForProp(prop) {
429
429
  * @returns A string representing the TypeScript output for the property.
430
430
  */
431
431
  function typeOutputForProp(prop) {
432
+ if (prop.type.name === 'event')
433
+ return `${getJSDocForProp(prop)}
434
+ public ${prop.name}: ${outputForType(prop.type, true)};`;
432
435
  return `${getJSDocForProp(prop)}
433
436
  public ${prop.name}: Observable<${outputForType(prop.type, true)}>;`;
434
437
  }
package/lib/types.d.ts CHANGED
@@ -124,11 +124,11 @@ export interface FunctionType extends BaseType {
124
124
  }
125
125
  /**
126
126
  * Type definition for an event type.
127
- *
128
- * This interface extends BaseType and is used specifically for defining an event type.
129
127
  */
130
128
  export interface EventType extends BaseType {
131
129
  name: 'event';
130
+ children: Type[];
131
+ names?: string[];
132
132
  }
133
133
  /** Internal use comment is sufficient for EntityType
134
134
  * @internal
@@ -164,7 +164,8 @@ export declare enum TypeHint {
164
164
  'time-milliseconds' = "time-milliseconds",
165
165
  'time-hertz' = "time-hertz",
166
166
  'emailaddress' = "emailaddress",
167
- 'url' = "url"
167
+ 'url' = "url",
168
+ 'function-emit-component-prop-event' = "function-emit-component-prop-event"
168
169
  }
169
170
  /**
170
171
  * Enumeration of values types used in a values definition.
package/lib/types.js CHANGED
@@ -43,6 +43,7 @@ export var TypeHint;
43
43
  TypeHint["time-hertz"] = "time-hertz";
44
44
  TypeHint["emailaddress"] = "emailaddress";
45
45
  TypeHint["url"] = "url";
46
+ TypeHint["function-emit-component-prop-event"] = "function-emit-component-prop-event";
46
47
  })(TypeHint || (TypeHint = {}));
47
48
  /**
48
49
  * Enumeration of values types used in a values definition.
@@ -350,10 +351,19 @@ export function mergeTypes(a, b) {
350
351
  comments: mergeComments(a.comments ?? [], b.comments ?? []),
351
352
  };
352
353
  case 'event':
354
+ const children = [];
355
+ for (let i = 0; i < a.children.length; i++) {
356
+ const merged = mergeTypes(a.children[i], b.children[i]);
357
+ if (!merged)
358
+ return undefined;
359
+ children.push(merged);
360
+ }
353
361
  return {
354
362
  name: a.name,
355
363
  typeHint,
364
+ children,
356
365
  comments: mergeComments(a.comments ?? [], b.comments ?? []),
366
+ names: mergeTupleNames(a, b),
357
367
  };
358
368
  }
359
369
  }
@@ -451,6 +461,11 @@ export function isValidValueForType(def, t, allowUndefined) {
451
461
  return false;
452
462
  return true;
453
463
  }
464
+ if (def['t'] === FunctionCallType.EmitComponentEvent) {
465
+ if (typeof def['prop'] !== 'string' || def['prop'].length === 0)
466
+ return false;
467
+ return true;
468
+ }
454
469
  return false;
455
470
  }
456
471
  return false;
@@ -494,7 +509,10 @@ function getBasicType(t) {
494
509
  case 'function':
495
510
  return `(${t.args.map(entry => `${entry.name}: ${outputForType(entry.type)}`).join(', ')}) => ${t.ret ? outputForType(t.ret) : 'void'}`;
496
511
  case 'event':
497
- return 'Event';
512
+ if (Array.isArray(t.names) && t.names.length === t.children.length) {
513
+ return `Event<[${t.children.map((c, indx) => `${getSafeKeyName(t.names[indx])}: ${outputForType(c)}`).join(', ')}]>`;
514
+ }
515
+ return `Event<[${t.children.map(c => outputForType(c)).join(', ')}]>`;
498
516
  }
499
517
  }
500
518
  /**
package/lib/zcomponent.js CHANGED
@@ -13,6 +13,7 @@ import { populateAnimationStructureFromData, populateAnimationTracksFromData, up
13
13
  import { EntityPropOverrideType, FunctionCallType } from './data/core';
14
14
  import { Observable } from './observable';
15
15
  import { setCurrentZComponentConstruction } from './zcomponentconstruction';
16
+ import { Event } from './event';
16
17
  export class ZComponentContext extends Context {
17
18
  /**
18
19
  * Creates an instance of ZComponentContext.
@@ -370,6 +371,25 @@ export class ZComponent extends Component {
370
371
  console.error('Unable to call entity function', err);
371
372
  }
372
373
  };
374
+ case FunctionCallType.EmitComponentEvent:
375
+ return () => {
376
+ try {
377
+ if (typeof c.prop !== 'string' || typeof c.args !== 'object')
378
+ throw new Error('Invalid function call data');
379
+ const evt = this[c.prop];
380
+ if (!evt)
381
+ throw new Error('Unable to find component event');
382
+ if (!(evt instanceof Event))
383
+ throw new Error('Component property is not an event');
384
+ const converted = [];
385
+ for (const key in c.args)
386
+ converted[key] = c.args[key];
387
+ evt.emit.call(evt, ...converted);
388
+ }
389
+ catch (err) {
390
+ console.error('Unable to emit component event', err);
391
+ }
392
+ };
373
393
  }
374
394
  }
375
395
  /**
@@ -484,7 +504,10 @@ export class ZComponent extends Component {
484
504
  _initializeComponentProps() {
485
505
  const componentProps = this._opts.data.props ?? {};
486
506
  for (const prop of Object.values(componentProps)) {
487
- this[prop.name] = new Observable(prop.default);
507
+ if (prop.type.name === 'event')
508
+ this[prop.name] = new Event();
509
+ else
510
+ this[prop.name] = new Observable(prop.default);
488
511
  }
489
512
  }
490
513
  _setEntityProp(entity, prop, v) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zcomponent/core",
3
- "version": "1.16.0-beta",
3
+ "version": "1.18.0-beta",
4
4
  "description": "The core component model and built-in functionality for Mattercraft.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",