@zcomponent/core 1.11.0-beta → 1.12.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.
@@ -1,6 +1,6 @@
1
1
  import { ActionBehavior } from '../actionbehavior';
2
2
  import { registerBehaviorRunAtDesignTime } from '../behavior';
3
- import { useCanvas, useOnAfterRender } from '../contexts/canvascontext';
3
+ import { CanvasContext } from '../contexts/canvascontext';
4
4
  import { SnapshotContext } from '../contexts/snapshotContext';
5
5
  import { Observable } from '../observable';
6
6
  export var ImageFormat;
@@ -65,21 +65,17 @@ export class TakeSnapshot extends ActionBehavior {
65
65
  ssctx.title.value = title;
66
66
  });
67
67
  this.dataUrl = '';
68
- this.capture = () => {
69
- const canvas = useCanvas(this.contextManager);
70
- if (!canvas) {
71
- console.error('No canvas found in context');
72
- return;
73
- }
68
+ this.capture = async () => {
74
69
  const snapshotContext = this.contextManager.get(SnapshotContext);
75
70
  const mimeType = snapshotContext.fileType.value === ImageFormat.PNG ? 'image/png' : 'image/jpeg';
76
- this.dataUrl = canvas.toDataURL(mimeType, this.quality.value);
71
+ const canvasContext = this.contextManager.get(CanvasContext);
72
+ this.dataUrl = await canvasContext.captureToDataURL(mimeType, this.quality.value);
77
73
  snapshotContext.canvasImage.value = this.dataUrl;
78
74
  };
79
75
  }
80
76
  /** @zprop */
81
77
  perform() {
82
- useOnAfterRender(this.contextManager).next.then(this.capture);
78
+ this.capture();
83
79
  }
84
80
  preview() {
85
81
  this.perform();
@@ -17,6 +17,20 @@ export declare class CanvasContext extends Context {
17
17
  readonly size: Observable<[number, number]>;
18
18
  private _resizeObserver;
19
19
  constructor(contextManager: ContextManager, constructorProps: CanvasContextOptions);
20
+ /**
21
+ * Captures the canvas content and returns it as a Data URL after the current frame has rendered.
22
+ * @param type The MIME image format type.
23
+ * @param quality Between 0 and 1 indicating image quality. Defaults to 1.
24
+ */
25
+ captureToDataURL(type?: 'image/png' | 'image/jpeg', quality?: number): Promise<string>;
26
+ /**
27
+ * Captures the canvas content and returns it as an ArrayBuffer after the current frame has rendered.
28
+ * @param type The MIME image format type.
29
+ * @param quality Between 0 and 1 indicating image quality. Defaults to 1.
30
+ */
31
+ captureToArrayBuffer(type?: 'image/png' | 'image/jpeg', quality?: number): Promise<ArrayBuffer>;
32
+ private canvasToBlob;
33
+ private blobToArrayBuffer;
20
34
  dispose(): never;
21
35
  }
22
36
  export declare enum HorizontalAlignment {
@@ -34,4 +48,16 @@ export declare function useOverlay(mgr: ContextManager): Observable<HTMLDivEleme
34
48
  export declare function useCanvasSize(mgr: ContextManager): Observable<[number, number], never>;
35
49
  export declare function useOnBeforeRender(mgr: ContextManager): Event<[number]>;
36
50
  export declare function useOnAfterRender(mgr: ContextManager): Event<[number]>;
51
+ /**
52
+ * Returns a promise that resovles a data url of the canvas as jpeg.
53
+ *
54
+ * For custom MIME types or quality settings, use`contextManager.get(CanvasContext).captureToDataURL();`
55
+ */
56
+ export declare function useCanvasToDataURL(mgr: ContextManager): Promise<string>;
57
+ /**
58
+ * Returns a promise that resolves an array buffer of the canvas image.
59
+ *
60
+ * For custom MIME types or quality settings, use`contextManager.get(CanvasContext).captureToArrayBuffer();`
61
+ */
62
+ export declare function useCanvasToArrayBuffer(mgr: ContextManager): Promise<ArrayBuffer>;
37
63
  export declare function classNameForAlignment(h: HorizontalAlignment, v: VerticalAlignment): string;
@@ -16,13 +16,13 @@ export class CanvasContext extends Context {
16
16
  }
17
17
  });
18
18
  if (!constructorProps.canvas) {
19
- this.canvas = document.createElement("canvas");
20
- this.canvas.style.position = "fixed";
21
- this.canvas.style.top = "0px";
22
- this.canvas.style.left = "0px";
23
- this.canvas.style.width = "100%";
24
- this.canvas.style.height = "100%";
25
- this.canvas.style.touchAction = "none";
19
+ this.canvas = document.createElement('canvas');
20
+ this.canvas.style.position = 'fixed';
21
+ this.canvas.style.top = '0px';
22
+ this.canvas.style.left = '0px';
23
+ this.canvas.style.width = '100%';
24
+ this.canvas.style.height = '100%';
25
+ this.canvas.style.touchAction = 'none';
26
26
  document.body.append(this.canvas);
27
27
  }
28
28
  else {
@@ -48,6 +48,54 @@ export class CanvasContext extends Context {
48
48
  lastRect = rect;
49
49
  });
50
50
  }
