summaryrefslogtreecommitdiffstats
path: root/devtools/client/inspector/fonts/components/FontPropertyValue.js
blob: 59920818c44020a49885ad7eeb996af3b83769ce (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/* 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 {
  createElement,
  Fragment,
  PureComponent,
} = require("resource://devtools/client/shared/vendor/react.js");
const dom = require("resource://devtools/client/shared/vendor/react-dom-factories.js");
const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.js");

const {
  toFixed,
} = require("resource://devtools/client/inspector/fonts/utils/font-utils.js");

class FontPropertyValue extends PureComponent {
  static get propTypes() {
    return {
      // Whether to allow input values above the value defined by the `max` prop.
      allowOverflow: PropTypes.bool,
      // Whether to allow input values below the value defined by the `min` prop.
      allowUnderflow: PropTypes.bool,
      className: PropTypes.string,
      defaultValue: PropTypes.number,
      disabled: PropTypes.bool.isRequired,
      label: PropTypes.string.isRequired,
      min: PropTypes.number.isRequired,
      // Whether to show the `min` prop value as a label.
      minLabel: PropTypes.bool,
      max: PropTypes.number.isRequired,
      // Whether to show the `max` prop value as a label.
      maxLabel: PropTypes.bool,
      name: PropTypes.string.isRequired,
      // Whether to show the `name` prop value as an extra label (used to show axis tags).
      nameLabel: PropTypes.bool,
      onChange: PropTypes.func.isRequired,
      step: PropTypes.number,
      // Whether to show the value input field.
      showInput: PropTypes.bool,
      // Whether to show the unit select dropdown.
      showUnit: PropTypes.bool,
      unit: PropTypes.string,
      unitOptions: PropTypes.array,
      value: PropTypes.number,
      valueLabel: PropTypes.string,
    };
  }

  static get defaultProps() {
    return {
      allowOverflow: false,
      allowUnderflow: false,
      className: "",
      minLabel: false,
      maxLabel: false,
      nameLabel: false,
      step: 1,
      showInput: true,
      showUnit: true,
      unit: null,
      unitOptions: [],
    };
  }

  constructor(props) {
    super(props);
    this.state = {
      // Whether the user is dragging the slider thumb or pressing on the numeric stepper.
      interactive: false,
      // Snapshot of the value from props before the user starts editing the number input.
      // Used to restore the value when the input is left invalid.
      initialValue: this.props.value,
      // Snapshot of the value from props. Reconciled with props on blur.
      // Used while the user is interacting with the inputs.
      value: this.props.value,
    };

    this.onBlur = this.onBlur.bind(this);
    this.onChange = this.onChange.bind(this);
    this.onFocus = this.onFocus.bind(this);
    this.onMouseDown = this.onMouseDown.bind(this);
    this.onMouseUp = this.onMouseUp.bind(this);
    this.onUnitChange = this.onUnitChange.bind(this);
  }

  /**
   * Given a `prop` key found on the component's props, check the matching `propLabel`.
   * If `propLabel` is true, return the `prop` value; Otherwise, return null.
   *
   * @param {String} prop
   *        Key found on the component's props.
   * @return {Number|null}
   */
  getPropLabel(prop) {
    const label = this.props[`${prop}Label`];
    // Decimal count used to limit numbers in labels.
    const decimals = Math.abs(Math.log10(this.props.step));

    return label ? toFixed(this.props[prop], decimals) : null;
  }

  /**
   * Check if the given value is valid according to the constraints of this component.
   * Ensure it is a number and that it does not go outside the min/max limits, unless
   * allowed by the `allowOverflow` and `allowUnderflow` props.
   *
   * @param  {Number} value
   *         Numeric value
   * @return {Boolean}
   *         Whether the value conforms to the components contraints.
   */
  isValueValid(value) {
    const { allowOverflow, allowUnderflow, min, max } = this.props;

    if (typeof value !== "number" || isNaN(value)) {
      return false;
    }

    // Ensure it does not go below minimum value, unless underflow is allowed.
    if (min !== undefined && value < min && !allowUnderflow) {
      return false;
    }

    // Ensure it does not go over maximum value, unless overflow is allowed.
    if (max !== undefined && value > max && !allowOverflow) {
      return false;
    }

    return true;
  }

  /**
   * Handler for "blur" events from the range and number input fields.
   * Reconciles the value between internal state and props.
   * Marks the input as non-interactive so it may update in response to changes in props.
   */
  onBlur() {
    const isValid = this.isValueValid(this.state.value);
    let value;

    if (isValid) {
      value = this.state.value;
    } else if (this.state.value !== null) {
      value = Math.max(
        this.props.min,
        Math.min(this.state.value, this.props.max)
      );
    } else {
      value = this.state.initialValue;
    }

    // Avoid updating the value if a keyword value like "normal" is present
    if (!this.props.valueLabel) {
      this.updateValue(value);
    }

    this.toggleInteractiveState(false);
  }

  /**
   * Handler for "change" events from the range and number input fields. Calls the change
   * handler provided with props and updates internal state with the current value.
   *
   * Number inputs in Firefox can't be trusted to filter out non-digit characters,
   * therefore we must implement our own validation.
   * @see https://bugzilla.mozilla.org/show_bug.cgi?id=1398528
   *
   * @param {Event} e
   *        Change event.
   */
  onChange(e) {
    // Regular expresion to check for floating point or integer numbers. Accept negative
    // numbers only if the min value is negative. Otherwise, expect positive numbers.
    // Whitespace and non-digit characters are invalid (aside from a single dot).
    const regex =
      this.props.min && this.props.min < 0
        ? /^-?[0-9]+(.[0-9]+)?$/
        : /^[0-9]+(.[0-9]+)?$/;
    let string = e.target.value.trim();

    if (e.target.validity.badInput) {
      return;
    }

    // Prefix with zero if the string starts with a dot: .5 => 0.5
    if (string.charAt(0) === "." && string.length > 1) {
      string = "0" + string;
      e.target.value = string;
    }

    // Accept empty strings to allow the input value to be completely erased while typing.
    // A null value will be handled on blur. @see this.onBlur()
    if (string === "") {
      this.setState(prevState => {
        return {
          ...prevState,
          value: null,
        };
      });

      return;
    }

    if (!regex.test(string)) {
      return;
    }

    const value = parseFloat(string);
    this.updateValue(value);
  }

  onFocus(e) {
    if (e.target.type === "number") {
      e.target.select();
    }

    this.setState(prevState => {
      return {
        ...prevState,
        interactive: true,
        initialValue: this.props.value,
      };
    });
  }

  onUnitChange(e) {
    this.props.onChange(
      this.props.name,
      this.props.value,
      this.props.unit,
      e.target.value
    );
    // Reset internal state value and wait for converted value from props.
    this.setState(prevState => {
      return {
        ...prevState,
        value: null,
      };
    });
  }

  onMouseDown() {
    this.toggleInteractiveState(true);
  }

  onMouseUp() {
    this.toggleInteractiveState(false);
  }

  /**
   * Toggle the "interactive" state which causes render() to use `value` fom internal
   * state instead of from props to prevent jittering during continous dragging of the
   * range input thumb or incrementing from the number input.
   *
   * @param {Boolean} isInteractive
   *        Whether to mark the interactive state on or off.
   */
  toggleInteractiveState(isInteractive) {
    this.setState(prevState => {
      return {
        ...prevState,
        interactive: isInteractive,
      };
    });
  }

  /**
   * Calls the given `onChange` callback with the current property name, value and unit
   * if the value is valid according to the constraints of this component (min, max).
   * Updates the internal state with the current value. This will be used to render the
   * UI while the input is interactive and the user may be typing a value that's not yet
   * valid.
   *
   * @see this.onBlur() for logic reconciling the internal state with props.
   *
   * @param {Number} value
   *        Numeric property value.
   */
  updateValue(value) {
    if (this.isValueValid(value)) {
      this.props.onChange(this.props.name, value, this.props.unit);
    }

    this.setState(prevState => {
      return {
        ...prevState,
        value,
      };
    });
  }

  renderUnitSelect() {
    if (!this.props.unitOptions.length) {
      return null;
    }

    // Ensure the select element has the current unit type even if we don't recognize it.
    // The unit conversion function will use a 1-to-1 scale for unrecognized units.
    const options = this.props.unitOptions.includes(this.props.unit)
      ? this.props.unitOptions
      : this.props.unitOptions.concat([this.props.unit]);

    return dom.select(
      {
        className: "font-value-select",
        disabled: this.props.disabled,
        onChange: this.onUnitChange,
        value: this.props.unit,
      },
      options.map(unit => {
        return dom.option(
          {
            key: unit,
            value: unit,
          },
          unit
        );
      })
    );
  }

  renderLabelContent() {
    const { label, name, nameLabel } = this.props;

    const labelEl = dom.span(
      {
        className: "font-control-label-text",
        "aria-describedby": nameLabel ? `detail-${name}` : null,
      },
      label
    );

    // Show the `name` prop value as an additional label if the `nameLabel` prop is true.
    const detailEl = nameLabel
      ? dom.span(
          {
            className: "font-control-label-detail",
            id: `detail-${name}`,
          },
          this.getPropLabel("name")
        )
      : null;

    return createElement(Fragment, null, labelEl, detailEl);
  }

  renderValueLabel() {
    if (!this.props.valueLabel) {
      return null;
    }

    return dom.div({ className: "font-value-label" }, this.props.valueLabel);
  }

  render() {
    // Guard against bad axis data.
    if (this.props.min === this.props.max) {
      return null;
    }

    const propsValue =
      this.props.value !== null ? this.props.value : this.props.defaultValue;

    const defaults = {
      min: this.props.min,
      max: this.props.max,
      onBlur: this.onBlur,
      onChange: this.onChange,
      onFocus: this.onFocus,
      step: this.props.step,
      // While interacting with the range and number inputs, prevent updating value from
      // outside props which is debounced and causes jitter on successive renders.
      value: this.state.interactive ? this.state.value : propsValue,
    };

    const range = dom.input({
      ...defaults,
      onMouseDown: this.onMouseDown,
      onMouseUp: this.onMouseUp,
      className: "font-value-slider",
      disabled: this.props.disabled,
      name: this.props.name,
      title: this.props.label,
      type: "range",
    });

    const input = dom.input({
      ...defaults,
      // Remove lower limit from number input if it is allowed to underflow.
      min: this.props.allowUnderflow ? null : this.props.min,
      // Remove upper limit from number input if it is allowed to overflow.
      max: this.props.allowOverflow ? null : this.props.max,
      name: this.props.name,
      className: "font-value-input",
      disabled: this.props.disabled,
      type: "number",
    });

    return dom.label(
      {
        className: `font-control ${this.props.className}`,
        disabled: this.props.disabled,
      },
      dom.div(
        {
          className: "font-control-label",
          title: this.props.label,
        },
        this.renderLabelContent()
      ),
      dom.div(
        {
          className: "font-control-input",
        },
        dom.div(
          {
            className: "font-value-slider-container",
            "data-min": this.getPropLabel("min"),
            "data-max": this.getPropLabel("max"),
          },
          range
        ),
        this.renderValueLabel(),
        this.props.showInput && input,
        this.props.showUnit && this.renderUnitSelect()
      )
    );
  }
}

module.exports = FontPropertyValue;