关于 Integrate Mind Elixir
Integrate Mind Elixir 是指导将 mind-elixir 集成到前端项目的技能,面向使用 JavaScript 思维导图核心的开发者。内容涵盖初始化、数据操作、事件、操作拦截和主题定制。
主要功能
- 01通过包管理器或脚本安装
- 02初始化思维导图实例
- 03获取与刷新图表数据
- 04监听操作和节点事件
- 05拦截并验证用户操作
- 06定制及切换主题
适用场景
- 01在前端项目中集成 mind-elixir
- 02从外部数据刷新思维导图
- 03监听节点选择和图表操作
- 04保存前异步验证用户操作
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ ssshooter/ mind-elixir-core --skill integrate-mind-elixir安装后在 Claude Code 中输入/integrate-mind-elixir确认已加载。
详细介绍
集成 Mind Elixir
本技能将引导你逐步将 mind-elixir 集成到前端项目中。Mind Elixir 是一个与框架无关的 JavaScript 思维导图核心。
1. 安装
你可以通过包管理器安装 Mind Elixir,也可以使用 script 标签。
NPM / Yarn / PNPM
npm i mind-elixir -S
# or
pnpm i mind-elixir
# or
yarn add mind-elixir
Script 标签
<script type="module" src="https://cdn.jsdelivr.net/npm/mind-elixir/dist/MindElixir.js"></script>
CSS: 如果使用 script 标签,还需要导入 CSS:
@import 'https://cdn.jsdelivr.net/npm/mind-elixir/dist/style.css';
2. 基本用法
HTML 结构
为思维导图创建一个容器。
<div id="map"></div>
<style>
#map {
height: 500px;
width: 100%;
}
</style>
初始化
在 JavaScript/TypeScript 文件中导入并初始化 Mind Elixir。
import MindElixir from 'mind-elixir'
import { en } from 'mind-elixir/i18n'
import 'mind-elixir/style.css'
// 1. Define options
let options = {
el: '#map', // or HTMLDivElement
direction: MindElixir.LEFT,
toolBar: true, // default true
keypress: true, // default true
overflowHidden: false, // default false
mouseSelectionButton: 0, // 0 for left button, 2 for right button, default 0
contextMenu: {
locale: en, // [cn,zh_CN,zh_TW,en,ru,ja,pt,it,es,fr,ko,ro,da,fi,de,nl,nb,sv]
focus: true,
link: true,
extend: [
{
name: 'Node edit',
onclick: () => {
alert('extend menu')
},
},
],
}, // default true
before: {
insertSibling(type, obj) {
return true
},
},
// Custom markdown parser (optional)
// markdown: (text) => customMarkdownParser(text), // provide your own markdown parser function
}
// 2. Create instance
let mind = new MindElixir(options)
// 3. Initialize with data
// Create new data
const data = MindElixir.new('new topic')
// Or use existing data passed from backend
// const data = { ... }
mind.init(data)
3. 数据结构
标准节点数据结构如下:
const nodeData = {
topic: 'node topic',
id: 'bd1c24420cd2c2f5',
style: { fontSize: '32', color: '#3298db', background: '#ecf0f1' },
expanded: true,
parent: null,
tags: ['Tag'],
icons: ['😀'],
hyperLink: 'https://github.com/ssshooter/mind-elixir-core',
image: {
url: 'https://raw.githubusercontent.com/ssshooter/mind-elixir-core/master/images/logo2.png',
height: 90,
width: 90,
},
children: [
{
topic: 'child',
id: 'xxxx',
// ...
},
],
}
4. 基本操作
获取数据
const data = mind.getData() // Returns the current data object
const dataString = mind.getDataString() // Returns serialized data string
刷新数据
如果你从外部来源更新了数据,或者需要重新渲染:
mind.refresh(data)
5. 事件处理
你可以使用 mind.bus 监听各种事件。
// Listen to all operations
mind.bus.addListener('operation', operation => {
console.log(operation)
// operation: { name: 'action_name', obj: target_object }
})
// Listen to node selection
mind.bus.addListener('selectNodes', nodes => {
console.log(nodes)
})
// Listen to node expansion
mind.bus.addListener('expandNode', node => {
console.log('expandNode: ', node)
})
6. 操作守卫
你可以使用 before 钩子拦截并控制用户操作。这适用于异步验证,或在应用更改前保存数据。
let mind = new MindElixir({
// ... other options
before: {
// Return true to allow, false to prevent
// Supports async functions
async addChild(el, obj) {
try {
await saveDataToDb()
return true
} catch (err) {
return false // Operation blocked
}
},
// Other hooks: insertSibling, removeNode, moveNode, etc.
},
})
7. 主题自定义
你可以在初始化时或动态自定义主题。
const options = {
// ...
theme: {
name: 'Dark',
// main lines color palette
palette: ['#848FA0', '#748BE9', '#D2F9FE', '#4145A5', '#789AFA', '#706CF4', '#EF987F', '#775DD5', '#FCEECF', '#DA7FBC'],
// overwrite css variables
cssVar: {
'--main-color': '#ffffff',
'--main-bgcolor': '#4c4f69',
'--color': '#cccccc',
'--bgcolor': '#252526',
'--panel-color': '255, 255, 255',
'--panel-bgcolor': '45, 55, 72',
},
// all variables see /src/index.less
},
// ...
}
动态更改主题:
mind.changeTheme({
name: 'Latte',
palette: ['#dd7878', '#ea76cb', '#8839ef', '#e64553', '#fe640b', '#df8e1d', '#40a02b', '#209fb5', '#1e66f5', '#7287fd'],
cssVar: {
'--main-color': '#444446',
'--main-bgcolor': '#ffffff',
'--color': '#777777',
'--bgcolor': '#f6f6f6',
},
})
评论
支持 Markdown