@jitword/mind-sdk
A self-contained mindmap editor SDK wrapping the mind engine. Provides structured APIs for data manipulation, theming, import/export, permissions, and real-time collaboration.
一个 支持本地部署 的独立思维导图 SDK。提供数据操作、主题管理、导入导出、权限控制和实时协作等结构化 API。
Installation (CDN)
安装(CDN)
Load the SDK via <script> tags. The full bundle includes all dependencies (mind-elixir, jsPDF, JSZip, etc.) in a single file.
通过 <script> 标签加载 SDK。完整包将所有依赖打包为单一文件。
<!-- 1. Process polyfill (required for bundled Node.js deps) --> <script> if (typeof window.process === 'undefined') { window.process = { env: { NODE_ENV: 'production' }, versions: { node: '' }, nextTick: function(fn) { setTimeout(fn, 0) }, browser: true }; } </script> <!-- 2. JitMind SDK (full bundle, UMD) --> <script src="./lib/jit-mind.full.js"></script>
Quick Start
快速开始
// Access the constructor from UMD global // 从 UMD 全局变量获取构造函数 var JitMind = window.JitMind.JitMind // 1. Create instance | 创建实例 var mindmap = new JitMind({ container: '#mindmap-root', locale: 'zh_CN', editable: true, direction: 'side', onChange: function(data) { console.log('Mindmap updated:', data) } }) // 2. Initialize (renders into DOM) | 初始化(渲染到 DOM) mindmap.init().then(function() { // 3. Use sub-APIs | 使用子 API mindmap.data.addChild('root', 'New Branch') mindmap.theme.setTheme('Rainbow') mindmap.io.exportPng({ name: 'my-map' }) }) // 4. Listen to events | 监听事件 mindmap.on('change', function(data) { console.log('Mindmap updated:', data) }) // 5. Cleanup | 销毁 mindmap.destroy()
Configuration Options
配置选项
| Option | Type | Default | Description |
|---|---|---|---|
container | string | HTMLElement | — | CSS selector or DOM element to mount into (required) |
locale | 'zh_CN' | 'en_US' | 'zh_CN' | UI language for mind toolbar/menus |
editable | boolean | true | Whether users can edit nodes |
direction | 'side' | 'right' | 'left' | 'side' | Layout direction of branches |
data | MindmapData | null | Default template | Initial mindmap data to render |
wsUrl | string | — | WebSocket URL for auto-connecting collaboration |
token | string | — | JWT token for authenticated collaboration |
userId | string | — | Current user identifier |
username | string | — | Display name for collaboration presence |
onChange | (data) => void | — | Shorthand for subscribing to the change event |
| 选项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
container | string | HTMLElement | — | CSS 选择器或 DOM 元素 (必填) |
locale | 'zh_CN' | 'en_US' | 'zh_CN' | 工具栏/菜单的语言 |
editable | boolean | true | 是否允许编辑节点 |
direction | 'side' | 'right' | 'left' | 'side' | 分支布局方向 |
data | MindmapData | null | 默认模板 | 初始化时加载的思维导图数据 |
wsUrl | string | — | WebSocket URL,传入后自动连接协作 |
token | string | — | JWT 鉴权 Token |
userId | string | — | 当前用户 ID |
username | string | — | 协作中显示的用户名 |
onChange | (data) => void | — | 数据变更回调(change 事件的快捷方式) |
JitMind Facade (formerly JitMindmap)
The main entry point. All functionality is accessed through its sub-API properties.
主入口类。所有功能通过其子 API 属性访问。
| Method | Returns | Description |
|---|---|---|
init() | Promise<void> | Renders the mindmap. Must be called after DOM is ready. |
destroy() | void | Tears down engine, disconnects collab, releases all resources. |
clearSelection() | void | Deselects current node/arrow/summary. |
getEngine() | MindElixirInstance | Escape hatch — returns the raw mind instance. |
isInitialized() | boolean | Check if init() has completed. |
on(event, handler) | () => void | Subscribe to events. Returns unsubscribe function. |
once(event, handler) | () => void | Subscribe once. |
off(event, handler) | void | Unsubscribe. |
| 方法 | 返回值 | 说明 |
|---|---|---|
init() | Promise<void> | 渲染思维导图。需在 DOM 就绪后调用。 |
destroy() | void | 销毁引擎、断开协作、释放所有资源。 |
clearSelection() | void | 取消当前节点/箭头/摘要的选中状态。 |
getEngine() | MindElixirInstance | 获取底层 mind 实例(高级用法)。 |
isInitialized() | boolean | 检查 init() 是否已完成。 |
on(event, handler) | () => void | 订阅事件。返回取消订阅函数。 |
once(event, handler) | () => void | 订阅一次性事件。 |
off(event, handler) | void | 取消订阅。 |
DataAPI mindmap.data
CRUD operations for mindmap nodes and data.
思维导图节点与数据的增删改查操作。
| Method | Description |
|---|---|
getData() | Returns the complete MindmapData tree. |
setData(data) | Replaces entire mindmap, re-renders, and centers viewport. |
getNode(nodeId) | Find a node by ID (DFS traversal). |
addChild(parentId, topic) | Add a child node. Returns new node ID. |
addSibling(nodeId, topic) | Add a sibling after the given node. |
removeNode(nodeId) | Remove a node (cannot remove root). |
moveNode(nodeId, targetParentId) | Reparent a node under a new parent. |
patchNodeStyle(patch) | Merge style properties on the currently selected node. |
setBranchColor(color) | Set branch color on selected node. Pass null to clear. |
resetNodeStyle() | Reset style & branchColor of the selected node to defaults. |
| 方法 | 说明 |
|---|---|
getData() | 返回完整的 MindmapData 数据树。 |
setData(data) | 替换整个思维导图数据,重新渲染并居中视口。 |
getNode(nodeId) | 按 ID 查找节点(深度优先遍历)。 |
addChild(parentId, topic) | 添加子节点。返回新节点 ID。 |
addSibling(nodeId, topic) | 在指定节点后添加兄弟节点。 |
removeNode(nodeId) | 删除节点(不能删除根节点)。 |
moveNode(nodeId, targetParentId) | 将节点移动到新的父节点下。 |
patchNodeStyle(patch) | 合并样式属性到当前选中节点。 |
setBranchColor(color) | 设置选中节点的分支颜色。传 null 清除。 |
resetNodeStyle() | 重置选中节点的样式和分支颜色为默认值。 |
Example: Programmatic node manipulation
示例:通过代码操作节点
// Add nodes | 添加节点 const id = mindmap.data.addChild('root', 'Market Research') mindmap.data.addChild(id, 'Competitor Analysis') mindmap.data.addSibling(id, 'User Interviews') // Style the selected node | 设置选中节点样式 mindmap.data.patchNodeStyle({ fontSize: '18px', fontWeight: 'bold' }) mindmap.data.setBranchColor('#ef4444') // Serialize for saving | 序列化用于保存 const snapshot = mindmap.data.getData() localStorage.setItem('mindmap', JSON.stringify(snapshot))
ThemeAPI mindmap.theme
Manage visual themes (15 built-in + custom registration).
管理视觉主题(15 个内置 + 自定义注册)。
| Method | Description |
|---|---|
setTheme(name) | Apply a theme by name (e.g. 'Rainbow', 'Dark'). |
getActiveTheme() | Returns the current theme name. |
listThemes() | Returns metadata for all available themes. |
registerCustomTheme(theme) | Register a custom MindmapTheme. |
applyThemeConfig(config) | Apply a raw ThemeConfig object directly. |
| 方法 | 说明 |
|---|---|
setTheme(name) | 按名称应用主题(如 'Rainbow'、'Dark')。 |
getActiveTheme() | 返回当前主题名称。 |
listThemes() | 返回所有可用主题的元数据。 |
registerCustomTheme(theme) | 注册自定义 MindmapTheme。 |
applyThemeConfig(config) | 直接应用一个 ThemeConfig 对象。 |
Built-in Themes
内置主题
Available names: Light, Minimal, Classic, Rainbow, Blue, Green, Orange, Purple, Teal, Sakura, Candy, Dawn, Dark, Midnight, Forest
可用主题名:Light(浅色)、Minimal(极简灰)、Classic(经典商务)、Rainbow(彩虹)、Blue(海洋蓝)、Green(清新绿)、Orange(活力橙)、Purple(优雅紫)、Teal(薄荷青)、Sakura(樱花粉)、Candy(糖果)、Dawn(晨曦)、Dark(深色)、Midnight(午夜蓝)、Forest(森林)
mindmap.theme.setTheme('Midnight') // Register custom theme | 注册自定义主题 mindmap.theme.registerCustomTheme({ name: 'Corporate', label: 'Corporate Blue', group: 'basic', preview: ['#f0f9ff', '#1e40af', '#3b82f6'], theme: { name: 'Corporate', palette: ['#1e40af', '#3b82f6', '#0ea5e9'], cssVar: { '--main-color': '#1e3a8a', '--bgcolor': '#f0f9ff' } } })
IOAPI mindmap.io
Export and import mindmap in various formats. All export methods trigger a browser download.
以多种格式导入导出思维导图。所有导出方法会触发浏览器下载。
| Method | Description |
|---|---|
exportPng({ name }) | Export as PNG image (transparent background). |
exportPdf({ name }) | Export as PDF (A4 landscape). |
exportXMind({ name }) | Export as .xmind file (ZIP archive, XMind 2020+ compatible). |
exportJson({ name }) | Export the raw JSON data. |
importJson(data) | Load a MindmapData object. |
importJsonFromFile() | Open file picker and import a .json file. |
| 方法 | 说明 |
|---|---|
exportPng({ name }) | 导出为 PNG 图片(透明背景)。 |
exportPdf({ name }) | 导出为 PDF(A4 横向)。 |
exportXMind({ name }) | 导出为 .xmind 文件(ZIP 归档,兼容 XMind 2020+)。 |
exportJson({ name }) | 导出原始 JSON 数据。 |
importJson(data) | 加载一个 MindmapData 对象。 |
importJsonFromFile() | 打开文件选择器导入 .json 文件。 |
await mindmap.io.exportPng({ name: 'project-plan' }) await mindmap.io.exportXMind({ name: 'project-plan' })
PermissionAPI mindmap.permission
Runtime permission toggling — switch between edit and readonly modes.
运行时权限切换 — 在编辑和只读模式间切换。
| Method | Description |
|---|---|
getMode() | Returns 'edit' or 'readonly'. |
setMode(mode) | Switch mode. Emits permission:change event. |
setReadonly() | Shorthand for setMode('readonly'). |
setEditable() | Shorthand for setMode('edit'). |
isEditable() | Returns true if currently in edit mode. |
| 方法 | 说明 |
|---|---|
getMode() | 返回 'edit' 或 'readonly'。 |
setMode(mode) | 切换模式。触发 permission:change 事件。 |
setReadonly() | setMode('readonly') 的快捷方式。 |
setEditable() | setMode('edit') 的快捷方式。 |
isEditable() | 返回当前是否处于编辑模式。 |
// Toggle based on user role | 根据用户角色切换 if (user.role === 'viewer') { mindmap.permission.setReadonly() }
CollabAPI mindmap.collab
Real-time collaboration via CRDT. Uses snapshot-level sync with 300ms debounce.
基于 CRDT算法 的实时协作。采用快照级同步,300ms 防抖。
| Method | Description |
|---|---|
connect(opts) | Connect to collaboration server. Opts: { wsUrl, docId, token?, userId?, username? } |
disconnect() | Disconnect and cleanup. |
getStatus() | Returns 'idle' | 'connecting' | 'connected' | 'disconnected'. |
getUsers() | Returns list of online CollabUser[]. |
getSelections() | Returns peer node selections PeerSelection[]. |
pushSnapshot() | Manually push current state to all peers. |
| 方法 | 说明 |
|---|---|
connect(opts) | 连接协作服务器。参数:{ wsUrl, docId, token?, userId?, username? } |
disconnect() | 断开连接并清理资源。 |
getStatus() | 返回 'idle' | 'connecting' | 'connected' | 'disconnected'。 |
getUsers() | 返回在线用户列表 CollabUser[]。 |
getSelections() | 返回其他用户的节点选中状态 PeerSelection[]。 |
pushSnapshot() | 手动推送当前状态到所有对等端。 |
// Manual connection | 手动连接 await mindmap.collab.connect({ wsUrl: 'wss://collab.example.com', docId: 'doc-123', token: 'eyJhbGciOi...', userId: 'user-1', username: 'Alice', }) mindmap.on('collab:users', (users) => { console.log('Online:', users.map(u => u.name)) })
Events EventBus
事件 EventBus
| Event | Payload | Description |
|---|---|---|
change | MindmapData | Fired on any data modification. |
node:select | NodeData | A node was selected. |
node:deselect | void | Selection was cleared. |
collab:status | CollabStatus | Connection status changed. |
collab:users | CollabUser[] | Online user list updated. |
collab:selections | PeerSelection[] | Peer node selections changed. |
permission:change | PermissionMode | Permission mode toggled. |
ready | void | SDK fully initialized. |
destroy | void | SDK destroyed. |
| 事件 | 载荷 | 说明 |
|---|---|---|
change | MindmapData | 任何数据修改时触发。 |
node:select | NodeData | 节点被选中。 |
node:deselect | void | 选中被清除。 |
collab:status | CollabStatus | 连接状态发生变化。 |
collab:users | CollabUser[] | 在线用户列表更新。 |
collab:selections | PeerSelection[] | 其他用户的选中状态变化。 |
permission:change | PermissionMode | 权限模式切换。 |
ready | void | SDK 初始化完成。 |
destroy | void | SDK 已销毁。 |
Types TypeScript
类型定义 TypeScript
interface MindmapData { nodeData: NodeData linkData?: Record<string, LinkData> direction?: number // 0=right, 1=side, 2=left theme?: ThemeConfig } interface NodeData { id: string topic: string root?: boolean children?: NodeData[] style?: NodeStyle tags?: string[] icons?: string[] hyperLink?: string memo?: string expanded?: boolean } interface NodeStyle { color?: string background?: string fontSize?: string fontWeight?: string } interface ThemeConfig { name: string palette: string[] cssVar: Record<string, string> } type PermissionMode = 'edit' | 'readonly' type CollabStatus = 'idle' | 'connecting' | 'connected' | 'disconnected'
Built-in Themes Reference
内置主题一览
Access via window.JitMind.builtinThemes and window.JitMind.themeGroups
通过 window.JitMind.builtinThemes 和 window.JitMind.themeGroups 获取
| Group | Themes |
|---|---|
| Basic | Light · Minimal · Classic |
| Vibrant | Rainbow · Blue · Green · Orange · Purple · Teal |
| Pastel | Sakura · Candy · Dawn |
| Dark | Dark · Midnight · Forest |
| 分组 | 主题 |
|---|---|
| 基础 | Light(浅色)· Minimal(极简灰)· Classic(经典商务) |
| 彩色 | Rainbow(彩虹)· Blue(海洋蓝)· Green(清新绿)· Orange(活力橙)· Purple(优雅紫)· Teal(薄荷青) |
| 柔和 | Sakura(樱花粉)· Candy(糖果)· Dawn(晨曦) |
| 深色 | Dark(深色)· Midnight(午夜蓝)· Forest(森林) |
Helper: Default Data
工具函数:默认数据
var createDefaultMindmapData = window.JitMind.createDefaultMindmapData // Creates a default tree with root + 2 branches // 创建一棵默认树:根节点 + 2 个分支 var data = createDefaultMindmapData('zh_CN') // → { nodeData: { id: 'root', topic: '中心主题', children: [...] }, linkData: {} }
@jitword/mind-sdk v0.1.0 — Built on mind