Skip to content

Commit

Permalink
feat: add support of JS files in assets
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-moreau committed Apr 22, 2023
1 parent fdb734a commit f69cc7a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
7 changes: 7 additions & 0 deletions assets/scripts/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { editor } = require("babylonjs-editor");

module.exports = {
main: function() {
editor.console.logInfo("My first editor script!");
},
};
30 changes: 28 additions & 2 deletions src/renderer/editor/components/assets-browser/files.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export class AssetsBrowserFiles extends React.Component<IAssetsBrowserFilesProps
{this.state.items}
</div>
</div>
)
);
}

/**
Expand Down Expand Up @@ -263,7 +263,11 @@ export class AssetsBrowserFiles extends React.Component<IAssetsBrowserFilesProps

<MenuDivider />

<MenuItem text="Scene..." icon={<Icon src="file.svg" />} onClick={() => NewProjectWizard.Show(this.props.editor)} />
<MenuItem text="Scene..." icon={<Icon src="file.svg" />} disabled={!isAssetsDirectory} onClick={() => NewProjectWizard.Show(this.props.editor)} />

<MenuDivider />

<MenuItem text="Script..." disabled={!isAssetsDirectory} icon={<Icon src="javascript.svg" style={{ filter: "none" }} />} onClick={() => this._handleAddScriptAsset()} />

<MenuDivider />

Expand Down Expand Up @@ -726,6 +730,28 @@ export class AssetsBrowserFiles extends React.Component<IAssetsBrowserFilesProps
await this.refresh();
}

/**
* Called on the user wants to add a new script asset (javascript file).
*/
private async _handleAddScriptAsset(): Promise<void> {
let name = await Dialog.Show("Script Name", "Please provide a name for the new JavaScript file.");

const extension = extname(name).toLowerCase();
if (extension !== ".js") {
name += ".js";
}

const dest = join(this.state.currentDirectory, name);
if (await pathExists(dest)) {
return Alert.Show("Can't Create Script", `A script named "${name}" already exists.`);
}

const skeleton = await readFile(join(AppTools.GetAppPath(), "assets/scripts/script.js"), { encoding: "utf-8" });
await writeFile(dest, skeleton);

await this.refresh();
}

/**
* Called on the user wants to add a new TypeScript script.
*/
Expand Down

0 comments on commit f69cc7a

Please sign in to comment.