Skip to content

Commit

Permalink
chandle links
Browse files Browse the repository at this point in the history
  • Loading branch information
blukai committed Sep 5, 2024
1 parent 4d0421e commit 83b8ee3
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 24 deletions.
68 changes: 62 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.0.0"
edition = "2021"

[dependencies]
haste = { git = "https://github.com/blukai/haste.git", rev = "0fa015ddf428468abfda843fced68557533f5694", package = "haste", features = ["preserve-metadata"] }
haste = { git = "https://github.com/blukai/haste.git", rev = "fbda4a3ec4727687e7097a69bebda9740469fa5a", package = "haste", features = ["preserve-metadata"] }
wasm-bindgen = "0.2"

[lib]
Expand Down
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
"indentStyle": "space"
},
"files": {
"ignore": ["src/lib/haste"]
"ignore": ["src/lib/haste", "target"]
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"build": "tsc && vite build",
"lint": "bunx @biomejs/biome lint . --apply",
"format": "bunx @biomejs/biome format . --write",
"build-haste": "wasm-pack build --target web --out-dir src/lib/haste --out-name haste -- --verbose && rm src/lib/haste/package.json && rm src/lib/haste/.gitignore"
"build:haste": "wasm-pack build --target web --out-dir src/lib/haste --out-name haste -- --verbose && rm src/lib/haste/package.json && rm src/lib/haste/.gitignore"
},
"dependencies": {
"@fontsource/jetbrains-mono": "^5.0.18",
Expand Down
53 changes: 44 additions & 9 deletions src/DemEntities.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as DropdownMenuPrimitive from "@radix-ui/react-dropdown-menu";
import { useVirtualizer } from "@tanstack/react-virtual";
import { useAtom } from "jotai";
import { CheckIcon, CogIcon } from "lucide-react";
import { CheckIcon, CogIcon, Link2Icon, Link2OffIcon } from "lucide-react";
import { useCallback, useMemo, useRef, useState, useTransition } from "react";
import { Panel, PanelGroup, PanelResizeHandle } from "react-resizable-panels";
import DemFilterBar, { type UpdateEventHandler } from "./DemFilterBar";
Expand All @@ -13,7 +13,7 @@ import {
import { Button } from "./lib/Button";
import { ScrollArea } from "./lib/ScrollArea";
import { Tooltip } from "./lib/Tooltip";
import { type EntityLi } from "./lib/haste";
import { type EntityLi, handleToIndex, isHandleValid } from "./lib/haste";
import { cn } from "./lib/style";

const LI_HEIGHT = 26;
Expand Down Expand Up @@ -56,7 +56,7 @@ function EntityList() {
);
const handleClick = useCallback(
(ev: React.MouseEvent<HTMLLIElement>) => {
const entityIndex = +ev.currentTarget.dataset.ei!;
const entityIndex = +ev.currentTarget.dataset.entidx!;
if (entityIndex >= 0 && entityIndex <= Number.MAX_SAFE_INTEGER) {
setDemSelectedEntityIndex((prevEntityIndex) =>
prevEntityIndex === entityIndex ? undefined : entityIndex,
Expand Down Expand Up @@ -89,14 +89,14 @@ function EntityList() {
<li
key={virtualItem.key}
className={cn(
"absolute top-0 left-0 w-full px-2 flex items-center cursor-pointer text-fg-subtle hover:bg-neutral-500/30",
entitySelected && "bg-neutral-500/30 text-fg",
"absolute top-0 left-0 w-full px-2 flex items-center cursor-pointer text-fg-subtle hover:bg-neutral-500/40",
entitySelected && "bg-neutral-500/60 text-fg",
)}
style={{
height: `${virtualItem.size}px`,
transform: `translateY(${virtualItem.start}px)`,
}}
data-ei={entityItem?.index}
data-entidx={entityItem?.index}
onClick={handleClick}
>
<span className="text-ellipsis overflow-hidden whitespace-nowrap">
Expand Down Expand Up @@ -146,7 +146,7 @@ function EntityFieldListPreferences(props: EntityFieldListPreferencesProps) {
<DropdownMenuPrimitive.CheckboxItem
checked={showTypeInfo}
onCheckedChange={setShowTypeInfo}
className="flex items-center hover:bg-neutral-500/30 rounded px-2 py-0.5 gap-x-2 cursor-pointer"
className="flex items-center hover:bg-neutral-500/40 rounded px-2 py-0.5 gap-x-2 cursor-pointer"
>
<DropdownMenuPrimitive.ItemIndicator>
<CheckIcon className="size-4" />
Expand Down Expand Up @@ -223,6 +223,19 @@ function EntityFieldList() {

const [showTypeInfo, setShowTypeInfo] = useState(DEFAULT_SHOW_TYPE_INFO);

const [, setDemSelectedEntityIndex] = useAtom(demSelectedEntityIndexAtom);
const handleClick = useCallback(
(ev: React.MouseEvent<HTMLLIElement>) => {
const entityIndex = +ev.currentTarget.dataset.entidx!;
if (entityIndex >= 0 && entityIndex <= Number.MAX_SAFE_INTEGER) {
setDemSelectedEntityIndex((prevEntityIndex) =>
prevEntityIndex === entityIndex ? undefined : entityIndex,
);
}
},
[setDemSelectedEntityIndex],
);

return (
<div className="w-full h-full flex flex-col">
<DemFilterBar
Expand Down Expand Up @@ -258,18 +271,30 @@ function EntityFieldList() {
>
{virtualizer.getVirtualItems().map((virtualItem) => {
const entityFieldItem = filteredEntityFieldList![virtualItem.index];

const handle =
entityFieldItem.inner.encodedAs.startsWith("CHandle");
const handleValid =
handle && isHandleValid(+entityFieldItem.inner.value);
const linkedEntIdx = handleValid
? handleToIndex(+entityFieldItem.inner.value)
: null;

return (
<li
key={virtualItem.key}
className={cn(
"absolute top-0 left-0 w-full px-2 flex items-center cursor-pointer hover:bg-neutral-500/30",
"absolute top-0 left-0 w-full px-2 flex items-center hover:bg-neutral-500/20",
handleValid && "cursor-pointer hover:bg-neutral-500/40",
)}
style={{
height: `${virtualItem.size}px`,
transform: `translateY(${virtualItem.start}px)`,
}}
data-entidx={linkedEntIdx}
onClick={handleClick}
>
<span className="whitespace-nowrap gap-x-[1ch] flex">
<span className="whitespace-nowrap gap-x-[1ch] flex items-center">
<span className="text-fg-subtle">
{entityFieldItem.joinedNamedPath}
</span>
Expand All @@ -286,6 +311,16 @@ function EntityFieldList() {
</>
)}
<span>{entityFieldItem.inner.value}</span>
{handle &&
(handleValid ? (
<Tooltip content="click to navigate to the linked entity">
<Link2Icon className="size-4" />
</Tooltip>
) : (
<Tooltip content="this handle is invalid">
<Link2OffIcon className="size-4 text-fg-subtle" />
</Tooltip>
))}
</span>
</li>
);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React from "react";
import { cn } from "./style";

const variants = cva(
"hover:bg-neutral-500/30 rounded inline-flex items-center justify-center disabled:pointer-events-none disabled:opacity-50",
"hover:bg-neutral-500/40 rounded inline-flex items-center justify-center disabled:pointer-events-none disabled:opacity-50",
{
variants: { size: { default: "h-8 p-2", small: "h-6 p-1" } },
defaultVariants: { size: "default" },
Expand Down
12 changes: 12 additions & 0 deletions src/lib/haste/haste.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
/* tslint:disable */
/* eslint-disable */
/**
* @param {number} handle
* @returns {boolean}
*/
export function isHandleValid(handle: number): boolean;
/**
* @param {number} handle
* @returns {number}
*/
export function handleToIndex(handle: number): number;
/**
*/
export class EntityFieldLi {
free(): void;
Expand Down Expand Up @@ -88,6 +98,8 @@ export interface InitOutput {
readonly wrappedparser_runToTick: (a: number, b: number, c: number) => void;
readonly wrappedparser_listEntities: (a: number, b: number) => void;
readonly wrappedparser_listEntityFields: (a: number, b: number, c: number) => void;
readonly isHandleValid: (a: number) => number;
readonly handleToIndex: (a: number) => number;
readonly __wbg_set_entityli_name: (a: number, b: number, c: number) => void;
readonly __wbindgen_malloc: (a: number, b: number) => number;
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
Expand Down
25 changes: 21 additions & 4 deletions src/lib/haste/haste.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,23 @@ function passArrayJsValueToWasm0(array, malloc) {
WASM_VECTOR_LEN = array.length;
return ptr;
}
/**
* @param {number} handle
* @returns {boolean}
*/
export function isHandleValid(handle) {
const ret = wasm.isHandleValid(handle);
return ret !== 0;
}

/**
* @param {number} handle
* @returns {number}
*/
export function handleToIndex(handle) {
const ret = wasm.handleToIndex(handle);
return ret >>> 0;
}

const EntityFieldLiFinalization = (typeof FinalizationRegistry === 'undefined')
? { register: () => {}, unregister: () => {} }
Expand Down Expand Up @@ -533,14 +550,14 @@ function __wbg_get_imports() {
const ret = new Error(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
};
imports.wbg.__wbg_entityfieldli_new = function(arg0) {
const ret = EntityFieldLi.__wrap(arg0);
return addHeapObject(ret);
};
imports.wbg.__wbg_entityli_new = function(arg0) {
const ret = EntityLi.__wrap(arg0);
return addHeapObject(ret);
};
imports.wbg.__wbg_entityfieldli_new = function(arg0) {
const ret = EntityFieldLi.__wrap(arg0);
return addHeapObject(ret);
};
imports.wbg.__wbindgen_string_new = function(arg0, arg1) {
const ret = getStringFromWasm0(arg0, arg1);
return addHeapObject(ret);
Expand Down
13 changes: 12 additions & 1 deletion src/lib/haste/haste.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// - https://stackoverflow.com/questions/65332927/is-it-possible-to-wasm-bindgen-public-structs-and-functions-defined-in-anothe

use haste::{
entities,
fieldpath::FieldPath,
fieldvalue::FieldValue,
flattenedserializers::FlattenedSerializer,
Expand Down Expand Up @@ -119,7 +120,7 @@ fn get_value_info(serializer: &FlattenedSerializer, fp: &FieldPath) -> (Vec<Stri
result.push(field.var_name.str.to_string());

for field_index in fp.iter().skip(1) {
if field.is_vector() {
if field.is_dynamic_array() {
field = field.get_child(0).unwrap_throw();
result.push(field_index.to_string());
} else {
Expand Down Expand Up @@ -157,3 +158,13 @@ fn get_field_value_discriminant_name(field_value: &FieldValue) -> &'static str {
FieldValue::String(_) => "String",
}
}

#[wasm_bindgen(js_name = "isHandleValid")]
pub fn is_handle_valid(handle: u32) -> bool {
entities::is_handle_valid(handle)
}

#[wasm_bindgen(js_name = "handleToIndex")]
pub fn handle_to_index(handle: u32) -> usize {
entities::handle_to_index(handle)
}
Binary file modified src/lib/haste/haste_bg.wasm
Binary file not shown.
2 changes: 2 additions & 0 deletions src/lib/haste/haste_bg.wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export function wrappedparser_totalTicks(a: number, b: number): void;
export function wrappedparser_runToTick(a: number, b: number, c: number): void;
export function wrappedparser_listEntities(a: number, b: number): void;
export function wrappedparser_listEntityFields(a: number, b: number, c: number): void;
export function isHandleValid(a: number): number;
export function handleToIndex(a: number): number;
export function __wbg_set_entityli_name(a: number, b: number, c: number): void;
export function __wbindgen_malloc(a: number, b: number): number;
export function __wbindgen_realloc(a: number, b: number, c: number, d: number): number;
Expand Down

0 comments on commit 83b8ee3

Please sign in to comment.