-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.html
166 lines (153 loc) · 4.9 KB
/
play.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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>tinymorph | toy implementation</title>
<style>
#editor-container {
display: flex;
font-family: monospace;
font-size: 14px;
line-height: 1.5;
position: relative;
}
#line-numbers {
width: 40px;
text-align: right;
padding-right: 10px;
color: #999;
}
#editor {
flex-grow: 1;
white-space: pre-wrap;
word-wrap: break-word;
outline: none;
padding: 0;
margin: 0;
}
#inlay-hints {
position: absolute;
pointer-events: none;
color: #888;
}
</style>
</head>
<body>
<div id="editor-container">
<div id="line-numbers"></div>
<div id="editor" contenteditable="true" spellcheck="false"></div>
<div id="inlay-hints"></div>
</div>
<script>
const editor = document.getElementById("editor")
const lineNumbers = document.getElementById("line-numbers")
const inlayHints = document.getElementById("inlay-hints")
let ws
let debounceTimer
function updateLineNumbers() {
const lines = editor.innerText.split("\n")
lineNumbers.innerHTML = lines.map((_, i) => i + 1).join("<br>")
}
function getCaretPosition() {
const selection = window.getSelection()
if (selection.rangeCount === 0) return null
const range = selection.getRangeAt(0)
const preCaretRange = range.cloneRange()
preCaretRange.selectNodeContents(editor)
preCaretRange.setEnd(range.endContainer, range.endOffset)
return preCaretRange.toString().length
}
function setCaretPosition(pos) {
const range = document.createRange()
const sel = window.getSelection()
let currentPos = 0
let found = false
function traverseNodes(node) {
if (found) return
if (node.nodeType === Node.TEXT_NODE) {
if (currentPos + node.length >= pos) {
range.setStart(node, pos - currentPos)
range.setEnd(node, pos - currentPos)
found = true
} else {
currentPos += node.length
}
} else {
for (let child of node.childNodes) {
traverseNodes(child)
}
}
}
traverseNodes(editor)
sel.removeAllRanges()
sel.addRange(range)
}
// function updateInlayHints() {
// // This is a placeholder for the inlay hints functionality
// // We'll implement this later when we add the streaming suggestion feature
// const cursorPosition = editor.selectionStart
// const text = editor.value.substring(0, cursorPosition)
//
// // For now, let's just display the current word count as a simple hint
// const wordCount = text.trim().split(/\s+/).length
// inlayHints.textContent = `Words: ${wordCount}`
// }
//
// editor.addEventListener("scroll", () => {
// lineNumbers.scrollTop = editor.scrollTop
// inlayHints.style.top = -editor.scrollTop + "px"
// })
//
// // Initialize
// updateLineNumbers()
// updateInlayHints()
function updateInlayHints() {
clearTimeout(debounceTimer)
debounceTimer = setTimeout(() => {
const caretPos = getCaretPosition()
if (caretPos !== null) {
const text = editor.innerText.substring(0, caretPos)
ws.send(JSON.stringify({ text, caretPos }))
}
}, 100)
}
function initWebSocket() {
ws = new WebSocket("ws://your-websocket-server-url")
ws.onmessage = (event) => {
const data = JSON.parse(event.data)
if (data.contents && data.contents.delta) {
const caretPos = getCaretPosition()
if (caretPos !== null) {
const rect = getCaretCoordinates()
inlayHints.style.left = `${rect.left}px`
inlayHints.style.top = `${rect.top}px`
inlayHints.textContent = data.contents.delta
}
}
}
}
function getCaretCoordinates() {
const range = window.getSelection().getRangeAt(0)
const rect = range.getBoundingClientRect()
return {
left: rect.left + window.scrollX,
top: rect.top + window.scrollY,
}
}
editor.addEventListener("input", () => {
updateLineNumbers()
updateInlayHints()
})
editor.addEventListener("keydown", (e) => {
if (e.key === "Tab") {
e.preventDefault()
document.execCommand("insertHTML", false, " ")
}
})
// Initialize
updateLineNumbers()
initWebSocket()
</script>
</body>
</html>