@khanacademy/perseus 17.8.0 → 18.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.
Files changed (56) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/dist/es/index.css +0 -4
  3. package/dist/es/index.js +1422 -1021
  4. package/dist/es/index.js.map +1 -1
  5. package/dist/index.css +0 -4
  6. package/dist/index.d.ts +4 -2
  7. package/dist/index.js +1321 -954
  8. package/dist/index.js.map +1 -1
  9. package/dist/perseus-types.d.ts +4 -3
  10. package/dist/renderer.d.ts +6 -6
  11. package/dist/server-item-renderer.d.ts +2 -4
  12. package/dist/strings.js +1 -1
  13. package/dist/types.d.ts +19 -2
  14. package/dist/util/drawing-transform.d.ts +21 -0
  15. package/dist/util/extract-perseus-data.d.ts +26 -0
  16. package/dist/util/graph-bounds.d.ts +9 -0
  17. package/dist/util/graphie.d.ts +2 -0
  18. package/dist/util/interval.d.ts +1 -0
  19. package/dist/util/parse-perseus-json.d.ts +10 -0
  20. package/dist/util/tex.d.ts +1 -3
  21. package/dist/widgets/deprecated-standin/index.d.ts +4 -4
  22. package/dist/widgets/passage.d.ts +3 -3
  23. package/dist/widgets.d.ts +16 -6
  24. package/package.json +9 -9
  25. package/src/__tests__/extract-perseus-data.test.ts +1027 -0
  26. package/src/index.ts +6 -1
  27. package/src/init.ts +1 -1
  28. package/src/perseus-types.ts +4 -3
  29. package/src/renderer.tsx +12 -9
  30. package/src/server-item-renderer.tsx +2 -4
  31. package/src/styles/widgets/image.less +0 -3
  32. package/src/types.ts +19 -16
  33. package/src/util/drawing-transform.test.ts +67 -0
  34. package/src/util/drawing-transform.ts +102 -0
  35. package/src/util/extract-perseus-data.ts +539 -0
  36. package/src/util/graph-bounds.ts +23 -0
  37. package/src/util/graphie.ts +1167 -1258
  38. package/src/util/interactive.ts +2 -2
  39. package/src/util/interval.ts +4 -0
  40. package/src/util/parse-perseus-json.test.ts +10 -0
  41. package/src/util/parse-perseus-json.ts +13 -0
  42. package/src/util/tex.ts +11 -84
  43. package/src/widgets/__stories__/image.stories.tsx +12 -26
  44. package/src/widgets/__tests__/__snapshots__/grapher.test.ts.snap +72 -288
  45. package/src/widgets/__tests__/__snapshots__/interaction.test.ts.snap +5 -20
  46. package/src/widgets/__tests__/__snapshots__/interactive-graph.test.ts.snap +32 -128
  47. package/src/widgets/__tests__/__snapshots__/number-line.test.ts.snap +22 -88
  48. package/src/widgets/__tests__/__snapshots__/plotter.test.tsx.snap +12 -48
  49. package/src/widgets/deprecated-standin/__stories__/deprecated-standin.stories.tsx +8 -12
  50. package/src/widgets/deprecated-standin/__tests__/__snapshots__/deprecated-standin.test.ts.snap +6 -7
  51. package/src/widgets/deprecated-standin/index.tsx +8 -7
  52. package/src/widgets/interactive-graph.tsx +3 -2
  53. package/src/widgets/label-image/answer-pill.tsx +6 -1
  54. package/src/widgets/passage.tsx +5 -7
  55. package/src/widgets.ts +34 -10
  56. package/tsconfig-build.tsbuildinfo +1 -1
package/src/widgets.ts CHANGED
@@ -3,12 +3,12 @@ import _ from "underscore";
3
3
  import {Errors, Log} from "./logging/log";
4
4
  import {PerseusError} from "./perseus-error";
5
5
 
6
+ import type {PerseusWidget} from "./perseus-types";
6
7
  import type {
7
8
  Alignment,
8
9
  Tracking,
9
10
  Version,
10
11
  WidgetExports,
11
- WidgetInfo,
12
12
  WidgetTransform,
13
13
  } from "./types";
