canvasengine 2.0.0-rc.3 → 2.0.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "canvasengine",
3
- "version": "2.0.0-rc.3",
3
+ "version": "2.0.0",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -240,7 +240,22 @@ export function Circle(props: CircleProps) {
240
240
  }
241
241
 
242
242
  export function Ellipse(props: EllipseProps) {
243
- return Circle(props as CircleProps);
243
+ const { color, border } = useProps(props, {
244
+ border: null
245
+ })
246
+
247
+ return Graphics({
248
+ draw: (g, width, height, anchor) => {
249
+ const { x, y } = graphicsAnchor(anchor, width, height);
250
+ g.ellipse(x + width / 2, y + height / 2, width / 2, height / 2);
251
+ const borderValue = propValue(border);
252
+ if (borderValue) {
253
+ g.stroke(borderValue);
254
+ }
255
+ g.fill(propValue(color));
256
+ },
257
+ ...props
258
+ })
244
259
  }
245
260
 
246
261
  export function Triangle(props: TriangleProps) {
@@ -753,7 +753,7 @@ export class CanvasSprite extends DisplayObject(PixiSprite) {
753
753
  spriteHeight: number,
754
754
  realSize?: TextureOptionsMerging["spriteRealSize"]
755
755
  ) {
756
- if (!this.hitbox || !spriteWidth || !spriteHeight) {
756
+ if (!this.hitbox || !spriteWidth || !spriteHeight || !this.anchor) {
757
757
  return;
758
758
  }
759
759
 
@@ -30,6 +30,28 @@ export let currentSubscriptionsTracker: ((subscription: Subscription) => void) |
30
30
  export let currentDefinePropsTracker: ((signals: Record<string, any>) => void) | null = null;
31
31
  export let mountTracker: MountFunction | null = null;
32
32
 
33
+ const readSignalValue = (value: any) => {
34
+ const callableValueSignal = value?.__canvasEngineCallableSignalValue;
35
+ if (callableValueSignal) return callableValueSignal();
36
+ return isSignal(value) ? value() : value;
37
+ };
38
+
39
+ const patchDefinePropsSignals = (target: Element, source: Element) => {
40
+ const targetSignals = (target as any)[DEFINE_PROPS_SIGNALS];
41
+ const sourceSignals = (source as any)[DEFINE_PROPS_SIGNALS];
42
+
43
+ if (!targetSignals || !sourceSignals) {
44
+ return;
45
+ }
46
+
47
+ Object.entries(sourceSignals as Record<string, any>).forEach(([key, sourceSignal]) => {
48
+ const targetSignal = targetSignals[key];
49
+ if (targetSignal && typeof targetSignal.set === "function") {
50
+ targetSignal.set(readSignalValue(sourceSignal));
51
+ }
52
+ });
53
+ };
54
+
33
55
  const getHotComponentRegistry = (): Map<string, HotComponentRecord> => {
34
56
  const hotGlobal = globalThis as any;
35
57
  if (!hotGlobal.__CANVAS_ENGINE_HOT_COMPONENTS__) {
@@ -274,6 +296,8 @@ export function createHotComponent<P>(
274
296
  nextProps.children = target.props.children;
275
297
  }
276
298
 
299
+ patchDefinePropsSignals(target, source);
300
+
277
301
  target.props = nextProps;
278
302
  target.propObservables = source.propObservables;
279
303
  target.componentInstance.onUpdate?.(nextProps);
@@ -1,6 +1,7 @@
1
1
  import { isSignal, signal } from "@signe/reactive"
2
2
  import { isPrimitive } from "../engine/reactive"
3
3
  import { currentDefinePropsTracker } from "../engine/signal"
4
+ import { isTrigger } from "../engine/trigger"
4
5
 
5
6
  /**
6
7
  * Converts props into reactive signals if they are primitive values.
@@ -47,7 +48,36 @@ type PropSchema = {
47
48
  [key: string]: PropType | PropType[] | PropConfig;
48
49
  }
49
50
 
50
- const toPropSignal = (value: any) => isSignal(value) ? value : signal(value)
51
+ const DEFINE_PROPS_CALLABLE_SIGNAL_VALUE = "__canvasEngineCallableSignalValue"
52
+
53
+ const createCallablePropSignal = (value: (...args: any[]) => any) => {
54
+ const valueSignal: any = signal(value)
55
+ const callable: any = (...args: any[]) => valueSignal()(...args)
56
+
57
+ callable[DEFINE_PROPS_CALLABLE_SIGNAL_VALUE] = valueSignal
58
+ callable.set = valueSignal.set
59
+ callable.freeze = valueSignal.freeze
60
+ callable.unfreeze = valueSignal.unfreeze
61
+ callable.mutate = valueSignal.mutate
62
+ callable.update = valueSignal.update
63
+ callable.observable = valueSignal.observable
64
+ callable._subject = valueSignal._subject
65
+
66
+ Object.defineProperty(callable, "_isFrozen", {
67
+ get: () => valueSignal._isFrozen,
68
+ set: (value) => {
69
+ valueSignal._isFrozen = value
70
+ }
71
+ })
72
+
73
+ return callable
74
+ }
75
+
76
+ const toPropSignal = (value: any) => {
77
+ if (isSignal(value) || isTrigger(value)) return value
78
+ if (typeof value === 'function') return createCallablePropSignal(value)
79
+ return signal(value)
80
+ }
51
81
 
52
82
  const definePropSignals = (props: any): any => {
53
83
  const obj: any = {}
@@ -133,6 +163,49 @@ export const useDefineProps = (props: any) => {
133
163
  }
134
164
  }
135
165
 
166
+ const getRawProps = (props: any) => isSignal(props) ? props() : (props ?? {})
167
+
168
+ const getEmitHandler = (handler: any) => {
169
+ if (handler?.[DEFINE_PROPS_CALLABLE_SIGNAL_VALUE]) return handler
170
+ if (isSignal(handler)) return handler()
171
+ return handler
172
+ }
173
+
174
+ /**
175
+ * Defines event emitters from component props.
176
+ *
177
+ * @param {object} props - The raw component props containing event handlers.
178
+ * @returns {function} A function returning named emit callbacks.
179
+ *
180
+ * @example
181
+ * const { select } = useDefineEmits({ select: (item) => item.id })();
182
+ * select({ id: 1 });
183
+ */
184
+ export const useDefineEmits = (props: any) => {
185
+ return () => new Proxy({}, {
186
+ get(_target, key) {
187
+ if (typeof key !== 'string') {
188
+ return undefined
189
+ }
190
+
191
+ return (...args: any[]) => {
192
+ const rawProps = getRawProps(props)
193
+ const handler = getEmitHandler(rawProps[key])
194
+
195
+ if (handler === undefined || handler === null) {
196
+ return undefined
197
+ }
198
+
199
+ if (typeof handler !== 'function') {
200
+ throw new Error(`Invalid emit handler: "${key}" must be a function`)
201
+ }
202
+
203
+ return handler(...args)
204
+ }
205
+ }
206
+ })
207
+ }
208
+
136
209
  /**
137
210
  * Validates the type of a property.
138
211
  *
package/src/index.ts CHANGED
@@ -10,7 +10,7 @@ export * from './engine/trigger'
10
10
  export * from './engine/bootstrap'
11
11
  export * from './engine/animation'
12
12
  export { FocusManager, focusManager, type ScrollOptions } from './engine/FocusManager'
13
- export { useProps, useDefineProps } from './hooks/useProps'
13
+ export { useProps, useDefineProps, useDefineEmits } from './hooks/useProps'
14
14
  export { useFocusIndex, useFocusedElement, useFocusChange } from './hooks/useFocus'
15
15
  export * from './utils/Ease'
16
16
  export * from './utils/RadialGradient'