51
+ /**
52
+ * Captures the canvas content and returns it as a Data URL after the current frame has rendered.
53
+ * @param type The MIME image format type.
54
+ * @param quality Between 0 and 1 indicating image quality. Defaults to 1.
55
+ */
56
+ async captureToDataURL(type = 'image/jpeg', quality = 1) {
57
+ await useOnAfterRender(this.contextManager).next;
58
+ return this.canvas.toDataURL(type, quality);
59
+ }
60
+ /**
61
+ * Captures the canvas content and returns it as an ArrayBuffer after the current frame has rendered.
62
+ * @param type The MIME image format type.
63
+ * @param quality Between 0 and 1 indicating image quality. Defaults to 1.
64
+ */
65
+ async captureToArrayBuffer(type = 'image/jpeg', quality = 1) {
66
+ await useOnAfterRender(this.contextManager).next;
67
+ const blob = await this.canvasToBlob(type, quality);
68
+ return this.blobToArrayBuffer(blob);
69
+ }
70
+ canvasToBlob(type, quality) {
71
+ return new Promise((resolve, reject) => {
72
+ this.canvas.toBlob(blob => {
73
+ if (blob) {
74
+ resolve(blob);
75
+ }
76
+ else {
77
+ reject(new Error('Failed to capture canvas as Blob'));
78
+ }
79
+ }, type, quality);
80
+ });
81
+ }
82
+ blobToArrayBuffer(blob) {
83
+ return new Promise((resolve, reject) => {
84
+ const reader = new FileReader();
85
+ reader.onloadend = () => {
86
+ if (reader.result instanceof ArrayBuffer) {
87
+ resolve(reader.result);
88
+ }
89
+ else {
90
+ reject(new Error('Failed to read Blob as ArrayBuffer'));
91
+ }
92
+ };
93
+ reader.onerror = () => {
94
+ reject(new Error('Error reading Blob as ArrayBuffer'));
95
+ };
96
+ reader.readAsArrayBuffer(blob);
97
+ });
98
+ }
51
99
  dispose() {
52
100
  this.onBeforeRender.clearListeners();
53
101
  this.onAfterRender.clearListeners();
@@ -86,6 +134,22 @@ export function useOnBeforeRender(mgr) {
86
134
  export function useOnAfterRender(mgr) {
87
135
  return mgr.get(CanvasContext).onAfterRender;
88
136
  }
137
+ /**
138
+ * Returns a promise that resovles a data url of the canvas as jpeg.
139
+ *
140
+ * For custom MIME types or quality settings, use`contextManager.get(CanvasContext).captureToDataURL();`
141
+ */
142
+ export function useCanvasToDataURL(mgr) {
143
+ return mgr.get(CanvasContext).captureToDataURL('image/jpeg', 1);
144
+ }
145
+ /**
146
+ * Returns a promise that resolves an array buffer of the canvas image.
147
+ *
148
+ * For custom MIME types or quality settings, use`contextManager.get(CanvasContext).captureToArrayBuffer();`
149
+ */
150
+ export function useCanvasToArrayBuffer(mgr) {
151
+ return mgr.get(CanvasContext).captureToArrayBuffer('image/jpeg', 1);
152
+ }
89
153
  export function classNameForAlignment(h, v) {
90
154
  const names = ['zcomponent-align'];
91
155
  switch (h) {
package/lib/types.d.ts CHANGED
@@ -109,12 +109,15 @@ export interface Prop {
109
109
  default?: any;
110
110
  group?: string;
111
111
  groupPriority?: number;
112
+ groupCollapsed?: boolean;
112
113
  values?: Values[];
113
114
  order?: number;
114
115
  priority?: number;
115
116
  optional?: boolean;
116
117
  showInUI?: boolean;
117
118
  readonly?: boolean;
119
+ isConstructorProp?: boolean;
120
+ deprecated?: boolean;
118
121
  }
119
122
  export interface DefaultChild {
120
123
  label: string;
@@ -146,6 +149,7 @@ export interface ComponentInfo {
146
149
  streamsObjects: {
147
150
  [id: string]: string;
148
151
  };
152
+ deprecated?: boolean;
149
153
  }
150
154
  export interface ValueInfo {
151
155
  name: string;
package/lib/types.js CHANGED
@@ -140,6 +140,8 @@ export function mergeProps(a, b) {
140
140
  return;
141
141
  if ((a.readonly && !b.readonly) || (b.readonly && !a.readonly))
142
142
  return;
143
+ if ((a.deprecated ?? false) !== (b.deprecated ?? false))
144
+ return;
143
145
  const type = mergeTypes(a.type, b.type);
144
146
  if (!type)
145
147
  return;
@@ -153,9 +155,11 @@ export function mergeProps(a, b) {
153
155
  default: def,
154
156
  group: a.group === b.group ? a.group : undefined,
155
157
  groupPriority: Math.max(a.groupPriority ?? 0, b.groupPriority ?? 0),
158
+ groupCollapsed: a.groupCollapsed || b.groupCollapsed,
156
159
  priority: Math.max(a.priority ?? 0, b.priority ?? 0),
157
160
  values: mergeValues(a.values, b.values),
158
161
  order: Math.min(a.order ?? 0, b.order ?? 0),
162
+ deprecated: a.deprecated,
159
163
  readonly: a.readonly,
160
164
  };
161
165
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zcomponent/core",
3
- "version": "1.11.0-beta",
3
+ "version": "1.12.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",