|
| 1 | +// const origSetTimeout = window.setTimeout; |
| 2 | +// window.setTimeout = function(...args) { |
| 3 | +// |
| 4 | +// }; |
| 5 | +const CANVAS_NODE_ID = 'TraceUpdatesWebNodePresenter'; |
| 6 | + |
| 7 | +const OUTLINE_COLOR = '#f0f0f0'; |
| 8 | + |
| 9 | +const COLORS = [ |
| 10 | + // coolest |
| 11 | + '#55cef6', |
| 12 | + '#55f67b', |
| 13 | + '#a5f655', |
| 14 | + '#f4f655', |
| 15 | + '#f6a555', |
| 16 | + '#f66855', |
| 17 | + // hottest |
| 18 | + '#ff0000', |
| 19 | +]; |
| 20 | + |
| 21 | +const HOTTEST_COLOR = COLORS[COLORS.length - 1]; |
| 22 | +const DURATION = 2500; |
| 23 | + |
| 24 | +class Tracer { |
| 25 | + _canvas; |
| 26 | + _pool = new Map(); |
| 27 | + _drawing; |
| 28 | + |
| 29 | + present(measurement) { |
| 30 | + console.log('new measurement'); |
| 31 | + var data; |
| 32 | + if (this._pool.has(measurement)) { |
| 33 | + data = this._pool.get(measurement); |
| 34 | + } else { |
| 35 | + data = {}; |
| 36 | + } |
| 37 | + |
| 38 | + data = { |
| 39 | + expiration: Date.now() + DURATION, |
| 40 | + hit: data.hit + 1, |
| 41 | + }; |
| 42 | + |
| 43 | + this._pool = this._pool.set(measurement, data); |
| 44 | + |
| 45 | + if (this._drawing) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + this._drawing = true; |
| 50 | + Zone.root.run(() => { |
| 51 | + requestAnimationFrame(this._draw.bind(this)); |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + _draw() { |
| 56 | + var now = Date.now(); |
| 57 | + var minExpiration = Number.MAX_VALUE; |
| 58 | + |
| 59 | + const temp = new Map(); |
| 60 | + for (let [measurement, data] of this._pool.entries()) { |
| 61 | + if (data.expiration < now) { |
| 62 | + // already passed the expiration time. |
| 63 | + } else { |
| 64 | + // TODO what does this even do? |
| 65 | + console.log('setting', minExpiration); |
| 66 | + minExpiration = Math.min(data.expiration, minExpiration); |
| 67 | + console.log('setting', minExpiration); |
| 68 | + temp.set(measurement, data); |
| 69 | + } |
| 70 | + } |
| 71 | + this._pool = temp; |
| 72 | + |
| 73 | + this.drawImpl(this._pool); |
| 74 | + |
| 75 | + if (this._pool.size > 0) { |
| 76 | + // debugger; |
| 77 | + if (this._clearTimer != null) { |
| 78 | + clearTimeout(this._clearTimer); |
| 79 | + } |
| 80 | + this._clearTimer = Zone.root.run(() => { |
| 81 | + setTimeout(this._redraw.bind(this), minExpiration - now); |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + this._drawing = false; |
| 86 | + console.log('set drawing false'); |
| 87 | + } |
| 88 | + |
| 89 | + _redraw() { |
| 90 | + this._clearTimer = null; |
| 91 | + if (!this._drawing && this._pool.size > 0) { |
| 92 | + this._drawing = true; |
| 93 | + this._draw(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + drawImpl(pool) { |
| 98 | + this._ensureCanvas(); |
| 99 | + var canvas = this._canvas; |
| 100 | + var ctx = canvas.getContext('2d'); |
| 101 | + ctx.clearRect( |
| 102 | + 0, |
| 103 | + 0, |
| 104 | + canvas.width, |
| 105 | + canvas.height |
| 106 | + ); |
| 107 | + for (const [measurement, data] of pool.entries()) { |
| 108 | + const color = COLORS[data.hit - 1] || HOTTEST_COLOR; |
| 109 | + drawBorder(ctx, measurement, 1, color); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + clearImpl() { |
| 115 | + var canvas = this._canvas; |
| 116 | + if (canvas === null) { |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + if (!canvas.parentNode) { |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + var ctx = canvas.getContext('2d'); |
| 125 | + ctx.clearRect( |
| 126 | + 0, |
| 127 | + 0, |
| 128 | + canvas.width, |
| 129 | + canvas.height |
| 130 | + ); |
| 131 | + |
| 132 | + canvas.parentNode.removeChild(canvas); |
| 133 | + this._canvas = null; |
| 134 | + } |
| 135 | + |
| 136 | + _ensureCanvas() { |
| 137 | + var canvas = this._canvas; |
| 138 | + if (canvas === null || canvas === undefined) { |
| 139 | + canvas = |
| 140 | + window.document.getElementById(CANVAS_NODE_ID) || |
| 141 | + window.document.createElement('canvas'); |
| 142 | + |
| 143 | + canvas.id = CANVAS_NODE_ID; |
| 144 | + canvas.width = window.screen.availWidth; |
| 145 | + canvas.height = window.screen.availHeight; |
| 146 | + canvas.style.cssText = ` |
| 147 | + xx-background-color: red; |
| 148 | + xx-opacity: 0.5; |
| 149 | + bottom: 0; |
| 150 | + left: 0; |
| 151 | + pointer-events: none; |
| 152 | + position: fixed; |
| 153 | + right: 0; |
| 154 | + top: 0; |
| 155 | + z-index: 1000000000; |
| 156 | + `; |
| 157 | + } |
| 158 | + |
| 159 | + if (!canvas.parentNode) { |
| 160 | + var root = window.document.documentElement; |
| 161 | + root.insertBefore(canvas, root.firstChild); |
| 162 | + } |
| 163 | + this._canvas = canvas; |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | + |
| 168 | +function drawBorder(ctx, measurement, borderWidth, borderColor) { |
| 169 | + // outline |
| 170 | + ctx.lineWidth = 1; |
| 171 | + ctx.strokeStyle = OUTLINE_COLOR; |
| 172 | + |
| 173 | + ctx.strokeRect( |
| 174 | + measurement.left - 1, |
| 175 | + measurement.top - 1, |
| 176 | + measurement.width + 2, |
| 177 | + measurement.height + 2, |
| 178 | + ); |
| 179 | + |
| 180 | + // inset |
| 181 | + ctx.lineWidth = 1; |
| 182 | + ctx.strokeStyle = OUTLINE_COLOR; |
| 183 | + ctx.strokeRect( |
| 184 | + measurement.left + borderWidth, |
| 185 | + measurement.top + borderWidth, |
| 186 | + measurement.width - borderWidth, |
| 187 | + measurement.height - borderWidth, |
| 188 | + ); |
| 189 | + ctx.strokeStyle = borderColor; |
| 190 | + |
| 191 | + |
| 192 | + // if (measurement.should_update) { |
| 193 | + ctx.setLineDash([0]); |
| 194 | + // } else { |
| 195 | + // ctx.setLineDash([0]); |
| 196 | + // } |
| 197 | + |
| 198 | + // border |
| 199 | + ctx.lineWidth = '' + borderWidth; |
| 200 | + ctx.strokeRect( |
| 201 | + measurement.left + Math.floor(borderWidth / 2), |
| 202 | + measurement.top + Math.floor(borderWidth / 2), |
| 203 | + measurement.width - borderWidth, |
| 204 | + measurement.height - borderWidth, |
| 205 | + ); |
| 206 | + |
| 207 | + ctx.setLineDash([0]); |
| 208 | +} |
| 209 | + |
| 210 | +let borderRemovals = []; |
| 211 | + |
| 212 | + |
| 213 | +// export type Measurement = { |
| 214 | +// bottom: number, |
| 215 | +// expiration: number, |
| 216 | +// height: number, |
| 217 | +// id: string, |
| 218 | +// left: number, |
| 219 | +// right: number, |
| 220 | +// scrollX: number, |
| 221 | +// scrollY: number, |
| 222 | +// top: number, |
| 223 | +// width: number, |
| 224 | +// }; |
| 225 | + |
| 226 | +const createMeasurement = (rect) => { |
| 227 | + return { |
| 228 | + left: rect.left, |
| 229 | + top: rect.top, |
| 230 | + width: rect.width, |
| 231 | + height: rect.height, |
| 232 | + } |
| 233 | +}; |
| 234 | + |
| 235 | +const createDiv = (rect, spanText) => { |
| 236 | + const div = document.createElement('div'); |
| 237 | + div.style.position = 'fixed'; |
| 238 | + div.style.height = rect.height + 1 + 'px'; |
| 239 | + div.style.width = rect.width + 1 + 'px'; |
| 240 | + div.style.top = rect.top + 'px'; |
| 241 | + div.style.left = rect.left + 'px'; |
| 242 | + div.style.border = '1px solid blue'; |
| 243 | + div.style.animation = 'hide 15000ms forwards'; |
| 244 | + |
| 245 | + const span = document.createElement('span'); |
| 246 | + span.style.position = 'fixed'; |
| 247 | + span.style.top = rect.top + 'px'; |
| 248 | + span.style.left = rect.left + 'px'; |
| 249 | + span.textContent = spanText; |
| 250 | + |
| 251 | + div.appendChild(span); |
| 252 | + return div; |
| 253 | +}; |
| 254 | + |
| 255 | +const refs = {}; |
| 256 | + |
| 257 | +const tracer = new Tracer(); |
| 258 | + |
| 259 | +const monkeyPatchTemplate = (instance, isRoot = false) => { |
| 260 | + const origTemplate = instance[1].template; |
| 261 | + instance[1].template = function (...args) { |
| 262 | + console.debug('Calling the original template function'); |
| 263 | + origTemplate(...args); |
| 264 | + console.debug('After the original template function'); |
| 265 | + const tagName = instance[0].tagName; |
| 266 | + console.log('CD for ', tagName); |
| 267 | + // if (refs[tagName]) { |
| 268 | + // document.body.removeChild(refs[tagName]); |
| 269 | + // } |
| 270 | + |
| 271 | + tracer.present(createMeasurement(instance[0].getBoundingClientRect())); |
| 272 | + // const runOutsideZone = () => { |
| 273 | + // setTimeout(() => { |
| 274 | + // // const div = createDiv(instance[0].getBoundingClientRect(), tagName); |
| 275 | + // // refs[tagName] = div; |
| 276 | + // // document.body.appendChild(div); |
| 277 | + // // Zone.root.run(() => { |
| 278 | + // // // TODO should this event listener be removed as well? |
| 279 | + // // div.addEventListener('animationend', () => { |
| 280 | + // // console.log('Removing the div'); |
| 281 | + // // document.body.removeChild(div); |
| 282 | + // // delete refs[tagName]; |
| 283 | + // // }); |
| 284 | + // // }); |
| 285 | + // }); |
| 286 | + // }; |
| 287 | + // |
| 288 | + // Zone.root.run(runOutsideZone); |
| 289 | + } |
| 290 | +}; |
| 291 | + |
| 292 | +const loopComponents = (parentNode) => { |
| 293 | + const components = parentNode[1].components; |
| 294 | + if (!components) { |
| 295 | + return; |
| 296 | + } |
| 297 | + for (let i = 0; i < components.length; i++) { |
| 298 | + console.log('found component ' + parentNode[components[i]][0].tagName); |
| 299 | + monkeyPatchTemplate(parentNode[components[i]]); |
| 300 | + loopComponents(parentNode[components[i]]); |
| 301 | + } |
| 302 | +}; |
| 303 | + |
| 304 | +const findRootNode = (node) => { |
| 305 | + if (!node || !node.childNodes) { |
| 306 | + return; |
| 307 | + } |
| 308 | + const childNodes = node.childNodes; |
| 309 | + for (let i = 0; i < childNodes.length; i++) { |
| 310 | + let childNode = childNodes[i]; |
| 311 | + if (childNode.__ngContext__) { |
| 312 | + const instance = childNode.__ngContext__.debug._raw_lView[20]; |
| 313 | + monkeyPatchTemplate(instance, true); |
| 314 | + loopComponents(instance) |
| 315 | + } else { |
| 316 | + findRootNode(childNode); |
| 317 | + } |
| 318 | + } |
| 319 | + } |
| 320 | +; |
| 321 | + |
| 322 | +setTimeout(() => { |
| 323 | + console.debug('booting the plugin'); |
| 324 | + |
| 325 | + findRootNode(document.body); |
| 326 | +}, 2000); |
| 327 | + |
| 328 | + |
0 commit comments