@teachinglab/omd 0.1.4 → 0.1.5

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.
Files changed (42) hide show
  1. package/canvas/tools/EraserTool.js +1 -1
  2. package/canvas/tools/PencilTool.js +1 -1
  3. package/canvas/tools/SelectTool.js +1 -1
  4. package/docs/api/configuration-options.md +198 -104
  5. package/docs/api/eventManager.md +83 -68
  6. package/docs/api/focusFrameManager.md +145 -150
  7. package/docs/api/index.md +106 -91
  8. package/docs/api/main.md +63 -58
  9. package/docs/api/omdBinaryExpressionNode.md +86 -227
  10. package/docs/api/omdCanvas.md +84 -142
  11. package/docs/api/omdConfigManager.md +113 -192
  12. package/docs/api/omdConstantNode.md +53 -117
  13. package/docs/api/omdDisplay.md +87 -121
  14. package/docs/api/omdEquationNode.md +174 -161
  15. package/docs/api/omdEquationSequenceNode.md +259 -301
  16. package/docs/api/omdEquationStack.md +157 -103
  17. package/docs/api/omdFunctionNode.md +83 -141
  18. package/docs/api/omdGroupNode.md +79 -182
  19. package/docs/api/omdHelpers.md +88 -96
  20. package/docs/api/omdLeafNode.md +86 -163
  21. package/docs/api/omdNode.md +202 -101
  22. package/docs/api/omdOperationDisplayNode.md +118 -139
  23. package/docs/api/omdOperatorNode.md +92 -127
  24. package/docs/api/omdParenthesisNode.md +134 -122
  25. package/docs/api/omdPopup.md +192 -117
  26. package/docs/api/omdPowerNode.md +132 -127
  27. package/docs/api/omdRationalNode.md +145 -128
  28. package/docs/api/omdSimplification.md +79 -110
  29. package/docs/api/omdSqrtNode.md +144 -79
  30. package/docs/api/omdStepVisualizer.md +147 -115
  31. package/docs/api/omdStepVisualizerHighlighting.md +66 -61
  32. package/docs/api/omdStepVisualizerInteractiveSteps.md +109 -129
  33. package/docs/api/omdStepVisualizerLayout.md +71 -60
  34. package/docs/api/omdStepVisualizerTextBoxes.md +77 -68
  35. package/docs/api/omdToolbar.md +131 -102
  36. package/docs/api/omdTranscriptionService.md +96 -76
  37. package/docs/api/omdTreeDiff.md +170 -134
  38. package/docs/api/omdUnaryExpressionNode.md +137 -174
  39. package/docs/api/omdUtilities.md +83 -70
  40. package/docs/api/omdVariableNode.md +123 -148
  41. package/index.js +2 -2
  42. package/package.json +1 -1
