-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor1.html
260 lines (255 loc) · 9.21 KB
/
editor1.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
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
<html>
<head>
<meta charset="utf-8" />
<title>editor</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="full-screen" content="true" />
<style>
:root {
--line-height: 19px;
}
* {
margin: 0;
padding: 0;
}
#editor {
position: relative;
width: calc(100%-40px);
height: 500px;
font-family: Consolas, "Courier New", monospace;
/* font-family: ui-monospace,SFMono-Regular,SF Mono,Menlo,Consolas,Liberation Mono,monospace; */
background-color: #eee;
user-select: none;
cursor: text;
margin: 20px;
}
.view-lines {
font-size: 14px;
}
.view-line {
/* font-family: Consolas, "Courier New", monospace; */
height: var(--line-height);
}
textarea {
position: absolute;
top: 0;
left: 0;
width: 1px;
height: 1px;
opacity: 0;
}
/* 设置光标的样式 */
.cursor {
position: absolute;
display: inline-block;
width: 2px;
height: var(--line-height);
top: 0;
left: 0;
background-color: black;
animation: blink 1s infinite;
}
/* 设置光标的闪烁效果 */
@keyframes blink {
50% {
opacity: 0;
}
}
</style>
</head>
<body>
<div id="editor">
<div class="view-lines"></div>
<textarea></textarea>
<span class="cursor"></span>
</div>
<script>
let exampleDoc = "function add(a, b) {\n return a + b \n}"
function renderDoc(lines) {
const coloredLines = document.createDocumentFragment()
for (let i = 0; i < lines.length; i++) {
const line = document.createElement("div")
line.className = "view-line"
line.innerHTML = lines[i]
.replace(/ /g, " ")
.replace(/\t/g, " ")
.replace(/function/g, '<span style="color: blue;">function</span>')
.replace(/return/g, '<span style="color: purple;">return</span>')
.replace(/\(/g, '<span style="color: blue;">(</span>')
.replace(/\)/g, '<span style="color: blue;">)</span>')
coloredLines.appendChild(line)
}
return coloredLines
}
// 尝试将以上代码封装成一个类,对外接口主要是 doc 文本内容,以及 renderDoc 的方法
class Editor {
constructor(rootEl, doc, renderer) {
this.rootEl = rootEl
this.doc = doc
this.renderer = renderer
this.lines = this.doc.split("\n")
this.pagination = {
line: 0,
column: 0,
}
this.position = {
left: 0,
top: 0,
}
this.charWidth = 7.7
this.charHeight = 16.5 // 19-16.5=2.5
this.lineHeight = 19
this.cursor = null
this.content = null
this.textarea = null
this.init()
}
init() {
this.getCharSize()
this.rootEl.style.setProperty("--line-height", this.lineHeight + "px")
this.cursor = this.rootEl.querySelector(".cursor")
this.content = this.rootEl.querySelector(".view-lines")
this.textarea = this.rootEl.querySelector("textarea")
this.textarea.addEventListener("keydown", this.handleKeydown.bind(this))
this.rootEl.addEventListener("click", (e) => {
// 根据点击的位置计算光标的位置,更新光标的位置,并且更新line和column
const x = e.clientX - this.content.getBoundingClientRect().left
const y = e.clientY - this.content.getBoundingClientRect().top
const line = Math.floor(y / this.lineHeight)
if (!this.lines[line]) return
const text = this.lines[line]
const column = Math.floor(x / this.charWidth) > text.length ? text.length : Math.floor(x / this.charWidth)
this.position.left = text.slice(0, column).length * this.charWidth
this.position.top = line * this.lineHeight
this.pagination.line = line
this.pagination.column = column
this.updatePosition()
})
if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
this.textarea.addEventListener("input", (e) => {
const text = e.target.value
this.insertText(text)
e.target.value = ""
})
}
this.updateLines()
}
getCharSize() {
const span = document.createElement("span")
span.innerHTML = "a"
span.style.fontSize = "14px"
span.style.fontFamily = 'Consolas, "Courier New", monospace'
document.body.appendChild(span)
this.charWidth = Number(span.getBoundingClientRect().width.toFixed(1))
this.charHeight = Number(span.getBoundingClientRect().height.toFixed(1))
this.lineHeight = Number(2.5 + this.charHeight).toFixed(1)
document.documentElement.style.setProperty("--line-height", this.lineHeight + "px")
document.body.removeChild(span)
}
updateCursor() {
this.cursor.style.left = this.position.left + "px"
this.cursor.style.top = this.position.top + "px"
this.textarea.style.left = this.position.left + "px"
this.textarea.style.top = this.position.top + "px"
this.textarea.focus()
}
updatePosition() {
const text = this.lines[this.pagination.line]
text.length > this.pagination.column ? (this.position.left = this.pagination.column * this.charWidth) : (this.position.left = text.length * this.charWidth)
this.position.top = this.pagination.line * this.lineHeight
this.updateCursor()
}
moveLeft() {
if (this.pagination.column > 0) {
this.pagination.column--
} else if (this.pagination.line > 0) {
this.pagination.line--
this.pagination.column = this.lines[this.pagination.line].length
}
this.updatePosition()
}
moveRight() {
if (this.pagination.column < this.lines[this.pagination.line].length) {
this.pagination.column++
} else if (this.pagination.line < this.lines.length - 1) {
this.pagination.line++
this.pagination.column = 0
}
this.updatePosition()
}
moveUp() {
if (this.pagination.line > 0) {
this.pagination.line--
}
this.updatePosition()
}
moveDown() {
if (this.pagination.line < this.lines.length - 1) {
this.pagination.line++
}
this.updatePosition()
}
updateLines() {
this.content.innerHTML = ""
this.content.appendChild(this.renderer(this.lines))
}
insertText(text) {
if (text === "\n") {
const line = this.lines[this.pagination.line]
const left = line.slice(0, this.pagination.column)
const right = line.slice(this.pagination.column)
this.lines[this.pagination.line] = left
this.lines.splice(this.pagination.line + 1, 0, right)
this.pagination.line++
this.pagination.column = 0
} else {
this.lines[this.pagination.line] = this.lines[this.pagination.line].slice(0, this.pagination.column) + text + this.lines[this.pagination.line].slice(this.pagination.column)
this.pagination.column += text.length
}
this.updateLines()
this.updatePosition()
}
deleteText() {
if (this.pagination.column > 0) {
this.lines[this.pagination.line] = this.lines[this.pagination.line].slice(0, this.pagination.column - 1) + this.lines[this.pagination.line].slice(this.pagination.column)
this.pagination.column--
} else if (this.pagination.line > 0) {
this.pagination.line--
this.pagination.column = this.lines[this.pagination.line].length
this.lines[this.pagination.line] = this.lines[this.pagination.line].slice(0, this.pagination.column) + this.lines[this.pagination.line + 1]
this.lines.splice(this.pagination.line + 1, 1)
}
this.updateLines()
this.updatePosition()
}
handleKeydown(event) {
switch (event.key) {
case "ArrowLeft":
this.moveLeft()
break
case "ArrowRight":
this.moveRight()
break
case "ArrowUp":
this.moveUp()
break
case "ArrowDown":
this.moveDown()
break
case "Backspace":
this.deleteText()
break
case "Enter":
this.insertText("\n")
break
default:
if (event.key.length === 1) {
this.insertText(event.key)
}
}
}
}
const editor = new Editor(document.querySelector("#editor"), exampleDoc, renderDoc)
</script>
</body>
</html>