-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
·291 lines (264 loc) · 8.46 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//
// File: main.js
//
// Description: This file defines the math mode for FoldingText. It
// processes the math lines using mathjs library. The
// library should be in the same directory as the extension.
//
// Author: Richard A. Guay
// Author Email: raguay@customct.com
//
define(function(require, exports, module) {
'use strict';
//
// Load the libraries I need to use.
//
var Extensions = require('ft/core/extensions').Extensions;
var mathjs = require("./mathjs.js");
//
// Add the 'imath' mode and 'plot' mode extension to the system.
//
Extensions.addMode({
name: 'imath'
});
Extensions.addMode({
name: 'plot'
})
//
// Function: inMathArea
//
// Description: This function determines if the current node is in
// a math area and is not empty.
//
function inMathArea(node) {
if ((node.modeContext() === 'imath') && (node.text().trim() != "")) {
return (true);
} else {
return (false);
}
};
//
// Function: inPlotArea
//
// Description: This function determines if the current node is in
// a plot area and is not empty.
//
function inPlotArea(node) {
if ((node.modeContext() === 'plot') && (node.text().trim() != "")) {
return (true);
} else {
return (false);
}
};
//
// Function: Calculate
//
// Description: This function get the current node and the current node's
// string contents. It returns the result of the calculation.
//
function Calculate(str) {
var result = 0,
scope = {};
try {
//
// Use the Mathjs library to evaluate the equation.
//
var lines = str.substr(0, str.length - 2).split("\n");
lines.forEach(function(line) {
var node, code;
code = mathjs.compile(line);
result = code.eval(scope);
});
} catch (e) {
//
// Mathjs had a problem with the expressions. Return an ?
//
result = "?" + " - " + e.toString();
}
return (result);
};
function ProcessPreviousNodes(node) {
var pnode = node,
text = "",
result = "";
while (pnode && (!pnode.mode()) && (pnode.modeContext() === 'imath')) {
//
// Not a heading, see if it has an evaluate command.
//
text = pnode.text();
if (text.search("=>") < 0) {
//
// No evaluation, add it to the rest.
//
result = text + "\n" + result;
}
pnode = pnode.previousBranch();
}
return (result);
}
//
// Function: ProcessMathNode
//
// Description: This function is used to process a node in the math
// context. It expects the node to be passed to it.
//
function ProcessMathNode(node) {
//
// Calculate if needed. Get the text of the line.
//
var result = node.text();
//
// See if it ends in "=>". If so, process the line.
//
if (result.substr(-2) == "=>") {
//
// See if some of the previous lines had
// variable declarations.
//
result = ProcessPreviousNodes(node) + "\n" + result;
//
// See if other areas have variable definitions.
//
var pnode = node.parent.previousBranch();
while (pnode) {
if (pnode.modeContext() === 'imath') {
if (!editor.nodeIsHiddenInFold(pnode) && !editor.isCollapsed(pnode)) {
result = ProcessPreviousNodes(pnode.lastChild) + "\n" + result;
}
}
pnode = pnode.previousBranch();
}
//
// Calculate the result. If Calculate returns an array, there were
// variables figured in as well. Just get the final result.
//
var cresult = Calculate(result);
if (isArray(cresult)) {
//
// We will get more than on answer back. Therefore,
// just get the last result.
//
cresult = cresult[cresult.length - 1];
}
//
// Put the result together with the original line.
//
result = node.text() + " " + cresult;
//
// Update the line.
//
node.setText(result);
}
};
//
// Function: ProcessMathNode
//
// Description: This function is used to process a node in the math
// context. It expects the node to be passed to it.
//
function ProcessPlotNode(node) {
//
// Calculate if needed. Get the text of the line.
//
var result = node.text();
};
//
// Add a TreeChanged event handler to figure out when to run
// a calculation.
//
Extensions.addTreeChanged(function(editor, e) {
var deltas = e.deltas;
for (var i = 0; i < deltas.length; i++) {
var updatedNode = deltas[i].updatedNode,
insertedNodes = deltas[i].insertedNodes,
length = 0;
//
// Check all the inserted nodes.
//
length = insertedNodes.length;
for (var j = 0; j < length; j++) {
var each = insertedNodes[j];
if (inMathArea(each)) {
//
// It's a math node. Process it.
//
ProcessMathNode(each);
} else if (inPlotArea(each)) {
//
// It's a plot node. Prcess it.
//
ProcessPlotNode(each);
}
}
//
// Check the updated Nodes.
//
if (updatedNode) {
//
// It is an updated Node. Make sure it is in the math area.
//
if (inMathArea(updatedNode)) {
//
// It's a math node. Process it.
//
ProcessMathNode(updatedNode);
}else if (inPlotArea(updatedNode)) {
//
// It's a plot node. Process it.
//
ProcessPlotNode(updatedNode);
}
}
}
});
function fun1(x) {
return Math.sin(x);
}
function fun2(x) {
return Math.cos(3 * x);
}
function draw(canvas) {
if (null == canvas || !canvas.getContext) return;
var axes = {},
ctx = canvas.getContext("2d");
axes.x0 = .5 + .5 * canvas.width; // x0 pixels from left to x=0
axes.y0 = .5 + .5 * canvas.height; // y0 pixels from top to y=0
axes.scale = 40; // 40 pixels from x=0 to x=1
axes.doNegativeX = true;
showAxes(ctx, axes);
funGraph(ctx, axes, fun1, "rgb(11,153,11)", 1);
funGraph(ctx, axes, fun2, "rgb(66,44,255)", 2);
}
function funGraph(ctx, axes, func, color, thick) {
var xx, yy, dx = 4,
x0 = axes.x0,
y0 = axes.y0,
scale = axes.scale;
var iMax = Math.round((ctx.canvas.width - x0) / dx);
var iMin = axes.doNegativeX ? Math.round(-x0 / dx) : 0;
ctx.beginPath();
ctx.lineWidth = thick;
ctx.strokeStyle = color;
for (var i = iMin; i <= iMax; i++) {
xx = dx * i;
yy = scale * func(xx / scale);
if (i == iMin) ctx.moveTo(x0 + xx, y0 - yy);
else ctx.lineTo(x0 + xx, y0 - yy);
}
ctx.stroke();
}
function showAxes(ctx, axes) {
var x0 = axes.x0,
w = ctx.canvas.width;
var y0 = axes.y0,
h = ctx.canvas.height;
var xmin = axes.doNegativeX ? 0 : x0;
ctx.beginPath();
ctx.strokeStyle = "rgb(128,128,128)";
ctx.moveTo(xmin, y0);
ctx.lineTo(w, y0); // X axis
ctx.moveTo(x0, 0);
ctx.lineTo(x0, h); // Y axis
ctx.stroke();
}
});