KJDrawDocs

KJDRAW / VUE INTEGRATION

Vue integration

Mount the complete KJDraw editor as a Vue component, control it through an exposed ref, or build a custom composable.

Render the editor component#

<script setup lang="ts">
import { ref } from 'vue'
import { KJDraw, type KJDrawExposed, type KJWorkbenchLayout } from '@kanjieteam/kjdraw/vue'

const editor = ref<KJDrawExposed | null>(null)
const layout = ref<KJWorkbenchLayout>('classic')

async function moveSelection() {
  const instance = editor.value
  const ids = instance?.getSelection() ?? []
  if (!instance || !ids.length) return
  await instance.execute('MOVE', { ids, dx: 10, dy: 0 })
}
</script>

<template>
  <button @click="layout = 'focus'">Focus drawing</button>
  <button @click="moveSelection">Move selected</button>
  <KJDraw
    ref="editor"
    document="sample"
    locale="en"
    theme="dark"
    :layout="layout"
    style="width: 100%; height: 720px"
  />
</template>

This is the shortest path to a functional CAD surface. The exposed ref provides ready, open(), save(), execute(), selection, view and lifecycle methods. layout="classic" provides the full CAD ribbon and panels, compact shortens the ribbon, and focus prioritizes the canvas. Layout prop changes update the existing editor in place, preserving its drawing, selection and undo history. Vue disposes it when the component unmounts. See the Editor API for every option and method.

A minimal composable#

import { onScopeDispose, ref, shallowRef } from 'vue'
import { createKJDrawSDK } from '@kanjieteam/kjdraw'

export function useKJDraw() {
  const sdk = createKJDrawSDK()
  const document = shallowRef(sdk.createDocument({
    documentId: 'vue-drawing',
    units: 'millimeter',
  }))
  const revision = ref(document.value.revision)

  const off = sdk.events.on('command:committed', ({ document: changed }) => {
    if (changed === document.value) revision.value = changed.revision
  })
  onScopeDispose(off)

  const drawLine = () => sdk.executeCommand('CREATE', {
    type: 'LINE',
    payload: { start: [0, 0, 0], end: [100, 0, 0] },
  }, { document: document.value })

  return { sdk, document, revision, drawLine }
}

Reactivity rules#

  • Use shallowRef for a KJDocument; do not ask Vue to proxy its complete object graph.
  • Mirror committed revision, selection or tool state with small refs.
  • Register the event disposer with onScopeDispose.
  • Keep mutations inside SDK commands so validation, receipts and undo remain intact.

Share an editor scope#

For one editor, create the composable once in a provider component and expose it with Vue dependency injection or your state store. Create separate SDK instances only when documents need separate command registries, plugin scopes or lifecycle boundaries.

Use KJDraw for an immediately usable CAD surface or the composable for a product-owned interface. See the maintained examples for complete applications that use the same public package exports.

KJDRAW / Vue 集成

Vue 集成

将完整 KJDraw 编辑器作为 Vue 组件挂载,通过暴露的 ref 控制,或构建自定义 Composable。

渲染编辑器组件#

<script setup lang="ts">
import { ref } from 'vue'
import { KJDraw, type KJDrawExposed, type KJWorkbenchLayout } from '@kanjieteam/kjdraw/vue'

const editor = ref<KJDrawExposed | null>(null)
const layout = ref<KJWorkbenchLayout>('classic')

async function moveSelection() {
  const instance = editor.value
  const ids = instance?.getSelection() ?? []
  if (!instance || !ids.length) return
  await instance.execute('MOVE', { ids, dx: 10, dy: 0 })
}
</script>

<template>
  <button @click="layout = 'focus'">专注图纸</button>
  <button @click="moveSelection">移动已选对象</button>
  <KJDraw
    ref="editor"
    document="sample"
    locale="zh-CN"
    theme="dark"
    :layout="layout"
    style="width: 100%; height: 720px"
  />
</template>

这是得到可用 CAD 界面的最短路径。暴露的 ref 提供 readyopen()save()execute()、选择、视图与生命周期方法。layout="classic" 提供完整 CAD Ribbon 与面板,compact 使用较矮的 Ribbon,focus 让画布优先。布局 prop 变化会原位更新同一个编辑器,图档、选择集与撤销历史都保持不变;Vue 卸载组件时会自动释放资源。全部选项与方法见 Editor API

最小 Composable#

import { onScopeDispose, ref, shallowRef } from 'vue'
import { createKJDrawSDK } from '@kanjieteam/kjdraw'

export function useKJDraw() {
  const sdk = createKJDrawSDK()
  const document = shallowRef(sdk.createDocument({
    documentId: 'vue-drawing',
    units: 'millimeter',
  }))
  const revision = ref(document.value.revision)

  const off = sdk.events.on('command:committed', ({ document: changed }) => {
    if (changed === document.value) revision.value = changed.revision
  })
  onScopeDispose(off)

  const drawLine = () => sdk.executeCommand('CREATE', {
    type: 'LINE',
    payload: { start: [0, 0, 0], end: [100, 0, 0] },
  }, { document: document.value })

  return { sdk, document, revision, drawLine }
}

响应式规则#

  • KJDocument 使用 shallowRef,不要让 Vue 代理完整对象图。
  • 用小型 ref 映射已提交修订号、选择集或工具状态。
  • 使用 onScopeDispose 注册事件释放函数。
  • 修改始终放在 SDK 命令中,保留校验、回执与撤销能力。

共享编辑器作用域#

同一个编辑器应在 Provider 组件中只创建一次 Composable,再通过 Vue 依赖注入或状态库暴露。只有当图档需要独立命令注册表、插件作用域或生命周期边界时,才创建不同 SDK 实例。

需要立即可用的 CAD 界面时使用 KJDraw,产品自建全部界面时使用 Composable。持续维护的示例提供使用相同公开包入口的完整应用。

Guides, Editor API and complete type reference指南、Editor API 与完整类型参考