14
14
  import type * as React from "react";
@@ -79,6 +79,30 @@ export const registerEditors = (editorsToRegister: ReadonlyArray<Editor>) => {
79
79
  });
80
80
  };
81
81
 
82
+ /**
83
+ *
84
+ * @param name - the widget that you are trying to replace
85
+ * @param replacementName - the name of the widget that takes its place
86
+ *
87
+ * e.g. replaceEditor("transformer", "always-correct") will make it so the
88
+ * transformer widget is replaced by the always correct widget
89
+ */
90
+ export const replaceEditor = (name: string, replacementName: string) => {
91
+ const substituteEditor = editors[replacementName];
92
+
93
+ if (!substituteEditor && Log) {
94
+ const errorMsg = `Failed to replace editor ${name} with ${replacementName}`;
95
+ Log.error(errorMsg, Errors.Internal);
96
+ return;
97
+ }
98
+
99
+ editors[name] = substituteEditor;
100
+
101
+ if (Log) {
102
+ Log.log(`INFO: Replacing editor ${name} with ${replacementName}`);
103
+ }
104
+ };
105
+
82
106
  export const getWidget = (
83
107
  name: string,
84
108
  ): React.ComponentType<any> | null | undefined => {
@@ -106,12 +130,12 @@ export const getTransform = (
106
130
  return _.has(widgets, name) ? widgets[name].transform || _.identity : null;
107
131
  };
108
132
 
109
- export const getVersion = (name: string): Version | null | undefined => {
133
+ export const getVersion = (name: string): Version | undefined => {
110
134
  const widgetInfo = widgets[name];
111
135
  if (widgetInfo) {
112
136
  return widgets[name].version || {major: 0, minor: 0};
113
137
  }
114
- return null;
138
+ return;
115
139
  };
116
140
 
117
141
  export const getVersionVector = (): {
@@ -136,7 +160,7 @@ export const getPublicWidgets = (): ReadonlyArray<WidgetExports> => {
136
160
  );
137
161
  };
138
162
 
139
- export const isAccessible = (widgetInfo: WidgetInfo): boolean => {
163
+ export const isAccessible = (widgetInfo: PerseusWidget): boolean => {
140
164
  const accessible = widgets[widgetInfo.type].accessible;
141
165
  if (typeof accessible === "function") {
142
166
  return accessible(widgetInfo.options);
@@ -149,8 +173,8 @@ export const getAllWidgetTypes = (): ReadonlyArray<string> => {
149
173
  };
150
174
 
151
175
  export const upgradeWidgetInfoToLatestVersion = (
152
- oldWidgetInfo: WidgetInfo,
153
- ): WidgetInfo => {
176
+ oldWidgetInfo: PerseusWidget,
177
+ ): PerseusWidget => {
154
178
  const type = oldWidgetInfo.type;
155
179
  // TODO(LP-10707): Remove unnecessary type checking (`type` is a string)
156
180
  if (!_.isString(type)) {
@@ -271,9 +295,9 @@ export const upgradeWidgetInfoToLatestVersion = (
271
295
  };
272
296
 
273
297
  export const getRendererPropsForWidgetInfo = (
274
- widgetInfo: WidgetInfo,
298
+ widgetInfo: PerseusWidget,
275
299
  problemNum?: number,
276
- ): any => {
300
+ ): PerseusWidget => {
277
301
  const type = widgetInfo.type;
278
302
  const widgetExports = widgets[type];
279
303
  if (widgetExports == null) {
@@ -298,9 +322,9 @@ export const getRendererPropsForWidgetInfo = (
298
322
  };
299
323
 
300
324
  export const traverseChildWidgets = (
301
- widgetInfo: WidgetInfo,
325
+ widgetInfo: PerseusWidget,
302
326
  traverseRenderer: any,
303
- ): WidgetInfo => {
327
+ ): PerseusWidget => {
304
328
  if (!traverseRenderer) {
305
329
  throw new PerseusError(
306
330
  "traverseRenderer must be provided, but was not",