@teachinglab/omd 0.5.6 → 0.5.8

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 (60) hide show
  1. package/canvas/ui/toolbar.js +6 -9
  2. package/index.js +3 -0
  3. package/npm-docs/DOCUMENTATION_SUMMARY.md +220 -0
  4. package/npm-docs/README.md +251 -0
  5. package/npm-docs/api/api-reference.md +85 -0
  6. package/npm-docs/api/configuration-options.md +198 -0
  7. package/npm-docs/api/eventManager.md +83 -0
  8. package/npm-docs/api/expression-nodes.md +561 -0
  9. package/npm-docs/api/focusFrameManager.md +145 -0
  10. package/npm-docs/api/index.md +106 -0
  11. package/npm-docs/api/main.md +63 -0
  12. package/npm-docs/api/omdBinaryExpressionNode.md +86 -0
  13. package/npm-docs/api/omdCanvas.md +84 -0
  14. package/npm-docs/api/omdConfigManager.md +113 -0
  15. package/npm-docs/api/omdConstantNode.md +53 -0
  16. package/npm-docs/api/omdDisplay.md +87 -0
  17. package/npm-docs/api/omdEquationNode.md +174 -0
  18. package/npm-docs/api/omdEquationSequenceNode.md +259 -0
  19. package/npm-docs/api/omdEquationStack.md +193 -0
  20. package/npm-docs/api/omdFunctionNode.md +83 -0
  21. package/npm-docs/api/omdGroupNode.md +79 -0
  22. package/npm-docs/api/omdHelpers.md +88 -0
  23. package/npm-docs/api/omdLeafNode.md +86 -0
  24. package/npm-docs/api/omdNode.md +202 -0
  25. package/npm-docs/api/omdOperationDisplayNode.md +118 -0
  26. package/npm-docs/api/omdOperatorNode.md +92 -0
  27. package/npm-docs/api/omdParenthesisNode.md +134 -0
  28. package/npm-docs/api/omdPopup.md +192 -0
  29. package/npm-docs/api/omdPowerNode.md +132 -0
  30. package/npm-docs/api/omdRationalNode.md +145 -0
  31. package/npm-docs/api/omdSequenceNode.md +128 -0
  32. package/npm-docs/api/omdSimplification.md +79 -0
  33. package/npm-docs/api/omdSqrtNode.md +144 -0
  34. package/npm-docs/api/omdStepVisualizer.md +147 -0
  35. package/npm-docs/api/omdStepVisualizerHighlighting.md +66 -0
  36. package/npm-docs/api/omdStepVisualizerInteractiveSteps.md +109 -0
  37. package/npm-docs/api/omdStepVisualizerLayout.md +71 -0
  38. package/npm-docs/api/omdStepVisualizerNodeUtils.md +140 -0
  39. package/npm-docs/api/omdStepVisualizerTextBoxes.md +77 -0
  40. package/npm-docs/api/omdToolbar.md +131 -0
  41. package/npm-docs/api/omdTranscriptionService.md +96 -0
  42. package/npm-docs/api/omdTreeDiff.md +170 -0
  43. package/npm-docs/api/omdUnaryExpressionNode.md +137 -0
  44. package/npm-docs/api/omdUtilities.md +83 -0
  45. package/npm-docs/api/omdVariableNode.md +123 -0
  46. package/npm-docs/api/selectTool.md +74 -0
  47. package/npm-docs/api/simplificationEngine.md +98 -0
  48. package/npm-docs/api/simplificationRules.md +77 -0
  49. package/npm-docs/api/simplificationUtils.md +64 -0
  50. package/npm-docs/api/transcribe.md +43 -0
  51. package/npm-docs/guides/equations.md +854 -0
  52. package/npm-docs/guides/factory-functions.md +354 -0
  53. package/npm-docs/guides/getting-started.md +318 -0
  54. package/npm-docs/guides/quick-examples.md +525 -0
  55. package/npm-docs/guides/visualizations.md +682 -0
  56. package/npm-docs/json-schemas.md +826 -0
  57. package/omd/utils/omdTranscriptionService.js +1 -1
  58. package/package.json +2 -1
  59. package/src/index.js +2 -0
  60. package/src/omdFactory.js +150 -0
