@teachinglab/omd 0.6.7 → 0.7.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.
@@ -37,6 +37,10 @@ export class ResizeHandleManager {
37
37
  if (!element || !element.classList.contains('omd-item')) {
38
38
  return;
39
39
  }
40
+
41
+ if (element?.dataset?.locked === 'true') {
42
+ return;
43
+ }
40
44
 
41
45
  this.selectedElement = element;
42
46
  this._createSelectionBorder();
@@ -72,7 +72,10 @@ export class SelectTool extends Tool {
72
72
  }
73
73
 
74
74
  const segmentSelection = this._findSegmentAtPoint(event.x, event.y);
75
- const omdElement = this._findOMDElementAtPoint(event.x, event.y);
75
+ let omdElement = this._findOMDElementAtPoint(event.x, event.y);
76
+ if (omdElement?.dataset?.locked === 'true') {
77
+ omdElement = null;
78
+ }
76
79
 
77
80
  if (segmentSelection) {
78
81
  // Clicking on a stroke segment - clear OMD selection and handle segment selection
@@ -473,8 +476,11 @@ export class SelectTool extends Tool {
473
476
 
474
477
  // Check all OMD items in the layer
475
478
  const omdItems = omdLayer.querySelectorAll('.omd-item');
476
-
479
+
477
480
  for (const item of omdItems) {
481
+ if (item?.dataset?.locked === 'true') {
482
+ continue;
483
+ }
478
484
  try {
479
485
  // Get the bounding box of the item
480
486
  const bbox = item.getBBox();
@@ -11,6 +11,9 @@ export class Toolbar {
11
11
  this.buttons = new Map();
12
12
  this.activeButton = null;
13
13
  this.omdColor = omdColor; // Use omdColor for consistent styling
14
+ this.toolbarWidth = 64;
15
+ this.toolbarHeight = 28;
16
+ this.customPosition = null;
14
17
 
15
18
 
16
19
 
@@ -44,8 +47,7 @@ export class Toolbar {
44
47
 
45
48
 
46
49
  // Initial size, will be updated after buttons are created
47
- this.background.setWidthAndHeight(100, 54);
48
- this.background.setCornerRadius(27); // Pill shape
50
+ this._setToolbarSize(this.toolbarWidth, this.toolbarHeight);
49
51
  this.background.setFillColor(this.omdColor.mediumGray); // Modern dark, semi-transparent
50
52
 
51
53
 
@@ -58,7 +60,7 @@ export class Toolbar {
58
60
 
59
61
 
60
62
  // Position the toolbar at bottom center
61
- this._updatePosition();``
63
+ this._updatePosition();
62
64
 
63
65
  // Add to main SVG so it is rendered
64
66
  this.canvas.svg.appendChild(this.toolbarGroup.svgObject);
@@ -70,16 +72,12 @@ export class Toolbar {
70
72
  */
71
73
  _updatePosition() {
72
74
  const canvasRect = this.canvas.container.getBoundingClientRect();
73
- const toolbarWidth = this.background.width;
74
- const toolbarHeight = this.background.height;
75
+ const toolbarWidth = this.toolbarWidth;
76
+ const toolbarHeight = this.toolbarHeight;
75
77
  // Bottom center, 24px from bottom
76
78
  const x = (canvasRect.width - toolbarWidth) / 2;
77
79
  const y = canvasRect.height - toolbarHeight - 24;
78
-
79
- // Ensure toolbar stays within canvas bounds
80
- const clampedX = Math.max(0, Math.min(x, canvasRect.width - toolbarWidth));
81
- const clampedY = Math.max(0, Math.min(y, canvasRect.height - toolbarHeight));
82
- this.toolbarGroup.setPosition(x, y);
80
+ this._applyPosition(x, y);
83
81
 
84
82
  // Debug the SVG object
85
83
 
@@ -88,14 +86,7 @@ export class Toolbar {
88
86
  this.toolbarGroup.svgObject.style.pointerEvents = 'auto';
89
87
 
90
88
  // Fix the viewBox to include the toolbar position
91
- const bgWidth = this.background.width;
92
- const bgHeight = this.background.height;
93
- const viewBoxX = x;
94
- const viewBoxY = y;
95
- const viewBoxWidth = Math.max(500, x + bgWidth);
96
- const viewBoxHeight = Math.max(500, y + bgHeight);
97
-
98
- this.toolbarGroup.svgObject.setAttribute('viewBox', `${viewBoxX} ${viewBoxY} ${viewBoxWidth} ${viewBoxHeight}`);
89
+ this._updateViewBox(x, y);
99
90
 
100
91
  // Don't set x/y attributes - let setPosition handle it via transform
101
92
  // this.toolbarGroup.svgObject.setAttribute('x', x);
@@ -109,9 +100,9 @@ export class Toolbar {
109
100
  _createToolButtons() {
110
101
  const tools = this.canvas.toolManager.getAllToolMetadata();
111
102
 
112
- const buttonSize = 48;
113
- const spacing = 8;
114
- const padding = 6;
103
+ const buttonSize = 24;
104
+ const spacing = 4;
105
+ const padding = 4;
115
106
  let xPos = padding;
116
107
  const yPos = padding;
117
108
  tools.forEach(toolMeta => {
@@ -124,8 +115,7 @@ export class Toolbar {
124
115
  // Remove last spacing
125
116
  const totalWidth = xPos - spacing + padding;
126
117
  const totalHeight = buttonSize + 2 * padding;
127
- this.background.setWidthAndHeight(totalWidth, totalHeight);
128
- this.background.setCornerRadius(totalHeight / 2);
118
+ this._setToolbarSize(totalWidth, totalHeight);
129
119
  // Reposition after sizing
130
120
  this._updatePosition();
131
121
 
@@ -215,6 +205,33 @@ export class Toolbar {
215
205
  this.activeButton = null;
216
206
  }
217
207
  }
208
+
209
+ _setToolbarSize(width, height) {
210
+ this.toolbarWidth = width;
211
+ this.toolbarHeight = height;
212
+ this.background.setWidthAndHeight(width, height);
213
+ this.background.setCornerRadius(height / 2);
214
+ }
215
+
216
+ _applyPosition(x, y) {
217
+ const canvasRect = this.canvas.container.getBoundingClientRect();
218
+ const clampedX = Math.max(0, Math.min(x, canvasRect.width - this.toolbarWidth));
219
+ const clampedY = Math.max(0, Math.min(y, canvasRect.height - this.toolbarHeight));
220
+ this.customPosition = { x: clampedX, y: clampedY };
221
+ this.toolbarGroup.setPosition(clampedX, clampedY);
222
+ this._updateViewBox(clampedX, clampedY);
223
+ }
224
+
225
+ _updateViewBox(x, y) {
226
+ this.toolbarGroup.svgObject.setAttribute('viewBox', `${x} ${y} ${this.toolbarWidth} ${this.toolbarHeight}`);
227
+ }
228
+
229
+ setBoundaryPosition(lineY) {
230
+ const canvasRect = this.canvas.container.getBoundingClientRect();
231
+ const x = (canvasRect.width - this.toolbarWidth) / 2;
232
+ const y = lineY - this.toolbarHeight;
233
+ this._applyPosition(x, y);
234
+ }
218
235
 
219
236
  /**
220
237
  * Add custom button to toolbar
package/index.js CHANGED
@@ -29,6 +29,7 @@ export { EventManager } from './canvas/events/eventManager.js';
29
29
 
30
30
  // Export utility components
31
31
  export { omdNodeOverlay, omdNodeOverlayPresets } from './omd/utils/omdNodeOverlay.js';
32
+ export { omdTranscriptionService } from './omd/utils/omdTranscriptionService.js';
32
33
 
33
34
  // Re-export the most commonly used components for easy access
34
35
  export { omdTable } from './src/omdTable.js';
@@ -77,4 +78,4 @@ export default {
77
78
  const { omdDisplay } = await import('./omd/core/index.js');
78
79
  return new omdDisplay(container);
79
80
  }
80
- };
81
+ };
@@ -1,4 +1,5 @@
1
1
  import { jsvgGroup, jsvgRect, jsvgTextLine, jsvgImage, jsvgClipMask } from './jsvg.js';
2
+ export * from './jsvg.js';
2
3
 
3
4
  // ================ jsvgButton ================================= //
4
5
 
@@ -355,4 +356,4 @@ export class jsvgScrollbox extends jsvgGroup
355
356
  }
356
357
  }
357
358
 
358
- }
359
+ }
@@ -460,54 +460,39 @@ Tabular data display with customizable styling.
460
460
 
461
461
  #### `omdEquation`
462
462
 
463
- Complete mathematical equations with left and right sides.
463
+ Complete mathematical equations. The visual component now uses the same math.js-powered parser and renderer as the interactive core (functions, rationals, roots, etc.).
464
464
 
465
- **Schema (String Form):**
465
+ **Preferred schema (string form):**
466
466
  ```json
467
467
  {
468
- "equation": "2x + 3 = 11"
468
+ "equation": "sin(x) + 2 = 3"
469
469
  }
470
470
  ```
471
471
 
472
- **Schema (Structured Form):**
472
+ **Structured fallback (legacy):**
473
473
  ```typescript
474
474
  {
475
- leftExpression: Expression | string,
476
- rightExpression: Expression | string,
477
- equation?: string
475
+ leftExpression?: Expression | string,
476
+ rightExpression?: Expression | string,
477
+ equation: string
478
478
  }
479
479
  ```
480
480
 
481
481
  **Examples:**
482
482
  ```json
483
- {
484
- "equation": "2x + 3 = 11"
485
- }
483
+ { "equation": "sin(x) + 2 = 3" }
486
484
  ```
487
-
488
485
  ```json
489
- {
490
- "leftExpression": {
491
- "termsAndOpers": [
492
- { "omdType": "term", "coefficient": 2, "variable": "x" },
493
- { "omdType": "operator", "operator": "+" },
494
- { "omdType": "number", "value": 3 }
495
- ]
496
- },
497
- "rightExpression": {
498
- "omdType": "number",
499
- "value": 11
500
- }
501
- }
486
+ { "equation": "(x^2 + 3x - 4)/(2x) = 5" }
502
487
  ```
503
488
 
504
489
  **Usage:**
505
490
  ```javascript
506
- // Simple string form
491
+ // Preferred string form (math.js parsing)
507
492
  const eq = new omdEquation();
508
- eq.loadFromJSON({ equation: '2x + 3 = 11' });
493
+ eq.loadFromJSON({ equation: 'sqrt(x+1) = 4', fontSize: 32 });
509
494
 
510
- // Structured form
495
+ // Legacy structured form (if you already have parsed pieces)
511
496
  eq.loadFromJSON({
512
497
  leftExpression: { omdType: 'term', coefficient: 2, variable: 'x' },
513
498
  rightExpression: { omdType: 'number', value: 11 }
@@ -55,6 +55,8 @@ export class omdDisplay {
55
55
  const height = this.container.offsetHeight || 600;
56
56
 
57
57
  this.svg.setViewbox(width, height);
58
+ this.svg.svgObject.style.width = '100%';
59
+ this.svg.svgObject.style.height = '100%';
58
60
  this.svg.svgObject.style.verticalAlign = "middle";
59
61
  // Enable internal scrolling via native SVG scrolling if content overflows
60
62
  this.svg.svgObject.style.overflow = 'hidden';
@@ -768,18 +770,7 @@ export class omdDisplay {
768
770
  render(expression) {
769
771
  // Clear previous node
770
772
  if (this.node) {
771
- if (this._contentGroup && this.node && this.node.svgObject) {
772
- try {
773
- if (this.node.svgObject.parentNode === this._contentGroup) {
774
- this._contentGroup.removeChild(this.node.svgObject);
775
- }
776
- } catch (e) {
777
- // Fallback to svg remove
778
- this.svg.removeChild(this.node);
779
- }
780
- } else {
781
- this.svg.removeChild(this.node);
782
- }
773
+ this.removeChild(this.node);
783
774
  }
784
775
 
785
776
  // Create node from expression
@@ -992,7 +983,7 @@ export class omdDisplay {
992
983
  */
993
984
  clear() {
994
985
  if (this.node) {
995
- this.svg.removeChild(this.node);
986
+ this.removeChild(this.node);
996
987
  this.node = null;
997
988
  }
998
989
  }
@@ -100,6 +100,7 @@ export class omdFunctionNode extends omdNode {
100
100
  // Calculate dimensions using getTextBounds for consistency
101
101
  const ratio = fontSize / this.getRootFontSize();
102
102
  const spacing = 2 * ratio;
103
+ const parenSpacing = 2 * ratio; // Spacing between function name and opening parenthesis
103
104
 
104
105
  const functionNameBounds = getTextBounds(this.functionName, fontSize);
105
106
  const openParenBounds = getTextBounds('(', fontSize);
@@ -117,7 +118,7 @@ export class omdFunctionNode extends omdNode {
117
118
  }
118
119
  });
119
120
 
120
- const totalWidth = functionNameBounds.width + openParenBounds.width + totalArgWidth + closeParenBounds.width + (spacing * 2);
121
+ const totalWidth = functionNameBounds.width + parenSpacing + openParenBounds.width + totalArgWidth + closeParenBounds.width + (spacing * 2);
121
122
  const totalHeight = Math.max(maxArgHeight, functionNameBounds.height, openParenBounds.height, closeParenBounds.height) + 4 * ratio;
122
123
 
123
124
  this.setWidthAndHeight(totalWidth, totalHeight);
@@ -132,6 +133,7 @@ export class omdFunctionNode extends omdNode {
132
133
  const argFontSize = fontSize * 5/6;
133
134
  const ratio = fontSize / this.getRootFontSize();
134
135
  const spacing = 2 * ratio;
136
+ const parenSpacing = 2 * ratio; // Spacing between function name and opening parenthesis
135
137
 
136
138
  let currentX = 0;
137
139
  const textY = this.height / 2;
@@ -140,6 +142,8 @@ export class omdFunctionNode extends omdNode {
140
142
  this.functionNameElement.setPosition(currentX, textY);
141
143
  currentX += getTextBounds(this.functionName, fontSize).width;
142
144
 
145
+ currentX += parenSpacing;
146
+
143
147
  // Position opening parenthesis
144
148
  this.openParenElement.setPosition(currentX, textY);
145
149
  currentX += getTextBounds('(', fontSize).width;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teachinglab/omd",
3
- "version": "0.6.7",
3
+ "version": "0.7.0",
4
4
  "description": "omd",
5
5
  "main": "./index.js",
6
6
  "module": "./index.js",
package/src/index.js CHANGED
@@ -26,6 +26,12 @@ export { omdFunction } from './omdFunction.js';
26
26
  export { omdApp } from './omdApp.js';
27
27
  export { omdAppCanvas } from './omdAppCanvas.js';
28
28
  export { omd } from './omd.js';
29
+ export { omdDisplay } from '../omd/display/omdDisplay.js';
30
+ export { omdEquationNode } from '../omd/nodes/omdEquationNode.js';
31
+ export { omdEquationStack } from '../omd/core/omdEquationStack.js';
32
+ export { omdEquationSequenceNode } from '../omd/nodes/omdEquationSequenceNode.js';
33
+ export { omdConstantNode } from '../omd/nodes/omdConstantNode.js';
34
+ export { omdNodeOverlay, omdNodeOverlayPresets } from '../omd/utils/omdNodeOverlay.js';
29
35
 
30
36
  // OMD Utilities and Helpers
31
37
  export { omdColor } from './omdColor.js';
@@ -73,6 +79,11 @@ export default {
73
79
  app: {
74
80
  omdApp: () => import('./omdApp.js').then(m => m.omdApp),
75
81
  omdAppCanvas: () => import('./omdAppCanvas.js').then(m => m.omdAppCanvas),
76
- omd: () => import('./omd.js').then(m => m.omd)
82
+ omd: () => import('./omd.js').then(m => m.omd),
83
+ async createDisplay(container) {
84
+ const { omdDisplay } = await import('../omd/core/index.js');
85
+ return new omdDisplay(container);
86
+ }
77
87
  }
78
- };
88
+ };
89
+ // Force reload
@@ -677,9 +677,16 @@ This document provides schemas and examples for the `loadFromJSON` method used i
677
677
 
678
678
  ## 20. `omdEquation`
679
679
 
680
- `omdEquation` represents a mathematical equation, such as "x + 2 = 5".
680
+ `omdEquation` represents a mathematical equation and now uses the same math.js-powered parser/renderer as the interactive core (functions, rationals, roots, etc.).
681
681
 
682
- ### Schema
682
+ ### Preferred schema (string form)
683
+ ```json
684
+ {
685
+ "equation": "sin(x) + 2 = 3"
686
+ }
687
+ ```
688
+
689
+ ### Structured fallback (legacy)
683
690
  ```json
684
691
  {
685
692
  "leftExpression": "object",
@@ -688,11 +695,10 @@ This document provides schemas and examples for the `loadFromJSON` method used i
688
695
  }
689
696
  ```
690
697
 
691
- ### Example
698
+ ### Examples
692
699
  ```json
693
- {
694
- "leftExpression": { "omdType": "term", "coefficient": 1, "variable": "x", "exponent": 1 },
695
- "rightExpression": { "omdType": "number", "value": 5 },
696
- "equation": "x + 2 = 5"
697
- }
698
- ```
700
+ { "equation": "sin(x) + 2 = 3" }
701
+ ```
702
+ ```json
703
+ { "equation": "(x^2 + 3x - 4)/(2x) = 5" }
704
+ ```
@@ -9,6 +9,7 @@ import { omdVariable } from "./omdVariable.js";
9
9
  import { omdString } from "./omdString.js";
10
10
  import { omdNumber } from "./omdNumber.js";
11
11
  import { parseEquationString } from "./omdUtils.js";
12
+ import { omdEquationNode } from "../omd/nodes/omdEquationNode.js";
12
13
 
13
14
  export class omdEquation extends omdMetaExpression
14
15
  {
@@ -37,12 +38,24 @@ export class omdEquation extends omdMetaExpression
37
38
 
38
39
  this.rightHolder = new jsvgGroup();
39
40
  this.equationStack.addChild( this.rightHolder );
41
+
42
+ this.equationNode = null;
40
43
  }
41
44
 
42
45
  // make an equation (x + 2) = (2x - 3)
43
46
 
44
47
  loadFromJSON( data )
45
48
  {
49
+ // Prefer math.js parsing into omdEquationNode for richer rendering (functions, rationals, roots)
50
+ if (typeof data.equation === 'string' && data.equation.trim()) {
51
+ try {
52
+ this._renderWithEquationNode(data.equation, data.fontSize);
53
+ return;
54
+ } catch (e) {
55
+ console.warn('⚠️ omdEquation math.js render failed, falling back to legacy parsing:', e?.message || e);
56
+ }
57
+ }
58
+
46
59
  // Helper function to fix operator symbols in termsAndOpers arrays
47
60
  function fixOperatorSymbols(expressionData) {
48
61
  if (expressionData && expressionData.termsAndOpers && Array.isArray(expressionData.termsAndOpers)) {
@@ -136,6 +149,37 @@ export class omdEquation extends omdMetaExpression
136
149
  this.updateLayout();
137
150
  }
138
151
 
152
+ _renderWithEquationNode(equationString, fontSize) {
153
+ if (typeof math === 'undefined' || typeof math.parse !== 'function') {
154
+ throw new Error('math.js is required to parse equation strings');
155
+ }
156
+
157
+ const eqNode = omdEquationNode.fromString(equationString);
158
+ if (typeof fontSize === 'number' && eqNode.setFontSize) {
159
+ eqNode.setFontSize(fontSize);
160
+ }
161
+
162
+ if (eqNode.hideBackgroundByDefault) eqNode.hideBackgroundByDefault();
163
+ if (eqNode.computeDimensions) eqNode.computeDimensions();
164
+ if (eqNode.updateLayout) eqNode.updateLayout();
165
+
166
+ // Clear the legacy stack and render just the parsed node
167
+ if (typeof this.equationStack.removeAllChildren === 'function') {
168
+ this.equationStack.removeAllChildren();
169
+ } else {
170
+ this.equationStack.childList = [];
171
+ }
172
+ this.equationStack.addChild(eqNode);
173
+ this.equationStack.setSpacer(0);
174
+
175
+ this.equationNode = eqNode;
176
+ this.leftExpression = null;
177
+ this.rightExpression = null;
178
+
179
+ this.centerEquation = false;
180
+ this.updateLayout();
181
+ }
182
+
139
183
 
140
184
  setLeftAndRightExpressions( leftExp, rightExp )
141
185
  {
@@ -156,6 +200,25 @@ export class omdEquation extends omdMetaExpression
156
200
 
157
201
  updateLayout()
158
202
  {
203
+ if (this.equationNode) {
204
+ const node = this.equationNode;
205
+ if (node.computeDimensions) node.computeDimensions();
206
+ if (node.updateLayout) node.updateLayout();
207
+
208
+ this.equationStack.doHorizontalLayout();
209
+ this.equationStack.setPosition(this.inset, this.inset);
210
+
211
+ const W = node.width || this.equationStack.width;
212
+ const H = node.height || this.equationStack.height;
213
+
214
+ this.backRect.setWidthAndHeight( W + this.inset*2, H + this.inset*2 );
215
+ this.setWidthAndHeight( this.backRect.width, this.backRect.height );
216
+ this.width = this.backRect.width;
217
+ this.height = this.backRect.height;
218
+ this.svgObject.setAttribute('viewBox', `0 0 ${this.width} ${this.height}`);
219
+ return;
220
+ }
221
+
159
222
  this.leftHolder.setWidthAndHeight( this.leftExpression.width, this.leftExpression.height );
160
223
  this.rightHolder.setWidthAndHeight( this.rightExpression.width, this.rightExpression.height );
161
224
 
@@ -186,4 +249,4 @@ export class omdEquation extends omdMetaExpression
186
249
  }
187
250
  }
188
251
 
189
- }
252
+ }