forked from ahw/excel2wiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
45 lines (39 loc) · 988 Bytes
/
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
var excel = document.getElementById('excel-input')
var wiki = document.getElementById('wiki-output')
excel.addEventListener('focus', function (e) {
e.target.select()
})
wiki.addEventListener('focus', function (e) {
e.target.select()
})
excel.addEventListener('paste', function (e) {
setTimeout(() => {
excel.blur()
}, 200)
})
excel.addEventListener('blur', update, true)
function isEmptyLine (values) {
return (values.length === 1 && values[0].length === 0)
}
function update (e) {
console.log('Updating...')
var text = excel.value
var markup = ''
text.split('\n').forEach(function (line, rowIndex) {
let values = line.split('\t')
if (isEmptyLine(values)) {
return
}
if (rowIndex === 1) {
for (let i = 0; i < values.length; i++) {
markup += '|---'
}
markup += '|\n'
}
values.forEach(function (content, colIndex) {
markup += `|${content}`
})
markup += '|\n'
})
wiki.innerHTML = markup
}