基于Vue2和jsmind.js实现思维导图

使用Vue2和jsmind.js实现思维导图,包括放大、缩小、添加、修改和删除等基本功能,以及使用jsmind.menu实现右键点击显示菜单。

1 参考文档

注意:vue-jsmind是在jsmind.js基础上进行了封装,本文用的是jsmind不是vue-jsmind;jsmind.menu.js不是jsmind.js自带的,是其他大神开发的。

1.1 参考网址

实现基本功能使用jsmind.js即可,不需要jsmind.menu.js

开源项目地址: http://hizzgdev.github.io/jsmind/developer.html

gitee上的地址:https://gitee.com/mirrors/jsmind

github上的地址:https://github.com/hizzgdev/jsmind

实现右键菜单的js文件,实现此功能需要将jsmind.menu.js文件下载下来,再动态导入到工程中。

https://github.com/allensunjian/jsmind.menu.js

1.2 安装jsmind

我使用的版本是:"jsmind": "^0.4.6";

cnpm install jsmind --save

2 截图

 

3 源代码

<template>
  <div>
    <div>
      <input type="button" value="放大" v-on:click="zoomIn" />
      <input type="button" value="缩小" v-on:click="zoomOut" />
    </div>
    <div id="jsmind_container"></div>
  </div>
</template>

<script>
import "jsmind/style/jsmind.css";
import jsMind from "jsmind/js/jsmind.js";
window.jsMind = jsMind;

require("jsmind/js/jsmind.draggable.js");
require("jsmind/js/jsmind.screenshot.js");
require("@/assets/js/jsmind.menu.js");

export default {
  name: "MyJsmind",

  data: function () {
    return {
      jm: null,
    };
  },

  mounted: function () {
    this.init_data();
  },

  methods: {
    init_data: function () {
      var mind = {
        /* 元数据,定义思维导图的名称、作者、版本等信息 */
        meta: {
          name: "jsMind-demo-tree",
          author: "hizzgdev@163.com",
          version: "0.2",
        },
        /* 数据格式声明 */
        format: "node_tree",
        /* 数据内容 */
        data: {
          id: "root",
          topic: "jsMind",
          children: [
            {
              id: "easy",
              topic: "Easy",
              direction: "left",
              expanded: false,
              children: [
                { id: "easy1", topic: "Easy to show" },
                { id: "easy2", topic: "Easy to edit" },
                { id: "easy3", topic: "Easy to store" },
                { id: "easy4", topic: "Easy to embed" },
              ],
            },
            {
              id: "open",
              topic: "Open Source",
              direction: "right",
              expanded: true,
              children: [
                { id: "open1", topic: "on GitHub" },
                { id: "open2", topic: "BSD License" },
              ],
            },
            {
              id: "powerful",
              topic: "Powerful",
              direction: "right",
              children: [
                { id: "powerful1", topic: "Base on Javascript" },
                { id: "powerful2", topic: "Base on HTML5" },
                { id: "powerful3", topic: "Depends on you" },
              ],
            },
            {
              id: "other",
              topic: "test node",
              direction: "left",
              children: [
                { id: "other1", topic: "I'm from local variable" },
                { id: "other2", topic: "I can do everything" },
              ],
            },
          ],
        },
      };

      var options = {
        container: "jsmind_container",
        editable: true,
        theme: "primary",

        menuOpts: {
          showMenu: true,
          injectionList: [
            {
              target: "edit",
              text: "编辑节点",
              callback: function (node) {
                console.log(node);
              },
            },
            {
              target: "addChild",
              text: "添加子节点",
              callback: function (node) {
                console.log(node);
              },
            },
            {
              target: "addBrother",
              text: "添加兄弟节点",
              callback: function (node) {
                console.log(node);
              },
            },
            {
              target: "delete",
              text: "删除节点",
              callback: function (node) {
                console.log(node);
              },
            },
            {
              target: "screenshot",
              text: "下载导图",
              callback: function (node) {
                console.log(node);
              },
            },
            {
              target: "showAll",
              text: "展开全部节点",
              callback: function (node) {
                console.log(node);
              },
            },
            {
              target: "hideAll",
              text: "收起全部节点",
              callback: function (node) {
                console.log(node);
              },
            },
          ],
        },
      };

      this.jm = new jsMind(options);
      this.jm.show(mind);
    },

    zoomIn: function () {
      this.jm.view.zoomIn();
    },

    zoomOut: function () {
      this.jm.view.zoomOut();
    },
  },
};
</script>

<style scoped>
#jsmind_container {
  width: 100%;
  height: 800px;
}
</style>

### Vue2 中通过 jsmind.draggable-node.js 实现 JSMind 的拖拽功能 要在 Vue2 项目中使用 `jsmind.draggable-node.js` 实现 JSMind 的拖拽功能,需要明确以下几点:JSMind 是一个基于 JavaScript思维导图工具[^4],而 `jsmind.draggable-node.js` 是为其提供节点拖拽功能的扩展脚本。以下是实现的具体方法: #### 1. 引入必要的依赖 首先,确保在项目中正确引入了 JSMind `jsmind.draggable-node.js`。可以通过以下方式加载这些文件: ```javascript // 在 main.js 或相关组件中引入 import 'jsmind/dist/jsmind.css'; // 引入样式 import jsMind from 'jsmind/dist/jsmind.js'; // 引入核心库 import 'jsmind/extensions/draggable/jsmind.draggable-node.js'; // 引入拖拽扩展 ``` 如果使用的是 CDN 方式,则可以在 HTML 文件中添加如下代码: ```html <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jsmind/css/jsmind.css"> <script src="https://cdn.jsdelivr.net/npm/jsmind/js/jsmind.js"></script> <script src="https://cdn.jsdelivr.net/npm/jsmind/extensions/draggable/jsmind.draggable-node.js"></script> ``` #### 2. 初始化 JSMind 并启用拖拽功能 在 Vue 组件中初始化 JSMind,并通过配置项启用拖拽功能。示例如下: ```javascript <template> <div id="jsmind_container" style="width: 100%; height: 500px;"></div> </template> <script> export default { data() { return { jm: null, // JSMind 实例 mindData: { meta: { name: "JSMind Example", author: "JSMinder", version: "0.1" }, format: "node_tree", data: { root: { topic: "Root Node", children: [ { topic: "Child Node 1" }, { topic: "Child Node 2" } ] } } } }; }, mounted() { this.initJSMind(); }, methods: { initJSMind() { const container = document.getElementById("jsmind_container"); const options = { container: container, editable: true, draggable: true, // 启用拖拽功能 theme: "primary" }; this.jm = jsMind.show(options, this.mindData); } } }; </script> ``` #### 3. 确保兼容性 由于 `jsmind.draggable-node.js` 是为 JSMind 提供的扩展功能,因此需要确保其版本与当前使用的 JSMind 版本匹配[^5]。如果不匹配,可能会导致拖拽功能无法正常工作。 #### 4. 自定义拖拽行为(可选) 如果需要自定义拖拽行为,可以通过监听事件或修改 `jsmind.draggable-node.js` 的源码来实现。例如,可以监听节点移动完成后的事件: ```javascript this.jm.on('node.move.end', (event) => { console.log('Node moved:', event.node.topic); }); ``` ### 注意事项 - 确保项目中正确加载了所有依赖项。 - 如果使用的是 Vue CLI 创建的项目,请注意调整 Webpack 配置以支持静态资源加载。 - 拖拽功能可能受浏览器兼容性影响,建议测试主流浏览器的支持情况。 ---
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值