-
Notifications
You must be signed in to change notification settings - Fork 1
/
text drag.html
196 lines (164 loc) · 4.03 KB
/
text drag.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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>text drag</title>
<style>
body
{
font-size: 80%;
}
#source
{
white-space: pre;
margin-left: 20px;
font-family: monaco;
font-size: 7pt;
border: solid 1px #888;
}
#dump
{
white-space: pre;
}
#escaped
{
white-space: normal;
color: #888;
font-family: helvetica neue;
position: absolute;
z-index: 1;
color: red;
}
</style>
</head>
<body>
<h1>text drag</h1>
<pre>
</pre>
<div contentEditable='true' id='source'>
<span id='line-1'>line 1, this is some text</span>
<span id='line-2'>line 2, this is some text</span>
<span id='line-3'>line 3, this is some text</span>
<span id='line-4'>line 4, this is some text</span>
<span id='line-5'>line 5, this is some text</span>
<span id='line-7'>line 7, this is some text</span>
<span id='line-9'>line 9, this is some text</span>
<span id='line-10'>line 10, this is some text</span>
<span id='line-11'>line 11, this is some text</span>
<span id='line-12'>line 12, this is some text</span>
<span id='line-13'>line 13, this is some text</span>
<span id='line-14'>line 14, this is some text</span>
<span id='line-16'>line 16, this is some text</span>
<span id='line-17'>line 17, this is some text</span>
<span id='line-18'>line 18, this is some text</span>
<span id='line-19'>line 19, this is some text</span>
<span id='line-20'>line 20, this is some text</span>
</div>
<p>-</p>
<button onclick='clear2()'>Clear</button>
<p>-</p>
<p>-</p>
<p>-</p>
<div id='dump'>-</div>
<p>-</p>
<p>-</p>
<p>-</p>
<div id='dump2'>-</div>
<script>
function getTickCount()
{
return (new Date).getTime()
}
function dumpHash(o)
{
var str = ''
for (var i in o) str += i + '=' + (o[i]) + '\n'
return str
}
function subtreeModified()
{
document.getElementById('dump').innerHTML = 'DOM Changed at ' + getTickCount() + ' ' + event.target + '(' + event.nodeValue + ')'
}
var n = document.getElementById('source')
document.addEventListener("DOMSubtreeModified", subtreeModified, false)
function lineNodeFromNode(node)
{
while (node)
{
if (node.nodeType == 1 && node.className == 'line')
return node
node = node.parentNode
}
}
function lineNumberFromNode(node)
{
node = lineNodeFromNode(node)
if (!node) return
// return parseFloat(node.id.match(/\d+/))
var i = 0
node = node.previousSibling
while (node)
{
i++
node = node.previousSibling
}
return i
}
function clear2()
{
document.getElementById('dump').innerHTML = ''
document.getElementById('dump2').innerHTML = ''
}
function drag(e)
{
try
{
event.type
}
catch (ex)
{
event = e
// alert(dumpHash(event))
}
var str = getTickCount() + ' ' + event.type
if (event.type == 'dragstart')
{
clear()
}
// alert(dumpHash(event))
// if (event.type )
// document.getElementById('dump2').innerHTML = str
var str = event.type
if (event.type == 'dragover')
{
var sel = getSelection()
var t = sel.type //+ ' l=' + sel.anchorNode.nodeValue
str += '<span style="color: red">' + t + '</span>'
// alert(dumpHash(event))
}
if (event.type == 'dragover')
{
var t = event.srcElement.innerHTML
str += '<span style="color: lime">' + t + '</span>'
}
str += ' '
document.getElementById('dump2').innerHTML += str
}
var source = document.getElementById('source')
source.ondrag = drag
source.ondragenter = drag
source.ondragleave = drag
source.ondragover = drag
source.ondragstart = drag
source.ondrop = drag
source.onmousedown = drag
source.onmouseup = drag
source.onmousemove = drag
// document.documentElement.ondrop = drag
</script>
<pre>
check selection -> not updated
DOMSubtreeModified is called when drop is successful
dragover shows CORRECT line
</body>
</html>