index.html 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8"/>
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  6. <title>思维导图</title>
  7. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. box-sizing: border-box;
  12. }
  13. #markmap {
  14. display: flex;
  15. width: 100%;
  16. height: 100vh;
  17. }
  18. </style>
  19. <script src="https://cdn.reallyshort.cn/static/markmap/dsbridge.js"></script>
  20. <script src="https://cdn.reallyshort.cn/static/markmap/d3.js"></script>
  21. <script src="https://cdn.reallyshort.cn/static/markmap/markmap-lib.js"></script>
  22. <script src="https://cdn.reallyshort.cn/static/markmap/markmap-view.js"></script>
  23. </head>
  24. <body>
  25. <svg id="markmap"></svg>
  26. </body>
  27. <script>
  28. const { markmap } = window;
  29. const { Transformer, Markmap, loadCSS, loadJS } = window.markmap;
  30. const transformer = new Transformer([]);
  31. const { scripts, styles } = transformer.getAssets();
  32. loadCSS(styles);
  33. loadJS(scripts, { getMarkmap: () => markmap });
  34. let mm;
  35. let index = 0;
  36. let title = "思维导图";
  37. let description = "";
  38. let mindmapRoot = null;
  39. const branchColors = {};
  40. const colors = ["#F63F2F", "#F6A24B", "#F7D422", "#02BB7B", "#4869FF", "#7416E6"];
  41. let svgEl = document.querySelector("#markmap");
  42. mm = Markmap.create(svgEl, {
  43. color: (node) => {
  44. const { depth, path, id } = node.state;
  45. // 根节点
  46. if (depth === 1) {
  47. return colors[0];
  48. }
  49. // 二级节点
  50. if (depth === 2) {
  51. if (!branchColors[path]) {
  52. branchColors[path] = {
  53. color: colors[index % colors.length],
  54. index: index,
  55. };
  56. index++;
  57. }
  58. return branchColors[path].color;
  59. }
  60. // 二级以下节点
  61. const rootPath = path.split(".")[1];
  62. return branchColors["1." + rootPath].color;
  63. },
  64. maxWidth: 400, // 节点最大宽度
  65. });
  66. function updateValue(value) {
  67. const { root } = transformer.transform(value);
  68. mindmapRoot = root;
  69. description = value;
  70. mm.setData(root);
  71. mm.fit();
  72. }
  73. // 注册更新数据的方法
  74. dsBridge.register("updateValue", function (value) {
  75. description = value;
  76. updateValue(value);
  77. });
  78. </script>
  79. </html>