Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make graph specific properties persist and display on preview graph #1354

Merged
merged 6 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/witty-moons-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/perseus-editor": minor
---

Fix interactive graph editor in storybook to display and persist options
Original file line number Diff line number Diff line change
Expand Up @@ -598,4 +598,21 @@ describe("InteractiveGraphEditor", () => {
// Assert
expect(ref.current?.getSaveWarnings()).toEqual([]);
});

test("buildGraphKey returns the correct key", async () => {
// Arrange
const graph: PerseusGraphType = {
type: "polygon",
numSides: 4,
snapTo: "grid",
showAngles: true,
showSides: true,
};

// Act
const key = InteractiveGraphEditor.buildGraphKey(graph);

// Assert
expect(key).toEqual("polygon:4:grid:true:true");
});
});
19 changes: 17 additions & 2 deletions packages/perseus-editor/src/widgets/interactive-graph-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import type {
PerseusInteractiveGraphWidgetOptions,
APIOptionsWithDefaults,
LockedFigure,
PerseusGraphType,
} from "@khanacademy/perseus";
import type {PropsFor} from "@khanacademy/wonder-blocks-core";

Expand Down Expand Up @@ -198,6 +199,20 @@ class InteractiveGraphEditor extends React.Component<Props> {
DeprecationMixin.UNSAFE_componentWillMount.call(this);
}

static buildGraphKey(correct: PerseusGraphType) {
const testGraphKey: any[] = [];
for (const key in correct) {
if (correct[key]) {
typeof correct[key] === "number" ||
typeof correct[key] === "string" ||
typeof correct[key] === "boolean"
? testGraphKey.push(correct[key])
: testGraphKey.push(0);
}
}
return testGraphKey.join(":");
}

render() {
let graph;
let equationString;
Expand Down Expand Up @@ -247,10 +262,10 @@ class InteractiveGraphEditor extends React.Component<Props> {
this.props.onChange({correct: correct});
},
} as const;

// This is used to force a remount of the graph component
// when there's a significant change
const graphKey = `${correct.type}:${correct.numSegments || 0}`;
const graphKey = InteractiveGraphEditor.buildGraphKey(correct);

graph = (
// There are a bunch of props that renderer.jsx passes to widgets via
// getWidgetProps() and widget-container.jsx that the editors don't
Expand Down