ToB企服应用市场:ToB评测及商务社交产业平台

标题: Vue3 前端 引入 markmap 思维导图,通过markdown解析成思维导图 [打印本页]

作者: 南七星之家    时间: 2024-7-13 06:26
标题: Vue3 前端 引入 markmap 思维导图,通过markdown解析成思维导图

 安装

  1. //
  2. npm install markmap-lib
  3. //
  4. npm install markmap-view
  5. //
  6. npm install markmap-common
复制代码

 完整代码

  1. <script setup>
  2. import { ref, onMounted, onUpdated } from 'vue'
  3. import { Transformer } from 'markmap-lib'
  4. import { Markmap } from 'markmap-view'
  5. import * as htmlToImage from 'html-to-image'
  6. import { saveAs } from 'file-saver'
  7. const transformer = new Transformer()
  8. const initValue = `
  9. # 思维导图
  10. 1. 标题1
  11. - 子标题1
  12. - 子标题2
  13. 3. 标题2
  14. 4. 标题3
  15. - beautiful
  16. - useful
  17. - easy
  18. - interactive
  19. `
  20. const mm = ref()
  21. const svgRef = ref()
  22. const update = () => {
  23.   const { root } = transformer.transform(initValue)
  24.   mm.value.setData(root)
  25.   mm.value.fit()
  26. }
  27. const zoomIn = () => mm.value.rescale(1.25)
  28. const zoomOut = () => mm.value.rescale(0.8)
  29. const fitToScreen = () => mm.value.fit()
  30. const onSave = async () => {
  31.   const dataUrl = await htmlToImage.toPng(svgRef.value)
  32.   saveAs(dataUrl, 'markmap.png')
  33. }
  34. onMounted(() => {
  35.   // 初始化markmap思维导图
  36.   mm.value = Markmap.create(svgRef.value)
  37.   // 更新思维导图渲染
  38.   update()
  39. })
  40. onUpdated(update)
  41. </script>
  42. <template>
  43.   <div class="mind">
  44.     <div class="svg-container">
  45.       <svg ref="svgRef"></svg>
  46.     </div>
  47.     <div class="controls">
  48.       <button @click="zoomIn">放大</button>
  49.       <button @click="zoomOut">缩小</button>
  50.       <button @click="fitToScreen">适应屏幕</button>
  51.       <button @click="onSave">下载</button>
  52.     </div>
  53.   </div>
  54. </template>
  55. <style lang="scss" scoped>
  56. .mind {
  57.   width: 100%;
  58.   height: 100%;
  59.   display: flex;
  60.   flex-direction: column;
  61.   .svg-container {
  62.     flex: 1;
  63.     svg {
  64.       width: 100%;
  65.       height: 100%;
  66.     }
  67.   }
  68.   .controls {
  69.     display: flex;
  70.     justify-content: center;
  71.     align-items: center;
  72.     height: 50px;
  73.   }
  74. }
  75. .controls {
  76.   margin-top: 10px;
  77. }
  78. </style>
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4