mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-15 07:24:52 +09:00
These variables were intended to be globals declare in the global namespace. That this ends up on the window object is an implementation detail of JavaScript. Remove the window. prefix put in front of staticRoot and webRoot and include the global declaration in the TypeScript module.
53 lines
1.4 KiB
TypeScript
53 lines
1.4 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
|
|
import { useExportManifest } from "./export_manifest";
|
|
|
|
|
|
type Metadata = {
|
|
x: number;
|
|
y: number;
|
|
size: number;
|
|
};
|
|
let itemMetadataCache: Map<string, Metadata> | null = null;
|
|
export function useItemMetadata() {
|
|
let exportManifest = useExportManifest();
|
|
let [itemMetadata, setItemMetadata] = useState<Map<string, Metadata>>(itemMetadataCache || new Map());
|
|
useEffect(() => {
|
|
async function load() {
|
|
if (
|
|
!exportManifest
|
|
|| !exportManifest.assets
|
|
|| !exportManifest.assets["item-metadata"]
|
|
|| !exportManifest.assets["item-spritesheet"]
|
|
) {
|
|
return;
|
|
}
|
|
let response = await fetch(`${staticRoot}static/${exportManifest.assets["item-metadata"]}`);
|
|
if (response.ok) {
|
|
let data = await response.json();
|
|
itemMetadataCache = new Map(data);
|
|
let style = document.createElement("style");
|
|
document.head.appendChild(style);
|
|
for (let [name, meta] of itemMetadataCache) {
|
|
style.sheet!.insertRule(`\
|
|
.item-${CSS.escape(name)} {
|
|
background-image: url("${staticRoot}static/${exportManifest.assets["item-spritesheet"]}");
|
|
background-repeat: no-repeat;
|
|
background-position: -${meta.x}px -${meta.y}px;
|
|
height: ${meta.size}px;
|
|
width: ${meta.size}px;
|
|
}`
|
|
);
|
|
}
|
|
setItemMetadata(itemMetadataCache);
|
|
}
|
|
}
|
|
|
|
if (!itemMetadataCache) {
|
|
load();
|
|
}
|
|
}, [exportManifest]);
|
|
|
|
return itemMetadata;
|
|
}
|