@khanacademy/perseus-editor 4.2.2 → 4.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,85 @@
1
- declare const GraphSettings: any;
1
+ import * as React from "react";
2
+ import type { PerseusImageBackground } from "@khanacademy/perseus";
3
+ type EditableSetting = "canvas" | "graph" | "snap" | "image" | "measure";
4
+ type Props = {
5
+ editableSettings: ReadonlyArray<EditableSetting>;
6
+ box: [number, number];
7
+ labels: ReadonlyArray<string>;
8
+ range: [[number, number], [number, number]];
9
+ step: [number, number];
10
+ gridStep: [number, number];
11
+ snapStep: [number, number];
12
+ valid: boolean | string;
13
+ backgroundImage: PerseusImageBackground;
14
+ markings: "graph" | "grid" | "none";
15
+ showProtractor: boolean;
16
+ showRuler: boolean;
17
+ showTooltips: boolean;
18
+ rulerLabel: string;
19
+ rulerTicks: number;
20
+ onChange: (arg1: Partial<Props>) => void;
21
+ };
22
+ type State = {
23
+ labelsTextbox: ReadonlyArray<string>;
24
+ gridStepTextbox: [number, number];
25
+ snapStepTextbox: [number, number];
26
+ stepTextbox: [number, number];
27
+ rangeTextbox: [[number, number], [number, number]];
28
+ backgroundImage: PerseusImageBackground;
29
+ };
30
+ declare class GraphSettings extends React.Component<Props, State> {
31
+ _isMounted: boolean;
32
+ static stateFromProps(props: any): {
33
+ labelsTextbox: any;
34
+ gridStepTextbox: any;
35
+ snapStepTextbox: any;
36
+ stepTextbox: any;
37
+ rangeTextbox: any;
38
+ backgroundImage: any;
39
+ };
40
+ constructor(props: Props);
41
+ static defaultProps: {
42
+ editableSettings: string[];
43
+ box: 288[];
44
+ labels: string[];
45
+ range: number[][];
46
+ step: number[];
47
+ gridStep: number[];
48
+ snapStep: number[];
49
+ valid: boolean;
50
+ backgroundImage: {
51
+ readonly url: null;
52
+ readonly width: 0;
53
+ readonly height: 0;
54
+ };
55
+ markings: string;
56
+ showProtractor: boolean;
57
+ showRuler: boolean;
58
+ showTooltips: boolean;
59
+ rulerLabel: string;
60
+ rulerTicks: number;
61
+ };
62
+ componentDidMount(): void;
63
+ UNSAFE_componentWillReceiveProps(nextProps: any): void;
64
+ componentWillUnmount(): void;
65
+ change(...args: any[]): any;
66
+ changeRulerLabel(e: any): void;
67
+ changeRulerTicks(e: any): void;
68
+ changeBackgroundUrl(e: any): void;
69
+ renderLabelChoices(choices: any): JSX.Element[];
70
+ validRange(range: any): true | "Range must be a valid number" | "Range must have a higher number on the right";
71
+ validateStepValue(settings: any): string | true;
72
+ validSnapStep(step: any, range: any): string | true;
73
+ validGridStep(step: any, range: any): string | true;
74
+ validStep(step: any, range: any): string | true;
75
+ validBackgroundImageSize(image: any): true | "Image must be smaller than 450px x 450px.";
76
+ validateGraphSettings(range: any, step: any, gridStep: any, snapStep: any, image: any): any;
77
+ changeLabel(i: any, e: any): void;
78
+ changeRange(i: any, values: any): void;
79
+ changeStep(step: any): void;
80
+ changeSnapStep(snapStep: any): void;
81
+ changeGridStep(gridStep: any): void;
82
+ changeGraph(): void;
83
+ render(): JSX.Element;
84
+ }
2
85
  export default GraphSettings;
package/dist/es/index.js CHANGED
@@ -25,7 +25,7 @@ import * as i18n$1 from '@khanacademy/wonder-blocks-i18n';
25
25
 
26
26
  // This file is processed by a Rollup plugin (replace) to inject the production
27
27
  const libName = "@khanacademy/perseus-editor";
28
- const libVersion = "4.2.2";
28
+ const libVersion = "4.2.4";
29
29
  addLibraryVersionToPerseusDebug(libName, libVersion);
30
30
 