@@ -0,0 +1,74 @@
1
+
2
+ # SelectTool
3
+
4
+ The `SelectTool` is a tool for selecting, moving, and deleting stroke segments on the canvas. It extends the `Tool` class.
5
+
6
+ ## Class Definition
7
+
8
+ ```javascript
9
+ export class SelectTool extends Tool {
10
+ // ...
11
+ }
12
+ ```
13
+
14
+ ## Constructor
15
+
16
+ ### `new SelectTool(canvas, [options])`
17
+
18
+ Creates a new `SelectTool` instance.
19
+
20
+ * **canvas** (`OMDCanvas`): The canvas instance.
21
+ * **[options]** (`object`, optional): Configuration options for the tool.
22
+ * **selectionColor** (`string`, optional): The color of the selection box. Defaults to `'#007bff'`.
23
+ * **selectionOpacity** (`number`, optional): The opacity of the selection box. Defaults to `0.3`.
24
+
25
+ ## Public Methods
26
+
27
+ ### `onPointerDown(event)`
28
+
29
+ Handles the pointer down event to start a selection.
30
+
31
+ * **event** (`PointerEvent`): The pointer event.
32
+
33
+ ### `onPointerMove(event)`
34
+
35
+ Handles the pointer move event to update the selection box.
36
+
37
+ * **event** (`PointerEvent`): The pointer event.
38
+
39
+ ### `onPointerUp(event)`
40
+
41
+ Handles the pointer up event to complete the selection.
42
+
43
+ * **event** (`PointerEvent`): The pointer event.
44
+
45
+ ### `onCancel()`
46
+
47
+ Cancels the current selection operation.
48
+
49
+ ### `onKeyboardShortcut(key, event)`
50
+
51
+ Handles keyboard shortcuts for selection-related actions.
52
+
53
+ * **key** (`string`): The key that was pressed.
54
+ * **event** (`KeyboardEvent`): The keyboard event.
55
+ * **Returns**: `boolean` - True if the shortcut was handled, false otherwise.
56
+
57
+ ### `getCursor()`
58
+
59
+ Gets the cursor for the tool.
60
+
61
+ * **Returns**: `string` - The CSS cursor name.
62
+
63
+ ### `clearSelection()`
64
+
65
+ Clears the current selection.
66
+
67
+ ## Properties
68
+
69
+ * **displayName** (`string`): The display name of the tool.
70
+ * **description** (`string`): A description of the tool.
71
+ * **icon** (`string`): The icon for the tool.
72
+ * **shortcut** (`string`): The keyboard shortcut for the tool.
73
+ * **category** (`string`): The category of the tool.
74
+ * **selectedSegments** (`Map<string, Set<number>>`): A map of selected segments, where the key is the stroke ID and the value is a set of segment indices.
@@ -0,0 +1,98 @@
1
+ # SimplificationEngine
2
+
3
+ The `SimplificationEngine` is a static class that provides a collection of helper methods for creating, matching, and transforming expression nodes. It forms the backbone of the simplification rule system.
4
+
5
+ ## Overview
6
+
7
+ The engine is not instantiated. Instead, its static methods are used directly by the simplification rules to perform their logic. The methods can be grouped into several categories:
8
+
9
+ - **Node Creation:** Helpers to build new `omdNode` instances (e.g., `createConstant`, `createBinaryOp`).
10
+ - **Pattern Matching:** Helpers to check if a node matches a specific structure or value (e.g., `isBinaryOp`, `isConstantValue`).
11
+ - **Monomial Helpers:** Specialized functions for working with monomials (terms like `2x^2`), which are essential for rules like combining like terms.
12
+ - **Rule Definition:** The `SimplificationRule` inner class, which defines the structure for all simplification rules.
13
+
14
+ ## Node Creation Helpers
15
+
16
+ These methods simplify the process of creating new nodes, often by abstracting away the underlying AST structure.
17
+
18
+ #### `createConstant(value, fontSize)`
19
+ - Creates an `omdConstantNode`.
20
+
21
+ #### `createBinaryOp(left, operator, right, ...)`
22
+ - Creates an `omdBinaryExpressionNode` for operations like 'add', 'subtract', etc.
23
+
24
+ #### `createMultiplication(leftConstantNode, rightNode, ...)`
25
+ - A specialized version of `createBinaryOp` for multiplication.
26
+
27
+ #### `createRational(numerator, denominator, fontSize)`
28
+ - Creates an `omdRationalNode` (a fraction).
29
+
30
+ #### `createMonomial(coefficient, variable, power, ...)`
31
+ - Constructs a complete monomial term (e.g., `2x^3`) from its constituent parts.
32
+
33
+ ## Pattern Matching Helpers
34
+
35
+ These methods are used within the `match` function of a simplification rule to determine if the rule applies to a given node.
36
+
37
+ #### `isType(node, typeName)`
38
+ - Checks if a node is an instance of a specific class (e.g., `'omdConstantNode'`).
39
+
40
+ #### `isBinaryOp(node, operator)`
41
+ - Checks if a node is a binary operation, optionally matching a specific operator (e.g., `'add'`).
42
+
43
+ #### `isConstantValue(node, value)`
44
+ - Checks if a node is a constant, optionally matching a specific value.
45
+
46
+ #### `hasConstantOperand(node)`
47
+ - For a binary operation, checks if one of its operands is a constant and returns both the constant and the other operand.
48
+
49
+ #### `unwrapParentheses(node)`
50
+ - If the given node is an `omdParenthesisNode`, it returns the inner expression; otherwise, it returns the node itself.
51
+
52
+ ## Monomial and Like Terms Helpers
53
+
54
+ #### `isMonomial(node)`
55
+ - A powerful helper that checks if a node represents a monomial (e.g., `x`, `2x`, `-5x^2`).
56
+ - **Returns:** An object with `{ coefficient, variable, power, ... }` if it is a monomial, otherwise `null`.
57
+
58
+ #### `areLikeTerms(monomial1, monomial2)`
59
+ - Compares the output of two `isMonomial` calls to see if they have the same variable and power.
60
+
61
+ ## `SimplificationRule` Class
62
+
63
+ This is an inner class of `SimplificationEngine` that provides the blueprint for all simplification rules.
64
+
65
+ ### Constructor
66
+
67
+ `new SimplificationRule(name, matchFn, transformFn, messageFn, type)`
68
+
69
+ - `name` {string}: The name of the rule (e.g., "Combine Constants").
70
+ - `matchFn` {function}: A function that takes a node and returns `true` or a data object if the rule applies, `false` otherwise.
71
+ - `transformFn` {function}: A function that takes the matched node and the data from `matchFn` and returns a new, transformed node.
72
+ - `messageFn` {function} (optional): A function to generate a user-friendly description of the change.
73
+
74
+ ### Key Methods
75
+
76
+ - `canApply(node)`: Executes the `matchFn`.
77
+ - `apply(node, ruleData, currentRoot)`: Executes the `transformFn` and handles replacing the old node with the new one in the expression tree.
78
+
79
+ ## Rule Factory Functions
80
+
81
+ These are static methods on `SimplificationEngine` that create common types of rules.
82
+
83
+ #### `createRule(...)`
84
+ - The base factory function for creating a `SimplificationRule` instance.
85
+
86
+ #### `createConstantFoldRule(name, operator)`
87
+ - Creates a rule that evaluates binary operations on two constants (e.g., `2 + 3` -> `5`).
88
+
89
+ #### `createIdentityRule(name, operator, identityValue, side)`
90
+ - Creates a rule for identity operations (e.g., `x + 0` -> `x` or `y * 1` -> `y`).
91
+
92
+ #### `createZeroMultiplicationRule()`
93
+ - Creates the rule for `x * 0` -> `0`.
94
+
95
+ ### See Also
96
+
97
+ - [`omdSimplification`](./omdSimplification.md): The top-level module that uses the engine.
98
+ - [`simplificationRules`](./simplificationRules.md): The module where specific rules are defined using the engine's helpers.
@@ -0,0 +1,77 @@
1
+ # Simplification Rules
2
+
3
+ This module defines the specific simplification rules used by the `SimplificationEngine`. The rules are organized into an object, keyed by the `omdNode` type they apply to.
4
+
5
+ ## Rule Structure
6
+
7
+ Each rule is an instance of `SimplificationEngine.SimplificationRule` and has three main parts:
8
+
9
+ 1. **`match` function:** Determines if the rule can be applied to a given node.
10
+ 2. **`transform` function:** Executes the simplification and returns a new, transformed node.
11
+ 3. **`message` function:** Generates a human-readable description of the transformation.
12
+
13
+ ## Available Rules
14
+
15
+ The `rules` object contains arrays of rules for each of the following node types:
16
+
17
+ ### `binary` (for `omdBinaryExpressionNode`)
18
+
19
+ - **Same Term Cancellation:** `a - a` simplifies to `0`.
20
+ - **Opposite Term Cancellation:** `a + (-a)` simplifies to `0`.
21
+ - **Cancel Constants in Sums:** `x + 2 - 2` simplifies to `x + 0`.
22
+ - **Constant Folding:** Evaluates operations on constants (e.g., `2 + 3` -> `5`). Includes rules for `add`, `subtract`, `multiply`, and `divide`.
23
+ - **Identity Operations:** Simplifies operations with identity elements (e.g., `x + 0` -> `x`, `y * 1` -> `y`).
24
+ - **Zero Multiplication:** `x * 0` simplifies to `0`.
25
+ - **Combine Coefficients:** `2 * 3x` simplifies to `6x`.
26
+ - **Distributive Property:** `2 * (x + 3)` expands to `2x + 6`.
27
+ - **Expand Polynomial Multiplication:** `(x + 2)(x + 3)` expands to `x^2 + 5x + 6`.
28
+ - **Multiply Monomials:** `2x * 3x` simplifies to `6x^2`.
29
+ - **Combine Like Factors:** `x * x` simplifies to `x^2`.
30
+ - **Combine Multiple Constants in Sums:** `x + 2 + 3` simplifies to `x + 5`.
31
+ - **Combine Like Terms:** `2x + 3x` simplifies to `5x`.
32
+
33
+ ### `rational` (for `omdRationalNode`)
34
+
35
+ - **Variable Self Division:** `x / x` simplifies to `1`.
36
+ - **Power Base Division:** `x^n / x` simplifies to `x^(n-1)`.
37
+ - **Monomial Variable Division:** `cx / x` simplifies to `c`.
38
+ - **Simplify Fraction:** Reduces constant fractions to their simplest form (e.g., `6/8` -> `3/4`).
39
+ - **Simplify Multiplication in Rational:** `(4x)/3` restructures to `(4/3) * x`.
40
+ - **Simplify Rational Division:** `(4x + 6) / 2` distributes the division to `2x + 3`.
41
+
42
+ ### `parenthesis` (for `omdParenthesisNode`)
43
+
44
+ - **Remove Redundant Parentheses:** `((x))` simplifies to `(x)`.
45
+
46
+ ### `unary` (for `omdUnaryExpressionNode`)
47
+
48
+ - **Simplify Double Negation:** `-(-x)` simplifies to `x`.
49
+ - **Remove Unary Plus:** `+x` simplifies to `x`.
50
+
51
+ ### `power` (for `omdPowerNode`)
52
+
53
+ - **Calculate Powers:** Evaluates constant powers (e.g., `2^3` -> `8`).
54
+ - **Power of Zero:** `x^0` simplifies to `1`.
55
+ - **Power of One:** `x^1` simplifies to `x`.
56
+ - **Expand Polynomial Power:** `(x - 2)^2` expands to `x^2 - 4x + 4`.
57
+ - **Square of Square Root:** `(sqrt(x))^2` simplifies to `x`.
58
+ - **Fractional Exponent to Square Root:** `x^(1/2)` converts to `sqrt(x)`.
59
+
60
+ ### `sqrt` (for `omdSqrtNode`)
61
+
62
+ - **Simplify Square Root of Constant:** `sqrt(9)` simplifies to `3`.
63
+ - **Square Root of Square:** `sqrt(x^2)` simplifies to `x` (assuming `x >= 0`).
64
+
65
+ ### `function` (for `omdFunctionNode`)
66
+
67
+ - **Function Inverse Simplification:** `sin(arcsin(x))` simplifies to `x`.
68
+ - **Function to Sqrt Node:** Converts `sqrt(x)` from a function representation to the dedicated `omdSqrtNode`.
69
+
70
+ ## Rule Lookup
71
+
72
+ The `getRulesForNode(node)` function is used by the simplification engine to retrieve the appropriate list of rules for a given node based on its type.
73
+
74
+ ### See Also
75
+
76
+ - [`omdSimplification`](./omdSimplification.md): The top-level module that uses these rules.
77
+ - [`SimplificationEngine`](./simplificationEngine.md): The engine that provides the helpers to create and apply these rules.
@@ -0,0 +1,64 @@
1
+ # Simplification Utilities
2
+
3
+ This module provides a collection of helper functions that are used throughout the simplification engine and rules. These utilities handle common tasks like creating nodes, manipulating expression trees, and formatting output.
4
+
5
+ ## Core Functions
6
+
7
+ #### `gcd(a, b)`
8
+ - Calculates the greatest common divisor of two numbers.
9
+
10
+ #### `createConstantNode(value, fontSize)`
11
+ - Creates a new, fully initialized `omdConstantNode`.
12
+
13
+ #### `createRationalNode(numerator, denominator, fontSize)`
14
+ - Creates a new, fully initialized `omdRationalNode`, handling negative numerators and cases where the denominator is 1.
15
+
16
+ #### `nodeToString(node)`
17
+ - A robust function that converts any `omdNode` into a human-readable string, handling different node types and formatting them correctly.
18
+
19
+ ## Expression Tree Manipulation
20
+
21
+ #### `flattenSum(node, terms)`
22
+ - A key utility that takes a nested tree of additions and subtractions (e.g., `a - (b + c)`) and flattens it into a linear list of terms, each with an associated sign.
23
+ - **Parameters:**
24
+ - `node`: The root of the expression to flatten.
25
+ - `terms`: An array that will be populated with the term objects (`{ node, sign }`).
26
+
27
+ #### `buildSumTree(terms, fontSize)`
28
+ - The counterpart to `flattenSum`. It takes a list of terms (like the one produced by `flattenSum`) and reconstructs a valid, balanced `omdBinaryExpressionNode` tree from them.
29
+
30
+ #### `_replaceNodeInTree(oldNode, newNode, currentRoot)`
31
+ - An internal helper that safely replaces a node within the larger expression tree, handling parent-child connections and ensuring the tree remains valid.
32
+
33
+ ## Polynomial and Monomial Helpers
34
+
35
+ #### `expandPolynomialPower(terms, exponent, fontSize)`
36
+ - Expands a polynomial raised to a power (e.g., `(a+b)^2`). It uses more specific helpers for common cases.
37
+
38
+ #### `expandBinomialSquare(terms, fontSize)`
39
+ - Expands `(a+b+...)^2`.
40
+
41
+ #### `expandBinomialCube(terms, fontSize)`
42
+ - Expands `(a+b)^3`.
43
+
44
+ #### `expandBinomialFourth(terms, fontSize)`
45
+ - Expands `(a+b)^4`.
46
+
47
+ #### `multiplyTermArrays(terms1, terms2, fontSize)`
48
+ - Performs polynomial multiplication by multiplying each term from the first array by each term from the second.
49
+
50
+ ## Provenance Helpers
51
+
52
+ #### `extractLeafNodes(node)`
53
+ - Traverses an expression and returns an array of all its leaf nodes (constants and variables). This is used for detailed provenance tracking.
54
+
55
+ #### `extractMonomialProvenance(termNode)`
56
+ - A specialized function that separates the leaf nodes of a monomial into `coefficientNodes` and `variableNodes` for highly granular highlighting.
57
+
58
+ #### `createMonomialWithGranularProvenance(...)`
59
+ - Creates a monomial while assigning separate provenance chains to its coefficient and variable parts.
60
+
61
+ ### See Also
62
+
63
+ - [`omdSimplification`](./omdSimplification.md)
64
+ - [`simplificationRules`](./simplificationRules.md)
@@ -0,0 +1,43 @@
1
+ # Netlify Function: transcribe
2
+
3
+ This Netlify serverless function provides a backend for the `omdTranscriptionService`. It receives an image, sends it to the Gemini API for transcription, and returns the result.
4
+
5
+ ## Endpoint
6
+
7
+ `/.netlify/functions/transcribe`
8
+
9
+ ## Method
10
+
11
+ `POST`
12
+
13
+ ## Request Body
14
+
15
+ The request body should be a JSON object with the following properties:
16
+
17
+ * **imageBase64** (`string`, required): The base64 encoded image data (without the `data:image/png;base64,` prefix).
18
+ * **prompt** (`string`, optional): A custom prompt to guide the transcription. If not provided, a default prompt for mathematical expressions is used.
19
+
20
+ ## Responses
21
+
22
+ ### Success Response (200 OK)
23
+
24
+ A successful response will be a JSON object with the following properties:
25
+
26
+ * **text** (`string`): The transcribed text.
27
+ * **provider** (`string`): The transcription provider used (always `'gemini'`).
28
+ * **confidence** (`number`): The confidence score of the transcription (currently hardcoded to `1.0`).
29
+
30
+ ### Error Responses
31
+
32
+ * **400 Bad Request**: This error is returned if the `imageBase64` property is missing from the request body.
33
+ * **500 Internal Server Error**: This error is returned if there is an issue with the transcription process on the server, such as an error from the Gemini API.
34
+
35
+ ## Environment Variables
36
+
37
+ This function requires the following environment variable to be set in your Netlify environment:
38
+
39
+ * `GEMINI_API_KEY`: Your API key for the Gemini service.
40
+
41
+ ## CORS
42
+
43
+ The function is configured to allow cross-origin requests from any origin.