当前位置: 首页 > ds >正文

[mind-elixir]Mind-Elixir 的交互增强:单击、双击与鼠标 Hover 功能实现

[mind-elixir]Mind-Elixir 的交互增强:单击、双击与鼠标 Hover 功能实现

功能简述

  1. 通过防抖,实现单击双击区分
  2. 通过mousemove事件,实现hover效果

实现思路

(一)单击与双击事件
  1. 功能描述
    • 单击节点时,可以触发单击事件,用于执行一些简单操作,如显示节点详情、切换样式等。
    • 双击节点时,可以触发双击事件,用于执行更复杂的操作,如编辑节点内容、展开/折叠子节点等。
    • 通过防抖处理,能够准确区分单击和双击事件,避免误判。
  2. 实现思路
    • 使用 clickTimerclickCount 来记录点击事件的时间和次数。
    • 当节点被选中时,通过 handleNodeSelect 方法判断是单击还是双击。
    • 如果是同一个节点在短时间内多次点击,则视为双击;否则视为单击。
  3. 代码实现
    handleNodeSelect(nodeData) {// 如果是同一个节点if (this.lastClickedNode && this.lastClickedNode.id === nodeData.id) {this.clickCount++} else {// 不同节点,重置计数this.clickCount = 1this.lastClickedNode = nodeData}// 清除之前的定时器if (this.clickTimer) {clearTimeout(this.clickTimer)}// 设置新的定时器this.clickTimer = setTimeout(() => {if (this.clickCount === 1) {// 单击console.log('触发点击', nodeData)} else if (this.clickCount >= 2) {// 双击(或多击,都当作双击处理)console.log('触发双击', nodeData)}// 重置计数this.clickCount = 0this.lastClickedNode = null}, 200) // 200ms内的多次点击判断为双击
    }
    
(二)鼠标 Hover 事件
  1. 功能描述
    • 当鼠标悬停在某个节点上时,可以获取该节点的 ID,用于高亮显示、提示信息等操作。
    • 通过监听 mousemove 事件,实时获取鼠标位置和对应的节点信息。
  2. 实现思路
    • handleMouseMove 方法中,通过事件目标(e.target)获取节点的 ID。
    • 判断当前鼠标所在位置是否包含特定的节点元素(如 me-tpc),从而确定是否触发 Hover 事件。
  3. 代码实现
    handleMouseMove(e) {const hasMeTpc = e?.target?.querySelector('me-tpc') !== nullif (!hasMeTpc) {if (e.target) {const nodeId = e.target.getAttribute('data-nodeid')console.log('鼠标所在节点的id:', nodeId)}}
    }
    

代码

