🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

@hy-bricks/canvas

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hy-bricks/canvas

HyperCard 画布 — 多组件实例编排 / 自由布局 / 拖拽 / 连线 / 属性面板。受控组件,SDK 边界守住:不 fetch / 不知后端 / 不开 Drawer。

latest
npmnpm
Version
0.6.6
Version published
Maintainers
1
Created
Source

@hy-bricks/canvas

HyperCard 画布 — 多组件实例编排 / 渐进渲染 / 只读渲染器。受控组件,SDK 边界守住:不 fetch / 不知后端 / 不开 Drawer。

version license vue

📦 当前发布版 0.6.3 ── 逐版变更(含历史 BREAKING)以 CHANGELOG 为准。需要留意的 BREAKING:0.6.0@hy-bricks/core 收口公开面(移除 test-only / 内部导出);0.4.0 instance:ready 提前到 created 后,回调内访问 vm.$elnextTick(() => vm.$el...)。布局自 0.5.0 起支持 free-split 自由分割(容器布局共 6 种:none / free / flex / grid / split / free-split)。

简介

@hy-bricks/canvas 是 HyperCard 画布 SDK,提供:

  • <HyperCardPageRenderer> — 只读页面渲染器:接受 PageRenderPayload,用 <RuntimeBox> 渲染所有实例,支持 IntersectionObserver 渐进 mount + 视口外缓冲 dispose
  • createRenderScheduler — 渲染调度器 composable:管理实例级 mount/dispose 状态机,与渲染组件解耦
  • 协议类型 — PageDocument / PageRenderPayload / ComponentContract / ComponentVersionKey 等完整类型导出
  • helperisDraftKey() / parseComponentVersionKey() 用于区分正式版本 vs 编辑期草稿

SDK 边界铁律:

  • 不 fetch — 所有数据由宿主通过 props 传入
  • 不知后端 — 不依赖任何特定后端 schema / API
  • 不开 Drawer / Dialog — UI 交互由宿主控制
  • 不绑定 UI 框架 — 不强依赖 Element Plus / Pinia / Monaco

快速开始

pnpm add @hy-bricks/canvas @hy-bricks/core vue
<script setup lang="ts">
import { HyperCardPageRenderer } from '@hy-bricks/canvas'
import type { PageRenderPayload } from '@hy-bricks/canvas'

const payload: PageRenderPayload = {
  page: { id: 'p1', name: 'Demo', targets: ['pc'] },
  version: { version: 'v1', label: 'v1', publishedAt: '2026-05-11T00:00:00Z' },
  document: {
    layout: { type: 'free', canvas: { w: 1920, h: 1080 } },
    instances: [
      {
        instanceId: 'inst-1',
        componentId: 'button',
        componentVersionKey: 'button@v1',
        rect: { x: 100, y: 100, w: 200, h: 60 },
        zIndex: 1,
        props: {},
      },
    ],
    bindings: [],
  },
  componentVersions: {
    'button@v1': {
      key: 'button@v1',
      componentId: 'button',
      version: 'v1',
      status: 'ok',
      source: {
        html: '<el-button @click="say">{{ msg }}</el-button>',
        js: 'export default { data(){ return { msg:"Hi" } }, methods:{ say(){ this.msg="Clicked" } } }',
        css: '',
      },
      contract: {
        propsDecl: [],
        emitsDecl: [],
        methodsDecl: [{ key: 'say', params: [] }],
        slotsDecl: [],
        modelDecl: [],
        dataInputsDecl: [],
        dataOutputsDecl: [],
      },
    },
  },
}
</script>

<template>
  <HyperCardPageRenderer :payload="payload" />
</template>

API 概览

组件:<HyperCardPageRenderer>

只读页面渲染器。接受宿主拼好的 payload 或拆开的 document + sourceMap。

Props:

Prop类型默认值说明
payloadPageRenderPayload?/render 端点产物(含 document + componentVersions)。优先使用
documentPageDocument?页面结构(payload 为空时使用,设计器场景)
componentSourceMapComponentSourceMap?组件版本 source 字典(payload 为空时使用)
schedulerOptionsRenderSchedulerOptions?调度器配置(透传给 createRenderScheduler)

