summaryrefslogtreecommitdiff
path: root/js/error.highlight.js
blob: 0b478e242cba45f4f456d1c31d9a57b897ee4b8a (plain)
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
(function(){

var dom = {}
var style = {}
var pos = {}
// i use this because a plain <br> collapses vertically sometimes...
var zero_width_space = '&#xfeff;'
var height_until_error = 0;
var height_with_error = 0;
var scrollbar_width = 0;

var off = function(){
  dom.highlight.style.display = 'none'
}

var on = function(line_num){
  pos = dom.textarea.getBoundingClientRect()
  var text = dom.textarea.value;

  var lines = text.split('\n')
  var lines_until_error = lines.slice(0, line_num)
  var line_with_error = lines[line_num]

  if (lines_until_error.length === 1)
    dom.textmeasure.innerHTML = lines_until_error + zero_width_space
  else
    dom.textmeasure.innerHTML = lines_until_error.join('<br>' + zero_width_space)

  height_until_error = dom.textmeasure.offsetHeight

  dom.textmeasure.innerHTML = line_with_error + zero_width_space

  height_with_error = dom.textmeasure.offsetHeight

  reposition_highlight()
}

var reposition_highlight = function(){

  var bounds_bottom = pos.top + pos.height

  if (dom.textarea.scrollHeight > dom.textarea.clientHeight) // scrollbar exists
    dom.highlight.style.width = pos.width - scrollbar_width + "px"
  else
    dom.highlight.style.width = pos.width + "px"

  dom.highlight.style.left = pos.left + "px"

  var y_pos = pos.top + height_until_error - dom.textarea.scrollTop
  dom.highlight.style.top = y_pos + dom.html.scrollTop + "px"

  var height_of_highlight = height_with_error;

  // nice clip on bottom
  if (y_pos + height_of_highlight > bounds_bottom)
    height_of_highlight = Math.max(0, bounds_bottom - y_pos)
  // crap clip on top
  if (y_pos < pos.top)
    height_of_highlight = 0

  dom.highlight.style.height = height_of_highlight + "px"

  dom.highlight.style.display = 'block'
}

var calc_textarea_style = function(){
  var $textarea = $("#shader")
  dom.textarea = $textarea[0]
  
  var props = ['lineHeight', 'fontFamily', 'fontSize', 'padding', 'margin', 'borderWidth', 'width']

  for (var i=0, p; p=props[i]; i++){
    style[p] = $textarea.css(p)
  }
}


var calc_scrollbar_width = function() {
    var outer = document.createElement("div");
    outer.style.visibility = "hidden";
    outer.style.width = "100px";
    document.body.appendChild(outer);
    
    var width_no_scroll = outer.offsetWidth;
    // force scrollbars
    outer.style.overflow = "scroll";
    
    var inner = document.createElement("div");
    inner.style.width = "100%";
    outer.appendChild(inner);        
    
    var width_with_scroll = inner.offsetWidth;
    outer.parentNode.removeChild(outer);
    
    return width_no_scroll - width_with_scroll;
}

var create_el_textmeasure = function(){
  var el = dom.textmeasure = document.createElement('div')
  el.id = 'textmeasure'
  var s = el.style
  for (var key in style)
    s[key] = style[key]

  s.wordWrap = 'break-word'
  s.wordBreak = 'break-all' 
  s.border = '1px solid red'
  s.padding = '0'
  s.display = 'block'
  s.position = 'absolute'
  s.left = "-5000px"
  document.body.appendChild(el)
}

var create_el_highlight = function(){
  var el = dom.highlight = document.createElement('div')
  var s = el.style
  for (var key in style)
    s[key] = style[key]
  
  s.pointerEvents = 'none'
  s.opacity = '0.2'
  s.backgroundColor = '#f00'
  s.position = 'absolute'
  s.lineHeight = '0'
  s.fontSize = '0'
  s.padding = '0'
  s.borderWidth = '0'
  s.display = 'block'
  document.body.appendChild(el)
}

var init = function(){
  dom.html = document.querySelector('html')
  calc_textarea_style()
  create_el_highlight()
  create_el_textmeasure()
  scrollbar_width = calc_scrollbar_width()
  textarea.addEventListener('scroll', reposition_highlight)
}

// exports
error_highlight = {
  init: init,
  on: on,
  off: off
}

})();

error_highlight.init()