@edv4h/usketch-plugin-container

中文文档
0.3.23发布于 2026/11/10MITTypeScriptES Module构建来源已验证近 30 天下载 2423
原始使用文档
文档目录3 节
  1. Usage
  2. Attachable child shapes ( createAttachablePlugin )
  3. Notes / limitations

@edv4h/usketch-plugin-container

Runtime for container shapes — shapes whose ShapeDefinition declares a container object. It supplies the parentId-based container mechanics that a shape definition can't express on its own:

  • Auto-attach — a shape dropped fully inside a container that sets container.autoAttach gets its parentId set (and cleared when moved out).
  • Arrange — a container's container.layout positions its children on attach/detach, resize, and (non-drag) updates.
  • Snap exclusion — while a container is dragged, its children (which follow natively) are excluded from snapping so they don't jitter. Requires @edv4h/usketch-plugin-snap; a no-op if snap isn't installed.

Selection resolution ("click a child, select the child") and move-follow ("drag the parent, children follow") are handled natively by tool-helpers/tool-select from the same container flags — this plugin only adds the reactive pieces above.

Usage

Register after the snap plugin so its snap:configure listener is ready:

import { createContainerPlugin } from "@edv4h/usketch-plugin-container";

const plugins = [
  // ...
  createSnapPlugin(),
  createContainerPlugin(),
];

Declare a container on a shape definition (all fields accept a boolean | (data) => boolean predicate, so a single type can vary per instance):

import { stackLayout } from "@edv4h/usketch-plugin-container";

ctx.shapes.register("wireframe", {
  // ...render/hitTest/resize/createDefault...
  container: {
    enabled: (s) => s.meta?.component === "card", // only cards are containers
    selectableChildren: true,                     // children selectable individually
    autoAttach: true,                             // drop-inside attaches as child
    layout: stackLayout({ padding: 16, gap: 8 }), // arrange children vertically
  },
});

Children are ordinary shapes referencing the container via native parentId.

Attachable child shapes (createAttachablePlugin)

The child-side counterpart. Whereas container opts a parent in to holding children, attachable opts a shape in as a child that sticks to and follows any shape it is dropped on — even a non-container (sticky note, card). Use it for stamps, badges, reaction pins, or handwritten annotation widgets. Because the child rides on the ordinary select tool (no pointer hijacking) it keeps selection / resize / rotate.

It is a separate plugin from createContainerPlugin (register either or both), colocated here because it drives the same parentId containment subsystem:

import { createAttachablePlugin } from "@edv4h/usketch-plugin-container";

const plugins = [
  // ...
  createAttachablePlugin(),
];

ctx.shapes.register("badge", {
  // ...render/hitTest/resize/createDefault...
  attachable: {
    toAny: (target) => target.type !== "connector", // eligible targets
    follow: true,                                    // follow the parent's move
    hitTest: "center",                               // "drop it on and it sticks"
  },
});
  • Auto-attach (this plugin): on drop, parentId is set to the front-most accepted shape under the badge (per hitTest / toAny), cleared when dropped over nothing.
  • Move-follow is native (tool-helpers/tool-select read attachable.follow) — it works even without this plugin. See ShapeDefinition.attachable in @edv4h/usketch-shared for the full API.

Notes / limitations

  • z-order / nesting: children are positioned but not z-ordered by this plugin — visual nesting relies on zIndex.
  • Single excludeTargets: snap:configure holds one excludeTargets predicate; if multiple plugins set it, last write wins. Only this plugin is expected to.
  • Rotation: children follow/arrange on move and resize; parent rotation is not propagated to children.

版本与发布记录

@edv4h/usketch-plugin-container 共发布 24 个版本,首次发布于 2026/07/09;过去 12 个月发布了 24 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.3.23latest2026/11/103 天前
0.3.222026/11/103 天前
0.3.212026/11/067 天前
0.3.192026/11/0310 天前
0.3.182026/11/0211 天前
0.3.172026/10/2519 天前

关于 @edv4h/usketch-plugin-container 的常见问题

如何安装 @edv4h/usketch-plugin-container?

在项目目录运行 npm install @edv4h/usketch-plugin-container,或使用 pnpm add / yarn add / bun add。

@edv4h/usketch-plugin-container 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@edv4h/usketch-plugin-container 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@edv4h/usketch-plugin-container 可以商用吗?

@edv4h/usketch-plugin-container 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@edv4h/usketch-plugin-container 的下载量有多少?

根据 npm 官方下载统计,@edv4h/usketch-plugin-container 在 2026/10/13 – 2026/11/11 这 30 天内被下载 2,423 次,日均约 81 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@edv4h/usketch-plugin-container 还在维护吗?

最新版本 0.3.23 发布于 2026/11/10(3 天前);至今共发布 24 个版本,其中过去 12 个月发布了 24 个;首次发布于 2026/07/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。