Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module.exports = {
parser: "@typescript-eslint/parser", // Eslint TypeScript Parser
extends: ["prettier/@typescript-eslint", "plugin:prettier/recommended"],
extends: [
"prettier/@typescript-eslint",
"plugin:prettier/recommended",
"plugin:storybook/recommended"
],
plugins: ["@typescript-eslint"],
parserOptions: {
ecmaVersion: 2019,
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ stats.html
tsconfig.tsbuildinfo
oasis-npm
api
miniprogram.js
miniprogram.js
*storybook.log
storybook-static
68 changes: 68 additions & 0 deletions .storybook/decorators/withCanvas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { WebGLEngine, type WebGLEngineConfiguration } from "@galacean/engine";
import { makeDecorator, useEffect } from 'storybook/preview-api';

export interface CanvasOptions {
width?: number;
height?: number;
engineOptions?: Omit<WebGLEngineConfiguration, 'canvas'>
}

export const withCanvas = makeDecorator({
name: 'withCanvas',
parameterName: 'canvas',
wrapper: (storyFn, context, { parameters = {} }) => {
const container = document.createElement('div');
container.style.width = '100vw';
container.style.height = '100vh';
container.style.position = 'relative';
container.style.overflow = 'hidden';

const canvas = document.createElement('canvas');
canvas.id = 'canvas';
canvas.style.width = '100%';
canvas.style.height = '100%';
container.appendChild(canvas);

const canvasOptions = context.parameters.canvas || {};

const enginePromise = WebGLEngine.create({
canvas: canvas,
graphicDeviceOptions: {

},
...(canvasOptions.engineOptions || {})
});

context.getEngine = () => {
return enginePromise;
};

const resizeObserver = new ResizeObserver(() => {
context.getEngine().then((engine) => {
engine.canvas.resizeByClientSize(window.devicePixelRatio);
})
})
resizeObserver.observe(container)
Comment on lines +40 to +45

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling for ResizeObserver callback.

The ResizeObserver callback should handle potential promise rejections and avoid resize operations if the engine isn't ready.

const resizeObserver = new ResizeObserver(() => {
-  context.getEngine().then((engine) => {
-    engine.canvas.resizeByClientSize(window.devicePixelRatio);
-  })
+  context.getEngine().then((engine) => {
+    if (engine && !engine.isDestroyed) {
+      engine.canvas.resizeByClientSize(window.devicePixelRatio);
+    }
+  }).catch((error) => {
+    console.warn('Failed to resize canvas:', error);
+  });
})
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const resizeObserver = new ResizeObserver(() => {
context.getEngine().then((engine) => {
engine.canvas.resizeByClientSize(window.devicePixelRatio);
})
})
resizeObserver.observe(container)
const resizeObserver = new ResizeObserver(() => {
context.getEngine().then((engine) => {
if (engine && !engine.isDestroyed) {
engine.canvas.resizeByClientSize(window.devicePixelRatio);
}
}).catch((error) => {
console.warn('Failed to resize canvas:', error);
});
})
resizeObserver.observe(container)
🤖 Prompt for AI Agents
In .storybook/decorators/withCanvas.ts around lines 40 to 45, the ResizeObserver
callback calls context.getEngine() which returns a promise but lacks error
handling. Modify the callback to handle promise rejections by adding a catch
block, and ensure the resize operation only occurs if the engine is successfully
retrieved to prevent errors when the engine isn't ready.



useEffect(() => {
return () => {
enginePromise.then(engine => {
engine.destroy();
resizeObserver.unobserve(container);
});
};
}, []);
Comment on lines +48 to +55

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve resource cleanup for better memory management.

The cleanup should use disconnect() instead of unobserve() and handle potential promise rejections during engine destruction.

useEffect(() => {
  return () => {
    enginePromise.then(engine => {
-      engine.destroy();
-      resizeObserver.unobserve(container);
+      if (engine && !engine.isDestroyed) {
+        engine.destroy();
+      }
+    }).catch((error) => {
+      console.warn('Failed to destroy engine:', error);
+    }).finally(() => {
+      resizeObserver.disconnect();
    });
  };
}, []);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
useEffect(() => {
return () => {
enginePromise.then(engine => {
engine.destroy();
resizeObserver.unobserve(container);
});
};
}, []);
useEffect(() => {
return () => {
enginePromise.then(engine => {
if (engine && !engine.isDestroyed) {
engine.destroy();
}
}).catch((error) => {
console.warn('Failed to destroy engine:', error);
}).finally(() => {
resizeObserver.disconnect();
});
};
}, []);
🤖 Prompt for AI Agents
In .storybook/decorators/withCanvas.ts between lines 48 and 55, update the
cleanup function to call resizeObserver.disconnect() instead of unobserve() for
better resource cleanup. Additionally, handle potential promise rejections from
enginePromise by adding a catch block to prevent unhandled promise errors during
engine destruction.


const story = storyFn(context);
if (typeof story === 'string') {
const div = document.createElement('div');
div.innerHTML = story;
container.appendChild(div);
} else if (story instanceof HTMLElement) {
container.appendChild(story);
}

return container;
}
});
39 changes: 39 additions & 0 deletions .storybook/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@


import { join, dirname } from "path";
import { string } from "rollup-plugin-string";

function getAbsolutePath(value) {
return dirname(require.resolve(join(value, 'package.json')))
}
Comment on lines +6 to +8

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling to prevent potential runtime failures.

The getAbsolutePath function should handle cases where require.resolve might fail to find the package.

function getAbsolutePath(value) {
-  return dirname(require.resolve(join(value, 'package.json')))
+  try {
+    return dirname(require.resolve(join(value, 'package.json')))
+  } catch (error) {
+    throw new Error(`Failed to resolve package path for ${value}: ${error.message}`)
+  }
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
function getAbsolutePath(value) {
return dirname(require.resolve(join(value, 'package.json')))
}
function getAbsolutePath(value) {
try {
return dirname(require.resolve(join(value, 'package.json')))
} catch (error) {
throw new Error(`Failed to resolve package path for ${value}: ${error.message}`)
}
}
🤖 Prompt for AI Agents
In .storybook/main.ts around lines 6 to 8, the getAbsolutePath function
currently calls require.resolve without handling errors, which can cause runtime
failures if the package is not found. Wrap the require.resolve call in a
try-catch block to catch any errors, and handle them gracefully by either
returning a default value, null, or throwing a custom error with a clear
message. This will prevent the function from crashing the application when the
package is missing.


/** @type { import('@storybook/html-vite').StorybookConfig } */
const config = {
"stories": [
"../stories/**/*.mdx",
"../stories/**/*.stories.@(js|jsx|mjs|ts|tsx)"
],
"addons": [
getAbsolutePath("@storybook/addon-links"),
getAbsolutePath("@storybook/addon-essentials")
],
"framework": {
"name": getAbsolutePath('@storybook/html-vite'),
"options": {}
},
"core": {
"disableTelemetry": true
},
"viteFinal": async (config) => {
config.plugins = config.plugins || [];
config.plugins.push(
string({
include: ['**/*.glsl', '**/*.shader', '**/*.vs.glsl', '**/*.fs.glsl']
})
);

return config;
}
};

export default config;
23 changes: 23 additions & 0 deletions .storybook/preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { withCanvas } from './decorators/withCanvas';

/** @type { import('@storybook/html-vite').Preview } */
const preview = {
parameters: {
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/i,
},
},
// Default canvas options
canvas: {
engineOptions: {
// Default engine options here
}
},
layout: 'fullscreen',
},
decorators: [withCanvas]
};

export default preview;
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,22 @@
"build": "rollup -c",
"b:all": "pnpm run b:types && cross-env BUILD_TYPE=ALL rollup -c",
"clean": "pnpm -r exec rm -rf dist && pnpm -r exec rm -rf types",
"release": "bumpp -r"
"release": "bumpp -r",
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build"
},
"publishConfig": {},
"devDependencies": {
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@galacean/engine": "^1.5.0",
"@rollup/plugin-commonjs": "^25.0.0",
"@rollup/plugin-inject": "^5.0.3",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-replace": "^5.0.2",
"@storybook/addon-essentials": "9.0.0-alpha.12",
"@storybook/addon-links": "^9.0.11",
"@storybook/html-vite": "^9.0.11",
"@swc/core": "^1.3.49",
"@swc/helpers": "^0.5.0",
"@types/chai": "^4.3.3",
Expand All @@ -46,6 +53,7 @@
"eslint": "^7.32.0",
"eslint-config-prettier": "^7.2.0",
"eslint-plugin-prettier": "^3.4.1",
"eslint-plugin-storybook": "^9.0.11",
"floss": "^5.0.1",
"husky": "^8.0.3",
"lint-staged": "^10.5.4",
Expand All @@ -58,6 +66,7 @@
"rollup-plugin-modify": "^3.0.0",
"rollup-plugin-string": "^3.0.0",
"rollup-plugin-swc3": "^0.8.0",
"storybook": "^9.0.11",
"ts-node": "^10",
"typescript": "^4.8.3"
},
Expand Down
8 changes: 5 additions & 3 deletions packages/auxiliary-lines/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"version": "1.5.3",
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
"registry": "https://registry.npmjs.org",
"main": "dist/commonjs/browser.js",
"module": "dist/es/index.js"
},
"homepage": "https://oasisengine.cn/",
"repository": {
Expand All @@ -17,8 +19,8 @@
"lint:fix": "tslint --fix --project ./tsconfig.json"
},
"types": "types/index.d.ts",
"main": "dist/commonjs/browser.js",
"module": "dist/es/index.js",
"main": "src/index.ts",
"module": "src/index.ts",
"files": [
"dist/**/*",
"types/**/*"
Expand Down
8 changes: 5 additions & 3 deletions packages/controls/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"license": "MIT",
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
"registry": "https://registry.npmjs.org",
"main": "dist/commonjs/browser.js",
"module": "dist/es/index.js"
},
"homepage": "https://oasisengine.cn/",
"repository": {
Expand All @@ -17,8 +19,8 @@
"lint:fix": "tslint --fix --project ./tsconfig.json"
},
"types": "types/index.d.ts",
"main": "dist/commonjs/browser.js",
"module": "dist/es/index.js",
"main": "src/index.ts",
"module": "src/index.ts",
"files": [
"dist/**/*",
"types/**/*"
Expand Down
8 changes: 5 additions & 3 deletions packages/custom-gltf-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
"registry": "https://registry.npmjs.org",
"main": "dist/commonjs/browser.js",
"module": "dist/es/index.js"
},
"homepage": "https://oasisengine.cn/",
"repository": {
Expand All @@ -16,8 +18,8 @@
},
"bugs": "https://github.com/galacean/engine-toolkit/issues",
"types": "types/index.d.ts",
"module": "dist/es/index.js",
"main": "dist/commonjs/browser.js",
"module": "src/index.ts",
"main": "src/index.ts",
"files": [
"dist/**/*",
"types/**/*"
Expand Down
8 changes: 5 additions & 3 deletions packages/custom-material/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
"registry": "https://registry.npmjs.org",
"main": "dist/commonjs/browser.js",
"module": "dist/es/index.js"
},
"homepage": "https://oasisengine.cn/",
"repository": {
Expand All @@ -17,8 +19,8 @@
},
"bugs": "https://github.com/galacean/engine-toolkit/issues",
"types": "types/index.d.ts",
"main": "dist/commonjs/browser.js",
"module": "dist/es/index.js",
"main": "src/index.ts",
"module": "src/index.ts",
"files": [
"dist/**/*",
"types/**/*"
Expand Down
8 changes: 5 additions & 3 deletions packages/draco/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"version": "1.5.3",
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
"registry": "https://registry.npmjs.org",
"main": "dist/main.js",
"module": "dist/es/index.js"
},
"repository": {
"url": "https://github.com/galacean/engine-toolkit.git"
Expand All @@ -13,8 +15,8 @@
"b:types": "tsc"
},
"types": "types/index.d.ts",
"main": "dist/main.js",
"module": "dist/es/index.js",
"main": "src/index.ts",
"module": "src/index.ts",
"files": [
"dist/**/*",
"types/**/*"
Expand Down
8 changes: 5 additions & 3 deletions packages/dynamic-bone/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"version": "1.5.3",
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
"registry": "https://registry.npmjs.org",
"main": "dist/commonjs/browser.js",
"module": "dist/es/index.js"
},
"homepage": "https://oasisengine.cn/",
"repository": {
Expand All @@ -17,8 +19,8 @@
"lint:fix": "tslint --fix --project ./tsconfig.json"
},
"types": "types/index.d.ts",
"main": "dist/commonjs/browser.js",
"module": "dist/es/index.js",
"main": "src/index.ts",
"module": "src/index.ts",
"files": [
"dist/**/*",
"types/**/*"
Expand Down
8 changes: 5 additions & 3 deletions packages/framebuffer-picker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
"name": "@galacean/engine-toolkit-framebuffer-picker",
"version": "1.5.3",
"license": "MIT",
"module": "dist/es/index.js",
"main": "dist/commonjs/browser.js",
"module": "src/index.ts",
"main": "src/index.ts",
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
"registry": "https://registry.npmjs.org",
"main": "dist/commonjs/browser.js",
"module": "dist/es/index.js"
},
"homepage": "https://oasisengine.cn/",
"repository": {
Expand Down
8 changes: 5 additions & 3 deletions packages/galacean-engine-toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
"registry": "https://registry.npmjs.org",
"main": "dist/umd/browser.js",
"module": "dist/es/index.js"
},
"homepage": "https://oasisengine.cn/",
"repository": {
Expand All @@ -16,8 +18,8 @@
},
"bugs": "https://github.com/galacean/engine-toolkit/issues",
"types": "types/index.d.ts",
"module": "dist/es/index.js",
"main": "dist/umd/browser.js",
"module": "src/index.ts",
"main": "src/index.ts",
"umd": {
"name": "Galacean.Toolkit",
"globals": {
Expand Down
8 changes: 5 additions & 3 deletions packages/geometry-sketch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"version": "1.5.3",
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
"registry": "https://registry.npmjs.org",
"main": "dist/commonjs/browser.js",
"module": "dist/es/index.js"
},
"homepage": "https://oasisengine.cn/",
"repository": {
Expand All @@ -17,8 +19,8 @@
"lint:fix": "tslint --fix --project ./tsconfig.json"
},
"types": "types/index.d.ts",
"main": "dist/commonjs/browser.js",
"module": "dist/es/index.js",
"main": "src/index.ts",
"module": "src/index.ts",
"files": [
"dist/**/*",
"types/**/*"
Expand Down
Loading