This repository has been archived by the owner on May 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.js
246 lines (223 loc) · 9 KB
/
main.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Get a reference to the VS Code webview api.
// We use this API to post messages back to our extension.
(function () {
// @ts-ignore
const vscode = acquireVsCodeApi();
window.onresize = (event) => {
document.getElementById('vditor').style.height = window.innerHeight - 20 + 'px';
};
const saveIcon =
'<svg t="1590139100426" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4845" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M292.571429 877.714286l438.857143 0 0-219.428571-438.857143 0 0 219.428571zm512 0l73.142857 0 0-512q0-8-5.714286-22t-11.428571-19.714286l-160.571429-160.571429q-5.714286-5.714286-19.428571-11.428571t-22.285714-5.714286l0 237.714286q0 22.857143-16 38.857143t-38.857143 16l-329.142857 0q-22.857143 0-38.857143-16t-16-38.857143l0-237.714286-73.142857 0 0 731.428571 73.142857 0 0-237.714286q0-22.857143 16-38.857143t38.857143-16l475.428571 0q22.857143 0 38.857143 16t16 38.857143l0 237.714286zm-219.428571-530.285714l0-182.857143q0-7.428571-5.428571-12.857143t-12.857143-5.428571l-109.714286 0q-7.428571 0-12.857143 5.428571t-5.428571 12.857143l0 182.857143q0 7.428571 5.428571 12.857143t12.857143 5.428571l109.714286 0q7.428571 0 12.857143-5.428571t5.428571-12.857143zm365.714286 18.285714l0 530.285714q0 22.857143-16 38.857143t-38.857143 16l-768 0q-22.857143 0-38.857143-16t-16-38.857143l0-768q0-22.857143 16-38.857143t38.857143-16l530.285714 0q22.857143 0 50.285714 11.428571t43.428571 27.428571l160 160q16 16 27.428571 43.428571t11.428571 50.285714z" p-id="4846"></path></svg>';
let toolbar = [
'emoji',
'headings',
'bold',
'italic',
'strike',
'link',
'|',
'list',
'ordered-list',
'check',
'outdent',
'indent',
'|',
'quote',
'line',
'code',
'inline-code',
'insert-before',
'insert-after',
'|',
'table',
'|',
'undo',
'redo',
'|',
'outline',
'edit-mode',
{
name: 'save',
tipPosition: 'n',
tip: 'save',
icon: saveIcon,
className: 'right',
click: () => {
saveDoc();
}
},
{
name: 'more',
toolbar: ['both', 'preview', 'devtools']
}
].map((menuItem) => {
if (typeof menuItem === 'string') {
menuItem = { name: menuItem };
}
menuItem.tipPosition = 's';
return menuItem;
});
let linkBase = '';
let editorTheme = {};
let imgConfig = {};
let imgPathPrefix = '';
let filenameList = [];
let vditor;
let options = {
height: window.innerHeight - 20,
mode: 'ir',
outline: {
enable: true,
postition: 'left'
},
cache: { enable: false },
toolbar: toolbar,
toolbarConfig: {
pin: true
},
counter: {
enable: true
},
upload: {
url: '/hhh',
accept: 'image/*',
async handler(files) {
console.info('handleImg');
handleImg(files);
}
},
input(md) {
const timeout = setTimeout(() => {
if (md === vditor.getValue()) {
saveDoc();
}
clearTimeout(timeout);
}, 3000);
},
after() {
vditor.setTheme(options.theme, options.preview.current);
}
};
function initVditor() {
console.info('init vditor with options:');
console.info(options);
vditor = new Vditor('vditor', options);
}
function saveDoc() {
vscode.postMessage({
type: 'save',
text: vditor.getValue()
});
}
let imgName = '';
let imgPath = '';
function handleImg(files) {
createLoading();
// create a new img file from files
let img = files[0];
// img file suffix
let imgSuffix = img.type.slice(6);
// rename img
imgName = `${genImgName()}.${imgSuffix}`;
let pathPrefix;
if (imgConfig.pathType === 'absolute') {
pathPrefix = imgConfig.dirPath;
} else if (imgConfig.pathType === 'relative') {
pathPrefix = imgPathPrefix;
}
imgPath = `${pathPrefix}/${imgName}`;
console.log(imgPath);
let reader = new FileReader();
reader.readAsBinaryString(img);
reader.onloadend = function () {
vscode.postMessage({
type: 'img',
file: reader.result,
imgName
});
};
}
// Handle messages sent from the extension to the webview
window.addEventListener('message', (event) => {
const message = event.data; // The markdown data that the extension sent
console.log('update: ' + message.type);
switch (message.type) {
case 'all':
console.log(message.filenameList);
// console.log('message.options: ');
// console.log(message.options);
Object.assign(options, message.options);
options.hint = {
extend: [
{
key: '/',
hint: (key) => {
return convertFileList(key);
}
}
]
};
console.log(options);
// console.log(convertFileList(message.filenameList, key));
imgConfig = message.imgConfig;
imgPathPrefix = message.imgPathPrefix;
initVditor();
return;
case 'imgSaved':
console.log('insertValue: image');
vditor.insertValue(` ![${imgName}](${imgPath})`);
clearLoading();
return;
case 'picgo':
console.log('picgo: ' + message.urlText);
vditor.insertValue(message.urlText);
clearLoading();
return;
case 'updateFilenameList':
this.filenameList = message.filenameList;
}
});
function convertFileList(pattern) {
const res = this.filenameList
.filter((filePath) => {
return filePath.indexOf(pattern) > -1;
})
.map((filePath) => {
return {
value: filePath,
html: filePath
};
});
return res;
}
function genImgName() {
var date = new Date(Date.now());
return date.getTime();
}
function clearLoading() {
this.document.getElementById('loading').innerHTML = '';
}
function createLoading() {
this.document.getElementById('loading').innerHTML = `
<div style="opacity: 0.8; background-color: #222; position: fixed; width: 100%; height: 100%; top: 0px; left: 0px; z-index: 1000; display: flex; align-items: center; justify-content: center;">
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="72px" height="90px" viewBox="0 0 24 30" style="enable-background:new 0 0 50 50;" xml:space="preserve">
<rect fill="#42a5f5" x="0" y="10" width="4" height="10" fill="#333" opacity="0.2">
<animate attributeName="opacity" attributeType="XML" values="0.2; 1; .2" begin="0s" dur="0.6s" repeatCount="indefinite" />
<animate attributeName="height" attributeType="XML" values="10; 20; 10" begin="0s" dur="0.6s" repeatCount="indefinite" />
<animate attributeName="y" attributeType="XML" values="10; 5; 10" begin="0s" dur="0.6s" repeatCount="indefinite" />
</rect>
<rect fill="#42a5f5" x="8" y="10" width="4" height="10" fill="#333" opacity="0.2">
<animate attributeName="opacity" attributeType="XML" values="0.2; 1; .2" begin="0.15s" dur="0.6s" repeatCount="indefinite" />
<animate attributeName="height" attributeType="XML" values="10; 20; 10" begin="0.15s" dur="0.6s" repeatCount="indefinite" />
<animate attributeName="y" attributeType="XML" values="10; 5; 10" begin="0.15s" dur="0.6s" repeatCount="indefinite" />
</rect>
<rect fill="#42a5f5" x="16" y="10" width="4" height="10" fill="#333" opacity="0.2">
<animate attributeName="opacity" attributeType="XML" values="0.2; 1; .2" begin="0.3s" dur="0.6s" repeatCount="indefinite" />
<animate attributeName="height" attributeType="XML" values="10; 20; 10" begin="0.3s" dur="0.6s" repeatCount="indefinite" />
<animate attributeName="y" attributeType="XML" values="10; 5; 10" begin="0.3s" dur="0.6s" repeatCount="indefinite" />
</rect>
</svg>
</div>`;
}
})();