@teachinglab/omd 0.5.7 → 0.6.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/README.md +190 -77
- package/README.old.md +138 -0
- package/index.js +3 -0
- package/npm-docs/DOCUMENTATION_SUMMARY.md +220 -0
- package/npm-docs/README.md +251 -0
- package/npm-docs/api/api-reference.md +85 -0
- package/npm-docs/api/configuration-options.md +198 -0
- package/npm-docs/api/eventManager.md +83 -0
- package/npm-docs/api/expression-nodes.md +561 -0
- package/npm-docs/api/focusFrameManager.md +145 -0
- package/npm-docs/api/index.md +106 -0
- package/npm-docs/api/main.md +63 -0
- package/npm-docs/api/omdBinaryExpressionNode.md +86 -0
- package/npm-docs/api/omdCanvas.md +84 -0
- package/npm-docs/api/omdConfigManager.md +113 -0
- package/npm-docs/api/omdConstantNode.md +53 -0
- package/npm-docs/api/omdDisplay.md +87 -0
- package/npm-docs/api/omdEquationNode.md +174 -0
- package/npm-docs/api/omdEquationSequenceNode.md +259 -0
- package/npm-docs/api/omdEquationStack.md +193 -0
- package/npm-docs/api/omdFunctionNode.md +83 -0
- package/npm-docs/api/omdGroupNode.md +79 -0
- package/npm-docs/api/omdHelpers.md +88 -0
- package/npm-docs/api/omdLeafNode.md +86 -0
- package/npm-docs/api/omdNode.md +202 -0
- package/npm-docs/api/omdOperationDisplayNode.md +118 -0
- package/npm-docs/api/omdOperatorNode.md +92 -0
- package/npm-docs/api/omdParenthesisNode.md +134 -0
- package/npm-docs/api/omdPopup.md +192 -0
- package/npm-docs/api/omdPowerNode.md +132 -0
- package/npm-docs/api/omdRationalNode.md +145 -0
- package/npm-docs/api/omdSequenceNode.md +128 -0
- package/npm-docs/api/omdSimplification.md +79 -0
- package/npm-docs/api/omdSqrtNode.md +144 -0
- package/npm-docs/api/omdStepVisualizer.md +147 -0
- package/npm-docs/api/omdStepVisualizerHighlighting.md +66 -0
- package/npm-docs/api/omdStepVisualizerInteractiveSteps.md +109 -0
- package/npm-docs/api/omdStepVisualizerLayout.md +71 -0
- package/npm-docs/api/omdStepVisualizerNodeUtils.md +140 -0
- package/npm-docs/api/omdStepVisualizerTextBoxes.md +77 -0
- package/npm-docs/api/omdToolbar.md +131 -0
- package/npm-docs/api/omdTranscriptionService.md +96 -0
- package/npm-docs/api/omdTreeDiff.md +170 -0
- package/npm-docs/api/omdUnaryExpressionNode.md +137 -0
- package/npm-docs/api/omdUtilities.md +83 -0
- package/npm-docs/api/omdVariableNode.md +123 -0
- package/npm-docs/api/selectTool.md +74 -0
- package/npm-docs/api/simplificationEngine.md +98 -0
- package/npm-docs/api/simplificationRules.md +77 -0
- package/npm-docs/api/simplificationUtils.md +64 -0
- package/npm-docs/api/transcribe.md +43 -0
- package/npm-docs/guides/equations.md +854 -0
- package/npm-docs/guides/factory-functions.md +354 -0
- package/npm-docs/guides/getting-started.md +318 -0
- package/npm-docs/guides/quick-examples.md +525 -0
- package/npm-docs/guides/visualizations.md +682 -0
- package/npm-docs/json-schemas.md +826 -0
- package/omd/utils/omdTranscriptionService.js +1 -1
- package/package.json +4 -3
- package/src/index.js +2 -0
- package/src/omdFactory.js +150 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# omdEquationNode
|
|
2
|
+
|
|
3
|
+
Represents a mathematical equation, consisting of a left side, a right side, and an equals sign. This class provides extensive functionality for manipulating the equation, such as applying operations to both sides, swapping sides, simplification, and rendering to various visualization formats.
|
|
4
|
+
|
|
5
|
+
## Class Definition
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
export class omdEquationNode extends omdNode
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Constructor
|
|
12
|
+
|
|
13
|
+
### `new omdEquationNode(ast)`
|
|
14
|
+
|
|
15
|
+
Creates a new `omdEquationNode` instance.
|
|
16
|
+
|
|
17
|
+
- **`ast`** (`object`): The math.js Abstract Syntax Tree (AST) for the equation. It typically expects an `AssignmentNode` (with `object` for the left side and `value` for the right side) or an `OperatorNode` (with `op: '='` and two arguments).
|
|
18
|
+
|
|
19
|
+
## Static Methods
|
|
20
|
+
|
|
21
|
+
### `fromString(equationString)`
|
|
22
|
+
|
|
23
|
+
Creates an `omdEquationNode` instance from a string representation of an equation.
|
|
24
|
+
|
|
25
|
+
- **`equationString`** (`string`): The string to parse (e.g., `"2x + 4 = 10"`).
|
|
26
|
+
- **Returns**: `omdEquationNode` - A new instance of `omdEquationNode`.
|
|
27
|
+
- **Throws**: `Error` if the string is not a valid equation (must contain exactly one `'='` and both sides must be non-empty).
|
|
28
|
+
|
|
29
|
+
## Public Properties
|
|
30
|
+
|
|
31
|
+
- **`left`** ([`omdNode`](./omdNode.md)): The node representing the left side of the equation.
|
|
32
|
+
- **`right`** ([`omdNode`](./omdNode.md)): The node representing the right side of the equation.
|
|
33
|
+
- **`equalsSign`** ([`omdOperatorNode`](./omdOperatorNode.md)): The node for the equals sign.
|
|
34
|
+
|
|
35
|
+
## Public Methods
|
|
36
|
+
|
|
37
|
+
### `addToBothSides(value)`
|
|
38
|
+
|
|
39
|
+
Returns a new equation with a value added to both sides.
|
|
40
|
+
|
|
41
|
+
- **`value`** (`number` | `object`): The value (number or math.js AST) to add.
|
|
42
|
+
- **Returns**: `omdEquationNode` - A new equation node with the operation applied.
|
|
43
|
+
|
|
44
|
+
### `subtractFromBothSides(value)`
|
|
45
|
+
|
|
46
|
+
Returns a new equation with a value subtracted from both sides.
|
|
47
|
+
|
|
48
|
+
- **`value`** (`number` | `object`): The value (number or math.js AST) to subtract.
|
|
49
|
+
- **Returns**: `omdEquationNode` - A new equation node.
|
|
50
|
+
|
|
51
|
+
### `multiplyBothSides(value)`
|
|
52
|
+
|
|
53
|
+
Returns a new equation with both sides multiplied by a value.
|
|
54
|
+
|
|
55
|
+
- **`value`** (`number` | `object`): The value (number or math.js AST) to multiply by.
|
|
56
|
+
- **Returns**: `omdEquationNode` - A new equation node.
|
|
57
|
+
|
|
58
|
+
### `divideBothSides(value)`
|
|
59
|
+
|
|
60
|
+
Returns a new equation with both sides divided by a value.
|
|
61
|
+
|
|
62
|
+
- **`value`** (`number` | `object`): The value (number or math.js AST) to divide by.
|
|
63
|
+
- **Returns**: `omdEquationNode` - A new equation node.
|
|
64
|
+
|
|
65
|
+
### `applyFunction(functionName)`
|
|
66
|
+
|
|
67
|
+
Applies a function (e.g., `sqrt`, `log`) to both sides of the equation.
|
|
68
|
+
|
|
69
|
+
- **`functionName`** (`string`): The name of the function to apply.
|
|
70
|
+
- **Returns**: `omdEquationNode` - A new equation with the function applied to both sides.
|
|
71
|
+
|
|
72
|
+
### `applyOperation(value, operation, side = 'both')`
|
|
73
|
+
|
|
74
|
+
Applies an arithmetic operation to one or both sides of the equation.
|
|
75
|
+
|
|
76
|
+
- **`value`** (`number` | `omdNode`): The value to apply.
|
|
77
|
+
- **`operation`** (`string`): The operation to perform: `'add'`, `'subtract'`, `'multiply'`, or `'divide'`.
|
|
78
|
+
- **`side`** (`string`): The side to apply the operation to: `'left'`, `'right'`, or `'both'`. Defaults to `'both'`.
|
|
79
|
+
- **Returns**: `omdEquationNode` - A new equation node with the operation applied.
|
|
80
|
+
|
|
81
|
+
### `swapSides()`
|
|
82
|
+
|
|
83
|
+
Swaps the left and right sides of the equation.
|
|
84
|
+
|
|
85
|
+
- **Returns**: `omdEquationNode` - A new equation node with the sides swapped.
|
|
86
|
+
|
|
87
|
+
### `simplify()`
|
|
88
|
+
|
|
89
|
+
Simplifies both sides of the equation using the simplification engine.
|
|
90
|
+
|
|
91
|
+
- **Returns**: `object` - An object containing `{ success, newRoot, message }`.
|
|
92
|
+
|
|
93
|
+
### `clone()`
|
|
94
|
+
|
|
95
|
+
Creates a deep clone of the equation node, preserving its structure and provenance.
|
|
96
|
+
|
|
97
|
+
- **Returns**: `omdEquationNode` - A new, identical instance of the equation node.
|
|
98
|
+
|
|
99
|
+
### `toString()`
|
|
100
|
+
|
|
101
|
+
Converts the equation to its string representation.
|
|
102
|
+
|
|
103
|
+
- **Returns**: `string` - The equation as a string (e.g., `"x + 1 = 5"`).
|
|
104
|
+
|
|
105
|
+
### `evaluate(variables)`
|
|
106
|
+
|
|
107
|
+
Evaluates both sides of the equation with a given set of variables.
|
|
108
|
+
|
|
109
|
+
- **`variables`** (`object`): A map of variable names to their numeric values (e.g., `{ x: 2 }`).
|
|
110
|
+
- **Returns**: `object` - An object with the evaluated `left` and `right` side values.
|
|
111
|
+
|
|
112
|
+
### `setBackgroundStyle(style)`
|
|
113
|
+
|
|
114
|
+
Configures the background styling for the equation node. This can include `backgroundColor`, `cornerRadius`, and `pill` (for a pill-shaped background).
|
|
115
|
+
|
|
116
|
+
- **`style`** (`object`): An object containing style properties.
|
|
117
|
+
|
|
118
|
+
### `getEqualsAnchorX()`
|
|
119
|
+
|
|
120
|
+
Returns the horizontal X-coordinate of the center of the equals sign relative to this node's origin. Useful for aligning equations in a sequence.
|
|
121
|
+
|
|
122
|
+
- **Returns**: `number`.
|
|
123
|
+
|
|
124
|
+
### `getBackgroundPaddingX()`
|
|
125
|
+
|
|
126
|
+
Returns the horizontal padding applied by the background style.
|
|
127
|
+
|
|
128
|
+
- **Returns**: `number`.
|
|
129
|
+
|
|
130
|
+
### `getEffectiveBackgroundPaddingX()`
|
|
131
|
+
|
|
132
|
+
Returns the effective horizontal padding used in layout, accounting for factors like pill clamping.
|
|
133
|
+
|
|
134
|
+
- **Returns**: `number`.
|
|
135
|
+
|
|
136
|
+
### `renderTo(visualizationType, options)`
|
|
137
|
+
|
|
138
|
+
Renders the equation to different visualization formats (e.g., graph, table, balance hanger, tile equation).
|
|
139
|
+
|
|
140
|
+
- **`visualizationType`** (`string`): The type of visualization (`"graph"`, `"table"`, `"hanger"`, `"tileequation"`).
|
|
141
|
+
- **`options`** (`object`, optional): Configuration options specific to the visualization type.
|
|
142
|
+
- **Returns**: `object` - A JSON object conforming to the schemas defined in `src/json-schemas.md`.
|
|
143
|
+
|
|
144
|
+
### `getLeft()`
|
|
145
|
+
|
|
146
|
+
Returns the `omdNode` representing the left side of the equation.
|
|
147
|
+
|
|
148
|
+
- **Returns**: `omdNode`.
|
|
149
|
+
|
|
150
|
+
### `getRight()`
|
|
151
|
+
|
|
152
|
+
Returns the `omdNode` representing the right side of the equation.
|
|
153
|
+
|
|
154
|
+
- **Returns**: `omdNode`.
|
|
155
|
+
|
|
156
|
+
## Internal Methods
|
|
157
|
+
|
|
158
|
+
- **`_applyOperation(value, op, fn)`**: Internal helper for applying arithmetic operations to both sides.
|
|
159
|
+
- **`_needsParenthesesForOperation(node, operation)`**: Determines if a child node needs parentheses when an operation is applied, based on operator precedence.
|
|
160
|
+
- **`_createNodeFromValue(value)`**: Converts a number or math.js AST into an appropriate `omdNode`.
|
|
161
|
+
- **`_establishGranularProvenance(newNode, originalNode, operationValue, operation)`**: Manages provenance tracking for equation operations, linking new nodes to their origins.
|
|
162
|
+
- **`_isMultiplicationOperation(node)`**: Checks if a given node represents a multiplication operation.
|
|
163
|
+
- **`_copyProvenanceStructure(target, source)`**: Recursively copies provenance information from a source node to a target node.
|
|
164
|
+
- **`_createFunctionNode(functionName, argument)`**: Creates an `omdFunctionNode` wrapping a given argument.
|
|
165
|
+
- **`_createNewEquation(left, right)`**: Creates a new `omdEquationNode` from two `omdNode` instances.
|
|
166
|
+
- **`_getEffectivePadding(contentHeight)`**: Computes effective padding for background styling, considering pill radius clamping.
|
|
167
|
+
- **`_applyPillToDescendants()`**: Applies pill-shaped corner radius to all descendant nodes' backgrounds when the parent equation uses a pill style.
|
|
168
|
+
- **`_matchChildBackgrounds(color)`**: Hides child node backgrounds by setting their color to match the parent equation's background.
|
|
169
|
+
- **`_renderToGraph(options)`**: Generates JSON for a coordinate plane graph visualization.
|
|
170
|
+
- **`_renderToTable(options)`**: Generates JSON for a table visualization.
|
|
171
|
+
- **`_renderSingleSideTable(side, title, options)`**: Helper for generating tables for a single side of the equation.
|
|
172
|
+
- **`_renderToHanger()`**: Generates JSON for a balance hanger visualization.
|
|
173
|
+
- **`_normalizeExpressionString(expr)`**: Normalizes expression strings for evaluation/graphing (e.g., inserts `*` for implicit multiplication).
|
|
174
|
+
- **`_convertToHangerValues(node)`**: Converts an equation side into values suitable for a balance hanger visualization.
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
# omdEquationSequenceNode
|
|
2
|
+
|
|
3
|
+
Represents a sequence of equations or mathematical expressions, designed for step-by-step calculations and visual problem-solving. This node manages the vertical layout of multiple steps, ensuring elements like equals signs are vertically aligned for readability. It also provides robust provenance tracking, simplification capabilities, and filtering options for displaying different levels of detail.
|
|
4
|
+
|
|
5
|
+
## Class Definition
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
export class omdEquationSequenceNode extends omdNode
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Constructor
|
|
12
|
+
|
|
13
|
+
### `new omdEquationSequenceNode(steps)`
|
|
14
|
+
|
|
15
|
+
Creates a new `omdEquationSequenceNode` instance.
|
|
16
|
+
|
|
17
|
+
- **`steps`** (`Array<omdNode>`): An initial array of `omdNode` objects (typically `omdEquationNode` or `omdOperationDisplayNode`) representing the sequence of steps.
|
|
18
|
+
|
|
19
|
+
Upon construction, the sequence initializes its internal state, sets up layout helpers, and builds a comprehensive node map for provenance tracking across all steps.
|
|
20
|
+
|
|
21
|
+
## Static Properties
|
|
22
|
+
|
|
23
|
+
### `OPERATION_MAP`
|
|
24
|
+
|
|
25
|
+
A static map that links common operation names (e.g., `'add'`) to the corresponding method names on `omdEquationNode` (e.g., `'addToBothSides'`).
|
|
26
|
+
|
|
27
|
+
```javascript
|
|
28
|
+
{
|
|
29
|
+
'add': 'addToBothSides',
|
|
30
|
+
'subtract': 'subtractFromBothSides',
|
|
31
|
+
'multiply': 'multiplyBothSides',
|
|
32
|
+
'divide': 'divideBothSides',
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Static Methods
|
|
37
|
+
|
|
38
|
+
### `fromStringArray(stepStrings)`
|
|
39
|
+
|
|
40
|
+
Creates an `omdEquationSequenceNode` instance from an array of equation strings. Each string must contain an equals sign (`=`).
|
|
41
|
+
|
|
42
|
+
- **`stepStrings`** (`Array<string>`): An array of strings, each representing an equation step.
|
|
43
|
+
- **Returns**: `omdEquationSequenceNode` - A new instance.
|
|
44
|
+
- **Throws**: `Error` if any string is not a valid equation (e.g., missing `=` or invalid format).
|
|
45
|
+
|
|
46
|
+
### `fromSteps(stepsArray)`
|
|
47
|
+
|
|
48
|
+
Creates an `omdEquationSequenceNode` instance from an array of expression strings. This method is more flexible than `fromStringArray` as it can parse both full equations (containing `=`) and general mathematical expressions.
|
|
49
|
+
|
|
50
|
+
- **`stepsArray`** (`Array<string>`): An array of expression strings.
|
|
51
|
+
- **Returns**: `omdEquationSequenceNode` - A new sequence node.
|
|
52
|
+
|
|
53
|
+
## Public Properties
|
|
54
|
+
|
|
55
|
+
- **`steps`** (`Array<omdNode>`): An array of `omdNode` objects representing each step in the sequence.
|
|
56
|
+
- **`currentStepIndex`** (`number`): The 0-based index of the currently active step. Default: `0`.
|
|
57
|
+
- **`stepDescriptions`** (`Array<string>`): An array storing optional human-readable descriptions for each step.
|
|
58
|
+
- **`importanceLevels`** (`Array<number>`): An array storing the importance level (0, 1, or 2) for each step, used for filtering.
|
|
59
|
+
- **`currentFilterLevels`** (`Array<number>`): An array of numbers representing the currently active importance levels for step visibility. Default: `[0]`.
|
|
60
|
+
- **`defaultEquationBackground`** (`object` | `null`): An object defining a default background style to be applied to all `omdEquationNode` steps added to the sequence.
|
|
61
|
+
|
|
62
|
+
## Public Methods
|
|
63
|
+
|
|
64
|
+
### `getCurrentEquation()`
|
|
65
|
+
|
|
66
|
+
Retrieves the last `omdEquationNode` in the sequence, which typically represents the current working equation.
|
|
67
|
+
|
|
68
|
+
- **Returns**: `omdEquationNode` | `null` - The last equation, or `null` if no equations exist.
|
|
69
|
+
|
|
70
|
+
### `getCurrentStep()`
|
|
71
|
+
|
|
72
|
+
Retrieves the currently active step node based on `currentStepIndex` and visibility. It prioritizes visible equation nodes.
|
|
73
|
+
|
|
74
|
+
- **Returns**: `omdNode` | `null` - The current step node.
|
|
75
|
+
|
|
76
|
+
### `addStep(step, descriptionOrOptions, importance)`
|
|
77
|
+
|
|
78
|
+
Adds a new step to the sequence. This method supports multiple signatures:
|
|
79
|
+
|
|
80
|
+
- `addStep(omdNode, optionsObject)`
|
|
81
|
+
- `addStep(omdNode, descriptionString, importanceNumber)`
|
|
82
|
+
- `addStep(expressionString, optionsObject)`
|
|
83
|
+
- `addStep(expressionString, descriptionString, importanceNumber)`
|
|
84
|
+
|
|
85
|
+
- **`step`** (`omdNode` | `string`): The `omdNode` object or an expression string for the step.
|
|
86
|
+
- **`descriptionOrOptions`** (`object` | `string`, optional): An options object (e.g., `{ description: '...', stepMark: 0 }`) or a description string.
|
|
87
|
+
- **`importance`** (`number`, optional): The importance level (0, 1, or 2) if `descriptionOrOptions` is a string.
|
|
88
|
+
- **Returns**: `number` - The index of the added step.
|
|
89
|
+
|
|
90
|
+
### `setDefaultEquationBackground(style)`
|
|
91
|
+
|
|
92
|
+
Sets a default background style that will be applied to all new `omdEquationNode` steps added to the sequence. It also applies the style to existing `omdEquationNode` steps immediately.
|
|
93
|
+
|
|
94
|
+
- **`style`** (`object`): An object defining the background style (e.g., `{ backgroundColor: '#f0f0f0', cornerRadius: 8, pill: true }`).
|
|
95
|
+
|
|
96
|
+
### `rebuildNodeMap()`
|
|
97
|
+
|
|
98
|
+
Rebuilds the internal `nodeMap`, which is a comprehensive map of all `omdNode` instances within the entire sequence, including historical nodes referenced in provenance chains. This is crucial for accurate highlighting and provenance tracking.
|
|
99
|
+
|
|
100
|
+
### `recordSimplificationHistory(name, affectedNodes, message, metadata)`
|
|
101
|
+
|
|
102
|
+
Records a simplification step in the sequence's history.
|
|
103
|
+
|
|
104
|
+
- **`name`** (`string`): The name of the simplification rule applied.
|
|
105
|
+
- **`affectedNodes`** (`Array<string>`): An array of node IDs that were affected by the simplification.
|
|
106
|
+
- **`message`** (`string`): A human-readable description of the simplification.
|
|
107
|
+
- **`metadata`** (`object`, optional): Additional metadata about the simplification.
|
|
108
|
+
|
|
109
|
+
### `getSimplificationHistory()`
|
|
110
|
+
|
|
111
|
+
Retrieves the complete simplification history for this sequence.
|
|
112
|
+
|
|
113
|
+
- **Returns**: `Array<object>` - An array of simplification history entries.
|
|
114
|
+
|
|
115
|
+
### `clearSimplificationHistory()`
|
|
116
|
+
|
|
117
|
+
Clears all recorded simplification history entries.
|
|
118
|
+
|
|
119
|
+
### `setFontSize(fontSize)`
|
|
120
|
+
|
|
121
|
+
Sets the font size for the entire sequence and propagates this setting to all individual steps. This triggers a re-computation of dimensions and layout.
|
|
122
|
+
|
|
123
|
+
- **`fontSize`** (`number`): The new font size in pixels.
|
|
124
|
+
|
|
125
|
+
### `applyEquationOperation(value, operation)`
|
|
126
|
+
|
|
127
|
+
Applies a specified arithmetic operation to the current equation in the sequence and adds the result as a new step. This method also adds an `omdOperationDisplayNode` to visually represent the operation.
|
|
128
|
+
|
|
129
|
+
- **`value`** (`number` | `string`): The constant value or expression string to apply.
|
|
130
|
+
- **`operation`** (`string`): The operation name (`'add'`, `'subtract'`, `'multiply'`, `'divide'`).
|
|
131
|
+
- **Returns**: `omdEquationSequenceNode` - Returns this sequence instance for chaining.
|
|
132
|
+
|
|
133
|
+
### `applyEquationFunction(functionName)`
|
|
134
|
+
|
|
135
|
+
Applies a mathematical function (e.g., `'sqrt'`, `'log'`) to both sides of the current equation in the sequence and adds the result as a new step.
|
|
136
|
+
|
|
137
|
+
- **`functionName`** (`string`): The name of the function to apply.
|
|
138
|
+
- **Returns**: `omdEquationSequenceNode` - Returns this sequence instance for chaining.
|
|
139
|
+
|
|
140
|
+
### `simplify()`
|
|
141
|
+
|
|
142
|
+
Applies one round of simplification rules to the last step in the sequence. If simplifications are applied, a new step is added to the sequence.
|
|
143
|
+
|
|
144
|
+
- **Returns**: `object` - A result object containing `success` (boolean), `foldedCount` (number of operations applied), `isFinalSimplification` (boolean), and `message` (string).
|
|
145
|
+
|
|
146
|
+
### `simplifyAll(maxIterations)`
|
|
147
|
+
|
|
148
|
+
Repeatedly calls `simplify()` until no more simplifications can be applied or `maxIterations` is reached. This adds multiple simplification steps to the sequence.
|
|
149
|
+
|
|
150
|
+
- **`maxIterations`** (`number`, optional): Maximum number of iterations to prevent infinite loops. Default: `50`.
|
|
151
|
+
- **Returns**: `object` - A result object containing `success`, `totalSteps`, `iterations`, and `message`.
|
|
152
|
+
|
|
153
|
+
### `evaluate(variables)`
|
|
154
|
+
|
|
155
|
+
Evaluates the current step in the sequence with the given variables and logs the result to the console.
|
|
156
|
+
|
|
157
|
+
- **`variables`** (`object`): A map of variable names to their numeric values.
|
|
158
|
+
|
|
159
|
+
### `validateSequenceProvenance()`
|
|
160
|
+
|
|
161
|
+
Validates the integrity of provenance tracking across all steps in the sequence.
|
|
162
|
+
|
|
163
|
+
- **Returns**: `Array<object>` - An array of validation issues found.
|
|
164
|
+
|
|
165
|
+
### `select()` / `deselect()` / `highlight(color)` / `clearProvenanceHighlights()`
|
|
166
|
+
|
|
167
|
+
These methods override the default `omdNode` behavior. The `omdEquationSequenceNode` itself does not highlight or respond to selection events directly. Instead, these methods propagate the calls to their child steps, allowing individual steps to be highlighted or selected.
|
|
168
|
+
|
|
169
|
+
### `computeDimensions()`
|
|
170
|
+
|
|
171
|
+
Calculates the overall dimensions (width and height) of the entire sequence, determining the correct alignment point for all equals signs.
|
|
172
|
+
|
|
173
|
+
### `updateLayout()`
|
|
174
|
+
|
|
175
|
+
Positions each step vertically and aligns their equals signs to the calculated alignment point, ensuring a clean, readable layout.
|
|
176
|
+
|
|
177
|
+
### `toMathJSNode()`
|
|
178
|
+
|
|
179
|
+
Converts the sequence to a math.js-compatible AST node. Currently, this returns the AST of the last step in the sequence.
|
|
180
|
+
|
|
181
|
+
- **Returns**: `object` - A math.js AST node.
|
|
182
|
+
|
|
183
|
+
### `navigateToStep(index)`
|
|
184
|
+
|
|
185
|
+
Navigates to a specific step in the sequence by setting `currentStepIndex`.
|
|
186
|
+
|
|
187
|
+
- **`index`** (`number`): The 0-based index of the step to navigate to.
|
|
188
|
+
- **Returns**: `boolean` - `true` if navigation was successful.
|
|
189
|
+
|
|
190
|
+
### `nextStep()`
|
|
191
|
+
|
|
192
|
+
Navigates to the next step in the sequence.
|
|
193
|
+
|
|
194
|
+
- **Returns**: `boolean` - `true` if there was a next step.
|
|
195
|
+
|
|
196
|
+
### `previousStep()`
|
|
197
|
+
|
|
198
|
+
Navigates to the previous step in the sequence.
|
|
199
|
+
|
|
200
|
+
- **Returns**: `boolean` - `true` if there was a previous step.
|
|
201
|
+
|
|
202
|
+
### `getFilteredSteps(maxImportance)`
|
|
203
|
+
|
|
204
|
+
Retrieves steps filtered by their importance level.
|
|
205
|
+
|
|
206
|
+
- **`maxImportance`** (`number`): The maximum importance level to include (0, 1, or 2).
|
|
207
|
+
- **Returns**: `Array<object>` - An array of objects, each containing `step`, `description`, `importance`, and `index`.
|
|
208
|
+
|
|
209
|
+
### `renderCurrentStep()`
|
|
210
|
+
|
|
211
|
+
Renders only the current step of the sequence.
|
|
212
|
+
|
|
213
|
+
- **Returns**: `SVGElement` - The SVG representation of the current step.
|
|
214
|
+
|
|
215
|
+
### `toString()`
|
|
216
|
+
|
|
217
|
+
Converts the entire sequence to a multi-line string representation, including step descriptions.
|
|
218
|
+
|
|
219
|
+
- **Returns**: `string` - Multi-line string of all steps.
|
|
220
|
+
|
|
221
|
+
### `clear()`
|
|
222
|
+
|
|
223
|
+
Removes all steps from the sequence, clears all associated data (descriptions, importance levels, history), and resets the sequence to its initial state.
|
|
224
|
+
|
|
225
|
+
### `show()` / `hide()`
|
|
226
|
+
|
|
227
|
+
Overrides `omdNode`'s `show()` and `hide()` to also update the visibility of the layout manager.
|
|
228
|
+
|
|
229
|
+
### `updateStepsVisibility(visibilityPredicate)`
|
|
230
|
+
|
|
231
|
+
Updates the visibility of multiple steps at once based on a provided predicate function. Also applies font weights based on `stepMark`.
|
|
232
|
+
|
|
233
|
+
- **`visibilityPredicate`** (`Function`): A function that takes a step node and returns `true` if it should be visible, `false` otherwise.
|
|
234
|
+
|
|
235
|
+
## Internal Methods
|
|
236
|
+
|
|
237
|
+
- **`_initializeState()`**: Initializes internal state variables like `currentStepIndex`, `stepDescriptions`, etc.
|
|
238
|
+
- **`_initializeLayout()`**: Sets up the `jsvgLayoutGroup` for vertical arrangement.
|
|
239
|
+
- **`_initializeNodeMap()`**: Initializes the `nodeMap` and calls `rebuildNodeMap()`.
|
|
240
|
+
- **`_disableContainerInteractions()`**: Disables default mouse interactions on the sequence container.
|
|
241
|
+
- **`_markInitialSteps()`**: Marks the initial steps with a default `stepMark` of 0.
|
|
242
|
+
- **`_applyDefaultFilter()`**: Applies the default filter (showing only level 0 steps) upon initialization.
|
|
243
|
+
- **`_reapplyCurrentFilter()`**: Reapplies the currently set filter levels to ensure consistent visibility.
|
|
244
|
+
- **`_determineStepMark(step, options)`**: Determines the appropriate `stepMark` for a new step.
|
|
245
|
+
- **`_isFullySimplified(step)`**: Heuristic to check if an equation step appears fully simplified.
|
|
246
|
+
- **`_isSimpleExpression(node)`**: Heuristic to check if an expression is simple (constant, variable, or simple operations).
|
|
247
|
+
- **`preserveProvenanceHistory(newNodeMap)`**: Ensures historical nodes referenced in provenance chains are preserved in the `nodeMap`.
|
|
248
|
+
- **`_collectAllProvenanceIds(newNodeMap)`**: Collects all provenance IDs from nodes in the `newNodeMap`.
|
|
249
|
+
- **`_collectNodeProvenanceIds(node, referencedIds, processedNodes)`**: Recursively collects provenance IDs for a given node.
|
|
250
|
+
- **`_preserveReferencedNodes(referencedIds, newNodeMap)`**: Preserves nodes referenced by `referencedIds` in the `newNodeMap`.
|
|
251
|
+
- **`_preserveNodeAndContext(id, newNodeMap, processedIds)`**: Preserves a node and its parent/sibling context.
|
|
252
|
+
- **`_preserveParentContext(node, newNodeMap)`**: Preserves the parent context of a node.
|
|
253
|
+
- **`_preserveSiblingContext(node, newNodeMap)`**: Preserves the sibling context of a node.
|
|
254
|
+
- **`_handleSuccessfulSimplification(originalStep, simplificationResult)`**: Handles the result of a successful simplification, adding a new step and recording history.
|
|
255
|
+
- **`_getOperationDescription(operation, value, isUnsimplified)`**: Generates a human-readable description for an equation operation.
|
|
256
|
+
- **`_calculateAlignmentPoint(visibleSteps)`**: Calculates the optimal X-coordinate for aligning equals signs across all visible steps.
|
|
257
|
+
- **`_calculateTotalDimensions(visibleSteps)`**: Calculates the total width and height of the sequence.
|
|
258
|
+
- **`_computeStepXOffset(step)`**: Computes the horizontal offset needed to align a step within the sequence.
|
|
259
|
+
- **`_stringToNode(str)`**: Converts an expression string into an `omdNode` (either `omdEquationNode` or a general expression node).
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
# omdEquationStack
|
|
2
|
+
|
|
3
|
+
A renderable component that bundles a sequence of mathematical equations with optional UI controls (toolbar). It extends `jsvgGroup` and acts as a node that can be rendered by an `omdDisplay`.
|
|
4
|
+
|
|
5
|
+
## Class Definition
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
import { omdEquationStack } from './omd/core/omdEquationStack.js';
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Constructor
|
|
12
|
+
|
|
13
|
+
### `new omdEquationStack(steps, options)`
|
|
14
|
+
|
|
15
|
+
Creates a new `omdEquationStack` instance.
|
|
16
|
+
|
|
17
|
+
- **`steps`** (`Array<omdNode>`, optional): An initial array of equation steps. Default: empty array.
|
|
18
|
+
- **`options`** (`object`, optional): Configuration options:
|
|
19
|
+
- **`toolbar`** (`boolean` | `object`): If `true`, creates a toolbar for equation operations. Can also be an object for more granular control:
|
|
20
|
+
- `enabled` (`boolean`): Whether the toolbar is enabled. Default: `false`.
|
|
21
|
+
- `position` (`string`): Where the toolbar should be positioned (`'bottom'`, `'overlay-bottom'`). Default: `'bottom'`.
|
|
22
|
+
- `showUndoButton` (`boolean`): Whether to show an undo button. Default: `false`.
|
|
23
|
+
- `onUndo` (`function`): Custom undo handler. If not provided, it attempts to call `window.onOMDToolbarUndo`.
|
|
24
|
+
- `overlayPadding` (`number`): Padding from the bottom edge when `position` is `'overlay-bottom'`. Default: `34`.
|
|
25
|
+
- **`stepVisualizer`** (`boolean`): If `true`, the underlying sequence will be an `omdStepVisualizer` instead of a plain `omdEquationSequenceNode`. Default: `false`.
|
|
26
|
+
- **`styling`** (`object`): Styling options for the equation stack:
|
|
27
|
+
- `equationBackground` (`object`): Default background style for equation steps (e.g., `{ backgroundColor: '#f0f0f0', cornerRadius: 8, pill: true }`).
|
|
28
|
+
|
|
29
|
+
## Public Properties
|
|
30
|
+
|
|
31
|
+
- **`sequence`** (`omdEquationSequenceNode` | `omdStepVisualizer`): The underlying sequence component that manages the equation steps.
|
|
32
|
+
- **`toolbar`** (`omdToolbar` | `undefined`): The toolbar component instance, if enabled.
|
|
33
|
+
- **`layoutGroup`** (`jsvgLayoutGroup`): The internal layout container that arranges the sequence and toolbar vertically.
|
|
34
|
+
- **`options`** (`object`): The merged configuration options passed to the constructor.
|
|
35
|
+
- **`overlayPadding`** (`number`): The calculated padding for the toolbar when it's in an overlay position.
|
|
36
|
+
|
|
37
|
+
## Public Methods
|
|
38
|
+
|
|
39
|
+
### `updateLayout()`
|
|
40
|
+
|
|
41
|
+
Updates the layout and positioning of internal components. This method ensures the sequence and toolbar are correctly arranged and that the toolbar is horizontally centered if it's in-flow.
|
|
42
|
+
|
|
43
|
+
### `getSequence()`
|
|
44
|
+
|
|
45
|
+
Returns the underlying sequence instance (either `omdEquationSequenceNode` or `omdStepVisualizer`).
|
|
46
|
+
|
|
47
|
+
- **Returns**: `omdEquationSequenceNode` | `omdStepVisualizer`.
|
|
48
|
+
|
|
49
|
+
### `getToolbar()`
|
|
50
|
+
|
|
51
|
+
Returns the toolbar instance, if one was created during construction.
|
|
52
|
+
|
|
53
|
+
- **Returns**: `omdToolbar` | `undefined`.
|
|
54
|
+
|
|
55
|
+
### `getOverlayPadding()`
|
|
56
|
+
|
|
57
|
+
Returns the padding value used for positioning the toolbar when it's in an overlay mode.
|
|
58
|
+
|
|
59
|
+
- **Returns**: `number`.
|
|
60
|
+
|
|
61
|
+
### `getToolbarVisualHeight()`
|
|
62
|
+
|
|
63
|
+
Returns the unscaled visual height of the toolbar's background element, if the toolbar is present.
|
|
64
|
+
|
|
65
|
+
- **Returns**: `number`.
|
|
66
|
+
|
|
67
|
+
### `isToolbarOverlay()`
|
|
68
|
+
|
|
69
|
+
Checks if the toolbar is configured to be overlaid at the bottom of the container.
|
|
70
|
+
|
|
71
|
+
- **Returns**: `boolean`.
|
|
72
|
+
|
|
73
|
+
### `positionToolbarOverlay(containerWidth, containerHeight, padding)`
|
|
74
|
+
|
|
75
|
+
Positions the toolbar overlay at the bottom center of the container. This method is typically called by the `omdDisplay` to ensure the toolbar remains visible and correctly placed even when the main content scales.
|
|
76
|
+
|
|
77
|
+
- **`containerWidth`** (`number`): The width of the parent container.
|
|
78
|
+
- **`containerHeight`** (`number`): The height of the parent container.
|
|
79
|
+
- **`padding`** (`number`, optional): Padding from the bottom edge. Default: `16`.
|
|
80
|
+
|
|
81
|
+
### `undoLastOperation()`
|
|
82
|
+
|
|
83
|
+
Removes the last equation step and any preceding operation display node from the sequence. This method also triggers a re-layout and updates any associated step visualizer.
|
|
84
|
+
|
|
85
|
+
- **Returns**: `boolean` - `true` if an operation was successfully undone, `false` otherwise.
|
|
86
|
+
|
|
87
|
+
## Usage
|
|
88
|
+
|
|
89
|
+
The `omdEquationStack` component is a complete equation solving interface that combines an equation sequence with an optional toolbar. It automatically handles layout and positioning.
|
|
90
|
+
|
|
91
|
+
```javascript
|
|
92
|
+
import { omdDisplay } from 'omd';
|
|
93
|
+
import { omdEquationStack } from 'omd';
|
|
94
|
+
import { omdEquationNode } from 'omd';
|
|
95
|
+
|
|
96
|
+
// Create display
|
|
97
|
+
const container = document.getElementById('math-display');
|
|
98
|
+
const display = new omdDisplay(container);
|
|
99
|
+
|
|
100
|
+
// Create equation steps
|
|
101
|
+
const steps = [
|
|
102
|
+
omdEquationNode.fromString('2x + 3 = 11'),
|
|
103
|
+
omdEquationNode.fromString('2x = 8'),
|
|
104
|
+
omdEquationNode.fromString('x = 4')
|
|
105
|
+
];
|
|
106
|
+
|
|
107
|
+
// Basic equation stack with toolbar
|
|
108
|
+
const stack = new omdEquationStack(steps, {
|
|
109
|
+
toolbar: true
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// Render the stack
|
|
113
|
+
display.render(stack);
|
|
114
|
+
|
|
115
|
+
// Alternative: Create with step visualizer and custom toolbar options
|
|
116
|
+
const stackWithVisualizer = new omdEquationStack(steps, {
|
|
117
|
+
toolbar: {
|
|
118
|
+
enabled: true,
|
|
119
|
+
showUndoButton: true,
|
|
120
|
+
position: 'overlay-bottom'
|
|
121
|
+
},
|
|
122
|
+
stepVisualizer: true,
|
|
123
|
+
styling: {
|
|
124
|
+
equationBackground: {
|
|
125
|
+
backgroundColor: '#e0f7fa',
|
|
126
|
+
cornerRadius: 10,
|
|
127
|
+
pill: true
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
display.render(stackWithVisualizer);
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Integration
|
|
136
|
+
|
|
137
|
+
The equation stack integrates seamlessly with the step visualizer:
|
|
138
|
+
|
|
139
|
+
```javascript
|
|
140
|
+
import { omdEquationStack } from 'omd';
|
|
141
|
+
import { omdEquationNode } from 'omd';
|
|
142
|
+
import { omdDisplay } from 'omd';
|
|
143
|
+
|
|
144
|
+
// Create steps for a more complex equation
|
|
145
|
+
const steps = [
|
|
146
|
+
omdEquationNode.fromString('x/2 + 3 = 7'),
|
|
147
|
+
omdEquationNode.fromString('x/2 = 4'),
|
|
148
|
+
omdEquationNode.fromString('x = 8')
|
|
149
|
+
];
|
|
150
|
+
|
|
151
|
+
// Enable step visualizer with highlighting
|
|
152
|
+
const stack = new omdEquationStack(steps, {
|
|
153
|
+
toolbar: true,
|
|
154
|
+
stepVisualizer: true
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// Render in display
|
|
158
|
+
const container = document.getElementById('equation-display');
|
|
159
|
+
const display = new omdDisplay(container);
|
|
160
|
+
display.render(stack);
|
|
161
|
+
|
|
162
|
+
// Access the visualizer for programmatic control
|
|
163
|
+
const visualizer = stack.getSequence();
|
|
164
|
+
// visualizer.nextStep(); // Progress through solution steps if interactive
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Layout Behavior
|
|
168
|
+
|
|
169
|
+
The equation stack automatically manages its internal layout:
|
|
170
|
+
- The sequence is positioned at the top
|
|
171
|
+
- The toolbar (if present) is centered below the sequence
|
|
172
|
+
- Layout updates automatically when content changes
|
|
173
|
+
|
|
174
|
+
```javascript
|
|
175
|
+
// Create a stack and let it handle layout automatically
|
|
176
|
+
const stack = new omdEquationStack(steps, { toolbar: true });
|
|
177
|
+
|
|
178
|
+
// Manual layout update (rarely needed)
|
|
179
|
+
stack.updateLayout();
|
|
180
|
+
|
|
181
|
+
// Access components
|
|
182
|
+
const sequence = stack.getSequence();
|
|
183
|
+
const toolbar = stack.getToolbar();
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
---
|
|
187
|
+
|
|
188
|
+
### See Also
|
|
189
|
+
|
|
190
|
+
- [`omdEquationSequenceNode`](./omdEquationSequenceNode.md)
|
|
191
|
+
- [`omdStepVisualizer`](./omdStepVisualizer.md)
|
|
192
|
+
- [`omdToolbar`](./omdToolbar.md)
|
|
193
|
+
- [`jsvgGroup`](../../jsvg/jsvg.js)
|