Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of |
| 14 | * its contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 28 | |
| 29 | import * as Common from '../common/common.js'; |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 30 | import * as SDK from '../sdk/sdk.js'; |
| 31 | import * as UI from '../ui/ui.js'; |
| 32 | |
Tim van der Lippe | aabc830 | 2019-12-10 15:34:45 | [diff] [blame] | 33 | import {ElementsSidebarPane} from './ElementsSidebarPane.js'; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * @unrestricted |
| 37 | */ |
Tim van der Lippe | aabc830 | 2019-12-10 15:34:45 | [diff] [blame] | 38 | export class MetricsSidebarPane extends ElementsSidebarPane { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 39 | constructor() { |
| 40 | super(); |
Jack Franklin | 71519f8 | 2020-11-03 12:08:59 | [diff] [blame^] | 41 | this.registerRequiredCSS('elements/metricsSidebarPane.css', {enableLegacyPatching: true}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 42 | |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 43 | /** @type {?SDK.CSSProperty.CSSProperty} */ |
| 44 | this.originalPropertyData = null; |
| 45 | /** @type {?SDK.CSSProperty.CSSProperty} */ |
| 46 | this.previousPropertyDataCandidate = null; |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 47 | /** @type {?SDK.CSSStyleDeclaration.CSSStyleDeclaration} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 48 | this._inlineStyle = null; |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 49 | /** @type {string} */ |
| 50 | this._highlightMode = ''; |
| 51 | /** @type {!Array<!{element: !HTMLElement, name: string, backgroundColor: string}>} */ |
| 52 | this._boxElements = []; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @override |
| 57 | * @protected |
| 58 | * @return {!Promise.<?>} |
| 59 | */ |
| 60 | doUpdate() { |
| 61 | // "style" attribute might have changed. Update metrics unless they are being edited |
| 62 | // (if a CSS property is added, a StyleSheetChanged event is dispatched). |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 63 | if (this._isEditingMetrics) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 64 | return Promise.resolve(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 65 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 66 | |
| 67 | // FIXME: avoid updates of a collapsed pane. |
| 68 | const node = this.node(); |
| 69 | const cssModel = this.cssModel(); |
| 70 | if (!node || node.nodeType() !== Node.ELEMENT_NODE || !cssModel) { |
| 71 | this.contentElement.removeChildren(); |
| 72 | return Promise.resolve(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @param {?Map.<string, string>} style |
Tim van der Lippe | 13f71fb | 2019-11-29 11:17:39 | [diff] [blame] | 77 | * @this {MetricsSidebarPane} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 78 | */ |
| 79 | function callback(style) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 80 | if (!style || this.node() !== node) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 81 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 82 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 83 | this._updateMetrics(style); |
| 84 | } |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 85 | |
| 86 | if (!node.id) { |
| 87 | return Promise.resolve(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | const promises = [ |
| 91 | cssModel.computedStylePromise(node.id).then(callback.bind(this)), |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 92 | cssModel.inlineStylesPromise(node.id).then(inlineStyleResult => { |
| 93 | if (inlineStyleResult && this.node() === node) { |
| 94 | this._inlineStyle = inlineStyleResult.inlineStyle; |
| 95 | } |
| 96 | }), |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 97 | ]; |
| 98 | return Promise.all(promises); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @override |
| 103 | */ |
| 104 | onCSSModelChanged() { |
| 105 | this.update(); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @param {!Map.<string, string>} style |
| 110 | * @param {string} propertyName |
| 111 | * @return {number} |
| 112 | */ |
| 113 | _getPropertyValueAsPx(style, propertyName) { |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 114 | const propertyValue = style.get(propertyName); |
| 115 | if (!propertyValue) { |
| 116 | return 0; |
| 117 | } |
| 118 | return Number(propertyValue.replace(/px$/, '') || 0); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @param {!Map.<string, string>} computedStyle |
| 123 | * @param {string} componentName |
| 124 | */ |
| 125 | _getBox(computedStyle, componentName) { |
| 126 | const suffix = componentName === 'border' ? '-width' : ''; |
| 127 | const left = this._getPropertyValueAsPx(computedStyle, componentName + '-left' + suffix); |
| 128 | const top = this._getPropertyValueAsPx(computedStyle, componentName + '-top' + suffix); |
| 129 | const right = this._getPropertyValueAsPx(computedStyle, componentName + '-right' + suffix); |
| 130 | const bottom = this._getPropertyValueAsPx(computedStyle, componentName + '-bottom' + suffix); |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 131 | return {left, top, right, bottom}; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @param {boolean} showHighlight |
| 136 | * @param {string} mode |
| 137 | * @param {!Event} event |
| 138 | */ |
| 139 | _highlightDOMNode(showHighlight, mode, event) { |
| 140 | event.consume(); |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 141 | const node = this.node(); |
| 142 | if (showHighlight && node) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 143 | if (this._highlightMode === mode) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 144 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 145 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 146 | this._highlightMode = mode; |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 147 | node.highlight(mode); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 148 | } else { |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 149 | this._highlightMode = ''; |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 150 | SDK.OverlayModel.OverlayModel.hideDOMNodeHighlight(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 151 | } |
| 152 | |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 153 | for (const {element, name, backgroundColor} of this._boxElements) { |
| 154 | if (!node || mode === 'all' || name === mode) { |
| 155 | element.style.backgroundColor = backgroundColor; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 156 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 157 | element.style.backgroundColor = ''; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 158 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * @param {!Map.<string, string>} style |
| 164 | */ |
| 165 | _updateMetrics(style) { |
| 166 | // Updating with computed style. |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 167 | const metricsElement = document.createElement('div'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 168 | metricsElement.className = 'metrics'; |
| 169 | const self = this; |
| 170 | |
| 171 | /** |
| 172 | * @param {!Map.<string, string>} style |
| 173 | * @param {string} name |
| 174 | * @param {string} side |
| 175 | * @param {string} suffix |
Tim van der Lippe | 13f71fb | 2019-11-29 11:17:39 | [diff] [blame] | 176 | * @this {MetricsSidebarPane} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 177 | */ |
| 178 | function createBoxPartElement(style, name, side, suffix) { |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 179 | const element = document.createElement('div'); |
| 180 | element.className = side; |
| 181 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 182 | const propertyName = (name !== 'position' ? name + '-' : '') + side + suffix; |
| 183 | let value = style.get(propertyName); |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 184 | if (value === undefined) { |
| 185 | return element; |
| 186 | } |
| 187 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 188 | if (value === '' || (name !== 'position' && value === '0px')) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 189 | value = '\u2012'; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 190 | } else if (name === 'position' && value === 'auto') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 191 | value = '\u2012'; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 192 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 193 | value = value.replace(/px$/, ''); |
| 194 | value = Number.toFixedIfFloating(value); |
| 195 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 196 | element.textContent = value; |
| 197 | element.addEventListener('dblclick', this.startEditing.bind(this, element, name, propertyName, style), false); |
| 198 | return element; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * @param {!Map.<string, string>} style |
| 203 | * @return {string} |
| 204 | */ |
| 205 | function getContentAreaWidthPx(style) { |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 206 | let width = style.get('width'); |
| 207 | if (!width) { |
| 208 | return ''; |
| 209 | } |
| 210 | width = width.replace(/px$/, ''); |
| 211 | const widthValue = Number(width); |
| 212 | if (!isNaN(widthValue) && style.get('box-sizing') === 'border-box') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 213 | const borderBox = self._getBox(style, 'border'); |
| 214 | const paddingBox = self._getBox(style, 'padding'); |
| 215 | |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 216 | width = (widthValue - borderBox.left - borderBox.right - paddingBox.left - paddingBox.right).toString(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 217 | } |
| 218 | |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 219 | return Number.toFixedIfFloating(width); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /** |
| 223 | * @param {!Map.<string, string>} style |
| 224 | * @return {string} |
| 225 | */ |
| 226 | function getContentAreaHeightPx(style) { |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 227 | let height = style.get('height'); |
| 228 | if (!height) { |
| 229 | return ''; |
| 230 | } |
| 231 | height = height.replace(/px$/, ''); |
| 232 | const heightValue = Number(height); |
| 233 | if (!isNaN(heightValue) && style.get('box-sizing') === 'border-box') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 234 | const borderBox = self._getBox(style, 'border'); |
| 235 | const paddingBox = self._getBox(style, 'padding'); |
| 236 | |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 237 | height = (heightValue - borderBox.top - borderBox.bottom - paddingBox.top - paddingBox.bottom).toString(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 238 | } |
| 239 | |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 240 | return Number.toFixedIfFloating(height); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | // Display types for which margin is ignored. |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 244 | const noMarginDisplayType = new Set([ |
| 245 | 'table-cell', |
| 246 | 'table-column', |
| 247 | 'table-column-group', |
| 248 | 'table-footer-group', |
| 249 | 'table-header-group', |
| 250 | 'table-row', |
| 251 | 'table-row-group', |
| 252 | ]); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 253 | |
| 254 | // Display types for which padding is ignored. |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 255 | const noPaddingDisplayType = new Set([ |
| 256 | 'table-column', |
| 257 | 'table-column-group', |
| 258 | 'table-footer-group', |
| 259 | 'table-header-group', |
| 260 | 'table-row', |
| 261 | 'table-row-group', |
| 262 | ]); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 263 | |
| 264 | // Position types for which top, left, bottom and right are ignored. |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 265 | const noPositionType = new Set(['static']); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 266 | |
| 267 | const boxes = ['content', 'padding', 'border', 'margin', 'position']; |
| 268 | const boxColors = [ |
| 269 | Common.Color.PageHighlight.Content, Common.Color.PageHighlight.Padding, Common.Color.PageHighlight.Border, |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 270 | Common.Color.PageHighlight.Margin, Common.Color.Color.fromRGBA([0, 0, 0, 0]) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 271 | ]; |
| 272 | const boxLabels = [ |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 273 | Common.UIString.UIString('content'), Common.UIString.UIString('padding'), Common.UIString.UIString('border'), |
| 274 | Common.UIString.UIString('margin'), Common.UIString.UIString('position') |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 275 | ]; |
| 276 | let previousBox = null; |
| 277 | this._boxElements = []; |
| 278 | for (let i = 0; i < boxes.length; ++i) { |
| 279 | const name = boxes[i]; |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 280 | const display = style.get('display'); |
| 281 | const position = style.get('position'); |
| 282 | if (!display || !position) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 283 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 284 | } |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 285 | if (name === 'margin' && noMarginDisplayType.has(display)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 286 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 287 | } |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 288 | if (name === 'padding' && noPaddingDisplayType.has(display)) { |
| 289 | continue; |
| 290 | } |
| 291 | if (name === 'position' && noPositionType.has(position)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 292 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 293 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 294 | |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 295 | const boxElement = /** @type {!HTMLDivElement} */ (document.createElement('div')); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 296 | boxElement.className = name; |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 297 | const backgroundColor = boxColors[i].asString(Common.Color.Format.RGBA) || ''; |
| 298 | boxElement.style.backgroundColor = backgroundColor; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 299 | boxElement.addEventListener( |
| 300 | 'mouseover', this._highlightDOMNode.bind(this, true, name === 'position' ? 'all' : name), false); |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 301 | this._boxElements.push({element: boxElement, name, backgroundColor}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 302 | |
| 303 | if (name === 'content') { |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 304 | const widthElement = document.createElement('span'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 305 | widthElement.textContent = getContentAreaWidthPx(style); |
| 306 | widthElement.addEventListener( |
| 307 | 'dblclick', this.startEditing.bind(this, widthElement, 'width', 'width', style), false); |
| 308 | |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 309 | const heightElement = document.createElement('span'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 310 | heightElement.textContent = getContentAreaHeightPx(style); |
| 311 | heightElement.addEventListener( |
| 312 | 'dblclick', this.startEditing.bind(this, heightElement, 'height', 'height', style), false); |
| 313 | |
| 314 | boxElement.appendChild(widthElement); |
Sigurd Schneider | 23c5297 | 2020-10-13 09:31:14 | [diff] [blame] | 315 | UI.UIUtils.createTextChild(boxElement, ' × '); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 316 | boxElement.appendChild(heightElement); |
| 317 | } else { |
| 318 | const suffix = (name === 'border' ? '-width' : ''); |
| 319 | |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 320 | const labelElement = document.createElement('div'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 321 | labelElement.className = 'label'; |
| 322 | labelElement.textContent = boxLabels[i]; |
| 323 | boxElement.appendChild(labelElement); |
| 324 | |
| 325 | boxElement.appendChild(createBoxPartElement.call(this, style, name, 'top', suffix)); |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 326 | boxElement.appendChild(document.createElement('br')); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 327 | boxElement.appendChild(createBoxPartElement.call(this, style, name, 'left', suffix)); |
| 328 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 329 | if (previousBox) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 330 | boxElement.appendChild(previousBox); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 331 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 332 | |
| 333 | boxElement.appendChild(createBoxPartElement.call(this, style, name, 'right', suffix)); |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 334 | boxElement.appendChild(document.createElement('br')); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 335 | boxElement.appendChild(createBoxPartElement.call(this, style, name, 'bottom', suffix)); |
| 336 | } |
| 337 | |
| 338 | previousBox = boxElement; |
| 339 | } |
| 340 | |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 341 | metricsElement.appendChild(/** @type {!HTMLDivElement} */ (previousBox)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 342 | metricsElement.addEventListener('mouseover', this._highlightDOMNode.bind(this, false, 'all'), false); |
Changhao Han | 1d41edd | 2020-09-14 03:02:22 | [diff] [blame] | 343 | metricsElement.addEventListener('mouseleave', this._highlightDOMNode.bind(this, false, 'all'), false); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 344 | this.contentElement.removeChildren(); |
| 345 | this.contentElement.appendChild(metricsElement); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /** |
| 349 | * @param {!Element} targetElement |
| 350 | * @param {string} box |
| 351 | * @param {string} styleProperty |
| 352 | * @param {!Map.<string, string>} computedStyle |
| 353 | */ |
| 354 | startEditing(targetElement, box, styleProperty, computedStyle) { |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 355 | if (UI.UIUtils.isBeingEdited(targetElement)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 356 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 357 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 358 | |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 359 | /** @type {!{box: string, styleProperty: string, computedStyle: !Map.<string, string>, keyDownHandler: function(!Event):void}} */ |
| 360 | const context = {box, styleProperty, computedStyle, keyDownHandler: () => {}}; |
| 361 | const boundKeyDown = this._handleKeyDown.bind(this, context); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 362 | context.keyDownHandler = boundKeyDown; |
| 363 | targetElement.addEventListener('keydown', boundKeyDown, false); |
| 364 | |
| 365 | this._isEditingMetrics = true; |
| 366 | |
| 367 | const config = |
| 368 | new UI.InplaceEditor.Config(this._editingCommitted.bind(this), this.editingCancelled.bind(this), context); |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 369 | UI.InplaceEditor.InplaceEditor.startEditing(targetElement, /** @type {!UI.InplaceEditor.Config<?>} */ (config)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 370 | |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 371 | const selection = targetElement.getComponentSelection(); |
| 372 | selection && selection.selectAllChildren(targetElement); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 373 | } |
| 374 | |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 375 | /** |
| 376 | * @param {!{box: string, styleProperty: string, computedStyle: !Map.<string, string>, keyDownHandler: function(!Event):void}} context |
| 377 | * @param {!Event} event |
| 378 | */ |
| 379 | _handleKeyDown(context, event) { |
| 380 | const element = /** @type {!Element} */ (event.currentTarget); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 381 | |
| 382 | /** |
| 383 | * @param {string} originalValue |
| 384 | * @param {string} replacementString |
Tim van der Lippe | 13f71fb | 2019-11-29 11:17:39 | [diff] [blame] | 385 | * @this {MetricsSidebarPane} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 386 | */ |
| 387 | function finishHandler(originalValue, replacementString) { |
| 388 | this._applyUserInput(element, replacementString, originalValue, context, false); |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * @param {string} prefix |
| 393 | * @param {number} number |
| 394 | * @param {string} suffix |
| 395 | * @return {string} |
| 396 | */ |
| 397 | function customNumberHandler(prefix, number, suffix) { |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 398 | if (context.styleProperty !== 'margin' && number < 0) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 399 | number = 0; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 400 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 401 | return prefix + number + suffix; |
| 402 | } |
| 403 | |
Tim van der Lippe | 97611c9 | 2020-02-12 16:56:58 | [diff] [blame] | 404 | UI.UIUtils.handleElementValueModifications( |
| 405 | event, element, finishHandler.bind(this), undefined, customNumberHandler); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 406 | } |
| 407 | |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 408 | /** |
| 409 | * @param {!Element} element |
| 410 | * @param {!{keyDownHandler: function(!Event):void}} context |
| 411 | */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 412 | editingEnded(element, context) { |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 413 | this.originalPropertyData = null; |
| 414 | this.previousPropertyDataCandidate = null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 415 | element.removeEventListener('keydown', context.keyDownHandler, false); |
| 416 | delete this._isEditingMetrics; |
| 417 | } |
| 418 | |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 419 | /** |
| 420 | * @param {!Element} element |
| 421 | * @param {!{box: string, styleProperty: string, computedStyle: !Map.<string, string>, keyDownHandler: function(!Event):void}} context |
| 422 | */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 423 | editingCancelled(element, context) { |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 424 | if (this._inlineStyle) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 425 | if (!this.originalPropertyData) { |
| 426 | // An added property, remove the last property in the style. |
| 427 | const pastLastSourcePropertyIndex = this._inlineStyle.pastLastSourcePropertyIndex(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 428 | if (pastLastSourcePropertyIndex) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 429 | this._inlineStyle.allProperties()[pastLastSourcePropertyIndex - 1].setText('', false); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 430 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 431 | } else { |
| 432 | this._inlineStyle.allProperties()[this.originalPropertyData.index].setText( |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 433 | this.originalPropertyData.propertyText || '', false); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | this.editingEnded(element, context); |
| 437 | this.update(); |
| 438 | } |
| 439 | |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 440 | /** |
| 441 | * @param {!Element} element |
| 442 | * @param {string} userInput |
| 443 | * @param {string} previousContent |
| 444 | * @param {!{box: string, styleProperty: string, computedStyle: !Map.<string, string>, keyDownHandler: function(!Event):void}} context |
| 445 | * @param {boolean} commitEditor |
| 446 | */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 447 | _applyUserInput(element, userInput, previousContent, context, commitEditor) { |
| 448 | if (!this._inlineStyle) { |
| 449 | // Element has no renderer. |
| 450 | return this.editingCancelled(element, context); // nothing changed, so cancel |
| 451 | } |
| 452 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 453 | if (commitEditor && userInput === previousContent) { |
| 454 | return this.editingCancelled(element, context); |
| 455 | } // nothing changed, so cancel |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 456 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 457 | if (context.box !== 'position' && (!userInput || userInput === '\u2012')) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 458 | userInput = '0px'; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 459 | } else if (context.box === 'position' && (!userInput || userInput === '\u2012')) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 460 | userInput = 'auto'; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 461 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 462 | |
| 463 | userInput = userInput.toLowerCase(); |
| 464 | // Append a "px" unit if the user input was just a number. |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 465 | if (/^\d+$/.test(userInput)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 466 | userInput += 'px'; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 467 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 468 | |
| 469 | const styleProperty = context.styleProperty; |
| 470 | const computedStyle = context.computedStyle; |
| 471 | |
| 472 | if (computedStyle.get('box-sizing') === 'border-box' && (styleProperty === 'width' || styleProperty === 'height')) { |
| 473 | if (!userInput.match(/px$/)) { |
Paul Lewis | a83ea61 | 2020-03-04 13:01:36 | [diff] [blame] | 474 | Common.Console.Console.instance().error( |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 475 | 'For elements with box-sizing: border-box, only absolute content area dimensions can be applied'); |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | const borderBox = this._getBox(computedStyle, 'border'); |
| 480 | const paddingBox = this._getBox(computedStyle, 'padding'); |
| 481 | let userValuePx = Number(userInput.replace(/px$/, '')); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 482 | if (isNaN(userValuePx)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 483 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 484 | } |
| 485 | if (styleProperty === 'width') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 486 | userValuePx += borderBox.left + borderBox.right + paddingBox.left + paddingBox.right; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 487 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 488 | userValuePx += borderBox.top + borderBox.bottom + paddingBox.top + paddingBox.bottom; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 489 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 490 | |
| 491 | userInput = userValuePx + 'px'; |
| 492 | } |
| 493 | |
| 494 | this.previousPropertyDataCandidate = null; |
| 495 | |
| 496 | const allProperties = this._inlineStyle.allProperties(); |
| 497 | for (let i = 0; i < allProperties.length; ++i) { |
| 498 | const property = allProperties[i]; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 499 | if (property.name !== context.styleProperty || !property.activeInStyle()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 500 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 501 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 502 | |
| 503 | this.previousPropertyDataCandidate = property; |
| 504 | property.setValue(userInput, commitEditor, true, callback.bind(this)); |
| 505 | return; |
| 506 | } |
| 507 | |
| 508 | this._inlineStyle.appendProperty(context.styleProperty, userInput, callback.bind(this)); |
| 509 | |
| 510 | /** |
| 511 | * @param {boolean} success |
Tim van der Lippe | 13f71fb | 2019-11-29 11:17:39 | [diff] [blame] | 512 | * @this {MetricsSidebarPane} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 513 | */ |
| 514 | function callback(success) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 515 | if (!success) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 516 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 517 | } |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 518 | if (!this.originalPropertyData) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 519 | this.originalPropertyData = this.previousPropertyDataCandidate; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 520 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 521 | |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 522 | if (this._highlightMode) { |
| 523 | const node = this.node(); |
| 524 | if (!node) { |
| 525 | return; |
| 526 | } |
| 527 | node.highlight(this._highlightMode); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 528 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 529 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 530 | if (commitEditor) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 531 | this.update(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 532 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
Changhao Han | b87e7b7 | 2020-09-15 20:48:20 | [diff] [blame] | 536 | /** |
| 537 | * @param {!Element} element |
| 538 | * @param {string} userInput |
| 539 | * @param {string} previousContent |
| 540 | * @param {!{box: string, styleProperty: string, computedStyle: !Map.<string, string>, keyDownHandler: function(!Event):void}} context |
| 541 | */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 542 | _editingCommitted(element, userInput, previousContent, context) { |
| 543 | this.editingEnded(element, context); |
| 544 | this._applyUserInput(element, userInput, previousContent, context, true); |
| 545 | } |
Tim van der Lippe | 13f71fb | 2019-11-29 11:17:39 | [diff] [blame] | 546 | } |