@vctrl/hooks 0.16.2 → 0.17.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 (76) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/GLTFLoader-CygFefRr.cjs +1 -0
  3. package/GLTFLoader-NiJ7qzh1.js +1381 -0
  4. package/README.md +0 -20
  5. package/__vite-browser-external-Bn_csQuM.js +7 -0
  6. package/__vite-browser-external-CVmj8TrW.cjs +1 -0
  7. package/chunk-foFoljWS.js +23 -0
  8. package/chunk-gGpyby2o.cjs +1 -0
  9. package/index.cjs.js +1 -0
  10. package/index.d.ts +5 -0
  11. package/index.es.js +4 -0
  12. package/lib-DKn2MJ9X.js +4111 -0
  13. package/lib-DZ-MihdJ.cjs +2 -0
  14. package/model-exporter-SMSAY90N.cjs +1 -0
  15. package/model-exporter-cvguDGnn.js +6512 -0
  16. package/package.json +1 -1
  17. package/server-communication-Bj6ZxFw2.js +4659 -0
  18. package/server-communication-yXZR8eso.cjs +18 -0
  19. package/use-export-model/use-export-model.d.ts +14 -0
  20. package/use-export-model-CaijtW1c.cjs +1 -0
  21. package/use-export-model-DzVKdqnY.js +49 -0
  22. package/use-export-model.cjs.js +1 -0
  23. package/use-export-model.es.js +2 -0
  24. package/use-load-model/event-system.d.ts +7 -0
  25. package/use-load-model/index.d.ts +4 -0
  26. package/use-load-model/state.d.ts +19 -0
  27. package/use-load-model/types.d.ts +231 -0
  28. package/use-load-model/use-load-model.d.ts +30 -0
  29. package/use-load-model/utils/calculate-referenced-bytes.d.ts +8 -0
  30. package/use-load-model/utils/index.d.ts +4 -0
  31. package/use-load-model/utils/read-directory.d.ts +8 -0
  32. package/use-load-model/utils/reconstruct-files.d.ts +29 -0
  33. package/use-load-model/utils/resolve-scene-payload.d.ts +6 -0
  34. package/use-load-model-B6UXiFWh.js +517 -0
  35. package/use-load-model-CH-WPQ_s.cjs +1 -0
  36. package/use-load-model.cjs.js +1 -0
  37. package/use-load-model.es.js +18 -0
  38. package/{src/use-optimize-model/index.ts → use-optimize-model/index.d.ts} +2 -2
  39. package/use-optimize-model/state.d.ts +13 -0
  40. package/use-optimize-model/types.d.ts +39 -0
  41. package/use-optimize-model/use-calc-optimization-info.d.ts +6 -0
  42. package/use-optimize-model/use-optimize-model.d.ts +119 -0
  43. package/use-optimize-model/utils/index.d.ts +2 -0
  44. package/use-optimize-model/utils/texture-optimization.d.ts +17 -0
  45. package/use-optimize-model/utils/validation.d.ts +8 -0
  46. package/use-optimize-model-C-fzzKh6.cjs +1 -0
  47. package/use-optimize-model-Cb3WMvDZ.js +299 -0
  48. package/use-optimize-model.cjs.js +1 -0
  49. package/use-optimize-model.es.js +2 -0
  50. package/utils/server-communication.d.ts +122 -0
  51. package/project.json +0 -55
  52. package/src/index.ts +0 -38
  53. package/src/use-export-model/use-export-model.ts +0 -113
  54. package/src/use-load-model/event-system.ts +0 -46
  55. package/src/use-load-model/index.ts +0 -15
  56. package/src/use-load-model/model-context.tsx +0 -174
  57. package/src/use-load-model/state.ts +0 -75
  58. package/src/use-load-model/types.ts +0 -284
  59. package/src/use-load-model/use-load-model.ts +0 -682
  60. package/src/use-load-model/utils/calculate-referenced-bytes.ts +0 -123
  61. package/src/use-load-model/utils/index.ts +0 -7
  62. package/src/use-load-model/utils/read-directory.ts +0 -35
  63. package/src/use-load-model/utils/reconstruct-files.ts +0 -85
  64. package/src/use-load-model/utils/resolve-scene-payload.ts +0 -151
  65. package/src/use-optimize-model/state.ts +0 -40
  66. package/src/use-optimize-model/types.ts +0 -37
  67. package/src/use-optimize-model/use-calc-optimization-info.ts +0 -101
  68. package/src/use-optimize-model/use-optimize-model.ts +0 -467
  69. package/src/use-optimize-model/utils/index.ts +0 -29
  70. package/src/use-optimize-model/utils/texture-optimization.ts +0 -292
  71. package/src/use-optimize-model/utils/validation.ts +0 -62
  72. package/src/utils/server-communication.ts +0 -351
  73. package/tsconfig.json +0 -21
  74. package/tsconfig.lib.json +0 -16
  75. package/vite.config.ts +0 -77
  76. /package/{src/use-export-model/index.ts → use-export-model/index.d.ts} +0 -0
