FontPreview.js (3614B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 const { 7 gDevTools, 8 } = require("resource://devtools/client/framework/devtools.js"); 9 const { 10 Component, 11 } = require("resource://devtools/client/shared/vendor/react.mjs"); 12 const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.mjs"); 13 const { 14 L10N, 15 } = require("resource://devtools/client/netmonitor/src/utils/l10n.js"); 16 17 const { 18 div, 19 img, 20 } = require("resource://devtools/client/shared/vendor/react-dom-factories.js"); 21 22 const { 23 getCssVariableColor, 24 } = require("resource://devtools/client/shared/theme.js"); 25 26 const FONT_NAME = L10N.getStr("netmonitor.response.name"); 27 const FONT_MIME_TYPE = L10N.getStr("netmonitor.response.mime"); 28 const FONT_PREVIEW_FAILED = L10N.getStr( 29 "netmonitor.response.fontPreviewFailed" 30 ); 31 32 const FONT_PREVIEW_TEXT = 33 "ABCDEFGHIJKLM\nNOPQRSTUVWXYZ\nabcdefghijklm\nnopqrstuvwxyz\n0123456789"; 34 35 class FontPreview extends Component { 36 static get propTypes() { 37 return { 38 connector: PropTypes.object.isRequired, 39 mimeType: PropTypes.string, 40 url: PropTypes.string, 41 }; 42 } 43 44 constructor(props) { 45 super(props); 46 47 this.state = { 48 name: "", 49 dataURL: "", 50 }; 51 52 this.onThemeChanged = this.onThemeChanged.bind(this); 53 } 54 55 componentDidMount() { 56 this.getPreview(); 57 58 // Listen for theme changes as the color of the preview depends on the theme 59 gDevTools.on("theme-switched", this.onThemeChanged); 60 } 61 62 componentDidUpdate(prevProps) { 63 const { url } = this.props; 64 if (prevProps.url !== url) { 65 this.getPreview(); 66 } 67 } 68 69 componentWillUnmount() { 70 gDevTools.off("theme-switched", this.onThemeChanged); 71 } 72 73 /** 74 * Handler for the "theme-switched" event. 75 */ 76 onThemeChanged(frame) { 77 if (frame === window) { 78 this.getPreview(); 79 } 80 } 81 82 /** 83 * Generate the font preview and receives information about the font. 84 */ 85 async getPreview() { 86 const { connector } = this.props; 87 88 const toolbox = connector.getToolbox(); 89 const inspectorFront = await toolbox.target.getFront("inspector"); 90 const { pageStyle } = inspectorFront; 91 const pageFontFaces = await pageStyle.getAllUsedFontFaces({ 92 includePreviews: true, 93 includeVariations: false, 94 previewText: FONT_PREVIEW_TEXT, 95 previewFillStyle: getCssVariableColor("--theme-body-color", toolbox.win), 96 }); 97 98 const fontFace = pageFontFaces.find( 99 pageFontFace => pageFontFace.URI === this.props.url 100 ); 101 102 this.setState({ 103 name: fontFace?.name ?? "", 104 dataURL: (await fontFace?.preview.data.string()) ?? "", 105 }); 106 } 107 108 render() { 109 const { mimeType } = this.props; 110 const { name, dataURL } = this.state; 111 112 if (dataURL === "") { 113 return div({ className: "empty-notice" }, FONT_PREVIEW_FAILED); 114 } 115 116 return div( 117 { className: "panel-container response-font-box devtools-monospace" }, 118 img({ 119 className: "response-font", 120 src: dataURL, 121 alt: "", 122 }), 123 div( 124 { className: "response-summary" }, 125 div({ className: "tabpanel-summary-label" }, FONT_NAME), 126 div({ className: "tabpanel-summary-value" }, name) 127 ), 128 div( 129 { className: "response-summary" }, 130 div({ className: "tabpanel-summary-label" }, FONT_MIME_TYPE), 131 div({ className: "tabpanel-summary-value" }, mimeType) 132 ) 133 ); 134 } 135 } 136 137 module.exports = FontPreview;