-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarkdownTableGenerator.html
More file actions
365 lines (326 loc) · 11.5 KB
/
Copy pathMarkdownTableGenerator.html
File metadata and controls
365 lines (326 loc) · 11.5 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Markdown表格生成器 - JustHTML 只需网页</title>
<script>
/**
* 动态加载(强制去除缓存) CSS 的函数
* @param {string} src - 要加载的 CSS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadStyle(src) {
return new Promise((resolve, reject) => {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
link.async = true;
link.onload = resolve;
link.onerror = reject;
document.head.appendChild(link);
});
}
/**
* 动态加载(强制去除缓存) JS 的函数
* @param {string} src - 要加载的 JS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadScript(src) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
script.async = true;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
</script>
<script>
loadStyle('../assets/style.css')
let scriptArray = [
"../assets/AddHeader_Tools.js",
"../assets/AddFooter.js",
"../assets/AddIco.js",
"../assets/AutoRun.js",
]
scriptArray.forEach(script => {
loadScript(script);
});
</script>
<script src="../package/Vue/code/vue.global.prod.js"></script>
<script type="module" src="../package/Sober/code/main.js"></script>
<style>
.table-generator {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin: 20px 0;
}
@media (max-width: 768px) {
.table-generator {
grid-template-columns: 1fr;
}
}
.input-group {
margin-bottom: 15px;
}
.input-group label {
display: block;
margin-bottom: 5px;
color: var(--accent-color);
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin: 15px 0;
}
.info-box {
background-color: var(--bg-tertiary);
padding: 10px;
border-radius: var(--border-radius);
margin: 10px 0;
}
</style>
</head>
<body>
<div id="app">
<div class="container">
<a href="../index.html" class="back-link">← 返回首页</a>
<div class="tool-section">
<h2>Markdown表格生成器</h2>
<p>生成Markdown格式的表格</p>
<div class="info-box">
<p><strong>使用说明:</strong></p>
<p>在左侧设置表格结构,在右侧预览生成的Markdown表格</p>
</div>
<div>
<div class="table-generator">
<div>
<div class="input-group">
<label for="columns">列数</label>
<input
type="number"
id="columns"
v-model.number="columns"
min="1"
max="10"
@change="updateTable"
placeholder="输入列数"
>
</div>
<div class="input-group">
<label>表头</label>
<div v-for="(header, index) in headers" :key="'header-' + index" class="input-group">
<label>列 {{ index + 1 }}</label>
<input
type="text"
v-model="headers[index]"
@input="updateTable"
:placeholder="'表头 ' + (index + 1)"
>
</div>
</div>
<div class="input-group">
<label>行数</label>
<input
type="number"
v-model.number="rows"
min="1"
max="20"
@change="updateTable"
placeholder="输入行数"
>
</div>
<div class="input-group">
<label>表格内容</label>
<div v-for="(row, rowIndex) in tableData" :key="'row-' + rowIndex">
<div v-for="(cell, colIndex) in row" :key="'cell-' + rowIndex + '-' + colIndex" class="input-group">
<label>第 {{ rowIndex + 2 }} 行, 第 {{ colIndex + 1 }} 列</label>
<input
type="text"
v-model="tableData[rowIndex][colIndex]"
@input="updateTable"
:placeholder="'单元格 (' + (rowIndex + 2) + ',' + (colIndex + 1) + ')'"
>
</div>
</div>
</div>
</div>
<div>
<div class="input-group">
<label for="generatedTable">生成的表格</label>
<textarea
id="generatedTable"
:value="generatedTable"
readonly
rows="15"
style="font-family: monospace;"
></textarea>
<div class="controls">
<button @click="copyTable">复制表格</button>
<button @click="resetTable">重置表格</button>
</div>
</div>
<div class="input-group">
<label>表格预览</label>
<div class="preview-container" v-html="tablePreview" style="background-color: var(--bg-tertiary); padding: 15px; border-radius: var(--border-radius);"></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
const { createApp, ref, computed } = Vue;
createApp({
setup() {
const columns = ref(3);
const rows = ref(3);
const headers = ref(['列 1', '列 2', '列 3']);
const tableData = ref([
['', '', ''],
['', '', ''],
['', '', '']
]);
// 当列数改变时更新表头和表格数据
const updateTable = () => {
// 更新表头
if (columns.value > headers.value.length) {
// 添加新列的表头
for (let i = headers.value.length; i < columns.value; i++) {
headers.value.push(`列 ${i + 1}`);
}
} else if (columns.value < headers.value.length) {
// 删除多余的表头
headers.value = headers.value.slice(0, columns.value);
}
// 更新表格数据
if (columns.value > tableData.value[0].length) {
// 为每一行添加新列
for (let r = 0; r < tableData.value.length; r++) {
for (let c = tableData.value[r].length; c < columns.value; c++) {
tableData.value[r].push('');
}
}
} else if (columns.value < tableData.value[0].length) {
// 删除多余的列
for (let r = 0; r < tableData.value.length; r++) {
tableData.value[r] = tableData.value[r].slice(0, columns.value);
}
}
// 更新行数
if (rows.value > tableData.value.length) {
// 添加新行
for (let r = tableData.value.length; r < rows.value; r++) {
const newRow = [];
for (let c = 0; c < columns.value; c++) {
newRow.push('');
}
tableData.value.push(newRow);
}
} else if (rows.value < tableData.value.length) {
// 删除多余的行
tableData.value = tableData.value.slice(0, rows.value);
}
};
// 生成Markdown表格
const generatedTable = computed(() => {
if (columns.value <= 0 || rows.value <= 0) {
return '';
}
let table = '';
// 添加表头
table += '| ' + headers.value.slice(0, columns.value).join(' | ') + ' |\n';
// 添加分隔行
table += '|' + headers.value.slice(0, columns.value).map(() => ' --- ').join('|') + '|\n';
// 添加数据行
for (let r = 0; r < rows.value; r++) {
if (r < tableData.value.length) {
const rowData = tableData.value[r].slice(0, columns.value);
table += '| ' + rowData.join(' | ') + ' |\n';
} else {
// 如果数据不足,添加空行
const emptyRow = Array(columns.value).fill('');
table += '| ' + emptyRow.join(' | ') + ' |\n';
}
}
return table;
});
// 生成HTML表格预览
const tablePreview = computed(() => {
if (columns.value <= 0 || rows.value <= 0) {
return '<p>请设置表格参数</p>';
}
let html = '<table style="width:100%; border-collapse: collapse;">';
// 添加表头
html += '<thead><tr>';
for (let c = 0; c < columns.value; c++) {
html += `<th style="border: 1px solid var(--bg-tertiary); padding: 8px; background-color: var(--bg-secondary);">${headers.value[c]}</th>`;
}
html += '</tr></thead>';
// 添加数据行
html += '<tbody>';
for (let r = 0; r < rows.value; r++) {
html += '<tr>';
if (r < tableData.value.length) {
for (let c = 0; c < columns.value && c < tableData.value[r].length; c++) {
html += `<td style="border: 1px solid var(--bg-tertiary); padding: 8px;">${tableData.value[r][c] || ''}</td>`;
}
} else {
// 如果数据不足,添加空单元格
for (let c = 0; c < columns.value; c++) {
html += '<td style="border: 1px solid var(--bg-tertiary); padding: 8px;"></td>';
}
}
html += '</tr>';
}
html += '</tbody></table>';
return html;
});
const copyTable = () => {
navigator.clipboard.writeText(generatedTable.value).then(() => {
alert('表格已复制到剪贴板!');
}).catch(err => {
console.error('复制失败: ', err);
// 降级方案:使用旧方法
const textArea = document.createElement('textarea');
textArea.value = generatedTable.value;
document.body.appendChild(textArea);
textArea.select();
document.execCommand('copy');
document.body.removeChild(textArea);
alert('表格已复制到剪贴板!');
});
};
const resetTable = () => {
columns.value = 3;
rows.value = 3;
headers.value = ['列 1', '列 2', '列 3'];
tableData.value = [
['', '', ''],
['', '', ''],
['', '', '']
];
};
// 初始化表格
updateTable();
return {
columns,
rows,
headers,
tableData,
generatedTable,
tablePreview,
updateTable,
copyTable,
resetTable
};
}
}).mount('#app');
</script>
</body>
</html>