@khanacademy/math-input 13.1.0 → 14.0.1

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.
package/dist/index.d.ts CHANGED
@@ -2,6 +2,7 @@
2
2
  * A single entry-point for all of the external-facing functionality.
3
3
  */
4
4
  import "../less/main.less";
5
+ export { libVersion } from "./version";
5
6
  export { default as KeypadInput } from "./components/input/math-input";
6
7
  export { createMathField, mathQuillInstance, } from "./components/input/mathquill-instance";
7
8
  export { type MathFieldInterface } from "./components/input/mathquill-types";
@@ -9,7 +10,7 @@ export { CursorContext } from "./components/input/cursor-contexts";
9
10
  export { getCursorContext } from "./components/input/mathquill-helpers";
10
11
  export { default as MobileKeypad } from "./components/keypad-switch";
11
12
  export { default as DesktopKeypad } from "./components/keypad";
12
- export { default as KeypadContext } from "./components/keypad-context";
13
+ export { KeypadContext, StatefulKeypadContextProvider, } from "./components/keypad-context";
13
14
  export { keypadElementPropType } from "./components/prop-types";
14
15
  export type { KeypadAPI, KeypadConfiguration } from "./types";
15
16
  export type { default as Keys } from "./data/keys";
package/dist/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ var perseusCore = require('@khanacademy/perseus-core');
5
6
  var Color = require('@khanacademy/wonder-blocks-color');
6
7
  var i18n = require('@khanacademy/wonder-blocks-i18n');
7
8
  var wonderStuffCore = require('@khanacademy/wonder-stuff-core');
@@ -49,6 +50,11 @@ var katex__default = /*#__PURE__*/_interopDefaultLegacy(katex);
49
50
  var PropTypes__default = /*#__PURE__*/_interopDefaultLegacy(PropTypes);
50
51
  var Redux__namespace = /*#__PURE__*/_interopNamespace(Redux);
51
52
 
