-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_tab_char.html
212 lines (189 loc) · 6.78 KB
/
create_tab_char.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
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
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>制表符字符生成器</title>
<style>
body {
width: 100vw;
height: 100vh;
padding: 0;
margin: 0;
flex-direction: column;
display: flex;
justify-content: space-around;
align-items: center;
}
.row {
display: flex;
}
.block {
width: 20px;
height: 20px;
text-align: center;
margin: 0;
padding: 0;
}
.block:hover{
background-color: #999;
}
.cell {
display: inline-block;
border: 0.5px solid #999;
cursor: pointer;
}
.floating {
position: absolute;
border: 1px solid #000;
background: #fff;
cursor: pointer;
}
</style>
</head>
<body>
<div>
<label for="rows">行数:</label>
<input type="number" id="rows" min="1" value="8">
<br>
<label for="columns">列数:</label>
<input type="number" id="columns" min="1" value="8">
<br>
<button id="generate">生成矩阵</button>
<input type="text" id="char_name">
<button id="copy">复制矩阵</button>
</div>
<div id="array">
</div>
<div>
<textarea id="textarea" rows="10" cols="30" readonly></textarea>
</div>
</body>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function () {
const charMap = {
'q': '┏', 'w': '┳', 'e': '┓',
'a': '┣', 'd': '┫',
'z': '┗', 'x': '┻', 'c': '┛',
'r': '━', 'f': '┃', 's': '╋', 'v': ' ',
};
const chars = Object.values(charMap); // 生成悬浮标签时用到的字符数组
const positions = [
{ top: -37, left: -21 }, { top: -37, left: 0 }, { top: -37, left: 21 },
{ top: -16, left: -21 }, { top: -16, left: 21 },
{ top: 5, left: -21 }, { top: 5, left: 0 }, { top: 5, left: 21 },
{ top: -58, left: 0 }, { top: -16, left: -42 }, { top: 26, left: 0 }, { top: -16, left: 42 },
];
let activeCell = null;
let initialText = " ";
let keyTriggered = false;
function build_array() {
const rows = parseInt($("#rows").val());
const columns = parseInt($("#columns").val());
createCellMatrix(rows, columns);
}
function createCellMatrix(rows, columns) {
const array = $("#array");
const currentRows = array.children(".row").length;
const currentColumns = currentRows > 0 ? array.children(".row").first().children(".cell").length : 0;
// 保留现有的 cell 数据
const cellData = [];
array.children(".row").each(function () {
const rowData = [];
$(this).children(".cell").each(function () {
rowData.push($(this).text());
});
cellData.push(rowData);
});
// 清空当前的 array
array.empty();
// 创建新的 cell 矩阵
for (let i = 0; i < rows; i++) {
const row = $("<div>").addClass("row");
for (let j = 0; j < columns; j++) {
const cell = $("<p>").addClass("cell").addClass("block");
// 如果是在原有的范围内,保留数据
if (i < currentRows && j < currentColumns) {
cell.text(cellData[i][j]);
} else {
cell.text(" ");
}
row.append(cell);
}
array.append(row);
}
updateTextarea(); // 更新 textarea 内容
}
function updateTextarea() {
let content = "";
$("#array .row").each(function () {
$(this).children(".cell").each(function () {
// content += $(this).text();
if ($(this).text() == " ") {
content += " ";
} else {
content += $(this).text();
}
});
content += "\n";
});
$("#textarea").val(content);
}
$(document).on("mousedown", "p.cell", function (event) {
event.preventDefault();
const cell = $(this);
const cellPosition = cell.position();
initialText = cell.text(); // 保存初始文字
keyTriggered = false; // 重置键盘触发标志
// 创建悬浮标签
positions.forEach((pos, index) => {
const floating = $("<p>").addClass("floating").addClass("block").text(chars[index]);
floating.css({
top: cellPosition.top + pos.top - 0.5,
left: cellPosition.left + pos.left - 0.5
});
floating.mouseup(function () {
cell.text($(this).text());
$(".floating").remove();
updateTextarea(); // 更新 textarea 内容
});
$("body").append(floating);
});
// 设置当前激活的 cell
activeCell = cell;
// 在 cell 上松开鼠标时恢复初始文字
cell.mouseup(function () {
if (!keyTriggered) {
cell.text(initialText);
$(".floating").remove();
cell = null; // 清除激活状态
updateTextarea(); // 更新 textarea 内容
}
});
});
$(document).on("keydown", function (event) {
if (activeCell && charMap[event.key]) {
activeCell.text(charMap[event.key]);
activeCell = null; // 清除激活状态
keyTriggered = true; // 设置键盘触发标志
$(".floating").remove();
updateTextarea(); // 更新 textarea 内容
}
});
build_array();
$("#generate").click(function () {
build_array();
});
$("#copy").click(function () {
const textarea = $("#textarea");
const copy_str = textarea.val();
const char_name = $("#char_name").val();
const new_copy_str = `"${char_name}":"\n${copy_str}"`.replaceAll(" ", " ")
textarea.val(new_copy_str);
textarea.select();
document.execCommand("copy");
textarea.val(copy_str);
});
});
</script>
</html>