31
31
  function _extends() {
@@ -27170,6 +27170,7 @@ GradedGroupSetEditor.defaultProps = {
27170
27170
  };
27171
27171
  const setArrayItem = (list, i, value) => [...list.slice(0, i), value, ...list.slice(i + 1)];
27172
27172
 
27173
+ /* eslint-disable react/forbid-prop-types, react/no-unsafe */
27173
27174
  const {
27174
27175
  ButtonGroup: ButtonGroup$3,
27175
27176
  InfoTip: InfoTip$h,
@@ -27187,89 +27188,55 @@ const defaultBackgroundImage$2 = {
27187
27188
  function numSteps(range, step) {
27188
27189
  return Math.floor((range[1] - range[0]) / step);
27189
27190
  }
27190
- const GraphSettings = createReactClass({
27191
- displayName: "GraphSettings",
27192
- propTypes: _extends({}, Changeable.propTypes, {
27193
- editableSettings: PropTypes.arrayOf(PropTypes.oneOf(["canvas", "graph", "snap", "image", "measure"])),
27194
- box: PropTypes.arrayOf(PropTypes.number),
27195
- labels: PropTypes.arrayOf(PropTypes.string),
27196
- range: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.number)),
27197
- step: PropTypes.arrayOf(PropTypes.number),
27198
- gridStep: PropTypes.arrayOf(PropTypes.number),
27199
- snapStep: PropTypes.arrayOf(PropTypes.number),
27200
- valid: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
27201
- backgroundImage: PropTypes.object,
27202
- markings: PropTypes.oneOf(["graph", "grid", "none"]),
27203
- showProtractor: PropTypes.bool,
27204
- showRuler: PropTypes.bool,
27205
- showTooltips: PropTypes.bool,
27206
- rulerLabel: PropTypes.string,
27207
- rulerTicks: PropTypes.number
27208
- }),
27209
- getDefaultProps: function () {
27191
+ class GraphSettings extends React.Component {
27192
+ static stateFromProps(props) {
27210
27193
  return {
27211
- editableSettings: ["graph", "snap", "image", "measure"],
27212
- box: [interactiveSizes.defaultBoxSizeSmall, interactiveSizes.defaultBoxSizeSmall],
27213
- labels: ["x", "y"],
27214
- range: [[-10, 10], [-10, 10]],
27215
- step: [1, 1],
27216
- gridStep: [1, 1],
27217
- snapStep: [1, 1],
27218
- valid: true,
27219
- backgroundImage: defaultBackgroundImage$2,
27220
- markings: "graph",
27221
- showProtractor: false,
27222
- showRuler: false,
27223
- showTooltips: false,
27224
- rulerLabel: "",
27225
- rulerTicks: 10
27194
+ labelsTextbox: props.labels,
27195
+ gridStepTextbox: props.gridStep,
27196
+ snapStepTextbox: props.snapStep,
27197
+ stepTextbox: props.step,
27198
+ rangeTextbox: props.range,
27199
+ backgroundImage: _.clone(props.backgroundImage)
27226
27200
  };
27227
- },
27228
- getInitialState: function () {
27229
- return this.stateFromProps(this.props);
27230
- },
27231
- componentDidMount: function () {
27201
+ }
27202
+ constructor(props) {
27203
+ super(props);
27204
+ this._isMounted = false;
27205
+ this.state = GraphSettings.stateFromProps(props);
27206
+ }
27207
+ componentDidMount() {
27232
27208
  // TODO(scottgrant): This is a hack to remove the deprecated call to
27233
27209
  // this.isMounted() but is still considered an anti-pattern.
27234
27210
  this._isMounted = true;
27235
27211
  this.changeGraph = _.debounce(this.changeGraph, 300);
27236
- },
27237
- UNSAFE_componentWillReceiveProps: function (nextProps) {
27212
+ }
27213
+ UNSAFE_componentWillReceiveProps(nextProps) {
27238
27214
  // Make sure that state updates when switching
27239
27215
  // between different items in a multi-item editor.
27240
27216
  if (!_.isEqual(this.props.labels, nextProps.labels) || !_.isEqual(this.props.gridStep, nextProps.gridStep) || !_.isEqual(this.props.snapStep, nextProps.snapStep) || !_.isEqual(this.props.step, nextProps.step) || !_.isEqual(this.props.range, nextProps.range) || !_.isEqual(this.props.backgroundImage, nextProps.backgroundImage)) {
27241
- this.setState(this.stateFromProps(nextProps));
27217
+ this.setState(GraphSettings.stateFromProps(nextProps));
27242
27218
  }
27243
- },
27244
- componentWillUnmount: function () {
27219
+ }
27220
+ componentWillUnmount() {
27245
27221
  this._isMounted = false;
27246
- },
27247
- stateFromProps: function (props) {
27248
- return {
27249
- labelsTextbox: props.labels,
27250
- gridStepTextbox: props.gridStep,
27251
- snapStepTextbox: props.snapStep,
27252
- stepTextbox: props.step,
27253
- rangeTextbox: props.range,
27254
- backgroundImage: _.clone(props.backgroundImage)
27255
- };
27256
- },
27222
+ }
27257
27223
  change(...args) {
27258
27224
  return Changeable.change.apply(this, args);
27259
- },
27225
+ }
27226
+
27260
27227
  // TODO(aria): Make either a wrapper for standard events to work
27261
27228
  // with this.change, or make these use some TextInput/NumberInput box
27262
- changeRulerLabel: function (e) {
27229
+ changeRulerLabel(e) {
27263
27230
  this.change({
27264
27231
  rulerLabel: e.target.value
27265
27232
  });
27266
- },
27267
- changeRulerTicks: function (e) {
27233
+ }
27234
+ changeRulerTicks(e) {
27268
27235
  this.change({
27269
27236
  rulerTicks: +e.target.value
27270
27237
  });
27271
- },
27272
- changeBackgroundUrl: function (e) {
27238
+ }
27239
+ changeBackgroundUrl(e) {
27273
27240
  // Only continue on blur or "enter"
27274
27241
  if (e.type === "keypress" && e.key !== "Enter") {
27275
27242
  return;
@@ -27295,15 +27262,15 @@ const GraphSettings = createReactClass({
27295
27262
  } else {
27296
27263
  setUrl(null, 0, 0);
27297
27264
  }
27298
- },
27299
- renderLabelChoices: function (choices) {
27265
+ }
27266
+ renderLabelChoices(choices) {
27300
27267
  return _.map(choices, function (nameAndValue) {
27301
27268
  return /*#__PURE__*/React.createElement("option", {
27302
27269
  value: nameAndValue[1]
27303
27270
  }, nameAndValue[0]);
27304
27271
  });
27305
- },
27306
- validRange: function (range) {
27272
+ }
27273
+ validRange(range) {
27307
27274
  const numbers = _.every(range, function (num) {
27308
27275
  return _.isFinite(num);
27309
27276
  });
@@ -27314,8 +27281,8 @@ const GraphSettings = createReactClass({
27314
27281
  return "Range must have a higher number on the right";
27315
27282
  }
27316
27283
  return true;
27317
- },
27318
- validateStepValue: function (settings) {
27284
+ }
27285
+ validateStepValue(settings) {
27319
27286
  const {
27320
27287
  step,
27321
27288
  range,
@@ -27334,8 +27301,8 @@ const GraphSettings = createReactClass({
27334
27301
  return name + " is too small, there can be at most " + maxTicks + " ticks.";
27335
27302
  }
27336
27303
  return true;
27337
- },
27338
- validSnapStep: function (step, range) {
27304
+ }
27305
+ validSnapStep(step, range) {
27339
27306
  return this.validateStepValue({
27340
27307
  step: step,
27341
27308
  range: range,
@@ -27343,8 +27310,8 @@ const GraphSettings = createReactClass({
27343
27310
  minTicks: 5,
27344
27311
  maxTicks: 60
27345
27312
  });
27346
- },
27347
- validGridStep: function (step, range) {
27313
+ }
27314
+ validGridStep(step, range) {
27348
27315
  return this.validateStepValue({
27349
27316
  step: step,
27350
27317
  range: range,
@@ -27352,8 +27319,8 @@ const GraphSettings = createReactClass({
27352
27319
  minTicks: 3,
27353
27320
  maxTicks: 60
27354
27321
  });
27355
- },
27356
- validStep: function (step, range) {
27322
+ }
27323
+ validStep(step, range) {
27357
27324
  return this.validateStepValue({
27358
27325
  step: step,
27359
27326
  range: range,
@@ -27361,8 +27328,8 @@ const GraphSettings = createReactClass({
27361
27328
  minTicks: 3,
27362
27329
  maxTicks: 20
27363
27330
  });
27364
- },
27365
- validBackgroundImageSize: function (image) {
27331
+ }
27332
+ validBackgroundImageSize(image) {
27366
27333
  // Ignore empty images
27367
27334
  if (!image.url) {
27368
27335
  return true;
@@ -27372,8 +27339,8 @@ const GraphSettings = createReactClass({
27372
27339
  return "Image must be smaller than 450px x 450px.";
27373
27340
  }
27374
27341
  return true;
27375
- },
27376
- validateGraphSettings: function (range, step, gridStep, snapStep, image) {
27342
+ }
27343
+ validateGraphSettings(range, step, gridStep, snapStep, image) {
27377
27344
  const self = this;
27378
27345
  let msg;
27379
27346
  const goodRange = _.every(range, function (range) {
@@ -27410,16 +27377,16 @@ const GraphSettings = createReactClass({
27410
27377
  return msg;
27411
27378
  }
27412
27379
  return true;
27413
- },
27414
- changeLabel: function (i, e) {
27380
+ }
27381
+ changeLabel(i, e) {
27415
27382
  const val = e.target.value;
27416
27383
  const labels = this.state.labelsTextbox.slice();
27417
27384
  labels[i] = val;
27418
27385
  this.setState({
27419
27386
  labelsTextbox: labels
27420
27387
  }, this.changeGraph);
27421
- },
27422
- changeRange: function (i, values) {
27388
+ }
27389
+ changeRange(i, values) {
27423
27390
  const ranges = this.state.rangeTextbox.slice();
27424
27391
  ranges[i] = values;
27425
27392
  const step = this.state.stepTextbox.slice();
@@ -27428,7 +27395,10 @@ const GraphSettings = createReactClass({
27428
27395
  const scale = Util.scaleFromExtent(ranges[i], this.props.box[i]);
27429
27396
  if (this.validRange(ranges[i]) === true) {
27430
27397
  step[i] = Util.tickStepFromExtent(ranges[i], this.props.box[i]);
27431
- gridStep[i] = Util.gridStepFromTickStep(step[i], scale);
27398
+ const gridStepValue = Util.gridStepFromTickStep(step[i], scale);
27399
+ if (gridStepValue) {
27400
+ gridStep[i] = gridStepValue;
27401
+ }
27432
27402
  snapStep[i] = gridStep[i] / 2;
27433
27403
  }
27434
27404
  this.setState({
@@ -27437,26 +27407,26 @@ const GraphSettings = createReactClass({
27437
27407
  snapStepTextbox: snapStep,
27438
27408
  rangeTextbox: ranges
27439
27409
  }, this.changeGraph);
27440
- },
27441
- changeStep: function (step) {
27410
+ }
27411
+ changeStep(step) {
27442
27412
  this.setState({
27443
27413
  stepTextbox: step
27444
27414
  }, this.changeGraph);
27445
- },
27446
- changeSnapStep: function (snapStep) {
27415
+ }
27416
+ changeSnapStep(snapStep) {
27447
27417
  this.setState({
27448
27418
  snapStepTextbox: snapStep
27449
27419
  }, this.changeGraph);
27450
- },
27451
- changeGridStep: function (gridStep) {
27420
+ }
27421
+ changeGridStep(gridStep) {
27452
27422
  this.setState({
27453
27423
  gridStepTextbox: gridStep,
27454
27424
  snapStepTextbox: _.map(gridStep, function (step) {
27455
27425
  return step / 2;
27456
27426
  })
27457
27427
  }, this.changeGraph);
27458
- },
27459
- changeGraph: function () {
27428
+ }
27429
+ changeGraph() {
27460
27430
  const labels = this.state.labelsTextbox;
27461
27431
  const range = _.map(this.state.rangeTextbox, function (range) {
27462
27432
  return _.map(range, Number);
@@ -27488,9 +27458,9 @@ const GraphSettings = createReactClass({
27488
27458
  valid: validationResult // a string message, not false
27489
27459
  });
27490
27460
  }
27491
- },
27461
+ }
27492
27462
 
27493
- render: function () {
27463
+ render() {
27494
27464
  const scale = [KhanMath.roundTo(2, Util.scaleFromExtent(this.props.range[0], this.props.box[0])), KhanMath.roundTo(2, Util.scaleFromExtent(this.props.range[1], this.props.box[1]))];
27495
27465
  const {
27496
27466
  TeX
@@ -27637,7 +27607,24 @@ const GraphSettings = createReactClass({
27637
27607
  }, n);
27638
27608
  })))))));
27639
27609
  }
27640
- });
27610
+ }
27611
+ GraphSettings.defaultProps = {
27612
+ editableSettings: ["graph", "snap", "image", "measure"],
27613
+ box: [interactiveSizes.defaultBoxSizeSmall, interactiveSizes.defaultBoxSizeSmall],
27614
+ labels: ["x", "y"],
27615
+ range: [[-10, 10], [-10, 10]],
27616
+ step: [1, 1],
27617
+ gridStep: [1, 1],
27618
+ snapStep: [1, 1],
27619
+ valid: true,
27620
+ backgroundImage: defaultBackgroundImage$2,
27621
+ markings: "graph",
27622
+ showProtractor: false,
27623
+ showRuler: false,
27624
+ showTooltips: false,
27625
+ rulerLabel: "",
27626
+ rulerTicks: 10
27627
+ };
27641
27628
 
27642
27629
  const {
27643
27630
  InfoTip: InfoTip$g,