Skip to content

Commit

Permalink
feat: fix and use interpretPath
Browse files Browse the repository at this point in the history
  • Loading branch information
Gugustinette committed Dec 17, 2024
1 parent d7ec47f commit a7bc998
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 41 deletions.
19 changes: 2 additions & 17 deletions packages/2d/src/sprite/FSprite.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as PIXI from 'pixi.js'
import { FAssetUtil } from '@fibbojs/util'
import type { FComponentOptions } from '../core/FComponent'
import { FComponent } from '../core/FComponent'
import type { FScene } from '../core/FScene'
Expand Down Expand Up @@ -49,24 +50,8 @@ export class FSprite extends FComponent {
* @param texture The path to the texture.
*/
async loadTexture(texture: string) {
// Interpret path function
function interpretPath(path: string) {
// Resource URL (if it starts http, treat as a URL)
if (path.startsWith('http')) {
return path
}
// Absolute path (if it starts with /), add the current domain + path
else if (path.startsWith('/')) {
return `${window.location.href}${path}`
}
// Otherwise, treat as a relative path starting to the assets folder
else {
return `${window.location.href}/assets/${path}`
}
}

// Interpret the path
const path = interpretPath(texture)
const path = FAssetUtil.interpretPath(texture)
// Load the texture
this.__TEXTURE__ = await PIXI.Assets.load(path)
this.__TEXTURE__.source.scaleMode = 'nearest'
Expand Down
21 changes: 3 additions & 18 deletions packages/3d/src/model/FModel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as THREE from 'three'
import { FAssetUtil } from '@fibbojs/util'
import type { FScene } from '../core/FScene'
import { FComponent } from '../core/FComponent'
import type { FComponentOptions } from '../core/FComponent'
Expand Down Expand Up @@ -71,28 +72,12 @@ export abstract class FModel extends FComponent {
this.textures = options.textures
this.fileExtension = options.fileExtension

// Interpret path function
function interpretPath(path: string) {
// Resource URL (if it starts http, treat as a URL)
if (path.startsWith('http')) {
return path
}
// Absolute path (if it starts with /), add the current domain + path
else if (path.startsWith('/')) {
return `${window.location.href}${path}`
}
// Otherwise, treat as a relative path starting to the assets folder
else {
return `${window.location.href}/assets/${path}`
}
}

// Interpret the path
this.path = interpretPath(this.path)
this.path = FAssetUtil.interpretPath(this.path)
// Interpret the textures
this.textures = Object.fromEntries(
Object.entries(this.textures).map(([materialName, texturePath]) => {
return [materialName, interpretPath(texturePath)]
return [materialName, FAssetUtil.interpretPath(texturePath)]
}),
)
}
Expand Down
8 changes: 4 additions & 4 deletions packages/util/src/FAssetUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ export class FAssetUtil {
* @returns The interpreted path.
*/
static interpretPath(path: string) {
// Resource URL (if it starts http, treat as a URL)
// Resource URL (if it starts with http, treat as a URL)
if (path.startsWith('http')) {
return path
}
// Absolute path (if it starts with /), add the current domain + path
// Absolute path (if it starts with /), add the current origin + path
else if (path.startsWith('/')) {
return `${window.location.origin}${path}`
return `${window.location.href}${path}`
}
// Otherwise, treat as a relative path starting to the assets folder
else {
return `${window.location.origin}/${FAssetUtil.__ASSETS_PATH__}/${path}`
return `${window.location.href}/${FAssetUtil.__ASSETS_PATH__}/${path}`
}
}
}
5 changes: 3 additions & 2 deletions packages/util/test/browser/asset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { FAssetUtil } from '../../src'
describe('@fibbojs/util/FAssetUtil should', () => {
it('interprete a given path', () => {
expect(FAssetUtil.interpretPath('http://localhost:5173/custom/my-asset.png')).toEqual('http://localhost:5173/custom/my-asset.png')
expect(FAssetUtil.interpretPath('/my-asset.png')).toEqual('http://localhost:5173/my-asset.png')
expect(FAssetUtil.interpretPath('my-asset.png')).toEqual('http://localhost:5173/assets/my-asset.png')
// for href based tests, the url isn't predictable as vitest will create a random id for the test, so we use regex to match the url
expect(FAssetUtil.interpretPath('/my-asset.png')).toMatch(/http:\/\/localhost:5173\/.*\/my-asset.png/)
expect(FAssetUtil.interpretPath('my-asset.png')).toMatch(/http:\/\/localhost:5173\/.*\/assets\/my-asset.png/)
})
})

0 comments on commit a7bc998

Please sign in to comment.