@@ -1,70 +1,83 @@
1
- # omdUtilities
2
-
3
- This module provides a collection of utility functions primarily used for mapping math.js AST nodes to OMD node classes, determining rendering behavior, and calculating text dimensions.
4
-
5
- ## Functions
6
-
7
- #### `astToOmdType(type, ast)`
8
-
9
- Maps a math.js AST node type to its corresponding OMD node class. This function is crucial for dynamically creating the correct `omdNode` subclass based on the parsed expression.
10
-
11
- - **Parameters:**
12
- - `type` {string} - The `type` property of the AST node (e.g., `"OperatorNode"`, `"ParenthesisNode"`).
13
- - `ast` {Object} - The full AST node object, which may contain additional context (e.g., `op` for `OperatorNode`).
14
- - **Returns:** {class} The appropriate `omdNode` class constructor.
15
-
16
- ---
17
-
18
- #### `getNodeForAST(ast)`
19
-
20
- A wrapper function that takes a complete math.js AST node and returns the corresponding OMD node class. It uses `astToOmdType` internally.
21
-
22
- - **Parameters:**
23
- - `ast` {Object} - The math.js AST node.
24
- - **Returns:** {class} The appropriate `omdNode` class constructor.
25
-
26
- ---
27
-
28
- #### `getTextBounds(text, fontSize)`
29
-
30
- Calculates the rendered width and height of a given text string at a specific font size. This is used for precise layout calculations.
31
-
32
- - **Parameters:**
33
- - `text` {string} - The text content to measure.
34
- - `fontSize` {number} - The font size in pixels.
35
- - **Returns:** {Object} An object with `width` and `height` properties.
36
-
37
- ---
38
-
39
- #### `shouldUseFractionNotation(ast)`
40
-
41
- Determines whether a division operation (represented by an AST node) should be rendered as a fraction (stacked numerator over denominator) or as a linear division (e.g., `a / b`). This is based on the complexity of the numerator and denominator.
42
-
43
- - **Parameters:**
44
- - `ast` {Object} - The AST node representing a division operation.
45
- - **Returns:** {boolean} `true` if it should be a fraction, `false` otherwise.
46
-
47
- ---
48
-
49
- #### `isComplexExpression(ast)`
50
-
51
- Checks if an AST node represents a "complex" expression, typically one that contains multiple operations or nested structures. This is used by `shouldUseFractionNotation` to decide on rendering style.
52
-
53
- - **Parameters:**
54
- - `ast` {Object} - The AST node to check.
55
- - **Returns:** {boolean} `true` if the expression is considered complex, `false` otherwise.
56
-
57
- ### Example
58
-
59
- ```javascript
60
- import { getNodeForAST, getTextBounds } from './omd/omdUtilities.js';
61
-
62
- // Example of getting a node class
63
- const ast = math.parse('x + 2');
64
- const NodeClass = getNodeForAST(ast);
65
- const node = new NodeClass(ast);
66
-
67
- // Example of getting text bounds
68
- const bounds = getTextBounds('Hello World', 24);
69
- console.log(`Text width: ${bounds.width}, height: ${bounds.height}`);
70
- ```
1
+ # omdUtilities
2
+
3
+ This module provides a collection of utility functions primarily used for mapping math.js AST nodes to OMD node classes, determining rendering behavior, and calculating text dimensions. These functions are internal helpers that support the core OMD node system.
4
+
5
+ ## Functions
6
+
7
+ ### `astToOmdType(type, ast)`
8
+
9
+ Maps a math.js AST node type to its corresponding OMD node class. This function is crucial for dynamically creating the correct `omdNode` subclass based on the parsed expression.
10
+
11
+ - **`type`** (`string`): The `type` property of the AST node (e.g., `"OperatorNode"`, `"ParenthesisNode"`).
12
+ - **`ast`** (`object`): The full AST node object, which may contain additional context (e.g., `op` for `OperatorNode`, `fn` for `FunctionNode`).
13
+ - **Returns**: `class` - The appropriate `omdNode` class constructor.
14
+
15
+ **Detailed Logic:**
16
+
17
+ - **`AssignmentNode`**: Returns `omdEquationNode`.
18
+ - **`OperatorNode`**:
19
+ - If `op` is `'-'` and `args.length` is `1` (unary minus), returns `omdUnaryExpressionNode`.
20
+ - If `op` is `'='`, returns `omdEquationNode`.
21
+ - If `op` is `'^'`, returns `omdPowerNode`.
22
+ - If `op` is `'/'`, returns `omdRationalNode`.
23
+ - Otherwise (binary operator), returns `omdBinaryExpressionNode`.
24
+ - **`ParenthesisNode`**: Returns `omdParenthesisNode`.
25
+ - **`ConstantNode`**: Returns `omdConstantNode`.
26
+ - **`SymbolNode`**: Returns `omdVariableNode`.
27
+ - **`FunctionNode`**:
28
+ - If `fn.name` or `name` is `'multiply'` and `ast.implicit` is `true` (implicit multiplication), returns `omdBinaryExpressionNode`.
29
+ - If `fn.name` or `name` is `'sqrt'`, returns `omdSqrtNode`.
30
+ - Otherwise, returns `omdFunctionNode`.
31
+ - **Default**: Returns `omdNode`.
32
+
33
+ ### `getNodeForAST(ast)`
34
+
35
+ A wrapper function that takes a complete math.js AST node and returns the corresponding OMD node class. It uses `astToOmdType` internally, handling cases where the AST might have a `mathjs` property indicating its type.
36
+
37
+ - **`ast`** (`object`): The math.js AST node.
38
+ - **Returns**: `class` - The appropriate `omdNode` class constructor.
39
+
40
+ ### `getTextBounds(text, fontSize)`
41
+
42
+ Calculates the rendered width and height of a given text string at a specific font size. This is achieved by creating a temporary, hidden `<span>` element in the DOM, applying the text and styling, measuring its dimensions, and then removing it.
43
+
44
+ - **`text`** (`string`): The text content to measure.
45
+ - **`fontSize`** (`number`): The font size in pixels.
46
+ - **Returns**: `object` - An object with `width` and `height` properties.
47
+
48
+ ### `shouldUseFractionNotation(ast)`
49
+
50
+ Determines whether a division operation (represented by an AST node) should be rendered as a fraction (stacked numerator over denominator) or as a linear division (e.g., `a / b`). This decision is based on the complexity of the numerator and denominator.
51
+
52
+ - **`ast`** (`object`): The AST node representing a division operation.
53
+ - **Returns**: `boolean` - `true` if the division should be rendered as a fraction, `false` otherwise.
54
+
55
+ ### `isComplexExpression(ast)`
56
+
57
+ Checks if an AST node represents a "complex" expression, typically one that contains multiple operations or nested structures. This function is used by `shouldUseFractionNotation` to decide on the appropriate rendering style for fractions.
58
+
59
+ - **`ast`** (`object`): The AST node to check.
60
+ - **Returns**: `boolean` - `true` if the expression is considered complex, `false` otherwise.
61
+
62
+ ## Example
63
+
64
+ ```javascript
65
+ import { getNodeForAST, getTextBounds } from '@teachinglab/omd'; // Assuming @teachinglab/omd exports these
66
+ import * as math from 'mathjs';
67
+
68
+ // Example of getting a node class for an expression
69
+ const astExpression = math.parse('x + 2');
70
+ const NodeClassExpression = getNodeForAST(astExpression);
71
+ const nodeExpression = new NodeClassExpression(astExpression);
72
+ console.log(nodeExpression.type); // e.g., "omdBinaryExpressionNode"
73
+
74
+ // Example of getting a node class for an equation
75
+ const astEquation = math.parse('y = 2x');
76
+ const NodeClassEquation = getNodeForAST(astEquation);
77
+ const nodeEquation = new NodeClassEquation(astEquation);
78
+ console.log(nodeEquation.type); // "omdEquationNode"
79
+
80
+ // Example of getting text bounds
81
+ const bounds = getTextBounds('Hello World', 24);
82
+ console.log(`Text width: ${bounds.width}, height: ${bounds.height}`);
83
+ ```
@@ -1,148 +1,123 @@
1
- # omdVariableNode
2
-
3
- Represents a variable (like x, y, a, b) in mathematical expressions.
4
-
5
- ## Class: `omdVariableNode extends omdLeafNode`
6
-
7
- ```javascript
8
- import { omdVariableNode } from './omd/nodes/omdVariableNode.js';
9
- ```
10
-
11
- ### Constructor
12
-
13
- ```javascript
14
- new omdVariableNode(nodeData)
15
- ```
16
-
17
- **Parameters:**
18
- - `nodeData` {Object|string} - The AST node data or variable name
19
-
20
- **Description:**
21
- Creates a node representing a variable. Usually created automatically when parsing expressions.
22
-
23
- ### Properties
24
-
25
- Inherits all properties from [`omdLeafNode`](./omdLeafNode.md), plus:
26
-
27
- #### `name`
28
- - **Type:** string
29
- - **Description:** The variable name (e.g., 'x', 'y', 'theta')
30
-
31
- #### `type`
32
- - **Type:** string
33
- - **Description:** Always "variable"
34
-
35
- #### `textElement`
36
- - **Type:** SVGElement
37
- - **Description:** The SVG text element displaying the variable name
38
-
39
- ### Methods
40
-
41
- Inherits all methods from [`omdLeafNode`](./omdLeafNode.md), plus:
42
-
43
- #### `parseName(nodeData)`
44
- Internal method to extract variable name from AST data.
45
- - **Returns:** string
46
-
47
- ---
48
-
49
- #### `parseType()`
50
- Internal method to set node type.
51
- - **Returns:** "variable"
52
-
53
- ---
54
-
55
- #### `computeDimensions()`
56
- Calculates dimensions with padding around the variable.
57
- Overrides base class method.
58
-
59
- ---
60
-
61
- #### `updateLayout()`
62
- Updates the layout of the node.
63
- Overrides base class method.
64
-
65
- ---
66
-
67
- #### `toMathJSNode()`
68
- Converts to math.js AST format.
69
- - **Returns:** Object - A math.js-compatible AST node with:
70
- - `type`: "SymbolNode"
71
- - `name`: The variable name
72
- - `id`: Node ID
73
- - `provenance`: Provenance array
74
-
75
- ---
76
-
77
- #### `highlight(color)`
78
- Highlights the node and sets text color to white.
79
- - `color` {omdColor} - The highlight color
80
-
81
- ---
82
-
83
- #### `clearProvenanceHighlights()`
84
- Clears highlights and resets text color.
85
-
86
- ---
87
-
88
- #### `toString()`
89
- Convert to string representation.
90
- - **Returns:** string - The variable name
91
-
92
- ---
93
-
94
- #### `evaluate(variables)`
95
- Evaluates the variable by looking up its value in a map.
96
- - `variables` {Object} - A map of variable names to their numeric values
97
- - **Returns:** number - The value of the variable
98
- - **Throws:** Error if variable is not defined in the map
99
-
100
- ---
101
-
102
- #### `simplify()`
103
- Attempt to simplify the variable.
104
- - **Returns:** Object
105
- - `success`: false (variables cannot be simplified)
106
- - `newRoot`: null
107
- - `message`: "Cannot simplify a variable"
108
-
109
- ### Static Methods
110
-
111
- #### `fromName(name)`
112
- Create a variable node from a name.
113
- - `name` {string} - The variable name
114
- - **Returns:** omdVariableNode
115
-
116
- ### Examples
117
-
118
- #### Creating Variables
119
-
120
- ```javascript
121
- // From a name
122
- const nodeX = omdVariableNode.fromName('x');
123
- const nodeTheta = omdVariableNode.fromName('θ');
124
-
125
- // From AST data
126
- const node = new omdVariableNode({
127
- type: 'SymbolNode',
128
- name: 'x'
129
- });
130
-
131
- // Direct string name
132
- const nodeY = new omdVariableNode('y');
133
- ```
134
-
135
- #### Rendering Variables
136
-
137
- ```javascript
138
- const node = omdVariableNode.fromName('x');
139
- node.setFontSize(24);
140
- node.computeDimensions(); // Calculate size with padding
141
- node.updateLayout(); // Position the text element
142
- ```
143
-
144
- ### See Also
145
-
146
- - [omdLeafNode](./omdLeafNode.md) - Parent class
147
- - [omdNode](./omdNode.md) - Base class
148
- - [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.
package/index.js CHANGED
@@ -5,10 +5,10 @@
5
5
  * It exports all core OMD components and visualization tools from a single endpoint.
6
6
  *
7
7
  * Usage:
8
- * import { omdTable, omdBalanceHanger, omdEquationNode, omdDisplay } from 'omd-library'
8
+ * import { omdTable, omdBalanceHanger, omdEquationNode, omdDisplay } from '@teachinglab/omd'
9
9
  *
10
10
  * Or for dynamic imports:
11
- * const { omdTable } = await import('omd-library')
11
+ * const { omdTable } = await import('@teachinglab/omd')
12
12
  */
13
13
 
14
14
  // Export everything from the core OMD library (equations, nodes, display, etc.)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teachinglab/omd",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "omd",
5
5
  "main": "./index.js",
6
6
  "module": "./index.js",