@teachinglab/omd 0.3.7 → 0.3.9

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.
@@ -1,157 +1,189 @@
1
-
2
- import { omdColor } from "./omdColor.js";
3
- import { jsvgLayoutGroup, jsvgGroup } from "@teachinglab/jsvg";
4
- import { omdOperator } from "./omdOperator.js";
5
- import { omdMetaExpression } from "./omdMetaExpression.js"
6
- import { omdTerm } from "./omdTerm.js";
7
- import { omdExpression } from "./omdExpression.js";
8
- import { omdVariable } from "./omdVariable.js";
9
- import { omdString } from "./omdString.js";
10
- import { omdNumber } from "./omdNumber.js";
11
-
12
- export class omdEquation extends omdMetaExpression
13
- {
14
- constructor()
15
- {
16
- // initialization
17
- super();
18
-
19
- this.type = "omdPowerExpression";
20
-
21
- this.leftExpression = null;
22
- this.rightExpression = null;
23
-
24
- this.centerEquation = true;
25
- this.inset = 5;
26
-
27
- this.equationStack = new jsvgLayoutGroup();
28
- this.equationStack.setSpacer(-7);
29
- this.addChild( this.equationStack );
30
-
31
- this.leftHolder = new jsvgGroup();
32
- this.equationStack.addChild( this.leftHolder );
33
-
34
- this.equalSign = new omdOperator('=');
35
- this.equationStack.addChild( this.equalSign );
36
-
37
- this.rightHolder = new jsvgGroup();
38
- this.equationStack.addChild( this.rightHolder );
39
- }
40
-
41
- // make an equation (x + 2) = (2x - 3)
42
-
43
- loadFromJSON( data )
44
- {
45
- // If explicit left/right JSON provided, build them according to their omdType
46
- if ( typeof data.leftExpression != "undefined" && data.leftExpression )
47
- {
48
- const left = data.leftExpression;
49
- if ( left.omdType == "expression" ) this.leftExpression = new omdExpression();
50
- else if ( left.omdType == "number" ) this.leftExpression = new omdNumber();
51
- else if ( left.omdType == "variable" ) this.leftExpression = new omdVariable();
52
- else if ( left.omdType == "term" ) this.leftExpression = new omdTerm();
53
- else if ( left.omdType == "string" || typeof left == 'string' ) this.leftExpression = new omdString( typeof left == 'string' ? left : left.name || '' );
54
-
55
- if (this.leftExpression && typeof this.leftExpression.loadFromJSON === 'function' && left && typeof left === 'object') {
56
- this.leftExpression.loadFromJSON( left );
57
- }
58
-
59
- if (this.leftExpression) {
60
- this.leftHolder.removeAllChildren();
61
- this.leftHolder.addChild( this.leftExpression );
62
- }
63
- }
64
-
65
- if ( typeof data.rightExpression != "undefined" && data.rightExpression )
66
- {
67
- const right = data.rightExpression;
68
- if ( right.omdType == "expression" ) this.rightExpression = new omdExpression();
69
- else if ( right.omdType == "number" ) this.rightExpression = new omdNumber();
70
- else if ( right.omdType == "variable" ) this.rightExpression = new omdVariable();
71
- else if ( right.omdType == "term" ) this.rightExpression = new omdTerm();
72
- else if ( right.omdType == "string" || typeof right == 'string' ) this.rightExpression = new omdString( typeof right == 'string' ? right : right.name || '' );
73
-
74
- if (this.rightExpression && typeof this.rightExpression.loadFromJSON === 'function' && right && typeof right === 'object') {
75
- this.rightExpression.loadFromJSON( right );
76
- }
77
-
78
- if (this.rightExpression) {
79
- this.rightHolder.removeAllChildren();
80
- this.rightHolder.addChild( this.rightExpression );
81
- }
82
- }
83
-
84
- // If no structured left/right provided but an `equation` string exists, parse it into simple string nodes
85
- if ( (!this.leftExpression || !this.rightExpression) && typeof data.equation === 'string' ) {
86
- const eq = data.equation || '';
87
- const parts = eq.split('=');
88
- const leftStr = (parts[0] || '').trim();
89
- const rightStr = (parts[1] || '').trim();
90
-
91
- if (!this.leftExpression) this.leftExpression = new omdString(leftStr || '');
92
- if (!this.rightExpression) this.rightExpression = new omdString(rightStr || '');
93
-
94
- if (this.leftExpression) {
95
- this.leftHolder.removeAllChildren();
96
- this.leftHolder.addChild(this.leftExpression);
97
- }
98
- if (this.rightExpression) {
99
- this.rightHolder.removeAllChildren();
100
- this.rightHolder.addChild(this.rightExpression);
101
- }
102
- }
103
-
104
- // Guarded calls to hide backgrounds only when components exist
105
- if (this.equalSign && typeof this.equalSign.hideBackgroundByDefault === 'function') this.equalSign.hideBackgroundByDefault();
106
- if (this.leftExpression && typeof this.leftExpression.hideBackgroundByDefault === 'function') this.leftExpression.hideBackgroundByDefault();
107
- if (this.rightExpression && typeof this.rightExpression.hideBackgroundByDefault === 'function') this.rightExpression.hideBackgroundByDefault();
108
-
109
- this.centerEquation = false;
110
- this.updateLayout();
111
- }
112
-
113
-
114
- setLeftAndRightExpressions( leftExp, rightExp )
115
- {
116
- this.leftExpression = leftExp;
117
- this.leftHolder.removeAllChildren();
118
- this.leftHolder.addChild( leftExp );
119
-
120
- this.rightExpression = rightExp;
121
- this.rightHolder.removeAllChildren();
122
- this.rightHolder.addChild( rightExp );
123
-
124
- this.equalSign.hideBackgroundByDefault();
125
- this.leftExpression.hideBackgroundByDefault();
126
- this.rightExpression.hideBackgroundByDefault();
127
-
128
- this.updateLayout();
129
- }
130
-
131
- updateLayout()
132
- {
133
- this.leftHolder.setWidthAndHeight( this.leftExpression.width, this.leftExpression.height );
134
- this.rightHolder.setWidthAndHeight( this.rightExpression.width, this.rightExpression.height );
135
-
136
- this.equationStack.doHorizontalLayout();
137
- this.equationStack.setPosition( this.inset, 0 );
138
-
139
- var W = this.equationStack.width;
140
- this.backRect.setWidthAndHeight( W + this.inset*2, 30 );
141
-
142
- this.setWidthAndHeight( this.backRect.width, this.backRect.height );
143
-
144
- // Set individual width/height properties and viewBox for API compatibility
145
- this.width = this.backRect.width;
146
- this.height = this.backRect.height;
147
- this.svgObject.setAttribute('viewBox', `0 0 ${this.width} ${this.height}`);
148
-
149
- if ( this.centerEquation )
150
- {
151
- var leftShift = this.leftExpression.width + this.equalSign.width*0.50;
152
- this.backRect.setPosition( -1.0 * leftShift + this.inset/2, 0 );
153
- this.equationStack.setPosition( -1.0 * leftShift + this.inset + this.inset/2, 0 );
154
- }
155
- }
156
-
1
+
2
+ import { omdColor } from "./omdColor.js";
3
+ import { jsvgLayoutGroup, jsvgGroup } from "@teachinglab/jsvg";
4
+ import { omdOperator } from "./omdOperator.js";
5
+ import { omdMetaExpression } from "./omdMetaExpression.js"
6
+ import { omdTerm } from "./omdTerm.js";
7
+ import { omdExpression } from "./omdExpression.js";
8
+ import { omdVariable } from "./omdVariable.js";
9
+ import { omdString } from "./omdString.js";
10
+ import { omdNumber } from "./omdNumber.js";
11
+ import { parseEquationString } from "./omdUtils.js";
12
+
13
+ export class omdEquation extends omdMetaExpression
14
+ {
15
+ constructor()
16
+ {
17
+ // initialization
18
+ super();
19
+
20
+ this.type = "omdPowerExpression";
21
+
22
+ this.leftExpression = null;
23
+ this.rightExpression = null;
24
+
25
+ this.centerEquation = true;
26
+ this.inset = 5;
27
+
28
+ this.equationStack = new jsvgLayoutGroup();
29
+ this.equationStack.setSpacer(-7);
30
+ this.addChild( this.equationStack );
31
+
32
+ this.leftHolder = new jsvgGroup();
33
+ this.equationStack.addChild( this.leftHolder );
34
+
35
+ this.equalSign = new omdOperator('=');
36
+ this.equationStack.addChild( this.equalSign );
37
+
38
+ this.rightHolder = new jsvgGroup();
39
+ this.equationStack.addChild( this.rightHolder );
40
+ }
41
+
42
+ // make an equation (x + 2) = (2x - 3)
43
+
44
+ loadFromJSON( data )
45
+ {
46
+ // Helper function to fix operator symbols in termsAndOpers arrays
47
+ function fixOperatorSymbols(expressionData) {
48
+ if (expressionData && expressionData.termsAndOpers && Array.isArray(expressionData.termsAndOpers)) {
49
+ expressionData.termsAndOpers.forEach(item => {
50
+ if (item && item.omdType === 'operator' && item.symbol) {
51
+ // Ensure operator symbols are correct
52
+ if (item.symbol === '−') item.symbol = '-'; // Fix unicode minus
53
+ if (item.symbol === '') item.symbol = '+'; // Fix unicode plus
54
+ }
55
+ });
56
+ }
57
+ return expressionData;
58
+ }
59
+
60
+ // If explicit left/right JSON provided, build them according to their omdType
61
+ if ( typeof data.leftExpression != "undefined" && data.leftExpression )
62
+ {
63
+ let left = data.leftExpression;
64
+ // Fix operator symbols in left expression before processing
65
+ left = fixOperatorSymbols(left);
66
+
67
+ if ( left.omdType == "expression" ) this.leftExpression = new omdExpression();
68
+ else if ( left.omdType == "number" ) this.leftExpression = new omdNumber();
69
+ else if ( left.omdType == "variable" ) this.leftExpression = new omdVariable();
70
+ else if ( left.omdType == "term" ) this.leftExpression = new omdTerm();
71
+ else if ( left.omdType == "string" || typeof left == 'string' ) this.leftExpression = new omdExpression();
72
+
73
+ if (this.leftExpression && typeof this.leftExpression.loadFromJSON === 'function' && left && typeof left === 'object') {
74
+ this.leftExpression.loadFromJSON( left );
75
+ }
76
+
77
+ if (this.leftExpression) {
78
+ this.leftHolder.removeAllChildren();
79
+ this.leftHolder.addChild( this.leftExpression );
80
+ }
81
+ }
82
+
83
+ if ( typeof data.rightExpression != "undefined" && data.rightExpression )
84
+ {
85
+ let right = data.rightExpression;
86
+ // Fix operator symbols in right expression before processing
87
+ right = fixOperatorSymbols(right);
88
+
89
+ if ( right.omdType == "expression" ) this.rightExpression = new omdExpression();
90
+ else if ( right.omdType == "number" ) this.rightExpression = new omdNumber();
91
+ else if ( right.omdType == "variable" ) this.rightExpression = new omdVariable();
92
+ else if ( right.omdType == "term" ) this.rightExpression = new omdTerm();
93
+ else if ( right.omdType == "string" || typeof right == 'string' ) this.rightExpression = new omdExpression();
94
+
95
+ if (this.rightExpression && typeof this.rightExpression.loadFromJSON === 'function' && right && typeof right === 'object') {
96
+ this.rightExpression.loadFromJSON( right );
97
+ }
98
+
99
+ if (this.rightExpression) {
100
+ this.rightHolder.removeAllChildren();
101
+ this.rightHolder.addChild( this.rightExpression );
102
+ }
103
+ }
104
+
105
+ // If no structured left/right provided but an `equation` string exists, try to parse it into structured expressions
106
+ if ( (!this.leftExpression || !this.rightExpression) && typeof data.equation === 'string' ) {
107
+ const parsed = parseEquationString(data.equation);
108
+ if ( parsed ) {
109
+ if (!this.leftExpression) this.leftExpression = new omdExpression();
110
+ if (!this.rightExpression) this.rightExpression = new omdExpression();
111
+ // pass parsed structured JSON to expressions
112
+ if (this.leftExpression && typeof this.leftExpression.loadFromJSON === 'function') this.leftExpression.loadFromJSON(parsed.leftExpression);
113
+ if (this.rightExpression && typeof this.rightExpression.loadFromJSON === 'function') this.rightExpression.loadFromJSON(parsed.rightExpression);
114
+ this.leftHolder.removeAllChildren(); this.leftHolder.addChild(this.leftExpression);
115
+ this.rightHolder.removeAllChildren(); this.rightHolder.addChild(this.rightExpression);
116
+ } else {
117
+ // fallback: treat sides as plain strings
118
+ const parts = String(data.equation || '').split('=');
119
+ const leftStr = (parts[0] || '').trim();
120
+ const rightStr = (parts[1] || '').trim();
121
+ if (!this.leftExpression) this.leftExpression = new omdString(leftStr || '');
122
+ if (!this.rightExpression) this.rightExpression = new omdString(rightStr || '');
123
+ if (this.leftExpression && typeof this.leftExpression.loadFromJSON === 'function' && typeof leftStr === 'string') this.leftExpression.loadFromJSON(leftStr);
124
+ if (this.rightExpression && typeof this.rightExpression.loadFromJSON === 'function' && typeof rightStr === 'string') this.rightExpression.loadFromJSON(rightStr);
125
+ this.leftHolder.removeAllChildren(); this.leftHolder.addChild(this.leftExpression);
126
+ this.rightHolder.removeAllChildren(); this.rightHolder.addChild(this.rightExpression);
127
+ }
128
+ }
129
+
130
+ // Guarded calls to hide backgrounds only when components exist
131
+ if (this.equalSign && typeof this.equalSign.hideBackgroundByDefault === 'function') this.equalSign.hideBackgroundByDefault();
132
+ if (this.leftExpression && typeof this.leftExpression.hideBackgroundByDefault === 'function') this.leftExpression.hideBackgroundByDefault();
133
+ if (this.rightExpression && typeof this.rightExpression.hideBackgroundByDefault === 'function') this.rightExpression.hideBackgroundByDefault();
134
+
135
+ this.centerEquation = false;
136
+ this.updateLayout();
137
+ }
138
+
139
+
140
+ setLeftAndRightExpressions( leftExp, rightExp )
141
+ {
142
+ this.leftExpression = leftExp;
143
+ this.leftHolder.removeAllChildren();
144
+ this.leftHolder.addChild( leftExp );
145
+
146
+ this.rightExpression = rightExp;
147
+ this.rightHolder.removeAllChildren();
148
+ this.rightHolder.addChild( rightExp );
149
+
150
+ this.equalSign.hideBackgroundByDefault();
151
+ this.leftExpression.hideBackgroundByDefault();
152
+ this.rightExpression.hideBackgroundByDefault();
153
+
154
+ this.updateLayout();
155
+ }
156
+
157
+ updateLayout()
158
+ {
159
+ this.leftHolder.setWidthAndHeight( this.leftExpression.width, this.leftExpression.height );
160
+ this.rightHolder.setWidthAndHeight( this.rightExpression.width, this.rightExpression.height );
161
+
162
+ // Layout the horizontal equation stack and place it with both horizontal and vertical inset
163
+ this.equationStack.doHorizontalLayout();
164
+ // Position the stack inside the back rectangle using inset for both X and Y
165
+ this.equationStack.setPosition( this.inset, this.inset );
166
+
167
+ var W = this.equationStack.width;
168
+ var H = this.equationStack.height;
169
+
170
+ // Size backRect to include top/bottom inset so internal content has consistent padding
171
+ this.backRect.setWidthAndHeight( W + this.inset*2, H + this.inset*2 );
172
+
173
+ this.setWidthAndHeight( this.backRect.width, this.backRect.height );
174
+
175
+ // Set individual width/height properties and viewBox for API compatibility
176
+ this.width = this.backRect.width;
177
+ this.height = this.backRect.height;
178
+ this.svgObject.setAttribute('viewBox', `0 0 ${this.width} ${this.height}`);
179
+
180
+ if ( this.centerEquation )
181
+ {
182
+ var leftShift = this.leftExpression.width + this.equalSign.width*0.50;
183
+ // shift backRect and equationStack together but keep vertical inset
184
+ this.backRect.setPosition( -1.0 * leftShift + this.inset/2, 0 );
185
+ this.equationStack.setPosition( -1.0 * leftShift + this.inset + this.inset/2, this.inset );
186
+ }
187
+ }
188
+
157
189
  }
@@ -7,6 +7,7 @@ import { omdOperator } from "./omdOperator.js";
7
7
  import { omdNumber } from "./omdNumber.js";
8
8
  import { omdVariable } from "./omdVariable.js";
9
9
  import { omdMetaExpression } from "./omdMetaExpression.js"
10
+ import { parseExpressionString } from "./omdUtils.js";
10
11
 
11
12
  export class omdExpression extends omdMetaExpression
12
13
  {
@@ -29,6 +30,12 @@ export class omdExpression extends omdMetaExpression
29
30
 
30
31
  loadFromJSON( data )
31
32
  {
33
+ // Accept either structured object or a plain expression string
34
+ if ( typeof data === 'string' ) {
35
+ const parsed = parseExpressionString(data);
36
+ if ( parsed ) data = parsed;
37
+ }
38
+
32
39
  if ( typeof data.termsAndOpers != "undefined" )
33
40
  {
34
41
  for( var i=0; i<data.termsAndOpers.length; i++ )
@@ -64,8 +64,9 @@ export class omdFunction extends omdMetaExpression
64
64
 
65
65
  // format the f(x) part on the left
66
66
  this.leftHolder.removeAllChildren();
67
- var funcText = this.functionName + "(" + this.inputVariableArray[0] + ")"; // this should be refactored as a function call
68
- this.leftExpression = new omdString(funcText);
67
+ var funcText = this.functionName + "(" + this.inputVariableArray[0] + ")"; // this should be refactored as a function call
68
+ this.leftExpression = new omdExpression();
69
+ this.leftExpression.loadFromJSON(funcText);
69
70
  this.leftHolder.addChild( this.leftExpression );
70
71
 
71
72
  this.updateLayout();
@@ -75,7 +76,8 @@ export class omdFunction extends omdMetaExpression
75
76
  {
76
77
  this.leftHolder.removeAllChildren();
77
78
  var funcText = funcName + "(" + funcVariable + ")"; // this should be refactored as a function call
78
- this.leftExpression = new omdString(funcText);
79
+ this.leftExpression = new omdExpression();
80
+ this.leftExpression.loadFromJSON(funcText);
79
81
  this.leftHolder.addChild( this.leftExpression );
80
82
 
81
83
  this.rightExpresion = rightExp;