@teachinglab/omd 0.1.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.
Files changed (144) hide show
  1. package/README.md +138 -0
  2. package/canvas/core/canvasConfig.js +203 -0
  3. package/canvas/core/omdCanvas.js +475 -0
  4. package/canvas/drawing/segment.js +168 -0
  5. package/canvas/drawing/stroke.js +386 -0
  6. package/canvas/events/eventManager.js +435 -0
  7. package/canvas/events/pointerEventHandler.js +263 -0
  8. package/canvas/features/focusFrameManager.js +287 -0
  9. package/canvas/index.js +49 -0
  10. package/canvas/tools/eraserTool.js +322 -0
  11. package/canvas/tools/pencilTool.js +319 -0
  12. package/canvas/tools/selectTool.js +457 -0
  13. package/canvas/tools/tool.js +223 -0
  14. package/canvas/tools/toolManager.js +394 -0
  15. package/canvas/ui/cursor.js +438 -0
  16. package/canvas/ui/toolbar.js +304 -0
  17. package/canvas/utils/boundingBox.js +378 -0
  18. package/canvas/utils/mathUtils.js +259 -0
  19. package/docs/api/configuration-options.md +104 -0
  20. package/docs/api/eventManager.md +68 -0
  21. package/docs/api/focusFrameManager.md +150 -0
  22. package/docs/api/index.md +91 -0
  23. package/docs/api/main.md +58 -0
  24. package/docs/api/omdBinaryExpressionNode.md +227 -0
  25. package/docs/api/omdCanvas.md +142 -0
  26. package/docs/api/omdConfigManager.md +192 -0
  27. package/docs/api/omdConstantNode.md +117 -0
  28. package/docs/api/omdDisplay.md +121 -0
  29. package/docs/api/omdEquationNode.md +161 -0
  30. package/docs/api/omdEquationSequenceNode.md +301 -0
  31. package/docs/api/omdEquationStack.md +139 -0
  32. package/docs/api/omdFunctionNode.md +141 -0
  33. package/docs/api/omdGroupNode.md +182 -0
  34. package/docs/api/omdHelpers.md +96 -0
  35. package/docs/api/omdLeafNode.md +163 -0
  36. package/docs/api/omdNode.md +101 -0
  37. package/docs/api/omdOperationDisplayNode.md +139 -0
  38. package/docs/api/omdOperatorNode.md +127 -0
  39. package/docs/api/omdParenthesisNode.md +122 -0
  40. package/docs/api/omdPopup.md +117 -0
  41. package/docs/api/omdPowerNode.md +127 -0
  42. package/docs/api/omdRationalNode.md +128 -0
  43. package/docs/api/omdSequenceNode.md +128 -0
  44. package/docs/api/omdSimplification.md +110 -0
  45. package/docs/api/omdSqrtNode.md +79 -0
  46. package/docs/api/omdStepVisualizer.md +115 -0
  47. package/docs/api/omdStepVisualizerHighlighting.md +61 -0
  48. package/docs/api/omdStepVisualizerInteractiveSteps.md +129 -0
  49. package/docs/api/omdStepVisualizerLayout.md +60 -0
  50. package/docs/api/omdStepVisualizerNodeUtils.md +140 -0
  51. package/docs/api/omdStepVisualizerTextBoxes.md +68 -0
  52. package/docs/api/omdToolbar.md +102 -0
  53. package/docs/api/omdTranscriptionService.md +76 -0
  54. package/docs/api/omdTreeDiff.md +134 -0
  55. package/docs/api/omdUnaryExpressionNode.md +174 -0
  56. package/docs/api/omdUtilities.md +70 -0
  57. package/docs/api/omdVariableNode.md +148 -0
  58. package/docs/api/selectTool.md +74 -0
  59. package/docs/api/simplificationEngine.md +98 -0
  60. package/docs/api/simplificationRules.md +77 -0
  61. package/docs/api/simplificationUtils.md +64 -0
  62. package/docs/api/transcribe.md +43 -0
  63. package/docs/api-reference.md +85 -0
  64. package/docs/index.html +454 -0
  65. package/docs/user-guide.md +9 -0
  66. package/index.js +67 -0
  67. package/omd/config/omdConfigManager.js +267 -0
  68. package/omd/core/index.js +150 -0
  69. package/omd/core/omdEquationStack.js +347 -0
  70. package/omd/core/omdUtilities.js +115 -0
  71. package/omd/display/omdDisplay.js +443 -0
  72. package/omd/display/omdToolbar.js +502 -0
  73. package/omd/nodes/omdBinaryExpressionNode.js +460 -0
  74. package/omd/nodes/omdConstantNode.js +142 -0
  75. package/omd/nodes/omdEquationNode.js +1223 -0
  76. package/omd/nodes/omdEquationSequenceNode.js +1273 -0
  77. package/omd/nodes/omdFunctionNode.js +352 -0
  78. package/omd/nodes/omdGroupNode.js +68 -0
  79. package/omd/nodes/omdLeafNode.js +77 -0
  80. package/omd/nodes/omdNode.js +557 -0
  81. package/omd/nodes/omdOperationDisplayNode.js +322 -0
  82. package/omd/nodes/omdOperatorNode.js +109 -0
  83. package/omd/nodes/omdParenthesisNode.js +293 -0
  84. package/omd/nodes/omdPowerNode.js +236 -0
  85. package/omd/nodes/omdRationalNode.js +295 -0
  86. package/omd/nodes/omdSqrtNode.js +308 -0
  87. package/omd/nodes/omdUnaryExpressionNode.js +178 -0
  88. package/omd/nodes/omdVariableNode.js +123 -0
  89. package/omd/simplification/omdSimplification.js +171 -0
  90. package/omd/simplification/omdSimplificationEngine.js +886 -0
  91. package/omd/simplification/package.json +6 -0
  92. package/omd/simplification/rules/binaryRules.js +1037 -0
  93. package/omd/simplification/rules/functionRules.js +111 -0
  94. package/omd/simplification/rules/index.js +48 -0
  95. package/omd/simplification/rules/parenthesisRules.js +19 -0
  96. package/omd/simplification/rules/powerRules.js +143 -0
  97. package/omd/simplification/rules/rationalRules.js +475 -0
  98. package/omd/simplification/rules/sqrtRules.js +48 -0
  99. package/omd/simplification/rules/unaryRules.js +37 -0
  100. package/omd/simplification/simplificationRules.js +32 -0
  101. package/omd/simplification/simplificationUtils.js +1056 -0
  102. package/omd/step-visualizer/omdStepVisualizer.js +597 -0
  103. package/omd/step-visualizer/omdStepVisualizerHighlighting.js +206 -0
  104. package/omd/step-visualizer/omdStepVisualizerLayout.js +245 -0
  105. package/omd/step-visualizer/omdStepVisualizerTextBoxes.js +163 -0
  106. package/omd/utils/omdNodeOverlay.js +638 -0
  107. package/omd/utils/omdPopup.js +1084 -0
  108. package/omd/utils/omdStepVisualizerInteractiveSteps.js +491 -0
  109. package/omd/utils/omdStepVisualizerNodeUtils.js +268 -0
  110. package/omd/utils/omdTranscriptionService.js +125 -0
  111. package/omd/utils/omdTreeDiff.js +734 -0
  112. package/package.json +46 -0
  113. package/src/index.js +62 -0
  114. package/src/json-schemas.md +109 -0
  115. package/src/omd-json-samples.js +115 -0
  116. package/src/omd.js +109 -0
  117. package/src/omdApp.js +391 -0
  118. package/src/omdAppCanvas.js +336 -0
  119. package/src/omdBalanceHanger.js +172 -0
  120. package/src/omdColor.js +13 -0
  121. package/src/omdCoordinatePlane.js +467 -0
  122. package/src/omdEquation.js +125 -0
  123. package/src/omdExpression.js +104 -0
  124. package/src/omdFunction.js +113 -0
  125. package/src/omdMetaExpression.js +287 -0
  126. package/src/omdNaturalExpression.js +564 -0
  127. package/src/omdNode.js +384 -0
  128. package/src/omdNumber.js +53 -0
  129. package/src/omdNumberLine.js +107 -0
  130. package/src/omdNumberTile.js +119 -0
  131. package/src/omdOperator.js +73 -0
  132. package/src/omdPowerExpression.js +92 -0
  133. package/src/omdProblem.js +55 -0
  134. package/src/omdRatioChart.js +232 -0
  135. package/src/omdRationalExpression.js +115 -0
  136. package/src/omdSampleData.js +215 -0
  137. package/src/omdShapes.js +476 -0
  138. package/src/omdSpinner.js +148 -0
  139. package/src/omdString.js +39 -0
  140. package/src/omdTable.js +369 -0
  141. package/src/omdTapeDiagram.js +245 -0
  142. package/src/omdTerm.js +92 -0
  143. package/src/omdTileEquation.js +349 -0
  144. package/src/omdVariable.js +51 -0
