npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@gantfdt/2d-viewer-react

v0.1.1

Published

gant-2d 的 React 薄壳:DrawingViewer 组件与 hooks,业务逻辑全部在 @gantfdt/2d-viewer-core

Readme

@gantfdt/2d-viewer-react

@gantfdt/2d-viewer-core 的 React 薄壳:提供 DrawingViewer 组件、DrawingViewerRef 与三个轻量 hooks。组件只负责挂载 / 销毁 Viewer 并把 props 映射到 core API,业务逻辑全部在 core。

当前版本 0.1.0。许可证为 UNLICENSED(源码可见,使用需单独授权),见包内 LICENSE。

安装

pnpm add @gantfdt/2d-viewer-react react react-dom

react / react-dom 是 peer 依赖(^18.0.0 || ^19.0.0),由宿主提供;@gantfdt/2d-viewer-core 会作为依赖自动安装。

引入样式

import '@gantfdt/2d-viewer-react/style.css';

该文件只转发 @gantfdt/2d-viewer-core/style.css(内置 UI 样式与字体),不需要再单独引入 core 的样式。样式不会自动注入。

最小示例

import { useRef } from 'react';
import { DrawingViewer } from '@gantfdt/2d-viewer-react';
import type { DrawingViewerRef } from '@gantfdt/2d-viewer-react';
import '@gantfdt/2d-viewer-react/style.css';

export function App() {
  const viewerRef = useRef<DrawingViewerRef>(null);

  return (
    <DrawingViewer
      ref={viewerRef}
      height="calc(100vh - 64px)"
      theme="light"
      locale="zh"
      source={{ type: 'url', url: '/samples/plan.dxf' }}
      onLoad={(result) => console.log('实体数', result.stats.entityCount)}
      onError={(error) => console.error(error.code, error.messageKey)}
      onSelectionChange={(entities) => console.log(entities.map((entity) => entity.id))}
    />
  );
}

source 的引用变化会自动重新加载;上传、URL 输入等取图控件由宿主自己提供(组件不内置上传)。

组件属性

| 属性 | 说明 | | --- | --- | | source | 图纸来源(url / file / base64 / gdf),引用变化时自动重新加载 | | options | ViewerOptions(去掉 ui / slots);仅挂载时读取一次,运行时变更请用 ref 上的方法 | | height | 根元素高度:数字按 px,字符串原样(如 calc(100vh - 90px));受控,变化时调用 setHeight | | theme | light / dark;受控,变化时调用 setTheme | | locale | zh / en;受控,变化时调用 setLocale | | messages | 外部语言包,局部覆盖内置文案 | | ui | UIConfig;仅挂载时读取一次 | | slots | 插槽表,见下 | | className / style | 根元素 class 与内联样式 |

回调属性与 core 事件一一对应(事件名转 PascalCase 加 on):

onReady、onLoadProgress、onLoad、onDegraded、onError、onViewChange、onLayerChange、onLayoutChange、onSelectionChange、onSearch、onThemeChange、onLocaleChange、onRendererChange。

回调通过 ref 桥接,宿主每次 render 传入新函数不会导致重新订阅。

ref 与命令式调用

ref 拿到的就是 core 的 Viewer 实例:

const viewerRef = useRef<DrawingViewerRef>(null);

viewerRef.current?.fit();
viewerRef.current?.setLayerVisible({ id: 'WALLS', visible: false });
viewerRef.current?.zoomBy({ factor: 1.25 });
await viewerRef.current?.exportImage({ format: 'png' });

hooks

| Hook | 说明 | | --- | --- | | useDrawingViewer(opts) | 直接创建并持有 Viewer(不需要组件外壳时用),返回 { viewer, state };卸载时销毁 | | useLayers(viewer) | 图层快照,随 layerchange / load 更新 | | useSelection(viewer) | 选择快照,随 selectionchange 更新 |

import { useDrawingViewer } from '@gantfdt/2d-viewer-react';

function Panel({ host }: { host: HTMLElement }) {
  const { viewer, state } = useDrawingViewer({ container: host, height: 480 });
  return <span>{state.activeLayout}</span>;
}

useDrawingViewer 需要传入已经存在的 container(CreateViewerOptions 的必填项)。

插槽

<DrawingViewer
  slots={{
    'toolbar-end': [
      {
        render: (ctx) => (
          <button type="button" onClick={() => ctx.viewer.fit()}>
            适配
          </button>
        ),
      },
    ],
    'info-tab': [
      {
        title: '我的面板',
        render: (ctx) => <div>自定义内容</div>,
      },
    ],
  }}
/>

四个插槽:toolbar-start、toolbar-end、layer-panel-footer、info-tab。每个插槽可挂多条,按数组顺序渲染;info-tab 的 title 是页签名,首次切到该页签才渲染,卸载时执行清理。

注意事项

  • options / ui / slots 只在挂载时读取一次;运行时改配置请改用 ref 上的方法。
  • 严格模式下会出现「挂载 → 清理 → 再挂载」,组件已在清理时销毁实例,不会残留两份 DOM。