zthxxx / react-dev-inspector

jump to local IDE code directly from browser React component by just a simple click
https://react-dev-inspector.zthxxx.me
MIT License
1.13k stars 67 forks source link

refactor: refactor Inspector implementation to DOMInspectAgent, support setting custom InspectAgent #163

Closed zthxxx closed 3 months ago

zthxxx commented 4 months ago

The design of InspectAgent:

the InspectAgent design different renderer binding (like React DOM, React Native, React Three.js etc.)

An Agent need implement these functions:

The proposal type of InspectAgent:

export interface InspectAgent<Element> {
  /**
   * trigger when user activate inspector in <Inspector/>
   *
   * Agent need setup event listeners to collect user interaction on their target renderer (like DOM, React Native, React Three.js etc.)
   */
  activate(params: {
    /**
     * the initial `PointerMove` event when activate inspector,
     * use its position to check whether hovered any element immediately at initialization then trigger Inspector.
     */
    pointer?: PointerEvent;
    /**
     * when hovered a element
     * trigger it like on PointerMove on PointerOver event.
     */
    onHover: (params: { element: Element; pointer: PointerEvent }) => void;
    /**
     * Just throw the `PointerDown` event to Inspector,
     *   that's no need to stopPropagation or preventDefault in agent, Inspector will auto stop it when agent is in active.
     * Normally, the `PointerDown` event will stop by Inspector to prevent the default behavior like text selection,
     *   and the `Click` event will use to trigger the inspection and remove event listeners (by deactivate agent).
     */
    onPointerDown: (params: { element?: Element; pointer: PointerEvent }) => void;
    /**
     * just throw the `client` event to Inspector,
     *   that's no need to stopPropagation or preventDefault in agent, Inspector will auto stop it when agent is in active.
     * Normally, the `PointerDown` event will stop by Inspector to prevent the default behavior like text selection,
     *   and the `Click` event will use to trigger the inspection and remove event listeners (by deactivate agent).
     */
    onClick: (params: { element?: Element; pointer: PointerEvent }) => void;
  }): void;

  /**
   * trigger when user deactivate inspector in <Inspector/>,
   * to clear agent's indicators, remove event listeners, release resources and reset states
   */
  deactivate(): void;

  /**
   * use for filter valid elements from input element upward to render root.
   * a "valid" element considered have a valid name and you want show it in the inspected list.
   */
  getAncestorChain(element: Element): Generator<Element, void, void>;

  /**
   * get the element display name and title for show in indicator UI
   */
  getNameInfo(element: Element): (
    | undefined
    | {
      /** element's constructor name */
      name: string;
      /** display to describe the element as short */
      title: string;
    }
  );

  findCodeInfo(element: Element): CodeInfo | undefined;

  /**
   * show a indicator UI for the element on page
   */
  indicate(params: {
    element: Element;
    pointer: PointerEvent;
    name?: string;
    title?: string;
  }): void;

  /**
   * hide agent's indicator UI
   */
  removeIndicate(): void;
}
vercel[bot] commented 4 months ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-dev-inspector ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 3, 2024 7:34am