@@ -0,0 +1,352 @@
1
+ import { omdNode } from "./omdNode.js";
2
+ import { getNodeForAST, getTextBounds } from "../core/omdUtilities.js";
3
+ import { omdConstantNode } from "./omdConstantNode.js";
4
+ import { jsvgTextLine } from '@teachinglab/jsvg';
5
+ /**
6
+ * Represents a function call node in the mathematical expression tree
7
+ * Handles rendering of function names, arguments, and parentheses
8
+ * @extends omdNode
9
+ */
10
+ export class omdFunctionNode extends omdNode {
11
+ /**
12
+ * Creates a function node from AST data
13
+ * @param {Object} astNodeData - The AST node containing function information
14
+ */
15
+ constructor(astNodeData) {
16
+ super(astNodeData);
17
+ this.type = "omdFunctionNode";
18
+ this.functionName = astNodeData.fn?.name || astNodeData.name || 'f';
19
+ this.args = astNodeData.args || [];
20
+
21
+ this.createTextElements();
22
+ this.createArgumentNodes();
23
+ this.computeDimensions();
24
+ this.updateLayout();
25
+ }
26
+
27
+ /**
28
+ * Creates text elements for function name and parentheses
29
+ * @private
30
+ */
31
+ createTextElements() {
32
+ const createText = (text) => {
33
+ const element = new jsvgTextLine();
34
+ element.setText(text);
35
+ element.setTextAnchor('start');
36
+ // Note: dominant-baseline doesn't have a jsvg method, keeping setAttribute for now
37
+ element.svgObject.setAttribute('dominant-baseline', 'middle');
38
+ this.addChild(element);
39
+ return element;
40
+ };
41
+
42
+ this.functionNameElement = createText(this.functionName);
43
+ this.openParenElement = createText('(');
44
+ this.closeParenElement = createText(')');
45
+ }
46
+
47
+ /**
48
+ * Creates nodes for function arguments and comma separators
49
+ * @private
50
+ */
51
+ createArgumentNodes() {
52
+ this.argNodes = [];
53
+ this.commaElements = [];
54
+
55
+ this.argumentNodeList.args = [];
56
+
57
+ this.args.forEach((argAst, index) => {
58
+ const ArgNodeType = getNodeForAST(argAst);
59
+ const argNode = new ArgNodeType(argAst);
60
+ this.argNodes.push(argNode);
61
+ this.addChild(argNode);
62
+
63
+ this.argumentNodeList.args.push(argNode);
64
+
65
+ if (index < this.args.length - 1) {
66
+ const commaElement = new jsvgTextLine();
67
+ commaElement.setText(', ');
68
+ commaElement.setTextAnchor('start');
69
+ commaElement.svgObject.setAttribute('dominant-baseline', 'middle');
70
+ this.commaElements.push(commaElement);
71
+ this.addChild(commaElement);
72
+ }
73
+ });
74
+ }
75
+
76
+ /**
77
+ * Calculates the dimensions of the function node and its children
78
+ * @override
79
+ */
80
+ computeDimensions() {
81
+ const fontSize = this.getFontSize();
82
+ const argFontSize = fontSize * 5/6; // Match rational node scaling
83
+
84
+ // Set font sizes for text elements
85
+ this.functionNameElement.setFontSize(fontSize);
86
+ this.openParenElement.setFontSize(fontSize);
87
+ this.closeParenElement.setFontSize(fontSize);
88
+
89
+ // Set font sizes for arguments and compute their dimensions
90
+ this.argNodes.forEach(argNode => {
91
+ argNode.setFontSize(argFontSize);
92
+ argNode.computeDimensions();
93
+ });
94
+
95
+ // Set font sizes for commas
96
+ this.commaElements.forEach(comma => {
97
+ comma.setFontSize(argFontSize);
98
+ });
99
+
100
+ // Calculate dimensions using getTextBounds for consistency
101
+ const ratio = fontSize / this.getRootFontSize();
102
+ const spacing = 2 * ratio;
103
+
104
+ const functionNameBounds = getTextBounds(this.functionName, fontSize);
105
+ const openParenBounds = getTextBounds('(', fontSize);
106
+ const closeParenBounds = getTextBounds(')', fontSize);
107
+ const commaBounds = getTextBounds(', ', argFontSize);
108
+
109
+ let totalArgWidth = 0;
110
+ let maxArgHeight = 0;
111
+
112
+ this.argNodes.forEach((argNode, index) => {
113
+ totalArgWidth += argNode.width;
114
+ maxArgHeight = Math.max(maxArgHeight, argNode.height);
115
+ if (index < this.commaElements.length) {
116
+ totalArgWidth += commaBounds.width + spacing;
117
+ }
118
+ });
119
+
120
+ const totalWidth = functionNameBounds.width + openParenBounds.width + totalArgWidth + closeParenBounds.width + (spacing * 2);
121
+ const totalHeight = Math.max(maxArgHeight, functionNameBounds.height, openParenBounds.height, closeParenBounds.height) + 4 * ratio;
122
+
123
+ this.setWidthAndHeight(totalWidth, totalHeight);
124
+ }
125
+
126
+ /**
127
+ * Updates the layout of the function node and its children
128
+ * @override
129
+ */
130
+ updateLayout() {
131
+ const fontSize = this.getFontSize();
132
+ const argFontSize = fontSize * 5/6;
133
+ const ratio = fontSize / this.getRootFontSize();
134
+ const spacing = 2 * ratio;
135
+
136
+ let currentX = 0;
137
+ const textY = this.height / 2;
138
+
139
+ // Position function name using setPosition where possible
140
+ this.functionNameElement.setPosition(currentX, textY);
141
+ currentX += getTextBounds(this.functionName, fontSize).width;
142
+
143
+ // Position opening parenthesis
144
+ this.openParenElement.setPosition(currentX, textY);
145
+ currentX += getTextBounds('(', fontSize).width;
146
+
147
+ // Position arguments and commas
148
+ this.argNodes.forEach((argNode, index) => {
149
+ argNode.setPosition(currentX, (this.height - argNode.height) / 2);
150
+ argNode.updateLayout();
151
+ currentX += argNode.width;
152
+
153
+ if (index < this.commaElements.length) {
154
+ currentX += spacing;
155
+ this.commaElements[index].setPosition(currentX, textY);
156
+ currentX += getTextBounds(', ', argFontSize).width + spacing;
157
+ }
158
+ });
159
+
160
+ // Position closing parenthesis
161
+ this.closeParenElement.setPosition(currentX, textY);
162
+ }
163
+
164
+ /**
165
+ * Highlights the function node and all its arguments
166
+ */
167
+ highlightAll() {
168
+ this.select();
169
+
170
+ this.argNodes.forEach(argNode => {
171
+ if (argNode.highlightAll) {
172
+ argNode.highlightAll();
173
+ }
174
+ });
175
+ }
176
+
177
+ /**
178
+ * Unhighlights the function node and all its arguments
179
+ */
180
+ unhighlightAll() {
181
+ this.deselect();
182
+
183
+ this.argNodes.forEach(argNode => {
184
+ if (argNode.unhighlightAll) {
185
+ argNode.unhighlightAll();
186
+ }
187
+ });
188
+ }
189
+
190
+ clone() {
191
+ let newAstData;
192
+ if (typeof this.astNodeData.clone === 'function') {
193
+ newAstData = this.astNodeData.clone();
194
+ } else {
195
+ newAstData = JSON.parse(JSON.stringify(this.astNodeData));
196
+ }
197
+ const clone = new omdFunctionNode(newAstData);
198
+
199
+ // Preserve the background rectangle.
200
+ const backRect = clone.backRect;
201
+ clone.removeAllChildren();
202
+ clone.addChild(backRect);
203
+
204
+ // Manually clone properties
205
+ clone.functionName = this.functionName;
206
+ clone.args = JSON.parse(JSON.stringify(this.args));
207
+
208
+ // Re-create text elements using the existing method
209
+ clone.createTextElements();
210
+
211
+ // Re-create argument nodes using the existing method, ensuring they are cloned properly
212
+ // The createArgumentNodes method already handles adding children and populating argumentNodeList.
213
+ clone.argNodes = this.argNodes.map(argNode => argNode.clone());
214
+ // Manually set parents and add to childList, as createArgumentNodes might re-parse AST if not careful
215
+ clone.argNodes.forEach(arg => {
216
+ arg.parent = clone;
217
+ clone.addChild(arg);
218
+ });
219
+ clone.argumentNodeList.args = clone.argNodes;
220
+
221
+ // Ensure commas are also cloned and added
222
+ clone.commaElements = this.commaElements.map(c => {
223
+ const newComma = new jsvgTextLine();
224
+ newComma.setText(c.text);
225
+ newComma.setTextAnchor(c.textAnchor);
226
+ newComma.svgObject.setAttribute('dominant-baseline', c.svgObject.getAttribute('dominant-baseline'));
227
+ newComma.parent = clone;
228
+ clone.addChild(newComma);
229
+ return newComma;
230
+ });
231
+
232
+ // Now ensure correct order of children, if not already handled by createTextElements and createArgumentNodes
233
+ // The constructor already calls computeDimensions and updateLayout, which will handle final layout.
234
+ clone.addChild(clone.closedParen);
235
+
236
+ // Explicitly update the argumentNodeList in the cloned node
237
+ clone.argumentNodeList.args = clone.args;
238
+
239
+ // The crucial step: link the clone to its origin
240
+ clone.provenance.push(this.id);
241
+
242
+ return clone;
243
+ }
244
+
245
+ /**
246
+ * Converts the omdFunctionNode to a math.js AST node.
247
+ * @returns {Object} A math.js-compatible AST node.
248
+ */
249
+ toMathJSNode() {
250
+ const astNode = {
251
+ type: 'FunctionNode',
252
+ fn: { type: 'SymbolNode', name: this.functionName, clone: function() { return {...this}; } },
253
+ args: this.argNodes.map(arg => arg.toMathJSNode())
254
+ };
255
+
256
+ // Add a clone method to maintain compatibility with math.js's expectations.
257
+ astNode.clone = function() {
258
+ const clonedNode = { ...this };
259
+ clonedNode.args = this.args.map(arg => arg.clone());
260
+ if (this.fn && typeof this.fn.clone === 'function') {
261
+ clonedNode.fn = this.fn.clone();
262
+ }
263
+ return clonedNode;
264
+ };
265
+ return astNode;
266
+ }
267
+
268
+ /**
269
+ * Convert to string representation
270
+ * @returns {string} The function as a string
271
+ */
272
+ toString() {
273
+ const argStrings = this.argNodes.map(arg => arg.toString());
274
+ return `${this.functionName}(${argStrings.join(', ')})`;
275
+ }
276
+
277
+ /**
278
+ * Evaluate the function with given variable values
279
+ * @param {Object} variables - Variable name to value mapping
280
+ * @returns {number} The evaluated result
281
+ */
282
+ evaluate(variables = {}) {
283
+ // First evaluate all arguments
284
+ const evaluatedArgs = this.argNodes.map(arg => {
285
+ if (arg.evaluate) {
286
+ return arg.evaluate(variables);
287
+ } else if (arg.type === 'omdConstantNode') {
288
+ return parseFloat(arg.value);
289
+ } else if (arg.type === 'omdVariableNode' && variables[arg.name] !== undefined) {
290
+ return variables[arg.name];
291
+ }
292
+ throw new Error(`Cannot evaluate argument: ${arg.toString()}`);
293
+ });
294
+
295
+ // Use math.js to evaluate the function
296
+ if (window.math && window.math[this.functionName]) {
297
+ try {
298
+ return window.math[this.functionName](...evaluatedArgs);
299
+ } catch (error) {
300
+ throw new Error(`Error evaluating ${this.functionName}: ${error.message}`);
301
+ }
302
+ }
303
+
304
+ // Fallback for common functions if math.js is not available
305
+ switch (this.functionName) {
306
+ case 'sin': return Math.sin(evaluatedArgs[0]);
307
+ case 'cos': return Math.cos(evaluatedArgs[0]);
308
+ case 'tan': return Math.tan(evaluatedArgs[0]);
309
+ case 'asin': return Math.asin(evaluatedArgs[0]);
310
+ case 'acos': return Math.acos(evaluatedArgs[0]);
311
+ case 'atan': return Math.atan(evaluatedArgs[0]);
312
+ case 'sqrt': return Math.sqrt(evaluatedArgs[0]);
313
+ case 'abs': return Math.abs(evaluatedArgs[0]);
314
+ case 'log': return Math.log(evaluatedArgs[0]);
315
+ case 'log10': return Math.log10(evaluatedArgs[0]);
316
+ case 'exp': return Math.exp(evaluatedArgs[0]);
317
+ case 'pow': return Math.pow(evaluatedArgs[0], evaluatedArgs[1]);
318
+ case 'min': return Math.min(...evaluatedArgs);
319
+ case 'max': return Math.max(...evaluatedArgs);
320
+ case 'floor': return Math.floor(evaluatedArgs[0]);
321
+ case 'ceil': return Math.ceil(evaluatedArgs[0]);
322
+ case 'round': return Math.round(evaluatedArgs[0]);
323
+ default:
324
+ throw new Error(`Unknown function: ${this.functionName}`);
325
+ }
326
+ }
327
+
328
+ /**
329
+ * Create a function from a string
330
+ * @param {string} functionString - The function as a string
331
+ * @returns {omdFunctionNode} The created function node
332
+ * @static
333
+ */
334
+ static fromString(functionString) {
335
+ if (!window.math) {
336
+ throw new Error("Math.js is required for parsing function strings");
337
+ }
338
+
339
+ try {
340
+ const ast = window.math.parse(functionString);
341
+
342
+ // Verify it's a function node
343
+ if (ast.type !== 'FunctionNode') {
344
+ throw new Error("String does not represent a function call");
345
+ }
346
+
347
+ return new omdFunctionNode(ast);
348
+ } catch (error) {
349
+ throw new Error(`Failed to parse function string: ${error.message}`);
350
+ }
351
+ }
352
+ }
@@ -0,0 +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
+ }
68
+ }
@@ -0,0 +1,77 @@
1
+ import { omdNode } from "./omdNode.js";
2
+ import { getTextBounds } from "../core/omdUtilities.js";
3
+ import { jsvgTextLine } from '@teachinglab/jsvg';
4
+
5
+ /**
6
+ * Represents a leaf in the AST tree, such as an operator, constant, variable, or grouping symbol.
7
+ * @extends omdNode
8
+ */
9
+ export class omdLeafNode extends omdNode {
10
+ /**
11
+ * Creates a leaf node from the AST data.
12
+ * @param {Object} astNodeData - The AST node containing leaf information.
13
+ */
14
+ constructor(nodeData) {
15
+ super(nodeData);
16
+ this.type = "omdLeafNode";
17
+ }
18
+
19
+ /**
20
+ * Creates and positions the text element for the constant
21
+ * @param {string|number} text - The text content to display.
22
+ */
23
+ createTextElement(text) {
24
+ let textElement = new jsvgTextLine();
25
+ textElement.setText(text);
26
+
27
+ textElement.setTextAnchor('middle');
28
+ textElement.svgObject.setAttribute('dominant-baseline', 'middle');
29
+
30
+ this.addChild(textElement);
31
+
32
+ return textElement;
33
+ }
34
+
35
+ clone() {
36
+ const clone = new this.constructor(this.astNodeData);
37
+
38
+ // The crucial step: link the clone to its origin
39
+ clone.provenance.push(this.id);
40
+
41
+ return clone;
42
+ }
43
+
44
+ updateTextElement(text) {
45
+ this.textElement.setText(text);
46
+ }
47
+
48
+ /**
49
+ * Calculates the dimensions of the node
50
+ * @override
51
+ */
52
+ computeDimensions() {
53
+ // Gerard: Checks for parent fontSize property and updates size accordingly
54
+ let fontSize = this.getFontSize();
55
+ this.textElement.setFontSize(fontSize);
56
+
57
+ // Gerard: Gets text bounds based on fontSize using temporary span element
58
+ let bounds = getTextBounds(this.textElement.getText(), fontSize);
59
+ this.setWidthAndHeight(bounds.width, bounds.height);
60
+ }
61
+
62
+ /**
63
+ * Updates the position of the node
64
+ * @override
65
+ */
66
+ updateLayout() {
67
+ this.updateTextPosition();
68
+ }
69
+
70
+ /**
71
+ * Updates the position of the text
72
+ * @private
73
+ */
74
+ updateTextPosition() {
75
+ this.textElement.setPosition(this.width / 2, this.height / 2);
76
+ }
77
+ }