Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

158 řádky
5.3 KiB

před 3 roky
  1. (function () {
  2. //var oldData;
  3. var html = '';
  4. html += '<button class="diy input">',
  5. html += '导入<input type="file" id="fileInput" accept=".km,.txt,.md,.json" >',
  6. html += '</button>',
  7. /*html += '<a class="diy output" href=""></a>'
  8. html += '<ul><li><a href="#" class="diy export" data-type="json">导出json</a></li>',
  9. html += '<li><a href="#" class="diy export" data-type="md">导出md</a></li>',
  10. html += '<li><a href="#" class="diy export" data-type="km">导出km</a></li>',
  11. html += '<li><a href="#" class="diy export" data-type="svg">导出svg</a></li>',
  12. html += '<li><a href="#" class="diy export" data-type="txt">导出text</a></li>',
  13. html += '<li><a href="#" class="diy export" data-type="png">导出png</a></li></ul>';*/
  14. //html += '<a href="#" class="diy export" data-type="json">导出json</a>',
  15. html += '<a href="#" class="diy export" data-type="km">导出km</a>',
  16. //html += '<a href="#" class="diy export" data-type="svg">导出svg</a>',
  17. html += '<a href="#" class="diy export" data-type="txt">导出txt</a>',
  18. html += '<a href="#" class="diy export" data-type="md">导出md</a>',
  19. html += '<a href="#" class="diy export" data-type="png">导出png</a>';
  20. $('.editor-title').append(html);
  21. $('.diy').css({
  22. // 'height': '30px',
  23. // 'line-height': '30px',
  24. 'margin-top': '0px',
  25. 'margin-left': '0px',
  26. 'float': 'left',
  27. 'background-color': '#393f4f',
  28. 'min-width': '60px',
  29. 'text-decoration': 'none',
  30. color: '#fff',
  31. 'padding': '0 10px',
  32. border: 'none',
  33. //'border-left': '1px solid #ccc',
  34. 'border-right': '1px solid #ccc',
  35. });
  36. $('.input').css({
  37. 'overflow': 'hidden',
  38. 'position': 'relative',
  39. }).find('input').css({
  40. cursor: 'pointer',
  41. position: 'absolute',
  42. top: 0,
  43. bottom: 0,
  44. left: 0,
  45. right: 0,
  46. display: 'inline-block',
  47. opacity: 0
  48. });
  49. $(document).on('click', '.export', function (event) {
  50. event.preventDefault();
  51. var $this = $(this),
  52. type = $this.data('type'),
  53. exportType;
  54. switch (type) {
  55. case 'km':
  56. exportType = 'json';
  57. break;
  58. case 'md':
  59. exportType = 'markdown';
  60. break;
  61. case 'svg':
  62. exportType = 'svg';
  63. break;
  64. case 'txt':
  65. exportType = 'text';
  66. break;
  67. case 'png':
  68. exportType = 'png';
  69. break;
  70. default:
  71. exportType = type;
  72. break;
  73. }
  74. editor.minder.exportData(exportType).then(function (content) {
  75. switch (exportType) {
  76. case 'json':
  77. console.log($.parseJSON(content));
  78. break;
  79. default:
  80. console.log(content);
  81. break;
  82. }
  83. var blob = new Blob();
  84. switch (exportType) {
  85. case 'png':
  86. blob = dataURLtoBlob(content); //将base64编码转换为blob对象
  87. break;
  88. default:
  89. blob = new Blob([content]);
  90. break;
  91. }
  92. var a = document.createElement("a"); //建立标签,模拟点击下载
  93. a.download = $('#node_text1').text() + '.' + type;
  94. a.href = URL.createObjectURL(blob);
  95. a.click();
  96. });
  97. });
  98. // 导入
  99. window.onload = function () {
  100. var fileInput = document.getElementById('fileInput');
  101. fileInput.addEventListener('change', function (e) {
  102. var file = fileInput.files[0],
  103. // textType = /(md|km)/,
  104. fileType = file.name.substr(file.name.lastIndexOf('.') + 1);
  105. console.log(file);
  106. switch (fileType) {
  107. case 'md':
  108. fileType = 'markdown';
  109. break;
  110. case 'txt':
  111. fileType = 'text';
  112. break;
  113. case 'km':
  114. case 'json':
  115. fileType = 'json';
  116. break;
  117. default:
  118. console.log("File not supported!");
  119. alert('只支持.km、.md、、text、.json文件');
  120. return;
  121. }
  122. var reader = new FileReader();
  123. reader.onload = function (e) {
  124. var content = reader.result;
  125. editor.minder.importData(fileType, content).then(function (data) {
  126. console.log(data)
  127. $(fileInput).val('');
  128. });
  129. }
  130. reader.readAsText(file);
  131. });
  132. }
  133. })();
  134. //base64转换为图片blob
  135. function dataURLtoBlob(dataurl) {
  136. var arr = dataurl.split(',');
  137. //注意base64的最后面中括号和引号是不转译的
  138. var _arr = arr[1].substring(0, arr[1].length - 2);
  139. var mime = arr[0].match(/:(.*?);/)[1],
  140. bstr = atob(_arr),
  141. n = bstr.length,
  142. u8arr = new Uint8Array(n);
  143. while (n--) {
  144. u8arr[n] = bstr.charCodeAt(n);
  145. }
  146. return new Blob([u8arr], {
  147. type: mime
  148. });
  149. }