summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/src/components/previews/FontPreview.js
blob: 2692fdefa04ff03647a9b4d175fcb22c8fa4fa07 (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";
const {
  gDevTools,
} = require("resource://devtools/client/framework/devtools.js");
const {
  Component,
} = require("resource://devtools/client/shared/vendor/react.js");
const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.js");
const {
  L10N,
} = require("resource://devtools/client/netmonitor/src/utils/l10n.js");

const {
  div,
  img,
} = require("resource://devtools/client/shared/vendor/react-dom-factories.js");

const { getColor } = require("resource://devtools/client/shared/theme.js");

const FONT_NAME = L10N.getStr("netmonitor.response.name");
const FONT_MIME_TYPE = L10N.getStr("netmonitor.response.mime");
const FONT_PREVIEW_FAILED = L10N.getStr(
  "netmonitor.response.fontPreviewFailed"
);

const FONT_PREVIEW_TEXT =
  "ABCDEFGHIJKLM\nNOPQRSTUVWXYZ\nabcdefghijklm\nnopqrstuvwxyz\n0123456789";

class FontPreview extends Component {
  static get propTypes() {
    return {
      connector: PropTypes.object.isRequired,
      mimeType: PropTypes.string,
      url: PropTypes.string,
    };
  }

  constructor(props) {
    super(props);

    this.state = {
      name: "",
      dataURL: "",
    };

    this.onThemeChanged = this.onThemeChanged.bind(this);
  }

  componentDidMount() {
    this.getPreview();

    // Listen for theme changes as the color of the preview depends on the theme
    gDevTools.on("theme-switched", this.onThemeChanged);
  }

  componentDidUpdate(prevProps) {
    const { url } = this.props;
    if (prevProps.url !== url) {
      this.getPreview();
    }
  }

  componentWillUnmount() {
    gDevTools.off("theme-switched", this.onThemeChanged);
  }

  /**
   * Handler for the "theme-switched" event.
   */
  onThemeChanged(frame) {
    if (frame === window) {
      this.getPreview();
    }
  }

  /**
   * Generate the font preview and receives information about the font.
   */
  async getPreview() {
    const { connector } = this.props;

    const toolbox = connector.getToolbox();
    const inspectorFront = await toolbox.target.getFront("inspector");
    const { pageStyle } = inspectorFront;
    const pageFontFaces = await pageStyle.getAllUsedFontFaces({
      includePreviews: true,
      includeVariations: false,
      previewText: FONT_PREVIEW_TEXT,
      previewFillStyle: getColor("body-color"),
    });

    const fontFace = pageFontFaces.find(
      pageFontFace => pageFontFace.URI === this.props.url
    );

    this.setState({
      name: fontFace?.name ?? "",
      dataURL: (await fontFace?.preview.data.string()) ?? "",
    });
  }

  render() {
    const { mimeType } = this.props;
    const { name, dataURL } = this.state;

    if (dataURL === "") {
      return div({ className: "empty-notice" }, FONT_PREVIEW_FAILED);
    }

    return div(
      { className: "panel-container response-font-box devtools-monospace" },
      img({
        className: "response-font",
        src: dataURL,
        alt: "",
      }),
      div(
        { className: "response-summary" },
        div({ className: "tabpanel-summary-label" }, FONT_NAME),
        div({ className: "tabpanel-summary-value" }, name)
      ),
      div(
        { className: "response-summary" },
        div({ className: "tabpanel-summary-label" }, FONT_MIME_TYPE),
        div({ className: "tabpanel-summary-value" }, mimeType)
      )
    );
  }
}

module.exports = FontPreview;