@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.
- package/canvas/core/canvasConfig.js +3 -3
- package/canvas/core/omdCanvas.js +479 -479
- package/canvas/events/eventManager.js +14 -4
- package/canvas/features/focusFrameManager.js +284 -286
- package/canvas/features/resizeHandleManager.js +482 -0
- package/canvas/index.js +2 -1
- package/canvas/tools/EraserTool.js +321 -322
- package/canvas/tools/PencilTool.js +321 -324
- package/canvas/tools/PointerTool.js +71 -0
- package/canvas/tools/SelectTool.js +902 -457
- package/canvas/tools/toolManager.js +389 -393
- package/canvas/ui/cursor.js +462 -437
- package/canvas/ui/toolbar.js +291 -290
- package/docs/omd-objects.md +258 -0
- package/jsvg/jsvg.js +898 -0
- package/jsvg/jsvgComponents.js +359 -0
- package/omd/nodes/omdEquationSequenceNode.js +1280 -1246
- package/package.json +1 -1
- package/src/json-schemas.md +546 -78
- package/src/omd.js +212 -109
- package/src/omdEquation.js +188 -156
- package/src/omdExpression.js +7 -0
- package/src/omdFunction.js +5 -3
- package/src/omdProblem.js +216 -11
- package/src/omdString.js +12 -1
- package/src/omdUtils.js +84 -0
- package/src/omdVariable.js +1 -0
package/src/omdEquation.js
CHANGED
|
@@ -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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
this.
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
this.
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
this.equationStack
|
|
29
|
-
this.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
this.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
this.
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
this.
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
{
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if (
|
|
69
|
-
else if (
|
|
70
|
-
else if (
|
|
71
|
-
else if (
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
this.
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (
|
|
92
|
-
if (
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
this.
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
this.rightHolder.
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
if (this.leftExpression && typeof
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
this.
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
this.
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
this.
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
this.
|
|
147
|
-
this.
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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
|
}
|
package/src/omdExpression.js
CHANGED
|
@@ -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++ )
|
package/src/omdFunction.js
CHANGED
|
@@ -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
|
-
|
|
68
|
-
|
|
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
|
|
79
|
+
this.leftExpression = new omdExpression();
|
|
80
|
+
this.leftExpression.loadFromJSON(funcText);
|
|
79
81
|
this.leftHolder.addChild( this.leftExpression );
|
|
80
82
|
|
|
81
83
|
this.rightExpresion = rightExp;
|