@vctrl/hooks 0.0.9 → 0.1.1

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 (41) hide show
  1. package/LICENSE.md +650 -0
  2. package/index.cjs.js +1 -0
  3. package/index.es.js +8 -0
  4. package/model-context-ClI5AGc8.js +19136 -0
  5. package/model-context-mPTTbSMx.cjs +3849 -0
  6. package/package.json +1 -1
  7. package/use-load-model/event-system.d.ts +7 -0
  8. package/use-load-model/file-type-hooks/use-load-binary.d.ts +11 -0
  9. package/use-load-model/file-type-hooks/use-load-gltf.d.ts +5 -0
  10. package/use-load-model/loaders/create-gltf-loader.d.ts +3 -0
  11. package/{src/use-load-model/loaders/create-usdz-loader.ts → use-load-model/loaders/create-usdz-loader.d.ts} +1 -7
  12. package/use-load-model/model-context.d.ts +20 -0
  13. package/use-load-model/state.d.ts +11 -0
  14. package/use-load-model/types.d.ts +48 -0
  15. package/use-load-model/use-load-model.d.ts +11 -0
  16. package/use-load-model/utils/array-buffer-to-base64.d.ts +2 -0
  17. package/use-load-model/utils/read-directory.d.ts +8 -0
  18. package/use-load-model.cjs.js +1 -0
  19. package/use-load-model.es.js +8 -0
  20. package/.babelrc +0 -12
  21. package/.eslintrc.json +0 -18
  22. package/LICENSE +0 -0
  23. package/project.json +0 -16
  24. package/src/use-load-model/event-system.ts +0 -29
  25. package/src/use-load-model/file-type-hooks/use-load-binary.ts +0 -74
  26. package/src/use-load-model/file-type-hooks/use-load-gltf.ts +0 -115
  27. package/src/use-load-model/loaders/create-gltf-loader.ts +0 -20
  28. package/src/use-load-model/model-context.tsx +0 -18
  29. package/src/use-load-model/state.ts +0 -33
  30. package/src/use-load-model/types.ts +0 -53
  31. package/src/use-load-model/use-load-model.ts +0 -111
  32. package/src/use-load-model/utils/array-buffer-to-base64.ts +0 -20
  33. package/src/use-load-model/utils/read-directory.ts +0 -31
  34. package/tsconfig.json +0 -21
  35. package/tsconfig.lib.json +0 -25
  36. package/vite.config.ts +0 -59
  37. /package/{src/index.ts → index.d.ts} +0 -0
  38. /package/{src/use-load-model/file-type-hooks/index.ts → use-load-model/file-type-hooks/index.d.ts} +0 -0
  39. /package/{src/use-load-model/index.ts → use-load-model/index.d.ts} +0 -0
  40. /package/{src/use-load-model/loaders/index.ts → use-load-model/loaders/index.d.ts} +0 -0
  41. /package/{src/use-load-model/utils/index.ts → use-load-model/utils/index.d.ts} +0 -0