@@ -0,0 +1,19 @@
1
+ import { Action, LoadData } from './types';
2
+ /**
3
+ * Initial state for the useReadModelFiles hook
4
+ *
5
+ * @property file - Currently loaded file
6
+ * @property isFileLoading - Flag indicating if a file is currently being loaded
7
+ * @property progress - Current loading progress (0-100)
8
+ * @property supportedFileTypes - List of supported file types
9
+ */
10
+ export declare const initialState: LoadData;
11
+ /**
12
+ * Reducer function for the useReadModelFiles hook
13
+ *
14
+ * @param state - The current state of the reducer
15
+ * @param action - The action to be performed
16
+ * @returns The updated state
17
+ */
18
+ declare function reducer(state: LoadData, action: Action): LoadData;
19
+ export default reducer;
@@ -0,0 +1,231 @@
1
+ import { ModelFileTypes } from '../../../core/src/model-loader/index.ts';
2
+ import { Object3D } from 'three';
3
+ import { useOptimizeModel } from '../use-optimize-model';
4
+ import { default as eventSystem } from './event-system';
5
+ import { initialState } from './state';
6
+ import { ServerOptions, ServerSceneData } from '../../../core/src/index.ts';
7
+ export type { ExtendedGLTFDocument, SceneAssetDataEntry, SerializedSceneAssetDataMap as SceneAssetDataMap, ServerSceneData, ServerScenePayload } from '../../../core/src/index.ts';
8
+ /**
9
+ * Type representing the input for file/folder uploads.
10
+ * Can be either File objects or FileSystemDirectoryHandle for folder drag-and-drop.
11
+ */
12
+ export type InputFileOrDirectory = (File | FileSystemDirectoryHandle)[];
13
+ /**
14
+ * Represents a loaded 3D model file with its metadata.
15
+ */
16
+ export interface ModelFile {
17
+ /** The Three.js Object3D scene containing the loaded model */
18
+ model: Object3D;
19
+ /** The file type/format of the model (GLTF, GLB, USDZ) */
20
+ type: ModelFileTypes;
21
+ /** The original filename of the model */
22
+ name: string;
23
+ /** Byte size of referenced source package (gltf + referenced buffers/images) */
24
+ sourcePackageBytes?: number;
25
+ /** Byte size of referenced texture assets from source package */
26
+ sourceTextureBytes?: number;
27
+ }
28
+ /**
29
+ * Configuration options for loading a scene from the server.
30
+ */
31
+ export interface SceneLoadOptions {
32
+ /** The unique identifier of the scene to load */
33
+ sceneId: string;
34
+ /** Server configuration (endpoint, auth, headers) */
35
+ serverOptions?: ServerOptions;
36
+ /** Whether to automatically apply scene settings (default: true) */
37
+ applySettings?: boolean;
38
+ }
39
+ /**
40
+ * Configuration options for loading an already-resolved scene payload.
41
+ */
42
+ export interface SceneDataLoadOptions {
43
+ /** Optional scene identifier for resolved payloads. */
44
+ sceneId?: string;
45
+ /** Resolved server scene payload */
46
+ sceneData: ServerSceneData;
47
+ /** Whether to automatically apply scene settings (default: true) */
48
+ applySettings?: boolean;
49
+ }
50
+ /**
51
+ * Result of a scene load operation based on server data.
52
+ * Combines the loaded model with its settings.
53
+ */
54
+ export interface SceneLoadResult extends ServerSceneData {
55
+ /** The loaded model file */
56
+ file: ModelFile;
57
+ /** Scene ID that was loaded */
58
+ sceneId?: string;
59
+ }
60
+ /**
61
+ * State interface for model loading data.
62
+ * Contains the current loaded file, loading status, and progress information.
63
+ */
64
+ export interface LoadData {
65
+ /** The currently loaded model file, or null if no model is loaded */
66
+ file: ModelFile | null;
67
+ /** Whether a file is currently being loaded */
68
+ isFileLoading: boolean;
69
+ /** Loading progress percentage (0-100) */
70
+ progress: number;
71
+ /** List of supported model file types */
72
+ supportedFileTypes: ModelFileTypes[];
73
+ }
74
+ /**
75
+ * Action types for the model loading reducer.
76
+ * Defines all possible state mutations for model loading operations.
77
+ */
78
+ export type Action = {
79
+ type: 'set-file';
80
+ payload: ModelFile;
81
+ } | {
82
+ type: 'set-file-loading';
83
+ payload: boolean;
84
+ } | {
85
+ type: 'set-progress';
86
+ payload: number;
87
+ } | {
88
+ type: 'reset-state';
89
+ };
90
+ /**
91
+ * Available event types emitted by the model loading system.
92
+ * Used for subscribing to various stages of the loading process.
93
+ */
94
+ export type EventTypes = 'multiple-models' | 'not-loaded-files' | 'load-start' | 'load-progress' | 'load-complete' | 'load-reset' | 'load-error' | 'server-load-start' | 'server-load-complete' | 'server-load-error';
95
+ /**
96
+ * Maps event types to their corresponding data payloads.
97
+ * Ensures type safety when handling events.
98
+ */
99
+ export type EventData = {
100
+ /** Array of model files when multiple are detected */
101
+ 'multiple-models': File[];
102
+ /** Array of unsupported files */
103
+ 'not-loaded-files': File[];
104
+ /** No data for load start event */
105
+ 'load-start': null;
106
+ /** Progress value (0-100) */
107
+ 'load-progress': number;
108
+ /** The loaded model file data */
109
+ 'load-complete': LoadData['file'];
110
+ /** No data for reset event */
111
+ 'load-reset': null;
112
+ /** Error object when loading fails */
113
+ 'load-error': Error | unknown;
114
+ /** Scene ID being loaded from server */
115
+ 'server-load-start': string;
116
+ /** Complete scene load result */
117
+ 'server-load-complete': SceneLoadResult;
118
+ /** Error during server scene loading */
119
+ 'server-load-error': Error | unknown;
120
+ };
121
+ /**
122
+ * Type-safe event handler function.
123
+ * @template T - The event type being handled
124
+ */
125
+ export type EventHandler<T extends EventTypes> = (data?: EventData[T]) => void;
126
+ /**
127
+ * Return type for the useLoadModel hook.
128
+ * Conditionally includes optimizer integration based on whether an optimizer was provided.
129
+ *
130
+ * @template HasOptimizer - Boolean indicating if optimizer integration is included
131
+ */
132
+ export type UseLoadModelReturn<HasOptimizer extends boolean> = typeof initialState & {
133
+ /**
134
+ * Subscribe to model loading events.
135
+ * @param event - The event type to listen for
136
+ * @param handler - Callback function to handle the event
137
+ */
138
+ on: typeof eventSystem.on;
139
+ /**
140
+ * Unsubscribe from model loading events.
141
+ * @param event - The event type to stop listening for
142
+ * @param handler - The callback function to remove
143
+ */
144
+ off: typeof eventSystem.off;
145
+ /**
146
+ * Load 3D model files from File objects or directory handles.
147
+ * Supports GLTF, GLB, and USDZ formats with associated assets.
148
+ */
149
+ load: (filesOrDirectories: InputFileOrDirectory) => Promise<void>;
150
+ /**
151
+ * Load a scene from already-resolved scene data.
152
+ * Preserves original asset payload semantics and bypasses upload-style file processing.
153
+ */
154
+ loadFromData: (options: SceneDataLoadOptions) => Promise<SceneLoadResult>;
155
+ /**
156
+ * Load a scene from the server by scene ID.
157
+ * Fetches both the model and scene settings, applies them automatically.
158
+ *
159
+ * @param options - Scene loading configuration
160
+ * @returns Promise resolving to the loaded scene data
161
+ *
162
+ * @example
163
+ * ```tsx
164
+ * const model = useLoadModel()
165
+ *
166
+ * // Load a scene from the server
167
+ * const scene = await model.loadFromServer({
168
+ * sceneId: 'abc-123',
169
+ * serverOptions: {
170
+ * endpoint: '/api/load-scene',
171
+ * apiKey: 'optional-auth-token'
172
+ * }
173
+ * })
174
+ * ```
175
+ */
176
+ loadFromServer: (options: SceneLoadOptions) => Promise<SceneLoadResult>;
177
+ /**
178
+ * Reset the model loading state and clear any loaded models.
179
+ */
180
+ reset: () => void;
181
+ /**
182
+ * Optimizer integration object.
183
+ * - When optimizer is provided: Contains full optimization methods and state
184
+ * - When no optimizer: null
185
+ */
186
+ optimizer: HasOptimizer extends true ? OptimizerIntegrationReturn<true> : null;
187
+ };
188
+ /**
189
+ * Return type of the useOptimizeModel hook.
190
+ * Provides access to all optimization methods and state.
191
+ */
192
+ export type OptimizerReturnType = ReturnType<typeof useOptimizeModel>;
193
+ /**
194
+ * Conditional return type for optimizer integration.
195
+ * Extends the optimizer with additional integration methods when present.
196
+ *
197
+ * @template HasOptimizer - Boolean indicating if optimizer is integrated
198
+ */
199
+ export type OptimizerIntegrationReturn<HasOptimizer extends boolean = false> = HasOptimizer extends true ? OptimizerReturnType & {
200
+ /**
201
+ * Whether the optimizer is still preparing the currently visible model.
202
+ */
203
+ isPreparing: boolean;
204
+ /**
205
+ * Applies an optimization and updates the loaded model with the result.
206
+ *
207
+ * This method:
208
+ * 1. Runs the specified optimization function
209
+ * 2. Retrieves the optimized model
210
+ * 3. Loads it back into the scene
211
+ * 4. Updates the model state
212
+ *
213
+ * @template TOptions - Type of options for the optimization function
214
+ * @param optimizationFunction - The optimization to apply (e.g., simplifyOptimization)
215
+ * @param options - Configuration options for the optimization
216
+ * @returns Promise that resolves when optimization is complete and model is updated
217
+ *
218
+ * @example
219
+ * // Apply simplification optimization
220
+ * await optimizer.applyOptimization(
221
+ * optimizer.simplifyOptimization,
222
+ * { ratio: 0.5 }
223
+ * )
224
+ *
225
+ * @example
226
+ * // Apply multiple optimizations in sequence
227
+ * await optimizer.applyOptimization(optimizer.dedupOptimization)
228
+ * await optimizer.applyOptimization(optimizer.quantizeOptimization, { bits: 12 })
229
+ */
230
+ applyOptimization: <TOptions>(optimizationFunction?: ((options?: TOptions) => Promise<void>) | undefined, options?: TOptions) => Promise<void>;
231
+ } : null;
@@ -0,0 +1,30 @@
1
+ import { UseLoadModelReturn } from './types';
2
+ import { useOptimizeModel } from '../use-optimize-model';
3
+ /**
4
+ * Custom hook to load and manage 3D models with optional optimization integration.
5
+ *
6
+ * This hook provides a complete solution for loading 3D models (GLTF, GLB, USDZ) with
7
+ * optional integration of model optimization capabilities. The return type is conditionally
8
+ * typed based on whether an optimizer is provided.
9
+ *
10
+ * **Type Safety:**
11
+ * - When called with an optimizer: `optimizer` property is fully typed with optimization methods
12
+ * - When called without an optimizer: `optimizer` property is typed as `null`
13
+ *
14
+ * @example
15
+ * // With optimizer integration
16
+ * const optimizer = useOptimizeModel()
17
+ * const model = useLoadModel(optimizer)
18
+ * model.optimizer.applyOptimization() // ✅ Fully typed
19
+ *
20
+ * @example
21
+ * // Without optimizer
22
+ * const model = useLoadModel()
23
+ * model.optimizer // ✅ Typed as null
24
+ *
25
+ * @template T - The type of the optimizer parameter (inferred automatically)
26
+ * @param optimizer - Optional optimizer hook returned from useOptimizeModel
27
+ * @returns Model loading state and methods, with conditionally typed optimizer property
28
+ */
29
+ declare function useLoadModel<T extends ReturnType<typeof useOptimizeModel> | undefined>(optimizer?: T): UseLoadModelReturn<T extends undefined ? false : true>;
30
+ export default useLoadModel;
@@ -0,0 +1,8 @@
1
+ import { ServerSceneData } from '../types';
2
+ type ReferencedBytes = {
3
+ sourcePackageBytes: number;
4
+ textureBytes: number;
5
+ };
6
+ export declare function calculateReferencedBytesFromFiles(gltfFile: File, assetFiles: File[]): Promise<ReferencedBytes>;
7
+ export declare function calculateReferencedBytesFromServerScene(data: ServerSceneData): ReferencedBytes;
8
+ export {};
@@ -0,0 +1,4 @@
1
+ export { default as readDirectory } from './read-directory';
2
+ export { reconstructGltfFiles } from './reconstruct-files';
3
+ export { calculateReferencedBytesFromFiles, calculateReferencedBytesFromServerScene } from './calculate-referenced-bytes';
4
+ export { resolveServerSceneDataContract } from './resolve-scene-payload';
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Recursively reads a directory
3
+ *
4
+ * @param directoryHandle - The directory handle to read
5
+ * @returns - An array of file objects
6
+ */
7
+ declare function readDirectory(directoryHandle: FileSystemDirectoryHandle): Promise<File[]>;
8
+ export default readDirectory;
@@ -0,0 +1,29 @@
1
+ import { InputFileOrDirectory, ServerSceneData } from '../types';
2
+ /**
3
+ * Reconstructs GLTF and asset files from scene data received from the server.
4
+ *
5
+ * This function converts the server's scene data format (GLTF JSON + binary assets)
6
+ * back into File objects that can be loaded by the model loader. This is the reverse
7
+ * operation of what happens when a scene is saved to the server.
8
+ *
9
+ * The function:
10
+ * 1. Converts binary asset data (stored as number arrays) back into File objects
11
+ * 2. Creates a GLTF file from the JSON structure
12
+ * 3. Returns an array of files compatible with the model loading system
13
+ *
14
+ * @param data - Scene data from the server containing GLTF JSON and assets
15
+ * @returns Array of File objects ready to be loaded (GLTF file + asset files)
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * // Load scene data from server
20
+ * const sceneData = await ServerCommunicationService.loadScene('scene-123')
21
+ *
22
+ * // Reconstruct files
23
+ * const files = reconstructGltfFiles(sceneData)
24
+ *
25
+ * // Load into the viewer
26
+ * await modelLoader.load(files)
27
+ * ```
28
+ */
29
+ export declare function reconstructGltfFiles(data: ServerSceneData): InputFileOrDirectory;
@@ -0,0 +1,6 @@
1
+ import { ServerSceneData, ServerScenePayload } from '../../../../core/src/index.ts';
2
+ /**
3
+ * Resolves raw scene payload from the API into normalized scene data used by hooks.
4
+ * This keeps payload-shape adaptation in one explicit contract boundary.
5
+ */
6
+ export declare function resolveServerSceneDataContract(payload: ServerScenePayload): ServerSceneData;