@teachinglab/omd 0.3.0 → 0.3.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.
- package/docs/api/configuration-options.md +198 -198
- package/docs/api/eventManager.md +82 -82
- package/docs/api/focusFrameManager.md +144 -144
- package/docs/api/index.md +105 -105
- package/docs/api/main.md +62 -62
- package/docs/api/omdBinaryExpressionNode.md +86 -86
- package/docs/api/omdCanvas.md +83 -83
- package/docs/api/omdConfigManager.md +112 -112
- package/docs/api/omdConstantNode.md +52 -52
- package/docs/api/omdDisplay.md +87 -87
- package/docs/api/omdEquationNode.md +174 -174
- package/docs/api/omdEquationSequenceNode.md +258 -258
- package/docs/api/omdEquationStack.md +156 -156
- package/docs/api/omdFunctionNode.md +82 -82
- package/docs/api/omdGroupNode.md +78 -78
- package/docs/api/omdHelpers.md +87 -87
- package/docs/api/omdLeafNode.md +85 -85
- package/docs/api/omdNode.md +201 -201
- package/docs/api/omdOperationDisplayNode.md +117 -117
- package/docs/api/omdOperatorNode.md +91 -91
- package/docs/api/omdParenthesisNode.md +133 -133
- package/docs/api/omdPopup.md +191 -191
- package/docs/api/omdPowerNode.md +131 -131
- package/docs/api/omdRationalNode.md +144 -144
- package/docs/api/omdSimplification.md +78 -78
- package/docs/api/omdSqrtNode.md +144 -144
- package/docs/api/omdStepVisualizer.md +146 -146
- package/docs/api/omdStepVisualizerHighlighting.md +65 -65
- package/docs/api/omdStepVisualizerInteractiveSteps.md +108 -108
- package/docs/api/omdStepVisualizerLayout.md +70 -70
- package/docs/api/omdStepVisualizerTextBoxes.md +76 -76
- package/docs/api/omdTranscriptionService.md +95 -95
- package/docs/api/omdTreeDiff.md +169 -169
- package/docs/api/omdUnaryExpressionNode.md +137 -137
- package/docs/api/omdUtilities.md +82 -82
- package/docs/api/omdVariableNode.md +123 -123
- package/omd/nodes/omdConstantNode.js +141 -141
- package/omd/nodes/omdGroupNode.js +67 -67
- package/omd/nodes/omdLeafNode.js +76 -76
- package/omd/nodes/omdOperatorNode.js +108 -108
- package/omd/nodes/omdParenthesisNode.js +292 -292
- package/omd/nodes/omdPowerNode.js +235 -235
- package/omd/nodes/omdRationalNode.js +295 -295
- package/omd/nodes/omdVariableNode.js +122 -122
- package/omd/simplification/omdSimplification.js +140 -140
- package/omd/step-visualizer/omdStepVisualizer.js +947 -947
- package/omd/step-visualizer/omdStepVisualizerLayout.js +892 -892
- package/package.json +1 -1
- package/src/index.js +11 -0
- package/src/omdBalanceHanger.js +2 -1
- package/src/omdEquation.js +1 -1
- package/src/omdNumber.js +1 -1
- package/src/omdNumberLine.js +13 -7
- package/src/omdRatioChart.js +11 -0
- package/src/omdShapes.js +1 -1
- package/src/omdTapeDiagram.js +1 -1
- package/src/omdTerm.js +1 -1
|
@@ -1,123 +1,123 @@
|
|
|
1
|
-
# omdVariableNode
|
|
2
|
-
|
|
3
|
-
Represents a variable (like `x`, `y`, `a`, `b`) in mathematical expressions. This node handles the visual representation of variables, their evaluation, and conversion to math.js AST.
|
|
4
|
-
|
|
5
|
-
## Class Definition
|
|
6
|
-
|
|
7
|
-
```javascript
|
|
8
|
-
export class omdVariableNode extends omdLeafNode
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Constructor
|
|
12
|
-
|
|
13
|
-
### `new omdVariableNode(nodeData)`
|
|
14
|
-
|
|
15
|
-
Creates a new `omdVariableNode` instance.
|
|
16
|
-
|
|
17
|
-
- **`nodeData`** (`object` | `string`): The AST node data (from math.js, typically a `SymbolNode` with a `name` property) or the variable name as a string (e.g., `"x"`). The constructor extracts the variable `name` and creates the `textElement` for display.
|
|
18
|
-
|
|
19
|
-
## Static Methods
|
|
20
|
-
|
|
21
|
-
### `fromName(name)`
|
|
22
|
-
|
|
23
|
-
Creates a variable node directly from a given name string. This is a convenience method for creating simple variable nodes without needing to construct a full AST object.
|
|
24
|
-
|
|
25
|
-
- **`name`** (`string`): The variable name (e.g., `"x"`, `"theta"`).
|
|
26
|
-
- **Returns**: `omdVariableNode` - A new instance of `omdVariableNode`.
|
|
27
|
-
|
|
28
|
-
## Public Properties
|
|
29
|
-
|
|
30
|
-
- **`name`** (`string`): The name of the variable (e.g., `'x'`, `'y'`, `'theta'`).
|
|
31
|
-
- **`type`** (`string`): Always `"omdVariableNode"`.
|
|
32
|
-
- **`textElement`** (`jsvgTextLine`): The internal `jsvgTextLine` instance that displays the variable name.
|
|
33
|
-
|
|
34
|
-
## Public Methods
|
|
35
|
-
|
|
36
|
-
### `computeDimensions()`
|
|
37
|
-
|
|
38
|
-
Calculates the dimensions of the node based on its text content, adding a small amount of padding around the variable name to improve visual spacing.
|
|
39
|
-
|
|
40
|
-
- **Overrides**: `omdLeafNode.computeDimensions()`.
|
|
41
|
-
|
|
42
|
-
### `updateLayout()`
|
|
43
|
-
|
|
44
|
-
Updates the layout of the node. This method primarily calls the superclass's `updateLayout`.
|
|
45
|
-
|
|
46
|
-
- **Overrides**: `omdLeafNode.updateLayout()`.
|
|
47
|
-
|
|
48
|
-
### `toMathJSNode()`
|
|
49
|
-
|
|
50
|
-
Converts the `omdVariableNode` to a math.js-compatible AST format. It creates a `SymbolNode` with the variable's `name`, `id`, and `provenance`.
|
|
51
|
-
|
|
52
|
-
- **Returns**: `object` - A math.js-compatible AST node with `type: "SymbolNode"` and `name` set to the variable name. The returned object also includes `id`, `provenance`, and a `clone` method for compatibility.
|
|
53
|
-
|
|
54
|
-
### `highlight(color)`
|
|
55
|
-
|
|
56
|
-
Applies a highlight to the node's background and sets the variable's text color to white for better contrast. This method respects the `isExplainHighlighted` lock.
|
|
57
|
-
|
|
58
|
-
- **`color`** (`string`): The color of the highlight.
|
|
59
|
-
|
|
60
|
-
### `clearProvenanceHighlights()`
|
|
61
|
-
|
|
62
|
-
Clears any provenance-related highlights from the node and resets the variable's text color to its default (black).
|
|
63
|
-
|
|
64
|
-
### `toString()`
|
|
65
|
-
|
|
66
|
-
Converts the variable node to its string representation, which is simply its `name`.
|
|
67
|
-
|
|
68
|
-
- **Returns**: `string` - The variable name.
|
|
69
|
-
|
|
70
|
-
### `evaluate(variables)`
|
|
71
|
-
|
|
72
|
-
Evaluates the variable by looking up its value in the provided `variables` map. If the variable is not defined in the map, it throws an error.
|
|
73
|
-
|
|
74
|
-
- **`variables`** (`object`): A map of variable names to their numeric values.
|
|
75
|
-
- **Returns**: `number` - The value of the variable.
|
|
76
|
-
- **Throws**: `Error` if the variable is not defined in the map.
|
|
77
|
-
|
|
78
|
-
## Internal Methods
|
|
79
|
-
|
|
80
|
-
- **`parseName(nodeData)`**: Extracts the variable name from the constructor's `nodeData`. It handles both string input and AST objects with a `name` property.
|
|
81
|
-
- **`parseType()`**: Sets the node's type. Always returns `"variable"`.
|
|
82
|
-
|
|
83
|
-
## Examples
|
|
84
|
-
|
|
85
|
-
#### Creating Variables
|
|
86
|
-
|
|
87
|
-
```javascript
|
|
88
|
-
import { omdVariableNode } from '@teachinglab/omd';
|
|
89
|
-
import * as math from 'mathjs';
|
|
90
|
-
|
|
91
|
-
// From a name string
|
|
92
|
-
const nodeX = omdVariableNode.fromName('x');
|
|
93
|
-
const nodeTheta = omdVariableNode.fromName('θ');
|
|
94
|
-
|
|
95
|
-
// From AST data (e.g., from math.js parse result)
|
|
96
|
-
const astNode = math.parse('y');
|
|
97
|
-
const nodeY = new omdVariableNode(astNode);
|
|
98
|
-
|
|
99
|
-
// Direct string name (less common, but supported)
|
|
100
|
-
const nodeZ = new omdVariableNode('z');
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
#### Rendering Variables
|
|
104
|
-
|
|
105
|
-
```javascript
|
|
106
|
-
import { omdVariableNode } from '@teachinglab/omd';
|
|
107
|
-
// import { jsvgContainer } from '@teachinglab/jsvg'; // if rendering directly
|
|
108
|
-
|
|
109
|
-
const node = omdVariableNode.fromName('x');
|
|
110
|
-
node.setFontSize(24);
|
|
111
|
-
node.initialize(); // Computes dimensions and layout
|
|
112
|
-
|
|
113
|
-
// To render, typically add to a parent node or an omdDisplay
|
|
114
|
-
// const svgContainer = new jsvgContainer();
|
|
115
|
-
// svgContainer.addChild(node);
|
|
116
|
-
// document.body.appendChild(svgContainer.svgObject);
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
## See Also
|
|
120
|
-
|
|
121
|
-
- [`omdLeafNode`](./omdLeafNode.md) - Parent class.
|
|
122
|
-
- [`omdNode`](./omdNode.md) - Base class.
|
|
123
|
-
- [`omdConstantNode`](./omdConstantNode.md) - For numeric values.
|
|
1
|
+
# omdVariableNode
|
|
2
|
+
|
|
3
|
+
Represents a variable (like `x`, `y`, `a`, `b`) in mathematical expressions. This node handles the visual representation of variables, their evaluation, and conversion to math.js AST.
|
|
4
|
+
|
|
5
|
+
## Class Definition
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
export class omdVariableNode extends omdLeafNode
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Constructor
|
|
12
|
+
|
|
13
|
+
### `new omdVariableNode(nodeData)`
|
|
14
|
+
|
|
15
|
+
Creates a new `omdVariableNode` instance.
|
|
16
|
+
|
|
17
|
+
- **`nodeData`** (`object` | `string`): The AST node data (from math.js, typically a `SymbolNode` with a `name` property) or the variable name as a string (e.g., `"x"`). The constructor extracts the variable `name` and creates the `textElement` for display.
|
|
18
|
+
|
|
19
|
+
## Static Methods
|
|
20
|
+
|
|
21
|
+
### `fromName(name)`
|
|
22
|
+
|
|
23
|
+
Creates a variable node directly from a given name string. This is a convenience method for creating simple variable nodes without needing to construct a full AST object.
|
|
24
|
+
|
|
25
|
+
- **`name`** (`string`): The variable name (e.g., `"x"`, `"theta"`).
|
|
26
|
+
- **Returns**: `omdVariableNode` - A new instance of `omdVariableNode`.
|
|
27
|
+
|
|
28
|
+
## Public Properties
|
|
29
|
+
|
|
30
|
+
- **`name`** (`string`): The name of the variable (e.g., `'x'`, `'y'`, `'theta'`).
|
|
31
|
+
- **`type`** (`string`): Always `"omdVariableNode"`.
|
|
32
|
+
- **`textElement`** (`jsvgTextLine`): The internal `jsvgTextLine` instance that displays the variable name.
|
|
33
|
+
|
|
34
|
+
## Public Methods
|
|
35
|
+
|
|
36
|
+
### `computeDimensions()`
|
|
37
|
+
|
|
38
|
+
Calculates the dimensions of the node based on its text content, adding a small amount of padding around the variable name to improve visual spacing.
|
|
39
|
+
|
|
40
|
+
- **Overrides**: `omdLeafNode.computeDimensions()`.
|
|
41
|
+
|
|
42
|
+
### `updateLayout()`
|
|
43
|
+
|
|
44
|
+
Updates the layout of the node. This method primarily calls the superclass's `updateLayout`.
|
|
45
|
+
|
|
46
|
+
- **Overrides**: `omdLeafNode.updateLayout()`.
|
|
47
|
+
|
|
48
|
+
### `toMathJSNode()`
|
|
49
|
+
|
|
50
|
+
Converts the `omdVariableNode` to a math.js-compatible AST format. It creates a `SymbolNode` with the variable's `name`, `id`, and `provenance`.
|
|
51
|
+
|
|
52
|
+
- **Returns**: `object` - A math.js-compatible AST node with `type: "SymbolNode"` and `name` set to the variable name. The returned object also includes `id`, `provenance`, and a `clone` method for compatibility.
|
|
53
|
+
|
|
54
|
+
### `highlight(color)`
|
|
55
|
+
|
|
56
|
+
Applies a highlight to the node's background and sets the variable's text color to white for better contrast. This method respects the `isExplainHighlighted` lock.
|
|
57
|
+
|
|
58
|
+
- **`color`** (`string`): The color of the highlight.
|
|
59
|
+
|
|
60
|
+
### `clearProvenanceHighlights()`
|
|
61
|
+
|
|
62
|
+
Clears any provenance-related highlights from the node and resets the variable's text color to its default (black).
|
|
63
|
+
|
|
64
|
+
### `toString()`
|
|
65
|
+
|
|
66
|
+
Converts the variable node to its string representation, which is simply its `name`.
|
|
67
|
+
|
|
68
|
+
- **Returns**: `string` - The variable name.
|
|
69
|
+
|
|
70
|
+
### `evaluate(variables)`
|
|
71
|
+
|
|
72
|
+
Evaluates the variable by looking up its value in the provided `variables` map. If the variable is not defined in the map, it throws an error.
|
|
73
|
+
|
|
74
|
+
- **`variables`** (`object`): A map of variable names to their numeric values.
|
|
75
|
+
- **Returns**: `number` - The value of the variable.
|
|
76
|
+
- **Throws**: `Error` if the variable is not defined in the map.
|
|
77
|
+
|
|
78
|
+
## Internal Methods
|
|
79
|
+
|
|
80
|
+
- **`parseName(nodeData)`**: Extracts the variable name from the constructor's `nodeData`. It handles both string input and AST objects with a `name` property.
|
|
81
|
+
- **`parseType()`**: Sets the node's type. Always returns `"variable"`.
|
|
82
|
+
|
|
83
|
+
## Examples
|
|
84
|
+
|
|
85
|
+
#### Creating Variables
|
|
86
|
+
|
|
87
|
+
```javascript
|
|
88
|
+
import { omdVariableNode } from '@teachinglab/omd';
|
|
89
|
+
import * as math from 'mathjs';
|
|
90
|
+
|
|
91
|
+
// From a name string
|
|
92
|
+
const nodeX = omdVariableNode.fromName('x');
|
|
93
|
+
const nodeTheta = omdVariableNode.fromName('θ');
|
|
94
|
+
|
|
95
|
+
// From AST data (e.g., from math.js parse result)
|
|
96
|
+
const astNode = math.parse('y');
|
|
97
|
+
const nodeY = new omdVariableNode(astNode);
|
|
98
|
+
|
|
99
|
+
// Direct string name (less common, but supported)
|
|
100
|
+
const nodeZ = new omdVariableNode('z');
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
#### Rendering Variables
|
|
104
|
+
|
|
105
|
+
```javascript
|
|
106
|
+
import { omdVariableNode } from '@teachinglab/omd';
|
|
107
|
+
// import { jsvgContainer } from '@teachinglab/jsvg'; // if rendering directly
|
|
108
|
+
|
|
109
|
+
const node = omdVariableNode.fromName('x');
|
|
110
|
+
node.setFontSize(24);
|
|
111
|
+
node.initialize(); // Computes dimensions and layout
|
|
112
|
+
|
|
113
|
+
// To render, typically add to a parent node or an omdDisplay
|
|
114
|
+
// const svgContainer = new jsvgContainer();
|
|
115
|
+
// svgContainer.addChild(node);
|
|
116
|
+
// document.body.appendChild(svgContainer.svgObject);
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## See Also
|
|
120
|
+
|
|
121
|
+
- [`omdLeafNode`](./omdLeafNode.md) - Parent class.
|
|
122
|
+
- [`omdNode`](./omdNode.md) - Base class.
|
|
123
|
+
- [`omdConstantNode`](./omdConstantNode.md) - For numeric values.
|
|
@@ -1,142 +1,142 @@
|
|
|
1
|
-
import { omdLeafNode } from "./omdLeafNode.js";
|
|
2
|
-
import { omdColor } from "../../src/omdColor.js";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Leaf node that represents a numerical constant.
|
|
6
|
-
* @extends omdLeafNode
|
|
7
|
-
*/
|
|
8
|
-
export class omdConstantNode extends omdLeafNode {
|
|
9
|
-
/**
|
|
10
|
-
* Creates object from AST data.
|
|
11
|
-
* @param {Object} astNodeData - The AST node containing leaf information.
|
|
12
|
-
*/
|
|
13
|
-
constructor(nodeData) {
|
|
14
|
-
super(nodeData);
|
|
15
|
-
this.type = "omdConstantNode";
|
|
16
|
-
|
|
17
|
-
this.number = this.parseNumber(nodeData);
|
|
18
|
-
|
|
19
|
-
this.textElement = super.createTextElement(this.number);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
parseNumber(nodeData) {
|
|
23
|
-
if (typeof nodeData === "number")
|
|
24
|
-
return nodeData;
|
|
25
|
-
|
|
26
|
-
return nodeData.value;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
parseType() {
|
|
30
|
-
return "constant";
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Calculates the dimensions of the node.
|
|
35
|
-
* Adds padding around the node.
|
|
36
|
-
* @override
|
|
37
|
-
*/
|
|
38
|
-
computeDimensions() {
|
|
39
|
-
super.computeDimensions();
|
|
40
|
-
|
|
41
|
-
const ratio = this.getFontSize() / this.getRootFontSize();
|
|
42
|
-
const padding = 4 * ratio;
|
|
43
|
-
let paddedWidth = this.width + padding;
|
|
44
|
-
let paddedHeight = this.height + padding;
|
|
45
|
-
this.setWidthAndHeight(paddedWidth, paddedHeight);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Updates the layout of the node.
|
|
50
|
-
* @override
|
|
51
|
-
*/
|
|
52
|
-
updateLayout() {
|
|
53
|
-
super.updateLayout();
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* A constant node is constant if its value is a number.
|
|
58
|
-
* @returns {boolean}
|
|
59
|
-
*/
|
|
60
|
-
isConstant() {
|
|
61
|
-
return typeof this.number === 'number';
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* The value of a constant node is its own value.
|
|
66
|
-
* @returns {number}
|
|
67
|
-
*/
|
|
68
|
-
getValue() {
|
|
69
|
-
return this.number;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Converts the constant node to a string.
|
|
74
|
-
* @returns {string} The string value of the constant.
|
|
75
|
-
*/
|
|
76
|
-
toString() {
|
|
77
|
-
return this.number.toString();
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Evaluates the constant node.
|
|
82
|
-
* @returns {number} The numeric value of the constant.
|
|
83
|
-
*/
|
|
84
|
-
evaluate() {
|
|
85
|
-
return this.number;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Retrieves the rational value of the node as a numerator/denominator pair.
|
|
90
|
-
* @returns {{num: number, den: number}}
|
|
91
|
-
*/
|
|
92
|
-
getRationalValue() {
|
|
93
|
-
return { num: this.getValue(), den: 1 };
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Converts the omdConstantNode to a math.js AST node.
|
|
98
|
-
* @returns {Object} A math.js-compatible AST node.
|
|
99
|
-
*/
|
|
100
|
-
toMathJSNode() {
|
|
101
|
-
const astNode = {
|
|
102
|
-
type: 'ConstantNode',
|
|
103
|
-
value: this.number,
|
|
104
|
-
id: this.id,
|
|
105
|
-
provenance: this.provenance
|
|
106
|
-
};
|
|
107
|
-
// Add a clone method to maintain compatibility with math.js's expectations.
|
|
108
|
-
astNode.clone = function() {
|
|
109
|
-
return { ...this };
|
|
110
|
-
};
|
|
111
|
-
return astNode;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
highlight(color) {
|
|
115
|
-
super.highlight(color);
|
|
116
|
-
if (this.textElement) {
|
|
117
|
-
this.textElement.setFillColor(omdColor.white);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
clearProvenanceHighlights() {
|
|
122
|
-
super.clearProvenanceHighlights();
|
|
123
|
-
if (this.textElement) {
|
|
124
|
-
this.textElement.setFillColor(omdColor.text);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Create a constant node from a numeric value.
|
|
130
|
-
* @param {number} value - The numeric value
|
|
131
|
-
* @returns {omdConstantNode}
|
|
132
|
-
* @static
|
|
133
|
-
*/
|
|
134
|
-
static fromValue(value) {
|
|
135
|
-
// Create a minimal AST-like object for the constructor
|
|
136
|
-
const astNodeData = {
|
|
137
|
-
type: 'ConstantNode',
|
|
138
|
-
value: value
|
|
139
|
-
};
|
|
140
|
-
return new omdConstantNode(astNodeData);
|
|
141
|
-
}
|
|
1
|
+
import { omdLeafNode } from "./omdLeafNode.js";
|
|
2
|
+
import { omdColor } from "../../src/omdColor.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Leaf node that represents a numerical constant.
|
|
6
|
+
* @extends omdLeafNode
|
|
7
|
+
*/
|
|
8
|
+
export class omdConstantNode extends omdLeafNode {
|
|
9
|
+
/**
|
|
10
|
+
* Creates object from AST data.
|
|
11
|
+
* @param {Object} astNodeData - The AST node containing leaf information.
|
|
12
|
+
*/
|
|
13
|
+
constructor(nodeData) {
|
|
14
|
+
super(nodeData);
|
|
15
|
+
this.type = "omdConstantNode";
|
|
16
|
+
|
|
17
|
+
this.number = this.parseNumber(nodeData);
|
|
18
|
+
|
|
19
|
+
this.textElement = super.createTextElement(this.number);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
parseNumber(nodeData) {
|
|
23
|
+
if (typeof nodeData === "number")
|
|
24
|
+
return nodeData;
|
|
25
|
+
|
|
26
|
+
return nodeData.value;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
parseType() {
|
|
30
|
+
return "constant";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Calculates the dimensions of the node.
|
|
35
|
+
* Adds padding around the node.
|
|
36
|
+
* @override
|
|
37
|
+
*/
|
|
38
|
+
computeDimensions() {
|
|
39
|
+
super.computeDimensions();
|
|
40
|
+
|
|
41
|
+
const ratio = this.getFontSize() / this.getRootFontSize();
|
|
42
|
+
const padding = 4 * ratio;
|
|
43
|
+
let paddedWidth = this.width + padding;
|
|
44
|
+
let paddedHeight = this.height + padding;
|
|
45
|
+
this.setWidthAndHeight(paddedWidth, paddedHeight);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Updates the layout of the node.
|
|
50
|
+
* @override
|
|
51
|
+
*/
|
|
52
|
+
updateLayout() {
|
|
53
|
+
super.updateLayout();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* A constant node is constant if its value is a number.
|
|
58
|
+
* @returns {boolean}
|
|
59
|
+
*/
|
|
60
|
+
isConstant() {
|
|
61
|
+
return typeof this.number === 'number';
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The value of a constant node is its own value.
|
|
66
|
+
* @returns {number}
|
|
67
|
+
*/
|
|
68
|
+
getValue() {
|
|
69
|
+
return this.number;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Converts the constant node to a string.
|
|
74
|
+
* @returns {string} The string value of the constant.
|
|
75
|
+
*/
|
|
76
|
+
toString() {
|
|
77
|
+
return this.number.toString();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Evaluates the constant node.
|
|
82
|
+
* @returns {number} The numeric value of the constant.
|
|
83
|
+
*/
|
|
84
|
+
evaluate() {
|
|
85
|
+
return this.number;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Retrieves the rational value of the node as a numerator/denominator pair.
|
|
90
|
+
* @returns {{num: number, den: number}}
|
|
91
|
+
*/
|
|
92
|
+
getRationalValue() {
|
|
93
|
+
return { num: this.getValue(), den: 1 };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Converts the omdConstantNode to a math.js AST node.
|
|
98
|
+
* @returns {Object} A math.js-compatible AST node.
|
|
99
|
+
*/
|
|
100
|
+
toMathJSNode() {
|
|
101
|
+
const astNode = {
|
|
102
|
+
type: 'ConstantNode',
|
|
103
|
+
value: this.number,
|
|
104
|
+
id: this.id,
|
|
105
|
+
provenance: this.provenance
|
|
106
|
+
};
|
|
107
|
+
// Add a clone method to maintain compatibility with math.js's expectations.
|
|
108
|
+
astNode.clone = function() {
|
|
109
|
+
return { ...this };
|
|
110
|
+
};
|
|
111
|
+
return astNode;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
highlight(color) {
|
|
115
|
+
super.highlight(color);
|
|
116
|
+
if (this.textElement) {
|
|
117
|
+
this.textElement.setFillColor(omdColor.white);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
clearProvenanceHighlights() {
|
|
122
|
+
super.clearProvenanceHighlights();
|
|
123
|
+
if (this.textElement) {
|
|
124
|
+
this.textElement.setFillColor(omdColor.text);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Create a constant node from a numeric value.
|
|
130
|
+
* @param {number} value - The numeric value
|
|
131
|
+
* @returns {omdConstantNode}
|
|
132
|
+
* @static
|
|
133
|
+
*/
|
|
134
|
+
static fromValue(value) {
|
|
135
|
+
// Create a minimal AST-like object for the constructor
|
|
136
|
+
const astNodeData = {
|
|
137
|
+
type: 'ConstantNode',
|
|
138
|
+
value: value
|
|
139
|
+
};
|
|
140
|
+
return new omdConstantNode(astNodeData);
|
|
141
|
+
}
|
|
142
142
|
}
|
|
@@ -1,68 +1,68 @@
|
|
|
1
|
-
import { omdLeafNode } from "./omdLeafNode.js";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Leaf node that represents a variable.
|
|
5
|
-
* @extends omdLeafNode
|
|
6
|
-
*/
|
|
7
|
-
export class omdGroupNode extends omdLeafNode {
|
|
8
|
-
/**
|
|
9
|
-
* Creates a leaf node from the AST data.
|
|
10
|
-
* @param {Object} astNodeData - The AST node containing leaf information.
|
|
11
|
-
* @param {String} groupingSymbol - A character representing the grouping symbol, e.g. ( or )
|
|
12
|
-
*/
|
|
13
|
-
constructor(nodeData) {
|
|
14
|
-
super(nodeData);
|
|
15
|
-
this.type = "omdGroupNode";
|
|
16
|
-
|
|
17
|
-
this.symbol = this.parseSymbol(nodeData);
|
|
18
|
-
|
|
19
|
-
this.textElement = super.createTextElement(this.symbol);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
parseSymbol(nodeData) {
|
|
23
|
-
return nodeData;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
parseType() {
|
|
27
|
-
return "parenthesis";
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
clone() {
|
|
31
|
-
const clone = new omdGroupNode(this.text);
|
|
32
|
-
|
|
33
|
-
// The crucial step: link the clone to its origin
|
|
34
|
-
clone.provenance.push(this.id);
|
|
35
|
-
|
|
36
|
-
return clone;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Calculates the dimensions of the node.
|
|
41
|
-
* Does NOT add padding, unlike other leaf nodes.
|
|
42
|
-
* @override
|
|
43
|
-
*/
|
|
44
|
-
computeDimensions() {
|
|
45
|
-
super.computeDimensions();
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Updates the layout of the node.
|
|
50
|
-
* @override
|
|
51
|
-
*/
|
|
52
|
-
updateLayout() {
|
|
53
|
-
super.updateLayout();
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Converts the omdGroupNode to a math.js AST node.
|
|
58
|
-
* @returns {Object} A math.js-compatible AST node.
|
|
59
|
-
*/
|
|
60
|
-
toMathJSNode() {
|
|
61
|
-
// This node is purely visual (like parentheses); it's represented by its symbol.
|
|
62
|
-
const astNode = { type: 'SymbolNode', name: this.symbol };
|
|
63
|
-
astNode.clone = function() {
|
|
64
|
-
return { ...this };
|
|
65
|
-
};
|
|
66
|
-
return astNode;
|
|
67
|
-
}
|
|
1
|
+
import { omdLeafNode } from "./omdLeafNode.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Leaf node that represents a variable.
|
|
5
|
+
* @extends omdLeafNode
|
|
6
|
+
*/
|
|
7
|
+
export class omdGroupNode extends omdLeafNode {
|
|
8
|
+
/**
|
|
9
|
+
* Creates a leaf node from the AST data.
|
|
10
|
+
* @param {Object} astNodeData - The AST node containing leaf information.
|
|
11
|
+
* @param {String} groupingSymbol - A character representing the grouping symbol, e.g. ( or )
|
|
12
|
+
*/
|
|
13
|
+
constructor(nodeData) {
|
|
14
|
+
super(nodeData);
|
|
15
|
+
this.type = "omdGroupNode";
|
|
16
|
+
|
|
17
|
+
this.symbol = this.parseSymbol(nodeData);
|
|
18
|
+
|
|
19
|
+
this.textElement = super.createTextElement(this.symbol);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
parseSymbol(nodeData) {
|
|
23
|
+
return nodeData;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
parseType() {
|
|
27
|
+
return "parenthesis";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
clone() {
|
|
31
|
+
const clone = new omdGroupNode(this.text);
|
|
32
|
+
|
|
33
|
+
// The crucial step: link the clone to its origin
|
|
34
|
+
clone.provenance.push(this.id);
|
|
35
|
+
|
|
36
|
+
return clone;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Calculates the dimensions of the node.
|
|
41
|
+
* Does NOT add padding, unlike other leaf nodes.
|
|
42
|
+
* @override
|
|
43
|
+
*/
|
|
44
|
+
computeDimensions() {
|
|
45
|
+
super.computeDimensions();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Updates the layout of the node.
|
|
50
|
+
* @override
|
|
51
|
+
*/
|
|
52
|
+
updateLayout() {
|
|
53
|
+
super.updateLayout();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Converts the omdGroupNode to a math.js AST node.
|
|
58
|
+
* @returns {Object} A math.js-compatible AST node.
|
|
59
|
+
*/
|
|
60
|
+
toMathJSNode() {
|
|
61
|
+
// This node is purely visual (like parentheses); it's represented by its symbol.
|
|
62
|
+
const astNode = { type: 'SymbolNode', name: this.symbol };
|
|
63
|
+
astNode.clone = function() {
|
|
64
|
+
return { ...this };
|
|
65
|
+
};
|
|
66
|
+
return astNode;
|
|
67
|
+
}
|
|
68
68
|
}
|