Skip to content

Commit

Permalink
Take 2: Grapher default widget options (#2157)
Browse files Browse the repository at this point in the history
## Summary:
This is an alternative for: #2146

In #2146 I tried to a direct move of logic from `perseus` to `perseus-core` to get the default widget options for Grapher. That required `defaultPlotProps` which required `maybePointsFromNormalized` which eventually required `kmath`. It was gross for a number of reasons:

1. I had to move a lot of utilities
2. I had to give `perseus-core` a dependency on `kmath` (and `perseus-core` probably shouldn't have dependencies)
3. There was a lot of misdirection and logic all to make one static object

Whatever, I was okay hacking my way through that. _However_ we ended up with a circular dependency: `kmath` depends on `perseus-core` which was now depending on `kmath`.

At this point I reflected on my life choices and thought "why are we using all of these utilities just to compute an unchanging object? Why am I moving all this logic just to generate 2 properties?

So I just logged the default object and copied it to `perseus-core`.

Issue: LEMS-2737

## Test plan:
Nothing should change, just copy/pasta-ing code.

Author: handeyeco

Reviewers: jeremywiebe

Required Reviewers:

Approved By: jeremywiebe

Checks: ✅ Publish npm snapshot (ubuntu-latest, 20.x), ✅ Cypress (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x), ✅ Check builds for changes in size (ubuntu-latest, 20.x), ✅ Publish Storybook to Chromatic (ubuntu-latest, 20.x)

Pull Request URL: #2157
  • Loading branch information
handeyeco authored Jan 30, 2025
1 parent 3c4c6bc commit af8f5d3
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 13 deletions.
6 changes: 6 additions & 0 deletions .changeset/dry-fans-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@khanacademy/perseus-core": minor
"@khanacademy/perseus-editor": patch
---

Move default widget options for Grapher to Perseus Core
2 changes: 2 additions & 0 deletions packages/perseus-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export {default as gradedGroupLogic} from "./widgets/graded-group";
export type {GradedGroupDefaultWidgetOptions} from "./widgets/graded-group";
export {default as gradedGroupSetLogic} from "./widgets/graded-group-set";
export type {GradedGroupSetDefaultWidgetOptions} from "./widgets/graded-group-set";
export {default as grapherLogic} from "./widgets/grapher";
export type {GrapherDefaultWidgetOptions} from "./widgets/grapher";
export {default as groupLogic} from "./widgets/group";
export type {GroupDefaultWidgetOptions} from "./widgets/group";
export {default as iframeLogic} from "./widgets/iframe";
Expand Down
38 changes: 38 additions & 0 deletions packages/perseus-core/src/widgets/grapher/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type {PerseusGrapherWidgetOptions} from "../../data-schema";
import type {WidgetLogic} from "../logic-export.types";

export type GrapherDefaultWidgetOptions = Pick<
PerseusGrapherWidgetOptions,
"graph" | "correct" | "availableTypes"
>;

const defaultWidgetOptions: GrapherDefaultWidgetOptions = {
graph: {
labels: ["x", "y"],
range: [
[-10, 10],
[-10, 10],
],
step: [1, 1],
backgroundImage: {
url: null,
},
markings: "graph",
rulerLabel: "",
rulerTicks: 10,
valid: true,
showTooltips: false,
},
correct: {
type: "linear",
coords: null,
},
availableTypes: ["linear"],
};

const grapherWidgetLogic: WidgetLogic = {
name: "grapher",
defaultWidgetOptions,
};

export default grapherWidgetLogic;
23 changes: 10 additions & 13 deletions packages/perseus-editor/src/widgets/grapher-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ import {
containerSizeClass,
getInteractiveBoxFromSizeClass,
} from "@khanacademy/perseus";
import {GrapherUtil as CoreGrapherUtil} from "@khanacademy/perseus-core";
import {
GrapherUtil as CoreGrapherUtil,
grapherLogic,
} from "@khanacademy/perseus-core";
import * as React from "react";
import _ from "underscore";

import GraphSettings from "../components/graph-settings";

import type {GrapherDefaultWidgetOptions} from "@khanacademy/perseus-core";

const {InfoTip, MultiButtonGroup} = components;
const Grapher = GrapherWidget.widget;
const {
DEFAULT_GRAPHER_PROPS,
chooseType,
defaultPlotProps,
getEquationString,
typeToButton,
} = GrapherUtil;
const {chooseType, defaultPlotProps, getEquationString, typeToButton} =
GrapherUtil;

type Props = any;

Expand All @@ -32,11 +32,8 @@ class GrapherEditor extends React.Component<Props> {

static widgetName = "grapher" as const;

static defaultProps: Props = {
correct: DEFAULT_GRAPHER_PROPS.plot,
graph: DEFAULT_GRAPHER_PROPS.graph,
availableTypes: DEFAULT_GRAPHER_PROPS.availableTypes,
};
static defaultProps: GrapherDefaultWidgetOptions =
grapherLogic.defaultWidgetOptions;

change: (arg1: any, arg2: any, arg3: any) => any = (...args) => {
return Changeable.change.apply(this, args);
Expand Down

0 comments on commit af8f5d3

Please sign in to comment.