diff --git a/example/src/App.tsx b/example/src/App.tsx index 7c6311b..4ecc62b 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -4,7 +4,8 @@ import { ASTVisualizer, initSourceFile, updateSourceFile, - clearSourceFile + clearSourceFile, + clearHighlightedNodes } from "ts-transformer-visualizer"; import * as ts from "typescript"; @@ -14,7 +15,7 @@ import { moduleMap, ModuleNames } from "./transforms"; export default function App({ moduleName, - timeDelayMs = 3000 + timeDelayMs = 1000 }: { moduleName: ModuleNames; timeDelayMs?: number; @@ -41,6 +42,7 @@ export default function App({ // 3. Clean timeout and reset visualizer clearTimeout(timeoutID); clearSourceFile(); + clearHighlightedNodes(); }; }); diff --git a/example/src/transforms/log-identifiers.ts b/example/src/transforms/log-identifiers.ts index 2ac77f3..c06b3c2 100644 --- a/example/src/transforms/log-identifiers.ts +++ b/example/src/transforms/log-identifiers.ts @@ -1,4 +1,6 @@ import * as ts from "typescript"; +import { highlightNode } from "ts-transformer-visualizer"; + import { TransformModuleExport } from "../types"; const sourceCodeString = `\ @@ -15,6 +17,7 @@ const transformer: ts.TransformerFactory = context => { return sourceFile => { const visitor = (node: ts.Node): ts.Node => { if (ts.isIdentifier(node)) { + highlightNode(node); console.log(node.text); } return ts.visitEachChild(node, visitor, context); diff --git a/lib/src/components/ASTOutline/ASTOutline.css b/lib/src/components/ASTOutline/ASTOutline.css index b67085a..589b07e 100644 --- a/lib/src/components/ASTOutline/ASTOutline.css +++ b/lib/src/components/ASTOutline/ASTOutline.css @@ -23,3 +23,9 @@ } .exitDone { } +.node { + padding: 0 0.5em; +} +.highlightNode { + background-color: rgba(254, 246, 2, 0.5); +} diff --git a/lib/src/components/ASTOutline/ASTOutline.tsx b/lib/src/components/ASTOutline/ASTOutline.tsx index e4cf276..0aeabcc 100644 --- a/lib/src/components/ASTOutline/ASTOutline.tsx +++ b/lib/src/components/ASTOutline/ASTOutline.tsx @@ -1,20 +1,29 @@ import React from "react"; -import { connect } from "react-redux"; import * as ts from "typescript"; import { CSSTransition, TransitionGroup } from "react-transition-group"; -import { AppState } from "../../store"; +import { useTypedSelector } from "../../store"; import { forEachChild, syntaxKindNameMapping, ObjectHash } from "../../utils"; import styles from "./ASTOutline.css"; interface ASTOutlineProps { node?: ts.Node; - sourceFile?: ts.SourceFile; } function ASTOutline(props: ASTOutlineProps) { - const { node, sourceFile } = props; + const { node: nodeFromProps } = props; + const { + node: nodeFromRedux, + sourceFile, + highlightedNodes + } = useTypedSelector(state => ({ + node: state.sourceFiles.currentSourceFile, + sourceFile: state.sourceFiles.currentSourceFile, + highlightedNodes: state.highlightedNodes.nodes + })); + const node = nodeFromProps || nodeFromRedux; + if (node === undefined || sourceFile === undefined) { return null; } @@ -22,7 +31,13 @@ function ASTOutline(props: ASTOutlineProps) { const output = ( <>
  • - {syntaxKindNameMapping[node.kind]} + + {syntaxKindNameMapping[node.kind]} +