Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(641)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/source_frame/FontView.js

Issue 2466123002: DevTools: reformat front-end code to match chromium style. (Closed)
Patch Set: all done Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 7 *
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the 11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution. 12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 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 14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission. 15 * from this software without specific prior written permission.
16 * 16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 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 18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 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 20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 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 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 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 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. 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28
29 /** 28 /**
30 * @constructor 29 * @unrestricted
31 * @extends {WebInspector.SimpleView}
32 * @param {string} mimeType
33 * @param {!WebInspector.ContentProvider} contentProvider
34 */ 30 */
35 WebInspector.FontView = function(mimeType, contentProvider) 31 WebInspector.FontView = class extends WebInspector.SimpleView {
36 { 32 /**
37 WebInspector.SimpleView.call(this, WebInspector.UIString("Font")); 33 * @param {string} mimeType
38 this.registerRequiredCSS("source_frame/fontView.css"); 34 * @param {!WebInspector.ContentProvider} contentProvider
39 this.element.classList.add("font-view"); 35 */
36 constructor(mimeType, contentProvider) {
37 super(WebInspector.UIString('Font'));
38 this.registerRequiredCSS('source_frame/fontView.css');
39 this.element.classList.add('font-view');
40 this._url = contentProvider.contentURL(); 40 this._url = contentProvider.contentURL();
41 this._mimeType = mimeType; 41 this._mimeType = mimeType;
42 this._contentProvider = contentProvider; 42 this._contentProvider = contentProvider;
43 this._mimeTypeLabel = new WebInspector.ToolbarText(mimeType); 43 this._mimeTypeLabel = new WebInspector.ToolbarText(mimeType);
44 }
45
46 /**
47 * @override
48 * @return {!Array<!WebInspector.ToolbarItem>}
49 */
50 syncToolbarItems() {
51 return [this._mimeTypeLabel];
52 }
53
54 /**
55 * @param {string} uniqueFontName
56 * @param {?string} content
57 */
58 _onFontContentLoaded(uniqueFontName, content) {
59 var url = content ? WebInspector.ContentProvider.contentAsDataURL(content, t his._mimeType, true) : this._url;
60 this.fontStyleElement.textContent =
61 String.sprintf('@font-face { font-family: "%s"; src: url(%s); }', unique FontName, url);
62 }
63
64 _createContentIfNeeded() {
65 if (this.fontPreviewElement)
66 return;
67
68 var uniqueFontName = 'WebInspectorFontPreview' + (++WebInspector.FontView._f ontId);
69
70 this.fontStyleElement = createElement('style');
71 this._contentProvider.requestContent().then(this._onFontContentLoaded.bind(t his, uniqueFontName));
72 this.element.appendChild(this.fontStyleElement);
73
74 var fontPreview = createElement('div');
75 for (var i = 0; i < WebInspector.FontView._fontPreviewLines.length; ++i) {
76 if (i > 0)
77 fontPreview.createChild('br');
78 fontPreview.createTextChild(WebInspector.FontView._fontPreviewLines[i]);
79 }
80 this.fontPreviewElement = fontPreview.cloneNode(true);
81 this.fontPreviewElement.style.setProperty('font-family', uniqueFontName);
82 this.fontPreviewElement.style.setProperty('visibility', 'hidden');
83
84 this._dummyElement = fontPreview;
85 this._dummyElement.style.visibility = 'hidden';
86 this._dummyElement.style.zIndex = '-1';
87 this._dummyElement.style.display = 'inline';
88 this._dummyElement.style.position = 'absolute';
89 this._dummyElement.style.setProperty('font-family', uniqueFontName);
90 this._dummyElement.style.setProperty('font-size', WebInspector.FontView._mea sureFontSize + 'px');
91
92 this.element.appendChild(this.fontPreviewElement);
93 }
94
95 /**
96 * @override
97 */
98 wasShown() {
99 this._createContentIfNeeded();
100
101 this.updateFontPreviewSize();
102 }
103
104 /**
105 * @override
106 */
107 onResize() {
108 if (this._inResize)
109 return;
110
111 this._inResize = true;
112 try {
113 this.updateFontPreviewSize();
114 } finally {
115 delete this._inResize;
116 }
117 }
118
119 _measureElement() {
120 this.element.appendChild(this._dummyElement);
121 var result = {width: this._dummyElement.offsetWidth, height: this._dummyElem ent.offsetHeight};
122 this.element.removeChild(this._dummyElement);
123
124 return result;
125 }
126
127 updateFontPreviewSize() {
128 if (!this.fontPreviewElement || !this.isShowing())
129 return;
130
131 this.fontPreviewElement.style.removeProperty('visibility');
132 var dimension = this._measureElement();
133
134 const height = dimension.height;
135 const width = dimension.width;
136
137 // Subtract some padding. This should match the paddings in the CSS plus roo m for the scrollbar.
138 const containerWidth = this.element.offsetWidth - 50;
139 const containerHeight = this.element.offsetHeight - 30;
140
141 if (!height || !width || !containerWidth || !containerHeight) {
142 this.fontPreviewElement.style.removeProperty('font-size');
143 return;
144 }
145
146 var widthRatio = containerWidth / width;
147 var heightRatio = containerHeight / height;
148 var finalFontSize = Math.floor(WebInspector.FontView._measureFontSize * Math .min(widthRatio, heightRatio)) - 2;
149
150 this.fontPreviewElement.style.setProperty('font-size', finalFontSize + 'px', null);
151 }
44 }; 152 };
45 153
46 WebInspector.FontView._fontPreviewLines = [ "ABCDEFGHIJKLM", "NOPQRSTUVWXYZ", "a bcdefghijklm", "nopqrstuvwxyz", "1234567890" ]; 154 WebInspector.FontView._fontPreviewLines =
155 ['ABCDEFGHIJKLM', 'NOPQRSTUVWXYZ', 'abcdefghijklm', 'nopqrstuvwxyz', '123456 7890'];
47 156
48 WebInspector.FontView._fontId = 0; 157 WebInspector.FontView._fontId = 0;
49 158
50 WebInspector.FontView._measureFontSize = 50; 159 WebInspector.FontView._measureFontSize = 50;
51
52 WebInspector.FontView.prototype = {
53 /**
54 * @override
55 * @return {!Array<!WebInspector.ToolbarItem>}
56 */
57 syncToolbarItems: function()
58 {
59 return [this._mimeTypeLabel];
60 },
61
62 /**
63 * @param {string} uniqueFontName
64 * @param {?string} content
65 */
66 _onFontContentLoaded: function(uniqueFontName, content)
67 {
68 var url = content ? WebInspector.ContentProvider.contentAsDataURL(conten t, this._mimeType, true) : this._url;
69 this.fontStyleElement.textContent = String.sprintf("@font-face { font-fa mily: \"%s\"; src: url(%s); }", uniqueFontName, url);
70 },
71
72 _createContentIfNeeded: function()
73 {
74 if (this.fontPreviewElement)
75 return;
76
77 var uniqueFontName = "WebInspectorFontPreview" + (++WebInspector.FontVie w._fontId);
78
79 this.fontStyleElement = createElement("style");
80 this._contentProvider.requestContent().then(this._onFontContentLoaded.bi nd(this, uniqueFontName));
81 this.element.appendChild(this.fontStyleElement);
82
83 var fontPreview = createElement("div");
84 for (var i = 0; i < WebInspector.FontView._fontPreviewLines.length; ++i) {
85 if (i > 0)
86 fontPreview.createChild("br");
87 fontPreview.createTextChild(WebInspector.FontView._fontPreviewLines[ i]);
88 }
89 this.fontPreviewElement = fontPreview.cloneNode(true);
90 this.fontPreviewElement.style.setProperty("font-family", uniqueFontName) ;
91 this.fontPreviewElement.style.setProperty("visibility", "hidden");
92
93 this._dummyElement = fontPreview;
94 this._dummyElement.style.visibility = "hidden";
95 this._dummyElement.style.zIndex = "-1";
96 this._dummyElement.style.display = "inline";
97 this._dummyElement.style.position = "absolute";
98 this._dummyElement.style.setProperty("font-family", uniqueFontName);
99 this._dummyElement.style.setProperty("font-size", WebInspector.FontView. _measureFontSize + "px");
100
101 this.element.appendChild(this.fontPreviewElement);
102 },
103
104 wasShown: function()
105 {
106 this._createContentIfNeeded();
107
108 this.updateFontPreviewSize();
109 },
110
111 onResize: function()
112 {
113 if (this._inResize)
114 return;
115
116 this._inResize = true;
117 try {
118 this.updateFontPreviewSize();
119 } finally {
120 delete this._inResize;
121 }
122 },
123
124 _measureElement: function()
125 {
126 this.element.appendChild(this._dummyElement);
127 var result = { width: this._dummyElement.offsetWidth, height: this._dumm yElement.offsetHeight };
128 this.element.removeChild(this._dummyElement);
129
130 return result;
131 },
132
133 updateFontPreviewSize: function()
134 {
135 if (!this.fontPreviewElement || !this.isShowing())
136 return;
137
138 this.fontPreviewElement.style.removeProperty("visibility");
139 var dimension = this._measureElement();
140
141 const height = dimension.height;
142 const width = dimension.width;
143
144 // Subtract some padding. This should match the paddings in the CSS plus room for the scrollbar.
145 const containerWidth = this.element.offsetWidth - 50;
146 const containerHeight = this.element.offsetHeight - 30;
147
148 if (!height || !width || !containerWidth || !containerHeight) {
149 this.fontPreviewElement.style.removeProperty("font-size");
150 return;
151 }
152
153 var widthRatio = containerWidth / width;
154 var heightRatio = containerHeight / height;
155 var finalFontSize = Math.floor(WebInspector.FontView._measureFontSize * Math.min(widthRatio, heightRatio)) - 2;
156
157 this.fontPreviewElement.style.setProperty("font-size", finalFontSize + " px", null);
158 },
159
160 __proto__: WebInspector.SimpleView.prototype
161 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698