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