-
Notifications
You must be signed in to change notification settings - Fork 1
/
linenumbering.js
191 lines (178 loc) · 5.89 KB
/
linenumbering.js
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
var lineObjOffsetTop = 2;
var MAXLINES=250;
/**
createTextAreaWithLines is a function which transform the content
of an html element in line numbered content.
id: the id of the object tob transformed
*/
function createTextAreaWithLines(id,maxlines)
{
var el = document.createElement('div'); // create div holding ta and linenumber table
var ta = document.getElementById(id);
ta.parentNode.insertBefore(el,ta); // insert el before the ta
el.appendChild(ta); // add ta to the div
el.className='textAreaWithLines'; // set the style
ta.style.position = 'absolute'; // give ta an absolute position in el
ta.style.left = '32px'; // 30 px to the right
ta.style.left = '32px'; // 30 px to the right
ta.style.margin = '0px';
el.style.left = '0px';
el.style.height = (ta.offsetHeight + 0) + 'px';
el.style.overflow='hidden';
el.style.position = 'relative';
if(el.parentNode.offsetWidth < ta.offsetWidth + 32)
{
el.parentNode.style.width= (ta.offsetWidth + 32) + 'px';
}
else
{
el.style.width = (ta.offsetWidth + 32) + 'px';
}
var lineObj = document.createElement('div');
lineObj.className='lineObj';
lineObj.style.position = 'absolute';
lineObj.style.top = lineObjOffsetTop + 'px';
lineObj.style.left = '0px';
lineObj.style.width = '29px';
lineObj.style.padding = '0px';
el.insertBefore(lineObj,ta);
lineObj.style.textAlign = 'right';
var string = '';
for(var no=1;no<MAXLINES;no++){
if(string.length>0)string = string + '<br>';
string = string + no;
}
ta.onkeydown = function() { positionLineObj(lineObj,ta); };
ta.onmousedown = function() { positionLineObj(lineObj,ta); };
ta.onscroll = function() { positionLineObj(lineObj,ta); };
ta.onblur = function() { positionLineObj(lineObj,ta); };
ta.onfocus = function() { positionLineObj(lineObj,ta); };
ta.onmouseover = function() { positionLineObj(lineObj,ta); };
lineObj.innerHTML = string;
// limit number of lines in el to maxlines
if( maxlines && !isNaN(maxlines))
{
var lineHeight = calculateLineHeight(ta);
el.style.height=(lineHeight*maxlines)+"px"
ta.style.height=(lineHeight*maxlines)+"px"
}
}
/**
createTextAreaWithLines is a function which transform the content
of an html element in line numbered content.
id: the id of the object tob transformed
*/
function createPreWithLines(id,maxlines)
{
var el = document.createElement('div'); // create div holding ta and linenumber table
var ta = document.getElementById(id);
ta.parentNode.insertBefore(el,ta); // insert el before the ta
el.appendChild(ta); // add ta to the div
el.className='preWithLines'; // set the style
ta.style.position = 'absolute'; // give ta an absolute position in el
ta.style.left = '32px'; // 30 px to the right
ta.style.left = '32px'; // 30 px to the right
ta.style.margin = '0px';
el.style.left = '0px';
el.style.overflow='hidden';
el.style.position = 'relative';
el.style.display= 'block';
el.style.width= '100%';
el.style.overflow= 'hidden';
el.style.height = (ta.offsetHeight + 0) + 'px';
/*
if(el.parentNode.offsetWidth < ta.offsetWidth + 32)
{
el.parentNode.style.width= (ta.offsetWidth + 32) + 'px';
}
else
{
el.style.width = (ta.offsetWidth + 32) + 'px';
}
*/
var lineObj = document.createElement('div');
lineObj.className='lineObj';
lineObj.style.position = 'absolute';
lineObj.style.top = lineObjOffsetTop + 'px';
lineObj.style.left = '0px';
lineObj.style.width = '29px';
lineObj.style.padding = '0px';
el.insertBefore(lineObj,ta);
lineObj.style.textAlign = 'right';
var string = '';
for(var no=1;no<MAXLINES;no++){
if(string.length>0)string = string + '<br>';
string = string + no;
}
ta.onkeydown = function() { positionLineObj(lineObj,ta); };
ta.onmousedown = function() { positionLineObj(lineObj,ta); };
ta.onscroll = function() { positionLineObj(lineObj,ta); };
ta.onblur = function() { positionLineObj(lineObj,ta); };
ta.onfocus = function() { positionLineObj(lineObj,ta); };
ta.onmouseover = function() { positionLineObj(lineObj,ta); };
lineObj.innerHTML = string;
// limit number of lines in el to maxlines
if( maxlines && !isNaN(maxlines))
{
var lineHeight = calculateLineHeight(ta);
el.style.height=(lineHeight*maxlines)+"px"
ta.style.height=(lineHeight*maxlines)+"px"
}
//console.log(el.innerHTML)
return el;
}
/**
positionLineObj is a fuction which defines the top of an object
above object ta adding the line distance
obj: the object to place
ta: the line below
*/
function positionLineObj(obj,ta)
{
obj.style.top = (ta.scrollTop * -1 + lineObjOffsetTop) + 'px';
}
/**
getStyle obtain style property from a dom element
*/
function getStyle(el,styleProp)
{
var x = el;
if (x.currentStyle)
var y = x.currentStyle[styleProp];
else if (window.getComputedStyle)
var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
return y;
}
// work arround to comute the line height in an dom element
function calculateLineHeight (element) {
var lineHeight = parseInt(getStyle(element, 'line-height'), 10);
var clone;
var singleLineHeight;
var doubleLineHeight;
if (isNaN(lineHeight)) {
clone = element.cloneNode();
clone.innerHTML = '<br>';
element.appendChild(clone);
singleLineHeight = clone.offsetHeight;
clone.innerHTML = '<br><br>';
doubleLineHeight = clone.offsetHeight;
element.removeChild(clone);
lineHeight = doubleLineHeight - singleLineHeight;
}
return lineHeight;
}
function init()
{
var i=1;
while( document.getElementById('codePre'+i) )
{
createPreWithLines('codePre' + i);
i++;
}
i=1;
while( document.getElementById('codeTextarea'+i) )
{
createTextAreaWithLines('codePre' + i);
i++;
}
}