Skip to content

Commit

Permalink
customize text state variable of electronConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
dqnykamp committed Dec 6, 2024
1 parent 4ffcd17 commit ff2100b
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 117 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { superSubscriptsToUnicode } from "../../utils/math";
import MathComponent from "../Math";

export default class ElectronConfiguration extends MathComponent {
Expand All @@ -10,15 +11,15 @@ export default class ElectronConfiguration extends MathComponent {
stateVariableDefinitions.latex = {
public: true,
shadowingInstructions: {
createComponentOfType: "text",
createComponentOfType: "latex",
},
returnDependencies: () => ({
valueForDisplay: {
dependencyType: "stateVariable",
variableName: "valueForDisplay",
},
}),
definition: function ({ dependencyValues, usedDefault }) {
definition: function ({ dependencyValues }) {
let latex;
try {
latex = dependencyValues.valueForDisplay.toLatex();
Expand All @@ -31,6 +32,32 @@ export default class ElectronConfiguration extends MathComponent {
},
};

stateVariableDefinitions.text = {
public: true,
shadowingInstructions: {
createComponentOfType: "text",
},
returnDependencies: () => ({
valueForDisplay: {
dependencyType: "stateVariable",
variableName: "valueForDisplay",
},
}),
definition: function ({ dependencyValues }) {
let text;
try {
text = superSubscriptsToUnicode(
dependencyValues.valueForDisplay.toString(),
);
} catch (e) {
text = "\uff3f";
}
text = text.replaceAll(" ", "");
text = text.replaceAll("*", " ");
return { setValue: { text } };
},
};

return stateVariableDefinitions;
}
}
12 changes: 11 additions & 1 deletion packages/doenetml-worker/src/test/chemistry/atom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { updateMathInputValue } from "../utils/actions";
import { atomDatabase } from "@doenet/static-assets";
//@ts-ignore
import me from "math-expressions";
import { superSubscriptsToUnicode } from "../../utils/math";

const Mock = vi.fn();
vi.stubGlobal("postMessage", Mock);
Expand Down Expand Up @@ -102,9 +103,18 @@ describe("Atom tests", async () => {
expect(stateVariables["/electronegativity"].stateValues.value).eqls(
to_number(data["Electronegativity"]),
);
let eConfig = data["Electron Configuration"];
expect(
stateVariables["/electronConfiguration"].stateValues.value.tree,
).eqls(me.fromText(data["Electron Configuration"]).tree);
).eqls(me.fromText(eConfig).tree);
expect(
stateVariables["/electronConfiguration"].stateValues.latex
.replaceAll(" ", "")
.replaceAll("~", " "),
).eq(eConfig.replaceAll(/\^(\d+)/g, "^{$1}"));
expect(
stateVariables["/electronConfiguration"].stateValues.text,
).eq(superSubscriptsToUnicode(eConfig));
}

let aNum = 1;
Expand Down
Loading

0 comments on commit ff2100b

Please sign in to comment.