行为:

  • 每个 PageInstance 渲染一个 wrapper div(absolute 定位)+ <RuntimeBox>
  • 视口内:渐进 mount(每 idle frame 最多 N 个)
  • 视口外:缓冲一段(默认 1500ms)后 dispose,滚回来 LRU 命中不重编
  • status !== 'ok' 的实例渲染降级卡,不阻断整页

Composable:createRenderScheduler

渲染调度器 — 管理实例级 pending → mounting → mounted → disposing 状态机。

import { createRenderScheduler } from '@hy-bricks/canvas'

const scheduler = createRenderScheduler({
  rootMargin: '500px',        // 视口缓冲
  disposeDelayMs: 1500,       // 视口外延迟 dispose
  mountConcurrency: 2,        // 每帧最多挂 2 个
})

scheduler.register('inst-1', element)  // 注册实例 + 开始观察
scheduler.unregister('inst-1')         // 停止观察 + 清单实例 state
scheduler.isMounted('inst-1')          // boolean — Vue 模板 reactive 跟踪
scheduler.getState('inst-1')           // MountState — 渲染 RuntimeBox vs skeleton 判别
scheduler.dispose()                    // 全量清理(取消 IO + 清 timer + 清 state),onBeforeUnmount 调

配置(RenderSchedulerOptions):

字段类型默认值说明
rootMarginstring'500px'IntersectionObserver 视口缓冲距离
disposeDelayMsnumber1500视口外多久才真 dispose
mountConcurrencynumber2渐进 mount 每帧个数
rootElement?null(viewport)IntersectionObserver root(画布自有滚动容器时传)

状态(MountState):

状态说明
pending已注册,未进入视口
mounting已进入视口,排队等 idle frame
mounted已挂载,正在渲染
disposing已离开视口,缓冲倒计时中

协议类型

@hy-bricks/canvas 直接 import,不需要额外装包:

import type {
  // 基础
  Source,
  // ComponentVersionKey
  ComponentVersionKey,
  ParsedComponentVersionKey,
  // ComponentContract(7 字段)
  PropDecl,
  EmitDecl,
  MethodDecl,
  SlotDecl,
  ModelDecl,
  DataInputDecl,
  DataOutputDecl,
  ComponentContract,
  ComponentKind,
  // ComponentVersionAsset
  ComponentVersionStatus,
  ComponentVersionAsset,
  DraftComponentVersionAsset,
  // Page 结构
  PageInstance,
  PageBinding,
  PageLayout,
  PageDocument,
  PageRenderPayload,
  ComponentSourceMap,
  // 版本状态 / 特性开关
  CanvasVersionStatus,
  CanvasFeatures,
  // 组件库面板
  ComponentCatalogItem,
} from '@hy-bricks/canvas'

Helper

import { isDraftKey, parseComponentVersionKey } from '@hy-bricks/canvas'

isDraftKey('button@v3')                  // false
isDraftKey('button@draft:p1:v3')         // true

parseComponentVersionKey('button@v3')
// → { kind: 'published', componentId: 'button', version: 'v3' }

parseComponentVersionKey('button@draft:p1:v3')
// → { kind: 'draft', componentId: 'button', pageId: 'p1',
//     baseVersion: 'v3', baseVersionKey: 'button@v3' }

parseComponentVersionKey('garbage')
// → null

性能特性

得益于 IntersectionObserver 渐进 mount + LRU 编译缓存,在数十到上百个实例的页面上仍能保持流畅:

  • 首屏骨架(skeleton):payload 到达后即出占位框,不阻塞首屏
  • 渐进挂载:视口内实例按 idle frame 节流挂载,避免单帧卡顿
  • 滚动重用:视口外延迟 dispose + 滚回 LRU 命中,组件不重新编译

配合使用 / Ecosystem

License

MIT © hy_top

Keywords

hypercard

FAQs

Package last updated on 27 Jun 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts