forked from webrtc-for-the-curious/explainer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.js
More file actions
143 lines (117 loc) · 4.71 KB
/
Copy pathcontroller.js
File metadata and controls
143 lines (117 loc) · 4.71 KB
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
import { parseSDP, mapLineToDescription } from './parser.js';
let lineHeight;
let paddingLeft;
let paddingTop;
let sdpInputTimer;
let displayedLine;
let sdpDescriptions = {};
let sdpOverlays = {};
const explanationTemplate = {
"<>": "h3",
"text": "${heading}"
};
class SectionOverlay {
constructor(parent, first, last) {
this.overlay = document.createElement("div");
let left = paddingLeft;
let top = first * lineHeight + paddingTop;
let width = parent.clientWidth - paddingLeft;
let height = (last + 1) * lineHeight - top + paddingTop;
console.log(`lines [${first}, ${last}], top ${top}, left ${left}, width ${width}, height ${height}`)
this.overlay.style.position = "absolute";
this.overlay.style.left = `${left}px`;
this.overlay.style.width = `${width}px`;
this.overlay.style.top = `${top}px`;
this.overlay.style.height = `${height}px`;
this.overlay.id = `overlay-section-${first}`
parent.appendChild(this.overlay);
}
highlight() {
this.overlay.style.backgroundColor = '#2af';
this.overlay.style.opacity = '0.2';
}
clear() {
this.overlay.style.backgroundColor = 'transparent';
this.overlay.style.opacity = '0.0';
}
};
function onSDPInput() {
const handleInput = () => {
// clear current children of overlay
for (const [,sectionOverlay] of Object.entries(sdpOverlays)) {
sectionOverlay.clear();
}
let overlay = document.getElementById("overlay");
overlay.replaceChildren()
// Parse new SDP and populate sdpOverlays
try {
let sdpinput = document.getElementById("sdp-input");
// trim white space from both ends
let lines = sdpinput.value.split("\n");
// Setting the minHeight expands the textarea to show all the lines without a scrollbar.
sdpinput.style.minHeight = `${lines.length + (sdpinput.offsetHeight - sdpinput.clientHeight)/lineHeight}lh`;
let trimmed = lines.map((element, index, array) => { return element.trim(); });
sdpinput.value = trimmed.join("\n");
sdpOverlays = {};
sdpDescriptions = parseSDP(sdpinput.value);
displayedLine = null;
for (const [index, [key, description]] of Object.entries(sdpDescriptions || []).entries()) {
sdpOverlays[key] = new SectionOverlay(overlay, description.firstLineNumber(), description.lastLineNumber());
}
} catch (error) {
console.error(`error ${error}`);
overlay.replaceChildren();
sdpDescriptions = {};
sdpOverlays = {};
}
}
let sdpinput = document.getElementById("sdp-input");
if (sdpinput.value.length == 0) {
handleInput();
} else {
// clear previous timer
clearTimeout(sdpInputTimer);
// start new timer
sdpInputTimer = setTimeout(handleInput, 300);
}
}
function onSDPLineClick(e) {
let sdpInput = document.getElementById("sdp-input");
if (sdpInput.value.length == 0) {
return;
}
const rect = sdpInput.getBoundingClientRect();
let linepos = Math.floor((e.clientY - rect.top - paddingTop) / lineHeight);
console.log(`Y = ${e.clientY}, top = ${rect.top}, line = ${linepos}, height = ${lineHeight}`);
if (displayedLine != null) {
sdpOverlays[displayedLine].clear();
}
let description = mapLineToDescription(sdpDescriptions, linepos);
if (description != null) {
displayedLine = description.firstLineNumber();
sdpOverlays[displayedLine].highlight();
}
let explanation = document.getElementById("explanation");
explanation.innerHTML = description.explain()
}
window.addEventListener('load', () => {
// This function calculates the height of each line
// in the textarea in a browser-independent way.
let element = document.getElementById("sdp-input");
let styles = window.getComputedStyle(element);
paddingLeft = parseInt(styles.paddingLeft);
paddingTop = parseInt(styles.paddingTop);
const clone = element.cloneNode(false);
clone.innerHTML = "A\nB"; // Format for two lines to get impact of line spacing
clone.style.padding = "0";
clone.style.border = "0";
clone.style.visibility = "hidden";
clone.style.position = "absolute";
clone.style.minHeight = "2lh"
document.body.appendChild(clone);
lineHeight = clone.offsetHeight/2; // This is the line height in pixels
document.body.removeChild(clone);
console.debug(`lineHeight=${lineHeight}, paddingLeft=${paddingLeft}, paddingTop=${paddingTop}`);
});
window.onSDPInput = onSDPInput;
window.onSDPLineClick = onSDPLineClick;