-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsql-element.js
More file actions
161 lines (134 loc) · 4.26 KB
/
sql-element.js
File metadata and controls
161 lines (134 loc) · 4.26 KB
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
import "./vendor/jswasm/sqlite3.mjs";
let setupPromise = null;
async function loadDatabase(dbPath) {
return fetch(dbPath)
.then((response) => response.arrayBuffer())
.then((buffer) => {
const p = sqlite3.wasm.allocFromTypedArray(buffer);
const db = new sqlite3.oo1.DB();
const rc = sqlite3.capi.sqlite3_deserialize(
db.pointer,
"main",
p,
buffer.byteLength,
buffer.byteLength,
sqlite3.capi.SQLITE_DESERIALIZE_FREEONCLOSE
);
db.checkRc(rc);
return db;
})
}
async function setupSqlite3() {
if (!setupPromise) {
setupPromise = window
.sqlite3InitModule()
.then((sqlite3) => (globalThis.sqlite3 = sqlite3));
}
await setupPromise;
}
class SQLTable extends HTMLElement {
constructor() {
super();
}
async connectedCallback() {
await setupSqlite3();
const shadow = this.attachShadow({ mode: "open" });
const style = document.createElement("style");
style.textContent = `
table {
width: 100%;
max-width: 600px;
margin: 0 auto;
border-collapse: separate;
border-spacing: 0;
table-layout: fixed;
font-family: system-ui, sans-serif;
font-size: 16px;
background: #fff;
border: 2px solid #bbb;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}
thead {
background-color: #f0f2f5;
}
thead th {
font-weight: bold;
text-align: left;
padding: 12px 16px;
border-bottom: 2px solid #bbb;
border-right: 1px solid #ddd;
}
thead th:last-child {
border-right: none;
}
tbody td {
padding: 12px 16px;
border-bottom: 1px solid #ddd;
border-right: 1px solid #eee;
}
tbody td:last-child {
border-right: none;
}
tbody tr:last-child td {
border-bottom: none;
}
tbody tr:hover {
background-color: #f9f9f9;
}
table th,
table td {
word-break: break-word;
}
`;
shadow.appendChild(style);
const table = document.createElement("table");
shadow.appendChild(table);
const dbPath = this.getAttribute("database");
const query = this.getAttribute("query");
loadDatabase(dbPath).then((db) => {
let columnNames = new Array();
db.exec({
sql: query,
rowMode: "array",
callback: function (row) {
const tableRow = table.insertRow(-1);
row.forEach((cell, index) => {
tableRow.insertCell(index).innerHTML = cell;
});
},
columnNames: columnNames,
});
var header = table.createTHead();
var headerRow = header.insertRow(0);
columnNames.forEach((name, index) => {
const cell = headerRow.insertCell(index);
cell.innerHTML = name;
cell.outerHTML = `<th>${cell.innerHTML}</th>`;
});
});
}
}
class SQLValue extends HTMLElement {
constructor() {
super();
}
async connectedCallback() {
await setupSqlite3();
const dbPath = this.getAttribute("database");
const query = this.getAttribute("query");
const element = this;
loadDatabase(dbPath).then((db) => {
db.exec({
sql: query,
rowMode: "$value",
callback: function (value) {
element.innerHTML = `<span>${value}</span>`;
},
});
});
}
}
customElements.define("sql-table", SQLTable);
customElements.define("sql-value", SQLValue);