forked from donatj/CsvToMarkdownTable
-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.html
121 lines (102 loc) · 3.47 KB
/
example.html
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
<html>
<style type="text/css">
textarea {
font-family: monospace;
}
</style>
<script src="lib/CsvToMarkdown.js"></script>
<script>
"use strict";
window.addEventListener('DOMContentLoaded', function () {
var insertAtCursor = function (myField, myValue) {
//IE support
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
//MOZILLA/NETSCAPE support
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
myField.selectionEnd = myField.selectionStart = startPos + myValue.length;
} else {
myField.value += myValue;
}
};
var input = document.getElementById('tsv-input');
var output = document.getElementById('table-output');
var headerCheckbox = document.getElementById('has-headers');
var delimiterMarker = document.getElementById('delimiter-marker');
var headerStyle = document.getElementById('header-style');
var getDelimiter = function () {
var delim = delimiterMarker.value;
if (delim == 'tab') {
delim = "\t";
}
return delim;
};
var getheaderStyle = function () {
let style = headerStyle.value;
let useDoubleHeader = false;
if (style == '||') {
useDoubleHeader = true;
}
return useDoubleHeader;
};
var populateData = document.getElementById('populate-data');
input.addEventListener('keydown', function (e) {
if (e.key.toLowerCase() == 'tab') {
e.preventDefault();
insertAtCursor(e.target, "\t");
}
});
var renderTable = function () {
var value = input.value.trim();
var hasHeader = headerCheckbox.checked;
output.value = csvToMarkdown(value, getDelimiter(), hasHeader, getheaderStyle());
};
input.addEventListener('keyup', renderTable);
headerCheckbox.addEventListener('change', renderTable);
delimiterMarker.addEventListener('change', renderTable);
headerStyle.addEventListener('change', renderTable);
populateData.addEventListener('change', function () {
input.value = populateData.value.split("|").join(getDelimiter()) + "\n";
headerCheckbox.checked = true;
populateData.selectedIndex = 0;
renderTable();
if (typeof input.selectionStart != 'undefined') {
input.select();
input.selectionEnd = input.selectionStart = input.value.length;
}
});
output.addEventListener('click', function (e) {
e.target.select();
});
renderTable();
});
</script>
<body>
<textarea style="width: 100%; height: 200px;" id="tsv-input"></textarea>
<label><input type="checkbox" id="has-headers" /> Use first line as headers</label>
<select id="header-style">
<option value="|---|">Markdown</option>
<option value="||">Confluence Markup</option>
</select>
<select id="delimiter-marker">
<option value="tab">Tab Separated</option>
<option value=",">Comma Separated</option>
<option value=";">Semicolon Separated</option>
</select>
<select id="populate-data">
<option>-- Populate With --</option>
<option value="id|select_type|table|type|possible_keys|key|key_len|ref|rows|extra">MySQL EXPLAIN Headers</option>
<option value="id|select_type|table|type|possible_keys|key|key_len|ref|rows|filtered|extra">MySQL EXPLAIN EXTENDED
Headers
</option>
</select>
<hr>
<textarea style="width: 100%; height: 200px;" id="table-output" readonly></textarea>
</body>
</html>