index_export.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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: auto;
  17. aspect-ratio: 1;
  18. }
  19. </style>
  20. <script src="https://cdn.reallyshort.cn/static/markmap/dsbridge.js"></script>
  21. <script src="https://cdn.reallyshort.cn/static/markmap/d3.js"></script>
  22. <script src="https://cdn.reallyshort.cn/static/markmap/markmap-lib.js"></script>
  23. <script src="https://cdn.reallyshort.cn/static/markmap/markmap-view.js"></script>
  24. <script src="https://cdn.reallyshort.cn/static/markmap/svg-png.js"></script>
  25. </head>
  26. <body>
  27. <svg id="markmap"></svg>
  28. </body>
  29. <script>
  30. const { markmap } = window;
  31. const { Transformer, Markmap, loadCSS, loadJS } = window.markmap;
  32. const transformer = new Transformer([]);
  33. const { scripts, styles } = transformer.getAssets();
  34. loadCSS(styles);
  35. loadJS(scripts, { getMarkmap: () => markmap });
  36. let mm;
  37. let index = 0;
  38. let title = "思维导图";
  39. let description = "";
  40. const branchColors = {};
  41. const colors = ["#F63F2F", "#F6A24B", "#F7D422", "#02BB7B", "#4869FF", "#7416E6"];
  42. async function mindMapSaveAsPngCreateFile() {
  43. const svgEl = document.querySelector("#markmap");
  44. const markmap = Markmap.create(svgEl, {
  45. color: (node) => {
  46. const { depth, path, id } = node.state;
  47. // 根节点
  48. if (depth === 1) {
  49. return colors[0];
  50. }
  51. // 二级节点
  52. if (depth === 2) {
  53. if (!branchColors[path]) {
  54. branchColors[path] = {
  55. color: colors[index % colors.length],
  56. index: index,
  57. };
  58. index++;
  59. }
  60. return branchColors[path].color;
  61. }
  62. // 二级以下节点
  63. const rootPath = path.split(".")[1];
  64. return branchColors["1." + rootPath].color;
  65. },
  66. duration: 0, // 动画时长
  67. maxWidth: 400, // 节点最大宽度
  68. });
  69. const { root } = transformer.transform(description);
  70. markmap.setData(root);
  71. markmap.fit();
  72. const { minX, maxX, minY, maxY } = markmap.state;
  73. const naturalWidth = maxY - minY;
  74. const naturalHeight = maxX - minX;
  75. const ratio = naturalHeight / naturalWidth;
  76. svgEl.style.aspectRatio = naturalWidth / naturalHeight;
  77. markmap.setData(root);
  78. markmap.fit();
  79. await delay(300);
  80. const base64 = await svgToPng("#markmap", title, { scale: 3, format: "png", background: "white" });
  81. markmap.destroy();
  82. return base64;
  83. }
  84. function delay(time) {
  85. return new Promise((resolve) => {
  86. setTimeout(() => {
  87. resolve();
  88. }, time);
  89. });
  90. }
  91. // 导出
  92. dsBridge.registerAsyn("export", async (fileName,summary, responseCallback) => {
  93. if (fileName) {
  94. title = fileName;
  95. }
  96. if(summary){
  97. description = summary;
  98. }
  99. const bytes = await mindMapSaveAsPngCreateFile();
  100. responseCallback(bytes);
  101. });
  102. </script>
  103. </html>