@khanacademy/math-input 22.2.6 → 23.0.0

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/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Copyright 2022 Khan Academy
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to use,
6
+ copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
7
+ Software, and to permit persons to whom the Software is furnished to do so, subject
8
+ to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
17
+ AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -11,8 +11,8 @@ Try it yourself on [Khan Academy](https://www.khanacademy.org/math/algebra-basic
11
11
  ## Getting started
12
12
 
13
13
  - Clone the project: `git clone git@github.com:Khan/math-input.git`
14
- - Install dependencies: `yarn`
15
- - Start the development server: `yarn start`
14
+ - Install dependencies: `pnpm install`
15
+ - Start the development server: `pnpm start`
16
16
 
17
17
  When you start the project you'll need to be on a device that supports touch or touch emulation to see the keyboard. The easiest way to get started is open the developer tools on Google Chrome.
18
18
 
@@ -1,6 +1,6 @@
1
1
  import type { MathFieldInterface, MathFieldUpdaterCallback } from "./mathquill-types";
2
- import type Key from "../../data/keys";
3
2
  import type { MathInputStrings } from "../../strings";
3
+ import type { KeypadKey } from "@khanacademy/perseus-core";
4
4
  /**
5
5
  * This file contains a wrapper around MathQuill so that we can provide a
6
6
  * more regular interface for the functionality we need while insulating us
@@ -9,7 +9,7 @@ import type { MathInputStrings } from "../../strings";
9
9
  declare class MathWrapper {
10
10
  mathField: MathFieldInterface;
11
11
  callbacks: any;
12
- mobileKeyTranslator: Record<Key, MathFieldUpdaterCallback>;
12
+ mobileKeyTranslator: Record<KeypadKey, MathFieldUpdaterCallback>;
13
13
  constructor(mathFieldMount: any, ariaLabel: string, strings: MathInputStrings, locale: string, callbacks?: {});
14
14
  focus(): void;
15
15
  blur(): void;
@@ -19,7 +19,7 @@ declare class MathWrapper {
19
19
  * @param {Key} key - an enum representing the key that was pressed
20
20
  * @returns {object} a cursor object, consisting of a cursor context
21
21
  */
22
- pressKey(key: Key): {
22
+ pressKey(key: KeypadKey): {
23
23
  context: import("./cursor-contexts").CursorContext;
24
24
  };
25
25
  /**
@@ -1,4 +1,4 @@
1
- import type Key from "../../data/keys";
1
+ import type { KeypadKey } from "@khanacademy/perseus-core";
2
2
  import type MathQuill from "mathquill";
3
3
  export type MathQuillInterface = MathQuill.v3.API;
4
4
  export type MathFieldConfig = MathQuill.v3.Config;
@@ -11,7 +11,7 @@ export type MathFieldInterface = MathQuill.v3.EditableMathQuill & {
11
11
  cursor: () => MathQuill.Cursor;
12
12
  controller: () => MathQuill.Controller;
13
13
  };
14
- export type MathFieldUpdaterCallback = (mathField: MathFieldInterface, key: Key) => void;
14
+ export type MathFieldUpdaterCallback = (mathField: MathFieldInterface, key: KeypadKey) => void;
15
15
  /**
16
16
  * The MathQuill API (see mathuill.d.ts) does not include types
17
17
  * for cursor() and controller(), and adding these types there
@@ -1,3 +1,3 @@
1
- import type Key from "../../data/keys";
2
1
  import type { MathFieldInterface } from "../input/mathquill-types";
3
- export default function handleArrow(mathField: MathFieldInterface, key: Key): void;
2
+ import type { KeypadKey } from "@khanacademy/perseus-core";
3
+ export default function handleArrow(mathField: MathFieldInterface, key: KeypadKey): void;
@@ -1,3 +1,3 @@
1
- import type Key from "../../data/keys";
2
1
  import type { MathFieldInterface } from "../input/mathquill-types";
3
- export default function handleExponent(mathField: MathFieldInterface, key: Key): void;
2
+ import type { KeypadKey } from "@khanacademy/perseus-core";
3
+ export default function handleExponent(mathField: MathFieldInterface, key: KeypadKey): void;
@@ -1,7 +1,7 @@
1
- import type Key from "../../data/keys";
2
1
  import type { MathFieldInterface } from "../input/mathquill-types";
2
+ import type { KeypadKey } from "@khanacademy/perseus-core";
3
3
  /**
4
4
  * Advances the cursor to the next logical position.
5
5
  */
6
- declare function handleJumpOut(mathField: MathFieldInterface, key: Key): void;
6
+ declare function handleJumpOut(mathField: MathFieldInterface, key: KeypadKey): void;
7
7
  export default handleJumpOut;
@@ -1,9 +1,9 @@
1
- import type Key from "../../data/keys";
2
1
  import type { MathFieldUpdaterCallback } from "../input/mathquill-types";
2
+ import type { KeypadKey } from "@khanacademy/perseus-core";
3
3
  type KeyTranslatorStrings = {
4
4
  sin: string;
5
5
  cos: string;
6
6
  tan: string;
7
7
  };
8
- export declare const getKeyTranslator: (locale: string, strings: KeyTranslatorStrings) => Record<Key, MathFieldUpdaterCallback>;
8
+ export declare const getKeyTranslator: (locale: string, strings: KeyTranslatorStrings) => Record<KeypadKey, MathFieldUpdaterCallback>;
9
9
  export {};
@@ -1,7 +1,7 @@
1
1
  import * as React from "react";
2
- import type Key from "../../data/keys";
2
+ import type { KeypadKey } from "@khanacademy/perseus-core";
3
3
  type Props = {
4
- id: Key;
4
+ id: KeypadKey;
5
5
  };
6
6
  export default function ButtonAsset({ id }: Props): React.ReactNode;
7
7
  export {};
@@ -1,8 +1,8 @@
1
1
  import * as React from "react";
2
- import type Key from "../../../data/keys";
3
2
  import type { ClickKeyCallback } from "../../../types";
3
+ import type { KeypadKey } from "@khanacademy/perseus-core";
4
4
  type Props = {
5
- extraKeys: ReadonlyArray<Key>;
5
+ extraKeys: ReadonlyArray<KeypadKey>;
6
6
  onClickKey: ClickKeyCallback;
7
7
  };
8
8
  export default function ExtrasPage(props: Props): React.JSX.Element;
@@ -1,10 +1,9 @@
1
1
  import * as React from "react";
2
- import type Key from "../../data/keys";
3
2
  import type { ClickKeyCallback } from "../../types";
4
3
  import type { CursorContext } from "../input/cursor-contexts";
5
- import type { AnalyticsEventHandlerFn } from "@khanacademy/perseus-core";
4
+ import type { AnalyticsEventHandlerFn, KeypadKey } from "@khanacademy/perseus-core";
6
5
  type Props = {
7
- extraKeys?: ReadonlyArray<Key>;
6
+ extraKeys?: ReadonlyArray<KeypadKey>;
8
7
  cursorContext?: (typeof CursorContext)[keyof typeof CursorContext];
9
8
  showDismiss?: boolean;
10
9
  expandedView?: boolean;
@@ -1,8 +1,7 @@
1
1
  import * as React from "react";
2
2
  import ReactDOM from "react-dom";
3
- import type Key from "../../data/keys";
4
- import type { Cursor, KeypadConfiguration, KeyHandler, KeypadAPI } from "../../types";
5
- import type { AnalyticsEventHandlerFn } from "@khanacademy/perseus-core";
3
+ import type { Cursor, KeyHandler, KeypadAPI } from "../../types";
4
+ import type { AnalyticsEventHandlerFn, KeypadConfiguration, KeypadKey } from "@khanacademy/perseus-core";
6
5
  import type { StyleType } from "@khanacademy/wonder-blocks-core";
7
6
  type Props = {
8
7
  onElementMounted?: (arg1: any) => void;
@@ -43,7 +42,7 @@ declare class MobileKeypadInternals extends React.Component<Props, State> implem
43
42
  setCursor: (cursor: Cursor) => void;
44
43
  setKeyHandler: (keyHandler: KeyHandler) => void;
45
44
  getDOMNode: () => ReturnType<typeof ReactDOM.findDOMNode>;
46
- _handleClickKey(key: Key): void;
45
+ _handleClickKey(key: KeypadKey): void;
47
46
  render(): React.ReactNode;
48
47
  }
49
48
  export default MobileKeypadInternals;
@@ -1,12 +1,4 @@
1
1
  /**
2
2
  * React PropTypes that may be shared between components.
3
3
  */
4
- import PropTypes from "prop-types";
5
- export declare const keypadElementPropType: PropTypes.Requireable<PropTypes.InferProps<{
6
- activate: PropTypes.Validator<(...args: any[]) => any>;
7
- dismiss: PropTypes.Validator<(...args: any[]) => any>;
8
- configure: PropTypes.Validator<(...args: any[]) => any>;
9
- setCursor: PropTypes.Validator<(...args: any[]) => any>;
10
- setKeyHandler: PropTypes.Validator<(...args: any[]) => any>;
11
- getDOMNode: PropTypes.Validator<(...args: any[]) => any>;
12
- }>>;
4
+ export declare const keypadElementPropType: any;
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * This file contains configuration settings for the buttons in the keypad.
3
3
  */
4
- import type Key from "./keys";
5
4
  import type { MathInputStrings } from "../strings";
6
5
  import type { KeyConfig } from "../types";
7
- declare const KeyConfigs: (strings: MathInputStrings) => { [key in Key]: KeyConfig; };
6
+ import type { KeypadKey } from "@khanacademy/perseus-core";
7
+ declare const KeyConfigs: (strings: MathInputStrings) => { [key in KeypadKey]: KeyConfig; };
8
8
  export default KeyConfigs;
@@ -1,3 +0,0 @@
1
- export declare const KeyArray: readonly ["PLUS", "MINUS", "NEGATIVE", "TIMES", "DIVIDE", "DECIMAL", "PERIOD", "PERCENT", "CDOT", "EQUAL", "NEQ", "GT", "LT", "GEQ", "LEQ", "FRAC_INCLUSIVE", "FRAC_EXCLUSIVE", "FRAC", "EXP", "EXP_2", "EXP_3", "SQRT", "CUBE_ROOT", "RADICAL", "LEFT_PAREN", "RIGHT_PAREN", "LN", "LOG", "LOG_N", "SIN", "COS", "TAN", "PI", "THETA", "UP", "RIGHT", "DOWN", "LEFT", "BACKSPACE", "DISMISS", "JUMP_OUT_PARENTHESES", "JUMP_OUT_EXPONENT", "JUMP_OUT_BASE", "JUMP_INTO_NUMERATOR", "JUMP_OUT_NUMERATOR", "JUMP_OUT_DENOMINATOR", "NUM_0", "NUM_1", "NUM_2", "NUM_3", "NUM_4", "NUM_5", "NUM_6", "NUM_7", "NUM_8", "NUM_9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
2
- type Key = (typeof KeyArray)[number];
3
- export default Key;
package/dist/enums.d.ts CHANGED
@@ -1,10 +1,6 @@
1
1
  /**
2
2
  * Constants that are shared between multiple files.
3
3
  */
4
- export declare enum KeypadType {
5
- FRACTION = "FRACTION",
6
- EXPRESSION = "EXPRESSION"
7
- }
8
4
  declare const KeyTypes: string[];
9
5
  export type KeyType = (typeof KeyTypes)[number];
10
6
  export {};
package/dist/es/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { addLibraryVersionToPerseusDebug, getDecimalSeparator } from '@khanacademy/perseus-core';
1
+ import _extends from '@babel/runtime/helpers/extends';
2
2
  import { KeypadContext } from '@khanacademy/keypad-context';
3
3
  import { color } from '@khanacademy/wonder-blocks-tokens';
4
4
  import { entries } from '@khanacademy/wonder-stuff-core';
@@ -8,27 +8,61 @@ import { useContext, useRef, useEffect, useState } from 'react';
8
8
  import ReactDOM from 'react-dom';
9
9
  import { SpeechRuleEngine } from '@khanacademy/mathjax-renderer';
10
10
  import MathQuill from 'mathquill';
11
+ import { getDecimalSeparator } from '@khanacademy/perseus-core';
12
+ import _objectWithoutPropertiesLoose from '@babel/runtime/helpers/objectWithoutPropertiesLoose';
11
13
  import { View as View$1 } from '@khanacademy/wonder-blocks-core';
12
14
  import Clickable from '@khanacademy/wonder-blocks-clickable';
13
15
  import { TransitionGroup } from 'react-transition-group';
14
16
  import { withActionScheduler } from '@khanacademy/wonder-blocks-timing';
15
17
  import PropTypes from 'prop-types';
16
18
 
19
+ /**
20
+ * Adds the given perseus library version information to the __perseus_debug__
21
+ * object and ensures that the object is attached to `globalThis` (`window` in
22
+ * browser environments).
23
+ *
24
+ * This allows each library to provide runtime version information to assist in
25
+ * debugging in production environments.
26
+ */
27
+ const addLibraryVersionToPerseusDebug = (libraryName, libraryVersion) => {
28
+ // If the library version is the default value, then we don't want to
29
+ // prefix it with a "v" to indicate that it is a version number.
30
+ let prefix = "v";
31
+ if (libraryVersion === "__lib_version__") {
32
+ prefix = "";
33
+ }
34
+ const formattedVersion = `${prefix}${libraryVersion}`;
35
+ if (typeof globalThis !== "undefined") {
36
+ var _globalThis$__perseus;
37
+ globalThis.__perseus_debug__ = (_globalThis$__perseus = globalThis.__perseus_debug__) != null ? _globalThis$__perseus : {};
38
+ const existingVersionEntry = globalThis.__perseus_debug__[libraryName];
39
+ if (existingVersionEntry) {
40
+ // If we already have an entry and it doesn't match the registered
41
+ // version, we morph the entry into an array and log a warning.
42
+ if (existingVersionEntry !== formattedVersion) {
43
+ // Existing entry might be an array already (oops, at least 2
44
+ // versions of the library already loaded!).
45
+ const allVersions = Array.isArray(existingVersionEntry) ? existingVersionEntry : [existingVersionEntry];
46
+ allVersions.push(formattedVersion);
47
+ globalThis.__perseus_debug__[libraryName] = allVersions;
48
+
49
+ // eslint-disable-next-line no-console
50
+ console.warn(`Multiple versions of ${libraryName} loaded on this page: ${allVersions.sort().join(", ")}`);
51
+ }
52
+ } else {
53
+ globalThis.__perseus_debug__[libraryName] = formattedVersion;
54
+ }
55
+ } else {
56
+ // eslint-disable-next-line no-console
57
+ console.warn(`globalThis not found found (${formattedVersion})`);
58
+ }
59
+ };
60
+
17
61
  // This file is processed by a Rollup plugin (replace) to inject the production
18
62
  const libName = "@khanacademy/math-input";
19
- const libVersion = "22.2.6";
63
+ const libVersion = "23.0.0";
20
64
  addLibraryVersionToPerseusDebug(libName, libVersion);
21
65
 
22
- function _extends() {
23
- return _extends = Object.assign ? Object.assign.bind() : function (n) {
24
- for (var e = 1; e < arguments.length; e++) {
25
- var t = arguments[e];
26
- for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
27
- }
28
- return n;
29
- }, _extends.apply(null, arguments);
30
- }
31
-
32
66
  class View extends React.Component {
33
67
  render() {
34
68
  const className = css(View.styles.initial, ...(Array.isArray(this.props.style) ? this.props.style : [this.props.style])) + (this.props.extraClassName ? ` ${this.props.extraClassName}` : "");
@@ -1191,7 +1225,6 @@ const getKeyTranslator = (locale, strings) => ({
1191
1225
  mathQuill.write("\\sqrt[3]{}");
1192
1226
  mathQuill.keystroke("Left"); // under the root
1193
1227
  },
1194
-
1195
1228
  FRAC_EXCLUSIVE: mathQuill => {
1196
1229
  const cursor = mathQuill.cursor();
1197
1230
  // If there's nothing to the left of the cursor, then we want to
@@ -1208,7 +1241,6 @@ const getKeyTranslator = (locale, strings) => ({
1208
1241
  mathQuill.keystroke("Left"); // out of parentheses
1209
1242
  mathQuill.keystroke("Left"); // into index
1210
1243
  },
1211
-
1212
1244
  // These need to be overwritten by the consumer
1213
1245
  // if they're going to be used
1214
1246
  DISMISS: () => {},
@@ -1578,6 +1610,9 @@ class MathInput extends React.Component {
1578
1610
  }
1579
1611
  });
1580
1612
  };
1613
+ // [Jan 2025] Third: While testing, I've discovered that we likely don't
1614
+ // need to be passing setKeypadActive here at all. Removing the parameter
1615
+ // still results in the same behavior.
1581
1616
  this.focus = setKeypadActive => {
1582
1617
  var _this$props$keypadEle, _this$props;
1583
1618
  // Pass this component's handleKey method to the keypad so it can call
@@ -2051,11 +2086,14 @@ class MathInput extends React.Component {
2051
2086
  // padding values for the vertical directions.
2052
2087
  const symbolaPaddingBottom = 3;
2053
2088
  const symbolaPaddingTop = 1;
2089
+ // We also add a little padding for the cursor to ensure there's no
2090
+ // overflow when the input is empty and set to right aligned.
2091
+ const cursorPadding = 2;
2054
2092
  const padding = {
2055
2093
  paddingTop: paddingInset - symbolaPaddingTop,
2056
- paddingRight: paddingInset,
2094
+ paddingRight: paddingInset + cursorPadding,
2057
2095
  paddingBottom: paddingInset - symbolaPaddingBottom,
2058
- paddingLeft: paddingInset
2096
+ paddingLeft: paddingInset + cursorPadding
2059
2097
  };
2060
2098
  return padding;
2061
2099
  };
@@ -2319,16 +2357,6 @@ const inlineStyles = {
2319
2357
  }
2320
2358
  };
2321
2359
 
2322
- function _objectWithoutPropertiesLoose(r, e) {
2323
- if (null == r) return {};
2324
- var t = {};
2325
- for (var n in r) if ({}.hasOwnProperty.call(r, n)) {
2326
- if (-1 !== e.indexOf(n)) continue;
2327
- t[n] = r[n];
2328
- }
2329
- return t;
2330
- }
2331
-
2332
2360
  const IconAsset = function IconAsset({
2333
2361
  tintColor,
2334
2362
  type
@@ -5704,27 +5732,5 @@ const keypadElementPropType = PropTypes.shape({
5704
5732
  getDOMNode: PropTypes.func.isRequired
5705
5733
  });
5706
5734
 
5707
- const KeyArray = ["PLUS", "MINUS", "NEGATIVE", "TIMES", "DIVIDE", "DECIMAL", "PERIOD", "PERCENT", "CDOT", "EQUAL", "NEQ", "GT", "LT", "GEQ", "LEQ",
5708
- // mobile native only
5709
- "FRAC_INCLUSIVE",
5710
- // mobile native only
5711
- "FRAC_EXCLUSIVE",
5712
- // mobile native only
5713
- "FRAC", "EXP", "EXP_2", "EXP_3", "SQRT", "CUBE_ROOT", "RADICAL", "LEFT_PAREN", "RIGHT_PAREN", "LN", "LOG", "LOG_N", "SIN", "COS",
5714
- // TODO(charlie): Add in additional Greek letters.,
5715
- "TAN", "PI", "THETA", "UP", "RIGHT", "DOWN", "LEFT", "BACKSPACE", "DISMISS", "JUMP_OUT_PARENTHESES", "JUMP_OUT_EXPONENT", "JUMP_OUT_BASE", "JUMP_INTO_NUMERATOR", "JUMP_OUT_NUMERATOR", "JUMP_OUT_DENOMINATOR",
5716
- // Multi-functional keys.
5717
- "NUM_0", "NUM_1", "NUM_2", "NUM_3", "NUM_4", "NUM_5", "NUM_6", "NUM_7", "NUM_8", "NUM_9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
5718
-
5719
- /**
5720
- * Constants that are shared between multiple files.
5721
- */
5722
-
5723
- let KeypadType = /*#__PURE__*/function (KeypadType) {
5724
- KeypadType["FRACTION"] = "FRACTION";
5725
- KeypadType["EXPRESSION"] = "EXPRESSION";
5726
- return KeypadType;
5727
- }({});
5728
-
5729
- export { CursorContext, Keypad as DesktopKeypad, KeyArray, KeyConfigs, MathInput as KeypadInput, KeypadType, MathInputI18nContext, MathInputI18nContextProvider, MobileKeypad, convertDotToTimesByLocale, createMathField, getCursorContext, getKeyTranslator, keypadElementPropType, libVersion, mathQuillInstance, useMathInputI18n };
5735
+ export { CursorContext, Keypad as DesktopKeypad, KeyConfigs, MathInput as KeypadInput, MathInputI18nContext, MathInputI18nContextProvider, MobileKeypad, convertDotToTimesByLocale, createMathField, getCursorContext, getKeyTranslator, keypadElementPropType, libVersion, mathQuillInstance, useMathInputI18n };
5730
5736
  //# sourceMappingURL=index.js.map