@@ -1,111 +0,0 @@
1
- import { useReducer, useCallback, useRef, useEffect } from 'react';
2
-
3
- import { ModelFileTypes, InputFileOrDirectory } from './types';
4
- import { useLoadBinary, useLoadGltf } from './file-type-hooks';
5
- import { readDirectory } from './utils';
6
-
7
- import eventSystem from './event-system';
8
- import reducer, { initialState } from './state';
9
-
10
- function useLoadModel() {
11
- const uploadCompleteRef = useRef(false);
12
- const [state, dispatch] = useReducer(reducer, initialState);
13
-
14
- const { loadGltf } = useLoadGltf(dispatch);
15
- const { loadBinary } = useLoadBinary(dispatch);
16
-
17
- const getFileOfType = useCallback(
18
- (files: File[], fileType: ModelFileTypes) =>
19
- files.find((file) => file.name.endsWith('.' + fileType)),
20
- [],
21
- );
22
-
23
- const updateProgress = useCallback((progress: number) => {
24
- dispatch({ type: 'SET_PROGRESS', payload: progress });
25
- eventSystem.emit('UPLOAD_PROGRESS', progress);
26
- }, []);
27
-
28
- const processFiles = useCallback(
29
- (files: File[]) => {
30
- if (files.length === 0) return;
31
-
32
- dispatch({ type: 'SET_FILE_LOADING', payload: true });
33
- updateProgress(0);
34
- uploadCompleteRef.current = false;
35
-
36
- const gltfFile = getFileOfType(files, ModelFileTypes.gltf);
37
- const glbFile = getFileOfType(files, ModelFileTypes.glb);
38
- const usdzFile = getFileOfType(files, ModelFileTypes.usdz);
39
-
40
- const supportedFiles = [gltfFile, glbFile, usdzFile].filter(
41
- Boolean,
42
- ) as File[];
43
-
44
- if (supportedFiles.length > 1) {
45
- eventSystem.emit('MULTIPLE_3D_MODELS', supportedFiles);
46
- return;
47
- }
48
-
49
- const otherFiles = files.filter(
50
- (file) => file !== gltfFile && file !== glbFile && file !== usdzFile,
51
- );
52
-
53
- const updateFileProgress = (progress: number) => {
54
- updateProgress(progress);
55
-
56
- if (progress === 100) {
57
- uploadCompleteRef.current = true;
58
- }
59
- };
60
-
61
- if (gltfFile) {
62
- loadGltf(gltfFile, otherFiles, updateFileProgress);
63
- } else if (glbFile) {
64
- loadBinary(glbFile, ModelFileTypes.glb, () => updateFileProgress(100));
65
- } else if (usdzFile) {
66
- loadBinary(usdzFile, ModelFileTypes.usdz, () =>
67
- updateFileProgress(100),
68
- );
69
- } else {
70
- eventSystem.emit('UNSUPPORTED_FILE_TYPE', files);
71
- dispatch({ type: 'SET_FILE_LOADING', payload: false });
72
- return;
73
- }
74
- },
75
- [updateProgress, getFileOfType, loadGltf, loadBinary],
76
- );
77
-
78
- const handleFileUpload = useCallback(
79
- async (filesOrDirectories: InputFileOrDirectory) => {
80
- const allFiles: File[] = [];
81
-
82
- for (const item of filesOrDirectories) {
83
- if (item instanceof File) {
84
- allFiles.push(item);
85
- } else if ('kind' in item && item.kind === 'directory') {
86
- const directoryFiles = await readDirectory(item);
87
- allFiles.push(...directoryFiles);
88
- }
89
- }
90
-
91
- processFiles(allFiles);
92
- },
93
- [processFiles],
94
- );
95
-
96
- useEffect(() => {
97
- if (uploadCompleteRef.current && state.file) {
98
- eventSystem.emit('UPLOAD_COMPLETE', state.file);
99
- uploadCompleteRef.current = false;
100
- }
101
- }, [state.file]);
102
-
103
- return {
104
- ...state,
105
- on: eventSystem.on,
106
- off: eventSystem.off,
107
- handleFileUpload,
108
- };
109
- }
110
-
111
- export default useLoadModel;
@@ -1,20 +0,0 @@
1
- // Helper function to convert ArrayBuffer to base64
2
- async function arrayBufferToBase64(arrayBuffer: ArrayBuffer) {
3
- // Create a Blob from the ArrayBuffer
4
- const blob = new Blob([arrayBuffer]);
5
-
6
- // Use the FileReader to convert the Blob to a Base64 string
7
- const reader = new FileReader();
8
- return new Promise((resolve, reject) => {
9
- reader.onloadend = () => {
10
- if (!reader?.result) return;
11
- // Extract the Base64 string from the data URL
12
- const base64String = (reader?.result as string).split(',')[1];
13
- resolve(base64String);
14
- };
15
- reader.onerror = reject;
16
- reader.readAsDataURL(blob);
17
- });
18
- }
19
-
20
- export default arrayBufferToBase64;
@@ -1,31 +0,0 @@
1
- /**
2
- * Recursively reads a directory
3
- *
4
- * @param directoryHandle - The directory handle to read
5
- * @returns - An array of file objects
6
- */
7
- async function readDirectory(
8
- directoryHandle: FileSystemDirectoryHandle,
9
- ): Promise<File[]> {
10
- const files: File[] = [];
11
-
12
- async function* getFilesRecursively(
13
- entry: FileSystemDirectoryHandle,
14
- ): AsyncGenerator<File> {
15
- for await (const [, handle] of entry) {
16
- if (handle.kind === 'file') {
17
- yield await (handle as FileSystemFileHandle).getFile();
18
- } else if (handle.kind === 'directory') {
19
- yield* getFilesRecursively(handle as FileSystemDirectoryHandle);
20
- }
21
- }
22
- }
23
-
24
- for await (const file of getFilesRecursively(directoryHandle)) {
25
- files.push(file);
26
- }
27
-
28
- return files;
29
- }
30
-
31
- export default readDirectory;
package/tsconfig.json DELETED
@@ -1,21 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "jsx": "react-jsx",
4
- "lib": ["ESNext", "DOM", "DOM.Iterable", "DOM.AsyncIterable"],
5
- "allowJs": false,
6
- "esModuleInterop": false,
7
- "allowSyntheticDefaultImports": true,
8
- "strict": true,
9
- "isolatedModules": true,
10
- "noEmit": true,
11
- "types": ["vite/client"]
12
- },
13
- "files": [],
14
- "include": [],
15
- "references": [
16
- {
17
- "path": "./tsconfig.lib.json"
18
- }
19
- ],
20
- "extends": "../../tsconfig.base.json"
21
- }
package/tsconfig.lib.json DELETED
@@ -1,25 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "noEmit": true,
5
- "isolatedModules": true,
6
- "declaration": true,
7
- "types": [
8
- "node",
9
- "@nx/react/typings/cssmodule.d.ts",
10
- "@nx/react/typings/image.d.ts",
11
- "vite/client"
12
- ]
13
- },
14
- "exclude": [
15
- "**/*.spec.ts",
16
- "**/*.test.ts",
17
- "**/*.spec.tsx",
18
- "**/*.test.tsx",
19
- "**/*.spec.js",
20
- "**/*.test.js",
21
- "**/*.spec.jsx",
22
- "**/*.test.jsx"
23
- ],
24
- "include": ["src/**/*.js", "src/**/*.jsx", "src/**/*.ts", "src/**/*.tsx"]
25
- }
package/vite.config.ts DELETED
@@ -1,59 +0,0 @@
1
- /// <reference types='vitest' />
2
- import { defineConfig } from 'vite';
3
- import react from '@vitejs/plugin-react';
4
- import dts from 'vite-plugin-dts';
5
- import * as path from 'path';
6
- import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
7
-
8
- export default defineConfig({
9
- root: __dirname,
10
- cacheDir: '../../node_modules/.vite/packages/@vctrl/hooks',
11
-
12
- plugins: [
13
- react(),
14
- nxViteTsPaths(),
15
- dts({
16
- entryRoot: 'src',
17
- tsconfigPath: path.join(__dirname, 'tsconfig.lib.json'),
18
- }),
19
- ],
20
-
21
- // Uncomment this if you are using workers.
22
- // worker: {
23
- // plugins: [ nxViteTsPaths() ],
24
- // },
25
-
26
- // Configuration for building your library.
27
- // See: https://vitejs.dev/guide/build.html#library-mode
28
- build: {
29
- outDir: '../../dist/packages/@vctrl/hooks',
30
- emptyOutDir: true,
31
- reportCompressedSize: true,
32
- commonjsOptions: {
33
- transformMixedEsModules: true,
34
- },
35
- lib: {
36
- entry: {
37
- index: path.resolve(__dirname, 'src/index.ts'),
38
- 'use-load-model': path.resolve(
39
- __dirname,
40
- 'src/use-load-model/index.ts',
41
- ),
42
- },
43
- name: '@vctrl/hooks',
44
- formats: ['es', 'cjs'],
45
- fileName: (format, entry) => `${entry}.${format}.js`,
46
- },
47
-
48
- rollupOptions: {
49
- // External packages that should not be bundled into your library.
50
- external: ['react', 'react-dom', 'react/jsx-runtime'],
51
- output: {
52
- globals: {
53
- react: 'React',
54
- 'react-dom': 'ReactDOM',
55
- },
56
- },
57
- },
58
- },
59
- });
File without changes