-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-table.html
183 lines (174 loc) · 5.33 KB
/
json-table.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>JSON table module</title>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/base.min.css"
integrity="sha256-ssLR+FMaogZBsdMJRkhlQqndXF0uoEd5BArJz/ftia4="
crossorigin="anonymous"
/>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/components.min.css"
integrity="sha256-8n6uuc2/UKJdr+2vLhORSI0Jc7bIY9nOpbZp2kmYZ8Y="
crossorigin="anonymous"
/>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/@tailwindcss/[email protected]/dist/typography.min.css"
integrity="sha256-Bzpo9sIAOqjJGDQ0XjfcgkXcGq01xeaudbvMA10ojDw="
crossorigin="anonymous"
/>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/utilities.min.css"
integrity="sha256-1xEMVyf5Obn9PZAEuVHiXzL4+QSO8bevhpynv2LgQUg="
crossorigin="anonymous"
/>
</head>
<body class="px-2">
<div id="app">
<div class="prose max-w-full">
<div class="py-2 sticky top-0 bg-white">
<div>{{ status }}</div>
<div v-if="data">
Viewing rows {{ offset + 1 }} – {{ Math.min(total, offset + limit)
}}. (Use <kbd>j</kbd> and <kbd>k</kbd> to navigate.)
</div>
</div>
<table v-if="data" ref="table">
<thead>
<tr>
<th>#</th>
<th>Contents</th>
</tr>
</thead>
<tbody>
<tr v-for="(row, index) in slice" :key="index">
<th scope="row">{{ index + offset + 1 }}</th>
<td>{{ row }}</td>
</tr>
</tbody>
</table>
</div>
</div>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.global.js"
integrity="sha256-mYrFFuhl9a7dhhIglG0BXjgCHrvV0Kol3TTbnnfghe4="
crossorigin="anonymous"
></script>
<script>
parent.postMessage(
{
register: {
title: document.title,
inputs: ['file'],
outputs: [],
},
},
'*',
)
/// <reference types="./types" />
const app = Vue.createApp({
setup() {
const data = Vue.shallowRef(null)
const offset = Vue.ref(0)
const table = Vue.ref(null)
const limit = Vue.ref(20)
const total = Vue.computed(() => {
return data ? data.value.length : 0
})
const slice = Vue.computed(() => {
return (
data && data.value.slice(offset.value, offset.value + limit.value)
)
})
const mode = Vue.ref('expand')
const targetUp = Vue.ref(null)
let queued
Vue.watch([offset, table], () => {
mode.value = 'expand'
if (!queued) queued = setTimeout(updateLimit)
})
const updateLimit = () => {
if (!table.value) return
queued = null
if (mode.value === 'expand') {
if (table.value.offsetHeight <= 720) {
limit.value++
if (!queued) queued = setTimeout(updateLimit)
} else {
mode.value = 'collapse'
if (!queued) queued = setTimeout(updateLimit)
}
} else if (mode.value === 'collapse') {
if (table.value.offsetHeight > 720 && limit.value > 1) {
limit.value--
if (!queued) queued = setTimeout(updateLimit)
} else if (
targetUp.value != null &&
offset.value + limit.value > targetUp.value &&
offset.value > 0
) {
offset.value -= 1
} else {
targetUp.value = null
mode.value = 'stall'
}
}
}
Vue.onMounted(() => {
updateLimit()
window.onkeydown = (e) => {
if (e.key === 'j') {
offset.value += limit.value
} else if (e.key === 'k') {
targetUp.value = offset.value
offset.value -= 1
if (offset.value < 0) offset.value = 0
}
}
})
return {
status: Vue.ref('Initializing...'),
data,
slice,
offset,
limit,
total,
table,
}
},
})
const vm = app.mount('#app')
const setStatus = (x) => {
vm.status = x
}
onmessage = async (e) => {
if (e.data.input) {
const file = e.data.input.file
if (!file) {
setStatus('No input file')
return
}
setStatus('Reading ' + file.name)
try {
const data = JSON.parse(await file.text())
if (!Array.isArray(data)) {
setStatus('Not an array.')
return
}
setStatus('Read ' + data.length + ' records.')
vm.data = data
} catch (error) {
setStatus(`Failed: ${error}`)
console.error(error)
}
}
}
</script>
</body>
</html>