diff --git a/src/def.d.ts b/src/def.d.ts index 45409b1..3c39e00 100644 --- a/src/def.d.ts +++ b/src/def.d.ts @@ -18,6 +18,13 @@ interface Logger { verbose: LoggerFunction, } +type SearchFilter = (tree: any) => boolean; +interface FindInTreeOptions { + walkable?: string[]; + ignore?: string[]; + maxDepth?: number; +} + interface VendettaObject { patcher: { after: typeof _spitroast.after; diff --git a/src/lib/utils/copyText.ts b/src/lib/utils/copyText.ts new file mode 100644 index 0000000..e13354c --- /dev/null +++ b/src/lib/utils/copyText.ts @@ -0,0 +1,11 @@ +import { findByProps } from "@metro/filters"; + +const clipboard = findByProps("setString", "getString"); + +export default function copyText(content: string) { + try { + clipboard.setString(content); + } catch (e) { + throw new Error("Failed to set clipboard content.") + } +} \ No newline at end of file diff --git a/src/lib/utils/findInReactTree.ts b/src/lib/utils/findInReactTree.ts new file mode 100644 index 0000000..904b238 --- /dev/null +++ b/src/lib/utils/findInReactTree.ts @@ -0,0 +1,8 @@ +import { SearchFilter } from "@types"; +import findInTree from "@utils/findInTree"; + +export default function findInReactTree(tree: { [key: string]: any }, filter: SearchFilter): any { + return findInTree(tree, filter, { + walkable: ["props", "children", "child", "sibling"], + }); +} \ No newline at end of file diff --git a/src/lib/utils/findInTree.ts b/src/lib/utils/findInTree.ts new file mode 100644 index 0000000..1e45959 --- /dev/null +++ b/src/lib/utils/findInTree.ts @@ -0,0 +1,40 @@ +/* + Disclaimer: https://github.com/Cordwood/Cordwood/blob/91c0b971bbf05e112927df75415df99fa105e1e7/src/lib/utils/findInTree.ts + */ + +import { FindInTreeOptions, SearchFilter } from "@types"; + +export default function findInTree(tree: { [key: string]: any }, filter: SearchFilter, { walkable = [], ignore = [], maxDepth = 100 }: FindInTreeOptions = {}): any { + let iteration = 0; + + function doSearch(tree: { [key: string]: any }, filter: SearchFilter, { walkable = [], ignore = [] }: FindInTreeOptions = {}): any { + iteration += 1; + if (iteration > maxDepth) return; + + if (typeof filter === "string") { + if (tree.hasOwnProperty(filter)) return tree[filter]; + } else if (filter(tree)) return tree; + + if (!tree) return; + + if (Array.isArray(tree)) { + for (const item of tree) { + const found = doSearch(item, filter, { walkable, ignore }); + if (found) return found; + } + } else if (typeof tree === "object") { + for (const key of Object.keys(tree)) { + if (walkable != null && walkable.includes(key)) continue; + + if (ignore.includes(key)) continue; + + try { + const found = doSearch(tree[key], filter, { walkable, ignore }); + if (found) return found; + } catch {} + } + } + } + + return doSearch(tree, filter, { walkable, ignore }); +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 2b5f08a..339e481 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -16,7 +16,8 @@ "@/*": ["src/*"], "@types": ["src/def.d.ts"], "@lib/*": ["src/lib/*"], - "@metro/*": ["src/lib/metro/*"] + "@metro/*": ["src/lib/metro/*"], + "@utils/*": ["src/lib/utils/*"] } } }