Make the fileBrowser reactive
This commit is contained in:
+16
-10
@@ -1,5 +1,11 @@
|
|||||||
import "./FileBrowser.css";
|
import "./FileBrowser.css";
|
||||||
import { type Component, createSignal, Index, Show } from "solid-js";
|
import {
|
||||||
|
type Accessor,
|
||||||
|
type Component,
|
||||||
|
createSignal,
|
||||||
|
For,
|
||||||
|
Show,
|
||||||
|
} from "solid-js";
|
||||||
import { FileIcon, FolderIcon, FolderOpenIcon } from "#/dev/icons/index.ts";
|
import { FileIcon, FolderIcon, FolderOpenIcon } from "#/dev/icons/index.ts";
|
||||||
|
|
||||||
export type BrowserFolder = Map<string, string | BrowserFolder>;
|
export type BrowserFolder = Map<string, string | BrowserFolder>;
|
||||||
@@ -11,15 +17,15 @@ function isFolder(entry: string | BrowserFolder): entry is BrowserFolder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const FileBrowser: Component<{
|
export const FileBrowser: Component<{
|
||||||
root: BrowserFolder;
|
root: Accessor<BrowserFolder>;
|
||||||
handler: FileHandler;
|
handler: FileHandler;
|
||||||
}> = ({ root, handler }) => {
|
}> = (props) => {
|
||||||
const FileEntry: Component<{
|
const FileEntry: Component<{
|
||||||
name: string;
|
name: string;
|
||||||
path: string;
|
path: string;
|
||||||
}> = ({ name, path }) => {
|
}> = ({ name, path }) => {
|
||||||
return (
|
return (
|
||||||
<div class="file-label" onClick={[handler, path]}>
|
<div class="file-label" onClick={[props.handler, path]}>
|
||||||
<FileIcon />
|
<FileIcon />
|
||||||
<span>{name}</span>
|
<span>{name}</span>
|
||||||
</div>
|
</div>
|
||||||
@@ -47,31 +53,31 @@ export const FileBrowser: Component<{
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const FileList: Component<{ folder: BrowserFolder }> = ({ folder }) => {
|
const FileList: Component<{ folder: BrowserFolder }> = (props) => {
|
||||||
return (
|
return (
|
||||||
<ul class="file-list">
|
<ul class="file-list">
|
||||||
<Index each={[...folder.entries()]}>
|
<For each={[...props.folder.entries()]}>
|
||||||
{(item, index) => {
|
{(item, index) => {
|
||||||
const [name, entry] = item();
|
const [name, entry] = item;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<li class="file-item">
|
<li class="file-item">
|
||||||
{isFolder(entry) ? (
|
{isFolder(entry) ? (
|
||||||
<FolderEntry folder={entry} name={name} open={index < 1} />
|
<FolderEntry folder={entry} name={name} open={index() < 1} />
|
||||||
) : (
|
) : (
|
||||||
<FileEntry name={name} path={entry} />
|
<FileEntry name={name} path={entry} />
|
||||||
)}
|
)}
|
||||||
</li>
|
</li>
|
||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
</Index>
|
</For>
|
||||||
</ul>
|
</ul>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div class="file-browser">
|
<div class="file-browser">
|
||||||
<FileList folder={root} />
|
<FileList folder={props.root()} />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
+13
-7
@@ -36,8 +36,7 @@ function mapSceneEntities(sceneData: SceneData): BrowserFolder {
|
|||||||
return new Map([["entities", entites]]);
|
return new Map([["entities", entites]]);
|
||||||
}
|
}
|
||||||
|
|
||||||
const scenes = await fetchBrowserData("scenes");
|
const scenesRaw = await fetchBrowserData("scenes");
|
||||||
const entites = mapSceneEntities(currentScene.data);
|
|
||||||
|
|
||||||
let activeEntity = null;
|
let activeEntity = null;
|
||||||
function setActiveEntity(entity: string): void {
|
function setActiveEntity(entity: string): void {
|
||||||
@@ -47,10 +46,6 @@ function setActiveEntity(entity: string): void {
|
|||||||
console.log(activeEntity);
|
console.log(activeEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setCurrentScene(name: string): void {
|
|
||||||
loadScene(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
function adjustGameContainer(gameContainer: HTMLElement, width: number): void {
|
function adjustGameContainer(gameContainer: HTMLElement, width: number): void {
|
||||||
gameContainer.style = `position: relative; top: 0; left: ${width}px; width: ${document.body.offsetWidth - width}px;`;
|
gameContainer.style = `position: relative; top: 0; left: ${width}px; width: ${document.body.offsetWidth - width}px;`;
|
||||||
}
|
}
|
||||||
@@ -78,6 +73,17 @@ const DevTools: Component<{ gameContainer: HTMLElement }> = ({
|
|||||||
|
|
||||||
adjustGameContainer(gameContainer, width());
|
adjustGameContainer(gameContainer, width());
|
||||||
|
|
||||||
|
const [entities, setEntities] = createSignal(
|
||||||
|
mapSceneEntities(currentScene.data),
|
||||||
|
);
|
||||||
|
|
||||||
|
const [scenes] = createSignal(scenesRaw);
|
||||||
|
|
||||||
|
const setCurrentScene = async (name: string) => {
|
||||||
|
await loadScene(name);
|
||||||
|
setEntities(mapSceneEntities(currentScene.data));
|
||||||
|
};
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
self.addEventListener("resize", () => {
|
self.addEventListener("resize", () => {
|
||||||
adjustGameContainer(gameContainer, width());
|
adjustGameContainer(gameContainer, width());
|
||||||
@@ -131,7 +137,7 @@ const DevTools: Component<{ gameContainer: HTMLElement }> = ({
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<FileBrowser root={scenes} handler={setCurrentScene} />
|
<FileBrowser root={scenes} handler={setCurrentScene} />
|
||||||
<FileBrowser root={entites} handler={setActiveEntity} />
|
<FileBrowser root={entities} handler={setActiveEntity} />
|
||||||
</div>
|
</div>
|
||||||
<div class="tool-bar-resize" onMouseDown={startResizing}></div>
|
<div class="tool-bar-resize" onMouseDown={startResizing}></div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user