From 0cccf7aeccdc7a922ff92d8a149ab05db8d27ffe Mon Sep 17 00:00:00 2001 From: thepaperpilot Date: Wed, 11 Oct 2023 21:39:01 -0500 Subject: [PATCH 1/2] Add isRendered utility --- src/data/common.tsx | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/data/common.tsx b/src/data/common.tsx index c81fa0d..e5b1fce 100644 --- a/src/data/common.tsx +++ b/src/data/common.tsx @@ -9,6 +9,7 @@ import { Resource, displayResource } from "features/resources/resource"; import type { GenericTree, GenericTreeNode, TreeNode, TreeNodeOptions } from "features/trees/tree"; import { createTreeNode } from "features/trees/tree"; import type { GenericFormula } from "game/formulas/types"; +import { BaseLayer } from "game/layers"; import type { Modifier } from "game/modifiers"; import type { Persistent } from "game/persistence"; import { DefaultValue, persistent } from "game/persistence"; @@ -485,3 +486,16 @@ export function createFormulaPreview( return <>{formatSmall(formula.evaluate())}; }); } + +/** + * Utility function for getting a computed boolean for whether or not a given feature is currently rendered in the DOM. + * Note it will have a true value even if the feature is off screen. + * @param layer The layer the feature appears within + * @param id The ID of the feature + */ +export function isRendered(layer: BaseLayer, id: string): ComputedRef; +export function isRendered(layer: BaseLayer, feature: { id: string }): ComputedRef; +export function isRendered(layer: BaseLayer, idOrFeature: string | { id: string }) { + const id = typeof idOrFeature === "string" ? idOrFeature : idOrFeature.id; + return computed(() => id in layer.nodes.value); +} From a5204106aa09a15709828e234ad2cd49c8803fa7 Mon Sep 17 00:00:00 2001 From: thepaperpilot Date: Wed, 11 Oct 2023 21:44:02 -0500 Subject: [PATCH 2/2] Forgot to comment the other signature --- src/data/common.tsx | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/data/common.tsx b/src/data/common.tsx index e5b1fce..4be7004 100644 --- a/src/data/common.tsx +++ b/src/data/common.tsx @@ -494,6 +494,12 @@ export function createFormulaPreview( * @param id The ID of the feature */ export function isRendered(layer: BaseLayer, id: string): ComputedRef; +/** + * Utility function for getting a computed boolean for whether or not a given feature is currently rendered in the DOM. + * Note it will have a true value even if the feature is off screen. + * @param layer The layer the feature appears within + * @param feature The feature that may be rendered + */ export function isRendered(layer: BaseLayer, feature: { id: string }): ComputedRef; export function isRendered(layer: BaseLayer, idOrFeature: string | { id: string }) { const id = typeof idOrFeature === "string" ? idOrFeature : idOrFeature.id;