53
+ // This file is processed by a Rollup plugin (replace) to inject the production
54
+ const libName = "@khanacademy/math-input";
55
+ const libVersion = "14.0.1";
56
+ perseusCore.addLibraryVersionToPerseusDebug(libName, libVersion);
57
+
52
58
  function _defineProperty(obj, key, value) {
53
59
  key = _toPropertyKey(key);
54
60
  if (key in obj) {
@@ -5322,7 +5328,6 @@ function Keypad$2(props) {
5322
5328
  style: styles$d.keypadInnerContainer
5323
5329
  }, /*#__PURE__*/React__namespace.createElement(wonderBlocksCore.View, {
5324
5330
  style: [styles$d.keypadGrid, gridStyle],
5325
- role: "grid",
5326
5331
  tabIndex: 0,
5327
5332
  "aria-label": "Keypad"
5328
5333
  }, selectedPage === "Fractions" && /*#__PURE__*/React__namespace.createElement(FractionsPage, {
@@ -5384,17 +5389,6 @@ const styles$d = aphrodite.StyleSheet.create({
5384
5389
  }
5385
5390
  });
5386
5391
 
5387
- /**
5388
- * This is the v2 equivalent of v1's ProvidedKeypad. It follows the same
5389
- * external API so that it can be hot-swapped with the v1 keypad and
5390
- * is responsible for connecting the keypad with MathInput and the Renderer.
5391
- *
5392
- * Ideally this strategy of attaching methods on the class component for
5393
- * other components to call will be replaced props/callbacks since React
5394
- * doesn't support this type of code anymore (functional components
5395
- * can't have methods attached to them).
5396
- */
5397
-
5398
5392
  class MobileKeypad extends React__namespace.Component {
5399
5393
  constructor() {
5400
5394
  super(...arguments);
@@ -5403,7 +5397,6 @@ class MobileKeypad extends React__namespace.Component {
5403
5397
  _defineProperty(this, "_throttleResize", false);
5404
5398
  _defineProperty(this, "hasMounted", false);
5405
5399
  _defineProperty(this, "state", {
5406
- active: false,
5407
5400
  containerWidth: 0,
5408
5401
  hasBeenActivated: false
5409
5402
  });
@@ -5425,18 +5418,15 @@ class MobileKeypad extends React__namespace.Component {
5425
5418
  }, 100);
5426
5419
  });
5427
5420
  _defineProperty(this, "activate", () => {
5421
+ this.props.setKeypadActive(true);
5428
5422
  this.setState({
5429
- active: true,
5430
5423
  hasBeenActivated: true
5431
5424
  });
5432
5425
  });
5433
5426
  _defineProperty(this, "dismiss", () => {
5434
- this.setState({
5435
- active: false
5436
- }, () => {
5437
- var _this$props$onDismiss, _this$props;
5438
- (_this$props$onDismiss = (_this$props = this.props).onDismiss) === null || _this$props$onDismiss === void 0 ? void 0 : _this$props$onDismiss.call(_this$props);
5439
- });
5427
+ var _this$props$onDismiss, _this$props;
5428
+ this.props.setKeypadActive(false);
5429
+ (_this$props$onDismiss = (_this$props = this.props).onDismiss) === null || _this$props$onDismiss === void 0 ? void 0 : _this$props$onDismiss.call(_this$props);
5440
5430
  });
5441
5431
  _defineProperty(this, "configure", (configuration, cb) => {
5442
5432
  this.setState({
@@ -5501,10 +5491,10 @@ class MobileKeypad extends React__namespace.Component {
5501
5491
  }
5502
5492
  render() {
5503
5493
  const {
5494
+ keypadActive,
5504
5495
  style
5505
5496
  } = this.props;
5506
5497
  const {
5507
- active,
5508
5498
  hasBeenActivated,
5509
5499
  containerWidth,
5510
5500
  cursor,
@@ -5512,7 +5502,7 @@ class MobileKeypad extends React__namespace.Component {
5512
5502
  } = this.state;
5513
5503
  const containerStyle = [
5514
5504
  // internal styles
5515
- styles$c.keypadContainer, active && styles$c.activeKeypadContainer,
5505
+ styles$c.keypadContainer, keypadActive && styles$c.activeKeypadContainer,
5516
5506
  // styles passed as props
5517
5507
  ...(Array.isArray(style) ? style : [style])];
5518
5508
 
@@ -5521,7 +5511,7 @@ class MobileKeypad extends React__namespace.Component {
5521
5511
  // during the initial render.
5522
5512
  // Done inline (dynamicStyle) since stylesheets might not be loaded yet.
5523
5513
  let dynamicStyle = {};
5524
- if (!active && !hasBeenActivated) {
5514
+ if (!keypadActive && !hasBeenActivated) {
5525
5515
  dynamicStyle = {
5526
5516
  visibility: "hidden"
5527
5517
  };
@@ -5587,6 +5577,51 @@ const styles$c = aphrodite.StyleSheet.create({
5587
5577
  }
5588
5578
  });
5589
5579
 
5580
+ /**
5581
+ * KeypadContext provides a way to the Keypad and Perseus Renderers to
5582
+ * communicate.
5583
+ *
5584
+ * The StatefulKeypadContextProvider wraps the application
5585
+ * while KeypadContext.Consumer wraps things that need this state:
5586
+ * - mobile keypad usages
5587
+ * - Perseus Renderers (Server/Item/Article)
5588
+ */
5589
+ // @ts-expect-error - TS2322 - Type 'Context<{ setKeypadElement: (keypadElement: HTMLElement | null | undefined) => void; keypadElement: null; setRenderer: (renderer: RendererInterface | null | undefined) => void; renderer: null; setScrollableElement: (scrollableElement: HTMLElement | ... 1 more ... | undefined) => void; scrollableElement: null; }>' is not assignable to type 'Context<KeypadContext>'.
5590
+ const KeypadContext = /*#__PURE__*/React__namespace.createContext({
5591
+ setKeypadActive: keypadActive => {},
5592
+ keypadActive: false,
5593
+ setKeypadElement: keypadElement => {},
5594
+ keypadElement: null,
5595
+ setRenderer: renderer => {},
5596
+ renderer: null,
5597
+ setScrollableElement: scrollableElement => {},
5598
+ scrollableElement: null
5599
+ });
5600
+ function StatefulKeypadContextProvider(props) {
5601
+ // whether or not to display the keypad
5602
+ const [keypadActive, setKeypadActive] = React.useState(false);
5603
+ // used to communicate between the keypad and the Renderer
5604
+ const [keypadElement, setKeypadElement] = React.useState();
5605
+ // this is a KeypadContextRendererInterface from Perseus
5606
+ const [renderer, setRenderer] = React.useState();
5607
+ const [scrollableElement, setScrollableElement] = React.useState();
5608
+ return /*#__PURE__*/React__namespace.createElement(KeypadContext.Provider, {
5609
+ value: {
5610
+ setKeypadActive,
5611
+ keypadActive,
5612
+ setKeypadElement,
5613
+ keypadElement,
5614
+ setRenderer,
5615
+ renderer,
5616
+ // The scrollableElement options can likely be removed after
5617
+ // the exercises-package is officially deprecated. They don't appear
5618
+ // to be used anywhere except for the exercises-package and tests.
5619
+ setScrollableElement,
5620
+ scrollableElement
5621
+ }
5622
+ }, props.children);
5623
+ }
5624
+
5590
5625
  /**
5591
5626
  * A small triangular decal to sit in the corner of a parent component.
5592
5627
  */
@@ -9883,10 +9918,10 @@ class ProvidedKeypad extends React__namespace.Component {
9883
9918
  super(props);
9884
9919
  _defineProperty(this, "store", void 0);
9885
9920
  _defineProperty(this, "activate", () => {
9886
- this.store.dispatch(activateKeypad());
9921
+ this.props.setKeypadActive(true);
9887
9922
  });
9888
9923
  _defineProperty(this, "dismiss", () => {
9889
- this.store.dispatch(dismissKeypad());
9924
+ this.props.setKeypadActive(false);
9890
9925
  });
9891
9926
  _defineProperty(this, "configure", (configuration, cb) => {
9892
9927
  this.store.dispatch(configureKeypad(configuration));
@@ -9909,47 +9944,57 @@ class ProvidedKeypad extends React__namespace.Component {
9909
9944
  _defineProperty(this, "getDOMNode", () => {
9910
9945
  return ReactDOM__default["default"].findDOMNode(this);
9911
9946
  });
9947
+ _defineProperty(this, "onElementMounted", element => {
9948
+ var _this$props$onElement, _this$props;
9949
+ this.props.onAnalyticsEvent({
9950
+ type: "math-input:keypad-opened",
9951
+ payload: {
9952
+ virtualKeypadVersion: "MATH_INPUT_KEYPAD_V1"
9953
+ }
9954
+ });
9955
+
9956
+ // Append the dispatch methods that we want to expose
9957
+ // externally to the returned React element.
9958
+ const elementWithDispatchMethods = {
9959
+ ...element,
9960
+ activate: this.activate,
9961
+ dismiss: this.dismiss,
9962
+ configure: this.configure,
9963
+ setCursor: this.setCursor,
9964
+ setKeyHandler: this.setKeyHandler,
9965
+ getDOMNode: this.getDOMNode
9966
+ };
9967
+ (_this$props$onElement = (_this$props = this.props).onElementMounted) === null || _this$props$onElement === void 0 ? void 0 : _this$props$onElement.call(_this$props, elementWithDispatchMethods);
9968
+ });
9969
+ _defineProperty(this, "onDismiss", () => {
9970
+ var _this$props$onDismiss, _this$props2;
9971
+ this.props.onAnalyticsEvent({
9972
+ type: "math-input:keypad-closed",
9973
+ payload: {
9974
+ virtualKeypadVersion: "MATH_INPUT_KEYPAD_V1"
9975
+ }
9976
+ });
9977
+ (_this$props$onDismiss = (_this$props2 = this.props).onDismiss) === null || _this$props$onDismiss === void 0 ? void 0 : _this$props$onDismiss.call(_this$props2);
9978
+ });
9912
9979
  this.store = createStore();
9913
9980
  }
9981
+ componentDidUpdate(prevProps) {
9982
+ if (this.props.keypadActive && !prevProps.keypadActive) {
9983
+ this.store.dispatch(activateKeypad());
9984
+ }
9985
+ if (!this.props.keypadActive && prevProps.keypadActive) {
9986
+ this.store.dispatch(dismissKeypad());
9987
+ }
9988
+ }
9914
9989
  render() {
9915
9990
  const {
9916
- onElementMounted,
9917
- onDismiss,
9918
9991
  style
9919
9992
  } = this.props;
9920
9993
  return /*#__PURE__*/React__namespace.createElement(reactRedux.Provider, {
9921
9994
  store: this.store
9922
9995
  }, /*#__PURE__*/React__namespace.createElement(KeypadContainer$1, {
9923
- onElementMounted: element => {
9924
- this.props.onAnalyticsEvent({
9925
- type: "math-input:keypad-opened",
9926
- payload: {
9927
- virtualKeypadVersion: "MATH_INPUT_KEYPAD_V1"
9928
- }
9929
- });
9930
-
9931
- // Append the dispatch methods that we want to expose
9932
- // externally to the returned React element.
9933
- const elementWithDispatchMethods = {
9934
- ...element,
9935
- activate: this.activate,
9936
- dismiss: this.dismiss,
9937
- configure: this.configure,
9938
- setCursor: this.setCursor,
9939
- setKeyHandler: this.setKeyHandler,
9940
- getDOMNode: this.getDOMNode
9941
- };
9942
- onElementMounted === null || onElementMounted === void 0 ? void 0 : onElementMounted(elementWithDispatchMethods);
9943
- },
9944
- onDismiss: () => {
9945
- this.props.onAnalyticsEvent({
9946
- type: "math-input:keypad-closed",
9947
- payload: {
9948
- virtualKeypadVersion: "MATH_INPUT_KEYPAD_V1"
9949
- }
9950
- });
9951
- onDismiss === null || onDismiss === void 0 ? void 0 : onDismiss();
9952
- },
9996
+ onElementMounted: this.onElementMounted,
9997
+ onDismiss: this.onDismiss,
9953
9998
  style: style
9954
9999
  }));
9955
10000
  }
@@ -9965,27 +10010,18 @@ function KeypadSwitch(props) {
9965
10010
  // Note: Although we pass the "onAnalyticsEvent" to both keypad components,
9966
10011
  // only the current one uses it. There's no point in instrumenting the
9967
10012
  // legacy keypad given that it's on its way out the door.
9968
- return /*#__PURE__*/React__namespace.createElement(KeypadComponent, rest);
10013
+ return /*#__PURE__*/React__namespace.createElement(KeypadContext.Consumer, null, _ref => {
10014
+ let {
10015
+ setKeypadActive,
10016
+ keypadActive
10017
+ } = _ref;
10018
+ return /*#__PURE__*/React__namespace.createElement(KeypadComponent, _extends({}, rest, {
10019
+ keypadActive: keypadActive,
10020
+ setKeypadActive: setKeypadActive
10021
+ }));
10022
+ });
9969
10023
  }
9970
10024
 
9971
- /**
9972
- * KeypadContext provides a way to the Keypad and (Server)ItemRenderer to
9973
- * communicate.
9974
- *
9975
- * The KeypadContext.Provider wraps the ExerciseFooter while KeypadContext.Consumer
9976
- * wraps each (Server)ItemRenderer render site and the Keypad rendered in the
9977
- * ExerciseFooter.
9978
- */
9979
- // @ts-expect-error - TS2322 - Type 'Context<{ setKeypadElement: (keypadElement: HTMLElement | null | undefined) => void; keypadElement: null; setRenderer: (renderer: RendererInterface | null | undefined) => void; renderer: null; setScrollableElement: (scrollableElement: HTMLElement | ... 1 more ... | undefined) => void; scrollableElement: null; }>' is not assignable to type 'Context<KeypadContext>'.
9980
- const context = /*#__PURE__*/React__namespace.createContext({
9981
- setKeypadElement: keypadElement => {},
9982
- keypadElement: null,
9983
- setRenderer: renderer => {},
9984
- renderer: null,
9985
- setScrollableElement: scrollableElement => {},
9986
- scrollableElement: null
9987
- });
9988
-
9989
10025
  /**
9990
10026
  * React PropTypes that may be shared between components.
9991
10027
  */
@@ -10004,13 +10040,15 @@ const keypadElementPropType = PropTypes__default["default"].shape({
10004
10040
  exports.CursorContext = CursorContext;
10005
10041
  exports.DesktopKeypad = Keypad$2;
10006
10042
  exports.KeyConfigs = KeyConfigs;
10007
- exports.KeypadContext = context;
10043
+ exports.KeypadContext = KeypadContext;
10008
10044
  exports.KeypadInput = MathInput;
10009
10045
  exports.KeypadType = KeypadType;
10010
10046
  exports.MobileKeypad = KeypadSwitch;
10047
+ exports.StatefulKeypadContextProvider = StatefulKeypadContextProvider;
10011
10048
  exports.createMathField = createMathField;
10012
10049
  exports.getCursorContext = getCursorContext;
10013
10050
  exports.keyTranslator = keyToMathquillMap;
10014
10051
  exports.keypadElementPropType = keypadElementPropType;
10052
+ exports.libVersion = libVersion;
10015
10053
  exports.mathQuillInstance = mathQuillInstance;
10016
10054
  //# sourceMappingURL=index.js.map