<template><div class="box"><div id="map" @mousemove="handleMouseMove"></div><div style="margin-top: 20px"><button @click="test1">测试1</button></div></div>
</template><script>
import MindElixir from 'mind-elixir'
import example from 'mind-elixir/example'const mock = {id: 'root',topic: '中心主题',children: [{id: 'child1',topic: '子主题1',children: []},{id: 'child2',topic: '子主题2',children: []}]
}export default {name: 'MindElixir',data() {return {ME: null,// 单击双击防抖处理clickTimer: null,clickCount: 0,lastClickedNode: null}},mounted() {const generateMainBranch = ({ pT, pL, pW, pH, cT, cL, cW, cH, direction }) => {console.log('111', pT, pL, pW, pH)console.log('222', cT, cL, cW, cH)console.log('direction', direction)const x1 = pWconst y1 = pT + pH / 2const c1 = pW + (cL - pW) / 2const c2 = cT + cH / 2return `M ${x1} ${y1} H ${c1} V ${c2} H ${cL}`}console.log('example', example)const theme = MindElixir.THEMEtheme.cssVar['--root-bgcolor'] = '#2499f2'theme.cssVar['--root-radius'] = '5px'theme.cssVar['--main-radius'] = '5px'theme.palette = ['#27f25a']this.ME = new MindElixir({el: '#map',locale: 'zh_CN',draggable: true, // 启用节点拖拽editable: true, // 启用编辑功能contextMenu: true, // 启用右键菜单toolBar: true, // 启用工具栏nodeMenu: true, // 启用节点菜单keypress: true, // 启用快捷键// before: {},generateMainBranch})this.ME.bus.addListener('operation', operation => {console.log('operation', operation)})this.ME.init({nodeData: mock,theme})// 监听节点选择事件(需要防抖处理单击/双击)this.ME.bus.addListener('selectNode', this.handleNodeSelect)},methods: {test1() {const mock2 = {id: 'root',topic: '中心主题222',style: {color: 'yellow'},children: [{id: 'child3',topic: '子主题3',children: []},{id: 'child4',topic: '子主题4',children: []}]}this.ME.refresh({nodeData: mock2})},// 单击双击事件handleNodeSelect(nodeData) {// 如果是同一个节点if (this.lastClickedNode && this.lastClickedNode.id === nodeData.id) {this.clickCount++} else {// 不同节点,重置计数this.clickCount = 1this.lastClickedNode = nodeData}// 清除之前的定时器if (this.clickTimer) {clearTimeout(this.clickTimer)}// 设置新的定时器this.clickTimer = setTimeout(() => {if (this.clickCount === 1) {// 单击console.log('触发点击', nodeData)} else if (this.clickCount >= 2) {// 双击(或多击,都当作双击处理)console.log('触发双击', nodeData)}// 重置计数this.clickCount = 0this.lastClickedNode = null}, 200) // 200ms内的多次点击判断为双击},// 鼠标移动事件handleMouseMove(e) {const hasMeTpc = e?.target?.querySelector('me-tpc') !== nullif (!hasMeTpc) {if (e.target) {const nodeId = e.target.getAttribute('data-nodeid')console.log('鼠标所在节点的id:', nodeId)}}}}
}
</script><style lang="less" scoped>
.box {text-align: center;
}
#map {width: 100%;height: 800px;overflow: auto;
}
</style>
http://www.xdnf.cn/news/16840.html

相关文章:

  • Web3.0 和 Web2.0 生态系统比较分析:差异在哪里?
  • 【Datawhale AI夏令营】科大讯飞AI大赛(大模型技术)/夏令营:让AI理解列车排期表(Task3)
  • 【python 获取邮箱验证码】模拟登录并获取163邮箱验证码,仅供学习!仅供测试!仅供交流!
  • uni-app webview的message监听不生效(uni.postmessage is not a function)
  • linux 执行sh脚本,提示$‘\r‘: command not found
  • 从一开始的网络攻防(十四):WAF绕过
  • day21-Excel文件解析
  • 【MySQL 数据库】MySQL索引特性(一)磁盘存储定位扇区InnoDB页
  • AI 代码助手在大前端项目中的协作开发模式探索
  • C++ Qt网络编程实战:跨平台TCP调试工具开发
  • 容器与虚拟机的本质差异:从资源隔离到网络存储机制
  • 2020 年 NOI 最后一题题解
  • Apple基础(Xcode②-Flutter结构解析)
  • 【硬件-笔试面试题】硬件/电子工程师,笔试面试题-49,(知识点:OSI模型,物理层、数据链路层、网络层)
  • 2025年湖北中级注册安全工程师报考那些事
  • 网络安全学习第16集(cdn知识点)
  • 知识速查大全:python面向对象基础
  • C++从入门到起飞之——智能指针!
  • 电子电气架构 --- 区域架构让未来汽车成为现实
  • 深入理解PostgreSQL的MVCC机制
  • SpringBoot之多环境配置全解析
  • Linux 系统日志管理与时钟同步实用指南
  • Tlias 案例-整体布局(前端)
  • cpp实现音频重采样8k->16k及16k->8k
  • 推扫式和凝视型高光谱相机分别采用哪些分光方式?
  • Web前端实战:Vue工程化+ElementPlus
  • 二叉树算法之【二叉树的层序遍历】
  • 专题:2025机器人产业技术图谱与商业化指南|附130+份报告PDF、数据汇总下载
  • Python爬虫05_Requests肯德基餐厅位置爬取
  • 小架构step系列30:多个校验注解