From 82593f244a523e9ba3813f1e6e5a0a4245453564 Mon Sep 17 00:00:00 2001 From: blukai <20866892+blukai@users.noreply.github.com> Date: Sat, 14 Sep 2024 00:39:12 +0200 Subject: [PATCH] add settings to enable entity indexes and field paths --- src/DemEntities.tsx | 222 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 185 insertions(+), 37 deletions(-) diff --git a/src/DemEntities.tsx b/src/DemEntities.tsx index 3d49aa3..3d9dc76 100644 --- a/src/DemEntities.tsx +++ b/src/DemEntities.tsx @@ -17,7 +17,83 @@ import { type EntityLi, handleToIndex, isHandleValid } from "./lib/haste"; import { cn } from "./lib/style"; const LI_HEIGHT = 26; -const DEFAULT_SHOW_TYPE_INFO = true; + +const DEFAULT_SHOW_ENTITY_INDEX = false; + +const DEFAULT_SHOW_FIELD_ENCODED_TYPE = true; +const DEFAULT_SHOW_FIELD_DECODED_TYPE = false; +const DEFAULT_SHOW_FIELD_PATH = false; + +type DropdownMenuCheckboxItemProps = Pick< + DropdownMenuPrimitive.DropdownMenuCheckboxItemProps, + "checked" | "onCheckedChange" +> & { + label: React.ReactNode; +}; + +function DropdownMenuCheckboxItem(props: DropdownMenuCheckboxItemProps) { + const { checked, onCheckedChange, label } = props; + + return ( + + + + + {label} + + ); +} + +type EntityListPreferencesProps = { + showEntityIndex: boolean; + setShowEntityIndex: (value: boolean) => void; +}; + +// NOTE: keep this in sync with EntityFieldListPreferences +function EntityListPreferences(props: EntityListPreferencesProps) { + const { + showEntityIndex, + setShowEntityIndex, + } = props; + + const [open, setOpen] = useState(false); + + const active = open; + + return ( + + + + + + + + + + + + + + + ); +} function EntityList() { const [demParser] = useAtom(demParserAtom); @@ -66,12 +142,23 @@ function EntityList() { [setDemSelectedEntityIndex], ); + const [showEntityIndex, setShowEntityIndex] = useState(DEFAULT_SHOW_ENTITY_INDEX); + return (
+
+ + + } className="border-b border-divider" /> {!entityList?.length && ( @@ -99,8 +186,16 @@ function EntityList() { data-entidx={entityItem?.index} onClick={handleClick} > + {showEntityIndex && ( + + {entityItem.index} + + )} - {entityItem?.name} + {entityItem.name} ); @@ -112,16 +207,28 @@ function EntityList() { } type EntityFieldListPreferencesProps = { - showTypeInfo: boolean; - setShowTypeInfo: (value: boolean) => void; + showFieldPath: boolean; + setShowFieldPath: (value: boolean) => void; + showFieldEncodedType: boolean; + setShowFieldEncodedType: (value: boolean) => void; + showFieldDecodedType: boolean; + setShowFieldDecodedType: (value: boolean) => void; }; +// NOTE: keep this in sync with EntityListPreferences function EntityFieldListPreferences(props: EntityFieldListPreferencesProps) { - const { showTypeInfo, setShowTypeInfo } = props; + const { + showFieldPath, + setShowFieldPath, + showFieldEncodedType, + setShowFieldEncodedType, + showFieldDecodedType, + setShowFieldDecodedType, + } = props; const [open, setOpen] = useState(false); - const active = open || showTypeInfo; + const active = open; return ( @@ -140,19 +247,24 @@ function EntityFieldListPreferences(props: EntityFieldListPreferencesProps) { - - - - - show type info - + + + @@ -163,21 +275,31 @@ function EntityFieldList() { const [demParser] = useAtom(demParserAtom); const [demTick] = useAtom(demTickAtom); const [demSelectedEntityIndex] = useAtom(demSelectedEntityIndexAtom); - const entityFieldList = useMemo(() => { + + const { entityFieldList, joinedPathMaxLen } = useMemo(() => { demTick; // trick eslint if (demSelectedEntityIndex === undefined) { - return undefined; + return {}; } - const efl = demParser + let joinedPathMaxLen = 0; + + const entityFieldList = demParser ?.listEntityFields(demSelectedEntityIndex) - ?.map((entityField) => ({ - inner: entityField, - joinedNamedPath: entityField.namedPath.join("."), - })); + ?.map((entityField) => { + const joinedPath = Array.from(entityField.path) + .map((part) => part.toString().padStart(4, " ")) + .join(""); + joinedPathMaxLen = Math.max(joinedPathMaxLen, joinedPath.length); + return { + inner: entityField, + joinedPath, + joinedNamedPath: entityField.namedPath.join("."), + }; + }); - efl?.sort((a, b) => { + entityFieldList?.sort((a, b) => { // compare path arrays element by element for ( let i = 0; @@ -193,7 +315,7 @@ function EntityFieldList() { return a.inner.path.length - b.inner.path.length; }); - return efl; + return { entityFieldList, joinedPathMaxLen }; }, [demSelectedEntityIndex, demParser, demTick]); type WrappedEntityFieldLi = NonNullable[0]; @@ -221,7 +343,13 @@ function EntityFieldList() { estimateSize: () => LI_HEIGHT, }); - const [showTypeInfo, setShowTypeInfo] = useState(DEFAULT_SHOW_TYPE_INFO); + const [showFieldEncodedType, setShowFieldEncodedType] = useState( + DEFAULT_SHOW_FIELD_ENCODED_TYPE, + ); + const [showFieldDecodedType, setShowFieldDecodedType] = useState( + DEFAULT_SHOW_FIELD_DECODED_TYPE, + ); + const [showFieldPath, setShowFieldPath] = useState(DEFAULT_SHOW_FIELD_PATH); const [, setDemSelectedEntityIndex] = useAtom(demSelectedEntityIndexAtom); const handleClick = useCallback( @@ -247,8 +375,12 @@ function EntityFieldList() { <>
} @@ -295,19 +427,35 @@ function EntityFieldList() { onClick={handleClick} > + {showFieldPath && ( + + {entityFieldItem.joinedPath} + + )} {entityFieldItem.joinedNamedPath} : - {showTypeInfo && ( + {(showFieldEncodedType || showFieldDecodedType) && ( <> - - {entityFieldItem.inner.encodedAs} - - {"->"} - - {entityFieldItem.inner.decodedAs} - + {showFieldEncodedType && ( + + {entityFieldItem.inner.encodedAs || "_"} + + )} + {showFieldEncodedType && showFieldDecodedType && ( + + {"->"} + + )} + {showFieldDecodedType && ( + + {entityFieldItem.inner.decodedAs} + + )} )} {entityFieldItem.inner.value}